From 255690eee0c95b53f83e5e0a71e867661b2b9ab4 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sat, 30 Sep 2023 15:23:56 +1000 Subject: [PATCH] [darwin] Add webview preferences --- v3/go.mod | 1 + v3/go.sum | 2 + v3/pkg/application/options_mac.go | 14 ++++++ v3/pkg/application/webview_window.go | 7 +++ v3/pkg/application/webview_window_darwin.go | 51 ++++++++++++++++++++- 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/v3/go.mod b/v3/go.mod index 45f4239c7..7a0037950 100644 --- a/v3/go.mod +++ b/v3/go.mod @@ -17,6 +17,7 @@ require ( github.com/leaanthony/clir v1.6.0 github.com/leaanthony/go-ansi-parser v1.6.1 github.com/leaanthony/gosod v1.0.3 + github.com/leaanthony/u v1.1.0 github.com/leaanthony/winicon v1.0.0 github.com/lmittmann/tint v1.0.0 github.com/markbates/goth v1.77.0 diff --git a/v3/go.sum b/v3/go.sum index a4fe60434..60bd79709 100644 --- a/v3/go.sum +++ b/v3/go.sum @@ -235,6 +235,8 @@ github.com/leaanthony/go-ansi-parser v1.6.1/go.mod h1:+vva/2y4alzVmmIEpk9QDhA7vL github.com/leaanthony/gosod v1.0.3 h1:Fnt+/B6NjQOVuCWOKYRREZnjGyvg+mEhd1nkkA04aTQ= github.com/leaanthony/gosod v1.0.3/go.mod h1:BJ2J+oHsQIyIQpnLPjnqFGTMnOZXDbvWtRCSG7jGxs4= github.com/leaanthony/slicer v1.5.0/go.mod h1:FwrApmf8gOrpzEWM2J/9Lh79tyq8KTX5AzRtwV7m4AY= +github.com/leaanthony/u v1.1.0 h1:2n0d2BwPVXSUq5yhe8lJPHdxevE2qK5G99PMStMZMaI= +github.com/leaanthony/u v1.1.0/go.mod h1:9+o6hejoRljvZ3BzdYlVL0JYCwtnAsVuN9pVTQcaRfI= github.com/leaanthony/winicon v1.0.0 h1:ZNt5U5dY71oEoKZ97UVwJRT4e+5xo5o/ieKuHuk8NqQ= github.com/leaanthony/winicon v1.0.0/go.mod h1:en5xhijl92aphrJdmRPlh4NI1L6wq3gEm0LpXAPghjU= github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBBOiM/uP402WwN8Y= diff --git a/v3/pkg/application/options_mac.go b/v3/pkg/application/options_mac.go index 5b9546d8f..c79287fc6 100644 --- a/v3/pkg/application/options_mac.go +++ b/v3/pkg/application/options_mac.go @@ -1,6 +1,7 @@ package application import "github.com/wailsapp/wails/v3/pkg/events" +import "github.com/leaanthony/u" // MacBackdrop is the backdrop type for macOS type MacBackdrop int @@ -48,6 +49,19 @@ type MacWindow struct { // EnableFraudulentWebsiteWarnings will enable warnings for fraudulent websites. // Default: false EnableFraudulentWebsiteWarnings bool + + // WebviewPreferences contains preferences for the webview + WebviewPreferences MacWebviewPreferences +} + +// MacWebviewPreferences contains preferences for the Mac webview +type MacWebviewPreferences struct { + // TabFocusesLinks will enable tabbing to links + TabFocusesLinks u.Bool + // TextInteractionEnabled will enable text interaction + TextInteractionEnabled u.Bool + // FullscreenEnabled will enable fullscreen + FullscreenEnabled u.Bool } // MacTitleBar contains options for the Mac titlebar diff --git a/v3/pkg/application/webview_window.go b/v3/pkg/application/webview_window.go index 630688f47..addd37a7f 100644 --- a/v3/pkg/application/webview_window.go +++ b/v3/pkg/application/webview_window.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/leaanthony/u" "runtime" "strings" "sync" @@ -12,6 +13,12 @@ import ( "github.com/wailsapp/wails/v3/pkg/events" ) +// Enabled means the feature should be enabled +var Enabled = u.True + +// Disabled means the feature should be disabled +var Disabled = u.False + type ( webviewWindowImpl interface { setTitle(title string) diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index 708120ef5..e53c88826 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -14,11 +14,16 @@ package application #import #import "webview_window_darwin_drag.h" +struct WebviewPreferences { + bool *TabFocusesLinks; + bool *TextInteractionEnabled; + bool *FullscreenEnabled; +}; 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) { +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; @@ -52,6 +57,25 @@ void* windowNew(unsigned int id, int width, int height, bool fraudulentWebsiteWa WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init]; [config autorelease]; + // Set preferences + if (preferences.TabFocusesLinks != NULL) { + config.preferences.tabFocusesLinks = *preferences.TabFocusesLinks; + } + + if (@available(macOS 11.3, *)) { + if (preferences.TextInteractionEnabled != NULL) { + config.preferences.textInteractionEnabled = *preferences.TextInteractionEnabled; + } + } + + if (@available(macOS 12.3, *)) { + if (preferences.FullscreenEnabled != NULL) { + config.preferences.elementFullscreenEnabled = *preferences.FullscreenEnabled; + } + } + + + config.suppressesIncrementalRendering = true; config.applicationNameForUserAgent = @"wails.io"; [config setURLSchemeHandler:delegate forURLScheme:@"wails"]; @@ -1023,6 +1047,29 @@ func (w *macosWebviewWindow) height() int { return int(height) } +func bool2CboolPtr(value bool) *C.bool { + v := C.bool(value) + return &v +} + +func (w *macosWebviewWindow) getWebviewPreferences() C.struct_WebviewPreferences { + wvprefs := w.parent.options.Mac.WebviewPreferences + + var result C.struct_WebviewPreferences + + if wvprefs.TextInteractionEnabled.IsSet() { + result.TextInteractionEnabled = bool2CboolPtr(wvprefs.TextInteractionEnabled.Get()) + } + if wvprefs.TabFocusesLinks.IsSet() { + result.TabFocusesLinks = bool2CboolPtr(wvprefs.TabFocusesLinks.Get()) + } + if wvprefs.FullscreenEnabled.IsSet() { + result.FullscreenEnabled = bool2CboolPtr(wvprefs.FullscreenEnabled.Get()) + } + + return result +} + func (w *macosWebviewWindow) run() { for eventId := range w.parent.eventListeners { w.on(eventId) @@ -1030,12 +1077,14 @@ func (w *macosWebviewWindow) run() { globalApplication.dispatchOnMainThread(func() { options := w.parent.options macOptions := options.Mac + w.nsWindow = C.windowNew(C.uint(w.parent.id), C.int(options.Width), C.int(options.Height), C.bool(macOptions.EnableFraudulentWebsiteWarnings), C.bool(options.Frameless), C.bool(options.EnableDragAndDrop), + w.getWebviewPreferences(), ) w.setTitle(options.Title) w.setAlwaysOnTop(options.AlwaysOnTop)