AlanWang1027713 on X: a right-click on the main window's title bar, by the lights, offered "Icon and Text / Icon Only / Text Only". It comes from the empty NSToolbar that MacTitleBarHiddenInset puts there to inset the lights; NSThemeFrame's menuForEvent: lists its display modes. insetLights takes the toolbar away only in the macOS 26 design, so on macOS 27, with an older SDK and in a right-to-left layout the toolbar, and its menu, stayed. quietToolbar turns the menu off where the toolbar stays: allowsDisplayModeCustomization = NO (macOS 15+), and AppKit's own _setWantsToolbarContextMenu: NO for macOS 12-14, each looked up with respondsToSelector, since the SDK that builds magpie may be older than the system it runs on. Nothing about the title bar's shape changes. windowshape.Of now reads the menu a right-click 20pt below the top, 120pt in, gets from the window frame (TitlebarMenu), and TestMainWindowCorners and TestMainWindowRightToLeft fail when the main window gets one, or when the reference toolbar window doesn't (so the probe is proven to see it). Run on mote (macOS 27.2), real makeMain window: - go test ./internal/gui -run 'TestMainWindowCorners|TestMainWindowRightToLeft' -count=1 -v: ok. Each stage logs "title bar menu []" next to the reference toolbar window's ["Icon and Text" "Icon Only" "Text Only" "-"] (macOS 26 SDK) and ["-"] (macOS 15 SDK). Lights [[26 26] [49 26] [72 26]], 66pt title bar and 16pt corners with the 26 SDK, [[19 19] [39 19] [59 19]] and 38pt with the 15 SDK: the same as without the change. - Broken by hand (quietToolbar call taken out): both FAIL, "a right-click on the title bar gets a menu: ["Icon and Text" "Icon Only" "Text Only" "-"]" (26 SDK) and ["-"] (15 SDK). - A probe app on mote: either switch alone empties the frame's menu. Suite and checks, on mote at aa31e737c8d271455865615fafdf512e09073663 (this commit and its pair, the tray logo size / the toolbar menu): - go test -count=1 -timeout 45m ./... in a sandbox HOME, caches pinned: exit 0, 103 packages ok, none FAIL (internal/gateway 1633s, internal/agent 949s, internal/provider 470s, internal/gui 295s). - gofmt -l internal/gui: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go build, GOOS=linux go build -tags nogui, GOOS=windows go build: all ok. - No GUI asset changed, so the Playwright suite wasn't run. After a rebase onto42c43c002(Antigravity sessions, internal/gui sessions reads; no file in common), at the pushed head: go vet, GOOS=windows and GOOS=linux -tags nogui go vet of ./internal/gui/... and go test -count=1 ./internal/gui/...: ok. Then rebased ontof7e0f68ca(Codex instructions, internal/provider only), not run again. Not tried: a screen screenshot (screencapture over ssh has no Screen Recording permission on mote; the menu is read from AppKit instead); macOS 12-14, where only the private selector applies.
172 lines
6.6 KiB
Go
172 lines
6.6 KiB
Go
//go:build cgo
|
|
|
|
package windowshape
|
|
|
|
/*
|
|
#cgo CFLAGS: -x objective-c
|
|
#cgo LDFLAGS: -framework Cocoa
|
|
#import <Cocoa/Cocoa.h>
|
|
#import <objc/message.h>
|
|
#import <objc/runtime.h>
|
|
#include <stdlib.h>
|
|
|
|
// AppKit's own: no public API gives a window's corner radius.
|
|
static double cornerRadius(void *p) {
|
|
SEL s = sel_registerName("_cornerRadius");
|
|
if (![(id)p respondsToSelector:s]) return -1;
|
|
return ((CGFloat (*)(id, SEL))objc_msgSend)((id)p, s);
|
|
}
|
|
|
|
static int hasToolbar(void *p) { return ((NSWindow *)p).toolbar != nil; }
|
|
|
|
static void lights(void *p, double *out) {
|
|
NSWindow *w = (NSWindow *)p;
|
|
NSWindowButton kinds[3] = {NSWindowCloseButton, NSWindowMiniaturizeButton, NSWindowZoomButton};
|
|
for (int i = 0; i < 3; i++) {
|
|
NSButton *b = [w standardWindowButton:kinds[i]];
|
|
NSRect r = [b convertRect:b.bounds toView:nil];
|
|
out[2 * i] = NSMidX(r);
|
|
out[2 * i + 1] = NSHeight(w.frame) - NSMidY(r);
|
|
}
|
|
out[6] = NSHeight([w standardWindowButton:NSWindowCloseButton].superview.superview.frame);
|
|
}
|
|
|
|
// The menu a right-click 20pt below the top of w, by its traffic lights,
|
|
// gets from the window's frame, where one the page passes up ends: its
|
|
// items' titles, a line for a separator, joined by newlines; "" for none.
|
|
static char *titlebarMenu(void *p) {
|
|
NSWindow *w = (NSWindow *)p;
|
|
NSView *frame = w.contentView.superview;
|
|
NSEvent *e = [NSEvent mouseEventWithType:NSEventTypeRightMouseDown location:NSMakePoint(120, NSHeight(w.frame) - 20)
|
|
modifierFlags:0 timestamp:NSProcessInfo.processInfo.systemUptime windowNumber:w.windowNumber context:nil
|
|
eventNumber:0 clickCount:1 pressure:1];
|
|
NSMutableArray *items = [NSMutableArray array];
|
|
for (NSMenuItem *i in [frame menuForEvent:e].itemArray) [items addObject:i.isSeparatorItem ? @"-" : i.title];
|
|
return strdup([items componentsJoinedByString:@"\n"].UTF8String);
|
|
}
|
|
|
|
static void *beginSheet(void *p, double h) {
|
|
NSWindow *s = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, h) styleMask:NSWindowStyleMaskTitled
|
|
backing:NSBackingStoreBuffered defer:NO];
|
|
[(NSWindow *)p beginSheet:s completionHandler:nil];
|
|
return s;
|
|
}
|
|
|
|
static double sheetTop(void *p, void *s) {
|
|
return NSMaxY(((NSWindow *)p).frame) - NSMaxY(((NSWindow *)s).frame);
|
|
}
|
|
|
|
static void endSheet(void *p, void *s) {
|
|
[(NSWindow *)p endSheet:(NSWindow *)s];
|
|
[(NSWindow *)s release];
|
|
}
|
|
|
|
static BOOL moreContrast;
|
|
static BOOL increaseContrast(id self, SEL _cmd) { return moreContrast; }
|
|
|
|
static void flipIncreaseContrast(void) {
|
|
static BOOL answered;
|
|
if (!answered) {
|
|
moreContrast = NSWorkspace.sharedWorkspace.accessibilityDisplayShouldIncreaseContrast;
|
|
method_setImplementation(class_getInstanceMethod([NSWorkspace class], @selector(accessibilityDisplayShouldIncreaseContrast)),
|
|
(IMP)increaseContrast);
|
|
answered = YES;
|
|
}
|
|
moreContrast = !moreContrast;
|
|
[NSWorkspace.sharedWorkspace.notificationCenter postNotificationName:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification
|
|
object:NSWorkspace.sharedWorkspace];
|
|
}
|
|
|
|
static int drags;
|
|
static void countDrag(id self, SEL _cmd, NSEvent *e) { drags++; }
|
|
|
|
static NSEvent *press(NSWindow *w, NSEventType t, NSPoint at) {
|
|
return [NSEvent mouseEventWithType:t location:at modifierFlags:0 timestamp:NSProcessInfo.processInfo.systemUptime
|
|
windowNumber:w.windowNumber context:nil eventNumber:0 clickCount:1 pressure:t == NSEventTypeLeftMouseDown];
|
|
}
|
|
|
|
static int firstPress(void *p, double x, double y) {
|
|
NSWindow *w = (NSWindow *)p;
|
|
if (w.isKeyWindow) return -1;
|
|
NSPoint at = NSMakePoint(x, NSHeight(w.frame) - y);
|
|
Method m = class_getInstanceMethod([w class], @selector(performWindowDragWithEvent:));
|
|
IMP drag = method_setImplementation(m, (IMP)countDrag);
|
|
drags = 0;
|
|
// The release is queued first: a loop the press begins (a resize) takes
|
|
// it and ends.
|
|
[NSApp postEvent:press(w, NSEventTypeLeftMouseUp, at) atStart:NO];
|
|
[NSApp sendEvent:press(w, NSEventTypeLeftMouseDown, at)];
|
|
NSEvent *up = [NSApp nextEventMatchingMask:NSEventMaskLeftMouseUp untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
|
|
if (up != nil) [NSApp sendEvent:up];
|
|
method_setImplementation(m, drag);
|
|
return drags;
|
|
}
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"strings"
|
|
"unsafe"
|
|
)
|
|
|
|
// Shape is how a window's frame looks, in points.
|
|
type Shape struct {
|
|
Radius float64 // of its corners; -1 when AppKit doesn't say
|
|
Lights [3][2]float64 // each traffic light's middle: x from the window's left, y from its top
|
|
Titlebar float64 // the title bar's height
|
|
Toolbar bool // whether it has a toolbar
|
|
Menu []string // the title bar's right-click menu (TitlebarMenu)
|
|
}
|
|
|
|
// Of measures the NSWindow w. Call it on the main thread.
|
|
func Of(w unsafe.Pointer) Shape {
|
|
var out [7]C.double
|
|
C.lights(w, &out[0])
|
|
s := Shape{Radius: float64(C.cornerRadius(w)), Titlebar: float64(out[6]), Toolbar: C.hasToolbar(w) != 0, Menu: TitlebarMenu(w)}
|
|
for i := range 3 {
|
|
s.Lights[i] = [2]float64{float64(out[2*i]), float64(out[2*i+1])}
|
|
}
|
|
return s
|
|
}
|
|
|
|
// TitlebarMenu is the menu a right-click on w's title bar, by its traffic
|
|
// lights, gets from the window itself (where a right-click the page
|
|
// doesn't take ends): its items' titles, "-" for a separator; none when
|
|
// there is no menu. Call it on the main thread.
|
|
func TitlebarMenu(w unsafe.Pointer) []string {
|
|
p := C.titlebarMenu(w)
|
|
defer C.free(unsafe.Pointer(p))
|
|
if s := C.GoString(p); s != "" {
|
|
return strings.Split(s, "\n")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// BeginSheet shows a sheet height points tall on w. Call it on the main
|
|
// thread, and EndSheet once done.
|
|
func BeginSheet(w unsafe.Pointer, height float64) unsafe.Pointer {
|
|
return C.beginSheet(w, C.double(height))
|
|
}
|
|
|
|
// SheetTop is how far below w's top its sheet begins.
|
|
func SheetTop(w, sheet unsafe.Pointer) float64 { return float64(C.sheetTop(w, sheet)) }
|
|
|
|
// EndSheet takes the sheet off w.
|
|
func EndSheet(w, sheet unsafe.Pointer) { C.endSheet(w, sheet) }
|
|
|
|
// FirstPress presses w once, and lets go, x points from its left and y from
|
|
// its top, as a press comes while another app is in front: w isn't the key
|
|
// window. The press is sent within this process, as AppKit sends the window
|
|
// server's, and tells how many window drags it began (counted, not begun),
|
|
// or -1 if w is the key window. Call it on the main thread.
|
|
func FirstPress(w unsafe.Pointer, x, y float64) int {
|
|
return int(C.firstPress(w, C.double(x), C.double(y)))
|
|
}
|
|
|
|
// FlipIncreaseContrast turns Increase contrast, for this process alone, to
|
|
// the opposite of what System Settings has, and back on the next call:
|
|
// AppKit is told the display options changed, and finds the setting
|
|
// changed, so every window that follows the system takes a new
|
|
// appearance. Call it on the main thread.
|
|
func FlipIncreaseContrast() { C.flipIncreaseContrast() }
|