66using Microsoft . UI . Xaml . Media ;
77using Microsoft . UI . Xaml . Markup ;
88using Microsoft . UI . Xaml . Shapes ;
9- using CommunityToolkit . WinUI . UI ;
10- using Microsoft . UI . Xaml . Controls . Primitives ;
119using System . Linq ;
1210using 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