Skip to content

Commit fabbf5d

Browse files
mdtauk0x5bfa
authored andcommitted
Changed IsToggled to ToggleBehavior
1 parent 9ecf257 commit fabbf5d

File tree

4 files changed

+663
-82
lines changed

4 files changed

+663
-82
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ public partial class ThemedIcon : Control
7979
// Boolean properties
8080

8181
/// <summary>
82-
/// The backing <see cref="DependencyProperty"/> for the <see cref="IsToggled"/> property.
82+
/// The backing <see cref="DependencyProperty"/> for the <see cref="ToggleBehavior"/> property.
8383
/// </summary>
84-
public static readonly DependencyProperty IsToggledProperty =
84+
public static readonly DependencyProperty ToggleBehaviorProperty =
8585
DependencyProperty.Register(
86-
nameof(IsToggled),
87-
typeof(bool),
86+
nameof(ToggleBehavior),
87+
typeof(ToggleBehaviors),
8888
typeof(ThemedIcon),
89-
new PropertyMetadata(defaultValue: false, (d, e) => ((ThemedIcon)d).OnIsToggledPropertyChanged((bool)e.OldValue, (bool)e.NewValue)));
89+
new PropertyMetadata(defaultValue: ToggleBehaviors.Auto, (d, e) => ((ThemedIcon)d).OnToggleBehaviorPropertyChanged((ToggleBehaviors)e.OldValue, (ToggleBehaviors)e.NewValue)));
9090

9191
/// <summary>
9292
/// The backing <see cref="DependencyProperty"/> for the <see cref="IsFilled"/> property.
@@ -191,10 +191,10 @@ public double IconSize
191191
/// <summary>
192192
/// Gets or sets a value indicating whether the Icon should use Toggled states.
193193
/// </summary>
194-
public bool IsToggled
194+
public ToggleBehaviors ToggleBehavior
195195
{
196-
get => (bool)GetValue(IsToggledProperty);
197-
set => SetValue(IsToggledProperty, value);
196+
get => (ToggleBehaviors)GetValue( ToggleBehaviorProperty );
197+
set => SetValue( ToggleBehaviorProperty , value);
198198
}
199199

200200
/// <summary>
@@ -269,9 +269,9 @@ protected virtual void OnIconSizePropertyChanged(double oldValue, double newValu
269269

270270
// Boolean changed events
271271

272-
protected virtual void OnIsToggledPropertyChanged(bool oldValue, bool newValue)
272+
protected virtual void OnToggleBehaviorPropertyChanged(ToggleBehaviors oldValue , ToggleBehaviors newValue)
273273
{
274-
ToggleChanged(newValue);
274+
ToggleBehaviorChanged(newValue);
275275
}
276276

277277
protected virtual void OnIsFilledPropertyChanged(bool oldValue, bool newValue)

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

Lines changed: 109 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ namespace Files.App.Controls
1919
/// </summary>
2020
public partial class ThemedIcon : Control
2121
{
22-
private bool _isHighContrast;
23-
private bool _isToggled;
24-
private bool _isEnabled;
25-
private bool _isFilled;
26-
private double _iconSize;
27-
28-
private ToggleButton? ownerToggleButton = null;
22+
private bool _isHighContrast;
23+
private ToggleBehaviors _toggleBehavior;
24+
private bool _ownerToggled;
25+
private bool _isEnabled;
26+
private bool _isFilled;
27+
private double _iconSize;
28+
29+
private ToggleButton? ownerToggleButton = null;
2930
private AppBarToggleButton? ownerAppBarToggleButton = null;
30-
private Control? ownerControl = null;
31+
private Control? ownerControl = null;
3132

3233
public ThemedIcon()
3334
{
@@ -152,7 +153,7 @@ private void FindOwnerControlStates()
152153
ownerToggleButton.Checked += OwnerControl_IsCheckedChanged;
153154
ownerToggleButton.Unchecked += OwnerControl_IsCheckedChanged;
154155

155-
ToggleChanged(ownerToggleButton.IsChecked is true);
156+
UpdateOwnerToggle( ownerToggleButton.IsChecked is true);
156157
}
157158

158159
ownerAppBarToggleButton = this.FindAscendant<AppBarToggleButton>();
@@ -162,7 +163,7 @@ private void FindOwnerControlStates()
162163
ownerAppBarToggleButton.Checked += OwnerControl_IsCheckedChanged;
163164
ownerAppBarToggleButton.Unchecked += OwnerControl_IsCheckedChanged;
164165

165-
ToggleChanged(ownerAppBarToggleButton.IsChecked is true);
166+
UpdateOwnerToggle( ownerAppBarToggleButton.IsChecked is true);
166167
}
167168

168169
ownerControl = this.FindAscendant<Control>();
@@ -182,9 +183,9 @@ private void OwnerControl_IsCheckedChanged(object sender, RoutedEventArgs e)
182183
return;
183184

184185
if (ownerToggleButton is not null)
185-
ToggleChanged(ownerToggleButton.IsChecked is true);
186+
UpdateOwnerToggle( ownerToggleButton.IsChecked is true);
186187
else if (ownerAppBarToggleButton is not null)
187-
ToggleChanged(ownerAppBarToggleButton.IsChecked is true);
188+
UpdateOwnerToggle( ownerAppBarToggleButton.IsChecked is true);
188189
}
189190

190191
private void OwnerControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
@@ -196,10 +197,17 @@ private void OwnerControl_IsEnabledChanged(object sender, DependencyPropertyChan
196197
EnabledChanged(ownerControl.IsEnabled);
197198
}
198199

199-
private void ToggleChanged(bool value)
200+
private void ToggleBehaviorChanged(ToggleBehaviors value)
200201
{
201202
// Handles the IsToggled property change
202-
_isToggled = value;
203+
_toggleBehavior = value;
204+
205+
UpdateVisualStates();
206+
}
207+
208+
private void UpdateOwnerToggle(bool isToggled)
209+
{
210+
_ownerToggled = isToggled;
203211

204212
UpdateVisualStates();
205213
}
@@ -256,14 +264,14 @@ private void HighContrastChanged(bool value)
256264
private void InitialIconStateValues()
257265
{
258266
_isEnabled = IsEnabled;
259-
_isToggled = IsToggled;
267+
_toggleBehavior = ToggleBehavior;
260268
_isHighContrast = IsHighContrast;
261269
_iconSize = IconSize;
262270
}
263271

264272
private void UpdateIconStates()
265273
{
266-
ToggleChanged(_isToggled);
274+
ToggleBehaviorChanged(_toggleBehavior);
267275
EnabledChanged(_isEnabled);
268276
HighContrastChanged(_isHighContrast);
269277
}
@@ -277,79 +285,108 @@ private void UpdateVisualStates()
277285

278286
private void UpdateIconTypeStates()
279287
{
280-
/*
281288
// Handles changes to the IconType and setting the correct Visual States.
282289

283-
// Handles the two IconType states, based on the ThemedIcon.IconType value
284-
// as well as states derived from owner controls, and other properties
285-
286-
// We first check for isToggled and isFilled icon types and states
287-
// Then we check for Contrast and Disabled states, to replace Layered with Outline and set EnabledStates
288-
// Finally we assigned Filled and Layered states, and default otherwise to Outline
289-
*/
290-
291-
if (_isToggled is true || IsToggled is true || _isFilled is true || IsFilled is true)
292-
{
293-
VisualStateManager.GoToState(this, FilledTypeStateName, true);
294-
return;
295-
}
296-
else if (_isHighContrast is true || IsHighContrast is true || _isEnabled is false || IsEnabled is false)
297-
{
298-
VisualStateManager.GoToState(this, OutlineTypeStateName, true);
299-
VisualStateManager.GoToState(this, DisabledStateName, true);
300-
return;
301-
}
302-
else
303-
{
304-
if (IconType == ThemedIconTypes.Layered)
305-
{
306-
VisualStateManager.GoToState(this, LayeredTypeStateName, true);
307-
}
308-
else
309-
{
310-
VisualStateManager.GoToState(this, OutlineTypeStateName, true);
311-
}
312-
}
290+
// If ToggleBehavior is Auto, we check for _ownerToggle
291+
if ( _toggleBehavior == ToggleBehaviors.Auto )
292+
{
293+
if ( _ownerToggled is true || _isFilled is true || IsFilled is true )
294+
{
295+
VisualStateManager.GoToState( this , FilledTypeStateName , true );
296+
return;
297+
}
298+
else if ( _isHighContrast is true || IsHighContrast is true || _isEnabled is false || IsEnabled is false )
299+
{
300+
VisualStateManager.GoToState( this , OutlineTypeStateName , true );
301+
VisualStateManager.GoToState( this , DisabledStateName , true );
302+
return;
303+
}
304+
else
305+
{
306+
if ( IconType == ThemedIconTypes.Layered )
307+
{
308+
VisualStateManager.GoToState( this , LayeredTypeStateName , true );
309+
}
310+
else
311+
{
312+
VisualStateManager.GoToState( this , OutlineTypeStateName , true );
313+
}
314+
}
315+
}
316+
// If ToggleBehavior is On, we only go to Filled.
317+
else if ( _toggleBehavior == ToggleBehaviors.On )
318+
{
319+
VisualStateManager.GoToState( this , FilledTypeStateName , true );
320+
}
321+
// For Off, we don't respond to _ownerToggle at all
322+
else
323+
{
324+
if ( _isFilled is true || IsFilled is true )
325+
{
326+
VisualStateManager.GoToState( this , FilledTypeStateName , true );
327+
return;
328+
}
329+
else if ( _isHighContrast is true || IsHighContrast is true || _isEnabled is false || IsEnabled is false )
330+
{
331+
VisualStateManager.GoToState( this , OutlineTypeStateName , true );
332+
VisualStateManager.GoToState( this , DisabledStateName , true );
333+
return;
334+
}
335+
else
336+
{
337+
if ( IconType == ThemedIconTypes.Layered )
338+
{
339+
VisualStateManager.GoToState( this , LayeredTypeStateName , true );
340+
}
341+
else
342+
{
343+
VisualStateManager.GoToState( this , OutlineTypeStateName , true );
344+
}
345+
}
346+
}
313347

314348
VisualStateManager.GoToState(this, EnabledStateName, true);
315349
}
316350

317351
private void UpdateIconColorTypeStates()
318352
{
319-
/*
320353
// Handles changes to the IconColorType and setting the correct Visual States.
321354

322-
// We first check if the Icon is Disabled
323-
// Then we check if the Disabled Icon is Toggled
324-
325-
// We then assume the Icon is Enabled
326-
// We then check the Toggled state for the Contrast Icons
327-
// We have two states depending on toggle.
328-
329-
// Finally we act on all other Enabled states
330-
// We check for Toggled state
331-
// And update the IconColorType in the Layered Icon's Layers
332-
*/
333-
355+
// First we check the enabled state
334356
if (_isEnabled is false || IsEnabled is false)
335357
{
336-
if (_isToggled is true || IsToggled is true)
337-
{
338-
VisualStateManager.GoToState(this, DisabledToggleColorStateName, true);
339-
}
340-
else
341-
{
342-
VisualStateManager.GoToState(this, DisabledColorStateName, true);
343-
}
358+
// If ToggleBehavior is Auto and _ownerToggled is true
359+
if ( _toggleBehavior == ToggleBehaviors.Auto && _ownerToggled is true )
360+
{
361+
VisualStateManager.GoToState( this , DisabledToggleColorStateName , true );
362+
}
363+
// If ToggleBehavior is On
364+
else if ( _toggleBehavior == ToggleBehaviors.On )
365+
{
366+
VisualStateManager.GoToState( this , DisabledToggleColorStateName , true );
367+
}
368+
// everything else uses Disabled Color
369+
else
370+
{
371+
VisualStateManager.GoToState( this , DisabledColorStateName , true );
372+
}
344373
}
374+
// Everything else is Enabled
345375
else
346376
{
347-
if (_isToggled is true || IsToggled is true)
348-
{
377+
// If ToggleBehavior is Auto and _ownerToggled is true
378+
if ( _toggleBehavior == ToggleBehaviors.Auto && _ownerToggled is true )
379+
{
349380
VisualStateManager.GoToState(this, ToggleStateName, true);
350381
}
351-
else
352-
{
382+
// If ToggleBehavior is On
383+
else if ( _toggleBehavior == ToggleBehaviors.On )
384+
{
385+
VisualStateManager.GoToState( this , ToggleStateName , true );
386+
}
387+
// everything else uses the appropriate color state
388+
else
389+
{
353390
VisualStateManager.GoToState(
354391
this,
355392
IconColorType switch
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+
/// Toggle Behavior type of <see cref="ThemedIcon"/> is Auto.
13+
/// </summary>
14+
Auto,
15+
16+
/// <summary>
17+
/// Toggle Behavior type of <see cref="ThemedIcon"/> is On.
18+
/// </summary>
19+
On,
20+
21+
/// <summary>
22+
/// Toggle Behavior type of <see cref="ThemedIcon"/> is Off.
23+
/// </summary>
24+
Off,
25+
}
26+
}

0 commit comments

Comments
 (0)