-
-
Notifications
You must be signed in to change notification settings - Fork 966
Expand file tree
/
Copy pathNavigationView.TemplateParts.cs
More file actions
347 lines (300 loc) · 12.8 KB
/
NavigationView.TemplateParts.cs
File metadata and controls
347 lines (300 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
// Based on Windows UI Library
// Copyright(c) Microsoft Corporation.All rights reserved.
using System;
using System.ComponentModel;
using System.Windows;
// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;
/// <content>
/// Defines the template parts for the <see cref="NavigationView"/> control
/// </content>
[TemplatePart(
Name = TemplateElementNavigationViewContentPresenter,
Type = typeof(NavigationViewContentPresenter)
)]
[TemplatePart(
Name = TemplateElementMenuItemsItemsControl,
Type = typeof(System.Windows.Controls.ItemsControl)
)]
[TemplatePart(
Name = TemplateElementFooterMenuItemsItemsControl,
Type = typeof(System.Windows.Controls.ItemsControl)
)]
[TemplatePart(Name = TemplateElementBackButton, Type = typeof(System.Windows.Controls.Button))]
[TemplatePart(Name = TemplateElementToggleButton, Type = typeof(System.Windows.Controls.Button))]
[TemplatePart(
Name = TemplateElementAutoSuggestBoxSymbolButton,
Type = typeof(System.Windows.Controls.Button)
)]
[TemplatePart(Name = TemplateElementPaneColumn, Type = typeof(System.Windows.Controls.ColumnDefinition))]
public partial class NavigationView
{
/// <summary>
/// Template element represented by the <c>PART_MenuItemsItemsControl</c> name.
/// </summary>
private const string TemplateElementNavigationViewContentPresenter =
"PART_NavigationViewContentPresenter";
/// <summary>
/// Template element represented by the <c>PART_MenuItemsItemsControl</c> name.
/// </summary>
private const string TemplateElementMenuItemsItemsControl = "PART_MenuItemsItemsControl";
/// <summary>
/// Template element represented by the <c>PART_FooterMenuItemsItemsControl</c> name.
/// </summary>
private const string TemplateElementFooterMenuItemsItemsControl = "PART_FooterMenuItemsItemsControl";
/// <summary>
/// Template element represented by the <c>PART_BackButton</c> name.
/// </summary>
private const string TemplateElementBackButton = "PART_BackButton";
/// <summary>
/// Template element represented by the <c>PART_ToggleButton</c> name.
/// </summary>
private const string TemplateElementToggleButton = "PART_ToggleButton";
/// <summary>
/// Template element represented by the <c>PART_AutoSuggestBoxSymbolButton</c> name.
/// </summary>
private const string TemplateElementAutoSuggestBoxSymbolButton = "PART_AutoSuggestBoxSymbolButton";
/// <summary>
/// Template element represented by the <c>PART_PaneColumn</c> name.
/// </summary>
private const string TemplateElementPaneColumn = "PART_PaneColumn";
/// <summary>
/// Gets or sets the control responsible for rendering the content.
/// </summary>
protected NavigationViewContentPresenter NavigationViewContentPresenter { get; set; } = null!;
/// <summary>
/// Gets or sets the control located at the top of the pane with left arrow icon.
/// </summary>
protected System.Windows.Controls.ItemsControl MenuItemsItemsControl { get; set; } = null!;
/// <summary>
/// Gets or sets the control located at the top of the pane with hamburger icon.
/// </summary>
protected System.Windows.Controls.ItemsControl FooterMenuItemsItemsControl { get; set; } = null!;
/// <summary>
/// Gets or sets the control located at the top of the pane with left arrow icon.
/// </summary>
protected System.Windows.Controls.Button? BackButton { get; set; }
/// <summary>
/// Gets or sets the control located at the top of the pane with hamburger icon.
/// </summary>
protected System.Windows.Controls.Button? ToggleButton { get; set; }
/// <summary>
/// Gets or sets the control that is visitable if PaneDisplayMode="Left" and in compact state
/// </summary>
protected System.Windows.Controls.Button? AutoSuggestBoxSymbolButton { get; set; }
/// <summary>
/// Gets or sets the pane column definition for GridSplitter support.
/// </summary>
protected System.Windows.Controls.ColumnDefinition? PaneColumn { get; set; }
private DependencyPropertyDescriptor? _openPaneLengthDescriptor;
private DependencyPropertyDescriptor? _isPaneOpenDescriptor;
private double _lastOpenPaneWidth = double.NaN;
/// <inheritdoc />
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
NavigationViewContentPresenter = GetTemplateChild<NavigationViewContentPresenter>(
TemplateElementNavigationViewContentPresenter
);
MenuItemsItemsControl = GetTemplateChild<System.Windows.Controls.ItemsControl>(
TemplateElementMenuItemsItemsControl
);
FooterMenuItemsItemsControl = GetTemplateChild<System.Windows.Controls.ItemsControl>(
TemplateElementFooterMenuItemsItemsControl
);
MenuItemsItemsControl.SetCurrentValue(
System.Windows.Controls.ItemsControl.ItemsSourceProperty,
MenuItems
);
FooterMenuItemsItemsControl.SetCurrentValue(
System.Windows.Controls.ItemsControl.ItemsSourceProperty,
FooterMenuItems
);
if (NavigationViewContentPresenter is not null)
{
NavigationViewContentPresenter.Navigated -= OnNavigationViewContentPresenterNavigated;
NavigationViewContentPresenter.Navigated += OnNavigationViewContentPresenterNavigated;
}
if (
GetTemplateChild(TemplateElementAutoSuggestBoxSymbolButton)
is System.Windows.Controls.Button autoSuggestBoxSymbolButton
)
{
AutoSuggestBoxSymbolButton = autoSuggestBoxSymbolButton;
AutoSuggestBoxSymbolButton.Click -= AutoSuggestBoxSymbolButtonOnClick;
AutoSuggestBoxSymbolButton.Click += AutoSuggestBoxSymbolButtonOnClick;
}
if (GetTemplateChild(TemplateElementBackButton) is System.Windows.Controls.Button backButton)
{
BackButton = backButton;
BackButton.Click -= OnBackButtonClick;
BackButton.Click += OnBackButtonClick;
}
if (GetTemplateChild(TemplateElementToggleButton) is System.Windows.Controls.Button toggleButton)
{
ToggleButton = toggleButton;
ToggleButton.Click -= OnToggleButtonClick;
ToggleButton.Click += OnToggleButtonClick;
}
// Clean up previous event handlers
if (_openPaneLengthDescriptor != null)
{
_openPaneLengthDescriptor.RemoveValueChanged(this, OnPanePropertyChanged);
_openPaneLengthDescriptor = null;
}
if (_isPaneOpenDescriptor != null)
{
_isPaneOpenDescriptor.RemoveValueChanged(this, OnPanePropertyChanged);
_isPaneOpenDescriptor = null;
}
// Initialize PaneColumn width for GridSplitter support
var paneColumn = GetTemplateChild(TemplateElementPaneColumn) as System.Windows.Controls.ColumnDefinition;
if (paneColumn != null)
{
PaneColumn = paneColumn;
// Set initial width using OpenPaneLength or saved width
if (IsPaneOpen && OpenPaneLength > 0)
{
var initialWidth = !double.IsNaN(_lastOpenPaneWidth) && _lastOpenPaneWidth > 0
? _lastOpenPaneWidth
: OpenPaneLength;
PaneColumn.Width = new GridLength(initialWidth);
}
else if (!IsPaneOpen)
{
PaneColumn.Width = new GridLength(40.0);
}
// Update on next render pass to avoid issues
Dispatcher.BeginInvoke(new Action(UpdatePaneColumnWidth), System.Windows.Threading.DispatcherPriority.Loaded);
// Listen to OpenPaneLength and IsPaneOpen changes
_openPaneLengthDescriptor = DependencyPropertyDescriptor.FromProperty(OpenPaneLengthProperty, typeof(NavigationView));
if (_openPaneLengthDescriptor != null)
{
_openPaneLengthDescriptor.AddValueChanged(this, OnPanePropertyChanged);
}
_isPaneOpenDescriptor = DependencyPropertyDescriptor.FromProperty(IsPaneOpenProperty, typeof(NavigationView));
if (_isPaneOpenDescriptor != null)
{
_isPaneOpenDescriptor.AddValueChanged(this, OnPanePropertyChanged);
}
}
else
{
PaneColumn = null;
}
}
private void OnPanePropertyChanged(object? sender, EventArgs e)
{
if (Dispatcher.CheckAccess())
{
UpdatePaneColumnWidth();
}
else
{
Dispatcher.BeginInvoke(new Action(UpdatePaneColumnWidth), System.Windows.Threading.DispatcherPriority.Normal);
}
}
private void UpdatePaneColumnWidth()
{
if (PaneColumn is null)
{
return;
}
try
{
// If pane is closed, don't update the width - maintain closed state
// This prevents the pane from automatically opening when window size changes
if (!IsPaneOpen)
{
// Only ensure it's set to 40.0 if it's not already
if (!PaneColumn.Width.IsAbsolute || Math.Abs(PaneColumn.Width.Value - 40.0) > 0.1)
{
PaneColumn.SetCurrentValue(System.Windows.Controls.ColumnDefinition.WidthProperty, new GridLength(40.0));
}
return;
}
// Only update width when pane is open
var currentWidth = PaneColumn.Width.IsAbsolute ? PaneColumn.Width.Value : OpenPaneLength;
var targetWidth = !double.IsNaN(_lastOpenPaneWidth) && _lastOpenPaneWidth > 0
? _lastOpenPaneWidth
: OpenPaneLength;
// Don't update if GridSplitter is enabled and user has manually resized
// This allows GridSplitter to control the width
if (IsGridSplitterEnabled && Math.Abs(currentWidth - targetWidth) > 1.0)
{
// User has manually resized, save the current width
if (PaneColumn.Width.IsAbsolute)
{
_lastOpenPaneWidth = PaneColumn.Width.Value;
}
return;
}
// Update to target width
if (Math.Abs(currentWidth - targetWidth) > 0.1)
{
PaneColumn.SetCurrentValue(System.Windows.Controls.ColumnDefinition.WidthProperty, new GridLength(targetWidth));
}
}
catch
{
// Ignore errors during initialization
}
}
/// <summary>
/// Updates PaneColumn width when pane is toggled open/closed.
/// This is called from OnPaneOpened/OnPaneClosed to ensure width updates even if user has resized.
/// </summary>
private void UpdatePaneColumnWidthForToggle()
{
if (PaneColumn is null)
{
return;
}
try
{
if (IsPaneOpen)
{
// When opening, use the last saved width if available, otherwise use OpenPaneLength
var targetWidth = !double.IsNaN(_lastOpenPaneWidth) && _lastOpenPaneWidth > 0
? _lastOpenPaneWidth
: OpenPaneLength;
if (targetWidth > 0)
{
PaneColumn.SetCurrentValue(System.Windows.Controls.ColumnDefinition.WidthProperty, new GridLength(targetWidth));
}
}
else
{
// When closing, save the current width if GridSplitter is enabled and pane was resized
if (IsGridSplitterEnabled && PaneColumn.Width.IsAbsolute)
{
var currentWidth = PaneColumn.Width.Value;
// Only save if it's different from the default OpenPaneLength (user has resized)
if (Math.Abs(currentWidth - OpenPaneLength) > 1.0)
{
_lastOpenPaneWidth = currentWidth;
}
}
// Always set to 40.0 when closing
PaneColumn.SetCurrentValue(System.Windows.Controls.ColumnDefinition.WidthProperty, new GridLength(40.0));
}
}
catch
{
// Ignore errors during initialization
}
}
protected T GetTemplateChild<T>(string name)
where T : DependencyObject
{
if (GetTemplateChild(name) is not T dependencyObject)
{
throw new ArgumentNullException(name);
}
return dependencyObject;
}
}