5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 00:10:47 +08:00

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:08 +11:00
parent c82facd6ff
commit 6522657893
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
27 changed files with 326 additions and 165 deletions

View File

@ -68,9 +68,13 @@ Basically, try to break it and let us know if you find any issues! :smile:
{{ read_csv("alpha4-wails3-dev.csv") }}
- Windows does work, with the following caveats
- Sometimes the main app is built twice on change
- Closing the main window does not terminate the dev process, requiring ctrl-c to terminate
- Windows is partially working:
- Frontend changes work as expected
- Go changes cause the application to be built twice
- Mac is partially working:
- Frontend changes work as expectedFrontend changes work as expected
- Go changes terminates the vite process
`wails3 package` command:

View File

@ -1,2 +1,2 @@
" ",Mac,Windows,Linux
`wails3 dev`," ",:material-minus:," "
`wails3 dev`,:material-minus:,:material-minus:," "
1 Mac Windows Linux
2 `wails3 dev` :material-minus: :material-minus:

View File

@ -3,6 +3,7 @@ package assetserver
import (
"fmt"
"html"
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
@ -144,20 +145,6 @@ func (a *AssetServer) serveError(rw http.ResponseWriter, err error, msg string,
rw.WriteHeader(http.StatusInternalServerError)
}
func (a *AssetServer) LogDetails() {
if a.options.IsDebug {
var info = []any{
"assetsFS", a.options.Assets != nil,
"middleware", a.options.Middleware != nil,
"handler", a.options.Handler != nil,
}
if a.devServerURL != "" {
info = append(info, "devServerURL", a.devServerURL)
}
a.options.Logger.Info("AssetServer Info:", info...)
}
}
func (a *AssetServer) AddPluginScript(pluginName string, script string) {
if a.pluginScripts == nil {
a.pluginScripts = make(map[string]string)
@ -170,6 +157,7 @@ func (a *AssetServer) AddPluginScript(pluginName string, script string) {
func GetStartURL(userURL string) (string, error) {
devServerURL := GetDevServerURL()
startURL := baseURL.String()
if devServerURL != "" {
// Parse the port
parsedURL, err := url.Parse(devServerURL)
@ -178,7 +166,8 @@ func GetStartURL(userURL string) (string, error) {
}
port := parsedURL.Port()
if port != "" {
startURL += ":" + port
baseURL.Host = net.JoinHostPort(baseURL.Host, port)
startURL = baseURL.String()
}
} else {
if userURL != "" {
@ -188,9 +177,10 @@ func GetStartURL(userURL string) (string, error) {
return "", fmt.Errorf("Error parsing URL: " + err.Error())
}
if parsedURL.Scheme == "" {
startURL = path.Join(startURL, userURL)
baseURL.Path = path.Join(baseURL.Path, userURL)
startURL = baseURL.String()
// if the original URL had a trailing slash, add it back
if strings.HasSuffix(userURL, "/") {
if strings.HasSuffix(userURL, "/") && !strings.HasSuffix(startURL, "/") {
startURL = startURL + "/"
}
} else {

View File

@ -0,0 +1,8 @@
package assetserver
import "net/url"
var baseURL = url.URL{
Scheme: "wails",
Host: "localhost",
}

View File

@ -0,0 +1,15 @@
//go:build !production
package assetserver
func (a *AssetServer) LogDetails() {
var info = []any{
"assetsFS", a.options.Assets != nil,
"middleware", a.options.Middleware != nil,
"handler", a.options.Handler != nil,
}
if a.devServerURL != "" {
info = append(info, "devServerURL", a.devServerURL)
}
a.options.Logger.Info("AssetServer Info:", info...)
}

View File

@ -0,0 +1,5 @@
//go:build production
package assetserver
func (a *AssetServer) LogDetails() {}

View File

@ -1,3 +1,8 @@
package assetserver
var startURL = "http://wails.localhost"
import "net/url"
var baseURL = url.URL{
Scheme: "http",
Host: "wails.localhost",
}

View File

@ -46,9 +46,6 @@ type Options struct {
// GetFlags returns the application flags
GetFlags func() []byte
// IsDebug is true if the server is running in debug mode
IsDebug bool
}
// Validate the options

View File

@ -1,6 +1,6 @@
{
"name": "@wailsio/runtime",
"version": "3.0.0-alpha.16",
"version": "3.0.0-alpha.17",
"description": "Wails Runtime",
"main": "src/index.js",
"types": "types/index.d.ts",

View File

@ -23,6 +23,7 @@ import * as WML from './wml';
import * as Events from "./events";
import * as Dialogs from "./dialogs";
import * as Call from "./calls";
import {setupEventCallbacks} from "./events";
export { Application, Browser, Call, Clipboard, Dialogs, Events, Flags, Screens, System, Window, WML};

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()
}
}

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() {
}

View File

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

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:

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()
}
})
}

View File

@ -0,0 +1,7 @@
//go:build production && !devtools
package application
func newShowDevToolsMenuItem() *MenuItem {
return nil
}

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

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)

View File

@ -0,0 +1,7 @@
//go:build !production || devtools
package application
func addDevToolMenuItem(viewMenu *Menu) {
viewMenu.AddRole(ShowDevTools)
}

View File

@ -0,0 +1,5 @@
//go:build production && !devtools
package application
func addDevToolMenuItem(viewMenu *Menu) {}

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()
})
}
})
}

View File

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

View File

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

View File

@ -0,0 +1,5 @@
//go:build darwin && production && !devtools
package application
func (w *macosWebviewWindow) enableDevTools() {}

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)

View File

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

View File

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