@@ -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
0 commit comments