Skip to content

Commit 3bcaaf3

Browse files
authored
Code Quality: ThemedIcon, Changed IsToggle to ToggleBehavior (#15948)
1 parent 7aa773d commit 3bcaaf3

21 files changed

+1641
-1235
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Controls
5+
{
6+
/// <summary>
7+
/// Defines the IconColorTypes for <see cref="ThemedIcon"/> which sets the visual state
8+
/// to use the correct brush values which match system signal colors.
9+
/// </summary>
10+
public enum ThemedIconColorType
11+
{
12+
None,
13+
14+
/// <summary>
15+
/// Icon color type of <see cref="ThemedIcon"/> is Normal. Default Value.
16+
/// </summary>
17+
Normal,
18+
19+
/// <summary>
20+
/// Icon color type of <see cref="ThemedIcon"/> is Critical.
21+
/// </summary>
22+
Critical,
23+
24+
/// <summary>
25+
/// Icon color type of <see cref="ThemedIcon"/> is Caution.
26+
/// </summary>
27+
Caution,
28+
29+
/// <summary>
30+
/// Icon color type of <see cref="ThemedIcon"/> is Success.
31+
/// </summary>
32+
Success,
33+
34+
/// <summary>
35+
/// Icon color type of <see cref="ThemedIcon"/> is Neutral.
36+
/// </summary>
37+
Neutral,
38+
39+
/// <summary>
40+
/// Icon color type of <see cref="ThemedIcon"/> is Accent.
41+
/// </summary>
42+
Accent,
43+
44+
/// <summary>
45+
/// Icon color type of <see cref="ThemedIcon"/> is Custom. Used in combination
46+
/// with the IconColor and Foreground brushes.
47+
/// </summary>
48+
Custom
49+
}
50+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Controls
5+
{
6+
/// <summary>
7+
/// Defines LayerTypes for <see cref="ThemedIcon"/>.
8+
/// </summary>
9+
public enum ThemedIconLayerType
10+
{
11+
/// <summary>
12+
/// The LayerType uses the Base color. Default state.
13+
/// </summary>
14+
Base,
15+
/// <summary>
16+
/// The LayerType uses the Alt color.
17+
/// </summary>
18+
Alt,
19+
/// <summary>
20+
/// The LayerType uses an Accented color.
21+
/// </summary>
22+
Accent,
23+
/// <summary>
24+
/// The LayerType uses a contrasting color for the Accented color.
25+
/// </summary>
26+
AccentContrast,
27+
}
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Controls
5+
{
6+
/// <summary>
7+
/// A collection of Layers for the ThemedIcon's Layered IconType
8+
/// </summary>
9+
public sealed class ThemedIconLayers : List<ThemedIconLayer>
10+
{
11+
}
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Controls
5+
{
6+
/// <summary>
7+
/// Defines IconTypes for <see cref="ThemedIcon"/>.
8+
/// </summary>
9+
public enum ToggleBehaviors
10+
{
11+
/// <summary>
12+
/// Auto enables the ThemedIcon to listen to owner control states.
13+
/// </summary>
14+
Auto,
15+
16+
/// <summary>
17+
/// On will always use the ThemedIcon's Toggle state
18+
/// </summary>
19+
On,
20+
21+
/// <summary>
22+
/// Off will not use the ThemedIcon's Toggle state
23+
/// </summary>
24+
Off,
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Controls
5+
{
6+
/// <summary>
7+
/// Defines IconTypes for <see cref="ThemedIcon"/>.
8+
/// </summary>
9+
public enum ThemedIconTypes
10+
{
11+
/// <summary>
12+
/// Icon type of <see cref="ThemedIcon"/> is Outline.
13+
/// </summary>
14+
Outline,
15+
16+
/// <summary>
17+
/// Icon type of <see cref="ThemedIcon"/> is Layered.
18+
/// </summary>
19+
Layered,
20+
}
21+
}

src/Files.App.Controls/ThemedIcon/ThemedIcon.Consts.cs

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,65 @@
66

77
namespace Files.App.Controls
88
{
9-
// Template Parts
10-
[TemplatePart(Name = FilledPathIconViewBox, Type = typeof(Viewbox))]
11-
[TemplatePart(Name = OutlinePathIconViewBox, Type = typeof(Viewbox))]
12-
[TemplatePart(Name = LayeredPathIconViewBox, Type = typeof(Viewbox))]
13-
[TemplatePart(Name = LayeredPathCanvas, Type = typeof(Canvas))]
14-
15-
// Icon Type Visual States
16-
[TemplateVisualState(Name = OutlineTypeStateName, GroupName = IconTypeStateGroupName)]
17-
[TemplateVisualState(Name = LayeredTypeStateName, GroupName = IconTypeStateGroupName)]
18-
[TemplateVisualState(Name = FilledTypeStateName, GroupName = IconTypeStateGroupName)]
9+
// Template Parts
10+
[TemplatePart(Name = FilledPathIconViewBox, Type = typeof(Viewbox))]
11+
[TemplatePart(Name = OutlinePathIconViewBox, Type = typeof(Viewbox))]
12+
[TemplatePart(Name = LayeredPathIconViewBox, Type = typeof(Viewbox))]
13+
[TemplatePart(Name = LayeredPathCanvas, Type = typeof(Canvas))]
14+
// Icon Type Visual States
15+
[TemplateVisualState(Name = OutlineTypeStateName, GroupName = IconTypeStateGroupName)]
16+
[TemplateVisualState(Name = LayeredTypeStateName, GroupName = IconTypeStateGroupName)]
17+
[TemplateVisualState(Name = FilledTypeStateName, GroupName = IconTypeStateGroupName)]
18+
// Icon Color Visual States
19+
[TemplateVisualState(Name = NormalStateName, GroupName = IconColorStateGroupName)]
20+
[TemplateVisualState(Name = CriticalStateName, GroupName = IconColorStateGroupName)]
21+
[TemplateVisualState(Name = CautionStateName, GroupName = IconColorStateGroupName)]
22+
[TemplateVisualState(Name = SuccessStateName, GroupName = IconColorStateGroupName)]
23+
[TemplateVisualState(Name = NeutralStateName, GroupName = IconColorStateGroupName)]
24+
[TemplateVisualState(Name = AccentStateName, GroupName = IconColorStateGroupName)]
25+
[TemplateVisualState(Name = CustomColorStateName, GroupName = IconColorStateGroupName)]
26+
[TemplateVisualState(Name = ToggleStateName, GroupName = IconColorStateGroupName)]
27+
[TemplateVisualState(Name = DisabledColorStateName, GroupName = IconColorStateGroupName)]
28+
[TemplateVisualState(Name = DisabledToggleColorStateName, GroupName = IconColorStateGroupName)]
29+
// Icon IsEnabled Visual States
30+
[TemplateVisualState(Name = EnabledStateName, GroupName = EnabledStateGroupName)]
31+
[TemplateVisualState(Name = DisabledStateName, GroupName = EnabledStateGroupName)]
32+
public partial class ThemedIcon
33+
{
34+
// Visual State Group Names
35+
internal const string IconTypeStateGroupName = "IconTypeStates";
36+
internal const string IconColorStateGroupName = "IconColorStates";
37+
internal const string EnabledStateGroupName = "EnabledStates";
1938

20-
// Icon Color Visual States
21-
[TemplateVisualState(Name = NormalStateName, GroupName = IconColorStateGroupName)]
22-
[TemplateVisualState(Name = CriticalStateName, GroupName = IconColorStateGroupName)]
23-
[TemplateVisualState(Name = CautionStateName, GroupName = IconColorStateGroupName)]
24-
[TemplateVisualState(Name = SuccessStateName, GroupName = IconColorStateGroupName)]
25-
[TemplateVisualState(Name = NeutralStateName, GroupName = IconColorStateGroupName)]
26-
[TemplateVisualState(Name = AccentStateName, GroupName = IconColorStateGroupName)]
27-
[TemplateVisualState(Name = CustomColorStateName, GroupName = IconColorStateGroupName)]
28-
[TemplateVisualState(Name = ToggleStateName, GroupName = IconColorStateGroupName)]
29-
[TemplateVisualState(Name = DisabledColorStateName, GroupName = IconColorStateGroupName)]
30-
[TemplateVisualState(Name = DisabledToggleColorStateName, GroupName = IconColorStateGroupName)]
39+
// "Icon Type" Visual State Names
40+
internal const string OutlineTypeStateName = "Outline";
41+
internal const string FilledTypeStateName = "Filled";
42+
internal const string LayeredTypeStateName = "Layered";
3143

32-
// Icon IsEnabled Visual States
33-
[TemplateVisualState(Name = EnabledStateName, GroupName = EnabledStateGroupName)]
34-
[TemplateVisualState(Name = DisabledStateName, GroupName = EnabledStateGroupName)]
44+
// "Icon State" Visual State Names
45+
internal const string NormalStateName = "Normal";
46+
internal const string CriticalStateName = "Critical";
47+
internal const string CautionStateName = "Caution";
48+
internal const string SuccessStateName = "Success";
49+
internal const string NeutralStateName = "Neutral";
50+
internal const string AccentStateName = "Accent";
51+
internal const string CustomColorStateName = "Custom";
52+
internal const string ToggleStateName = "Toggle";
53+
internal const string DisabledColorStateName = "DisabledColor";
54+
internal const string DisabledToggleColorStateName = "DisabledToggleColor";
3555

36-
public partial class ThemedIcon
37-
{
38-
// Visual State Group Names
39-
internal const string IconTypeStateGroupName = "IconTypeStates";
40-
internal const string IconColorStateGroupName = "IconColorStates";
41-
internal const string EnabledStateGroupName = "EnabledStates";
56+
// "Enabled" Visual State Names
57+
internal const string EnabledStateName = "Enabled";
58+
internal const string DisabledStateName = "Disabled";
4259

43-
// "Icon Type" Visual State Names
44-
internal const string OutlineTypeStateName = "Outline";
45-
internal const string FilledTypeStateName = "Filled";
46-
internal const string LayeredTypeStateName = "Layered";
60+
// ViewBox Controls
61+
internal const string FilledPathIconViewBox = "PART_FilledIconViewBox";
62+
internal const string OutlinePathIconViewBox = "PART_OutlineIconViewBox";
63+
internal const string LayeredPathIconViewBox = "PART_LayeredIconViewBox";
64+
internal const string LayeredPathCanvas = "PART_LayerCanvas";
4765

48-
// "Icon State" Visual State Names
49-
internal const string NormalStateName = "Normal";
50-
internal const string CriticalStateName = "Critical";
51-
internal const string CautionStateName = "Caution";
52-
internal const string SuccessStateName = "Success";
53-
internal const string NeutralStateName = "Neutral";
54-
internal const string AccentStateName = "Accent";
55-
internal const string CustomColorStateName = "Custom";
56-
internal const string ToggleStateName = "Toggle";
57-
internal const string DisabledColorStateName = "DisabledColor";
58-
internal const string DisabledToggleColorStateName = "DisabledToggleColor";
59-
60-
// "Enabled" Visual State Names
61-
internal const string EnabledStateName = "Enabled";
62-
internal const string DisabledStateName = "Disabled";
63-
64-
// ViewBox Controls
65-
internal const string FilledPathIconViewBox = "PART_FilledIconViewBox";
66-
internal const string OutlinePathIconViewBox = "PART_OutlineIconViewBox";
67-
internal const string LayeredPathIconViewBox = "PART_LayeredIconViewBox";
68-
internal const string LayeredPathCanvas = "PART_LayerCanvas";
69-
70-
// Path Controls
71-
internal const string FilledIconPath = "PART_FilledPath";
72-
internal const string OutlineIconPath = "PART_OutlinePath";
73-
}
74-
}
66+
// Path Controls
67+
internal const string FilledIconPath = "PART_FilledPath";
68+
internal const string OutlineIconPath = "PART_OutlinePath";
69+
}
70+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using CommunityToolkit.WinUI.UI;
5+
using Microsoft.UI.Xaml;
6+
using Microsoft.UI.Xaml.Controls;
7+
using Microsoft.UI.Xaml.Controls.Primitives;
8+
9+
namespace Files.App.Controls
10+
{
11+
public partial class ThemedIcon
12+
{
13+
private bool _isOwnerToggled;
14+
private bool _isOwnerEnabled;
15+
16+
private Control? ownerControl = null;
17+
private ToggleButton? ownerToggleButton = null;
18+
19+
private void FindOwnerControlStates()
20+
{
21+
if (this.FindAscendant<ToggleButton>() is ToggleButton toggleButton)
22+
{
23+
ownerToggleButton = toggleButton;
24+
25+
// IsChecked/IsToggled change aware
26+
ownerToggleButton.Checked += OwnerControl_IsCheckedChanged;
27+
ownerToggleButton.Unchecked += OwnerControl_IsCheckedChanged;
28+
_isOwnerToggled = ownerToggleButton.IsChecked is true;
29+
}
30+
31+
if (this.FindAscendant<Control>() is Control control)
32+
{
33+
ownerControl = control;
34+
35+
// IsEnabled change aware
36+
ownerControl.IsEnabledChanged += OwnerControl_IsEnabledChanged;
37+
_isOwnerEnabled = ownerControl.IsEnabled;
38+
39+
UpdateVisualStates();
40+
}
41+
}
42+
43+
private void OwnerControl_IsCheckedChanged(object sender, RoutedEventArgs e)
44+
{
45+
if (ownerToggleButton is null)
46+
return;
47+
48+
_isOwnerToggled = ownerToggleButton.IsChecked is true;
49+
UpdateVisualStates();
50+
51+
}
52+
53+
private void OwnerControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
54+
{
55+
if (ownerControl is null)
56+
return;
57+
58+
_isOwnerEnabled = ownerControl.IsEnabled;
59+
UpdateVisualStates();
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)