-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathSettingsCard.cs
More file actions
315 lines (271 loc) · 11.3 KB
/
SettingsCard.cs
File metadata and controls
315 lines (271 loc) · 11.3 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel.Design;
namespace CommunityToolkit.WinUI.Controls;
/// <summary>
/// This is the base control to create consistent settings experiences, inline with the Windows 11 design language.
/// A SettingsCard can also be hosted within a SettingsExpander.
/// </summary>
[TemplatePart(Name = ActionIconPresenterHolder, Type = typeof(Viewbox))]
[TemplatePart(Name = HeaderPresenter, Type = typeof(ContentPresenter))]
[TemplatePart(Name = DescriptionPresenter, Type = typeof(ContentPresenter))]
[TemplatePart(Name = HeaderIconPresenterHolder, Type = typeof(Viewbox))]
[TemplateVisualState(Name = NormalState, GroupName = CommonStates)]
[TemplateVisualState(Name = PointerOverState, GroupName = CommonStates)]
[TemplateVisualState(Name = PressedState, GroupName = CommonStates)]
[TemplateVisualState(Name = DisabledState, GroupName = CommonStates)]
[TemplateVisualState(Name = RightState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = RightWrappedState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = RightWrappedNoIconState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = LeftState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = VerticalState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = HorizontalState, GroupName = ContentAlignmentStates)]
[TemplateVisualState(Name = NoContentSpacingState, GroupName = ContentSpacingStates)]
[TemplateVisualState(Name = ContentSpacingState, GroupName = ContentSpacingStates)]
public partial class SettingsCard : ButtonBase
{
internal const string CommonStates = "CommonStates";
internal const string NormalState = "Normal";
internal const string PointerOverState = "PointerOver";
internal const string PressedState = "Pressed";
internal const string DisabledState = "Disabled";
internal const string ContentAlignmentStates = "ContentAlignmentStates";
internal const string RightState = "Right";
internal const string RightWrappedState = "RightWrapped";
internal const string RightWrappedNoIconState = "RightWrappedNoIcon";
internal const string LeftState = "Left";
internal const string VerticalState = "Vertical";
internal const string HorizontalState = "Horizontal";
internal const string ContentSpacingStates = "ContentSpacingStates";
internal const string NoContentSpacingState = "NoContentSpacing";
internal const string ContentSpacingState = "ContentSpacing";
internal const string ActionIconPresenterHolder = "PART_ActionIconPresenterHolder";
internal const string HeaderPresenter = "PART_HeaderPresenter";
internal const string DescriptionPresenter = "PART_DescriptionPresenter";
internal const string HeaderIconPresenterHolder = "PART_HeaderIconPresenterHolder";
/// <summary>
/// Creates a new instance of the <see cref="SettingsCard"/> class.
/// </summary>
public SettingsCard()
{
this.DefaultStyleKey = typeof(SettingsCard);
}
/// <inheritdoc />
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
IsEnabledChanged -= OnIsEnabledChanged;
OnActionIconChanged();
OnHeaderChanged();
OnHeaderIconChanged();
OnDescriptionChanged();
OnIsClickEnabledChanged();
CheckInitialVisualState();
SetAccessibleContentName();
RegisterPropertyChangedCallback(ContentProperty, OnContentChanged);
IsEnabledChanged += OnIsEnabledChanged;
}
private void CheckInitialVisualState()
{
VisualStateManager.GoToState(this, IsEnabled ? NormalState : DisabledState, true);
if (GetTemplateChild("ContentAlignmentStates") is VisualStateGroup contentAlignmentStatesGroup)
{
contentAlignmentStatesGroup.CurrentStateChanged -= this.ContentAlignmentStates_Changed;
CheckVerticalSpacingState(contentAlignmentStatesGroup.CurrentState);
contentAlignmentStatesGroup.CurrentStateChanged += this.ContentAlignmentStates_Changed;
}
}
// We automatically set the AutomationProperties.Name of the Content if not configured.
private void SetAccessibleContentName()
{
if (Header is string headerString && headerString != string.Empty)
{
// We don't want to override an AutomationProperties.Name that is manually set, or if the Content basetype is of type ButtonBase (the ButtonBase.Content will be used then)
if (Content is UIElement element && string.IsNullOrEmpty(AutomationProperties.GetName(element)) && element.GetType().BaseType != typeof(ButtonBase) && element.GetType() != typeof(TextBlock))
{
AutomationProperties.SetName(element, headerString);
}
}
}
private void EnableButtonInteraction()
{
DisableButtonInteraction();
IsTabStop = true;
PointerEntered += Control_PointerEntered;
PointerExited += Control_PointerExited;
PointerCaptureLost += Control_PointerCaptureLost;
PointerCanceled += Control_PointerCanceled;
PreviewKeyDown += Control_PreviewKeyDown;
PreviewKeyUp += Control_PreviewKeyUp;
}
private void DisableButtonInteraction()
{
IsTabStop = false;
PointerEntered -= Control_PointerEntered;
PointerExited -= Control_PointerExited;
PointerCaptureLost -= Control_PointerCaptureLost;
PointerCanceled -= Control_PointerCanceled;
PreviewKeyDown -= Control_PreviewKeyDown;
PreviewKeyUp -= Control_PreviewKeyUp;
}
private void Control_PreviewKeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter || e.Key == Windows.System.VirtualKey.Space || e.Key == Windows.System.VirtualKey.GamepadA)
{
VisualStateManager.GoToState(this, NormalState, true);
}
}
private void Control_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter || e.Key == Windows.System.VirtualKey.Space || e.Key == Windows.System.VirtualKey.GamepadA)
{
// Check if the active focus is on the card itself - only then we show the pressed state.
if (GetFocusedElement() is SettingsCard)
{
VisualStateManager.GoToState(this, PressedState, true);
}
}
}
public void Control_PointerEntered(object sender, PointerRoutedEventArgs e)
{
base.OnPointerEntered(e);
VisualStateManager.GoToState(this, PointerOverState, true);
}
public void Control_PointerExited(object sender, PointerRoutedEventArgs e)
{
base.OnPointerExited(e);
VisualStateManager.GoToState(this, NormalState, true);
}
private void Control_PointerCaptureLost(object sender, PointerRoutedEventArgs e)
{
base.OnPointerCaptureLost(e);
VisualStateManager.GoToState(this, NormalState, true);
}
private void Control_PointerCanceled(object sender, PointerRoutedEventArgs e)
{
base.OnPointerCanceled(e);
VisualStateManager.GoToState(this, NormalState, true);
}
protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
// e.Handled = true;
if (IsClickEnabled)
{
base.OnPointerPressed(e);
VisualStateManager.GoToState(this, PressedState, true);
}
}
protected override void OnPointerReleased(PointerRoutedEventArgs e)
{
if (IsClickEnabled)
{
base.OnPointerReleased(e);
VisualStateManager.GoToState(this, NormalState, true);
}
}
/// <summary>
/// Creates AutomationPeer
/// </summary>
/// <returns>An automation peer for <see cref="SettingsCard"/>.</returns>
protected override AutomationPeer OnCreateAutomationPeer()
{
return new SettingsCardAutomationPeer(this);
}
private void OnIsClickEnabledChanged()
{
OnActionIconChanged();
if (IsClickEnabled)
{
EnableButtonInteraction();
}
else
{
DisableButtonInteraction();
}
}
private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
VisualStateManager.GoToState(this, IsEnabled ? NormalState : DisabledState, true);
}
private void OnActionIconChanged()
{
if (GetTemplateChild(ActionIconPresenterHolder) is FrameworkElement actionIconPresenter)
{
if (IsClickEnabled && IsActionIconVisible)
{
actionIconPresenter.Visibility = Visibility.Visible;
}
else
{
actionIconPresenter.Visibility =Visibility.Collapsed;
}
}
}
private void OnHeaderIconChanged()
{
if (GetTemplateChild(HeaderIconPresenterHolder) is FrameworkElement headerIconPresenter)
{
headerIconPresenter.Visibility = HeaderIcon != null
? Visibility.Visible
: Visibility.Collapsed;
}
}
private void OnDescriptionChanged()
{
if (GetTemplateChild(DescriptionPresenter) is FrameworkElement descriptionPresenter)
{
descriptionPresenter.Visibility = IsNullOrEmptyString(Description)
? Visibility.Collapsed
: Visibility.Visible;
}
}
private void OnHeaderChanged()
{
if (GetTemplateChild(HeaderPresenter) is FrameworkElement headerPresenter)
{
headerPresenter.Visibility = IsNullOrEmptyString(Header)
? Visibility.Collapsed
: Visibility.Visible;
}
}
private void ContentAlignmentStates_Changed(object sender, VisualStateChangedEventArgs e)
{
CheckVerticalSpacingState(e.NewState);
}
private void CheckVerticalSpacingState(VisualState s)
{
// On state change, checking if the Content should be wrapped (e.g. when the card is made smaller or the ContentAlignment is set to Vertical). If the Content and the Header or Description are not null, we add spacing between the Content and the Header/Description.
if (s != null && (s.Name == RightWrappedState || s.Name == RightWrappedNoIconState || s.Name == VerticalState) && (Content != null) && (!IsNullOrEmptyString(Header) || !IsNullOrEmptyString(Description)))
{
VisualStateManager.GoToState(this, ContentSpacingState, true);
}
else
{
VisualStateManager.GoToState(this, NoContentSpacingState, true);
}
}
private FrameworkElement? GetFocusedElement()
{
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
{
return FocusManager.GetFocusedElement(XamlRoot) as FrameworkElement;
}
else
{
return FocusManager.GetFocusedElement() as FrameworkElement;
}
}
private static bool IsNullOrEmptyString(object obj)
{
if (obj == null)
{
return true;
}
if (obj is string objString && objString == string.Empty)
{
return true;
}
return false;
}
}