Files
magpie/internal/gui/titlebar_darwin.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

219 lines
9.6 KiB
Go

//go:build !nogui
package gui
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
#import <crt_externs.h>
#import <mach-o/loader.h>
#import <objc/runtime.h>
static const NSWindowButton lightKinds[3] = {NSWindowCloseButton, NSWindowMiniaturizeButton, NSWindowZoomButton};
// MagpieLights keeps a window's traffic lights where its toolbar had put
// them, once the toolbar is gone. AppKit lays the title bar out again
// whenever it changes (a resize or zoom, leaving full screen, a new title or
// appearance), with its own height for the title bar and its own place for
// the lights. Each time, they're put back once AppKit is done: at once after
// a resize, whose animation draws its frames before the run loop turns, else
// as the run loop turns. While AppKit lays them out they can't be: it
// ignores a move of the light it is placing. When the system's appearance
// changes (light or dark, more contrast or less transparency), and at times
// on the way into full screen, AppKit draws the title bar at its own height
// before the run loop turns: the lights keep their distance from its top,
// not its bottom, which would draw them above the window.
@interface MagpieLights : NSObject {
@public
NSWindow *win; // not held: the window holds this; nil once it closes
CGFloat height; // the title bar's, as the toolbar had it
CGFloat x[3], top[3];
BOOL busy, pending;
}
@end
@implementation MagpieLights
- (void)apply {
if (busy || win == nil || (win.styleMask & NSWindowStyleMaskFullScreen)) return;
NSView *bar = [win standardWindowButton:NSWindowCloseButton].superview, *box = bar.superview, *frame = box.superview;
if (frame == nil) return;
busy = YES;
BOOL moved = NO;
NSRect f = box.frame;
NSRect want = NSMakeRect(f.origin.x, frame.isFlipped ? 0 : NSHeight(frame.bounds) - height, f.size.width, height);
if (!NSEqualRects(f, want)) {
box.frame = want;
moved = YES;
}
for (int i = 0; i < 3; i++) {
NSButton *b = [win standardWindowButton:lightKinds[i]];
if (b.superview != bar) continue;
NSAutoresizingMaskOptions keepTop = bar.isFlipped ? NSViewMaxYMargin : NSViewMinYMargin;
if (b.autoresizingMask != keepTop) b.autoresizingMask = keepTop;
NSPoint o = NSMakePoint(x[i], bar.isFlipped ? top[i] : NSHeight(bar.bounds) - top[i] - NSHeight(b.frame));
if (!NSEqualPoints(b.frame.origin, o)) {
[b setFrameOrigin:o];
moved = YES;
}
}
// AppKit follows the mouse over the lights, to show their symbols, where
// it laid them out; it looks again where they are as when a live resize
// ends (and during one, it will then).
if (moved && !frame.inLiveResize) [frame viewDidEndLiveResize];
busy = NO;
}
- (void)later {
if (pending) return;
pending = YES;
CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, ^{
pending = NO;
[self apply];
});
CFRunLoopWakeUp(CFRunLoopGetMain());
}
- (void)changed:(NSNotification *)n {
if (!busy) [self later];
}
- (void)resized:(NSNotification *)n {
[self apply];
[self later];
}
- (void)closing:(NSNotification *)n {
[[NSNotificationCenter defaultCenter] removeObserver:self];
win = nil;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
@end
static char lightsKey;
// A sheet, such as the folder chooser, begins below the title bar, as it
// began below the toolbar. With neither, AppKit lets it rise to the window's
// top, over the header: macOS 26 centres a sheet, so in a short window.
static NSRect sheetBelowTitlebar(id self, SEL _cmd, NSWindow *w, NSWindow *sheet, NSRect r) {
MagpieLights *l = objc_getAssociatedObject(w, &lightsKey);
if (l != nil && l->win != nil && !(w.styleMask & NSWindowStyleMaskFullScreen)) r.origin.y = NSHeight(w.frame) - l->height;
return r;
}
// A first press on the window while it isn't the key one (another app's is
// in front) moves it from anywhere in the title bar the toolbar had, as
// AppKit moved it then. AppKit now does so only in its own, a plain title
// bar's top 32pt, and the page doesn't take a first press: below that, the
// press would only bring the window forward. As AppKit does, a press on a
// view that takes it (a traffic light, an edge that resizes) is left alone.
static NSEvent *firstPress(NSEvent *e) {
NSWindow *w = e.window;
MagpieLights *l = w == nil ? nil : objc_getAssociatedObject(w, &lightsKey);
if (l == nil || l->win == nil || w.isKeyWindow || !w.isMovable || e.clickCount != 1 || (w.styleMask & NSWindowStyleMaskFullScreen)) return e;
NSView *frame = w.contentView.superview;
NSPoint p = e.locationInWindow;
// The content layout rect begins below AppKit's title bar.
if (frame == nil || !NSPointInRect(p, w.contentLayoutRect) || NSHeight(frame.bounds) - p.y >= l->height) return e;
NSView *hit = [frame hitTest:p];
if (hit == nil || [hit acceptsFirstMouse:e]) return e;
[w performWindowDragWithEvent:e];
return e;
}
// Whether AppKit gives this program the design of macOS 26, where a toolbar
// gives a window larger corners: from macOS 26, for a program linked against
// the macOS 26 SDK or a later one, unless its Info.plist asks for the earlier
// design (UIDesignRequiresCompatibility). Each is read at run time. The SDK is
// the one the program names, which depends on the toolchain that linked it:
// 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 macOS version
// isn't asked with @available(macOS 26.0, *): without make's flags, clang
// compiles this code for the Mac's own macOS, though the program is linked to
// run on earlier ones too, and on a Mac with macOS 26 the check would be
// compiled away.
static BOOL designOf26(void) {
if (![NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){26, 0, 0}]) return NO;
id compat = [NSBundle.mainBundle objectForInfoDictionaryKey:@"UIDesignRequiresCompatibility"];
if ([compat respondsToSelector:@selector(boolValue)] && [compat boolValue]) return NO;
const struct mach_header_64 *h = _NSGetMachExecuteHeader();
const struct load_command *c = (const struct load_command *)(h + 1);
for (uint32_t i = 0; i < h->ncmds; i++, c = (const struct load_command *)((const char *)c + c->cmdsize)) {
if (c->cmd == LC_BUILD_VERSION) return ((const struct build_version_command *)c)->sdk >= 0x1A0000; // 26.0
if (c->cmd == LC_VERSION_MIN_MACOSX) return ((const struct version_min_command *)c)->sdk >= 0x1A0000;
}
return NO;
}
// The Mac's title bar with no toolbar (the design of macOS 26). The main
// window has an empty toolbar only to inset the traffic lights into the
// page's header (MacTitleBarHiddenInset), and in that design a window with a
// toolbar has the larger corners of one: the size of Finder's, where every
// other window with a plain title bar has smaller ones (TextEdit's, and an
// Electron app's that insets its lights by moving them). The toolbar is
// taken off, and the lights are kept where it had them, so the header still
// fits them. The window is left as it was in the earlier design, where a
// toolbar doesn't change the corners (see designOf26), with its lights on
// the right (a right-to-left language), or with a title bar not laid out as
// expected.
static void insetLights(void *p) {
if (!designOf26()) return;
NSWindow *w = (NSWindow *)p;
if (w.toolbar == nil || objc_getAssociatedObject(w, &lightsKey) != nil) return;
if (w.windowTitlebarLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft) return;
NSView *bar = [w standardWindowButton:NSWindowCloseButton].superview, *box = bar.superview, *frame = box.superview;
if (frame == nil) return;
[frame layoutSubtreeIfNeeded];
MagpieLights *l = [[[MagpieLights alloc] init] autorelease];
l->win = w;
l->height = NSHeight(box.frame);
NSView *views[5] = {box, bar};
for (int i = 0; i < 3; i++) {
NSButton *b = [w standardWindowButton:lightKinds[i]];
if (b == nil || b.superview != bar) return;
l->x[i] = b.frame.origin.x;
l->top[i] = bar.isFlipped ? NSMinY(b.frame) : NSHeight(bar.bounds) - NSMaxY(b.frame);
views[2 + i] = b;
}
objc_setAssociatedObject(w, &lightsKey, l, OBJC_ASSOCIATION_RETAIN);
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
for (int i = 0; i < 5; i++) {
views[i].postsFrameChangedNotifications = YES;
[nc addObserver:l selector:@selector(changed:) name:NSViewFrameDidChangeNotification object:views[i]];
}
[nc addObserver:l selector:@selector(resized:) name:NSWindowDidResizeNotification object:w];
[nc addObserver:l selector:@selector(closing:) name:NSWindowWillCloseNotification object:w];
static BOOL watchingPresses;
if (!watchingPresses) {
watchingPresses = YES;
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskLeftMouseDown handler:^NSEvent *(NSEvent *e) { return firstPress(e); }];
}
// Wails' window delegate leaves sheets to AppKit; it's taught to place
// this window's (a window without the helper keeps AppKit's place).
SEL s = @selector(window:willPositionSheet:usingRect:);
id d = w.delegate;
if (d != nil && ![d respondsToSelector:s])
class_addMethod([d class], s, (IMP)sheetBelowTitlebar, protocol_getMethodDescription(@protocol(NSWindowDelegate), s, NO, YES).types);
w.toolbar = nil;
[l apply];
}
*/
import "C"
import "github.com/wailsapp/wails/v3/pkg/application"
// plainTitlebar gives the main window, in the design of macOS 26, the
// corners of a window with a plain title bar (see insetLights); the traffic
// lights stay inset into the page's header.
func plainTitlebar(w *application.WebviewWindow) {
application.InvokeSync(func() {
if p := w.NativeWindow(); p != nil {
C.insetLights(p)
}
})
}
func nameWindow(*application.WebviewWindow, string) {}
func ownFrame(*application.WebviewWindow) {}