From ff49ca72fef5251db0a5712186fdc5adf7cd04d5 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 12 Oct 2026 22:03:19 +0800 Subject: [PATCH] gui: Windows without the WebView2 runtime says so in a message box and offers the download page, instead of a double-click doing nothing (#1806) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit magpie's windows are WebView2, which Windows 10 doesn't always have. Without it Wails handed the failure to ErrorHandler and exited, and the desktop build (-H windowsgui) has no console for the log line to land in: ufec's double-click came up to nothing at all. ufec found the cause, that Wails' own Env.Info().PlatformInfo["WebView2"] answers "" then, and that a box can't come from ErrorHandler (it runs on the thread Wails makes its windows on); this takes both from their branch. Run asks webView2Check right after application.New, before any window. Missing, it logs it and shows MessageBoxW (Yes/No, error icon, topmost) in the reader's language (trayLang): the runtime is missing, Microsoft's page https://developer.microsoft.com/microsoft-edge/webview2/, that "magpie web" and "magpie tui" work without it, and whether to open the page now; Yes opens it (ShellExecute), and Run ends. A key Wails doesn't set is read as present: refusing to start a magpie that would have worked is worse than the silent exit. The tray isn't kept: its panel is a WebView2 window too. Run: TestWebView2Missing and TestWebView2Notice (all OSes); TestWebView2MissingFromWails on the Windows box (10.0.0.132, Windows with WebView2 installed): a child process with Wails pointed at a folder that isn't a runtime reads missing, the machine's own runtime reads present. TestWebView2Missing goes red with webView2Missing made to answer false. On the box, a throwaway -H windowsgui test binary calling webView2Check with that bogus runtime path, run in the desktop session: the box came up titled "magpie 需要 WebView2" with the Chinese text, the page and 是/否 buttons (read back from the dialog's controls), the log line was written, pressing 是 opened the page in Edge and the check answered false. go test -tags nogui ./internal/gui in a sandbox HOME; gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui and GOOS=windows; windows and linux builds. Not tried: a machine that really lacks the runtime (the box has it; the missing case is Wails' lookup failing on a path that isn't a runtime, the same failure), nor a real magpie.exe start through Run. --- docs/subsystems/gui-shell.md | 3 +- internal/gui/app.go | 6 +++ internal/gui/webview2.go | 61 +++++++++++++++++++++++++ internal/gui/webview2_other.go | 8 ++++ internal/gui/webview2_test.go | 54 ++++++++++++++++++++++ internal/gui/webview2_windows.go | 52 +++++++++++++++++++++ internal/gui/webview2_windows_test.go | 65 +++++++++++++++++++++++++++ 7 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 internal/gui/webview2.go create mode 100644 internal/gui/webview2_other.go create mode 100644 internal/gui/webview2_test.go create mode 100644 internal/gui/webview2_windows.go create mode 100644 internal/gui/webview2_windows_test.go diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 9e99bbd3a..03cb6bdc4 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -21,7 +21,7 @@ three. ## Runtime path -1. `magpie` with no arguments, `magpie app` or `magpie tray` calls `runGUI`, which calls `gui.Run`. A `nogui` build runs the TUI instead. A second launch hands over to the instance already running. +1. `magpie` with no arguments, `magpie app` or `magpie tray` calls `runGUI`, which calls `gui.Run`. A `nogui` build runs the TUI instead. A second launch hands over to the instance already running. On Windows, before any window is made, `webView2Check` asks Wails whether the WebView2 runtime is there (`Env.Info().PlatformInfo["WebView2"]`, "" when its lookup fails; a key Wails doesn't set counts as there). Without it Wails would exit on the first window with nothing on screen, so magpie logs it, shows a Windows message box in the reader's language naming the runtime, Microsoft's download page (`webView2Page`) and `magpie web` / `magpie tui`, opens the page on Yes, and `Run` ends (#1806, `webview2.go`, `TestWebView2MissingFromWails` on Windows). The box can't come from `ErrorHandler`, which runs on the thread Wails makes its windows on. 2. `startBackend` brings up the gateway, or finds another magpie serving it. 3. The panel and the window load `index.html` from `Handler`. `boot.js` passes the saved language, theme, text size and desktop font choices before the first paint. 4. The page reads `GET /api/state` and writes through `POST` routes. Each write answers with the new state, built under `held`. @@ -182,6 +182,7 @@ waits for `show` to accept navigation before creating its draft. ```sh go test -tags nogui ./internal/gui +go test -run TestWebView2 ./internal/gui # on Windows: Wails' runtime lookup, missing and present go test -v ./internal/fonts # native discovery on each desktop OS make test-ui # every internal/gui/tests/*.test.cjs, Chromium and WebKit BROWSER=webkit node --test internal/gui/tests/click-scroll.test.cjs diff --git a/internal/gui/app.go b/internal/gui/app.go index cc393d248..4100a243a 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -361,6 +361,12 @@ func Run(version string, showMain bool, link string) error { // Wails exits on some webview errors; say why before it does. ErrorHandler: func(err error) { log.Println("magpie:", err) }, }) + // Windows without the WebView2 runtime can make no window: Wails would + // exit on the first, with nothing on screen to say why (#1806). The + // reader is told in a box of Windows' own instead, and the launch ends. + if !webView2Check(h.app) { + return nil + } // Wails' default Learn More replaces the current window with wails.io. // Keep the native menus, but open magpie's help in the system browser. // macOS only: it is the one that shows a menu bar unasked; on Linux a diff --git a/internal/gui/webview2.go b/internal/gui/webview2.go new file mode 100644 index 000000000..ff66f6efb --- /dev/null +++ b/internal/gui/webview2.go @@ -0,0 +1,61 @@ +package gui + +import "fmt" + +// magpie's windows are WebView2 on Windows, a runtime Windows 10 doesn't +// always have. Without it Wails hands the failure to ErrorHandler and exits, +// and the desktop build has no console for that to land in: a double-click +// came up to nothing (#1806, ufec). Run asks before any window is made and, +// missing, says so in a box of Windows' own. + +// webView2Page is Microsoft's download page for the runtime. +const webView2Page = "https://developer.microsoft.com/microsoft-edge/webview2/" + +// webView2Missing reads Wails' own answer, Env.Info().PlatformInfo's +// "WebView2": the runtime's version, "" when its lookup failed, the lookup +// a window's WebView2 environment makes. A key Wails doesn't set at all +// says nothing, and is read as there: refusing to start a magpie that would +// have worked is worse than the silent exit. +func webView2Missing(info map[string]any) bool { + v, ok := info["WebView2"] + return ok && fmt.Sprint(v) == "" +} + +// webView2Notice is the box's title and text in lang (trayLang's), English +// where it has none. It names the page and asks whether to open it; Yes +// opens it. +func webView2Notice(lang string) (title, body string) { + t, ok := webView2Words[lang] + if !ok { + t = webView2Words["en"] + } + return t[0], fmt.Sprintf(t[1], webView2Page) +} + +var webView2Words = map[string][2]string{ + "en": {"magpie needs WebView2", + "magpie's windows need Microsoft Edge WebView2 Runtime, which isn't installed on this computer.\r\n\r\n" + + "Install it from:\r\n%s\r\n\r\n" + + "Until then, \"magpie web\" opens magpie in your browser and \"magpie tui\" in a terminal.\r\n\r\n" + + "Open the download page now?"}, + "zh": {"magpie 需要 WebView2", + "magpie 的窗口需要 Microsoft Edge WebView2 运行时,这台电脑上没有安装。\r\n\r\n" + + "请从这里安装:\r\n%s\r\n\r\n" + + "装好之前,可以用 \"magpie web\" 在浏览器里打开 magpie,或用 \"magpie tui\" 在终端里打开。\r\n\r\n" + + "现在打开下载页面吗?"}, + "zh-TW": {"magpie 需要 WebView2", + "magpie 的視窗需要 Microsoft Edge WebView2 執行階段,這台電腦上沒有安裝。\r\n\r\n" + + "請從這裡安裝:\r\n%s\r\n\r\n" + + "裝好之前,可以用 \"magpie web\" 在瀏覽器裡開啟 magpie,或用 \"magpie tui\" 在終端機裡開啟。\r\n\r\n" + + "現在開啟下載頁面嗎?"}, + "ja": {"magpie には WebView2 が必要です", + "magpie のウィンドウには Microsoft Edge WebView2 ランタイムが必要ですが、このコンピューターにはインストールされていません。\r\n\r\n" + + "こちらからインストールしてください:\r\n%s\r\n\r\n" + + "それまでは \"magpie web\" でブラウザーから、\"magpie tui\" でターミナルから magpie を使えます。\r\n\r\n" + + "今すぐダウンロードページを開きますか?"}, + "de": {"magpie braucht WebView2", + "Die Fenster von magpie brauchen die Microsoft Edge WebView2 Runtime, die auf diesem Computer nicht installiert ist.\r\n\r\n" + + "Installieren Sie sie von:\r\n%s\r\n\r\n" + + "Bis dahin öffnet \"magpie web\" magpie im Browser und \"magpie tui\" im Terminal.\r\n\r\n" + + "Die Download-Seite jetzt öffnen?"}, +} diff --git a/internal/gui/webview2_other.go b/internal/gui/webview2_other.go new file mode 100644 index 000000000..6fa846572 --- /dev/null +++ b/internal/gui/webview2_other.go @@ -0,0 +1,8 @@ +//go:build !windows && !nogui + +package gui + +import "github.com/wailsapp/wails/v3/pkg/application" + +// webView2Check: only Windows' windows are WebView2. +func webView2Check(*application.App) bool { return true } diff --git a/internal/gui/webview2_test.go b/internal/gui/webview2_test.go new file mode 100644 index 000000000..75e5726f2 --- /dev/null +++ b/internal/gui/webview2_test.go @@ -0,0 +1,54 @@ +package gui + +import ( + "strings" + "testing" +) + +// #1806 (ufec): Wails' "WebView2" is "" when its lookup fails, the version +// otherwise; a key it doesn't set says nothing and starts the app. +func TestWebView2Missing(t *testing.T) { + for _, c := range []struct { + name string + info map[string]any + want bool + }{ + {"failed lookup", map[string]any{"WebView2": ""}, true}, + {"a version", map[string]any{"WebView2": "141.0.3537.71"}, false}, + {"not reported", map[string]any{}, false}, + {"no info", nil, false}, + } { + if got := webView2Missing(c.info); got != c.want { + t.Errorf("%s: %v, want %v", c.name, got, c.want) + } + } +} + +// The box names the runtime, gives Microsoft's page and asks to open it +// (Yes opens it), in each language the app speaks, English otherwise. +func TestWebView2Notice(t *testing.T) { + for _, lang := range []string{"en", "zh", "zh-TW", "ja", "de", "pt"} { + title, body := webView2Notice(lang) + if !strings.Contains(title, "WebView2") || !strings.Contains(body, "WebView2") { + t.Errorf("%s: doesn't name WebView2: %q / %q", lang, title, body) + } + if !strings.Contains(body, "https://developer.microsoft.com/microsoft-edge/webview2/") { + t.Errorf("%s: no download page in %q", lang, body) + } + if !strings.Contains(body, "magpie web") || !strings.Contains(body, "magpie tui") { + t.Errorf("%s: doesn't say what works without it: %q", lang, body) + } + if strings.Contains(body, "%!") { + t.Errorf("%s: bad format: %q", lang, body) + } + if !strings.HasSuffix(body, "?") && !strings.HasSuffix(body, "?") { + t.Errorf("%s: doesn't end asking whether to open it: %q", lang, body) + } + } + if _, body := webView2Notice("zh"); !strings.Contains(body, "运行时") { + t.Errorf("zh isn't Chinese: %q", body) + } + if title, _ := webView2Notice("pt"); title != "magpie needs WebView2" { + t.Errorf("pt: %q, want English", title) + } +} diff --git a/internal/gui/webview2_windows.go b/internal/gui/webview2_windows.go new file mode 100644 index 000000000..e4dac4bcb --- /dev/null +++ b/internal/gui/webview2_windows.go @@ -0,0 +1,52 @@ +//go:build windows && !nogui + +package gui + +import ( + "log" + "unsafe" + + "github.com/wailsapp/wails/v3/pkg/application" + "github.com/yetone/magpie/internal/settings" + "golang.org/x/sys/windows" +) + +// webView2Check says whether app's windows can be made. Without the +// runtime it logs it and shows the box (webView2Notice), opening the +// download page on Yes, and Run ends: Wails would exit on the first window +// anyway. It is read before app.Run: the lookup reads the registry and +// makes no window. The box can't come from ErrorHandler, which runs on the +// thread Wails makes its windows on (ufec tried it in #1806). +func webView2Check(app *application.App) bool { + if !webView2Missing(app.Env.Info().PlatformInfo) { + return true + } + log.Printf("magpie: no Microsoft Edge WebView2 Runtime, which magpie's windows need; install it from %s, or use magpie web / magpie tui", webView2Page) + title, body := webView2Notice(trayLang(settings.Load().Lang, systemLang)) + if webView2Box(title, body) == idYes { + if err := windows.ShellExecute(0, windows.StringToUTF16Ptr("open"), windows.StringToUTF16Ptr(webView2Page), nil, nil, windows.SW_SHOWNORMAL); err != nil { + log.Println("magpie: opening the WebView2 page:", err) + } + } + return false +} + +const ( + mbYesNo = 0x00000004 + mbIconError = 0x00000010 + mbSetForeground = 0x00010000 + mbTopmost = 0x00040000 + idYes = 6 + webView2BoxStyle = mbYesNo | mbIconError | mbSetForeground | mbTopmost +) + +var procMessageBoxW = windows.NewLazySystemDLL("user32.dll").NewProc("MessageBoxW") + +// webView2Box is Windows' own message box, answering the button pressed. +func webView2Box(title, body string) int { + r, _, _ := procMessageBoxW.Call(0, + uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(body))), + uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(title))), + webView2BoxStyle) + return int(r) +} diff --git a/internal/gui/webview2_windows_test.go b/internal/gui/webview2_windows_test.go new file mode 100644 index 000000000..38403f30f --- /dev/null +++ b/internal/gui/webview2_windows_test.go @@ -0,0 +1,65 @@ +//go:build windows && !nogui + +package gui + +import ( + "errors" + "os" + "os/exec" + "path/filepath" + "testing" + + "github.com/wailsapp/wails/v3/pkg/application" +) + +// application.New is one per process, so each case runs in a child of this +// test binary (ufec's way in #1806). +const testWebView2Env = "MAGPIE_TEST_WEBVIEW2" + +func TestWebView2Child(t *testing.T) { + mode := os.Getenv(testWebView2Env) + if mode == "" { + t.Skip("not the child") + } + opts := application.Options{Name: "webview2-check"} + if mode != "system" { + // a runtime folder that isn't one: Wails' lookup fails as it does + // on a machine with none + opts.Windows.WebviewBrowserPath = mode + } + if webView2Missing(application.New(opts).Env.Info().PlatformInfo) { + os.Exit(1) + } + os.Exit(0) +} + +func webView2Child(t *testing.T, mode string) int { + t.Helper() + self, err := os.Executable() + if err != nil { + t.Fatal(err) + } + cmd := exec.Command(self, "-test.run=^TestWebView2Child$") + cmd.Env = append(os.Environ(), testWebView2Env+"="+mode) + out, err := cmd.CombinedOutput() + var ee *exec.ExitError + switch { + case err == nil: + return 0 + case errors.As(err, &ee): + return ee.ExitCode() + } + t.Fatalf("child: %v\n%s", err, out) + return -1 +} + +// Wails' own lookup, as Run reads it, says missing for a runtime that isn't +// there, and not for this machine's. +func TestWebView2MissingFromWails(t *testing.T) { + if got := webView2Child(t, filepath.Join(t.TempDir(), "no-runtime")); got != 1 { + t.Fatalf("no runtime: child exit %d, want 1 (missing)", got) + } + if got := webView2Child(t, "system"); got != 0 { + t.Skipf("this machine has no WebView2 runtime itself (exit %d)", got) + } +}