-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathwebview_window_darwin.go
More file actions
1569 lines (1309 loc) · 46.4 KB
/
Copy pathwebview_window_darwin.go
File metadata and controls
1569 lines (1309 loc) · 46.4 KB
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
//go:build darwin && !ios
package application
/*
#cgo CFLAGS: -mmacosx-version-min=10.13 -x objective-c
#cgo LDFLAGS: -framework Cocoa -framework WebKit -framework QuartzCore
#include "application_darwin.h"
#include "webview_window_darwin.h"
#include <stdlib.h>
#include "Cocoa/Cocoa.h"
#import <WebKit/WebKit.h>
#import <AppKit/AppKit.h>
#import "webview_window_darwin_drag.h"
struct WebviewPreferences {
bool *TabFocusesLinks;
bool *TextInteractionEnabled;
bool *FullscreenEnabled;
bool *AllowsBackForwardNavigationGestures;
};
extern void registerListener(unsigned int event);
// Create a new Window
void* windowNew(unsigned int id, int width, int height, bool fraudulentWebsiteWarningEnabled, bool frameless, bool enableDragAndDrop, struct WebviewPreferences preferences) {
NSWindowStyleMask styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
if (frameless) {
styleMask = NSWindowStyleMaskBorderless | NSWindowStyleMaskResizable;
}
WebviewWindow* window = [[WebviewWindow alloc] initWithContentRect:NSMakeRect(0, 0, width-1, height-1)
styleMask:styleMask
backing:NSBackingStoreBuffered
defer:NO];
// Note: collectionBehavior is set later via windowSetCollectionBehavior()
// to allow user configuration of Space and fullscreen behavior
// Create delegate
WebviewWindowDelegate* delegate = [[WebviewWindowDelegate alloc] init];
[delegate autorelease];
// Set delegate
[window setDelegate:delegate];
delegate.windowId = id;
// Add NSView to window
NSView* view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, width-1, height-1)];
[view autorelease];
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
if( frameless ) {
[view setWantsLayer:YES];
view.layer.cornerRadius = 8.0;
}
[window setContentView:view];
// Embed wkwebview in window
NSRect frame = NSMakeRect(0, 0, width, height);
WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init];
[config autorelease];
// Set preferences
if (preferences.TabFocusesLinks != NULL) {
config.preferences.tabFocusesLinks = *preferences.TabFocusesLinks;
}
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 110300
if (@available(macOS 11.3, *)) {
if (preferences.TextInteractionEnabled != NULL) {
config.preferences.textInteractionEnabled = *preferences.TextInteractionEnabled;
}
}
#endif
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 120300
if (@available(macOS 12.3, *)) {
if (preferences.FullscreenEnabled != NULL) {
config.preferences.elementFullscreenEnabled = *preferences.FullscreenEnabled;
}
}
#endif
config.suppressesIncrementalRendering = true;
config.applicationNameForUserAgent = @"wails.io";
[config setURLSchemeHandler:delegate forURLScheme:@"wails"];
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
if (@available(macOS 10.15, *)) {
config.preferences.fraudulentWebsiteWarningEnabled = fraudulentWebsiteWarningEnabled;
}
#endif
// Setup user content controller
WKUserContentController* userContentController = [WKUserContentController new];
[userContentController autorelease];
[userContentController addScriptMessageHandler:delegate name:@"external"];
config.userContentController = userContentController;
WKWebView* webView = [[WKWebView alloc] initWithFrame:frame configuration:config];
[webView autorelease];
// Set allowsBackForwardNavigationGestures if specified
if (preferences.AllowsBackForwardNavigationGestures != NULL) {
webView.allowsBackForwardNavigationGestures = *preferences.AllowsBackForwardNavigationGestures;
}
[view addSubview:webView];
// support webview events
[webView setNavigationDelegate:delegate];
// Ensure webview resizes with the window
[webView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
if( enableDragAndDrop ) {
WebviewDrag* dragView = [[WebviewDrag alloc] initWithFrame:NSMakeRect(0, 0, width-1, height-1)];
[dragView autorelease];
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[view addSubview:dragView];
dragView.windowId = id;
}
window.webView = webView;
return window;
}
void printWindowStyle(void *window) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
NSWindowStyleMask styleMask = [nsWindow styleMask];
// Get delegate
WebviewWindowDelegate* windowDelegate = (WebviewWindowDelegate*)[nsWindow delegate];
printf("Window %d style mask: ", windowDelegate.windowId);
if (styleMask & NSWindowStyleMaskTitled)
{
printf("NSWindowStyleMaskTitled ");
}
if (styleMask & NSWindowStyleMaskClosable)
{
printf("NSWindowStyleMaskClosable ");
}
if (styleMask & NSWindowStyleMaskMiniaturizable)
{
printf("NSWindowStyleMaskMiniaturizable ");
}
if (styleMask & NSWindowStyleMaskResizable)
{
printf("NSWindowStyleMaskResizable ");
}
if (styleMask & NSWindowStyleMaskFullSizeContentView)
{
printf("NSWindowStyleMaskFullSizeContentView ");
}
if (styleMask & NSWindowStyleMaskNonactivatingPanel)
{
printf("NSWindowStyleMaskNonactivatingPanel ");
}
if (styleMask & NSWindowStyleMaskFullScreen)
{
printf("NSWindowStyleMaskFullScreen ");
}
if (styleMask & NSWindowStyleMaskBorderless)
{
printf("MSWindowStyleMaskBorderless ");
}
printf("\n");
}
// setInvisibleTitleBarHeight sets the invisible title bar height
void setInvisibleTitleBarHeight(void* window, unsigned int height) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
// Get delegate
WebviewWindowDelegate* delegate = (WebviewWindowDelegate*)[nsWindow delegate];
// Set height
delegate.invisibleTitleBarHeight = height;
}
// Make NSWindow transparent
void windowSetTransparent(void* nsWindow) {
[(WebviewWindow*)nsWindow setOpaque:NO];
[(WebviewWindow*)nsWindow setBackgroundColor:[NSColor clearColor]];
}
void windowSetInvisibleTitleBar(void* nsWindow, unsigned int height) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
WebviewWindowDelegate* delegate = (WebviewWindowDelegate*)[window delegate];
delegate.invisibleTitleBarHeight = height;
}
// Set the title of the NSWindow
void windowSetTitle(void* nsWindow, char* title) {
NSString* nsTitle = [NSString stringWithUTF8String:title];
[(WebviewWindow*)nsWindow setTitle:nsTitle];
free(title);
}
// Set the size of the NSWindow
void windowSetSize(void* nsWindow, int width, int height) {
// Set window size on main thread
WebviewWindow* window = (WebviewWindow*)nsWindow;
NSSize contentSize = [window contentRectForFrameRect:NSMakeRect(0, 0, width, height)].size;
[window setContentSize:contentSize];
[window setFrame:NSMakeRect(window.frame.origin.x, window.frame.origin.y, width, height) display:YES animate:YES];
}
// Set NSWindow always on top
void windowSetAlwaysOnTop(void* nsWindow, bool alwaysOnTop) {
// Set window always on top on main thread
[(WebviewWindow*)nsWindow setLevel:alwaysOnTop ? NSFloatingWindowLevel : NSNormalWindowLevel];
}
void setNormalWindowLevel(void* nsWindow) { [(WebviewWindow*)nsWindow setLevel:NSNormalWindowLevel]; }
void setFloatingWindowLevel(void* nsWindow) { [(WebviewWindow*)nsWindow setLevel:NSFloatingWindowLevel];}
void setPopUpMenuWindowLevel(void* nsWindow) { [(WebviewWindow*)nsWindow setLevel:NSPopUpMenuWindowLevel]; }
void setMainMenuWindowLevel(void* nsWindow) { [(WebviewWindow*)nsWindow setLevel:NSMainMenuWindowLevel]; }
void setStatusWindowLevel(void* nsWindow) { [(WebviewWindow*)nsWindow setLevel:NSStatusWindowLevel]; }
void setModalPanelWindowLevel(void* nsWindow) { [(WebviewWindow*)nsWindow setLevel:NSModalPanelWindowLevel]; }
void setScreenSaverWindowLevel(void* nsWindow) { [(WebviewWindow*)nsWindow setLevel:NSScreenSaverWindowLevel]; }
void setTornOffMenuWindowLevel(void* nsWindow) { [(WebviewWindow*)nsWindow setLevel:NSTornOffMenuWindowLevel]; }
// Set NSWindow collection behavior for Spaces and fullscreen
// The behavior parameter is a bitmask that can combine multiple NSWindowCollectionBehavior values
void windowSetCollectionBehavior(void* nsWindow, int behavior) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
if (behavior == 0) {
// Default: use FullScreenPrimary for backwards compatibility
window.collectionBehavior = NSWindowCollectionBehaviorFullScreenPrimary;
} else {
// Pass through the combined bitmask directly
window.collectionBehavior = (NSWindowCollectionBehavior)behavior;
}
}
// Load URL in NSWindow
void navigationLoadURL(void* nsWindow, char* url) {
// Load URL on main thread
NSURL* nsURL = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
NSURLRequest* request = [NSURLRequest requestWithURL:nsURL];
WebviewWindow* window = (WebviewWindow*)nsWindow;
[window.webView loadRequest:request];
free(url);
}
// Set NSWindow resizable
void windowSetResizable(void* nsWindow, bool resizable) {
// Set window resizable on main thread
WebviewWindow* window = (WebviewWindow*)nsWindow;
if (resizable) {
NSWindowStyleMask styleMask = [window styleMask] | NSWindowStyleMaskResizable;
[window setStyleMask:styleMask];
} else {
NSWindowStyleMask styleMask = [window styleMask] & ~NSWindowStyleMaskResizable;
[window setStyleMask:styleMask];
}
}
// Set NSWindow min size
void windowSetMinSize(void* nsWindow, int width, int height) {
// Set window min size on main thread
WebviewWindow* window = (WebviewWindow*)nsWindow;
NSSize contentSize = [window contentRectForFrameRect:NSMakeRect(0, 0, width, height)].size;
[window setContentMinSize:contentSize];
NSSize size = { width, height };
[window setMinSize:size];
}
// Set NSWindow max size
void windowSetMaxSize(void* nsWindow, int width, int height) {
// Set window max size on main thread
NSSize size = { FLT_MAX, FLT_MAX };
size.width = width > 0 ? width : FLT_MAX;
size.height = height > 0 ? height : FLT_MAX;
WebviewWindow* window = (WebviewWindow*)nsWindow;
NSSize contentSize = [window contentRectForFrameRect:NSMakeRect(0, 0, size.width, size.height)].size;
[window setContentMaxSize:contentSize];
[window setMaxSize:size];
}
// windowZoomReset
void windowZoomReset(void* nsWindow) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
[window.webView setMagnification:1.0];
}
// windowZoomSet
void windowZoomSet(void* nsWindow, double zoom) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
// Reset zoom
[window.webView setMagnification:zoom];
}
// windowZoomGet
float windowZoomGet(void* nsWindow) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
// Get zoom
return [window.webView magnification];
}
// windowZoomIn
void windowZoomIn(void* nsWindow) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
// Zoom in
[window.webView setMagnification:window.webView.magnification + 0.05];
}
// windowZoomOut
void windowZoomOut(void* nsWindow) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
// Zoom out
if( window.webView.magnification > 1.05 ) {
[window.webView setMagnification:window.webView.magnification - 0.05];
} else {
[window.webView setMagnification:1.0];
}
}
// set the window position relative to the screen
void windowSetRelativePosition(void* nsWindow, int x, int y) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
NSScreen* screen = [window screen];
if( screen == NULL ) {
screen = [NSScreen mainScreen];
}
NSRect windowFrame = [window frame];
NSRect screenFrame = [screen frame];
windowFrame.origin.x = screenFrame.origin.x + (float)x;
windowFrame.origin.y = (screenFrame.origin.y + screenFrame.size.height) - windowFrame.size.height - (float)y;
[window setFrame:windowFrame display:TRUE animate:FALSE];
}
// Execute JS in NSWindow
void windowExecJS(void* nsWindow, const char* js) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
[window.webView evaluateJavaScript:[NSString stringWithUTF8String:js] completionHandler:nil];
free((void*)js);
}
// Execute JS without allocation - buffer is NOT freed
void windowExecJSNoAlloc(void* nsWindow, const char* js) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
[window.webView evaluateJavaScript:[NSString stringWithUTF8String:js] completionHandler:nil];
}
// Make NSWindow backdrop translucent
void windowSetTranslucent(void* nsWindow) {
// Get window
WebviewWindow* window = (WebviewWindow*)nsWindow;
id contentView = [window contentView];
NSVisualEffectView *effectView = [NSVisualEffectView alloc];
NSRect bounds = [contentView bounds];
[effectView initWithFrame:bounds];
[effectView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[effectView setBlendingMode:NSVisualEffectBlendingModeBehindWindow];
[effectView setState:NSVisualEffectStateActive];
[contentView addSubview:effectView positioned:NSWindowBelow relativeTo:nil];
}
// Make webview background transparent
void webviewSetTransparent(void* nsWindow) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
// Set webview background transparent
[window.webView setValue:@NO forKey:@"drawsBackground"];
}
// Set webview background colour
void webviewSetBackgroundColour(void* nsWindow, int r, int g, int b, int alpha) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
// Set webview background color
[window.webView setValue:[NSColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:alpha/255.0] forKey:@"backgroundColor"];
}
// Set the window background colour
void windowSetBackgroundColour(void* nsWindow, int r, int g, int b, int alpha) {
[(WebviewWindow*)nsWindow setBackgroundColor:[NSColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:alpha/255.0]];
}
bool windowIsMaximised(void* nsWindow) {
return [(WebviewWindow*)nsWindow isZoomed];
}
bool windowIsFullscreen(void* nsWindow) {
return [(WebviewWindow*)nsWindow styleMask] & NSWindowStyleMaskFullScreen;
}
bool windowIsMinimised(void* nsWindow) {
return [(WebviewWindow*)nsWindow isMiniaturized];
}
bool windowIsFocused(void* nsWindow) {
return [(WebviewWindow*)nsWindow isKeyWindow];
}
// Set Window fullscreen
void windowFullscreen(void* nsWindow) {
if( windowIsFullscreen(nsWindow) ) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[(WebviewWindow*)nsWindow toggleFullScreen:nil];
});}
void windowUnFullscreen(void* nsWindow) {
if( !windowIsFullscreen(nsWindow) ) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[(WebviewWindow*)nsWindow toggleFullScreen:nil];
});
}
// restore window to normal size
void windowRestore(void* nsWindow) {
// If window is fullscreen
if([(WebviewWindow*)nsWindow styleMask] & NSWindowStyleMaskFullScreen) {
[(WebviewWindow*)nsWindow toggleFullScreen:nil];
}
// If window is maximised
if([(WebviewWindow*)nsWindow isZoomed]) {
[(WebviewWindow*)nsWindow zoom:nil];
}
// If window in minimised
if([(WebviewWindow*)nsWindow isMiniaturized]) {
[(WebviewWindow*)nsWindow deminiaturize:nil];
}
}
// disable window fullscreen button
void setFullscreenButtonEnabled(void* nsWindow, bool enabled) {
NSButton *fullscreenButton = [(WebviewWindow*)nsWindow standardWindowButton:NSWindowZoomButton];
fullscreenButton.enabled = enabled;
}
// Set the titlebar style
void windowSetTitleBarAppearsTransparent(void* nsWindow, bool transparent) {
if( transparent ) {
[(WebviewWindow*)nsWindow setTitlebarAppearsTransparent:true];
} else {
[(WebviewWindow*)nsWindow setTitlebarAppearsTransparent:false];
}
}
// Set window fullsize content view
void windowSetFullSizeContent(void* nsWindow, bool fullSize) {
if( fullSize ) {
[(WebviewWindow*)nsWindow setStyleMask:[(WebviewWindow*)nsWindow styleMask] | NSWindowStyleMaskFullSizeContentView];
} else {
[(WebviewWindow*)nsWindow setStyleMask:[(WebviewWindow*)nsWindow styleMask] & ~NSWindowStyleMaskFullSizeContentView];
}
}
// Set Hide Titlebar
void windowSetHideTitleBar(void* nsWindow, bool hideTitlebar) {
if( hideTitlebar ) {
[(WebviewWindow*)nsWindow setStyleMask:[(WebviewWindow*)nsWindow styleMask] & ~NSWindowStyleMaskTitled];
} else {
[(WebviewWindow*)nsWindow setStyleMask:[(WebviewWindow*)nsWindow styleMask] | NSWindowStyleMaskTitled];
}
}
// Set Hide Title in Titlebar
void windowSetHideTitle(void* nsWindow, bool hideTitle) {
if( hideTitle ) {
[(WebviewWindow*)nsWindow setTitleVisibility:NSWindowTitleHidden];
} else {
[(WebviewWindow*)nsWindow setTitleVisibility:NSWindowTitleVisible];
}
}
// Set Window use toolbar
void windowSetUseToolbar(void* nsWindow, bool useToolbar) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
if( useToolbar ) {
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"wails.toolbar"];
[toolbar autorelease];
[window setToolbar:toolbar];
} else {
[window setToolbar:nil];
}
}
// Set window toolbar style
void windowSetToolbarStyle(void* nsWindow, int style) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
if (@available(macOS 11.0, *)) {
NSToolbar* toolbar = [window toolbar];
if ( toolbar == nil ) {
return;
}
[window setToolbarStyle:style];
}
#endif
}
// Set Hide Toolbar Separator
void windowSetHideToolbarSeparator(void* nsWindow, bool hideSeparator) {
NSToolbar* toolbar = [(WebviewWindow*)nsWindow toolbar];
if( toolbar == nil ) {
return;
}
[toolbar setShowsBaselineSeparator:!hideSeparator];
}
// Configure the toolbar auto-hide feature
void windowSetShowToolbarWhenFullscreen(void* window, bool setting) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
// Get delegate
WebviewWindowDelegate* delegate = (WebviewWindowDelegate*)[nsWindow delegate];
// Set height
delegate.showToolbarWhenFullscreen = setting;
}
// Set Window appearance type
void windowSetAppearanceTypeByName(void* nsWindow, const char *appearanceName) {
// set window appearance type by name
// Convert appearance name to NSString
NSString* appearanceNameString = [NSString stringWithUTF8String:appearanceName];
// Set appearance
[(WebviewWindow*)nsWindow setAppearance:[NSAppearance appearanceNamed:appearanceNameString]];
free((void*)appearanceName);
}
// Center window on current monitor
void windowCenter(void* nsWindow) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
NSScreen* screen = [window screen];
if (screen == NULL) {
screen = [NSScreen mainScreen];
}
NSRect screenFrame = [screen visibleFrame];
NSRect windowFrame = [window frame];
CGFloat x = screenFrame.origin.x + (screenFrame.size.width - windowFrame.size.width) / 2;
CGFloat y = screenFrame.origin.y + (screenFrame.size.height - windowFrame.size.height) / 2;
[window setFrame:NSMakeRect(x, y, windowFrame.size.width, windowFrame.size.height) display:YES];
}
// Get the current size of the window
void windowGetSize(void* nsWindow, int* width, int* height) {
NSRect frame = [(WebviewWindow*)nsWindow frame];
*width = frame.size.width;
*height = frame.size.height;
}
// Get window width
int windowGetWidth(void* nsWindow) {
return [(WebviewWindow*)nsWindow frame].size.width;
}
// Get window height
int windowGetHeight(void* nsWindow) {
return [(WebviewWindow*)nsWindow frame].size.height;
}
// Get window position
void windowGetRelativePosition(void* nsWindow, int* x, int* y) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
NSRect frame = [window frame];
*x = frame.origin.x;
// Translate to screen coordinates so Y=0 is the top of the screen
NSScreen* screen = [window screen];
if( screen == NULL ) {
screen = [NSScreen mainScreen];
}
NSRect screenFrame = [screen frame];
*y = screenFrame.size.height - frame.origin.y - frame.size.height;
}
// Get absolute window position (in screen coordinates with Y=0 at top, scaled for DPI)
void windowGetPosition(void* nsWindow, int* x, int* y) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
NSScreen* screen = [window screen];
if (screen == NULL) {
screen = [NSScreen mainScreen];
}
CGFloat scale = [screen backingScaleFactor];
NSRect frame = [window frame];
NSRect screenFrame = [screen frame];
// Convert to top-origin coordinates and apply scale (matching windowSetPosition)
*x = frame.origin.x * scale;
*y = (screenFrame.size.height - frame.origin.y - frame.size.height) * scale;
}
void windowSetPosition(void* nsWindow, int x, int y) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
NSScreen* screen = [window screen];
if (screen == NULL) {
screen = [NSScreen mainScreen];
}
// Get the scale of the screen
CGFloat scale = [screen backingScaleFactor];
NSRect frame = [window frame];
// Scale the position
frame.origin.x = x / scale;
frame.origin.y = (screen.frame.size.height - frame.size.height) - (y / scale);
// Set the frame
[window setFrame:frame display:YES];
}
// Destroy window
void windowDestroy(void* nsWindow) {
[(WebviewWindow*)nsWindow close];
}
// Remove drop shadow from window
void windowSetShadow(void* nsWindow, bool hasShadow) {
[(WebviewWindow*)nsWindow setHasShadow:hasShadow];
}
// windowClose closes the current window
static void windowClose(void *window) {
[(WebviewWindow*)window close];
}
// windowZoom
static void windowZoom(void *window) {
[(WebviewWindow*)window zoom:nil];
}
// webviewRenderHTML renders the given HTML
static void windowRenderHTML(void *window, const char *html) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
// get window delegate
WebviewWindowDelegate* windowDelegate = (WebviewWindowDelegate*)[nsWindow delegate];
// render html
[nsWindow.webView loadHTMLString:[NSString stringWithUTF8String:html] baseURL:nil];
}
static void windowInjectCSS(void *window, const char *css) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
// inject css
[nsWindow.webView evaluateJavaScript:[NSString stringWithFormat:@"(function() { var style = document.createElement('style'); style.appendChild(document.createTextNode('%@')); document.head.appendChild(style); })();", [NSString stringWithUTF8String:css]] completionHandler:nil];
free((void*)css);
}
static void windowMinimise(void *window) {
[(WebviewWindow*)window miniaturize:nil];
}
// zoom maximizes the window to the screen dimensions
static void windowMaximise(void *window) {
[(WebviewWindow*)window zoom:nil];
}
static bool isFullScreen(void *window) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
long mask = [nsWindow styleMask];
return (mask & NSWindowStyleMaskFullScreen) == NSWindowStyleMaskFullScreen;
}
static bool isVisible(void *window) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
return (nsWindow.occlusionState & NSWindowOcclusionStateVisible) == NSWindowOcclusionStateVisible;
}
// windowSetFullScreen
static void windowSetFullScreen(void *window, bool fullscreen) {
if (isFullScreen(window)) {
return;
}
WebviewWindow* nsWindow = (WebviewWindow*)window;
windowSetMaxSize(nsWindow, 0, 0);
windowSetMinSize(nsWindow, 0, 0);
[nsWindow toggleFullScreen:nil];
}
// windowUnminimise
static void windowUnminimise(void *window) {
[(WebviewWindow*)window deminiaturize:nil];
}
// windowUnmaximise
static void windowUnmaximise(void *window) {
[(WebviewWindow*)window zoom:nil];
}
static void windowDisableSizeConstraints(void *window) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
// disable size constraints
[nsWindow setContentMinSize:CGSizeZero];
[nsWindow setContentMaxSize:CGSizeZero];
}
static void windowShow(void *window) {
[(WebviewWindow*)window makeKeyAndOrderFront:nil];
}
static void windowHide(void *window) {
[(WebviewWindow*)window orderOut:nil];
}
// setButtonState sets the state of the given button
// 0 = enabled
// 1 = disabled
// 2 = hidden
static void setButtonState(void *button, int state) {
if (button == nil) {
return;
}
NSButton *nsbutton = (NSButton*)button;
nsbutton.hidden = state == 2;
nsbutton.enabled = state != 1;
}
// setMinimiseButtonState sets the minimise button state
static void setMinimiseButtonState(void *window, int state) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
NSButton *minimiseButton = [nsWindow standardWindowButton:NSWindowMiniaturizeButton];
setButtonState(minimiseButton, state);
}
// setMaximiseButtonState sets the maximise button state
static void setMaximiseButtonState(void *window, int state) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
NSButton *maximiseButton = [nsWindow standardWindowButton:NSWindowZoomButton];
setButtonState(maximiseButton, state);
}
// setCloseButtonState sets the close button state
static void setCloseButtonState(void *window, int state) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
NSButton *closeButton = [nsWindow standardWindowButton:NSWindowCloseButton];
setButtonState(closeButton, state);
}
// windowShowMenu opens an NSMenu at the given coordinates
static void windowShowMenu(void *window, void *menu, int x, int y) {
NSMenu* nsMenu = (NSMenu*)menu;
WKWebView* webView = ((WebviewWindow*)window).webView;
NSPoint point = NSMakePoint(x, y);
[nsMenu popUpMenuPositioningItem:nil atLocation:point inView:webView];
}
// Make the given window frameless
static void windowSetFrameless(void *window, bool frameless) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
// set the window style to be frameless
if (frameless) {
[nsWindow setStyleMask:([nsWindow styleMask] | NSWindowStyleMaskFullSizeContentView)];
} else {
[nsWindow setStyleMask:([nsWindow styleMask] & ~NSWindowStyleMaskFullSizeContentView)];
}
}
static void startDrag(void *window) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
// Get delegate
WebviewWindowDelegate* windowDelegate = (WebviewWindowDelegate*)[nsWindow delegate];
// start drag
[windowDelegate startDrag:nsWindow];
}
// Credit: https://stackoverflow.com/q/33319295
static void windowPrint(void *window) {
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
// Check if macOS 11.0 or newer
if (@available(macOS 11.0, *)) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
WebviewWindowDelegate* windowDelegate = (WebviewWindowDelegate*)[nsWindow delegate];
WKWebView* webView = nsWindow.webView;
// TODO: Think about whether to expose this as config
NSPrintInfo *pInfo = [NSPrintInfo sharedPrintInfo];
pInfo.horizontalPagination = NSPrintingPaginationModeAutomatic;
pInfo.verticalPagination = NSPrintingPaginationModeAutomatic;
pInfo.verticallyCentered = YES;
pInfo.horizontallyCentered = YES;
pInfo.orientation = NSPaperOrientationLandscape;
pInfo.leftMargin = 30;
pInfo.rightMargin = 30;
pInfo.topMargin = 30;
pInfo.bottomMargin = 30;
NSPrintOperation *po = [webView printOperationWithPrintInfo:pInfo];
po.showsPrintPanel = YES;
po.showsProgressPanel = YES;
// Without the next line you get an exception. Also it seems to
// completely ignore the values in the rect. I tried changing them
// in both x and y direction to include content scrolled off screen.
// It had no effect whatsoever in either direction.
po.view.frame = webView.bounds;
// [printOperation runOperation] DOES NOT WORK WITH WKWEBVIEW, use
[po runOperationModalForWindow:nsWindow delegate:windowDelegate didRunSelector:nil contextInfo:nil];
}
#endif
}
void setWindowEnabled(void *window, bool enabled) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
[nsWindow setIgnoresMouseEvents:!enabled];
}
void windowSetEnabled(void *window, bool enabled) {
// TODO: Implement
}
void windowFocus(void *window) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
// If the current application is not active, activate it
if (![[NSApplication sharedApplication] isActive]) {
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
[nsWindow makeKeyAndOrderFront:nil];
[nsWindow makeKeyWindow];
}
static bool isIgnoreMouseEvents(void *nsWindow) {
NSWindow *window = (__bridge NSWindow *)nsWindow;
return [window ignoresMouseEvents];
}
static void setIgnoreMouseEvents(void *nsWindow, bool ignore) {
NSWindow *window = (__bridge NSWindow *)nsWindow;
[window setIgnoresMouseEvents:ignore];
}
static void setContentProtection(void *nsWindow, bool enabled) {
NSWindow *window = (__bridge NSWindow *)nsWindow;
if( ! [window respondsToSelector:@selector(setSharingType:)]) {
return;
}
if( enabled ) {
[window setSharingType:NSWindowSharingNone];
} else {
[window setSharingType:NSWindowSharingReadOnly];
}
}
*/
import "C"
import (
"fmt"
"sync"
"sync/atomic"
"unsafe"
"github.com/wailsapp/wails/v3/internal/assetserver"
"github.com/wailsapp/wails/v3/internal/runtime"
"github.com/wailsapp/wails/v3/pkg/events"
)
type macosWebviewWindow struct {
nsWindow unsafe.Pointer
parent *WebviewWindow
}
func (w *macosWebviewWindow) handleKeyEvent(acceleratorString string) {
// Parse acceleratorString
accelerator, err := parseAccelerator(acceleratorString)
if err != nil {
globalApplication.error("unable to parse accelerator: %w", err)
return
}
w.parent.processKeyBinding(accelerator.String())
}
func (w *macosWebviewWindow) getBorderSizes() *LRTB {
return &LRTB{}
}
func (w *macosWebviewWindow) isFocused() bool {
return bool(C.windowIsFocused(w.nsWindow))
}
func (w *macosWebviewWindow) setPosition(x int, y int) {
C.windowSetPosition(w.nsWindow, C.int(x), C.int(y))
}
func (w *macosWebviewWindow) print() error {
C.windowPrint(w.nsWindow)
return nil
}
func (w *macosWebviewWindow) startResize(_ string) error {
// Never called. Handled natively by the OS.
return nil
}
func (w *macosWebviewWindow) focus() {
// Make the window key and main
C.windowFocus(w.nsWindow)
}
func (w *macosWebviewWindow) openContextMenu(menu *Menu, data *ContextMenuData) {
// Reuse existing impl if available, otherwise create new one
if menu.impl == nil {
menu.impl = newMenuImpl(menu)
}
thisMenu := menu.impl.(*macosMenu)
thisMenu.update()
C.windowShowMenu(w.nsWindow, thisMenu.nsMenu, C.int(data.X), C.int(data.Y))
}
func (w *macosWebviewWindow) getZoom() float64 {
return float64(C.windowZoomGet(w.nsWindow))
}
func (w *macosWebviewWindow) setZoom(zoom float64) {
C.windowZoomSet(w.nsWindow, C.double(zoom))
}
func (w *macosWebviewWindow) setFrameless(frameless bool) {
C.windowSetFrameless(w.nsWindow, C.bool(frameless))
if frameless {
C.windowSetTitleBarAppearsTransparent(w.nsWindow, C.bool(true))
C.windowSetHideTitle(w.nsWindow, C.bool(true))
} else {
macOptions := w.parent.options.Mac
appearsTransparent := macOptions.TitleBar.AppearsTransparent
hideTitle := macOptions.TitleBar.HideTitle
C.windowSetTitleBarAppearsTransparent(w.nsWindow, C.bool(appearsTransparent))
C.windowSetHideTitle(w.nsWindow, C.bool(hideTitle))
}
}
func (w *macosWebviewWindow) setHasShadow(hasShadow bool) {
C.windowSetShadow(w.nsWindow, C.bool(hasShadow))
}
func (w *macosWebviewWindow) getScreen() (*Screen, error) {
return getScreenForWindow(w)
}
func (w *macosWebviewWindow) show() {
C.windowShow(w.nsWindow)
}
func (w *macosWebviewWindow) hide() {
globalApplication.debug("Window hiding", "windowId", w.parent.id, "title", w.parent.options.Title)
C.windowHide(w.nsWindow)
}
func (w *macosWebviewWindow) setFullscreenButtonEnabled(enabled bool) {
C.setFullscreenButtonEnabled(w.nsWindow, C.bool(enabled))
}
func (w *macosWebviewWindow) disableSizeConstraints() {
C.windowDisableSizeConstraints(w.nsWindow)
}
func (w *macosWebviewWindow) unfullscreen() {
C.windowUnFullscreen(w.nsWindow)
}
func (w *macosWebviewWindow) fullscreen() {
C.windowFullscreen(w.nsWindow)
}
func (w *macosWebviewWindow) unminimise() {
C.windowUnminimise(w.nsWindow)
}
func (w *macosWebviewWindow) unmaximise() {
C.windowUnmaximise(w.nsWindow)
}
func (w *macosWebviewWindow) maximise() {
C.windowMaximise(w.nsWindow)
}
func (w *macosWebviewWindow) minimise() {
C.windowMinimise(w.nsWindow)
}
func (w *macosWebviewWindow) on(eventID uint) {
//C.registerListener(C.uint(eventID))
}