Refactor menu functions and streamline devtools toggle across systems

This update adds implementation to several menu item functions, replacing their previous 'not implemented' state. This includes actions for close, reload, forcing reload, toggling of fullscreen, reset zoom, and others. The update also includes modifications for the handling of developer tools toggle under different build configurations. This refactoring is aimed to standardize devtools configuration across different operating systems.
This commit is contained in:
Lea Anthony
2024-01-21 20:52:32 +11:00
parent c82facd6ff
commit 6522657893
27 changed files with 326 additions and 165 deletions
+3 -7
View File
@@ -80,7 +80,6 @@ func New(appOptions Options) *App {
Handler: appOptions.Assets.Handler,
Middleware: assetserver.Middleware(appOptions.Assets.Middleware),
Logger: result.Logger,
IsDebug: result.isDebugMode,
RuntimeHandler: NewMessageProcessor(result.Logger),
GetCapabilities: func() []byte {
return globalApplication.capabilities.AsBytes()
@@ -102,7 +101,6 @@ func New(appOptions Options) *App {
srv, err := assetserver.NewAssetServer(opts)
if err != nil {
result.Logger.Error("Fatal error in application initialisation: " + err.Error())
os.Exit(1)
}
result.assets = srv
@@ -111,20 +109,17 @@ func New(appOptions Options) *App {
result.bindings, err = NewBindings(appOptions.Bind, appOptions.BindAliases)
if err != nil {
globalApplication.fatal("Fatal error in application initialisation: " + err.Error())
os.Exit(1)
}
result.plugins = NewPluginManager(appOptions.Plugins, srv)
err = result.plugins.Init()
if err != nil {
result.Quit()
os.Exit(1)
globalApplication.fatal("Fatal error in plugins initialisation: " + err.Error())
}
err = result.bindings.AddPlugins(appOptions.Plugins)
if err != nil {
globalApplication.fatal("Fatal error in application initialisation: " + err.Error())
os.Exit(1)
}
// Process keybindings
@@ -407,7 +402,7 @@ func (a *App) debug(message string, args ...any) {
func (a *App) fatal(message string, args ...any) {
msg := "A FATAL ERROR HAS OCCURRED: " + message
if a.Logger != nil {
go a.Logger.Error(msg, args...)
a.Logger.Error(msg, args...)
} else {
println(msg)
}
@@ -659,6 +654,7 @@ func (a *App) cleanup() {
func (a *App) Quit() {
if a.impl != nil {
InvokeSync(a.impl.destroy)
a.postQuit()
}
}
+15 -3
View File
@@ -3,16 +3,18 @@
package application
import (
"errors"
"github.com/wailsapp/wails/v3/internal/assetserver"
"net/http"
"time"
)
var devMode = false
func (a *App) preRun() error {
// Check for frontend server url
frontendURL := assetserver.GetDevServerURL()
if frontendURL != "" {
devMode = true
// We want to check if the frontend server is running by trying to http get the url
// and if it is not, we wait 500ms and try again for a maximum of 10 times. If it is
// still not available, we return an error.
@@ -31,8 +33,18 @@ func (a *App) preRun() error {
a.Logger.Info("Retrying...")
}
}
a.Logger.Info("failed!")
return errors.New("unable to connect to frontend server. Please check it is running")
a.fatal("unable to connect to frontend server. Please check it is running", "FRONTEND_DEVSERVER_URL", frontendURL)
}
return nil
}
func (a *App) postQuit() {
if devMode {
a.Logger.Info("The application has terminated, but the watcher is still running.")
a.Logger.Info("To terminate the watcher, press CTRL+C")
}
}
func (a *App) enableDevTools() {
}
@@ -14,3 +14,5 @@ func newApplication(options Options) *App {
func (a *App) logStartup() {}
func (a *App) preRun() error { return nil }
func (a *App) postQuit() error { return nil }
+2 -2
View File
@@ -167,8 +167,8 @@ func newRole(role Role) *MenuItem {
return newForceReloadMenuItem()
case ToggleFullscreen:
return newToggleFullscreenMenuItem()
case ToggleDevTools:
return newToggleDevToolsMenuItem()
case ShowDevTools:
return newShowDevToolsMenuItem()
case ResetZoom:
return newZoomResetMenuItem()
case ZoomIn:
+14
View File
@@ -0,0 +1,14 @@
//go:build !production || devtools
package application
func newShowDevToolsMenuItem() *MenuItem {
return newMenuItem("Show Developer Tools").
SetAccelerator("Alt+Command+I").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.ToggleDevTools()
}
})
}
@@ -0,0 +1,7 @@
//go:build production && !devtools
package application
func newShowDevToolsMenuItem() *MenuItem {
return nil
}
+91 -28
View File
@@ -4,6 +4,7 @@ package application
import (
"github.com/wailsapp/wails/v3/pkg/w32"
"runtime"
"unsafe"
)
@@ -209,54 +210,116 @@ func newAboutMenuItem() *MenuItem {
}
func newCloseMenuItem() *MenuItem {
panic("implement me")
return newMenuItem("Close").
SetAccelerator("CmdOrCtrl+w").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.Close()
}
})
}
func newReloadMenuItem() *MenuItem {
panic("implement me")
return newMenuItem("Reload").
SetAccelerator("CmdOrCtrl+r").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.Reload()
}
})
}
func newForceReloadMenuItem() *MenuItem {
panic("implement me")
return newMenuItem("Force Reload").
SetAccelerator("CmdOrCtrl+Shift+r").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.ForceReload()
}
})
}
func newToggleFullscreenMenuItem() *MenuItem {
panic("implement me")
}
func newToggleDevToolsMenuItem() *MenuItem {
panic("implement me")
result := newMenuItem("Toggle Full Screen").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.ToggleFullscreen()
}
})
if runtime.GOOS == "darwin" {
result.SetAccelerator("Ctrl+Command+F")
} else {
result.SetAccelerator("F11")
}
return result
}
func newZoomResetMenuItem() *MenuItem {
panic("implement me")
// reset zoom menu item
return newMenuItem("Actual Size").
SetAccelerator("CmdOrCtrl+0").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.ZoomReset()
}
})
}
func newZoomInMenuItem() *MenuItem {
panic("implement me")
return newMenuItem("Zoom In").
SetAccelerator("CmdOrCtrl+plus").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.ZoomIn()
}
})
}
func newZoomOutMenuItem() *MenuItem {
panic("implement me")
}
func newMinimizeMenuItem() *MenuItem {
panic("implement me")
}
func newZoomMenuItem() *MenuItem {
panic("implement me")
return newMenuItem("Zoom Out").
SetAccelerator("CmdOrCtrl+-").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.ZoomOut()
}
})
}
func newFullScreenMenuItem() *MenuItem {
panic("implement me")
return newMenuItem("Fullscreen").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.Fullscreen()
}
})
}
func newMinimizeMenuItem() *MenuItem {
return newMenuItem("Minimize").
SetAccelerator("CmdOrCtrl+M").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.Minimise()
}
})
}
func newZoomMenuItem() *MenuItem {
return newMenuItem("Zoom").
OnClick(func(ctx *Context) {
currentWindow := globalApplication.CurrentWindow()
if currentWindow != nil {
currentWindow.Zoom()
}
})
}
// ---------- unsupported on windows ----------
+2 -2
View File
@@ -36,7 +36,7 @@ const (
Close Role = iota
Reload Role = iota
ForceReload Role = iota
ToggleDevTools Role = iota
ShowDevTools Role = iota
ResetZoom Role = iota
ZoomIn Role = iota
ZoomOut Role = iota
@@ -76,7 +76,7 @@ func newViewMenu() *MenuItem {
viewMenu := NewMenu()
viewMenu.AddRole(Reload)
viewMenu.AddRole(ForceReload)
viewMenu.AddRole(ToggleDevTools)
addDevToolMenuItem(viewMenu)
viewMenu.AddSeparator()
viewMenu.AddRole(ResetZoom)
viewMenu.AddRole(ZoomIn)
+7
View File
@@ -0,0 +1,7 @@
//go:build !production || devtools
package application
func addDevToolMenuItem(viewMenu *Menu) {
viewMenu.AddRole(ShowDevTools)
}
+5
View File
@@ -0,0 +1,5 @@
//go:build production && !devtools
package application
func addDevToolMenuItem(viewMenu *Menu) {}
+28 -42
View File
@@ -256,13 +256,6 @@ void windowSetMaxSize(void* nsWindow, int width, int height) {
[window setMaxSize:size];
}
// Enable NSWindow devtools
void windowEnableDevTools(void* nsWindow) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
// Enable devtools in webview
[window.webView.configuration.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
}
// windowZoomReset
void windowZoomReset(void* nsWindow) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
@@ -734,7 +727,7 @@ void windowFocus(void *window) {
*/
import "C"
import (
"net/url"
"github.com/wailsapp/wails/v3/internal/assetserver"
"sync"
"unsafe"
@@ -969,15 +962,6 @@ func (w *macosWebviewWindow) execJS(js string) {
}
func (w *macosWebviewWindow) setURL(uri string) {
if uri != "" {
parsedURL, err := url.Parse(uri)
if err == nil && parsedURL.Scheme == "" && parsedURL.Host == "" {
// TODO handle this in a central location, the scheme and host might be platform dependant.
parsedURL.Scheme = "wails"
parsedURL.Host = "wails"
uri = parsedURL.String()
}
}
C.navigationLoadURL(w.nsWindow, C.CString(uri))
}
@@ -1017,9 +1001,6 @@ func (w *macosWebviewWindow) setMaxSize(width, height int) {
func (w *macosWebviewWindow) setResizable(resizable bool) {
C.windowSetResizable(w.nsWindow, C.bool(resizable))
}
func (w *macosWebviewWindow) enableDevTools() {
C.windowEnableDevTools(w.nsWindow)
}
func (w *macosWebviewWindow) size() (int, int) {
var width, height C.int
@@ -1109,9 +1090,8 @@ func (w *macosWebviewWindow) run() {
w.setMaxSize(options.MaxWidth, options.MaxHeight)
}
//w.setZoom(options.Zoom)
if globalApplication.isDebugMode || options.DevToolsEnabled {
w.enableDevTools()
}
w.enableDevTools()
w.setBackgroundColour(options.BackgroundColour)
switch macOptions.Backdrop {
@@ -1157,17 +1137,34 @@ func (w *macosWebviewWindow) run() {
}
C.windowCenter(w.nsWindow)
if options.URL != "" {
w.setURL(options.URL)
startURL, err := assetserver.GetStartURL(options.URL)
if err != nil {
globalApplication.fatal(err.Error())
}
w.setURL(startURL)
// We need to wait for the HTML to load before we can execute the javascript
w.parent.On(events.Mac.WebViewDidFinishNavigation, func(_ *WindowEvent) {
if options.JS != "" {
w.execJS(options.JS)
}
if options.CSS != "" {
C.windowInjectCSS(w.nsWindow, C.CString(options.CSS))
}
InvokeAsync(func() {
if options.JS != "" {
w.execJS(options.JS)
}
if options.CSS != "" {
C.windowInjectCSS(w.nsWindow, C.CString(options.CSS))
}
if options.Hidden == false {
C.windowShow(w.nsWindow)
w.setHasShadow(!options.Mac.DisableShadow)
} else {
// We have to wait until the window is shown before we can remove the shadow
var cancel func()
cancel = w.parent.On(events.Mac.WindowDidBecomeKey, func(_ *WindowEvent) {
w.setHasShadow(!options.Mac.DisableShadow)
cancel()
})
}
})
})
// Translate ShouldClose to common WindowClosing event
@@ -1186,17 +1183,6 @@ func (w *macosWebviewWindow) run() {
if options.HTML != "" {
w.setHTML(options.HTML)
}
if options.Hidden == false {
C.windowShow(w.nsWindow)
w.setHasShadow(!options.Mac.DisableShadow)
} else {
// We have to wait until the window is shown before we can remove the shadow
var cancel func()
cancel = w.parent.On(events.Mac.WindowDidBecomeKey, func(_ *WindowEvent) {
w.setHasShadow(!options.Mac.DisableShadow)
cancel()
})
}
})
}
@@ -0,0 +1,68 @@
//go:build darwin && (!production || devtools)
package application
/*
#cgo CFLAGS: -mmacosx-version-min=10.13 -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
#include "webview_window_darwin.h"
@interface _WKInspector : NSObject
- (void)show;
- (void)detach;
@end
@interface WKWebView ()
- (_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) {
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 120000
dispatch_async(dispatch_get_main_queue(), ^{
if (@available(macOS 12.0, *)) {
WebviewWindow* nsWindow = (WebviewWindow*)window;
@try {
[nsWindow.webView._inspector show];
} @catch (NSException *exception) {
NSLog(@"Opening the inspector failed: %@", exception.reason);
return;
}
} else {
NSLog(@"Opening the inspector needs at least MacOS 12");
}
});
#endif
}
// Enable NSWindow devtools
void windowEnableDevTools(void* nsWindow) {
WebviewWindow* window = (WebviewWindow*)nsWindow;
// Enable devtools in webview
[window.webView.configuration.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
}
*/
import "C"
import "unsafe"
func init() {
showDevTools = func(window unsafe.Pointer) {
C.showDevTools(window)
}
}
func (w *macosWebviewWindow) enableDevTools() {
C.windowEnableDevTools(w.nsWindow)
}
@@ -1,38 +0,0 @@
//go:build darwin && !production
package application
/*
#cgo CFLAGS: -mmacosx-version-min=10.13 -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
#include "webview_window_darwin.h"
@interface _WKInspector : NSObject
- (void)show;
- (void)detach;
@end
@interface WKWebView ()
- (_WKInspector *)_inspector;
@end
void showDevTools(void *window) {
// get main window
WebviewWindow* nsWindow = (WebviewWindow*)window;
dispatch_async(dispatch_get_main_queue(), ^{
[nsWindow.webView._inspector show];
});
}
*/
import "C"
import "unsafe"
func init() {
showDevTools = func(window unsafe.Pointer) {
C.showDevTools(window)
}
}
@@ -0,0 +1,5 @@
//go:build darwin && production && !devtools
package application
func (w *macosWebviewWindow) enableDevTools() {}
+2 -10
View File
@@ -38,8 +38,6 @@ var edgeMap = map[string]uintptr{
"nw-resize": w32.HTTOPLEFT,
}
var showDevTools = func(chromium *edge.Chromium) {}
type windowsWebviewWindow struct {
windowImpl unsafe.Pointer
parent *WebviewWindow
@@ -407,10 +405,6 @@ func (w *windowsWebviewWindow) forceReload() {
panic("implement me")
}
func (w *windowsWebviewWindow) toggleDevTools() {
showDevTools(w.chromium)
}
func (w *windowsWebviewWindow) zoomReset() {
w.setZoom(1.0)
}
@@ -1409,10 +1403,8 @@ func (w *windowsWebviewWindow) setupChromium() {
if err != nil {
globalApplication.fatal(err.Error())
}
err = settings.PutAreDevToolsEnabled(debugMode || w.parent.options.DevToolsEnabled)
if err != nil {
globalApplication.fatal(err.Error())
}
w.enableDevTools(settings)
if w.parent.options.Zoom > 0.0 {
chromium.PutZoomFactor(w.parent.options.Zoom)
@@ -1,13 +1,16 @@
//go:build windows && !production
//go:build windows && (!production || devtools)
package application
import (
"github.com/wailsapp/go-webview2/pkg/edge"
)
import "github.com/wailsapp/go-webview2/pkg/edge"
func init() {
showDevTools = func(chromium *edge.Chromium) {
chromium.OpenDevToolsWindow()
func (w *windowsWebviewWindow) toggleDevTools() {
w.chromium.OpenDevToolsWindow()
}
func (w *windowsWebviewWindow) enableDevTools(settings *edge.ICoreWebViewSettings) {
err := settings.PutAreDevToolsEnabled(true)
if err != nil {
globalApplication.fatal(err.Error())
}
}
@@ -0,0 +1,14 @@
//go:build windows && production && !devtools
package application
import "github.com/wailsapp/go-webview2/pkg/edge"
func (w *windowsWebviewWindow) toggleDevTools() {}
func (w *windowsWebviewWindow) enableDevTools(settings *edge.ICoreWebViewSettings) {
err := settings.PutAreDevToolsEnabled(false)
if err != nil {
globalApplication.fatal(err.Error())
}
}