From a34ccbff334015e42a56cf1644e67746bc4a1faa Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Fri, 9 Feb 2024 20:06:38 +1100 Subject: [PATCH] ToggleDevTools -> OpenDevTools More refactor CGO methods. --- v3/examples/window/main.go | 5 ++ v3/pkg/application/linux_cgo.go | 58 +++++++++++-------- v3/pkg/application/linux_purego.go | 2 +- v3/pkg/application/menuitem.go | 4 +- v3/pkg/application/menuitem_darwin.go | 11 ---- v3/pkg/application/menuitem_dev.go | 6 +- v3/pkg/application/menuitem_linux.go | 11 ---- v3/pkg/application/menuitem_production.go | 2 +- v3/pkg/application/roles.go | 2 +- v3/pkg/application/roles_dev.go | 2 +- v3/pkg/application/webview_window.go | 6 +- v3/pkg/application/webview_window_darwin.go | 6 +- .../application/webview_window_darwin_dev.go | 17 +----- .../webview_window_darwin_production.go | 1 + v3/pkg/application/webview_window_linux.go | 47 +-------------- .../application/webview_window_linux_dev.go | 11 ++++ .../webview_window_linux_devtools.go | 9 --- .../webview_window_linux_production.go | 7 +++ .../webview_window_windows_devtools.go | 2 +- .../webview_window_windows_production.go | 2 +- v3/pkg/application/window.go | 2 +- 21 files changed, 81 insertions(+), 132 deletions(-) create mode 100644 v3/pkg/application/webview_window_linux_dev.go delete mode 100644 v3/pkg/application/webview_window_linux_devtools.go create mode 100644 v3/pkg/application/webview_window_linux_production.go diff --git a/v3/examples/window/main.go b/v3/examples/window/main.go index 4a31eaa0a..bfa6d2252 100644 --- a/v3/examples/window/main.go +++ b/v3/examples/window/main.go @@ -439,6 +439,11 @@ func main() { w.SetEnabled(true) }) }) + stateMenu.Add("Open Dev Tools").OnClick(func(ctx *application.Context) { + currentWindow(func(w *application.WebviewWindow) { + w.OpenDevTools() + }) + }) if runtime.GOOS == "windows" { stateMenu.Add("Flash Start").OnClick(func(ctx *application.Context) { diff --git a/v3/pkg/application/linux_cgo.go b/v3/pkg/application/linux_cgo.go index dc921df27..5c664f395 100644 --- a/v3/pkg/application/linux_cgo.go +++ b/v3/pkg/application/linux_cgo.go @@ -644,13 +644,14 @@ func getScreens(app pointer) ([]*Screen, error) { } // widgets -func widgetSetSensitive(widget pointer, enabled bool) { - value := C.int(0) + +func (w *linuxWebviewWindow) setEnabled(enabled bool) { + var value C.int if enabled { value = C.int(1) } - C.gtk_widget_set_sensitive((*C.GtkWidget)(widget), value) + C.gtk_widget_set_sensitive(w.gtkWidget(), value) } func widgetSetVisible(widget pointer, hidden bool) { @@ -692,32 +693,41 @@ func (w *linuxWebviewWindow) execJS(js string) { C.free(unsafe.Pointer(value)) } -func windowDestroy(window pointer) { - // Should this truly 'destroy' ? - C.gtk_window_close((*C.GtkWindow)(window)) - //C.gtk_widget_destroy((*C.GtkWidget)(window)) +func (w *linuxWebviewWindow) destroy() { + w.parent.markAsDestroyed() + // Free menu + if w.gtkmenu != nil { + C.gtk_widget_destroy((*C.GtkWidget)(w.gtkmenu)) + w.gtkmenu = nil + } + w.destroy() } -func menuDestroy(gtkMenu pointer) { - C.gtk_widget_destroy((*C.GtkWidget)(gtkMenu)) +func (w *linuxWebviewWindow) fullscreen() { + w.maximise() + //w.lastWidth, w.lastHeight = w.size() + x, y, width, height, scale := w.getCurrentMonitorGeometry() + if x == -1 && y == -1 && width == -1 && height == -1 { + return + } + w.setMinMaxSize(0, 0, width*scale, height*scale) + w.setSize(width*scale, height*scale) + C.gtk_window_fullscreen(w.gtkWindow()) + w.setRelativePosition(0, 0) } -func windowFullscreen(window pointer) { - C.gtk_window_fullscreen((*C.GtkWindow)(window)) -} - -func windowGetCurrentMonitor(window pointer) *C.GdkMonitor { +func (w *linuxWebviewWindow) getCurrentMonitor() *C.GdkMonitor { // Get the monitor that the window is currently on - display := C.gtk_widget_get_display((*C.GtkWidget)(window)) - gdk_window := C.gtk_widget_get_window((*C.GtkWidget)(window)) - if gdk_window == nil { + display := C.gtk_widget_get_display(w.gtkWidget()) + gdkWindow := C.gtk_widget_get_window(w.gtkWidget()) + if gdkWindow == nil { return nil } - return C.gdk_display_get_monitor_at_window(display, gdk_window) + return C.gdk_display_get_monitor_at_window(display, gdkWindow) } func (w *linuxWebviewWindow) getCurrentMonitorGeometry() (x int, y int, width int, height int, scale int) { - monitor := windowGetCurrentMonitor(w.window) + monitor := w.getCurrentMonitor() if monitor == nil { return -1, -1, -1, -1, 1 } @@ -727,10 +737,10 @@ func (w *linuxWebviewWindow) getCurrentMonitorGeometry() (x int, y int, width in return int(result.x), int(result.y), int(result.width), int(result.height), scale } -func windowGetSize(window pointer) (int, int) { +func (w *linuxWebviewWindow) size() (int, int) { var windowWidth C.int var windowHeight C.int - C.gtk_window_get_size((*C.GtkWindow)(window), &windowWidth, &windowHeight) + C.gtk_window_get_size(w.gtkWindow(), &windowWidth, &windowHeight) return int(windowWidth), int(windowHeight) } @@ -738,7 +748,7 @@ func (w *linuxWebviewWindow) relativePosition() (int, int) { x, y := w.absolutePosition() // The position must be relative to the screen it is on // We need to get the screen it is on - monitor := windowGetCurrentMonitor(w.window) + monitor := w.getCurrentMonitor() geometry := C.GdkRectangle{} C.gdk_monitor_get_geometry(monitor, &geometry) x = x - int(geometry.x) @@ -1014,7 +1024,7 @@ func (w *linuxWebviewWindow) setupSignalHandlers(emit func(e events.WindowEventT C.signal_connect(wv, c.String("key-press-event"), C.onKeyPressEvent, winID) } -func windowShowDevTools(webview pointer) { +func openDevTools(webview pointer) { inspector := C.webkit_web_view_get_inspector((*C.WebKitWebView)(webview)) C.webkit_web_inspector_show(inspector) } @@ -1029,7 +1039,7 @@ func (w *linuxWebviewWindow) startDrag() error { return nil } -func windowToggleDevTools(webview pointer) { +func enableDevTools(webview pointer) { settings := C.webkit_web_view_get_settings((*C.WebKitWebView)(webview)) enabled := C.webkit_settings_get_enable_developer_extras(settings) switch enabled { diff --git a/v3/pkg/application/linux_purego.go b/v3/pkg/application/linux_purego.go index 682bf195e..5c58ae578 100644 --- a/v3/pkg/application/linux_purego.go +++ b/v3/pkg/application/linux_purego.go @@ -976,7 +976,7 @@ func windowSetupSignalHandlers(windowId uint, window, webview pointer, emit func */ } -func windowToggleDevTools(webview pointer) { +func windowOpenDevTools(webview pointer) { settings := webkitWebViewGetSettings(pointer(webview)) webkitSettingsSetEnableDeveloperExtras( settings, diff --git a/v3/pkg/application/menuitem.go b/v3/pkg/application/menuitem.go index 7e0910a4d..1f05874e8 100644 --- a/v3/pkg/application/menuitem.go +++ b/v3/pkg/application/menuitem.go @@ -167,8 +167,8 @@ func newRole(role Role) *MenuItem { return newForceReloadMenuItem() case ToggleFullscreen: return newToggleFullscreenMenuItem() - case ShowDevTools: - return newShowDevToolsMenuItem() + case OpenDevTools: + return newOpenDevToolsMenuItem() case ResetZoom: return newZoomResetMenuItem() case ZoomIn: diff --git a/v3/pkg/application/menuitem_darwin.go b/v3/pkg/application/menuitem_darwin.go index 714851bb5..3b25b56e2 100644 --- a/v3/pkg/application/menuitem_darwin.go +++ b/v3/pkg/application/menuitem_darwin.go @@ -576,17 +576,6 @@ func newToggleFullscreenMenuItem() *MenuItem { return result } -func newToggleDevToolsMenuItem() *MenuItem { - return newMenuItem("Toggle Developer Tools"). - SetAccelerator("Alt+Command+I"). - OnClick(func(ctx *Context) { - currentWindow := globalApplication.CurrentWindow() - if currentWindow != nil { - currentWindow.ToggleDevTools() - } - }) -} - func newZoomResetMenuItem() *MenuItem { // reset zoom menu item return newMenuItem("Actual Size"). diff --git a/v3/pkg/application/menuitem_dev.go b/v3/pkg/application/menuitem_dev.go index 23b0395cd..7375b117d 100644 --- a/v3/pkg/application/menuitem_dev.go +++ b/v3/pkg/application/menuitem_dev.go @@ -2,13 +2,13 @@ package application -func newShowDevToolsMenuItem() *MenuItem { - return newMenuItem("Show Developer Tools"). +func newOpenDevToolsMenuItem() *MenuItem { + return newMenuItem("Open Developer Tools"). SetAccelerator("Alt+Command+I"). OnClick(func(ctx *Context) { currentWindow := globalApplication.CurrentWindow() if currentWindow != nil { - currentWindow.ToggleDevTools() + currentWindow.OpenDevTools() } }) } diff --git a/v3/pkg/application/menuitem_linux.go b/v3/pkg/application/menuitem_linux.go index 058b58609..abdd8fe05 100644 --- a/v3/pkg/application/menuitem_linux.go +++ b/v3/pkg/application/menuitem_linux.go @@ -294,17 +294,6 @@ func newToggleFullscreenMenuItem() *MenuItem { return result } -func newToggleDevToolsMenuItem() *MenuItem { - return newMenuItem("Toggle Developer Tools"). - SetAccelerator("Alt+Command+I"). - OnClick(func(ctx *Context) { - currentWindow := globalApplication.CurrentWindow() - if currentWindow != nil { - currentWindow.ToggleDevTools() - } - }) -} - func newZoomResetMenuItem() *MenuItem { // reset zoom menu item return newMenuItem("Actual Size"). diff --git a/v3/pkg/application/menuitem_production.go b/v3/pkg/application/menuitem_production.go index 553338e87..387b9d06c 100644 --- a/v3/pkg/application/menuitem_production.go +++ b/v3/pkg/application/menuitem_production.go @@ -2,6 +2,6 @@ package application -func newShowDevToolsMenuItem() *MenuItem { +func newOpenDevToolsMenuItem() *MenuItem { return nil } diff --git a/v3/pkg/application/roles.go b/v3/pkg/application/roles.go index c09609273..bce6b5c2c 100644 --- a/v3/pkg/application/roles.go +++ b/v3/pkg/application/roles.go @@ -36,7 +36,7 @@ const ( Close Role = iota Reload Role = iota ForceReload Role = iota - ShowDevTools Role = iota + OpenDevTools Role = iota ResetZoom Role = iota ZoomIn Role = iota ZoomOut Role = iota diff --git a/v3/pkg/application/roles_dev.go b/v3/pkg/application/roles_dev.go index 6b0b1acc4..1c03e398d 100644 --- a/v3/pkg/application/roles_dev.go +++ b/v3/pkg/application/roles_dev.go @@ -3,5 +3,5 @@ package application func addDevToolMenuItem(viewMenu *Menu) { - viewMenu.AddRole(ShowDevTools) + viewMenu.AddRole(OpenDevTools) } diff --git a/v3/pkg/application/webview_window.go b/v3/pkg/application/webview_window.go index 833ba7a91..b08f39008 100644 --- a/v3/pkg/application/webview_window.go +++ b/v3/pkg/application/webview_window.go @@ -40,7 +40,7 @@ type ( destroy() reload() forceReload() - toggleDevTools() + openDevTools() zoomReset() zoomIn() zoomOut() @@ -817,11 +817,11 @@ func (w *WebviewWindow) ToggleMaximise() { }) } -func (w *WebviewWindow) ToggleDevTools() { +func (w *WebviewWindow) OpenDevTools() { if w.impl == nil && !w.isDestroyed() { return } - InvokeSync(w.impl.toggleDevTools) + InvokeSync(w.impl.openDevTools) } // ZoomReset resets the zoom level of the webview content to 100% diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index 156dba789..b185e189f 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -761,8 +761,6 @@ import ( "github.com/wailsapp/wails/v3/pkg/events" ) -var showDevTools = func(window unsafe.Pointer) {} - type macosWebviewWindow struct { nsWindow unsafe.Pointer parent *WebviewWindow @@ -906,8 +904,8 @@ func (w *macosWebviewWindow) zoomReset() { C.windowZoomReset(w.nsWindow) } -func (w *macosWebviewWindow) toggleDevTools() { - showDevTools(w.nsWindow) +func (w *macosWebviewWindow) openDevTools() { + openDevTools(w.nsWindow) } func (w *macosWebviewWindow) reload() { diff --git a/v3/pkg/application/webview_window_darwin_dev.go b/v3/pkg/application/webview_window_darwin_dev.go index 4e76c5a57..41ddfcd0c 100644 --- a/v3/pkg/application/webview_window_darwin_dev.go +++ b/v3/pkg/application/webview_window_darwin_dev.go @@ -19,15 +19,7 @@ package application - (_WKInspector *)_inspector; @end -//void showDevTools(void *window) { -// // get main window -// WebviewWindow* nsWindow = (WebviewWindow*)window; -// dispatch_async(dispatch_get_main_queue(), ^{ -// [nsWindow.webView._inspector show]; -// }); -//} - -void showDevTools(void *window) { +void openDevTools(void *window) { #if MAC_OS_X_VERSION_MAX_ALLOWED >= 120000 dispatch_async(dispatch_get_main_queue(), ^{ if (@available(macOS 12.0, *)) { @@ -55,12 +47,9 @@ void windowEnableDevTools(void* nsWindow) { */ import "C" -import "unsafe" -func init() { - showDevTools = func(window unsafe.Pointer) { - C.showDevTools(window) - } +func (w *macosWebviewWindow) openDevTools() { + openDevTools(w.nsWindow) } func (w *macosWebviewWindow) enableDevTools() { diff --git a/v3/pkg/application/webview_window_darwin_production.go b/v3/pkg/application/webview_window_darwin_production.go index e0a5576e5..96e2de74c 100644 --- a/v3/pkg/application/webview_window_darwin_production.go +++ b/v3/pkg/application/webview_window_darwin_production.go @@ -3,3 +3,4 @@ package application func (w *macosWebviewWindow) enableDevTools() {} +func (w *macosWebviewWindow) openDevTools() {} diff --git a/v3/pkg/application/webview_window_linux.go b/v3/pkg/application/webview_window_linux.go index 261287d13..1309fa7b8 100644 --- a/v3/pkg/application/webview_window_linux.go +++ b/v3/pkg/application/webview_window_linux.go @@ -12,8 +12,6 @@ import ( "math" ) -var showDevTools = func(window pointer) {} - type dragInfo struct { XRoot int YRoot int @@ -103,19 +101,6 @@ func (w *linuxWebviewWindow) disableSizeConstraints() { w.setMinMaxSize(x, y, width*scale, height*scale) } -func (w *linuxWebviewWindow) fullscreen() { - w.maximise() - //w.lastWidth, w.lastHeight = w.size() - x, y, width, height, scale := w.getCurrentMonitorGeometry() - if x == -1 && y == -1 && width == -1 && height == -1 { - return - } - w.setMinMaxSize(0, 0, width*scale, height*scale) - w.setSize(width*scale, height*scale) - windowFullscreen(w.window) - w.setRelativePosition(0, 0) -} - func (w *linuxWebviewWindow) unminimise() { w.present() } @@ -146,7 +131,7 @@ func (w *linuxWebviewWindow) center() { if x == -1 && y == -1 && width == -1 && height == -1 { return } - windowWidth, windowHeight := windowGetSize(w.window) + windowWidth, windowHeight := w.size() newX := ((width - windowWidth) / 2) + x newY := ((height - windowHeight) / 2) + y @@ -195,18 +180,6 @@ func (w *linuxWebviewWindow) setMaxSize(width, height int) { w.setMinMaxSize(w.parent.options.MinWidth, w.parent.options.MinHeight, width, height) } -func (w *linuxWebviewWindow) showDevTools() { - windowShowDevTools(w.webview) -} - -func (w *linuxWebviewWindow) toggleDevTools() { - showDevTools(w.webview) -} - -func (w *linuxWebviewWindow) size() (int, int) { - return windowGetSize(w.window) -} - func (w *linuxWebviewWindow) setRelativePosition(x, y int) { mx, my, _, _, _ := w.getCurrentMonitorGeometry() w.move(x+mx, y+my) @@ -331,27 +304,13 @@ func (w *linuxWebviewWindow) run() { } } if w.parent.options.DevToolsEnabled || globalApplication.isDebugMode { - w.toggleDevTools() + w.enableDevTools() if w.parent.options.OpenInspectorOnStartup { - w.showDevTools() + w.openDevTools() } } } -func (w *linuxWebviewWindow) destroy() { - w.parent.markAsDestroyed() - // Free menu - if w.gtkmenu != nil { - menuDestroy(w.gtkmenu) - w.gtkmenu = nil - } - windowDestroy(w.window) -} - -func (w *linuxWebviewWindow) setEnabled(enabled bool) { - widgetSetSensitive(w.window, enabled) -} - func (w *linuxWebviewWindow) startResize(border string) error { // FIXME: what do we need to do here? return nil diff --git a/v3/pkg/application/webview_window_linux_dev.go b/v3/pkg/application/webview_window_linux_dev.go new file mode 100644 index 000000000..3f34a6e64 --- /dev/null +++ b/v3/pkg/application/webview_window_linux_dev.go @@ -0,0 +1,11 @@ +//go:build linux && !production + +package application + +func (w *linuxWebviewWindow) openDevTools() { + openDevTools(w.webview) +} + +func (w *linuxWebviewWindow) enableDevTools() { + enableDevTools(w.webview) +} diff --git a/v3/pkg/application/webview_window_linux_devtools.go b/v3/pkg/application/webview_window_linux_devtools.go deleted file mode 100644 index eb883ae24..000000000 --- a/v3/pkg/application/webview_window_linux_devtools.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build linux && !production - -package application - -func init() { - showDevTools = func(wv pointer) { - windowToggleDevTools(wv) - } -} diff --git a/v3/pkg/application/webview_window_linux_production.go b/v3/pkg/application/webview_window_linux_production.go new file mode 100644 index 000000000..c65520b8e --- /dev/null +++ b/v3/pkg/application/webview_window_linux_production.go @@ -0,0 +1,7 @@ +//go:build linux && production && !devtools + +package application + +func (w *linuxWebviewWindow) openDevTools() {} + +func (w *linuxWebviewWindow) enableDevTools() {} diff --git a/v3/pkg/application/webview_window_windows_devtools.go b/v3/pkg/application/webview_window_windows_devtools.go index 54a799bf9..2ee7ea1a3 100644 --- a/v3/pkg/application/webview_window_windows_devtools.go +++ b/v3/pkg/application/webview_window_windows_devtools.go @@ -4,7 +4,7 @@ package application import "github.com/wailsapp/go-webview2/pkg/edge" -func (w *windowsWebviewWindow) toggleDevTools() { +func (w *windowsWebviewWindow) openDevTools() { w.chromium.OpenDevToolsWindow() } diff --git a/v3/pkg/application/webview_window_windows_production.go b/v3/pkg/application/webview_window_windows_production.go index 0dd2fff45..55a9d2ad2 100644 --- a/v3/pkg/application/webview_window_windows_production.go +++ b/v3/pkg/application/webview_window_windows_production.go @@ -4,7 +4,7 @@ package application import "github.com/wailsapp/go-webview2/pkg/edge" -func (w *windowsWebviewWindow) toggleDevTools() {} +func (w *windowsWebviewWindow) openDevTools() {} func (w *windowsWebviewWindow) enableDevTools(settings *edge.ICoreWebViewSettings) { err := settings.PutAreDevToolsEnabled(false) diff --git a/v3/pkg/application/window.go b/v3/pkg/application/window.go index 2398cd368..ce8079444 100644 --- a/v3/pkg/application/window.go +++ b/v3/pkg/application/window.go @@ -65,7 +65,7 @@ type Window interface { SetZoom(magnification float64) Window Show() Window Size() (width int, height int) - ToggleDevTools() + OpenDevTools() ToggleFullscreen() ToggleMaximise() UnFullscreen()