-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathultralight.go
1421 lines (1169 loc) · 36.7 KB
/
ultralight.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ultralight
/*
#cgo CFLAGS: -I./SDK/include
#cgo LDFLAGS: -L./SDK/bin -lUltralight -lUltralightCore -lWebCore -lAppCore -Wl,-rpath,./SDK/bin
#include <AppCore/CAPI.h>
#include <stdlib.h>
extern void appUpdateCallback(void *);
extern void winResizeCallback(void *, unsigned int, unsigned int);
extern void winCloseCallback(void *);
extern void viewBeginLoadingCallback(void *, ULView);
extern void viewFinishLoadingCallback(void *, ULView);
extern void viewUpdateHistoryCallback(void *, ULView);
extern void viewDOMReadyCallback(void *, ULView);
extern void viewChangeTitleCallback(void *, ULView, ULString);
extern void viewChangeURLCallback(void *, ULView, ULString);
extern void viewChangeCursorCallback(void *, ULView, ULCursor);
extern void viewConsoleMessageCallback(void* user_data, ULView caller,
ULMessageSource source, ULMessageLevel level,
ULString message, unsigned int line_number,
unsigned int column_number,
ULString source_id);
extern JSValueRef objFunctionCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argumentCount, JSValueRef *arguments, JSValueRef* exception);
static inline void set_app_update_callback(ULApp app, void *data) {
if (data == NULL) {
ulAppSetUpdateCallback(app, NULL, NULL);
} else {
ulAppSetUpdateCallback(app, appUpdateCallback, data);
}
}
static inline void set_win_resize_callback(ULWindow win, void *data) {
if (data == NULL) {
ulWindowSetResizeCallback(win, NULL, NULL);
} else {
ulWindowSetResizeCallback(win, winResizeCallback, data);
}
}
static inline void set_win_close_callback(ULWindow win, void *data) {
if (data == NULL) {
ulWindowSetCloseCallback(win, NULL, NULL);
} else {
ulWindowSetCloseCallback(win, winCloseCallback, data);
}
}
static inline void set_view_begin_loading_callback(ULView view, void *data) {
if (data == NULL) {
ulViewSetBeginLoadingCallback(view, NULL, NULL);
} else {
ulViewSetBeginLoadingCallback(view, viewBeginLoadingCallback, data);
}
}
static inline void set_view_finish_loading_callback(ULView view, void *data) {
if (data == NULL) {
ulViewSetFinishLoadingCallback(view, NULL, NULL);
} else {
ulViewSetFinishLoadingCallback(view, viewFinishLoadingCallback, data);
}
}
static inline void set_view_update_history_callback(ULView view, void *data) {
if (data == NULL) {
ulViewSetUpdateHistoryCallback(view, NULL, NULL);
} else {
ulViewSetUpdateHistoryCallback(view, viewUpdateHistoryCallback, data);
}
}
static inline void set_view_dom_ready_callback(ULView view, void *data) {
if (data == NULL) {
ulViewSetDOMReadyCallback(view, NULL, NULL);
} else {
ulViewSetDOMReadyCallback(view, viewDOMReadyCallback, data);
}
}
static inline void set_view_change_title_callback(ULView view, void *data) {
if (data == NULL) {
ulViewSetChangeTitleCallback(view, NULL, NULL);
} else {
ulViewSetChangeTitleCallback(view, viewChangeTitleCallback, data);
}
}
static inline void set_view_change_url_callback(ULView view, void *data) {
if (data == NULL) {
ulViewSetChangeURLCallback(view, NULL, NULL);
} else {
ulViewSetChangeURLCallback(view, viewChangeURLCallback, data);
}
}
static inline void set_view_change_cursor_callback(ULView view, void *data) {
if (data == NULL) {
ulViewSetChangeCursorCallback(view, NULL, NULL);
} else {
ulViewSetChangeCursorCallback(view, viewChangeCursorCallback, data);
}
}
static inline void set_view_console_message_callback(ULView view, void *data) {
if (data == NULL) {
ulViewSetAddConsoleMessageCallback(view, NULL, NULL);
} else {
ulViewSetAddConsoleMessageCallback(view, viewConsoleMessageCallback, data);
}
}
static inline JSObjectRef make_function_callback(JSContextRef ctx, JSStringRef name) {
return JSObjectMakeFunctionWithCallback(ctx, name, (JSObjectCallAsFunctionCallback)objFunctionCallback);
}
*/
import "C"
import "unsafe"
import "unicode/utf16"
import "unicode/utf8"
import "reflect"
import "bytes"
import "log"
type JSType int
const (
JSTypeUndefined = JSType(C.kJSTypeUndefined)
JSTypeNull = JSType(C.kJSTypeNull)
JSTypeBoolean = JSType(C.kJSTypeBoolean)
JSTypeNumber = JSType(C.kJSTypeNumber)
JSTypeString = JSType(C.kJSTypeString)
JSTypeObject = JSType(C.kJSTypeObject)
)
type MessageSource int
const (
MessageSourceXML = MessageSource(C.kMessageSource_XML)
MessageSourceJS = MessageSource(C.kMessageSource_JS)
MessageSourceNetwork = MessageSource(C.kMessageSource_Network)
MessageSourceConsoleAPI = MessageSource(C.kMessageSource_ConsoleAPI)
MessageSourceStorage = MessageSource(C.kMessageSource_Storage)
MessageSourceAppCache = MessageSource(C.kMessageSource_AppCache)
MessageSourceRendering = MessageSource(C.kMessageSource_Rendering)
MessageSourceCSS = MessageSource(C.kMessageSource_CSS)
MessageSourceSecurity = MessageSource(C.kMessageSource_Security)
MessageSourceContentBlocker = MessageSource(C.kMessageSource_ContentBlocker)
MessageSourceOther = MessageSource(C.kMessageSource_Other)
)
type MessageLevel int
const (
MessageLevelLog = MessageLevel(C.kMessageLevel_Log)
MessageLevelWarning = MessageLevel(C.kMessageLevel_Warning)
MessageLevelError = MessageLevel(C.kMessageLevel_Error)
MessageLevelDebug = MessageLevel(C.kMessageLevel_Debug)
MessageLevelInfo = MessageLevel(C.kMessageLevel_Info)
)
type Cursor int
const (
CursorPointer = Cursor(C.kCursor_Pointer)
CursorCross = Cursor(C.kCursor_Cross)
CursorHand = Cursor(C.kCursor_Hand)
CursorIBeam = Cursor(C.kCursor_IBeam)
CursorWait = Cursor(C.kCursor_Wait)
CursorHelp = Cursor(C.kCursor_Help)
CursorEastResize = Cursor(C.kCursor_EastResize)
CursorNorthResize = Cursor(C.kCursor_NorthResize)
CursorNorthEastResize = Cursor(C.kCursor_NorthEastResize)
CursorNorthWestResize = Cursor(C.kCursor_NorthWestResize)
CursorSouthResize = Cursor(C.kCursor_SouthResize)
CursorSouthEastResize = Cursor(C.kCursor_SouthEastResize)
CursorSouthWestResize = Cursor(C.kCursor_SouthWestResize)
CursorWestResize = Cursor(C.kCursor_WestResize)
CursorNorthSouthResize = Cursor(C.kCursor_NorthSouthResize)
CursorEastWestResize = Cursor(C.kCursor_EastWestResize)
CursorNorthEastSouthWestResiz = Cursor(C.kCursor_NorthEastSouthWestResize)
CursorNorthWestSouthEastResize = Cursor(C.kCursor_NorthWestSouthEastResize)
CursorColumnResize = Cursor(C.kCursor_ColumnResize)
CursorRowResize = Cursor(C.kCursor_RowResize)
CursorMiddlePanning = Cursor(C.kCursor_MiddlePanning)
CursorEastPanning = Cursor(C.kCursor_EastPanning)
CursorNorthPanning = Cursor(C.kCursor_NorthPanning)
CursorNorthEastPanning = Cursor(C.kCursor_NorthEastPanning)
CursorNorthWestPanning = Cursor(C.kCursor_NorthWestPanning)
CursorSouthPanning = Cursor(C.kCursor_SouthPanning)
CursorSouthEastPanning = Cursor(C.kCursor_SouthEastPanning)
CursorSouthWestPanning = Cursor(C.kCursor_SouthWestPanning)
CursorWestPanning = Cursor(C.kCursor_WestPanning)
CursorMove = Cursor(C.kCursor_Move)
CursorVerticalText = Cursor(C.kCursor_VerticalText)
CursorCell = Cursor(C.kCursor_Cell)
CursorContextMenu = Cursor(C.kCursor_ContextMenu)
CursorAlias = Cursor(C.kCursor_Alias)
CursorProgress = Cursor(C.kCursor_Progress)
CursorNoDrop = Cursor(C.kCursor_NoDrop)
CursorCopy = Cursor(C.kCursor_Copy)
CursorNone = Cursor(C.kCursor_None)
CursorNotAllowed = Cursor(C.kCursor_NotAllowed)
CursorZoomIn = Cursor(C.kCursor_ZoomIn)
CursorZoomOut = Cursor(C.kCursor_ZoomOut)
CursorGrab = Cursor(C.kCursor_Grab)
CursorGrabbing = Cursor(C.kCursor_Grabbing)
CursorCustom = Cursor(C.kCursor_Custom)
)
// App is the main application object
type App struct {
app C.ULApp
windows map[C.ULWindow]*Window
onUpdate func()
}
// Window is an application window
type Window struct {
win C.ULWindow
ovl []Overlay
app *App
onResize func(width, height uint)
onClose func()
}
type Overlay struct {
ovl C.ULOverlay
view View
}
// View is the window "content"
type View struct {
view C.ULView
onBeginLoading func()
onFinishLoading func()
onUpdateHistory func()
onDOMReady func()
onChangeTitle func(string)
onChangeURL func(string)
onChangeCursor func(Cursor)
onConsoleMessage func(MessageSource, MessageLevel, string, uint, uint, string)
}
// JSContext
type JSContext struct {
ctx C.JSContextRef
}
// JSGlobalContext
type JSGlobalContext struct {
ctx C.JSGlobalContextRef
}
// JSValue
type JSValue struct {
val C.JSValueRef
ctx C.JSContextRef
}
// JSObject
type JSObject struct {
obj C.JSObjectRef
ctx C.JSContextRef
}
func decodeUTF16(p *C.ULChar16, l C.size_t) string {
var u []uint16
sl := (*reflect.SliceHeader)((unsafe.Pointer(&u)))
sl.Cap = int(l)
sl.Len = int(l)
sl.Data = uintptr(unsafe.Pointer(p))
runes := utf16.Decode(u)
ret := &bytes.Buffer{}
b8buf := make([]byte, 4)
for _, r := range runes {
n := utf8.EncodeRune(b8buf, r)
ret.Write(b8buf[:n])
}
return ret.String()
}
func decodeULString(s C.ULString) string {
l := C.ulStringGetLength(s)
if l == 0 {
return ""
}
data := C.ulStringGetData(s)
return decodeUTF16(data, l)
}
func decodeJSString(s C.JSStringRef) string {
l := C.JSStringGetLength(s)
if l == 0 {
return ""
}
data := C.JSStringGetCharactersPtr(s)
return decodeUTF16((*C.ULChar16)(data), l)
}
// NewApp creates the App singleton.
//
// Note: You should only create one of these per application lifetime.
func NewApp() *App {
return &App{app: C.ulCreateApp(C.ulCreateSettings(), C.ulCreateConfig()), windows: map[C.ULWindow]*Window{}}
}
// Destroy destroys the App instance.
func (app *App) Destroy() {
C.ulDestroyApp(app.app)
app.app = nil
app.windows = nil
}
// Window gets the main application window.
func (app *App) Window() *Window {
ulwin := C.ulAppGetWindow(app.app)
if win, ok := app.windows[ulwin]; ok {
return win
}
win := &Window{win: ulwin}
app.windows[ulwin] = win
return win
}
// IsRunning checks whether or not the App is running.
func (app *App) IsRunning() bool {
return bool(C.ulAppIsRunning(app.app))
}
// OnUpdate sets a callback for whenever the App updates.
// You should update all app logic here.
func (app *App) OnUpdate(cb func()) {
app.onUpdate = cb
p := unsafe.Pointer(app.app)
if cb == nil {
callbackData[p] = nil
C.set_app_update_callback(app.app, nil)
} else {
callbackData[p] = app
C.set_app_update_callback(app.app, p)
}
}
// Run runs the main loop.
func (app *App) Run() {
C.ulAppRun(app.app)
}
// Quit the application.
func (app *App) Quit() {
C.ulAppQuit(app.app)
}
var callbackData = map[unsafe.Pointer]interface{}{}
var reverseCallbback = map[interface{}]unsafe.Pointer{}
// NewWindow create a new window and sets it as the main application window.
func (app *App) NewWindow(width, height uint, fullscreen bool, title string) *Window {
win := &Window{win: C.ulCreateWindow(C.ulAppGetMainMonitor(app.app),
C.uint(width), C.uint(height),
C.bool(fullscreen),
C.kWindowFlags_Titled|C.kWindowFlags_Resizable|C.kWindowFlags_Maximizable),
app: app}
C.ulAppSetWindow(app.app, win.win)
win.SetTitle(title)
win.NewOverlay(width, height, 0, 0)
return win
}
// Destroy destroys the window.
func (win *Window) Destroy() {
delete(win.app.windows, win.win)
for _, o := range win.ovl {
o.Destroy()
}
C.ulDestroyWindow(win.win)
win.OnResize(nil)
win.ovl = nil
win.win = nil
win.app = nil
}
// Close closes the window.
func (win *Window) Close() {
C.ulWindowClose(win.win)
// should this remove the window from win.app ?
}
// SetTitle sets the window title.
func (win *Window) SetTitle(title string) {
t := C.CString(title)
C.ulWindowSetTitle(win.win, t)
C.free(unsafe.Pointer(t))
}
func (win *Window) SetCursor(cursor Cursor) {
C.ulWindowSetCursor(win.win, C.ULCursor(cursor))
}
func (win *Window) Width() uint {
return uint(C.ulWindowGetWidth(win.win))
}
func (win *Window) Height() uint {
return uint(C.ulWindowGetHeight(win.win))
}
// Create a new Overlay.
func (win *Window) NewOverlay(width, height uint, x, y int) *Overlay {
cOvl := C.ulCreateOverlay(win.win, C.uint(width), C.uint(height), C.int(x), C.int(y))
ovl := Overlay{ovl: cOvl, view: View{view: C.ulOverlayGetView(cOvl)}}
win.ovl = append(win.ovl, ovl)
return &ovl
}
func (win *Window) RemoveOverlay(i int) {
if i >= 0 && i < len(win.ovl) {
win.ovl[i].Destroy()
win.ovl = append(win.ovl[i:], win.ovl[i+1:]...)
}
}
func (win *Window) NOverlay() int {
return len(win.ovl)
}
func (win *Window) Overlay(i int) *Overlay {
return &win.ovl[i]
}
// IsFullscreen checks whether or not a window is fullscreen.
func (win *Window) IsFullscreen() bool {
return bool(C.ulWindowIsFullscreen(win.win))
}
// Whether or not the overlay is hidden (not drawn).
func (win *Window) IsHidden() bool {
return win.ovl[0].IsHidden()
}
// Hide the overlay (will no longer be drawn)
func (win *Window) Hide() {
win.ovl[0].Hide()
}
// Show the overlay.
func (win *Window) Show() {
win.ovl[0].Show()
}
// Whether or not an overlay has keyboard focus.
func (win *Window) HasFocus() bool {
return win.ovl[0].HasFocus()
}
// Grant this overlay exclusive keyboard focus.
func (win *Window) Focus() {
win.ovl[0].Focus()
}
// Remove keyboard focus.
func (win *Window) Unfocus() {
win.ovl[0].Unfocus()
}
// Resize resizes the window (and underlying View).
// Dimensions should be specified in device coordinates.
func (win *Window) Resize(width, height uint) {
win.ovl[0].Resize(width, height)
}
// OnResize sets a callback to be notified when a window resizes
// (parameters are passed back in device coordinates).
func (win *Window) OnResize(cb func(width, height uint)) {
win.onResize = cb
p := unsafe.Pointer(win.win)
if cb == nil {
callbackData[p] = nil
C.set_win_resize_callback(win.win, nil)
} else {
callbackData[p] = win
C.set_win_resize_callback(win.win, p)
}
}
// OnClose sets a callback to be notified when a window closes.
func (win *Window) OnClose(cb func()) {
win.onClose = cb
p := unsafe.Pointer(win.win)
if cb == nil {
callbackData[p] = nil
C.set_win_close_callback(win.win, nil)
} else {
callbackData[p] = win
C.set_win_close_callback(win.win, p)
}
}
// View gets the underlying View.
func (win *Window) View() *View {
if len(win.ovl) > 0 {
return win.ovl[0].View()
}
return nil
}
// LoadHTML loads a raw string of html
func (view *View) LoadHTML(html string) {
s := C.CString(html)
uls := C.ulCreateString(s)
defer func() {
C.ulDestroyString(uls)
C.free(unsafe.Pointer(s))
}()
C.ulViewLoadHTML(view.view, uls)
}
// LoadURL loads a URL into main frame
func (view *View) LoadURL(url string) {
s := C.CString(url)
uls := C.ulCreateString(s)
defer func() {
C.ulDestroyString(uls)
C.free(unsafe.Pointer(s))
}()
C.ulViewLoadURL(view.view, uls)
}
// URL returns the current URL.
func (view *View) URL() string {
return decodeULString(C.ulViewGetURL(view.view))
}
// Title returns the current title.
func (view *View) Title() string {
s := C.ulViewGetTitle(view.view)
l := C.ulStringGetLength(s)
if l == 0 {
return ""
}
data := C.ulStringGetData(s)
return decodeUTF16(data, l)
}
// IsLoading Checks if main frame is loading.
func (view *View) IsLoading() bool {
return bool(C.ulViewIsLoading(view.view))
}
// JSContext gets the page's JSContext for use with JavaScriptCore API
func (view *View) JSContext() *JSContext {
return &JSContext{ctx: C.ulViewGetJSContext(view.view)}
}
// EvaluateScript evaluates a raw string of JavaScript and return result
func (view *View) EvaluateScript(script string) *JSValue {
s := C.CString(script)
uls := C.ulCreateString(s)
defer func() {
C.ulDestroyString(uls)
C.free(unsafe.Pointer(s))
}()
return &JSValue{val: C.ulViewEvaluateScript(view.view, uls), ctx: C.ulViewGetJSContext(view.view)}
}
// CanGoBack checks if can navigate backwards in history
func (view *View) CanGoBack() bool {
return bool(C.ulViewCanGoBack(view.view))
}
// CanGoForward checks if can navigate forwards in history
func (view *View) CanGoForward() bool {
return bool(C.ulViewCanGoForward(view.view))
}
// GoBack navigates backwards in history
func (view *View) GoBack() {
C.ulViewGoBack(view.view)
}
// GoForward navigates forwards in history
func (view *View) GoForward() {
C.ulViewGoForward(view.view)
}
// GoToHistoryOffset navigates to arbitrary offset in history
func (view *View) GoToHistoryOffset(offset int) {
C.ulViewGoToHistoryOffset(view.view, C.int(offset))
}
// Reload reloads the current page
func (view *View) Reload() {
C.ulViewReload(view.view)
}
// Stop stops all page loads
func (view *View) Stop() {
C.ulViewStop(view.view)
}
// Set callback for when the page begins loading new URL into main frame
func (view *View) OnBeginLoading(cb func()) {
view.onBeginLoading = cb
p := unsafe.Pointer(view.view)
if cb == nil {
callbackData[p] = nil
C.set_view_begin_loading_callback(view.view, nil)
} else {
callbackData[p] = view
C.set_view_begin_loading_callback(view.view, p)
}
}
// Set callback for when the page finishes loading new URL into main frame
func (view *View) OnFinishLoading(cb func()) {
view.onFinishLoading = cb
p := unsafe.Pointer(view.view)
if cb == nil {
callbackData[p] = nil
C.set_view_finish_loading_callback(view.view, nil)
} else {
callbackData[p] = view
C.set_view_finish_loading_callback(view.view, p)
}
}
// Set callback for when the history (back/forward state) is modified
func (view *View) OnUpdateHistory(cb func()) {
view.onUpdateHistory = cb
p := unsafe.Pointer(view.view)
if cb == nil {
callbackData[p] = nil
C.set_view_update_history_callback(view.view, nil)
} else {
callbackData[p] = view
C.set_view_update_history_callback(view.view, p)
}
}
// Set callback for when all JavaScript has been parsed and the document is
// ready. This is the best time to make initial JavaScript calls to your page.
func (view *View) OnDOMReady(cb func()) {
view.onDOMReady = cb
p := unsafe.Pointer(view.view)
if cb == nil {
callbackData[p] = nil
C.set_view_dom_ready_callback(view.view, nil)
} else {
callbackData[p] = view
C.set_view_dom_ready_callback(view.view, p)
}
}
// Set callback for when the page title changes
func (view *View) OnChangeTitle(cb func(string)) {
view.onChangeTitle = cb
p := unsafe.Pointer(view.view)
if cb == nil {
callbackData[p] = nil
C.set_view_change_title_callback(view.view, nil)
} else {
callbackData[p] = view
C.set_view_change_title_callback(view.view, p)
}
}
// Set callback for when the page URL changes
func (view *View) OnChangeURL(cb func(string)) {
view.onChangeURL = cb
p := unsafe.Pointer(view.view)
if cb == nil {
callbackData[p] = nil
C.set_view_change_url_callback(view.view, nil)
} else {
callbackData[p] = view
C.set_view_change_url_callback(view.view, p)
}
}
// Set callback for when the mouse cursor changes
func (view *View) OnChangeCursor(cb func(Cursor)) {
view.onChangeCursor = cb
p := unsafe.Pointer(view.view)
if cb == nil {
callbackData[p] = nil
C.set_view_change_cursor_callback(view.view, nil)
} else {
callbackData[p] = view
C.set_view_change_cursor_callback(view.view, p)
}
}
// Set callback for when a message is added to the console (useful for
// JavaScript / network errors and debugging)
func (view *View) OnConsoleMessage(cb func(source MessageSource, level MessageLevel,
message string, line uint, col uint, sourceID string)) {
view.onConsoleMessage = cb
p := unsafe.Pointer(view.view)
if cb == nil {
callbackData[p] = nil
C.set_view_console_message_callback(view.view, nil)
} else {
callbackData[p] = view
C.set_view_console_message_callback(view.view, p)
}
}
// Returns a JavaScript value's type.
func (v *JSValue) Type() JSType {
return JSType(C.JSValueGetType(v.ctx, v.val))
}
// Tests whether a JavaScript value's type is the undefined type.
func (v *JSValue) IsUndefined() bool {
return bool(C.JSValueIsUndefined(v.ctx, v.val))
}
// Tests whether a JavaScript value's type is the null type.
func (v *JSValue) IsNull() bool {
return bool(C.JSValueIsNull(v.ctx, v.val))
}
// Tests whether a JavaScript value's type is the boolean type.
func (v *JSValue) IsBoolean() bool {
return bool(C.JSValueIsBoolean(v.ctx, v.val))
}
// Tests whether a JavaScript value's type is the number type.
func (v *JSValue) IsNumber() bool {
return bool(C.JSValueIsNumber(v.ctx, v.val))
}
// Tests whether a JavaScript value's type is the string type.
func (v *JSValue) IsString() bool {
return bool(C.JSValueIsString(v.ctx, v.val))
}
// Tests whether a JavaScript value's type is the object type.
func (v *JSValue) IsObject() bool {
return bool(C.JSValueIsObject(v.ctx, v.val))
}
// Tests whether a JavaScript value is an array.
func (v *JSValue) IsArray() bool {
return bool(C.JSValueIsArray(v.ctx, v.val))
}
// Tests whether a JavaScript value is a date.
func (v *JSValue) IsDate() bool {
return bool(C.JSValueIsDate(v.ctx, v.val))
}
func (v *JSValue) IsFunction() bool {
if !v.IsObject() {
return false
}
return v.Object().IsFunction()
}
// Converts a JavaScript value to boolean and returns the resulting boolean.
func (v *JSValue) Boolean() bool {
return bool(C.JSValueToBoolean(v.ctx, v.val))
}
// Converts a JavaScript value to number and returns the resulting number.
func (v *JSValue) Number() float64 {
return float64(C.JSValueToNumber(v.ctx, v.val, nil))
}
// Converts a JavaScript value to string and copies the result into a JavaScript string.
func (v *JSValue) String() string {
js := C.JSValueToStringCopy(v.ctx, v.val, nil)
if js == nil {
return ""
}
return decodeJSString(js)
}
// Converts a JavaScript value to object and returns the resulting object.
func (v *JSValue) Object() *JSObject {
o := C.JSValueToObject(v.ctx, v.val, nil)
if o == nil {
return nil
}
return &JSObject{ctx: v.ctx, obj: o}
}
// Creates a JavaScript value of the undefined type.
func (ctx *JSContext) Undefined() JSValue {
return JSValue{ctx: ctx.ctx, val: C.JSValueMakeUndefined(ctx.ctx)}
}
// Creates a JavaScript value of the null type.
func (ctx *JSContext) Null() JSValue {
return JSValue{ctx: ctx.ctx, val: C.JSValueMakeNull(ctx.ctx)}
}
// Creates a JavaScript value of the boolean type.
func (ctx *JSContext) Boolean(v bool) JSValue {
return JSValue{ctx: ctx.ctx, val: C.JSValueMakeBoolean(ctx.ctx, C.bool(v))}
}
// Creates a JavaScript value of the number type.
func (ctx *JSContext) Number(v float64) JSValue {
return JSValue{ctx: ctx.ctx, val: C.JSValueMakeNumber(ctx.ctx, C.double(v))}
}
// Creates a JavaScript value of the string type.
func (ctx *JSContext) String(v string) JSValue {
s := C.CString(v)
js := C.JSStringCreateWithUTF8CString(s)
defer C.free(unsafe.Pointer(s))
return JSValue{ctx: ctx.ctx, val: C.JSValueMakeString(ctx.ctx, js)}
}
func (ctx *JSContext) JSValue(v interface{}) JSValue {
if v == nil {
return ctx.Null()
}
switch t := v.(type) {
case *JSValue:
return *t
case JSValue:
return t
case bool:
return ctx.Boolean(t)
case string:
return ctx.String(t)
case float64:
return ctx.Number(t)
case float32:
return ctx.Number(float64(t))
case int:
return ctx.Number(float64(t))
case int8:
return ctx.Number(float64(t))
case int16:
return ctx.Number(float64(t))
case int32:
return ctx.Number(float64(t))
case int64:
return ctx.Number(float64(t))
case FunctionCallback:
fv := ctx.FunctionCallback("", t)
return *fv
case func(function, this *JSObject, args ...*JSValue) *JSValue:
fv := ctx.FunctionCallback("", t)
return *fv
default:
log.Fatalf("cannot convert %#T to JSValue", t)
}
return ctx.Undefined() // not reached
}
func makeJSString(v string) C.JSStringRef {
s := C.CString(v)
defer C.free(unsafe.Pointer(s))
return C.JSStringCreateWithUTF8CString(s)
}
type FunctionCallback func(function, this *JSObject, args ...*JSValue) *JSValue
// Convenience method for creating a JavaScript function with a given callback as its implementation.
func (ctx *JSContext) FunctionCallback(name string, cb FunctionCallback) *JSValue {
obj := C.make_function_callback(ctx.ctx, makeJSString(name))
p := unsafe.Pointer(obj)
callbackData[p] = cb
return &JSValue{ctx: ctx.ctx, val: C.JSValueRef(obj)}
}
// Gets the global object of a JavaScript execution context.
func (ctx *JSContext) GlobalObject() *JSObject {
return &JSObject{ctx: ctx.ctx, obj: C.JSContextGetGlobalObject(ctx.ctx)}
}
// Gets the global object of a JavaScript execution context.
func (ctx *JSContext) GlobalContext() JSGlobalContext {
return JSGlobalContext{ctx: C.JSContextGetGlobalContext(ctx.ctx)}
}
// Tests whether an object can be called as a function.
func (o *JSObject) IsFunction() bool {
return bool(C.JSObjectIsFunction(o.ctx, o.obj))
}
// Calls an object as a function.
func (o *JSObject) Call(this *JSObject, args ...interface{}) *JSValue {
var thisObj C.JSObjectRef
var jargs *C.JSValueRef
if this != nil {
thisObj = this.obj
}
nargs := len(args)
if nargs > 0 {
jargs = (*C.JSValueRef)(C.malloc(C.size_t(nargs) * C.size_t(unsafe.Sizeof(uintptr(0)))))
defer C.free(unsafe.Pointer(jargs))
var ja []C.JSValueRef
sl := (*reflect.SliceHeader)(unsafe.Pointer(&ja))
sl.Cap = nargs
sl.Len = nargs
sl.Data = uintptr(unsafe.Pointer(jargs))
ctx := &JSContext{ctx: o.ctx}
for i, v := range args {
ja[i] = ctx.JSValue(v).val
}
}
ret := C.JSObjectCallAsFunction(o.ctx, o.obj, thisObj, C.size_t(nargs), jargs, nil)
return &JSValue{ctx: o.ctx, val: ret}
}
// Sets a property on an object.
func (o *JSObject) SetProperty(name string, value *JSValue) {
C.JSObjectSetProperty(o.ctx, o.obj, makeJSString(name), value.val, 0, nil)
}
// Gets a property from an object.
func (o *JSObject) Property(name string) *JSValue {
return &JSValue{ctx: o.ctx, val: C.JSObjectGetProperty(o.ctx, o.obj, makeJSString(name), nil)}
}
// Gets the names of an object's enumerable properties.
func (o *JSObject) PropertyNames() []string {
anames := C.JSObjectCopyPropertyNames(o.ctx, o.obj)
nnames := int(C.JSPropertyNameArrayGetCount(anames))
if nnames == 0 {
return nil
}
names := make([]string, nnames)
for i := 0; i < len(names); i++ {
n := C.JSPropertyNameArrayGetNameAtIndex(anames, C.size_t(i))
names[i] = decodeJSString(n)
}
return names
}