From 05eddc65b155851baaf50bb1ec74433201fedb40 Mon Sep 17 00:00:00 2001 From: Fadi Khadra Date: Mon, 25 Sep 2023 00:15:55 +0200 Subject: [PATCH] [v2 mac] Allow to specify webview preferences (#2937) * [mac] allow to specify webview preferences --- v2/go.mod | 1 + v2/go.sum | 4 +++ .../frontend/desktop/darwin/Application.h | 2 +- .../frontend/desktop/darwin/Application.m | 4 +-- .../frontend/desktop/darwin/WailsContext.h | 7 +++- .../frontend/desktop/darwin/WailsContext.m | 12 ++++++- v2/internal/frontend/desktop/darwin/window.go | 19 ++++++++++- v2/pkg/options/mac/mac.go | 1 + v2/pkg/options/mac/preferences.go | 16 +++++++++ website/docs/reference/options.mdx | 34 +++++++++++++++++++ website/src/pages/changelog.mdx | 1 + 11 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 v2/pkg/options/mac/preferences.go diff --git a/v2/go.mod b/v2/go.mod index 93e178d81..60bf75abc 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -22,6 +22,7 @@ require ( github.com/leaanthony/go-ansi-parser v1.6.0 github.com/leaanthony/gosod v1.0.3 github.com/leaanthony/slicer v1.6.0 + github.com/leaanthony/u v1.1.0 github.com/leaanthony/winicon v1.0.0 github.com/matryer/is v1.4.0 github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 diff --git a/v2/go.sum b/v2/go.sum index 59a625411..7769626fe 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -119,6 +119,10 @@ github.com/leaanthony/gosod v1.0.3/go.mod h1:BJ2J+oHsQIyIQpnLPjnqFGTMnOZXDbvWtRC github.com/leaanthony/slicer v1.5.0/go.mod h1:FwrApmf8gOrpzEWM2J/9Lh79tyq8KTX5AzRtwV7m4AY= github.com/leaanthony/slicer v1.6.0 h1:1RFP5uiPJvT93TAHi+ipd3NACobkW53yUiBqZheE/Js= github.com/leaanthony/slicer v1.6.0/go.mod h1:o/Iz29g7LN0GqH3aMjWAe90381nyZlDNquK+mtH2Fj8= +github.com/leaanthony/u v1.0.1 h1:tJLpf9EsuCgSB02ojRxg8KRqMgRN6mCTvFwI55kxRFE= +github.com/leaanthony/u v1.0.1/go.mod h1:9+o6hejoRljvZ3BzdYlVL0JYCwtnAsVuN9pVTQcaRfI= +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/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c= diff --git a/v2/internal/frontend/desktop/darwin/Application.h b/v2/internal/frontend/desktop/darwin/Application.h index 32314df89..3c044faf3 100644 --- a/v2/internal/frontend/desktop/darwin/Application.h +++ b/v2/internal/frontend/desktop/darwin/Application.h @@ -17,7 +17,7 @@ #define WindowStartsMinimised 2 #define WindowStartsFullscreen 3 -WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int devtoolsEnabled, int defaultContextMenu, int windowStartState, int startsHidden, int minWidth, int minHeight, int maxWidth, int maxHeight, bool fraudulentWebsiteWarningEnabled); +WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int devtoolsEnabled, int defaultContextMenu, int windowStartState, int startsHidden, int minWidth, int minHeight, int maxWidth, int maxHeight, bool fraudulentWebsiteWarningEnabled, struct Preferences preferences); void Run(void*, const char* url); void SetTitle(void* ctx, const char *title); diff --git a/v2/internal/frontend/desktop/darwin/Application.m b/v2/internal/frontend/desktop/darwin/Application.m index e37d6bf4a..5b0fc5776 100644 --- a/v2/internal/frontend/desktop/darwin/Application.m +++ b/v2/internal/frontend/desktop/darwin/Application.m @@ -14,7 +14,7 @@ #import "WailsMenu.h" #import "WailsMenuItem.h" -WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int devtoolsEnabled, int defaultContextMenu, int windowStartState, int startsHidden, int minWidth, int minHeight, int maxWidth, int maxHeight, bool fraudulentWebsiteWarningEnabled) { +WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int devtoolsEnabled, int defaultContextMenu, int windowStartState, int startsHidden, int minWidth, int minHeight, int maxWidth, int maxHeight, bool fraudulentWebsiteWarningEnabled, struct Preferences preferences) { [NSApplication sharedApplication]; @@ -27,7 +27,7 @@ WailsContext* Create(const char* title, int width, int height, int frameless, in fullscreen = 1; } - [result CreateWindow:width :height :frameless :resizable :fullscreen :fullSizeContent :hideTitleBar :titlebarAppearsTransparent :hideTitle :useToolbar :hideToolbarSeparator :webviewIsTransparent :hideWindowOnClose :safeInit(appearance) :windowIsTranslucent :minWidth :minHeight :maxWidth :maxHeight :fraudulentWebsiteWarningEnabled]; + [result CreateWindow:width :height :frameless :resizable :fullscreen :fullSizeContent :hideTitleBar :titlebarAppearsTransparent :hideTitle :useToolbar :hideToolbarSeparator :webviewIsTransparent :hideWindowOnClose :safeInit(appearance) :windowIsTranslucent :minWidth :minHeight :maxWidth :maxHeight :fraudulentWebsiteWarningEnabled :preferences]; [result SetTitle:safeInit(title)]; [result Center]; diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.h b/v2/internal/frontend/desktop/darwin/WailsContext.h index c788ea6a6..77e35a783 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.h +++ b/v2/internal/frontend/desktop/darwin/WailsContext.h @@ -55,7 +55,12 @@ @property (retain) NSString* aboutTitle; @property (retain) NSString* aboutDescription; -- (void) CreateWindow:(int)width :(int)height :(bool)frameless :(bool)resizable :(bool)fullscreen :(bool)fullSizeContent :(bool)hideTitleBar :(bool)titlebarAppearsTransparent :(bool)hideTitle :(bool)useToolbar :(bool)hideToolbarSeparator :(bool)webviewIsTransparent :(bool)hideWindowOnClose :(NSString *)appearance :(bool)windowIsTranslucent :(int)minWidth :(int)minHeight :(int)maxWidth :(int)maxHeight :(bool)fraudulentWebsiteWarningEnabled; +struct Preferences { + bool *tabFocusesLinks; + bool *textInteractionEnabled; +}; + +- (void) CreateWindow:(int)width :(int)height :(bool)frameless :(bool)resizable :(bool)fullscreen :(bool)fullSizeContent :(bool)hideTitleBar :(bool)titlebarAppearsTransparent :(bool)hideTitle :(bool)useToolbar :(bool)hideToolbarSeparator :(bool)webviewIsTransparent :(bool)hideWindowOnClose :(NSString *)appearance :(bool)windowIsTranslucent :(int)minWidth :(int)minHeight :(int)maxWidth :(int)maxHeight :(bool)fraudulentWebsiteWarningEnabled :(struct Preferences)preferences; - (void) SetSize:(int)width :(int)height; - (void) SetPosition:(int)x :(int) y; - (void) SetMinSize:(int)minWidth :(int)minHeight; diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.m b/v2/internal/frontend/desktop/darwin/WailsContext.m index 35a0e1f2d..bd4e40439 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.m +++ b/v2/internal/frontend/desktop/darwin/WailsContext.m @@ -136,7 +136,7 @@ typedef void (^schemeTaskCaller)(id); return NO; } -- (void) CreateWindow:(int)width :(int)height :(bool)frameless :(bool)resizable :(bool)fullscreen :(bool)fullSizeContent :(bool)hideTitleBar :(bool)titlebarAppearsTransparent :(bool)hideTitle :(bool)useToolbar :(bool)hideToolbarSeparator :(bool)webviewIsTransparent :(bool)hideWindowOnClose :(NSString*)appearance :(bool)windowIsTranslucent :(int)minWidth :(int)minHeight :(int)maxWidth :(int)maxHeight :(bool)fraudulentWebsiteWarningEnabled { +- (void) CreateWindow:(int)width :(int)height :(bool)frameless :(bool)resizable :(bool)fullscreen :(bool)fullSizeContent :(bool)hideTitleBar :(bool)titlebarAppearsTransparent :(bool)hideTitle :(bool)useToolbar :(bool)hideToolbarSeparator :(bool)webviewIsTransparent :(bool)hideWindowOnClose :(NSString*)appearance :(bool)windowIsTranslucent :(int)minWidth :(int)minHeight :(int)maxWidth :(int)maxHeight :(bool)fraudulentWebsiteWarningEnabled :(struct Preferences)preferences { NSWindowStyleMask styleMask = 0; if( !frameless ) { @@ -214,6 +214,16 @@ typedef void (^schemeTaskCaller)(id); config.suppressesIncrementalRendering = true; config.applicationNameForUserAgent = @"wails.io"; [config setURLSchemeHandler:self forURLScheme:@"wails"]; + + if (preferences.tabFocusesLinks != NULL) { + config.preferences.tabFocusesLinks = *preferences.tabFocusesLinks; + } + + if (@available(macOS 11.3, *)) { + if (preferences.textInteractionEnabled != NULL) { + config.preferences.textInteractionEnabled = *preferences.textInteractionEnabled; + } + } // [config.preferences setValue:[NSNumber numberWithBool:true] forKey:@"developerExtrasEnabled"]; diff --git a/v2/internal/frontend/desktop/darwin/window.go b/v2/internal/frontend/desktop/darwin/window.go index 2586b1b48..ae114873f 100644 --- a/v2/internal/frontend/desktop/darwin/window.go +++ b/v2/internal/frontend/desktop/darwin/window.go @@ -40,6 +40,11 @@ func bool2Cint(value bool) C.int { return C.int(0) } +func bool2CboolPtr(value bool) *C.bool { + v := C.bool(value) + return &v +} + func NewWindow(frontendOptions *options.App, debug bool, devtools bool) *Window { c := NewCalloc() @@ -57,6 +62,7 @@ func NewWindow(frontendOptions *options.App, debug bool, devtools bool) *Window var fullSizeContent, hideTitleBar, hideTitle, useToolbar, webviewIsTransparent C.int var titlebarAppearsTransparent, hideToolbarSeparator, windowIsTranslucent C.int var appearance, title *C.char + var preferences C.struct_Preferences width := C.int(frontendOptions.Width) height := C.int(frontendOptions.Height) @@ -80,6 +86,17 @@ func NewWindow(frontendOptions *options.App, debug bool, devtools bool) *Window titlebarAppearsTransparent = bool2Cint(mac.TitleBar.TitlebarAppearsTransparent) hideToolbarSeparator = bool2Cint(mac.TitleBar.HideToolbarSeparator) } + + if mac.Preferences != nil { + if mac.Preferences.TabFocusesLinks.IsSet() { + preferences.tabFocusesLinks = bool2CboolPtr(mac.Preferences.TabFocusesLinks.Get()) + } + + if mac.Preferences.TextInteractionEnabled.IsSet() { + preferences.textInteractionEnabled = bool2CboolPtr(mac.Preferences.TextInteractionEnabled.Get()) + } + } + windowIsTranslucent = bool2Cint(mac.WindowIsTranslucent) webviewIsTransparent = bool2Cint(mac.WebviewIsTransparent) @@ -88,7 +105,7 @@ func NewWindow(frontendOptions *options.App, debug bool, devtools bool) *Window var context *C.WailsContext = C.Create(title, width, height, frameless, resizable, fullscreen, fullSizeContent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, devtoolsEnabled, defaultContextMenu, - windowStartState, startsHidden, minWidth, minHeight, maxWidth, maxHeight, enableFraudulentWebsiteWarnings) + windowStartState, startsHidden, minWidth, minHeight, maxWidth, maxHeight, enableFraudulentWebsiteWarnings, preferences) // Create menu result := &Window{ diff --git a/v2/pkg/options/mac/mac.go b/v2/pkg/options/mac/mac.go index 033cfd1a2..9838b145b 100644 --- a/v2/pkg/options/mac/mac.go +++ b/v2/pkg/options/mac/mac.go @@ -20,6 +20,7 @@ type Options struct { Appearance AppearanceType WebviewIsTransparent bool WindowIsTranslucent bool + Preferences *Preferences //ActivationPolicy ActivationPolicy About *AboutInfo //URLHandlers map[string]func(string) diff --git a/v2/pkg/options/mac/preferences.go b/v2/pkg/options/mac/preferences.go new file mode 100644 index 000000000..2c690e7f7 --- /dev/null +++ b/v2/pkg/options/mac/preferences.go @@ -0,0 +1,16 @@ +package mac + +import "github.com/leaanthony/u" + +var Enabled = u.True +var Disabled = u.False + +// Preferences allows to set webkit preferences +type Preferences struct { + // A Boolean value that indicates whether pressing the tab key changes the focus to links and form controls. + // Set to false by default. + TabFocusesLinks u.Bool + // A Boolean value that indicates whether to allow people to select or otherwise interact with text. + // Set to true by default. + TextInteractionEnabled u.Bool +} diff --git a/website/docs/reference/options.mdx b/website/docs/reference/options.mdx index 1f96f154c..de6b34957 100644 --- a/website/docs/reference/options.mdx +++ b/website/docs/reference/options.mdx @@ -797,6 +797,40 @@ with [WebviewIsTransparent](#WebviewIsTransparent) to make frosty-looking applic Name: WindowIsTranslucent
Type: `bool` +#### Preferences + +The Preferences struct provides the ability to configure the Webview preferences. + +Name: Preferences
+Type: [`*mac.Preferences`](#preferences-struct) + +##### Preferences struct + +You can specify the webview preferences. + +```go +type Preferences struct { + TabFocusesLinks u.Bool + TextInteractionEnabled u.Bool +} +``` + +| Name | Description | +| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| TabFocusesLinks | A Boolean value that indicates whether pressing the tab key changes the focus to links and form controls. [Apple Docs](https://developer.apple.com/documentation/webkit/wkpreferences/2818595-tabfocuseslinks?language=objc) | +| TextInteractionEnabled | A Boolean value that indicates whether to allow people to select or otherwise interact with text. [Apple Docs](https://developer.apple.com/documentation/webkit/wkpreferences/3727362-textinteractionenabled?language=objc) | + +Example: + +```go +Mac: &mac.Options{ + Preferences: &mac.Preferences{ + TabFocusesLinks: mac.Enabled, + TextInteractionEnabled: mac.Disabled, + } +} +``` + #### About This configuration lets you set the title, message and icon for the "About" menu item in the app menu created by the "AppMenu" role. diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index 9f8cf8559..cd7e0e69e 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added support for enabling/disabling swipe gestures for Windows WebView2. Added by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/2878) - When building with `-devtools` flag, CMD/CTRL+SHIFT+F12 can be used to open the devtools. Added by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/2915) +- Added support for setting some of the Webview preferences, `textInteractionEnabled` and `tabFocusesLinks` on Mac. Added by @fkhadra in [PR](https://github.com/wailsapp/wails/pull/2937) ### Changed