Skip to content

Commit a784bad

Browse files
niels9001AndrewKeepCodingArlodotexe
authored
Lower the opacity of a BitmapIcon when card is Disabled (#732)
* Lower the opacity of a BitmapIcon when card is Disabled * Using VisualStates vs codebehind * Fixes Co-Authored-By: Andrew KeepCoding <andrewkeepcoding@gmail.com> * More tweaks Co-Authored-By: Andrew KeepCoding <andrewkeepcoding@gmail.com> * Update SettingsCard.cs * Update SettingsCard.cs --------- Co-authored-by: Andrew KeepCoding <andrewkeepcoding@gmail.com> Co-authored-by: Arlo <arlo.godfrey@outlook.com>
1 parent a8238f1 commit a784bad

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

components/SettingsControls/src/SettingsCard/SettingsCard.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.ComponentModel.Design;
6-
75
namespace CommunityToolkit.WinUI.Controls;
86

97
/// <summary>
@@ -21,6 +19,9 @@ namespace CommunityToolkit.WinUI.Controls;
2119
[TemplateVisualState(Name = PressedState, GroupName = CommonStates)]
2220
[TemplateVisualState(Name = DisabledState, GroupName = CommonStates)]
2321

22+
[TemplateVisualState(Name = BitmapHeaderIconEnabledState, GroupName = BitmapHeaderIconStates)]
23+
[TemplateVisualState(Name = BitmapHeaderIconDisabledState, GroupName = BitmapHeaderIconStates)]
24+
2425
[TemplateVisualState(Name = RightState, GroupName = ContentAlignmentStates)]
2526
[TemplateVisualState(Name = RightWrappedState, GroupName = ContentAlignmentStates)]
2627
[TemplateVisualState(Name = RightWrappedNoIconState, GroupName = ContentAlignmentStates)]
@@ -38,6 +39,10 @@ public partial class SettingsCard : ButtonBase
3839
internal const string PressedState = "Pressed";
3940
internal const string DisabledState = "Disabled";
4041

42+
internal const string BitmapHeaderIconStates = "BitmapHeaderIconStates";
43+
internal const string BitmapHeaderIconEnabledState = "BitmapHeaderIconEnabled";
44+
internal const string BitmapHeaderIconDisabledState = "BitmapHeaderIconDisabled";
45+
4146
internal const string ContentAlignmentStates = "ContentAlignmentStates";
4247
internal const string RightState = "Right";
4348
internal const string RightWrappedState = "RightWrapped";
@@ -76,7 +81,7 @@ protected override void OnApplyTemplate()
7681
CheckInitialVisualState();
7782
SetAccessibleContentName();
7883
RegisterPropertyChangedCallback(ContentProperty, OnContentChanged);
79-
IsEnabledChanged += OnIsEnabledChanged;
84+
IsEnabledChanged += OnIsEnabledChanged;
8085
}
8186

8287
private void CheckInitialVisualState()
@@ -89,6 +94,8 @@ private void CheckInitialVisualState()
8994
CheckVerticalSpacingState(contentAlignmentStatesGroup.CurrentState);
9095
contentAlignmentStatesGroup.CurrentStateChanged += this.ContentAlignmentStates_Changed;
9196
}
97+
98+
CheckHeaderIconState();
9299
}
93100

94101
// We automatically set the AutomationProperties.Name of the Content if not configured.
@@ -183,7 +190,6 @@ private void Control_PointerCanceled(object sender, PointerRoutedEventArgs e)
183190
/// </summary>
184191
protected override void OnPointerPressed(PointerRoutedEventArgs e)
185192
{
186-
// e.Handled = true;
187193
if (IsClickEnabled)
188194
{
189195
base.OnPointerPressed(e);
@@ -228,6 +234,18 @@ private void OnIsClickEnabledChanged()
228234
private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
229235
{
230236
VisualStateManager.GoToState(this, IsEnabled ? NormalState : DisabledState, true);
237+
238+
CheckHeaderIconState();
239+
}
240+
241+
private void CheckHeaderIconState()
242+
{
243+
// The Disabled visual state will only set the right Foreground brush, but for images we need to lower the opacity so it looks disabled.
244+
245+
if (HeaderIcon is BitmapIcon)
246+
{
247+
VisualStateManager.GoToState(this, IsEnabled ? BitmapHeaderIconEnabledState : BitmapHeaderIconDisabledState, true);
248+
}
231249
}
232250

233251
private void OnActionIconChanged()
@@ -240,7 +258,7 @@ private void OnActionIconChanged()
240258
}
241259
else
242260
{
243-
actionIconPresenter.Visibility =Visibility.Collapsed;
261+
actionIconPresenter.Visibility = Visibility.Collapsed;
244262
}
245263
}
246264
}

components/SettingsControls/src/SettingsCard/SettingsCard.xaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
<x:Double x:Key="SettingsCardVerticalHeaderContentSpacing">8</x:Double>
110110
<x:Double x:Key="SettingsCardWrapThreshold">476</x:Double>
111111
<x:Double x:Key="SettingsCardWrapNoIconThreshold">286</x:Double>
112+
<x:Double x:Key="SettingsCardBitmapIconDisabledOpacity">0.4</x:Double>
112113

113114
<Style BasedOn="{StaticResource DefaultSettingsCardStyle}"
114115
TargetType="local:SettingsCard" />
@@ -294,6 +295,16 @@
294295
</VisualState>
295296
</VisualStateGroup>
296297

298+
<!-- These states are only relevant if a BitmapIcon is used as HeaderIcon or ActionIcon. -->
299+
<VisualStateGroup x:Name="BitmapHeaderIconStates">
300+
<VisualState x:Name="BitmapHeaderIconEnabled" />
301+
<VisualState x:Name="BitmapHeaderIconDisabled">
302+
<VisualState.Setters>
303+
<Setter Target="PART_HeaderIconPresenterHolder.Opacity" Value="{StaticResource SettingsCardBitmapIconDisabledOpacity}" />
304+
</VisualState.Setters>
305+
</VisualState>
306+
</VisualStateGroup>
307+
297308
<VisualStateGroup x:Name="ContentAlignmentStates">
298309
<!-- Default -->
299310
<VisualState x:Name="Right" />

0 commit comments

Comments
 (0)