|
11 | 11 | using System.Linq; |
12 | 12 | using System.Windows; |
13 | 13 | using System.Windows.Input; |
| 14 | +using System.Windows.Media; |
14 | 15 | using DrawingSize = System.Drawing.Size; |
15 | 16 |
|
16 | 17 | namespace ContextMenuManager |
@@ -63,6 +64,7 @@ public MainWindow() |
63 | 64 |
|
64 | 65 | // Populate navigation items from AppString |
65 | 66 | BuildNavigation(); |
| 67 | + AdjustPaneWidthToContent(); |
66 | 68 |
|
67 | 69 | // First-run language download prompt |
68 | 70 | Loaded += (_, _) => FirstRunDownloadLanguage(); |
@@ -149,6 +151,50 @@ private void BuildNavigation() |
149 | 151 | } |
150 | 152 | } |
151 | 153 |
|
| 154 | + // Sizes OpenPaneLength to fit the widest item label so translations don't get clipped. |
| 155 | + // Chrome values are measured against iNKORE.UI.WPF.Modern v0.10.2's NavigationViewItem template |
| 156 | + // (Segoe UI 14pt): an item's rendered width = LPad + content + RPad, where LPad/RPad depend |
| 157 | + // on whether the item has an icon or an expand chevron. |
| 158 | + private void AdjustPaneWidthToContent() |
| 159 | + { |
| 160 | + const double chevronChrome = 100; // expandable parent (icon + chevron slot) |
| 161 | + const double iconChrome = 74; // leaf with icon (footer items) |
| 162 | + const double plainChrome = 67; // nested leaf, no icon |
| 163 | + const double minPaneLength = 185; |
| 164 | + const double maxPaneLength = 400; |
| 165 | + |
| 166 | + var typeface = new Typeface(NavView.FontFamily, NavView.FontStyle, NavView.FontWeight, NavView.FontStretch); |
| 167 | + var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip; |
| 168 | + var fontSize = NavView.FontSize > 0 ? NavView.FontSize : 14; |
| 169 | + |
| 170 | + double maxRequired = 0; |
| 171 | + foreach (var item in EnumerateNavigationItems()) |
| 172 | + { |
| 173 | + if (item.Content is not string text || text.Length == 0) continue; |
| 174 | + |
| 175 | + double chrome = item.MenuItems.Count > 0 ? chevronChrome |
| 176 | + : item.Icon != null ? iconChrome |
| 177 | + : plainChrome; |
| 178 | + |
| 179 | + var ft = new FormattedText( |
| 180 | + text, |
| 181 | + CultureInfo.CurrentUICulture, |
| 182 | + FlowDirection.LeftToRight, |
| 183 | + typeface, |
| 184 | + fontSize, |
| 185 | + Brushes.Black, |
| 186 | + pixelsPerDip); |
| 187 | + |
| 188 | + double required = ft.Width + chrome; |
| 189 | + if (required > maxRequired) maxRequired = required; |
| 190 | + } |
| 191 | + |
| 192 | + NavView.OpenPaneLength = Math.Clamp( |
| 193 | + Math.Ceiling(maxRequired), |
| 194 | + minPaneLength, |
| 195 | + maxPaneLength); |
| 196 | + } |
| 197 | + |
152 | 198 | private static NavigationViewItem MakeSectionItem(string content, string glyph) |
153 | 199 | { |
154 | 200 | var item = new NavigationViewItem() { Content = content, Icon = new FontIcon { Glyph = glyph } }; |
|
0 commit comments