diff --git a/mkdocs-website/docs/en/roadmap.md b/mkdocs-website/docs/en/roadmap.md index a48b56efa..1cc10277b 100644 --- a/mkdocs-website/docs/en/roadmap.md +++ b/mkdocs-website/docs/en/roadmap.md @@ -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: diff --git a/mkdocs-website/shared/alpha4-wails3-dev.csv b/mkdocs-website/shared/alpha4-wails3-dev.csv index bcc9522d8..7e7534810 100644 --- a/mkdocs-website/shared/alpha4-wails3-dev.csv +++ b/mkdocs-website/shared/alpha4-wails3-dev.csv @@ -1,2 +1,2 @@ " ",Mac,Windows,Linux -`wails3 dev`," ",:material-minus:," " \ No newline at end of file +`wails3 dev`,:material-minus:,:material-minus:," " \ No newline at end of file diff --git a/v3/internal/assetserver/assetserver.go b/v3/internal/assetserver/assetserver.go index fcaa38a4d..08947e92e 100644 --- a/v3/internal/assetserver/assetserver.go +++ b/v3/internal/assetserver/assetserver.go @@ -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 { diff --git a/v3/internal/assetserver/assetserver_darwin.go b/v3/internal/assetserver/assetserver_darwin.go new file mode 100644 index 000000000..faab164a4 --- /dev/null +++ b/v3/internal/assetserver/assetserver_darwin.go @@ -0,0 +1,8 @@ +package assetserver + +import "net/url" + +var baseURL = url.URL{ + Scheme: "wails", + Host: "localhost", +} diff --git a/v3/internal/assetserver/assetserver_dev.go b/v3/internal/assetserver/assetserver_dev.go new file mode 100644 index 000000000..6b5af30a8 --- /dev/null +++ b/v3/internal/assetserver/assetserver_dev.go @@ -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...) +} diff --git a/v3/internal/assetserver/assetserver_production.go b/v3/internal/assetserver/assetserver_production.go new file mode 100644 index 000000000..6a7731191 --- /dev/null +++ b/v3/internal/assetserver/assetserver_production.go @@ -0,0 +1,5 @@ +//go:build production + +package assetserver + +func (a *AssetServer) LogDetails() {} diff --git a/v3/internal/assetserver/assetserver_windows.go b/v3/internal/assetserver/assetserver_windows.go index 288a213da..22deda4d2 100644 --- a/v3/internal/assetserver/assetserver_windows.go +++ b/v3/internal/assetserver/assetserver_windows.go @@ -1,3 +1,8 @@ package assetserver -var startURL = "http://wails.localhost" +import "net/url" + +var baseURL = url.URL{ + Scheme: "http", + Host: "wails.localhost", +} diff --git a/v3/internal/assetserver/options.go b/v3/internal/assetserver/options.go index 0a3e8530c..f199a9b0d 100644 --- a/v3/internal/assetserver/options.go +++ b/v3/internal/assetserver/options.go @@ -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 diff --git a/v3/internal/runtime/desktop/@wailsio/runtime/package.json b/v3/internal/runtime/desktop/@wailsio/runtime/package.json index 070230841..9d720ec94 100644 --- a/v3/internal/runtime/desktop/@wailsio/runtime/package.json +++ b/v3/internal/runtime/desktop/@wailsio/runtime/package.json @@ -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", diff --git a/v3/internal/runtime/desktop/@wailsio/runtime/src/index.js b/v3/internal/runtime/desktop/@wailsio/runtime/src/index.js index c69a07239..7a983300c 100644 --- a/v3/internal/runtime/desktop/@wailsio/runtime/src/index.js +++ b/v3/internal/runtime/desktop/@wailsio/runtime/src/index.js @@ -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}; diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index 2b02b6ec5..319acd6b8 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -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() } } diff --git a/v3/pkg/application/application_dev.go b/v3/pkg/application/application_dev.go index 50eadc972..8a8c65ecd 100644 --- a/v3/pkg/application/application_dev.go +++ b/v3/pkg/application/application_dev.go @@ -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() { + +} diff --git a/v3/pkg/application/application_production.go b/v3/pkg/application/application_production.go index 0750e527e..75f86b44d 100644 --- a/v3/pkg/application/application_production.go +++ b/v3/pkg/application/application_production.go @@ -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 } diff --git a/v3/pkg/application/menuitem.go b/v3/pkg/application/menuitem.go index b16619b5d..f363fb740 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 ToggleDevTools: - return newToggleDevToolsMenuItem() + case ShowDevTools: + return newShowDevToolsMenuItem() case ResetZoom: return newZoomResetMenuItem() case ZoomIn: diff --git a/v3/pkg/application/menuitem_dev.go b/v3/pkg/application/menuitem_dev.go new file mode 100644 index 000000000..23b0395cd --- /dev/null +++ b/v3/pkg/application/menuitem_dev.go @@ -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() + } + }) +} diff --git a/v3/pkg/application/menuitem_production.go b/v3/pkg/application/menuitem_production.go new file mode 100644 index 000000000..553338e87 --- /dev/null +++ b/v3/pkg/application/menuitem_production.go @@ -0,0 +1,7 @@ +//go:build production && !devtools + +package application + +func newShowDevToolsMenuItem() *MenuItem { + return nil +} diff --git a/v3/pkg/application/menuitem_windows.go b/v3/pkg/application/menuitem_windows.go index 38aa25df3..2a46f8da6 100644 --- a/v3/pkg/application/menuitem_windows.go +++ b/v3/pkg/application/menuitem_windows.go @@ -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 ---------- diff --git a/v3/pkg/application/roles.go b/v3/pkg/application/roles.go index fd8f46ed1..c09609273 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 - 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) diff --git a/v3/pkg/application/roles_dev.go b/v3/pkg/application/roles_dev.go new file mode 100644 index 000000000..6b0b1acc4 --- /dev/null +++ b/v3/pkg/application/roles_dev.go @@ -0,0 +1,7 @@ +//go:build !production || devtools + +package application + +func addDevToolMenuItem(viewMenu *Menu) { + viewMenu.AddRole(ShowDevTools) +} diff --git a/v3/pkg/application/roles_production.go b/v3/pkg/application/roles_production.go new file mode 100644 index 000000000..d1af8dba7 --- /dev/null +++ b/v3/pkg/application/roles_production.go @@ -0,0 +1,5 @@ +//go:build production && !devtools + +package application + +func addDevToolMenuItem(viewMenu *Menu) {} diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index 448332e18..0ff09e1ca 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -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() - }) - } }) } diff --git a/v3/pkg/application/webview_window_darwin_dev.go b/v3/pkg/application/webview_window_darwin_dev.go new file mode 100644 index 000000000..4e76c5a57 --- /dev/null +++ b/v3/pkg/application/webview_window_darwin_dev.go @@ -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 + +#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) +} diff --git a/v3/pkg/application/webview_window_darwin_devtools.go b/v3/pkg/application/webview_window_darwin_devtools.go deleted file mode 100644 index 2a6442156..000000000 --- a/v3/pkg/application/webview_window_darwin_devtools.go +++ /dev/null @@ -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 - -#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) - } -} diff --git a/v3/pkg/application/webview_window_darwin_production.go b/v3/pkg/application/webview_window_darwin_production.go new file mode 100644 index 000000000..e0a5576e5 --- /dev/null +++ b/v3/pkg/application/webview_window_darwin_production.go @@ -0,0 +1,5 @@ +//go:build darwin && production && !devtools + +package application + +func (w *macosWebviewWindow) enableDevTools() {} diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index a546cf84a..9049dd4f7 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -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) diff --git a/v3/pkg/application/webview_window_windows_devtools.go b/v3/pkg/application/webview_window_windows_devtools.go index 590d4115c..54a799bf9 100644 --- a/v3/pkg/application/webview_window_windows_devtools.go +++ b/v3/pkg/application/webview_window_windows_devtools.go @@ -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()) } } diff --git a/v3/pkg/application/webview_window_windows_production.go b/v3/pkg/application/webview_window_windows_production.go new file mode 100644 index 000000000..0dd2fff45 --- /dev/null +++ b/v3/pkg/application/webview_window_windows_production.go @@ -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()) + } +}