Skip to content

Commit 5ca89b3

Browse files
authored
Show Windows 11 Snap Layouts flyout on the maximize button (#4976)
1 parent 4bcd5d0 commit 5ca89b3

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

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

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ public partial class MainWindow : Window
5151
private const uint WM_STYLECHANGING = 0x007C;
5252
private const uint WM_GETMINMAXINFO = 0x0024;
5353
private const uint WM_NCCALCSIZE = 0x0083;
54+
// Snap Layouts: DWM shows the maximize flyout only when WM_NCHITTEST reports HTMAXBUTTON
55+
// over the button. That routes the button's input through non-client messages, so the
56+
// click and hover/press visuals are driven from the WndProc below (the Avalonia Button
57+
// no longer sees pointer events there).
58+
private const uint WM_NCHITTEST = 0x0084;
59+
private const uint WM_NCMOUSEMOVE = 0x00A0;
60+
private const uint WM_NCMOUSELEAVE = 0x02A2;
61+
private const uint WM_NCLBUTTONDOWN = 0x00A1;
62+
private const uint WM_NCLBUTTONUP = 0x00A2;
63+
private const uint WM_NCLBUTTONDBLCLK = 0x00A3;
64+
private const uint WM_MOUSEMOVE = 0x0200;
65+
private const uint WM_SYSCOMMAND = 0x0112;
66+
private const nint SC_MAXIMIZE = 0xF030;
67+
private const nint SC_RESTORE = 0xF120;
68+
private const int HTMAXBUTTON = 9;
69+
private const uint TME_LEAVE = 0x0002;
70+
private const uint TME_NONCLIENT = 0x0010;
5471
private const int GWL_STYLE = -16;
5572
private const uint WS_CAPTION = 0x00C00000;
5673
private const uint WS_THICKFRAME = 0x00040000;
@@ -71,6 +88,8 @@ public partial class MainWindow : Window
7188
private const int DWMWA_COLOR_NONE = unchecked((int)0xFFFFFFFE);
7289

7390
private bool _focusSidebarSelectionOnNextPageChange;
91+
private bool _maxButtonHover;
92+
private bool _maxButtonPressed;
7493
private TrayService? _trayService;
7594
private bool _allowClose;
7695
private int _isQuitting;
@@ -698,6 +717,53 @@ private void UpdateMaximizeButtonState(bool isMaximized)
698717
CoreTools.Translate(isMaximized ? "Restore" : "Maximize"));
699718
}
700719

720+
// True when the WM_NCHITTEST screen point (physical px in lParam) falls within the maximize
721+
// button's bounds — the area for which we claim HTMAXBUTTON so Snap Layouts can attach.
722+
private bool HitTestMaximizeButton(nint lParam)
723+
{
724+
if (!MaximizeButton.IsVisible)
725+
return false;
726+
try
727+
{
728+
int x = unchecked((short)(lParam.ToInt64() & 0xFFFF));
729+
int y = unchecked((short)((lParam.ToInt64() >> 16) & 0xFFFF));
730+
PixelPoint topLeft = MaximizeButton.PointToScreen(new Point(0, 0));
731+
PixelPoint bottomRight = MaximizeButton.PointToScreen(
732+
new Point(MaximizeButton.Bounds.Width, MaximizeButton.Bounds.Height));
733+
return x >= topLeft.X && x < bottomRight.X && y >= topLeft.Y && y < bottomRight.Y;
734+
}
735+
catch
736+
{
737+
return false;
738+
}
739+
}
740+
741+
// Emulates the button's pointer-over/pressed fill (lost once input is non-client) using the
742+
// same Fluent brushes the neighbouring min/close buttons use, so the row stays consistent.
743+
private void SetMaximizeButtonVisual(bool hover, bool pressed)
744+
{
745+
_maxButtonHover = hover;
746+
_maxButtonPressed = pressed;
747+
748+
string? key = pressed ? "ButtonBackgroundPressed" : hover ? "ButtonBackgroundPointerOver" : null;
749+
if (key is not null && this.TryFindResource(key, ActualThemeVariant, out var res) && res is IBrush brush)
750+
MaximizeButton.Background = brush;
751+
else
752+
MaximizeButton.Background = Brushes.Transparent;
753+
}
754+
755+
private static void TrackNonClientMouseLeave(nint hWnd)
756+
{
757+
var tme = new NativeMethods.TRACKMOUSEEVENT
758+
{
759+
cbSize = (uint)Marshal.SizeOf<NativeMethods.TRACKMOUSEEVENT>(),
760+
dwFlags = TME_LEAVE | TME_NONCLIENT,
761+
hwndTrack = hWnd,
762+
dwHoverTime = 0,
763+
};
764+
NativeMethods.TrackMouseEvent(ref tme);
765+
}
766+
701767
// Applies the Windows 11 Mica look when it's actually usable (Win11 + transparency on):
702768
// a transparent window so the backdrop shows, native rounded corners, and no accent
703769
// border (it reads as out of place on the large main window). Otherwise the window keeps
@@ -735,6 +801,60 @@ private void SetupMicaAndAccentBorder()
735801

736802
private static nint OnWindowsWndProc(nint hWnd, uint msg, nint wParam, nint lParam, ref bool handled)
737803
{
804+
// ── Snap Layouts: report HTMAXBUTTON over the custom maximize button so Win11 shows the
805+
// layout flyout, and emulate hover/press/click since input now arrives as NC messages. ──
806+
if (Instance is { } self && self.WindowButtons.IsVisible)
807+
{
808+
switch (msg)
809+
{
810+
case WM_NCHITTEST:
811+
if (self.HitTestMaximizeButton(lParam))
812+
{
813+
handled = true;
814+
return HTMAXBUTTON;
815+
}
816+
break;
817+
818+
case WM_NCMOUSEMOVE:
819+
if (wParam == HTMAXBUTTON)
820+
{
821+
self.SetMaximizeButtonVisual(hover: true, self._maxButtonPressed);
822+
TrackNonClientMouseLeave(hWnd);
823+
handled = true;
824+
return 0;
825+
}
826+
self.SetMaximizeButtonVisual(hover: false, pressed: false);
827+
break;
828+
829+
case WM_MOUSEMOVE:
830+
if (self._maxButtonHover)
831+
self.SetMaximizeButtonVisual(hover: false, pressed: false);
832+
break;
833+
834+
case WM_NCMOUSELEAVE:
835+
self.SetMaximizeButtonVisual(hover: false, pressed: false);
836+
break;
837+
838+
case WM_NCLBUTTONDOWN when wParam == HTMAXBUTTON:
839+
self.SetMaximizeButtonVisual(hover: true, pressed: true);
840+
handled = true;
841+
return 0;
842+
843+
case WM_NCLBUTTONUP when wParam == HTMAXBUTTON:
844+
bool wasPressed = self._maxButtonPressed;
845+
self.SetMaximizeButtonVisual(hover: true, pressed: false);
846+
if (wasPressed)
847+
NativeMethods.PostMessage(hWnd, WM_SYSCOMMAND,
848+
self.WindowState == WindowState.Maximized ? SC_RESTORE : SC_MAXIMIZE, 0);
849+
handled = true;
850+
return 0;
851+
852+
case WM_NCLBUTTONDBLCLK when wParam == HTMAXBUTTON:
853+
handled = true; // the down/up pair already toggled; swallow the follow-up dblclk
854+
return 0;
855+
}
856+
}
857+
738858
// Force client = full window rect. Avalonia's ExtendClientArea handler only overrides
739859
// the top inset, leaving the WS_THICKFRAME left/right/bottom resize border as glass.
740860
if (msg == WM_NCCALCSIZE && wParam.ToInt64() != 0)
@@ -833,6 +953,14 @@ private static class NativeMethods
833953
[DllImport("user32.dll")]
834954
public static extern uint GetDpiForWindow(nint hWnd);
835955

956+
[DllImport("user32.dll", SetLastError = true)]
957+
[return: MarshalAs(UnmanagedType.Bool)]
958+
public static extern bool PostMessage(nint hWnd, uint Msg, nint wParam, nint lParam);
959+
960+
[DllImport("user32.dll", SetLastError = true)]
961+
[return: MarshalAs(UnmanagedType.Bool)]
962+
public static extern bool TrackMouseEvent(ref TRACKMOUSEEVENT lpEventTrack);
963+
836964
[DllImport("user32.dll", SetLastError = true)]
837965
[return: MarshalAs(UnmanagedType.Bool)]
838966
public static extern bool AdjustWindowRectExForDpi(ref RECT lpRect, uint dwStyle, [MarshalAs(UnmanagedType.Bool)] bool bMenu, uint dwExStyle, uint dpi);
@@ -861,6 +989,15 @@ public struct POINT
861989
public int Y;
862990
}
863991

992+
[StructLayout(LayoutKind.Sequential)]
993+
public struct TRACKMOUSEEVENT
994+
{
995+
public uint cbSize;
996+
public uint dwFlags;
997+
public nint hwndTrack;
998+
public uint dwHoverTime;
999+
}
1000+
8641001
[StructLayout(LayoutKind.Sequential)]
8651002
public struct MINMAXINFO
8661003
{

0 commit comments

Comments
 (0)