github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/clipboard/apis_js_wasm.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package clipboard 5 6 import ( 7 "github.com/primecitizens/pcz/std/core/abi" 8 "github.com/primecitizens/pcz/std/ffi/js" 9 "github.com/primecitizens/pcz/std/plat/js/webext/clipboard/bindings" 10 ) 11 12 type DataItemType uint32 13 14 const ( 15 _ DataItemType = iota 16 17 DataItemType_TEXT_PLAIN 18 DataItemType_TEXT_HTML 19 ) 20 21 func (DataItemType) FromRef(str js.Ref) DataItemType { 22 return DataItemType(bindings.ConstOfDataItemType(str)) 23 } 24 25 func (x DataItemType) String() (string, bool) { 26 switch x { 27 case DataItemType_TEXT_PLAIN: 28 return "textPlain", true 29 case DataItemType_TEXT_HTML: 30 return "textHtml", true 31 default: 32 return "", false 33 } 34 } 35 36 type AdditionalDataItem struct { 37 // Type is "AdditionalDataItem.type" 38 // 39 // Optional 40 Type DataItemType 41 // Data is "AdditionalDataItem.data" 42 // 43 // Optional 44 Data js.String 45 46 FFI_USE bool 47 } 48 49 // FromRef calls UpdateFrom and returns a AdditionalDataItem with all fields set. 50 func (p AdditionalDataItem) FromRef(ref js.Ref) AdditionalDataItem { 51 p.UpdateFrom(ref) 52 return p 53 } 54 55 // New creates a new AdditionalDataItem in the application heap. 56 func (p AdditionalDataItem) New() js.Ref { 57 return bindings.AdditionalDataItemJSLoad( 58 js.Pointer(&p), js.True, 0, 59 ) 60 } 61 62 // UpdateFrom copies value of all fields of the heap object to p. 63 func (p *AdditionalDataItem) UpdateFrom(ref js.Ref) { 64 bindings.AdditionalDataItemJSStore( 65 js.Pointer(p), ref, 66 ) 67 } 68 69 // Update writes all fields of the p to the heap object referenced by ref. 70 func (p *AdditionalDataItem) Update(ref js.Ref) { 71 bindings.AdditionalDataItemJSLoad( 72 js.Pointer(p), js.False, ref, 73 ) 74 } 75 76 // FreeMembers frees fields with heap reference, if recursive is true 77 // free all heap references reachable from p. 78 func (p *AdditionalDataItem) FreeMembers(recursive bool) { 79 js.Free( 80 p.Data.Ref(), 81 ) 82 p.Data = p.Data.FromRef(js.Undefined) 83 } 84 85 type ImageType uint32 86 87 const ( 88 _ ImageType = iota 89 90 ImageType_PNG 91 ImageType_JPEG 92 ) 93 94 func (ImageType) FromRef(str js.Ref) ImageType { 95 return ImageType(bindings.ConstOfImageType(str)) 96 } 97 98 func (x ImageType) String() (string, bool) { 99 switch x { 100 case ImageType_PNG: 101 return "png", true 102 case ImageType_JPEG: 103 return "jpeg", true 104 default: 105 return "", false 106 } 107 } 108 109 type SetImageDataCallbackFunc func(this js.Ref) js.Ref 110 111 func (fn SetImageDataCallbackFunc) Register() js.Func[func()] { 112 return js.RegisterCallback[func()]( 113 fn, abi.FuncPCABIInternal(fn), 114 ) 115 } 116 117 func (fn SetImageDataCallbackFunc) DispatchCallback( 118 targetPC uintptr, ctx *js.CallbackContext, 119 ) { 120 args := ctx.Args() 121 if len(args) != 0+1 /* js this */ || 122 targetPC != uintptr(abi.FuncPCABIInternal(fn)) { 123 js.ThrowInvalidCallbackInvocation() 124 } 125 126 if ctx.Return(fn( 127 args[0], 128 )) { 129 return 130 } 131 132 js.ThrowCallbackValueNotReturned() 133 } 134 135 type SetImageDataCallback[T any] struct { 136 Fn func(arg T, this js.Ref) js.Ref 137 Arg T 138 } 139 140 func (cb *SetImageDataCallback[T]) Register() js.Func[func()] { 141 return js.RegisterCallback[func()]( 142 cb, abi.FuncPCABIInternal(cb.Fn), 143 ) 144 } 145 146 func (cb *SetImageDataCallback[T]) DispatchCallback( 147 targetPC uintptr, ctx *js.CallbackContext, 148 ) { 149 args := ctx.Args() 150 if len(args) != 0+1 /* js this */ || 151 targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) { 152 js.ThrowInvalidCallbackInvocation() 153 } 154 155 if ctx.Return(cb.Fn( 156 cb.Arg, 157 args[0], 158 )) { 159 return 160 } 161 162 js.ThrowCallbackValueNotReturned() 163 } 164 165 type OnClipboardDataChangedEventCallbackFunc func(this js.Ref) js.Ref 166 167 func (fn OnClipboardDataChangedEventCallbackFunc) Register() js.Func[func()] { 168 return js.RegisterCallback[func()]( 169 fn, abi.FuncPCABIInternal(fn), 170 ) 171 } 172 173 func (fn OnClipboardDataChangedEventCallbackFunc) DispatchCallback( 174 targetPC uintptr, ctx *js.CallbackContext, 175 ) { 176 args := ctx.Args() 177 if len(args) != 0+1 /* js this */ || 178 targetPC != uintptr(abi.FuncPCABIInternal(fn)) { 179 js.ThrowInvalidCallbackInvocation() 180 } 181 182 if ctx.Return(fn( 183 args[0], 184 )) { 185 return 186 } 187 188 js.ThrowCallbackValueNotReturned() 189 } 190 191 type OnClipboardDataChangedEventCallback[T any] struct { 192 Fn func(arg T, this js.Ref) js.Ref 193 Arg T 194 } 195 196 func (cb *OnClipboardDataChangedEventCallback[T]) Register() js.Func[func()] { 197 return js.RegisterCallback[func()]( 198 cb, abi.FuncPCABIInternal(cb.Fn), 199 ) 200 } 201 202 func (cb *OnClipboardDataChangedEventCallback[T]) DispatchCallback( 203 targetPC uintptr, ctx *js.CallbackContext, 204 ) { 205 args := ctx.Args() 206 if len(args) != 0+1 /* js this */ || 207 targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) { 208 js.ThrowInvalidCallbackInvocation() 209 } 210 211 if ctx.Return(cb.Fn( 212 cb.Arg, 213 args[0], 214 )) { 215 return 216 } 217 218 js.ThrowCallbackValueNotReturned() 219 } 220 221 // HasFuncOnClipboardDataChanged returns true if the function "WEBEXT.clipboard.onClipboardDataChanged.addListener" exists. 222 func HasFuncOnClipboardDataChanged() bool { 223 return js.True == bindings.HasFuncOnClipboardDataChanged() 224 } 225 226 // FuncOnClipboardDataChanged returns the function "WEBEXT.clipboard.onClipboardDataChanged.addListener". 227 func FuncOnClipboardDataChanged() (fn js.Func[func(callback js.Func[func()])]) { 228 bindings.FuncOnClipboardDataChanged( 229 js.Pointer(&fn), 230 ) 231 return 232 } 233 234 // OnClipboardDataChanged calls the function "WEBEXT.clipboard.onClipboardDataChanged.addListener" directly. 235 func OnClipboardDataChanged(callback js.Func[func()]) (ret js.Void) { 236 bindings.CallOnClipboardDataChanged( 237 js.Pointer(&ret), 238 callback.Ref(), 239 ) 240 241 return 242 } 243 244 // TryOnClipboardDataChanged calls the function "WEBEXT.clipboard.onClipboardDataChanged.addListener" 245 // in a try/catch block and returns (_, err, ok = false) when it went through 246 // the catch clause. 247 func TryOnClipboardDataChanged(callback js.Func[func()]) (ret js.Void, exception js.Any, ok bool) { 248 ok = js.True == bindings.TryOnClipboardDataChanged( 249 js.Pointer(&ret), js.Pointer(&exception), 250 callback.Ref(), 251 ) 252 253 return 254 } 255 256 // HasFuncOffClipboardDataChanged returns true if the function "WEBEXT.clipboard.onClipboardDataChanged.removeListener" exists. 257 func HasFuncOffClipboardDataChanged() bool { 258 return js.True == bindings.HasFuncOffClipboardDataChanged() 259 } 260 261 // FuncOffClipboardDataChanged returns the function "WEBEXT.clipboard.onClipboardDataChanged.removeListener". 262 func FuncOffClipboardDataChanged() (fn js.Func[func(callback js.Func[func()])]) { 263 bindings.FuncOffClipboardDataChanged( 264 js.Pointer(&fn), 265 ) 266 return 267 } 268 269 // OffClipboardDataChanged calls the function "WEBEXT.clipboard.onClipboardDataChanged.removeListener" directly. 270 func OffClipboardDataChanged(callback js.Func[func()]) (ret js.Void) { 271 bindings.CallOffClipboardDataChanged( 272 js.Pointer(&ret), 273 callback.Ref(), 274 ) 275 276 return 277 } 278 279 // TryOffClipboardDataChanged calls the function "WEBEXT.clipboard.onClipboardDataChanged.removeListener" 280 // in a try/catch block and returns (_, err, ok = false) when it went through 281 // the catch clause. 282 func TryOffClipboardDataChanged(callback js.Func[func()]) (ret js.Void, exception js.Any, ok bool) { 283 ok = js.True == bindings.TryOffClipboardDataChanged( 284 js.Pointer(&ret), js.Pointer(&exception), 285 callback.Ref(), 286 ) 287 288 return 289 } 290 291 // HasFuncHasOnClipboardDataChanged returns true if the function "WEBEXT.clipboard.onClipboardDataChanged.hasListener" exists. 292 func HasFuncHasOnClipboardDataChanged() bool { 293 return js.True == bindings.HasFuncHasOnClipboardDataChanged() 294 } 295 296 // FuncHasOnClipboardDataChanged returns the function "WEBEXT.clipboard.onClipboardDataChanged.hasListener". 297 func FuncHasOnClipboardDataChanged() (fn js.Func[func(callback js.Func[func()]) bool]) { 298 bindings.FuncHasOnClipboardDataChanged( 299 js.Pointer(&fn), 300 ) 301 return 302 } 303 304 // HasOnClipboardDataChanged calls the function "WEBEXT.clipboard.onClipboardDataChanged.hasListener" directly. 305 func HasOnClipboardDataChanged(callback js.Func[func()]) (ret bool) { 306 bindings.CallHasOnClipboardDataChanged( 307 js.Pointer(&ret), 308 callback.Ref(), 309 ) 310 311 return 312 } 313 314 // TryHasOnClipboardDataChanged calls the function "WEBEXT.clipboard.onClipboardDataChanged.hasListener" 315 // in a try/catch block and returns (_, err, ok = false) when it went through 316 // the catch clause. 317 func TryHasOnClipboardDataChanged(callback js.Func[func()]) (ret bool, exception js.Any, ok bool) { 318 ok = js.True == bindings.TryHasOnClipboardDataChanged( 319 js.Pointer(&ret), js.Pointer(&exception), 320 callback.Ref(), 321 ) 322 323 return 324 } 325 326 // HasFuncSetImageData returns true if the function "WEBEXT.clipboard.setImageData" exists. 327 func HasFuncSetImageData() bool { 328 return js.True == bindings.HasFuncSetImageData() 329 } 330 331 // FuncSetImageData returns the function "WEBEXT.clipboard.setImageData". 332 func FuncSetImageData() (fn js.Func[func(imageData js.ArrayBuffer, typ ImageType, additionalItems js.Array[AdditionalDataItem]) js.Promise[js.Void]]) { 333 bindings.FuncSetImageData( 334 js.Pointer(&fn), 335 ) 336 return 337 } 338 339 // SetImageData calls the function "WEBEXT.clipboard.setImageData" directly. 340 func SetImageData(imageData js.ArrayBuffer, typ ImageType, additionalItems js.Array[AdditionalDataItem]) (ret js.Promise[js.Void]) { 341 bindings.CallSetImageData( 342 js.Pointer(&ret), 343 imageData.Ref(), 344 uint32(typ), 345 additionalItems.Ref(), 346 ) 347 348 return 349 } 350 351 // TrySetImageData calls the function "WEBEXT.clipboard.setImageData" 352 // in a try/catch block and returns (_, err, ok = false) when it went through 353 // the catch clause. 354 func TrySetImageData(imageData js.ArrayBuffer, typ ImageType, additionalItems js.Array[AdditionalDataItem]) (ret js.Promise[js.Void], exception js.Any, ok bool) { 355 ok = js.True == bindings.TrySetImageData( 356 js.Pointer(&ret), js.Pointer(&exception), 357 imageData.Ref(), 358 uint32(typ), 359 additionalItems.Ref(), 360 ) 361 362 return 363 }