diff --git a/src/Avalonia.Native/WindowImplBase.cs b/src/Avalonia.Native/WindowImplBase.cs index 852317a793..eab3a2e3f9 100644 --- a/src/Avalonia.Native/WindowImplBase.cs +++ b/src/Avalonia.Native/WindowImplBase.cs @@ -51,11 +51,30 @@ namespace Avalonia.Native base.Init(handle); + int defaultWidth = 0, defaultHeight = 0; + + // AppKit can transiently report [NSScreen screens] as empty -- or report screens + // whose bounds don't contain the last-known window position -- during a CGS + // demote/promote race after wake-from-sleep, headless boots, or Spaces + // transitions. FirstOrDefault keeps window construction resilient through that + // window: the window falls back to a default size and is repositioned on the next + // ScreenChanged event once the display server settles, mirroring X11Window. + // See https://github.com/AvaloniaUI/Avalonia/issues/18895 var monitor = this.TryGetFeature()!.AllScreens .OrderBy(x => x.Scaling) - .First(m => m.Bounds.Contains(Position)); + .FirstOrDefault(m => m.Bounds.Contains(Position)); - Resize(new Size(monitor.WorkingArea.Width * 0.75d, monitor.WorkingArea.Height * 0.7d), WindowResizeReason.Layout); + if (monitor != null) + { + // Emulate Windows 7+ default window size behavior. + defaultWidth = (int)(monitor.WorkingArea.Width * 0.75d); + defaultHeight = (int)(monitor.WorkingArea.Height * 0.7d); + } + + defaultWidth = Math.Max(defaultWidth, 300); + defaultHeight = Math.Max(defaultHeight, 200); + + Resize(new Size(defaultWidth, defaultHeight), WindowResizeReason.Layout); } public void Activate()