Skip to content

Commit 5602c96

Browse files
RBridCopilot
andauthored
Fix tray popup height measurement (#826)
Measure the tray popup against its final client width and apply pixel-exact window sizing so wrapped content determines height deterministically. Co-authored-by: Copilot <[email protected]>
1 parent 84d2e6a commit 5602c96

5 files changed

Lines changed: 120 additions & 33 deletions

File tree

‎src/OpenClaw.Shared/MenuSizingHelper.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public static int ConvertPixelsToViewUnits(int pixels, uint dpi)
1515
return Math.Max(1, (int)Math.Floor(pixels * 96.0 / dpi));
1616
}
1717

18+
public static int ConvertViewUnitsToPixels(double viewUnits, uint dpi)
19+
{
20+
if (!double.IsFinite(viewUnits) || viewUnits <= 0) return 0;
21+
if (dpi == 0) dpi = 96;
22+
23+
return Math.Max(1, (int)Math.Ceiling(viewUnits * dpi / 96.0));
24+
}
25+
1826
public static bool HasDpiOrScaleChanged(uint previousDpi, double previousRasterizationScale, uint currentDpi, double currentRasterizationScale)
1927
{
2028
previousDpi = NormalizeDpi(previousDpi);

‎src/OpenClaw.Tray.WinUI/Windows/TrayMenuWindow.xaml.cs‎

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public sealed partial class TrayMenuWindow : WindowEx
2323
{
2424
private const int MenuWidthViewUnits = 320;
2525
private const int SubmenuWidthViewUnits = 280;
26+
private const int MinimumMenuHeightViewUnits = 100;
2627

2728
#region Win32 Imports
2829
[DllImport("user32.dll")]
@@ -59,6 +60,9 @@ public sealed partial class TrayMenuWindow : WindowEx
5960
[DllImport("user32.dll")]
6061
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
6162

63+
[DllImport("user32.dll")]
64+
private static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
65+
6266
[DllImport("user32.dll")]
6367
private static extern IntPtr GetForegroundWindow();
6468

@@ -109,7 +113,8 @@ private enum MonitorDpiType
109113

110114
public event EventHandler<string>? MenuItemClicked;
111115

112-
private int _menuHeight = 400;
116+
private int _menuWidthPx = 320;
117+
private int _menuHeightPx = 400;
113118
private int _itemCount = 0;
114119
private int _separatorCount = 0;
115120
private int _headerCount = 0;
@@ -260,8 +265,8 @@ public void ShowAtCursor()
260265
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
261266
var dpi = GetEffectiveMonitorDpi(hMonitor, hwnd);
262267
SizeToContent(workArea.Bottom - workArea.Top, dpi);
263-
var menuWidthPx = ConvertViewUnitsToPixels(MenuWidthViewUnits, dpi);
264-
var menuHeightPx = ConvertViewUnitsToPixels(_menuHeight, dpi);
268+
var menuWidthPx = _menuWidthPx;
269+
var menuHeightPx = _menuHeightPx;
265270

266271
const int margin = 8;
267272

@@ -313,8 +318,8 @@ private void ShowAdjacentTo(FrameworkElement parentElement)
313318
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
314319
var dpi = GetEffectiveMonitorDpi(hMonitor, hwnd);
315320
SizeToContent(monitorInfo.rcWork.Bottom - monitorInfo.rcWork.Top, dpi, SubmenuWidthViewUnits);
316-
var submenuWidthPx = ConvertViewUnitsToPixels(SubmenuWidthViewUnits, dpi);
317-
var submenuHeightPx = ConvertViewUnitsToPixels(_menuHeight, dpi);
321+
var submenuWidthPx = _menuWidthPx;
322+
var submenuHeightPx = _menuHeightPx;
318323

319324
const int overlap = 2;
320325
const int margin = 8;
@@ -324,8 +329,8 @@ private void ShowAdjacentTo(FrameworkElement parentElement)
324329
if (submenuHeightPx > maxSubmenuHeightPx)
325330
{
326331
submenuHeightPx = maxSubmenuHeightPx;
327-
_menuHeight = MenuSizingHelper.ConvertPixelsToViewUnits(submenuHeightPx, dpi);
328-
this.SetWindowSize(SubmenuWidthViewUnits, _menuHeight);
332+
_menuHeightPx = submenuHeightPx;
333+
ResizeWindowToPixelSize(submenuWidthPx, submenuHeightPx);
329334
}
330335

331336
var roomRight = workArea.Right - parentRect.Right;
@@ -952,24 +957,58 @@ private void SizeToContent(int workAreaHeightPx, uint dpi)
952957

953958
private void SizeToContent(int workAreaHeightPx, uint dpi, int widthViewUnits)
954959
{
960+
dpi = dpi == 0 ? 96 : dpi;
955961
PrepareLayoutForMeasurement(dpi);
956962

957-
// Measure the actual content size instead of estimating
958-
MenuPanel.Measure(new global::Windows.Foundation.Size(widthViewUnits, double.PositiveInfinity));
959-
var desiredHeight = MenuPanel.DesiredSize.Height;
960-
961-
// Add border chrome (1px border top+bottom = 2px, plus small rounding buffer)
962-
var contentHeight = (int)Math.Ceiling(desiredHeight) + 2;
963-
_menuHeight = Math.Max(contentHeight, 100);
963+
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
964+
var windowWidthPx = Math.Max(1, MenuSizingHelper.ConvertViewUnitsToPixels(widthViewUnits, dpi));
965+
var nonClientSize = GetNonClientSize(hwnd);
966+
var clientWidthPx = Math.Max(1, windowWidthPx - nonClientSize.Width);
967+
var clientWidthViewUnits = Math.Max(1, MenuSizingHelper.ConvertPixelsToViewUnits(clientWidthPx, dpi));
968+
969+
// Measure the full XAML root at the width it will actually receive.
970+
// This lets the platform account for Border, ScrollViewer, padding, and
971+
// any text wrapping caused by the final client width.
972+
RootGrid.Measure(new global::Windows.Foundation.Size(clientWidthViewUnits, double.PositiveInfinity));
973+
974+
var desiredClientHeightPx = MenuSizingHelper.ConvertViewUnitsToPixels(RootGrid.DesiredSize.Height, dpi);
975+
var desiredWindowHeightPx = Math.Max(1, desiredClientHeightPx + nonClientSize.Height);
976+
var minimumHeightPx = MenuSizingHelper.ConvertViewUnitsToPixels(MinimumMenuHeightViewUnits, dpi);
977+
978+
_menuWidthPx = windowWidthPx;
979+
_menuHeightPx = MenuSizingHelper.CalculateWindowHeight(
980+
desiredWindowHeightPx,
981+
workAreaHeightPx,
982+
minimumHeightPx);
983+
984+
ResizeWindowToPixelSize(_menuWidthPx, _menuHeightPx);
985+
ApplyRoundedWindowRegion();
986+
}
987+
988+
private void ResizeWindowToPixelSize(int widthPx, int heightPx)
989+
{
990+
AppWindow.Resize(new global::Windows.Graphics.SizeInt32(
991+
Math.Max(1, widthPx),
992+
Math.Max(1, heightPx)));
993+
}
964994

965-
if (workAreaHeightPx > 0)
995+
private static (int Width, int Height) GetNonClientSize(IntPtr hwnd)
996+
{
997+
if (hwnd == IntPtr.Zero ||
998+
!GetWindowRect(hwnd, out var windowRect) ||
999+
!GetClientRect(hwnd, out var clientRect))
9661000
{
967-
var workAreaHeight = MenuSizingHelper.ConvertPixelsToViewUnits(workAreaHeightPx, dpi);
968-
_menuHeight = MenuSizingHelper.CalculateWindowHeight(_menuHeight, workAreaHeight);
1001+
return (0, 0);
9691002
}
9701003

971-
this.SetWindowSize(widthViewUnits, _menuHeight);
972-
ApplyRoundedWindowRegion();
1004+
var windowWidth = windowRect.Right - windowRect.Left;
1005+
var windowHeight = windowRect.Bottom - windowRect.Top;
1006+
var clientWidth = clientRect.Right - clientRect.Left;
1007+
var clientHeight = clientRect.Bottom - clientRect.Top;
1008+
1009+
return (
1010+
Math.Max(0, windowWidth - clientWidth),
1011+
Math.Max(0, windowHeight - clientHeight));
9731012
}
9741013

9751014
private void PrepareLayoutForMeasurement(uint dpi)
@@ -1087,7 +1126,7 @@ private void ApplyRoundedWindowRegion()
10871126
return;
10881127

10891128
var dpi = GetDpiForWindow(hwnd);
1090-
var cornerDiameter = ConvertViewUnitsToPixels(16, dpi);
1129+
var cornerDiameter = MenuSizingHelper.ConvertViewUnitsToPixels(16, dpi);
10911130
var region = CreateRoundRectRgn(0, 0, width + 1, height + 1, cornerDiameter, cornerDiameter);
10921131
if (region == IntPtr.Zero)
10931132
return;
@@ -1169,8 +1208,6 @@ private void ShowCascadingFlyout(Button ownerButton, IReadOnlyList<TrayMenuFlyou
11691208
}
11701209
_activeFlyoutTag = ownerButton.Tag as string;
11711210

1172-
flyoutWindow.SizeToContent(SubmenuWidthViewUnits);
1173-
11741211
flyoutWindow.ShowAdjacentTo(ownerButton);
11751212
}
11761213

@@ -1227,17 +1264,6 @@ private bool TryGetElementScreenRect(FrameworkElement element, out RECT rect)
12271264
}
12281265
}
12291266

1230-
private static int ConvertViewUnitsToPixels(int viewUnits, uint dpi)
1231-
{
1232-
if (viewUnits <= 0)
1233-
return 1;
1234-
1235-
if (dpi == 0)
1236-
dpi = 96;
1237-
1238-
return Math.Max(1, (int)Math.Ceiling(viewUnits * dpi / 96.0));
1239-
}
1240-
12411267
private void OnMenuPanelKeyDown(object sender, KeyRoutedEventArgs e)
12421268
{
12431269
var buttons = MenuPanel.Children

‎tests/OpenClaw.Shared.Tests/MenuHelpersTests.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ public void ConvertPixelsToViewUnits_SmallResult_ReturnsAtLeastOne()
177177
Assert.Equal(1, MenuSizingHelper.ConvertPixelsToViewUnits(1, 192));
178178
}
179179

180+
[Fact]
181+
public void ConvertViewUnitsToPixels_96Dpi_ReturnsSameViewUnits()
182+
{
183+
Assert.Equal(100, MenuSizingHelper.ConvertViewUnitsToPixels(100, 96));
184+
}
185+
186+
[Fact]
187+
public void ConvertViewUnitsToPixels_FractionalDpi_RoundsUpToContainingPixel()
188+
{
189+
Assert.Equal(125, MenuSizingHelper.ConvertViewUnitsToPixels(100, 120));
190+
Assert.Equal(151, MenuSizingHelper.ConvertViewUnitsToPixels(100.5, 144));
191+
}
192+
193+
[Fact]
194+
public void ConvertViewUnitsToPixels_InvalidOrNonPositive_ReturnsZero()
195+
{
196+
Assert.Equal(0, MenuSizingHelper.ConvertViewUnitsToPixels(0, 96));
197+
Assert.Equal(0, MenuSizingHelper.ConvertViewUnitsToPixels(-1, 96));
198+
Assert.Equal(0, MenuSizingHelper.ConvertViewUnitsToPixels(double.NaN, 96));
199+
}
200+
180201
// ── HasDpiOrScaleChanged ─────────────────────────────────────────────────
181202

182203
[Fact]

‎tests/OpenClaw.Tray.Tests/MenuSizingHelperTests.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ public void ConvertPixels_Dpi120_CorrectScaling()
6464
Assert.Equal(800, MenuSizingHelper.ConvertPixelsToViewUnits(1000, 120));
6565
}
6666

67+
[Fact]
68+
public void ConvertViewUnits_StandardDpi96_ReturnsSameValue()
69+
{
70+
Assert.Equal(200, MenuSizingHelper.ConvertViewUnitsToPixels(200, 96));
71+
}
72+
73+
[Fact]
74+
public void ConvertViewUnits_Dpi120_RoundsUpToContainingPixel()
75+
{
76+
Assert.Equal(250, MenuSizingHelper.ConvertViewUnitsToPixels(200, 120));
77+
Assert.Equal(251, MenuSizingHelper.ConvertViewUnitsToPixels(200.5, 120));
78+
}
79+
80+
[Fact]
81+
public void ConvertViewUnits_InvalidOrNonPositive_ReturnsZero()
82+
{
83+
Assert.Equal(0, MenuSizingHelper.ConvertViewUnitsToPixels(0, 96));
84+
Assert.Equal(0, MenuSizingHelper.ConvertViewUnitsToPixels(-100, 96));
85+
Assert.Equal(0, MenuSizingHelper.ConvertViewUnitsToPixels(double.NaN, 96));
86+
}
87+
6788
[Fact]
6889
public void HasDpiOrScaleChanged_SameDpiAndScale_ReturnsFalse()
6990
{

‎tests/OpenClaw.Tray.Tests/TrayMenuWindowMarkupTests.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ public void Source_DoesNotDeclareAsyncVoidHandlers()
108108
string.Join(", ", offenders));
109109
}
110110

111+
[Fact]
112+
public void TrayMenuWindow_SizeToContent_MeasuresFinalRootWidthAndAppliesPixelSize()
113+
{
114+
var source = Read("src", "OpenClaw.Tray.WinUI", "Windows", "TrayMenuWindow.xaml.cs");
115+
116+
Assert.Contains("GetClientRect", source);
117+
Assert.Contains("RootGrid.Measure(new global::Windows.Foundation.Size(clientWidthViewUnits, double.PositiveInfinity))", source);
118+
Assert.Contains("ResizeWindowToPixelSize(_menuWidthPx, _menuHeightPx)", source);
119+
Assert.DoesNotContain("MenuPanel.Measure(new global::Windows.Foundation.Size(widthViewUnits, double.PositiveInfinity))", source);
120+
}
121+
111122
private static string Read(params string[] parts)
112123
=> File.ReadAllText(Path.Combine(new[] { TestRepositoryPaths.GetRepositoryRoot() }.Concat(parts).ToArray()));
113124

0 commit comments

Comments
 (0)