gui: Windows on ARM puts the text size on WebView2 itself, since Wails can't pass it a double there (#2212, EdwinDigital)

On windows/arm64 Wails beta.24's SetZoom fails with
ErrDoubleArgUnsupported and only logs it: Go's stdcall on arm64 loads
x0-x7 and never d0 (golang/go#62583), so ICoreWebView2Controller::
put_ZoomFactor(double) can't be called through SyscallN. The text size
was saved and never applied.

zoom_windows_arm64.go calls put_ZoomFactor (vtbl slot 8) through a
two-instruction thunk in VirtualAlloc'd executable memory,
`fmov d0, x1; br x2`, called as SyscallN(thunk, this, bits, fn). The
controller is reached by reflection (WebviewWindow.impl.chromium.
controller). The window's Zoom option goes through the same broken call,
so on that platform the size is put on each page once navigation
completes (zoomsAfterLoad), on the UI thread. Other platforms are
unchanged: setPageZoom moves to zoom_other.go.

Run:
- The thunk's encoding in a C harness on macOS arm64 (MAP_JIT): it
  passed 1.25 in d0, returned the callee's HRESULT, kept `this`.
- On the Windows 11 AMD64 box, in a sandbox profile on its own port, a
  check build of this change (same reflection path and vtbl slot, the
  call made directly since amd64 passes doubles in X1): put_ZoomFactor
  returned S_OK for both windows at 110% and 150%, and screenshots show
  the page drawn at 150%.
- gofmt -l; go vet ./... darwin, GOOS=linux -tags nogui, GOOS=windows,
  and GOOS=windows GOARCH=arm64 (vet + build); go test ./internal/gui/.

Not tried: the thunk on real Windows on ARM hardware; the reporter is
asked to confirm. No GUI assets changed, so the Playwright suite wasn't run.
This commit is contained in:
yetone
2026-10-15 00:34:14 +08:00
parent e98440ae6b
commit d5873b0d85
5 changed files with 163 additions and 4 deletions
+4
View File
@@ -213,6 +213,10 @@ func boolInt(b bool) int {
return 0
}
// zoomsAfterLoad: the Mac's text size is put on a page by applyZoom and
// madeAgain.
const zoomsAfterLoad = false
// setPageZoom zooms w's page to z, the text size.
func setPageZoom(w *application.WebviewWindow, z float64) {
if p := w.NativeWindow(); p != nil {
-4
View File
@@ -28,7 +28,3 @@ func (h *host) glidePanel(height int, g Glide) bool {
// TintPanel: the page paints the panel's tint itself here.
func (h *host) TintPanel(c [4]uint8, ms int) bool { return false }
// setPageZoom zooms w's page to z, as a browser zooms (WebView2's zoom
// factor, WebKitGTK's zoom level): the text size.
func setPageZoom(w *application.WebviewWindow, z float64) { w.SetZoom(z) }
+14
View File
@@ -56,6 +56,7 @@ func (h *host) makeMain(url string) *application.WebviewWindow {
Windows: winOpts,
BackgroundColour: winBg,
})
h.zoomWhenLoaded(w)
// A resize is kept once it settles (settle): the size, or that the
// window is maximised. Nothing is kept of a window not placed and shown
// yet (placeMain: a text size can resize it, unmaximised as yet), one
@@ -113,6 +114,7 @@ func (h *host) makePanel() *application.WebviewWindow {
po := panelOptions(runtime.GOOS, h.query)
po.Width, po.Height, po.Zoom = h.panelW(), cmp.Or(h.panelHeight, zoomed(panelStart, z)), z
w := h.app.Window.NewWithOptions(po)
h.zoomWhenLoaded(w)
if h.tray != nil {
h.tray.AttachWindow(w).WindowOffset(6)
}
@@ -138,6 +140,18 @@ func (h *host) makePanel() *application.WebviewWindow {
return w
}
// zoomWhenLoaded puts the text size on w's page each time the page has
// come, where the window's Zoom option doesn't reach it (zoomsAfterLoad:
// Windows on ARM, #2212). A size picked before then is put on it here too.
func (h *host) zoomWhenLoaded(w *application.WebviewWindow) {
if !zoomsAfterLoad {
return
}
w.OnWindowEvent(events.Windows.WebViewNavigationCompleted, func(*application.WindowEvent) {
application.InvokeAsync(func() { setPageZoom(w, h.zoom()) })
})
}
// madeAgain gives a window made again after lightweight mode let it go
// what the start gave the first: the Mac's text size (Windows' and
// Linux's took it with the options), the main window's title bar
+12
View File
@@ -0,0 +1,12 @@
//go:build !darwin && !nogui && !(windows && arm64)
package gui
import "github.com/wailsapp/wails/v3/pkg/application"
// setPageZoom zooms w's page to z, as a browser zooms (WebView2's zoom
// factor, WebKitGTK's zoom level): the text size.
func setPageZoom(w *application.WebviewWindow, z float64) { w.SetZoom(z) }
// zoomsAfterLoad: the window takes its text size with its options (Zoom).
const zoomsAfterLoad = false
+133
View File
@@ -0,0 +1,133 @@
//go:build windows && arm64 && !nogui
package gui
import (
"errors"
"fmt"
"log"
"math"
"reflect"
"sync"
"syscall"
"unsafe"
"github.com/wailsapp/wails/v3/pkg/application"
"golang.org/x/sys/windows"
)
// setPageZoom zooms w's page to z, the text size. On Windows on ARM,
// Wails' SetZoom (and the Zoom option a window is made with) never reaches
// the page: WebView2's put_ZoomFactor takes its double by value, which the
// ARM64 ABI passes in d0, and Go's syscall fills only x0–x7
// (golang.org/issue/62583), so Wails refuses the call and only logs it. The
// choice was saved and shown picked, and the page stayed at 100% (#2212).
// magpie makes the call itself, through a two-instruction thunk that moves
// the double's bits from x1 into d0.
func setPageZoom(w *application.WebviewWindow, z float64) {
if err := putZoomFactor(w, z); err != nil {
log.Println("text size:", err)
}
}
// zoomsAfterLoad: a window's Zoom option is lost here, so its text size is
// put on its page once WebView2 has made the page's controller.
const zoomsAfterLoad = true
// putZoomFactor calls ICoreWebView2Controller::put_ZoomFactor(z) on w's
// WebView2 controller.
func putZoomFactor(w *application.WebviewWindow, z float64) error {
ctrl := webviewController(w)
if ctrl == nil {
return errors.New("no WebView2 controller yet")
}
thunk, err := doubleThunk()
if err != nil {
return err
}
// ICoreWebView2ControllerVtbl: QueryInterface, AddRef, Release,
// get_IsVisible, put_IsVisible, get_Bounds, put_Bounds,
// get_ZoomFactor, put_ZoomFactor (WebView2.h; Wails'
// ICoreWebView2ControllerVtbl in the same order)
const putZoomFactorSlot = 8
vtbl := *(*unsafe.Pointer)(ctrl)
fn := *(*uintptr)(unsafe.Add(vtbl, putZoomFactorSlot*unsafe.Sizeof(uintptr(0))))
hr, _, _ := syscall.SyscallN(thunk, uintptr(ctrl), uintptr(math.Float64bits(z)), fn)
if int32(hr) < 0 {
return fmt.Errorf("put_ZoomFactor(%g): %w", z, windows.Errno(hr))
}
return nil
}
// webviewController is w's ICoreWebView2Controller, nil before WebView2
// has made it. Wails keeps it unexported: WebviewWindow.impl, a
// *windowsWebviewWindow, holds chromium, an *edge.Chromium, which holds
// controller (Wails v3.0.0-beta.24). A Wails without that path gives nil,
// and the text size stays as Wails left it.
func webviewController(w *application.WebviewWindow) unsafe.Pointer {
if w == nil {
return nil
}
v := reflect.ValueOf(w).Elem()
for _, name := range []string{"impl", "chromium", "controller"} {
if v.Kind() != reflect.Struct {
return nil
}
v = v.FieldByName(name)
for v.IsValid() && v.Kind() == reflect.Interface {
if v.IsNil() {
return nil
}
v = v.Elem()
}
if !v.IsValid() || v.Kind() != reflect.Pointer || v.IsNil() {
return nil
}
if name != "controller" {
v = v.Elem()
}
}
return v.UnsafePointer()
}
var (
flushInstructionCache = windows.NewLazySystemDLL("kernel32.dll").NewProc("FlushInstructionCache")
thunkOnce sync.Once
thunkAddr uintptr
thunkErr error
)
// doubleThunk is code that calls x2 with x0 as it is and the bits in x1 as
// the double in d0, so a COM method taking (this, double) is called as
// SyscallN(thunk, this, bits, method):
//
// fmov d0, x1
// br x2
//
// br leaves the return address the caller's bl put in x30, so the method
// returns straight to Go's asmstdcall with its HRESULT in x0.
func doubleThunk() (uintptr, error) {
thunkOnce.Do(func() {
code := []uint32{0x9E670020, 0xD61F0040}
size := uintptr(len(code) * 4)
addr, err := windows.VirtualAlloc(0, size, windows.MEM_COMMIT|windows.MEM_RESERVE, windows.PAGE_READWRITE)
if err != nil {
thunkErr = fmt.Errorf("VirtualAlloc: %w", err)
return
}
// addr is memory VirtualAlloc gave, not Go's
copy(unsafe.Slice((*uint32)(*(*unsafe.Pointer)(unsafe.Pointer(&addr))), len(code)), code)
var old uint32
if err := windows.VirtualProtect(addr, size, windows.PAGE_EXECUTE_READ, &old); err != nil {
thunkErr = fmt.Errorf("VirtualProtect: %w", err)
return
}
if ok, _, err := flushInstructionCache.Call(uintptr(windows.CurrentProcess()), addr, size); ok == 0 {
thunkErr = fmt.Errorf("FlushInstructionCache: %w", err)
return
}
thunkAddr = addr
})
return thunkAddr, thunkErr
}