Skip to content

Commit 6c03d80

Browse files
lostcauseclaude
andcommitted
Decouple context menu from debug flag on all backends
Every backend (GTK, EdgeChromium, Cocoa) tied the entire native right-click context menu to webview.start()'s debug flag, not just the devtools "Inspect Element" entry. Apps running with debug=False (the normal production case) lost basic copy/paste and Back/Forward/Reload along with it -- found live driving a real WebKitGTK window under Xvfb: right-clicking a loaded page produced nothing with debug=False, no matter what. Adds settings['ENABLE_CONTEXT_MENU'] (default None, preserving the old debug-tied behavior) so an app can force the context menu on/off independently of debug. Devtools exposure itself (enable_developer_extras / AreDevToolsEnabled / developerExtrasEnabled) stays tied to debug on all three backends, unchanged -- this only separates "show a normal context menu" from "expose devtools", which were never actually the same decision for an end user. Verified live: with ENABLE_CONTEXT_MENU=True and debug=False, right- click on GTK now shows the normal Back/Forward/Stop/Reload menu with no Inspect Element entry; the same window with the setting left at its default shows no menu at all, reproducing the original bug. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent f12c7e6 commit 6c03d80

4 files changed

Lines changed: 22 additions & 3 deletions

File tree

webview/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ class FileDialog(enum.IntEnum):
139139
'IGNORE_SSL_ERRORS': False,
140140
'SHOW_DEFAULT_MENUS': True,
141141
'WEBVIEW2_RUNTIME_PATH': None,
142+
# Every backend used to tie the native right-click context menu
143+
# (copy/paste, etc.) to the `debug` flag on webview.start(), so
144+
# production apps running with debug=False lost basic copy/paste
145+
# entirely, not just the "Inspect Element" devtools entry -- the
146+
# two are unrelated to an end user. None preserves that original
147+
# behavior (follow `debug`) for backward compat; True/False force
148+
# the context menu on/off regardless of `debug`, letting an app
149+
# ship a normal right-click menu without exposing devtools.
150+
'ENABLE_CONTEXT_MENU': None,
142151
}
143152
)
144153

webview/platforms/cocoa.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,10 @@ def mouseDragged_(self, event):
506506
super(BrowserView.WebKitHost, self).mouseDragged_(event)
507507

508508
def willOpenMenu_withEvent_(self, menu, event):
509-
if not _state['debug']:
509+
enable_context_menu = webview_settings['ENABLE_CONTEXT_MENU']
510+
if enable_context_menu is None:
511+
enable_context_menu = _state['debug']
512+
if not enable_context_menu:
510513
menu.removeAllItems()
511514

512515
def keyDown_(self, event):

webview/platforms/edgechromium.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,10 @@ def on_webview_ready(self, sender, args):
285285

286286
settings = sender.CoreWebView2.Settings
287287
settings.AreBrowserAcceleratorKeysEnabled = _state['debug']
288-
settings.AreDefaultContextMenusEnabled = _state['debug']
288+
enable_context_menu = webview_settings['ENABLE_CONTEXT_MENU']
289+
if enable_context_menu is None:
290+
enable_context_menu = _state['debug']
291+
settings.AreDefaultContextMenusEnabled = enable_context_menu
289292
settings.AreDefaultScriptDialogsEnabled = True
290293
settings.AreDevToolsEnabled = _state['debug']
291294
settings.IsBuiltInErrorPageEnabled = True

webview/platforms/gtk.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,11 @@ def __init__(self, window: Window) -> None:
254254

255255
if settings['OPEN_DEVTOOLS_IN_DEBUG']:
256256
self.webview.get_inspector().show()
257-
else:
257+
258+
enable_context_menu = settings['ENABLE_CONTEXT_MENU']
259+
if enable_context_menu is None:
260+
enable_context_menu = _state['debug']
261+
if not enable_context_menu:
258262
self.webview.connect('context-menu', lambda a, b, c, d: True) # Disable context menu
259263

260264
if _state['private_mode']:

0 commit comments

Comments
 (0)