From 94a243a13ae3b78bd1a567995f2e5833ed3bf7f4 Mon Sep 17 00:00:00 2001 From: Brian Pham Date: Wed, 10 Jun 2026 09:45:31 -0700 Subject: [PATCH] osx: anchor new windows to the primary screen origin WindowBaseImpl::CreateNSWindow anchors the new NSWindow's content rect to the primary screen's frame.origin. The primary screen is at Cocoa (0,0) on a healthy Mac, but can be offset (often negative) while the display server is mid-reconfiguration -- e.g. CGMainDisplayID() briefly returns 0 after wake-from-sleep -- or on multi-monitor layouts where the primary is not at the origin. Anchoring keeps the window on a real screen so it is positioned on-screen and its flipped Position lands inside a Screen.Bounds. See #18895. Co-authored-by: Cursor --- .../Avalonia.Native/src/OSX/WindowBaseImpl.mm | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm index 2a1de62b05..4124381be5 100644 --- a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm @@ -432,11 +432,24 @@ void WindowBaseImpl::CleanNSWindow() { } void WindowBaseImpl::CreateNSWindow(bool usePanel) { + // Anchor the content rect to the primary screen's origin. The primary screen is + // at Cocoa (0,0) on a healthy Mac, but can be offset (often negative) while the + // display server is mid-reconfiguration -- e.g. CGMainDisplayID() briefly returns + // 0 after wake-from-sleep -- or on multi-monitor layouts. Anchoring keeps the + // window on a real screen, so it is positioned on-screen and its flipped Position + // lands inside a Screen.Bounds. See https://github.com/AvaloniaUI/Avalonia/issues/18895 + NSPoint origin = NSZeroPoint; + NSArray* screens = [NSScreen screens]; + if (screens.count > 0) + origin = screens.firstObject.frame.origin; + + NSRect contentRect = NSRect { origin.x, origin.y, lastSize }; + if (usePanel) { - Window = [[AvnPanel alloc] initWithParent:this contentRect:NSRect{0, 0, lastSize} styleMask:NSWindowStyleMaskBorderless]; + Window = [[AvnPanel alloc] initWithParent:this contentRect:contentRect styleMask:NSWindowStyleMaskBorderless]; [Window setHidesOnDeactivate:false]; } else { - Window = [[AvnWindow alloc] initWithParent:this contentRect:NSRect{0, 0, lastSize} styleMask:NSWindowStyleMaskBorderless]; + Window = [[AvnWindow alloc] initWithParent:this contentRect:contentRect styleMask:NSWindowStyleMaskBorderless]; } }