Skip to content

Commit fd1cb3b

Browse files
Inset the macOS window buttons inside the glass sidebar (#5529)
1 parent db4273d commit fd1cb3b

2 files changed

Lines changed: 187 additions & 7 deletions

File tree

‎src/UniGetUI.Avalonia/Infrastructure/MacGlassWindowHelper.cs‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ internal static partial class MacGlassWindowHelper
1919

2020
private const string ObjCRuntime = "/usr/lib/libobjc.A.dylib";
2121

22+
// NSWindowButton values of the close, minimise and zoom buttons.
23+
private const long NSWindowCloseButton = 0;
24+
private const long NSWindowMiniaturizeButton = 1;
25+
private const long NSWindowZoomButton = 2;
26+
2227
// AppKit NSVisualEffectMaterial / NSVisualEffectBlendingMode / NSVisualEffectState values.
2328
private const long NSVisualEffectMaterialSidebar = 7;
2429
private const long NSVisualEffectMaterialMenu = 5;
@@ -256,6 +261,132 @@ private static void StyleBehindWindowEffectViews(nint nsWindow, long material, d
256261
}
257262
}
258263

264+
/// <summary>The zoom (green) button's frame in the window, in DIPs from the top-left corner, as macOS
265+
/// actually placed it; null when it can't be read. The title-bar row aligns to it.</summary>
266+
public static Rect? GetZoomButtonFrame(Window window)
267+
{
268+
if (!OperatingSystem.IsMacOS())
269+
return null;
270+
271+
try
272+
{
273+
nint nsWindow = GetNSWindow(window);
274+
if (nsWindow == 0)
275+
return null;
276+
277+
nint zoom = objc_msgSend_IntPtr_Long(nsWindow, Sel.StandardWindowButton, NSWindowZoomButton);
278+
nint contentView = objc_msgSend_IntPtr(nsWindow, Sel.ContentView);
279+
if (zoom == 0 || contentView == 0)
280+
return null;
281+
282+
NSRect frame = ConvertRect(zoom, GetRect(zoom, Sel.Bounds), contentView);
283+
NSRect content = GetRect(contentView, Sel.Bounds);
284+
if (frame.Width <= 0 || content.Height <= 0)
285+
return null;
286+
287+
// AppKit views measure from the bottom-left unless flipped; Avalonia measures from the top-left.
288+
double top = objc_msgSend_Bool(contentView, Sel.IsFlipped)
289+
? frame.Y
290+
: content.Height - frame.Y - frame.Height;
291+
return new Rect(frame.X, top, frame.Width, frame.Height);
292+
}
293+
catch (Exception ex)
294+
{
295+
Logger.Warn("Could not read the macOS window buttons' position; using the default title-bar inset");
296+
Logger.Warn(ex);
297+
return null;
298+
}
299+
}
300+
301+
/// <summary>Moves the close/minimise/zoom buttons so the first one starts <paramref name="left"/> DIPs from
302+
/// the window's left edge and their centre line sits <paramref name="centerY"/> DIPs from its top. macOS
303+
/// lays them out again on resize, zoom and full screen, so call it again after each. Inside the floating
304+
/// glass sidebar they need the inset System Settings gives them, not the bare title-bar position.</summary>
305+
public static void SetWindowButtonsPosition(Window window, double left, double centerY)
306+
{
307+
if (!OperatingSystem.IsMacOS())
308+
return;
309+
310+
try
311+
{
312+
nint nsWindow = GetNSWindow(window);
313+
if (nsWindow == 0)
314+
return;
315+
316+
nint close = objc_msgSend_IntPtr_Long(nsWindow, Sel.StandardWindowButton, NSWindowCloseButton);
317+
nint miniaturize = objc_msgSend_IntPtr_Long(nsWindow, Sel.StandardWindowButton, NSWindowMiniaturizeButton);
318+
nint zoom = objc_msgSend_IntPtr_Long(nsWindow, Sel.StandardWindowButton, NSWindowZoomButton);
319+
if (close == 0 || miniaturize == 0 || zoom == 0)
320+
return;
321+
322+
// The buttons live in the title-bar view, inside NSTitlebarContainerView; the container is
323+
// only as tall as the native title bar, so it grows to hold the buttons at the new centre line.
324+
nint titlebarView = objc_msgSend_IntPtr(close, Sel.Superview);
325+
nint container = titlebarView == 0 ? 0 : objc_msgSend_IntPtr(titlebarView, Sel.Superview);
326+
nint contentView = objc_msgSend_IntPtr(nsWindow, Sel.ContentView);
327+
if (container == 0 || contentView == 0)
328+
return;
329+
330+
NSRect closeFrame = GetRect(close, Sel.Frame);
331+
NSRect miniaturizeFrame = GetRect(miniaturize, Sel.Frame);
332+
double spacing = miniaturizeFrame.X - closeFrame.X;
333+
double height = Math.Max(closeFrame.Height, 2 * centerY);
334+
NSRect content = GetRect(contentView, Sel.Bounds);
335+
NSRect current = GetRect(container, Sel.Frame);
336+
337+
SetRect(container, Sel.SetFrame, new NSRect(current.X, content.Height - height, current.Width, height));
338+
SetRect(titlebarView, Sel.SetFrame, new NSRect(0, 0, current.Width, height));
339+
340+
// Title-bar view coordinates start at the bottom-left; centre the buttons on the requested line.
341+
double y = height - centerY - (closeFrame.Height / 2);
342+
nint[] buttons = [close, miniaturize, zoom];
343+
for (int i = 0; i < buttons.Length; i++)
344+
{
345+
objc_msgSend_Void_Point(buttons[i], Sel.SetFrameOrigin, left + (i * spacing), y);
346+
}
347+
}
348+
catch (Exception ex)
349+
{
350+
Logger.Warn("Could not move the macOS window buttons; keeping their default position");
351+
Logger.Warn(ex);
352+
}
353+
}
354+
355+
private static void SetRect(nint receiver, nint selector, NSRect rect)
356+
=> objc_msgSend_Void_Rect(receiver, selector, rect);
357+
358+
// An NSRect comes back in registers on arm64 and through a hidden pointer (objc_msgSend_stret) on x64.
359+
private static NSRect GetRect(nint receiver, nint selector)
360+
{
361+
if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
362+
{
363+
objc_msgSend_stret_Rect(out NSRect result, receiver, selector);
364+
return result;
365+
}
366+
367+
return objc_msgSend_Rect(receiver, selector);
368+
}
369+
370+
private static NSRect ConvertRect(nint view, NSRect rect, nint toView)
371+
{
372+
if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
373+
{
374+
objc_msgSend_stret_Rect_Rect_IntPtr(out NSRect result, view, Sel.ConvertRectToView, rect, toView);
375+
return result;
376+
}
377+
378+
return objc_msgSend_Rect_Rect_IntPtr(view, Sel.ConvertRectToView, rect, toView);
379+
}
380+
381+
[StructLayout(LayoutKind.Sequential)]
382+
private readonly struct NSRect(double x, double y, double width, double height)
383+
{
384+
public readonly double X = x;
385+
public readonly double Y = y;
386+
public readonly double Width = width;
387+
public readonly double Height = height;
388+
}
389+
259390
private static nint GetNSWindow(TopLevel window)
260391
{
261392
if (window.TryGetPlatformHandle() is not { Handle: not 0 } handle)
@@ -299,20 +430,28 @@ private static class Sel
299430
public static readonly nint AccessibilityDisplayShouldIncreaseContrast = sel_registerName("accessibilityDisplayShouldIncreaseContrast");
300431
public static readonly nint AccessibilityDisplayShouldReduceTransparency = sel_registerName("accessibilityDisplayShouldReduceTransparency");
301432
public static readonly nint BlendingMode = sel_registerName("blendingMode");
433+
public static readonly nint Bounds = sel_registerName("bounds");
302434
public static readonly nint ContentView = sel_registerName("contentView");
435+
public static readonly nint ConvertRectToView = sel_registerName("convertRect:toView:");
436+
public static readonly nint Frame = sel_registerName("frame");
303437
public static readonly nint Count = sel_registerName("count");
304438
public static readonly nint InvalidateShadow = sel_registerName("invalidateShadow");
439+
public static readonly nint IsFlipped = sel_registerName("isFlipped");
305440
public static readonly nint IsKindOfClass = sel_registerName("isKindOfClass:");
306441
public static readonly nint Layer = sel_registerName("layer");
307442
public static readonly nint ObjectAtIndex = sel_registerName("objectAtIndex:");
308443
public static readonly nint SetCornerRadius = sel_registerName("setCornerRadius:");
444+
public static readonly nint SetFrame = sel_registerName("setFrame:");
445+
public static readonly nint SetFrameOrigin = sel_registerName("setFrameOrigin:");
309446
public static readonly nint SetHasShadow = sel_registerName("setHasShadow:");
310447
public static readonly nint SetMasksToBounds = sel_registerName("setMasksToBounds:");
311448
public static readonly nint SetMaterial = sel_registerName("setMaterial:");
312449
public static readonly nint SetState = sel_registerName("setState:");
313450
public static readonly nint SetWantsLayer = sel_registerName("setWantsLayer:");
314451
public static readonly nint SharedWorkspace = sel_registerName("sharedWorkspace");
452+
public static readonly nint StandardWindowButton = sel_registerName("standardWindowButton:");
315453
public static readonly nint Subviews = sel_registerName("subviews");
454+
public static readonly nint Superview = sel_registerName("superview");
316455
public static readonly nint Window = sel_registerName("window");
317456
}
318457

@@ -335,6 +474,25 @@ private static class Sel
335474
[return: MarshalAs(UnmanagedType.U1)]
336475
private static partial bool objc_msgSend_Bool(nint receiver, nint selector);
337476

477+
[LibraryImport(ObjCRuntime, EntryPoint = "objc_msgSend")]
478+
private static partial NSRect objc_msgSend_Rect(nint receiver, nint selector);
479+
480+
[LibraryImport(ObjCRuntime, EntryPoint = "objc_msgSend")]
481+
private static partial NSRect objc_msgSend_Rect_Rect_IntPtr(nint receiver, nint selector, NSRect rect, nint view);
482+
483+
[LibraryImport(ObjCRuntime, EntryPoint = "objc_msgSend")]
484+
private static partial void objc_msgSend_Void_Rect(nint receiver, nint selector, NSRect rect);
485+
486+
// NSPoint is two doubles, passed in registers on both arm64 and x64.
487+
[LibraryImport(ObjCRuntime, EntryPoint = "objc_msgSend")]
488+
private static partial void objc_msgSend_Void_Point(nint receiver, nint selector, double x, double y);
489+
490+
[LibraryImport(ObjCRuntime, EntryPoint = "objc_msgSend_stret")]
491+
private static partial void objc_msgSend_stret_Rect(out NSRect result, nint receiver, nint selector);
492+
493+
[LibraryImport(ObjCRuntime, EntryPoint = "objc_msgSend_stret")]
494+
private static partial void objc_msgSend_stret_Rect_Rect_IntPtr(out NSRect result, nint receiver, nint selector, NSRect rect, nint view);
495+
338496
[LibraryImport(ObjCRuntime, EntryPoint = "objc_msgSend")]
339497
[return: MarshalAs(UnmanagedType.U1)]
340498
private static partial bool objc_msgSend_Bool_IntPtr(nint receiver, nint selector, nint arg);

‎src/UniGetUI.Avalonia/Views/MainWindow.axaml.cs‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public partial class MainWindow : Window
7878
// gap — enough for the row to read as separate from the window buttons rather than crowding them.
7979
private const double MAC_TRAFFIC_LIGHT_INSET = 76;
8080

81+
// Space between the glass sidebar panel's edge and the close (red) button.
82+
private const double MAC_TRAFFIC_LIGHT_PANEL_PADDING = 12;
83+
84+
// Space between the zoom (green) button and the first title-bar control, when its frame can be read.
85+
private const double MAC_TRAFFIC_LIGHT_GAP = 14;
86+
87+
// Last zoom button frame read on macOS; logged when it changes, to diagnose title-bar alignment.
88+
private Rect? _lastMacZoomButtonFrame;
89+
8190
// Last known macOS title-bar height; seeded with the requested height for the window's first layout
8291
// pass and while fullscreen reports a collapsed decoration margin.
8392
private double _macTitleBarHeight = 44;
@@ -643,19 +652,32 @@ void ApplyMacTitleBarLayout()
643652
}
644653
else
645654
{
646-
// Pin the row to the traffic lights: top-aligned, offset so its centre line
647-
// matches theirs, and inset far enough to clear the close/minimise/zoom cluster.
655+
// Inset the window buttons inside the floating glass sidebar, on the bar's centre line (the
656+
// search field's), as System Settings does; macOS resets them on every layout pass.
657+
MacGlassWindowHelper.SetWindowButtonsPosition(
658+
this, MAC_GLASS_INSET + MAC_TRAFFIC_LIGHT_PANEL_PADDING, _macTitleBarHeight / 2);
659+
660+
// Pin the row to the traffic lights where macOS actually placed them: their position
661+
// changes with the macOS version and the window state, so the constants are only a fallback.
662+
Rect? zoom = MacGlassWindowHelper.GetZoomButtonFrame(this);
663+
if (zoom != _lastMacZoomButtonFrame)
664+
{
665+
_lastMacZoomButtonFrame = zoom;
666+
Logger.Info($"macOS title bar: zoom button at {zoom?.ToString() ?? "unknown"}, window {WindowState}");
667+
}
668+
669+
double inset = zoom is { } z ? z.Right + MAC_TRAFFIC_LIGHT_GAP : MAC_TRAFFIC_LIGHT_INSET;
670+
double centerY = zoom is { } c ? c.Center.Y : MAC_TRAFFIC_LIGHT_CENTER_Y;
648671
HamburgerPanel.VerticalAlignment = VerticalAlignment.Top;
649-
HamburgerPanel.Margin = new Thickness(
650-
MAC_TRAFFIC_LIGHT_INSET,
651-
MAC_TRAFFIC_LIGHT_CENTER_Y - (MAC_TITLE_BAR_CONTROL_SIZE / 2),
652-
8,
653-
0);
672+
HamburgerPanel.Margin = new Thickness(inset, centerY - (MAC_TITLE_BAR_CONTROL_SIZE / 2), 8, 0);
654673
}
655674
}
656675

657676
this.GetObservable(WindowStateProperty).SubscribeValue(_ => ApplyMacTitleBarLayout());
658677
this.GetObservable(WindowDecorationMarginProperty).SubscribeValue(_ => ApplyMacTitleBarLayout());
678+
// macOS lays the window buttons out again on resize and zoom; follow them.
679+
this.GetObservable(ClientSizeProperty).SubscribeValue(_ => ApplyMacTitleBarLayout());
680+
Opened += (_, _) => Dispatcher.UIThread.Post(ApplyMacTitleBarLayout, DispatcherPriority.Background);
659681
NavRail.GetObservable(IsVisibleProperty).SubscribeValue(_ => ApplyMacSidebarLayout());
660682
NavDock.GetObservable(IsVisibleProperty).SubscribeValue(_ => ApplyMacSidebarLayout());
661683
SidebarGlass.GetObservable(BoundsProperty).SubscribeValue(_ => UpdateMacWindowSurfaceClip());

0 commit comments

Comments
 (0)