From 3f9067c81507a01c75d66aac36d4431628d6aed1 Mon Sep 17 00:00:00 2001 From: Mohamed Gharib Date: Tue, 26 Sep 2023 00:43:18 +0300 Subject: [PATCH] [v2] Devtools tag doesn't enable default context-menu (#2923) * Devtools tag doesn't enable default context-menu * Update changelog --- v2/internal/frontend/desktop/darwin/Application.h | 2 +- v2/internal/frontend/desktop/darwin/Application.m | 4 ++-- v2/internal/frontend/desktop/darwin/WailsContext.h | 2 +- v2/internal/frontend/desktop/darwin/WailsContext.m | 6 ++++-- v2/internal/frontend/desktop/darwin/main.m | 4 ++-- v2/internal/frontend/desktop/darwin/window.go | 4 ++-- v2/internal/frontend/desktop/linux/window.c | 3 +-- v2/internal/frontend/desktop/linux/window.go | 4 +++- v2/internal/frontend/desktop/windows/frontend.go | 2 +- v2/pkg/options/options.go | 2 +- website/docs/reference/options.mdx | 2 +- website/src/pages/changelog.mdx | 4 ++++ 12 files changed, 23 insertions(+), 16 deletions(-) diff --git a/v2/internal/frontend/desktop/darwin/Application.h b/v2/internal/frontend/desktop/darwin/Application.h index 3c044faf3..458016cc8 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, struct Preferences preferences); +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 defaultContextMenuEnabled, 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 5b0fc5776..e0c05212d 100644 --- a/v2/internal/frontend/desktop/darwin/Application.m +++ b/v2/internal/frontend/desktop/darwin/Application.m @@ -14,14 +14,14 @@ #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, struct Preferences preferences) { +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 defaultContextMenuEnabled, int windowStartState, int startsHidden, int minWidth, int minHeight, int maxWidth, int maxHeight, bool fraudulentWebsiteWarningEnabled, struct Preferences preferences) { [NSApplication sharedApplication]; WailsContext *result = [WailsContext new]; result.devtoolsEnabled = devtoolsEnabled; - result.defaultContextMenu = defaultContextMenu; + result.defaultContextMenuEnabled = defaultContextMenuEnabled; if ( windowStartState == WindowStartsFullscreen ) { fullscreen = 1; diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.h b/v2/internal/frontend/desktop/darwin/WailsContext.h index 77e35a783..fe66f3549 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.h +++ b/v2/internal/frontend/desktop/darwin/WailsContext.h @@ -45,7 +45,7 @@ @property bool alwaysOnTop; @property bool devtoolsEnabled; -@property bool defaultContextMenu; +@property bool defaultContextMenuEnabled; @property (retain) WKUserContentController* userContentController; diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.m b/v2/internal/frontend/desktop/darwin/WailsContext.m index bd4e40439..379b2fe9c 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.m +++ b/v2/internal/frontend/desktop/darwin/WailsContext.m @@ -235,16 +235,18 @@ typedef void (^schemeTaskCaller)(id); [userContentController addScriptMessageHandler:self name:@"external"]; config.userContentController = userContentController; self.userContentController = userContentController; + if (self.devtoolsEnabled) { [config.preferences setValue:@YES forKey:@"developerExtrasEnabled"]; - } else if (!self.defaultContextMenu) { + } + + if (!self.defaultContextMenuEnabled) { // Disable default context menus WKUserScript *initScript = [WKUserScript new]; [initScript initWithSource:@"window.wails.flags.disableDefaultContextMenu = true;" injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:false]; [userContentController addUserScript:initScript]; - } self.webview = [WKWebView alloc]; diff --git a/v2/internal/frontend/desktop/darwin/main.m b/v2/internal/frontend/desktop/darwin/main.m index 3a7a4defc..1dd8bb1f3 100644 --- a/v2/internal/frontend/desktop/darwin/main.m +++ b/v2/internal/frontend/desktop/darwin/main.m @@ -216,10 +216,10 @@ int main(int argc, const char * argv[]) { const char* appearance = "NSAppearanceNameDarkAqua"; int windowIsTranslucent = 1; int devtoolsEnabled = 1; - int defaultContextMenu = 1; + int defaultContextMenuEnabled = 1; int windowStartState = 0; int startsHidden = 0; - WailsContext *result = Create("OI OI!",400,400, frameless, resizable, fullscreen, fullSizeContent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, devtoolsEnabled, defaultContextMenu, windowStartState, + WailsContext *result = Create("OI OI!",400,400, frameless, resizable, fullscreen, fullSizeContent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, devtoolsEnabled, defaultContextMenuEnabled, windowStartState, startsHidden, 400, 400, 600, 600, false); SetBackgroundColour(result, 255, 0, 0, 255); void *m = NewMenu(""); diff --git a/v2/internal/frontend/desktop/darwin/window.go b/v2/internal/frontend/desktop/darwin/window.go index ae114873f..803d04c1e 100644 --- a/v2/internal/frontend/desktop/darwin/window.go +++ b/v2/internal/frontend/desktop/darwin/window.go @@ -57,7 +57,7 @@ func NewWindow(frontendOptions *options.App, debug bool, devtools bool) *Window hideWindowOnClose := bool2Cint(frontendOptions.HideWindowOnClose) startsHidden := bool2Cint(frontendOptions.StartHidden) devtoolsEnabled := bool2Cint(devtools) - defaultContextMenu := bool2Cint(frontendOptions.EnableDefaultContextMenu) + defaultContextMenuEnabled := bool2Cint(debug || frontendOptions.EnableDefaultContextMenu) var fullSizeContent, hideTitleBar, hideTitle, useToolbar, webviewIsTransparent C.int var titlebarAppearsTransparent, hideToolbarSeparator, windowIsTranslucent C.int @@ -104,7 +104,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, + alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, devtoolsEnabled, defaultContextMenuEnabled, windowStartState, startsHidden, minWidth, minHeight, maxWidth, maxHeight, enableFraudulentWebsiteWarnings, preferences) // Create menu diff --git a/v2/internal/frontend/desktop/linux/window.c b/v2/internal/frontend/desktop/linux/window.c index 19a07339f..7cd1c249b 100644 --- a/v2/internal/frontend/desktop/linux/window.c +++ b/v2/internal/frontend/desktop/linux/window.c @@ -474,8 +474,7 @@ void DevtoolsEnabled(void *webview, int enabled, bool showInspector) if (genabled && showInspector) { - WebKitWebInspector *inspector = webkit_web_view_get_inspector(WEBKIT_WEB_VIEW(webview)); - webkit_web_inspector_show(WEBKIT_WEB_INSPECTOR(inspector)); + ShowInspector(webview); } } diff --git a/v2/internal/frontend/desktop/linux/window.go b/v2/internal/frontend/desktop/linux/window.go index b10ac1ebd..82030f439 100644 --- a/v2/internal/frontend/desktop/linux/window.go +++ b/v2/internal/frontend/desktop/linux/window.go @@ -107,7 +107,9 @@ func NewWindow(appoptions *options.App, debug bool, devtoolsEnabled bool) *Windo C.DevtoolsEnabled(unsafe.Pointer(webview), C.int(1), C.bool(debug && appoptions.Debug.OpenInspectorOnStartup)) // Install Ctrl-Shift-F12 hotkey to call ShowInspector C.InstallF12Hotkey(unsafe.Pointer(gtkWindow)) - } else if !appoptions.EnableDefaultContextMenu { + } + + if !(debug || appoptions.EnableDefaultContextMenu) { C.DisableContextMenu(unsafe.Pointer(webview)) } diff --git a/v2/internal/frontend/desktop/windows/frontend.go b/v2/internal/frontend/desktop/windows/frontend.go index e51be56cb..a753bbd30 100644 --- a/v2/internal/frontend/desktop/windows/frontend.go +++ b/v2/internal/frontend/desktop/windows/frontend.go @@ -517,7 +517,7 @@ func (f *Frontend) setupChromium() { if err != nil { log.Fatal(err) } - err = settings.PutAreDefaultContextMenusEnabled(f.devtoolsEnabled || f.frontendOptions.EnableDefaultContextMenu) + err = settings.PutAreDefaultContextMenusEnabled(f.debug || f.frontendOptions.EnableDefaultContextMenu) if err != nil { log.Fatal(err) } diff --git a/v2/pkg/options/options.go b/v2/pkg/options/options.go index 2fbcbc8b8..b64befe12 100644 --- a/v2/pkg/options/options.go +++ b/v2/pkg/options/options.go @@ -74,7 +74,7 @@ type App struct { CSSDragValue string // EnableDefaultContextMenu enables the browser's default context-menu in production - // This menu is already enabled in development, as well as in debug builds and production builds with the `-devtools` flag + // This menu is already enabled in development and debug builds EnableDefaultContextMenu bool // EnableFraudulentWebsiteDetection enables scan services for fraudulent content, such as malware or phishing attempts. diff --git a/website/docs/reference/options.mdx b/website/docs/reference/options.mdx index de6b34957..67e897077 100644 --- a/website/docs/reference/options.mdx +++ b/website/docs/reference/options.mdx @@ -424,7 +424,7 @@ Type: `string` EnableDefaultContextMenu enables the browser's default context-menu in production. -By default, the browser's default context-menu is only available in development and in a `-debug` or `-devtools` [build](../reference/cli.mdx#build) along with the devtools inspector, Using this option you can enable the default context-menu in `production` while the devtools inspector won't be available unless the `-devtools` build flag is used. +By default, the browser's default context-menu is only available in development and in a `-debug` [build](../reference/cli.mdx#build) along with the devtools inspector, Using this option you can enable the default context-menu in `production` while the devtools inspector won't be available unless the `-devtools` build flag is used. When this option is enabled, by default the context-menu will only be shown for text contexts (where Cut/Copy/Paste is needed), to override this behavior, you can use the CSS property `--default-contextmenu` on any HTML element (including the `body`) with the following values : diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index 521899726..99c2f8ffb 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -28,6 +28,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed typo on docs/reference/options page. Added by [@pylotlight](https://github.com/pylotlight) in [PR](https://github.com/wailsapp/wails/pull/2887) +### Changed + +- Now building with `-devtools` flag doesn't enable the default context-menu. Changed by @mmghv in [PR](https://github.com/wailsapp/wails/pull/2923) + ## v2.6.0 - 2023-09-06