5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 23:51:44 +08:00

[v2 mac] Allow to specify webview preferences (#2937)

* [mac] allow to specify webview preferences
This commit is contained in:
Fadi Khadra 2023-09-25 00:15:55 +02:00 committed by GitHub
parent 787ca80770
commit 05eddc65b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 95 additions and 6 deletions

View File

@ -22,6 +22,7 @@ require (
github.com/leaanthony/go-ansi-parser v1.6.0 github.com/leaanthony/go-ansi-parser v1.6.0
github.com/leaanthony/gosod v1.0.3 github.com/leaanthony/gosod v1.0.3
github.com/leaanthony/slicer v1.6.0 github.com/leaanthony/slicer v1.6.0
github.com/leaanthony/u v1.1.0
github.com/leaanthony/winicon v1.0.0 github.com/leaanthony/winicon v1.0.0
github.com/matryer/is v1.4.0 github.com/matryer/is v1.4.0
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8

View File

@ -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.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 h1:1RFP5uiPJvT93TAHi+ipd3NACobkW53yUiBqZheE/Js=
github.com/leaanthony/slicer v1.6.0/go.mod h1:o/Iz29g7LN0GqH3aMjWAe90381nyZlDNquK+mtH2Fj8= 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 h1:ZNt5U5dY71oEoKZ97UVwJRT4e+5xo5o/ieKuHuk8NqQ=
github.com/leaanthony/winicon v1.0.0/go.mod h1:en5xhijl92aphrJdmRPlh4NI1L6wq3gEm0LpXAPghjU= github.com/leaanthony/winicon v1.0.0/go.mod h1:en5xhijl92aphrJdmRPlh4NI1L6wq3gEm0LpXAPghjU=
github.com/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c= github.com/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c=

View File

@ -17,7 +17,7 @@
#define WindowStartsMinimised 2 #define WindowStartsMinimised 2
#define WindowStartsFullscreen 3 #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 Run(void*, const char* url);
void SetTitle(void* ctx, const char *title); void SetTitle(void* ctx, const char *title);

View File

@ -14,7 +14,7 @@
#import "WailsMenu.h" #import "WailsMenu.h"
#import "WailsMenuItem.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]; [NSApplication sharedApplication];
@ -27,7 +27,7 @@ WailsContext* Create(const char* title, int width, int height, int frameless, in
fullscreen = 1; 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 SetTitle:safeInit(title)];
[result Center]; [result Center];

View File

@ -55,7 +55,12 @@
@property (retain) NSString* aboutTitle; @property (retain) NSString* aboutTitle;
@property (retain) NSString* aboutDescription; @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) SetSize:(int)width :(int)height;
- (void) SetPosition:(int)x :(int) y; - (void) SetPosition:(int)x :(int) y;
- (void) SetMinSize:(int)minWidth :(int)minHeight; - (void) SetMinSize:(int)minWidth :(int)minHeight;

View File

@ -136,7 +136,7 @@ typedef void (^schemeTaskCaller)(id<WKURLSchemeTask>);
return NO; 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; NSWindowStyleMask styleMask = 0;
if( !frameless ) { if( !frameless ) {
@ -214,6 +214,16 @@ typedef void (^schemeTaskCaller)(id<WKURLSchemeTask>);
config.suppressesIncrementalRendering = true; config.suppressesIncrementalRendering = true;
config.applicationNameForUserAgent = @"wails.io"; config.applicationNameForUserAgent = @"wails.io";
[config setURLSchemeHandler:self forURLScheme:@"wails"]; [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"]; // [config.preferences setValue:[NSNumber numberWithBool:true] forKey:@"developerExtrasEnabled"];

View File

@ -40,6 +40,11 @@ func bool2Cint(value bool) C.int {
return C.int(0) 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 { func NewWindow(frontendOptions *options.App, debug bool, devtools bool) *Window {
c := NewCalloc() 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 fullSizeContent, hideTitleBar, hideTitle, useToolbar, webviewIsTransparent C.int
var titlebarAppearsTransparent, hideToolbarSeparator, windowIsTranslucent C.int var titlebarAppearsTransparent, hideToolbarSeparator, windowIsTranslucent C.int
var appearance, title *C.char var appearance, title *C.char
var preferences C.struct_Preferences
width := C.int(frontendOptions.Width) width := C.int(frontendOptions.Width)
height := C.int(frontendOptions.Height) height := C.int(frontendOptions.Height)
@ -80,6 +86,17 @@ func NewWindow(frontendOptions *options.App, debug bool, devtools bool) *Window
titlebarAppearsTransparent = bool2Cint(mac.TitleBar.TitlebarAppearsTransparent) titlebarAppearsTransparent = bool2Cint(mac.TitleBar.TitlebarAppearsTransparent)
hideToolbarSeparator = bool2Cint(mac.TitleBar.HideToolbarSeparator) 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) windowIsTranslucent = bool2Cint(mac.WindowIsTranslucent)
webviewIsTransparent = bool2Cint(mac.WebviewIsTransparent) 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, var context *C.WailsContext = C.Create(title, width, height, frameless, resizable, fullscreen, fullSizeContent,
hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent,
alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, devtoolsEnabled, defaultContextMenu, alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, devtoolsEnabled, defaultContextMenu,
windowStartState, startsHidden, minWidth, minHeight, maxWidth, maxHeight, enableFraudulentWebsiteWarnings) windowStartState, startsHidden, minWidth, minHeight, maxWidth, maxHeight, enableFraudulentWebsiteWarnings, preferences)
// Create menu // Create menu
result := &Window{ result := &Window{

View File

@ -20,6 +20,7 @@ type Options struct {
Appearance AppearanceType Appearance AppearanceType
WebviewIsTransparent bool WebviewIsTransparent bool
WindowIsTranslucent bool WindowIsTranslucent bool
Preferences *Preferences
//ActivationPolicy ActivationPolicy //ActivationPolicy ActivationPolicy
About *AboutInfo About *AboutInfo
//URLHandlers map[string]func(string) //URLHandlers map[string]func(string)

View File

@ -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
}

View File

@ -797,6 +797,40 @@ with [WebviewIsTransparent](#WebviewIsTransparent) to make frosty-looking applic
Name: WindowIsTranslucent<br/> Name: WindowIsTranslucent<br/>
Type: `bool` Type: `bool`
#### Preferences
The Preferences struct provides the ability to configure the Webview preferences.
Name: Preferences<br/>
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 #### 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. This configuration lets you set the title, message and icon for the "About" menu item in the app menu created by the "AppMenu" role.

View File

@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Added support for enabling/disabling swipe gestures for Windows WebView2. Added by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/2878) - 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) - 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 ### Changed