Skip to content

Commit 1a1d61b

Browse files
committed
Simplified ThemedIcon infra
1 parent 6316fe1 commit 1a1d61b

File tree

1 file changed

+34
-93
lines changed

1 file changed

+34
-93
lines changed

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

Lines changed: 34 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ namespace Files.App.Controls
1919
public partial class ThemedIcon : Control
2020
{
2121
private bool _ownerToggled;
22-
private bool _isEnabled;
22+
private bool _isOwnerEnabled;
2323

24-
private ToggleButton? ownerToggleButton = null;
25-
private AppBarToggleButton? ownerAppBarToggleButton = null;
2624
private Control? ownerControl = null;
25+
private ToggleButton? ownerToggleButton = null;
2726

2827
public ThemedIcon()
2928
{
@@ -40,7 +39,8 @@ protected override void OnApplyTemplate()
4039
IsEnabledChanged += OnIsEnabledChanged;
4140
SizeChanged += OnSizeChanged;
4241

43-
InitialIconStateValues();
42+
_isOwnerEnabled = IsEnabled;
43+
4444
FindOwnerControlStates();
4545
UpdateIconContent();
4646
UpdateIconStates();
@@ -92,8 +92,8 @@ private void UpdateLayeredIconContent()
9292
IconColorType = layer.IconColorType,
9393
PathData = layer.PathData,
9494
Opacity = layer.Opacity,
95-
LayerColor = this.Color,
96-
Foreground = this.Foreground,
95+
LayerColor = Color,
96+
Foreground = Foreground,
9797
HorizontalAlignment = HorizontalAlignment.Stretch,
9898
VerticalAlignment = VerticalAlignment.Stretch,
9999
LayerSize = IconSize,
@@ -123,84 +123,49 @@ private void SetPathData(string partName, string pathData, FrameworkElement elem
123123

124124
private void FindOwnerControlStates()
125125
{
126-
/*
127-
// Finds the owner Control and it's Checked and Enabled state
128-
129-
//
130-
// Check if owner Control is a ToggleButton
131-
// Hooks onto Event handlers when IsChecked and IsUnchecked runs
132-
// Runs the ToggleChanged event, to set initial value, if the ToggleButton's isChecked is true
133-
//
134-
// Check if owner Control is an AppBarToggleButton
135-
// Hooks onto Event handlers when IsChecked and IsUnchecked runs
136-
// Runs the ToggleChanged event, to set initial value, if the AppBarToggleButton's isChecked is true
137-
//
138-
// Gets the owner Control
139-
// Hooks onto Event handlers when IsEnabledChanged runs
140-
// Runs the EnabledChanged event to set initial value
141-
//
142-
*/
143-
144-
ownerToggleButton = this.FindAscendant<ToggleButton>();
145-
146-
if (ownerToggleButton != null)
126+
if (this.FindAscendant<ToggleButton>() is ToggleButton toggleButton)
147127
{
128+
ownerToggleButton = toggleButton;
129+
130+
// IsChecked/IsToggled change aware
148131
ownerToggleButton.Checked += OwnerControl_IsCheckedChanged;
149132
ownerToggleButton.Unchecked += OwnerControl_IsCheckedChanged;
150-
151-
UpdateOwnerToggle(ownerToggleButton.IsChecked is true);
133+
_ownerToggled = ownerToggleButton.IsChecked is true;
152134
}
153135

154-
ownerAppBarToggleButton = this.FindAscendant<AppBarToggleButton>();
155-
156-
if (ownerAppBarToggleButton != null)
136+
if (this.FindAscendant<Control>() is Control control)
157137
{
158-
ownerAppBarToggleButton.Checked += OwnerControl_IsCheckedChanged;
159-
ownerAppBarToggleButton.Unchecked += OwnerControl_IsCheckedChanged;
160-
161-
UpdateOwnerToggle(ownerAppBarToggleButton.IsChecked is true);
162-
}
138+
ownerControl = control;
163139

164-
ownerControl = this.FindAscendant<Control>();
165-
166-
if (ownerControl != null)
167-
{
140+
// IsEnabled change aware
168141
ownerControl.IsEnabledChanged += OwnerControl_IsEnabledChanged;
142+
_isOwnerEnabled = ownerControl.IsEnabled;
169143

170-
EnabledChanged(ownerControl.IsEnabled);
144+
UpdateVisualStates();
171145
}
172146
}
173147

174148
private void OwnerControl_IsCheckedChanged(object sender, RoutedEventArgs e)
175149
{
176-
// Responds to owner checked changes
177-
if (ownerToggleButton is null && ownerAppBarToggleButton is null)
150+
if (ownerToggleButton is null)
178151
return;
179152

180-
if (ownerToggleButton is not null)
181-
UpdateOwnerToggle(ownerToggleButton.IsChecked is true);
182-
else if (ownerAppBarToggleButton is not null)
183-
UpdateOwnerToggle(ownerAppBarToggleButton.IsChecked is true);
153+
_ownerToggled = ownerToggleButton.IsChecked is true;
154+
UpdateVisualStates();
155+
184156
}
185157

186158
private void OwnerControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
187159
{
188-
// Responds to owner control enabled changes
189160
if (ownerControl is null)
190161
return;
191162

192-
EnabledChanged(ownerControl.IsEnabled);
193-
}
194-
195-
private void ToggleBehaviorChanged()
196-
{
163+
_isOwnerEnabled = ownerControl.IsEnabled;
197164
UpdateVisualStates();
198165
}
199166

200-
private void UpdateOwnerToggle(bool isToggled)
167+
private void ToggleBehaviorChanged()
201168
{
202-
_ownerToggled = isToggled;
203-
204169
UpdateVisualStates();
205170
}
206171

@@ -211,8 +176,7 @@ private void FilledChanged()
211176

212177
private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
213178
{
214-
// Handles for the derived control's IsEnabled property change
215-
EnabledChanged((bool)e.NewValue);
179+
UpdateVisualStates();
216180
}
217181

218182
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
@@ -225,28 +189,13 @@ private void IconSizePropertyChanged()
225189
UpdateVisualStates();
226190
}
227191

228-
private void EnabledChanged(bool value)
229-
{
230-
// Handles the IsEnabled property change
231-
_isEnabled = value;
232-
233-
UpdateVisualStates();
234-
}
235-
236192
private void HighContrastChanged()
237193
{
238194
UpdateVisualStates();
239195
}
240196

241-
private void InitialIconStateValues()
242-
{
243-
_isEnabled = IsEnabled;
244-
}
245-
246197
private void UpdateIconStates()
247198
{
248-
EnabledChanged(_isEnabled);
249-
250199
UpdateVisualStates();
251200
}
252201

@@ -268,22 +217,18 @@ private void UpdateIconTypeStates()
268217
VisualStateManager.GoToState(this, FilledTypeStateName, true);
269218
return;
270219
}
271-
else if (IsHighContrast is true || _isEnabled is false || IsEnabled is false)
220+
else if (IsHighContrast is true || _isOwnerEnabled is false || IsEnabled is false)
272221
{
273222
VisualStateManager.GoToState(this, OutlineTypeStateName, true);
274223
VisualStateManager.GoToState(this, DisabledStateName, true);
275224
return;
276225
}
277226
else
278227
{
279-
if (IconType == ThemedIconTypes.Layered)
280-
{
281-
VisualStateManager.GoToState(this, LayeredTypeStateName, true);
282-
}
283-
else
284-
{
285-
VisualStateManager.GoToState(this, OutlineTypeStateName, true);
286-
}
228+
VisualStateManager.GoToState(
229+
this,
230+
IconType is ThemedIconTypes.Layered ? LayeredTypeStateName : OutlineTypeStateName,
231+
true);
287232
}
288233
}
289234
// If ToggleBehavior is On, we only go to Filled.
@@ -299,22 +244,18 @@ private void UpdateIconTypeStates()
299244
VisualStateManager.GoToState(this, FilledTypeStateName, true);
300245
return;
301246
}
302-
else if (IsHighContrast is true || _isEnabled is false || IsEnabled is false)
247+
else if (IsHighContrast is true || _isOwnerEnabled is false || IsEnabled is false)
303248
{
304249
VisualStateManager.GoToState(this, OutlineTypeStateName, true);
305250
VisualStateManager.GoToState(this, DisabledStateName, true);
306251
return;
307252
}
308253
else
309254
{
310-
if (IconType == ThemedIconTypes.Layered)
311-
{
312-
VisualStateManager.GoToState(this, LayeredTypeStateName, true);
313-
}
314-
else
315-
{
316-
VisualStateManager.GoToState(this, OutlineTypeStateName, true);
317-
}
255+
VisualStateManager.GoToState(
256+
this,
257+
IconType is ThemedIconTypes.Layered ? LayeredTypeStateName : OutlineTypeStateName,
258+
true);
318259
}
319260
}
320261

@@ -326,7 +267,7 @@ private void UpdateIconColorTypeStates()
326267
// Handles changes to the IconColorType and setting the correct Visual States.
327268

328269
// First we check the enabled state
329-
if (_isEnabled is false || IsEnabled is false)
270+
if (_isOwnerEnabled is false || IsEnabled is false)
330271
{
331272
// If ToggleBehavior is Auto and _ownerToggled is true
332273
if (ToggleBehavior == ToggleBehaviors.Auto && _ownerToggled is true)

0 commit comments

Comments
 (0)