Skip to content

Commit 64b612f

Browse files
committed
Merge branch 'mdta-ThemedIcon-ToggleFix' of https://github.com/mdtauk/Files into mdta-ThemedIcon-ToggleFix
2 parents 1a1d61b + 7e730c6 commit 64b612f

File tree

3 files changed

+118
-138
lines changed

3 files changed

+118
-138
lines changed
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 Microsoft.UI.Xaml;
5+
using Microsoft.UI.Xaml.Controls;
6+
using CommunityToolkit.WinUI.UI;
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+
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,61 +234,61 @@ public object Layers
234234

235235
protected virtual void OnFilledIconPropertyChanged(string oldValue, string newValue)
236236
{
237-
UpdateFilledIconPath();
237+
OnFilledIconChanged();
238238
}
239239

240240
protected virtual void OnOutlineIconPropertyChanged(string oldValue, string newValue)
241241
{
242-
UpdateOutlineIconPath();
242+
OnOutlineIconChanged();
243243
}
244244

245245
// Color changed events
246246
protected virtual void OnColorPropertyChanged(Brush oldValue, Brush newValue)
247247
{
248-
UpdateIconTypeStates();
248+
OnIconTypeChanged();
249249
}
250250

251251
// Enum changed events
252252

253253
protected virtual void OnIconTypePropertyChanged(ThemedIconTypes oldValue, ThemedIconTypes newValue)
254254
{
255-
UpdateIconTypeStates();
255+
OnIconTypeChanged();
256256
}
257257

258258
protected virtual void OnIconColorTypePropertyChanged(ThemedIconColorType oldValue, ThemedIconColorType newValue)
259259
{
260-
UpdateIconColorTypeStates();
260+
OnIconColorTypeChanged();
261261
}
262262

263263
// Double changed events
264264

265265
protected virtual void OnIconSizePropertyChanged(double oldValue, double newValue)
266266
{
267-
IconSizePropertyChanged();
267+
UpdateVisualStates();
268268
}
269269

270270
// Boolean changed events
271271

272272
protected virtual void OnToggleBehaviorPropertyChanged(ToggleBehaviors oldValue, ToggleBehaviors newValue)
273273
{
274-
ToggleBehaviorChanged();
274+
UpdateVisualStates();
275275
}
276276

277277
protected virtual void OnIsFilledPropertyChanged(bool oldValue, bool newValue)
278278
{
279-
FilledChanged();
279+
UpdateVisualStates();
280280
}
281281

282282
protected virtual void OnIsHighContrastPropertyChanged(bool oldValue, bool newValue)
283283
{
284-
HighContrastChanged();
284+
UpdateVisualStates();
285285
}
286286

287287
// Object changed events
288288

289289
protected virtual void OnLayersPropertyChanged(object oldValue, object newValue)
290290
{
291-
UpdateLayeredIconContent();
291+
OnLayeredIconChanged();
292292
}
293293

294294
#endregion

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

Lines changed: 46 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using Microsoft.UI.Xaml.Media;
77
using Microsoft.UI.Xaml.Markup;
88
using Microsoft.UI.Xaml.Shapes;
9-
using CommunityToolkit.WinUI.UI;
10-
using Microsoft.UI.Xaml.Controls.Primitives;
119
using System.Linq;
1210
using System.Collections.Generic;
1311

@@ -18,12 +16,6 @@ namespace Files.App.Controls
1816
/// </summary>
1917
public partial class ThemedIcon : Control
2018
{
21-
private bool _ownerToggled;
22-
private bool _isOwnerEnabled;
23-
24-
private Control? ownerControl = null;
25-
private ToggleButton? ownerToggleButton = null;
26-
2719
public ThemedIcon()
2820
{
2921
DefaultStyleKey = typeof(ThemedIcon);
@@ -32,30 +24,25 @@ public ThemedIcon()
3224
protected override void OnApplyTemplate()
3325
{
3426
IsEnabledChanged -= OnIsEnabledChanged;
35-
SizeChanged -= OnSizeChanged;
3627

3728
base.OnApplyTemplate();
3829

3930
IsEnabledChanged += OnIsEnabledChanged;
40-
SizeChanged += OnSizeChanged;
4131

4232
_isOwnerEnabled = IsEnabled;
4333

4434
FindOwnerControlStates();
45-
UpdateIconContent();
46-
UpdateIconStates();
47-
UpdateVisualStates();
48-
}
35+
OnFilledIconChanged();
36+
OnOutlineIconChanged();
37+
OnLayeredIconChanged();
4938

50-
private void UpdateIconContent()
51-
{
52-
// Updates PathData and Layers
53-
UpdateFilledIconPath();
54-
UpdateOutlineIconPath();
55-
UpdateLayeredIconContent();
39+
OnIconTypeChanged();
40+
OnIconColorTypeChanged();
5641
}
5742

58-
private void UpdateFilledIconPath()
43+
// Updates paths and layers
44+
45+
private void OnFilledIconChanged()
5946
{
6047
// Updates Filled Icon from Path Data
6148
if (GetTemplateChild(FilledPathIconViewBox) is not Viewbox filledViewBox)
@@ -64,7 +51,7 @@ private void UpdateFilledIconPath()
6451
SetPathData(FilledIconPath, FilledIconData ?? string.Empty, filledViewBox);
6552
}
6653

67-
private void UpdateOutlineIconPath()
54+
private void OnOutlineIconChanged()
6855
{
6956
// Updates Outline Icon from Path Data
7057
if (GetTemplateChild(OutlinePathIconViewBox) is not Viewbox outlineViewBox)
@@ -73,7 +60,7 @@ private void UpdateOutlineIconPath()
7360
SetPathData(OutlineIconPath, OutlineIconData ?? string.Empty, outlineViewBox);
7461
}
7562

76-
private void UpdateLayeredIconContent()
63+
private void OnLayeredIconChanged()
7764
{
7865
// Updates Layered Icon from it's Layers
7966
if (GetTemplateChild(LayeredPathIconViewBox) is not Viewbox layeredViewBox ||
@@ -104,115 +91,16 @@ private void UpdateLayeredIconContent()
10491
}
10592
}
10693

107-
private void SetPathData(string partName, string pathData, FrameworkElement element)
108-
{
109-
// Updates PathData
110-
if (string.IsNullOrEmpty(pathData))
111-
return;
112-
113-
var geometry = (Geometry)XamlReader.Load(
114-
$"<Geometry xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>{pathData}</Geometry>");
94+
// Updates visual states
11595

116-
if (GetTemplateChild(partName) is Path path)
117-
{
118-
path.Data = geometry;
119-
path.Width = IconSize;
120-
path.Height = IconSize;
121-
}
122-
}
123-
124-
private void FindOwnerControlStates()
125-
{
126-
if (this.FindAscendant<ToggleButton>() is ToggleButton toggleButton)
127-
{
128-
ownerToggleButton = toggleButton;
129-
130-
// IsChecked/IsToggled change aware
131-
ownerToggleButton.Checked += OwnerControl_IsCheckedChanged;
132-
ownerToggleButton.Unchecked += OwnerControl_IsCheckedChanged;
133-
_ownerToggled = ownerToggleButton.IsChecked is true;
134-
}
135-
136-
if (this.FindAscendant<Control>() is Control control)
137-
{
138-
ownerControl = control;
139-
140-
// IsEnabled change aware
141-
ownerControl.IsEnabledChanged += OwnerControl_IsEnabledChanged;
142-
_isOwnerEnabled = ownerControl.IsEnabled;
143-
144-
UpdateVisualStates();
145-
}
146-
}
147-
148-
private void OwnerControl_IsCheckedChanged(object sender, RoutedEventArgs e)
149-
{
150-
if (ownerToggleButton is null)
151-
return;
152-
153-
_ownerToggled = ownerToggleButton.IsChecked is true;
154-
UpdateVisualStates();
155-
156-
}
157-
158-
private void OwnerControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
159-
{
160-
if (ownerControl is null)
161-
return;
162-
163-
_isOwnerEnabled = ownerControl.IsEnabled;
164-
UpdateVisualStates();
165-
}
166-
167-
private void ToggleBehaviorChanged()
168-
{
169-
UpdateVisualStates();
170-
}
171-
172-
private void FilledChanged()
173-
{
174-
UpdateVisualStates();
175-
}
176-
177-
private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
178-
{
179-
UpdateVisualStates();
180-
}
181-
182-
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
183-
{
184-
// Handles resizing of child layers when Width and Height properties change
185-
}
186-
187-
private void IconSizePropertyChanged()
188-
{
189-
UpdateVisualStates();
190-
}
191-
192-
private void HighContrastChanged()
193-
{
194-
UpdateVisualStates();
195-
}
196-
197-
private void UpdateIconStates()
198-
{
199-
UpdateVisualStates();
200-
}
201-
202-
private void UpdateVisualStates()
203-
{
204-
UpdateIconTypeStates();
205-
UpdateIconColorTypeStates();
206-
}
207-
208-
private void UpdateIconTypeStates()
96+
private void OnIconTypeChanged()
20997
{
21098
// Handles changes to the IconType and setting the correct Visual States.
21199

212100
// If ToggleBehavior is Auto, we check for _ownerToggle
213101
if (ToggleBehavior == ToggleBehaviors.Auto)
214102
{
215-
if (_ownerToggled is true || IsFilled is true)
103+
if (_isOwnerToggled is true || IsFilled is true)
216104
{
217105
VisualStateManager.GoToState(this, FilledTypeStateName, true);
218106
return;
@@ -262,15 +150,15 @@ private void UpdateIconTypeStates()
262150
VisualStateManager.GoToState(this, EnabledStateName, true);
263151
}
264152

265-
private void UpdateIconColorTypeStates()
153+
private void OnIconColorTypeChanged()
266154
{
267155
// Handles changes to the IconColorType and setting the correct Visual States.
268156

269157
// First we check the enabled state
270158
if (_isOwnerEnabled is false || IsEnabled is false)
271159
{
272160
// If ToggleBehavior is Auto and _ownerToggled is true
273-
if (ToggleBehavior == ToggleBehaviors.Auto && _ownerToggled is true)
161+
if (ToggleBehavior == ToggleBehaviors.Auto && _isOwnerToggled is true)
274162
{
275163
VisualStateManager.GoToState(this, DisabledToggleColorStateName, true);
276164
}
@@ -289,7 +177,7 @@ private void UpdateIconColorTypeStates()
289177
else
290178
{
291179
// If ToggleBehavior is Auto and _ownerToggled is true
292-
if (ToggleBehavior == ToggleBehaviors.Auto && _ownerToggled is true)
180+
if (ToggleBehavior == ToggleBehaviors.Auto && _isOwnerToggled is true)
293181
{
294182
VisualStateManager.GoToState(this, ToggleStateName, true);
295183
}
@@ -323,5 +211,35 @@ private void UpdateIconColorTypeStates()
323211
}
324212
}
325213
}
214+
215+
// Misc
216+
217+
private void SetPathData(string partName, string pathData, FrameworkElement element)
218+
{
219+
// Updates PathData
220+
if (string.IsNullOrEmpty(pathData))
221+
return;
222+
223+
var geometry = (Geometry)XamlReader.Load(
224+
$"<Geometry xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>{pathData}</Geometry>");
225+
226+
if (GetTemplateChild(partName) is Path path)
227+
{
228+
path.Data = geometry;
229+
path.Width = IconSize;
230+
path.Height = IconSize;
231+
}
232+
}
233+
234+
private void UpdateVisualStates()
235+
{
236+
OnIconTypeChanged();
237+
OnIconColorTypeChanged();
238+
}
239+
240+
private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
241+
{
242+
UpdateVisualStates();
243+
}
326244
}
327245
}

0 commit comments

Comments
 (0)