forked from MahApps/MahApps.Metro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplitView.cs
428 lines (378 loc) · 18.1 KB
/
SplitView.cs
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
namespace MahApps.Metro.Controls
{
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
/// <summary>
/// Represents a container with two views; one view for the main content and another view that is typically used for
/// navigation commands.
/// </summary>
[TemplatePart(Name = "PaneClipRectangle", Type = typeof(RectangleGeometry))]
[TemplatePart(Name = "LightDismissLayer", Type = typeof(Rectangle))]
[TemplateVisualState(Name = "Closed ", GroupName = "DisplayModeStates")]
[TemplateVisualState(Name = "ClosedCompactLeft ", GroupName = "DisplayModeStates")]
[TemplateVisualState(Name = "ClosedCompactRight ", GroupName = "DisplayModeStates")]
[TemplateVisualState(Name = "OpenOverlayLeft ", GroupName = "DisplayModeStates")]
[TemplateVisualState(Name = "OpenOverlayRight ", GroupName = "DisplayModeStates")]
[TemplateVisualState(Name = "OpenInlineLeft ", GroupName = "DisplayModeStates")]
[TemplateVisualState(Name = "OpenInlineRight ", GroupName = "DisplayModeStates")]
[TemplateVisualState(Name = "OpenCompactOverlayLeft ", GroupName = "DisplayModeStates")]
[TemplateVisualState(Name = "OpenCompactOverlayRight", GroupName = "DisplayModeStates")]
[ContentProperty("Content")]
public class SplitView : Control
{
/// <summary>
/// Identifies the <see cref="CompactPaneLength" /> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="CompactPaneLength" /> property.</returns>
public static readonly DependencyProperty CompactPaneLengthProperty =
DependencyProperty.Register("CompactPaneLength", typeof(double), typeof(SplitView), new PropertyMetadata(0d, OnMetricsChanged));
private static void OnMetricsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = d as SplitView;
sender?.TemplateSettings?.Update();
sender?.ChangeVisualState(true, true);
}
/// <summary>
/// Gets or sets the width of the <see cref="SplitView" /> pane in its compact display mode.
/// </summary>
/// <returns>
/// The width of the pane in it's compact display mode. The default is 48 device-independent pixel (DIP) (defined
/// by the SplitViewCompactPaneThemeLength resource).
/// </returns>
public double CompactPaneLength
{
get { return (double)this.GetValue(CompactPaneLengthProperty); }
set { this.SetValue(CompactPaneLengthProperty, value); }
}
/// <summary>
/// Identifies the <see cref="Content" /> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="Content" /> dependency property.</returns>
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(UIElement), typeof(SplitView), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the contents of the main panel of a <see cref="SplitView" />.
/// </summary>
/// <returns>The contents of the main panel of a <see cref="SplitView" />. The default is null.</returns>
public UIElement Content
{
get { return (UIElement)this.GetValue(ContentProperty); }
set { this.SetValue(ContentProperty, value); }
}
/// <summary>
/// Identifies the <see cref="DisplayMode" /> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="DisplayMode" /> dependency property.</returns>
public static readonly DependencyProperty DisplayModeProperty =
DependencyProperty.Register("DisplayMode", typeof(SplitViewDisplayMode), typeof(SplitView), new PropertyMetadata(SplitViewDisplayMode.Overlay, OnStateChanged));
private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = d as SplitView;
sender?.ChangeVisualState();
}
/// <summary>
/// Gets of sets a value that specifies how the pane and content areas of a <see cref="SplitView" /> are shown.
/// </summary>
/// <returns>
/// A value of the enumeration that specifies how the pane and content areas of a <see cref="SplitView" /> are
/// shown. The default is <see cref="SplitViewDisplayMode.Overlay" />.
/// </returns>
public SplitViewDisplayMode DisplayMode
{
get { return (SplitViewDisplayMode)this.GetValue(DisplayModeProperty); }
set { this.SetValue(DisplayModeProperty, value); }
}
/// <summary>
/// Identifies the <see cref="IsPaneOpen" /> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="IsPaneOpen" /> dependency property.</returns>
public static readonly DependencyProperty IsPaneOpenProperty =
DependencyProperty.Register("IsPaneOpen", typeof(bool), typeof(SplitView), new PropertyMetadata(false, OnIsPaneOpenChanged));
private static void OnIsPaneOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = d as SplitView;
var newValue = (bool)e.NewValue;
var oldValue = (bool)e.OldValue;
if (sender == null
|| newValue == oldValue)
return;
if (newValue)
sender.ChangeVisualState(); // Open pane
else
sender.OnIsPaneOpenChanged(); // Close pane
}
/// <summary>
/// Gets or sets a value that specifies whether the <see cref="SplitView" /> pane is expanded to its full width.
/// </summary>
/// <returns>true if the pane is expanded to its full width; otherwise, false. The default is true.</returns>
public bool IsPaneOpen
{
get { return (bool)this.GetValue(IsPaneOpenProperty); }
set { this.SetValue(IsPaneOpenProperty, value); }
}
/// <summary>
/// Identifies the <see cref="OverlayBrush"/> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="OverlayBrush" /> dependency property.</returns>
public static readonly DependencyProperty OverlayBrushProperty =
DependencyProperty.Register("OverlayBrush", typeof(Brush), typeof(SplitView), new PropertyMetadata(Brushes.Transparent));
/// <summary>
/// Gets or sets a value that specifies the OverlayBrush
/// </summary>
/// <returns>The current OverlayBrush</returns>
public Brush OverlayBrush
{
get { return (Brush)this.GetValue(OverlayBrushProperty); }
set { this.SetValue(OverlayBrushProperty, value); }
}
/// <summary>
/// Identifies the <see cref="OpenPaneLength" /> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="OpenPaneLength" /> dependency property.</returns>
public static readonly DependencyProperty OpenPaneLengthProperty =
DependencyProperty.Register("OpenPaneLength", typeof(double), typeof(SplitView), new PropertyMetadata(0d, OnMetricsChanged));
/// <summary>
/// Gets or sets the width of the <see cref="SplitView" /> pane when it's fully expanded.
/// </summary>
/// <returns>
/// The width of the <see cref="SplitView" /> pane when it's fully expanded. The default is 320 device-independent
/// pixel (DIP).
/// </returns>
public double OpenPaneLength
{
get { return (double)this.GetValue(OpenPaneLengthProperty); }
set { this.SetValue(OpenPaneLengthProperty, value); }
}
/// <summary>
/// Identifies the <see cref="Pane" /> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="Pane" /> dependency property.</returns>
public static readonly DependencyProperty PaneProperty =
DependencyProperty.Register("Pane", typeof(UIElement), typeof(SplitView), new PropertyMetadata(null, UpdateLogicalChild));
/// <summary>
/// Gets or sets the contents of the pane of a <see cref="SplitView" />.
/// </summary>
/// <returns>The contents of the pane of a <see cref="SplitView" />. The default is null.</returns>
public UIElement Pane
{
get { return (UIElement)this.GetValue(PaneProperty); }
set { this.SetValue(PaneProperty, value); }
}
/// <summary>
/// Identifies the <see cref="PaneBackground" /> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="PaneBackground" /> dependency property.</returns>
public static readonly DependencyProperty PaneBackgroundProperty =
DependencyProperty.Register("PaneBackground", typeof(Brush), typeof(SplitView), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the Brush to apply to the background of the <see cref="Pane" /> area of the control.
/// </summary>
/// <returns>The Brush to apply to the background of the <see cref="Pane" /> area of the control.</returns>
public Brush PaneBackground
{
get { return (Brush)this.GetValue(PaneBackgroundProperty); }
set { this.SetValue(PaneBackgroundProperty, value); }
}
/// <summary>
/// Identifies the <see cref="PaneForeground" /> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="PaneForeground" /> dependency property.</returns>
public static readonly DependencyProperty PaneForegroundProperty =
DependencyProperty.Register("PaneForeground", typeof(Brush), typeof(SplitView), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the Brush to apply to the foreground of the <see cref="Pane" /> area of the control.
/// </summary>
/// <returns>The Brush to apply to the background of the <see cref="Pane" /> area of the control.</returns>
public Brush PaneForeground
{
get { return (Brush)this.GetValue(PaneForegroundProperty); }
set { this.SetValue(PaneForegroundProperty, value); }
}
/// <summary>
/// Identifies the PanePlacement dependency property.
/// </summary>
/// <returns>The identifier for the PanePlacement dependency property.</returns>
public static readonly DependencyProperty PanePlacementProperty =
DependencyProperty.Register("PanePlacement", typeof(SplitViewPanePlacement), typeof(SplitView), new PropertyMetadata(SplitViewPanePlacement.Left, OnStateChanged));
/// <summary>
/// Gets or sets a value that specifies whether the pane is shown on the right or left side of the
/// <see cref="SplitView" />.
/// </summary>
/// <returns>
/// A value of the enumeration that specifies whether the pane is shown on the right or left side of the
/// <see cref="SplitView" />. The default is <see cref="SplitViewPanePlacement.Left" />.
/// </returns>
public SplitViewPanePlacement PanePlacement
{
get { return (SplitViewPanePlacement)this.GetValue(PanePlacementProperty); }
set { this.SetValue(PanePlacementProperty, value); }
}
/// <summary>
/// Identifies the <see cref="TemplateSettings" /> dependency property.
/// </summary>
/// <returns>The identifier for the <see cref="TemplateSettings" /> dependency property.</returns>
public static readonly DependencyProperty TemplateSettingsProperty =
DependencyProperty.Register("TemplateSettings", typeof(SplitViewTemplateSettings), typeof(SplitView), new PropertyMetadata(null));
/// <summary>
/// Gets an object that provides calculated values that can be referenced as TemplateBinding sources when defining
/// templates for a <see cref="SplitView" /> control.
/// </summary>
/// <returns>An object that provides calculated values for templates.</returns>
public SplitViewTemplateSettings TemplateSettings
{
get { return (SplitViewTemplateSettings)this.GetValue(TemplateSettingsProperty); }
private set { this.SetValue(TemplateSettingsProperty, value); }
}
private Rectangle lightDismissLayer;
private RectangleGeometry paneClipRectangle;
/// <summary>
/// Initializes a new instance of the <see cref="SplitView" /> class.
/// </summary>
public SplitView()
{
this.DefaultStyleKey = typeof(SplitView);
this.TemplateSettings = new SplitViewTemplateSettings(this);
}
/// <summary>
/// Occurs when the <see cref="SplitView" /> pane is closed.
/// </summary>
public event EventHandler PaneClosed;
/// <summary>
/// Occurs when the <see cref="SplitView" /> pane is closing.
/// </summary>
public event EventHandler<SplitViewPaneClosingEventArgs> PaneClosing;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.paneClipRectangle = this.GetTemplateChild("PaneClipRectangle") as RectangleGeometry;
this.lightDismissLayer = this.GetTemplateChild("LightDismissLayer") as Rectangle;
if (this.lightDismissLayer != null)
{
this.lightDismissLayer.MouseDown += this.OnLightDismiss;
}
this.ExecuteWhenLoaded(() =>
{
this.TemplateSettings.Update();
this.ChangeVisualState(false);
});
}
private static void UpdateLogicalChild(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (!(dependencyObject is SplitView splitView))
{
return;
}
if (e.OldValue is FrameworkElement oldChild)
{
splitView.RemoveLogicalChild(oldChild);
}
if (e.NewValue is FrameworkElement newChild)
{
splitView.AddLogicalChild(newChild);
newChild.DataContext = splitView.DataContext;
}
}
/// <inheritdoc />
protected override IEnumerator LogicalChildren
{
get
{
// cheat, make a list with all logical content and return the enumerator
ArrayList children = new ArrayList();
if (this.Pane != null)
{
children.Add(this.Pane);
}
if (this.Content != null)
{
children.Add(this.Content);
}
return children.GetEnumerator();
}
}
protected override void OnRenderSizeChanged(SizeChangedInfo info)
{
base.OnRenderSizeChanged(info);
if (this.paneClipRectangle != null)
{
this.paneClipRectangle.Rect = new Rect(0, 0, this.OpenPaneLength, (double)this.ActualHeight);
}
}
protected virtual void ChangeVisualState(bool animated = true, bool reset = false)
{
if (this.paneClipRectangle != null)
{
this.paneClipRectangle.Rect = new Rect(0, 0, this.OpenPaneLength, (double)this.ActualHeight); // We could also use ActualHeight and subscribe to the SizeChanged property
}
var state = string.Empty;
if (this.IsPaneOpen)
{
state += "Open";
switch (this.DisplayMode)
{
case SplitViewDisplayMode.CompactInline:
state += "Inline";
break;
default:
state += this.DisplayMode.ToString();
break;
}
state += this.PanePlacement.ToString();
}
else
{
state += "Closed";
if (this.DisplayMode == SplitViewDisplayMode.CompactInline
|| this.DisplayMode == SplitViewDisplayMode.CompactOverlay)
{
state += "Compact";
state += this.PanePlacement.ToString();
}
}
if (reset)
{
VisualStateManager.GoToState(this, "None", animated);
}
VisualStateManager.GoToState(this, state, animated);
}
protected virtual void OnIsPaneOpenChanged()
{
var cancel = false;
if (this.PaneClosing != null)
{
var args = new SplitViewPaneClosingEventArgs();
foreach (var paneClosingDelegates in this.PaneClosing.GetInvocationList())
{
var eventHandler = paneClosingDelegates as EventHandler<SplitViewPaneClosingEventArgs>;
if (eventHandler == null)
continue;
eventHandler(this, args);
if (args.Cancel)
{
cancel = true;
break;
}
}
}
if (!cancel)
{
this.ChangeVisualState();
this.PaneClosed?.Invoke(this, EventArgs.Empty);
}
else
{
this.SetCurrentValue(IsPaneOpenProperty, false);
}
}
private void OnLightDismiss(object sender, MouseButtonEventArgs e)
{
this.SetCurrentValue(IsPaneOpenProperty, false);
}
}
}