Files
magpie/internal/gui/titlebar_darwin_test.go
T
forever 2c31129692 gui: on macOS 26 the main window has a plain title bar's corners (TextEdit's, not Finder's), its traffic lights still in the header (waitingsnow) (#1042)
* gui: on macOS 26 the main window has a plain title bar's corners (TextEdit's, not Finder's), its traffic lights still in the header (waitingsnow)

From macOS 26 a window with a toolbar has larger corners than one with a
plain title bar: 26pt, Finder's, against 16pt for TextEdit or an
Electron app. The main window has an empty toolbar only to inset its
traffic lights into the page's header (Wails' MacTitleBarHiddenInset),
so its corners were a toolbar window's. On macOS 15 a toolbar doesn't
change them (12pt either way, measured on 15.8).

On macOS 26 and later plainTitlebar now takes the toolbar off
(insetLights, titlebar_darwin.go), and MagpieLights keeps the lights and
the title bar's 66pt height where the toolbar had them. AppKit lays the
title bar out again at its own 32pt after a resize or zoom, a new title,
a new appearance and leaving full screen; the lights are put back once
it is done, and AppKit's hover tracking is told where they went. Their
autoresizing mask keeps their distance from the top: AppKit draws a new
system appearance (light or dark, more contrast, less transparency)
before the run loop turns, which drew them 8pt above the window for one
frame. A sheet, such as the folder chooser, still begins below the title
bar (Wails' window delegate gets window:willPositionSheet:usingRect:,
which places this window's sheets only).

A first press while another app is in front still moves the window from
anywhere in the 66pt title bar, as AppKit moved it with the toolbar
(firstPress, a local mouse-down monitor). AppKit's own band is now a
plain title bar's 32pt; below it the page doesn't take a first press, so
the press would only have brought the window forward. A press on a view
that takes a first press (a traffic light, a resize edge) is left alone,
as AppKit leaves it.

The macOS version is asked at run time. go build and go test build the
cgo objects for the Mac's own macOS, where @available(macOS 26.0, *) is
compiled away: a test binary built so on 26.7.1 took the toolbar off on
15.8. An older macOS, the lights on the right (a right-to-left language)
or a title bar not laid out as expected keep the toolbar. Linux and
Windows are unchanged; the GTK 3 comment now says its corners stay
square.

TestMainWindowCorners (new; darwin, run by CI's macOS job) measures the
main window beside a plain and a toolbar window in a process of its own
(runAppKit, so WebKit's data stays out of the real ~/Library): once
shown, after a new title and size and a new system appearance, as AppKit
draws that appearance, a first press 20, 40, 60 and 70pt below the top
while no window is key, and where a sheet begins.
TestMainWindowRightToLeft launches it right to left. Both pass on macOS
26.7.1 (3 runs in a row), 26.6.2 (CI's) and 15.8 Intel, where the
toolbar stays. With each part taken out, they fail:
- the change itself: "corners of 26.00pt; a window with a plain title
  bar has 16.00pt, one with a toolbar 26.00pt";
- putting the lights back: "traffic light 1 at [16 16] (x, y from the
  top left), the toolbar had it at [26 26]";
- the height: "a title bar 32pt tall, the toolbar's was 66pt";
- the mask: "as AppKit draws a new system appearance, traffic light 1
  is at [26 -8]";
- the sheet's place: "a sheet on a short window begins 16pt below its
  top; below the toolbar it began 66pt";
- the press monitor: "a first press 40pt below the top, with another
  app in front, began 0 window drags; with the toolbar it began 1";
- the right-to-left check: "the traffic lights are on the right, and
  the main window's toolbar was taken off";
- the run-time check (@available instead), on 15.8: "a toolbar doesn't
  change the corners on this macOS, yet the main window's was taken
  off".
The presses are sent within the test's own process, and a drag is
counted, not begun; no system setting changes.

The real app on 26.7.1, built from this commit and driven through
Accessibility under a HOME of its own, has 16.25pt corners (26.50pt on
main) and its lights where main has them, through resize, zoom, light
and dark, a new title, full screen, minimise, close and show, a
maximised start, and the window lightweight mode makes again. A real
pointer and a key window's press were not tried. go vet, the three
builds and go test -tags nogui ./... pass.

* gui: where AppKit gives magpie the Mac's earlier design (an SDK before macOS 26's, or UIDesignRequiresCompatibility), the main window keeps its toolbar (waitingsnow)

The main window's toolbar comes off only in the design of macOS 26,
where a toolbar gives a window larger corners. AppKit gives a program
that design from macOS 26 when the program is linked against the macOS
26 SDK or a later one and its Info.plist doesn't set
UIDesignRequiresCompatibility; the last commit asked only for the macOS
version. A program linked against an earlier SDK gets the earlier design
on macOS 26 too: yetone's test binary (Go 1.26.3, Xcode 26.2) names SDK
11.0, and on 26.2 every window there had 12pt corners, yet the main
window's toolbar was taken off and TestMainWindowCorners failed at every
stage.

designOf26 now reads each at run time: the macOS version (NSProcessInfo),
the key (NSBundle) and the SDK the program names. The SDK is in
LC_BUILD_VERSION, or in LC_VERSION_MIN_MACOSX in a program that runs on
macOS 10.13, as go build links one for an Intel Mac. The header is read
through _NSGetMachExecuteHeader (crt_externs.h):
dyld_get_program_sdk_version is declared only in dyld's private header,
and it doesn't see the key (27.0 in a bundle that sets it, where AppKit
gives the earlier design). The comment no longer says go build builds
for the Mac's own macOS: the program names 11.0, and it is clang that
compiles the cgo code for the Mac's own macOS (ld: "built for newer
'macOS' version (26.0) than being linked (11.0)"), where
@available(macOS 26.0, *) would be compiled away.

TestMainWindowCorners now runs the windows' program twice, as linked
against the macOS 26 SDK and against macOS 15's (runAppKitSDK; stampSDK
rewrites the SDK in a copy and signs it again), so on macOS 26 it checks
both designs whatever SDK the toolchain names. In the earlier design it
checks that the main window keeps its toolbar. gui-shell.md says which
design gets the change, which SDKs the test runs with, why the key has
no test, and how to run it as for an Intel Mac.

Both subtests and TestMainWindowRightToLeft pass on 26.7.1 (arm64, 3
runs in a row, and amd64 under Rosetta), 26.6.2 (arm64, as CI) and 15.8
(Intel). With each new part taken out, its subtest fails:
- reading the SDK (designOf26 answering yes after the macOS and the
  key): "a toolbar doesn't change the corners in this design, yet the
  main window's was taken off", with macOS 15's SDK;
- 26.0 itself (> for >=): "corners of 26.00pt; a window with a plain
  title bar has 16.00pt, one with a toolbar 26.00pt", with the macOS 26
  SDK;
- reading LC_VERSION_MIN_MACOSX, or 26.0 there, built for amd64: the
  corners message, with the macOS 26 SDK;
- the macOS version, asked with @available instead, built on 26.7.1 for
  amd64 and run on 15.8: the toolbar message, with the macOS 26 SDK.
Taking out the last commit's parts fails the macOS 26 SDK subtest or
TestMainWindowRightToLeft as before.

UIDesignRequiresCompatibility is checked by hand, not in a test: AppKit
reads it from an app bundle's Info.plist, LaunchServices registers each
bundle that runs (and keeps a temporary one after it is deleted), and in
a process run from a bundle a press sent within it began a window drag
in only 3 of 12 runs. On 26.7.1 the test program in a bundle whose
Info.plist sets the key has 12pt corners like every window there, and
keeps its toolbar; in a bundle without the key it has 16pt and no
toolbar, as when run on its own. With the key's check taken out, the
toolbar is taken off although AppKit gives the earlier design. The
bundles were unregistered (lsregister -u) and deleted.

go vet, the three builds, go test -tags nogui ./... and go test
./internal/gui/... pass. The real app wasn't run again.

* ci: run the checks again (waitingsnow)

test (macos-latest) failed in TestRelayKeepsClientAlive (internal/gateway, 1 ping in a 150ms gap), a timing test this PR doesn't touch; it passes on main and 5 times of 5 locally.
2026-10-13 09:55:23 +08:00

278 lines
11 KiB
Go

//go:build darwin && cgo && !nogui
package gui
import (
"bytes"
"encoding/json"
"fmt"
"math"
"net/http"
"os"
"testing"
"time"
"unsafe"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
"github.com/yetone/magpie/internal/gui/windowshape"
)
// titlebarRun is what the separate process measured: the main window, one
// with a plain title bar and one with the toolbar (the main window's as it
// was), at each stage, the main window and the toolbar one as AppKit draws
// a new system appearance, how many window drags a first press Y points
// below the top of each began, and how far below the top a sheet on each
// begins.
type titlebarRun struct {
Stages []struct {
Name string
Main, Plain, Toolbar windowshape.Shape
}
Drawn struct{ Main, Toolbar windowshape.Shape }
Presses []struct {
Y float64
Main, Plain, Toolbar int
}
Sheet struct{ Main, Plain, Toolbar float64 }
}
const titlebarMark = "titlebar run: "
// The macOS SDKs a test runs the windows' program as linked against
// (runAppKitSDK): AppKit gives one linked against macOS 26's or a later one
// the design of macOS 26.
const (
sdkMacOS15 uint32 = 15 << 16
sdkMacOS26 uint32 = 26 << 16
)
// The main window has the corners of a window with a plain title bar
// (TextEdit's, an Electron app's), not a toolbar's: in the design of macOS
// 26 one with a toolbar has larger ones. Its traffic lights, its title bar's
// height and where a sheet on it begins stay where the toolbar had them,
// also after a new title and size and a new system appearance. The lights
// are in place as AppKit draws that appearance, too. A first press while
// another app is in front moves it from where it did with the toolbar. In
// the earlier design, where a toolbar doesn't change the corners (an older
// macOS, or a program linked against an earlier SDK than macOS 26's), the
// window keeps its toolbar. The windows' program is run as linked against
// each SDK, whatever the toolchain has, in a process of its own that runs
// AppKit on its main thread.
func TestMainWindowCorners(t *testing.T) {
for _, sdk := range []struct {
name string
v uint32
}{{"macOS 26 SDK", sdkMacOS26}, {"macOS 15 SDK", sdkMacOS15}} {
t.Run(sdk.name, func(t *testing.T) { checkMainWindowCorners(t, sdk.v) })
}
}
func checkMainWindowCorners(t *testing.T, sdk uint32) {
var run titlebarRun
measureWindows(t, sdk, "corners", &run)
near := func(a, b float64) bool { return math.Abs(a-b) < 0.5 }
for _, s := range run.Stages {
t.Logf("%s: corners %.2fpt (plain %.2f, toolbar %.2f), lights %v (toolbar %v), title bar %.0fpt (toolbar %.0f), toolbar kept %v", s.Name,
s.Main.Radius, s.Plain.Radius, s.Toolbar.Radius, s.Main.Lights, s.Toolbar.Lights, s.Main.Titlebar, s.Toolbar.Titlebar, s.Main.Toolbar)
if s.Plain.Radius < 0 {
t.Errorf("%s: AppKit doesn't tell a window's corner radius", s.Name)
} else if !near(s.Main.Radius, s.Plain.Radius) {
t.Errorf("%s: corners of %.2fpt; a window with a plain title bar has %.2fpt, one with a toolbar %.2fpt",
s.Name, s.Main.Radius, s.Plain.Radius, s.Toolbar.Radius)
}
for l := range 3 {
if m, tb := s.Main.Lights[l], s.Toolbar.Lights[l]; !near(m[0], tb[0]) || !near(m[1], tb[1]) {
t.Errorf("%s: traffic light %d at %v (x, y from the top left), the toolbar had it at %v", s.Name, l+1, m, tb)
}
}
if !near(s.Main.Titlebar, s.Toolbar.Titlebar) {
t.Errorf("%s: a title bar %.0fpt tall, the toolbar's was %.0fpt", s.Name, s.Main.Titlebar, s.Toolbar.Titlebar)
}
// Where a toolbar doesn't change the corners (the earlier design),
// the main window is left as it was
if near(s.Plain.Radius, s.Toolbar.Radius) && !s.Main.Toolbar {
t.Errorf("%s: a toolbar doesn't change the corners in this design, yet the main window's was taken off", s.Name)
}
}
if len(run.Stages) != 3 {
t.Errorf("measured %d stages, want 3", len(run.Stages))
}
t.Logf("as AppKit draws a new system appearance: lights %v (toolbar %v), title bar %.0fpt", run.Drawn.Main.Lights,
run.Drawn.Toolbar.Lights, run.Drawn.Main.Titlebar)
for l := range 3 {
if m, tb := run.Drawn.Main.Lights[l], run.Drawn.Toolbar.Lights[l]; !near(m[0], tb[0]) || !near(m[1], tb[1]) {
t.Errorf("as AppKit draws a new system appearance, traffic light %d is at %v (x, y from the top left); the toolbar had it at %v",
l+1, m, tb)
}
}
for _, p := range run.Presses {
t.Logf("a first press %.0fpt below the top, another app in front: %d window drags (plain %d, toolbar %d)", p.Y, p.Main, p.Plain, p.Toolbar)
if p.Main < 0 || p.Plain < 0 || p.Toolbar < 0 {
t.Errorf("a window was the key one, so a press as when another app is in front couldn't be tried")
} else if p.Main != p.Toolbar {
t.Errorf("a first press %.0fpt below the top, with another app in front, began %d window drags; with the toolbar it began %d (with a plain title bar: %d)",
p.Y, p.Main, p.Toolbar, p.Plain)
}
}
// AppKit moves each of them from the top of its title bar: a press there
// that moved none didn't arrive
if len(run.Presses) != 4 || run.Presses[0].Plain != 1 || run.Presses[0].Toolbar != 1 {
t.Errorf("a first press near the top didn't begin a window drag, or not each was tried: %+v", run.Presses)
}
t.Logf("a sheet begins %.0fpt below the top (plain %.0f, toolbar %.0f)", run.Sheet.Main, run.Sheet.Plain, run.Sheet.Toolbar)
if !near(run.Sheet.Main, run.Sheet.Toolbar) {
t.Errorf("a sheet on a short window begins %.0fpt below its top; below the toolbar it began %.0fpt (with a plain title bar: %.0fpt)",
run.Sheet.Main, run.Sheet.Toolbar, run.Sheet.Plain)
}
}
// With its traffic lights on the right, as in a right-to-left language,
// the main window keeps its toolbar, also in the design of macOS 26: the
// lights are kept in place from the left.
func TestMainWindowRightToLeft(t *testing.T) {
var s windowshape.Shape
// as Xcode launches an app in its right-to-left pseudolanguage
measureWindows(t, sdkMacOS26, "right to left", &s,
"-AppleTextDirection", "YES", "-NSForceRightToLeftWritingDirection", "YES")
t.Logf("lights %v (x, y from the top left), toolbar kept %v", s.Lights, s.Toolbar)
if s.Lights[0][0] < s.Lights[2][0] {
t.Fatalf("launched right to left, the traffic lights are on the left: %v", s.Lights)
}
if !s.Toolbar {
t.Errorf("the traffic lights are on the right, and the main window's toolbar was taken off")
}
}
// measureWindows measures the windows in mode in a process of its own, run
// as linked against the macOS SDK sdk (runAppKitSDK), with args for AppKit,
// and decodes what it measured into v.
func measureWindows(t *testing.T, sdk uint32, mode string, v any, args ...string) {
t.Helper()
out, err := runAppKitSDK(t, sdk, 40*time.Second, []string{"MAGPIE_TEST_TITLEBAR=" + mode}, args...)
i := bytes.LastIndex(out, []byte(titlebarMark))
if err != nil || i < 0 {
t.Fatalf("measuring the windows: %v\n%s", err, out)
}
line, _, _ := bytes.Cut(out[i+len(titlebarMark):], []byte("\n"))
if err := json.Unmarshal(line, v); err != nil {
t.Fatalf("%v: %s", err, line)
}
}
// The process measureWindows runs: the arguments go to AppKit, as the
// test's own flags are never parsed.
func init() {
mode := os.Getenv("MAGPIE_TEST_TITLEBAR")
if mode == "" {
return
}
app := application.New(application.Options{
Name: "magpie title bar regression",
Mac: application.MacOptions{ActivationPolicy: application.ActivationPolicyProhibited},
Assets: application.AssetOptions{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<body style="background:#f4f1ea">`)
})},
})
h := &host{app: app}
main := h.makeMain("/")
if mode == "right to left" {
app.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) {
plainTitlebar(main)
go func() {
application.InvokeSync(func() { main.SetSize(900, 600); main.Show() })
time.Sleep(time.Second)
var s windowshape.Shape
application.InvokeSync(func() { s = windowshape.Of(main.NativeWindow()) })
measured(app, s)
}()
})
runAndExit(app)
}
ref := func(tb application.MacTitleBar) *application.WebviewWindow {
m := mainMacWindow()
m.TitleBar = tb
return app.Window.NewWithOptions(application.WebviewWindowOptions{URL: "/", Width: 900, Height: 600, Hidden: true, Mac: m})
}
wins := []*application.WebviewWindow{main, ref(application.MacTitleBarHidden), ref(application.MacTitleBarHiddenInset)}
started := make(chan struct{})
app.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) {
plainTitlebar(main) // as Run does, before the window is first shown
close(started)
})
each := func(f func(i int, w *application.WebviewWindow)) {
application.InvokeSync(func() {
for i, w := range wins {
f(i, w)
}
})
}
go func() {
<-started
var run titlebarRun
measure := func(name string) {
time.Sleep(time.Second)
var s [3]windowshape.Shape
each(func(i int, w *application.WebviewWindow) { s[i] = windowshape.Of(w.NativeWindow()) })
run.Stages = append(run.Stages, struct {
Name string
Main, Plain, Toolbar windowshape.Shape
}{name, s[0], s[1], s[2]})
}
each(func(_ int, w *application.WebviewWindow) { w.SetSize(900, 600); w.Show() })
measure("shown")
each(func(_ int, w *application.WebviewWindow) { w.SetTitle("magpie, renamed"); w.SetSize(1000, 680) })
measure("a new title and size")
// AppKit draws a new system appearance before the run loop turns:
// the windows are measured at once, and again once it has
application.InvokeSync(func() {
windowshape.FlipIncreaseContrast()
run.Drawn.Main, run.Drawn.Toolbar = windowshape.Of(main.NativeWindow()), windowshape.Of(wins[2].NativeWindow())
})
measure("a new system appearance")
// The app may not activate, so the windows are never the key one
for _, y := range []float64{20, 40, 60, 70} {
var d [3]int
each(func(i int, w *application.WebviewWindow) { d[i] = windowshape.FirstPress(w.NativeWindow(), 200, y) })
run.Presses = append(run.Presses, struct {
Y float64
Main, Plain, Toolbar int
}{y, d[0], d[1], d[2]})
}
// macOS 26 centres a sheet, no higher than where it may begin: in a
// short window, that's where it is
each(func(_ int, w *application.WebviewWindow) { w.SetSize(900, 480) })
time.Sleep(time.Second)
var sheets [3]unsafe.Pointer
each(func(i int, w *application.WebviewWindow) { sheets[i] = windowshape.BeginSheet(w.NativeWindow(), 448) })
time.Sleep(1500 * time.Millisecond)
var tops [3]float64
each(func(i int, w *application.WebviewWindow) {
tops[i] = windowshape.SheetTop(w.NativeWindow(), sheets[i])
windowshape.EndSheet(w.NativeWindow(), sheets[i])
})
run.Sheet.Main, run.Sheet.Plain, run.Sheet.Toolbar = tops[0], tops[1], tops[2]
measured(app, run)
}()
runAndExit(app)
}
// measured prints what was measured, for measureWindows, and quits.
func measured(app *application.App, v any) {
b, err := json.Marshal(v)
if err != nil {
panic(err)
}
fmt.Println(titlebarMark + string(b))
app.Quit()
}
func runAndExit(app *application.App) {
if err := app.Run(); err != nil {
panic(err)
}
os.Exit(0)
}