Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MacOS/iOS] Fixed incorrect color of RadioButton in Dark Mode using Default Control Template. #13215

Merged
merged 16 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
c86a07a
Moved initialization of radioButtonThemeColor and radioButtonCheckMar…
dustin-wojciechowski Feb 8, 2023
dbbc6dd
Added test CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes
dustin-wojciechowski Feb 8, 2023
9fbf597
Added RadioButton changes to Tizen version. Redid test to use casting.
dustin-wojciechowski Feb 9, 2023
1144d01
Added AppThemeBinding for light/dark modes to VisualStateGroups for R…
dustin-wojciechowski Feb 9, 2023
ec43e04
Fix for Graphics.Colors in RadioButton.Tizen
dustin-wojciechowski Feb 9, 2023
a7fe489
Merge branch 'main' into dev/fix-radio-button-in-dark-mode
dustin-wojciechowski Mar 6, 2023
6523d0e
Auto-format source code
Mar 6, 2023
44ad5e2
Addressed PR comments. Made changes to Tizen version as well.
dustin-wojciechowski Mar 7, 2023
bd08452
Auto-format source code
Mar 7, 2023
29b1029
Made strings constant
dustin-wojciechowski Mar 7, 2023
b4b4c2d
Created new EllipseExtensions class, added methods for TrySetAppTheme…
dustin-wojciechowski Mar 8, 2023
1af3bb6
Removed EllipseExtensions class and updated unshipped api files
dustin-wojciechowski Mar 9, 2023
26e86f5
Added TrySetAppTheme and TrySetDynamicTheme to BindableObjectExtensio…
dustin-wojciechowski Mar 9, 2023
a3a79d1
Changed TrySetAppTheme and TrySetDynamicTheme to internal, removed re…
dustin-wojciechowski Mar 9, 2023
de39082
Update RadioButton.Tizen to match new changes to BuildDefaultTemplate…
dustin-wojciechowski Mar 10, 2023
1b3e834
Updated test to use Border instead of Frame
dustin-wojciechowski Mar 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable disable
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui.Controls
{
Expand Down Expand Up @@ -54,13 +55,11 @@ static View BuildTizenDefaultTemplate()
HeightRequest = 21,
WidthRequest = 21,
StrokeThickness = 2,
Stroke = RadioButtonThemeColor,
InputTransparent = true
};

var checkMark = new Ellipse
{
Fill = RadioButtonCheckMarkThemeColor,
Aspect = Stretch.Uniform,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Expand All @@ -76,6 +75,55 @@ static View BuildTizenDefaultTemplate()
VerticalOptions = LayoutOptions.Center
};

object outerEllipseVisualStateLight;
object outerEllipseVisualStateDark;
object checkMarkVisualStateLight;
object checkMarkVisualStateDark;

// First, Light/Dark App themes for outer ellipse, then for the check mark.
// Then check for older themecolor.
// If nothing set, use the defaults for light/dark mode.
if (Application.Current.TryGetResource("RadioButtonOuterEllipseStrokeLight", out var outerLight) &&
Application.Current.TryGetResource("RadioButtonOuterEllipseStrokeDark", out var outerDark))
dustin-wojciechowski marked this conversation as resolved.
Show resolved Hide resolved
{
normalEllipse.SetAppTheme(Ellipse.StrokeProperty, outerLight, outerDark);
outerEllipseVisualStateLight = outerLight;
outerEllipseVisualStateDark = outerDark;
}
else if (Application.Current.TryGetResource("RadioButtonThemeColor", out var themeColor))
{
normalEllipse.SetDynamicResource(Ellipse.StrokeProperty, "RadioButtonThemeColor");
outerEllipseVisualStateLight = outerEllipseVisualStateDark = themeColor;
}
else
{
normalEllipse.SetAppTheme(Ellipse.StrokeProperty, SolidColorBrush.Black, SolidColorBrush.White);
outerEllipseVisualStateLight = SolidColorBrush.Black;
outerEllipseVisualStateDark = SolidColorBrush.White;
}

if (Application.Current.TryGetResource("RadioButtonCheckGlyphStrokeLight", out var checkLight) &&
Application.Current.TryGetResource("RadioButtonCheckGlyphStrokeDark", out var checkDark))
{
checkMark.SetAppTheme(Ellipse.StrokeProperty, checkLight, checkDark);
checkMark.Fill = (Brush)(Application.Current?.RequestedTheme == AppTheme.Dark ? checkDark : checkLight);
dustin-wojciechowski marked this conversation as resolved.
Show resolved Hide resolved
checkMarkVisualStateLight = checkLight;
checkMarkVisualStateDark = checkDark;
}
else if (Application.Current.TryGetResource("RadioButtonCheckMarkThemeColor", out var themeColor))
{
checkMark.SetDynamicResource(Ellipse.StrokeProperty, "RadioButtonCheckMarkThemeColor");
checkMark.Fill = (Brush)themeColor;
checkMarkVisualStateLight = checkMarkVisualStateDark = themeColor;
}
else
{
checkMark.SetAppTheme(Ellipse.StrokeProperty, SolidColorBrush.Black, SolidColorBrush.White);
checkMark.Fill = (Application.Current?.RequestedTheme == AppTheme.Dark) ? SolidColorBrush.White : SolidColorBrush.Black;
checkMarkVisualStateLight = SolidColorBrush.Black;
checkMarkVisualStateDark = SolidColorBrush.White;
}

contentPresenter.SetBinding(BackgroundColorProperty, new Binding(BackgroundColorProperty.PropertyName,
source: RelativeBindingSource.TemplatedParent));

Expand Down Expand Up @@ -104,12 +152,31 @@ static View BuildTizenDefaultTemplate()

VisualState checkedVisualState = new VisualState() { Name = CheckedVisualState };
checkedVisualState.Setters.Add(new Setter() { Property = OpacityProperty, TargetName = CheckedIndicator, Value = 1 });
checkedVisualState.Setters.Add(new Setter() { Property = Shape.StrokeProperty, TargetName = UncheckedButton, Value = RadioButtonCheckMarkThemeColor });
checkedVisualState.Setters.Add(
new Setter()
{
Property = Shape.StrokeProperty,
TargetName = UncheckedButton,
Value = new AppThemeBinding() { Light = outerEllipseVisualStateLight, Dark = outerEllipseVisualStateDark }
});
checkedVisualState.Setters.Add(
new Setter()
{
Property = Shape.StrokeProperty,
TargetName = CheckedIndicator,
Value = new AppThemeBinding() { Light = checkMarkVisualStateLight, Dark = checkMarkVisualStateDark }
});
checkedStates.States.Add(checkedVisualState);

VisualState uncheckedVisualState = new VisualState() { Name = UncheckedVisualState };
uncheckedVisualState.Setters.Add(new Setter() { Property = OpacityProperty, TargetName = CheckedIndicator, Value = 0 });
uncheckedVisualState.Setters.Add(new Setter() { Property = Shape.StrokeProperty, TargetName = UncheckedButton, Value = RadioButtonThemeColor });
uncheckedVisualState.Setters.Add(
new Setter()
{
Property = Shape.StrokeProperty,
TargetName = UncheckedButton,
Value = new AppThemeBinding() { Light = outerEllipseVisualStateLight, Dark = outerEllipseVisualStateDark }
});
checkedStates.States.Add(uncheckedVisualState);

visualStateGroups.Add(checkedStates);
Expand Down
78 changes: 72 additions & 6 deletions src/Controls/src/Core/RadioButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public partial class RadioButton : TemplatedView, IElementConfiguration<RadioBut
TapGestureRecognizer _tapGestureRecognizer;
View _templateRoot;

static readonly Brush RadioButtonCheckMarkThemeColor = ResolveThemeColor("RadioButtonCheckMarkThemeColor");
static readonly Brush RadioButtonThemeColor = ResolveThemeColor("RadioButtonThemeColor");
dustin-wojciechowski marked this conversation as resolved.
Show resolved Hide resolved
static ControlTemplate s_defaultTemplate;

readonly Lazy<PlatformConfigurationRegistry<RadioButton>> _platformConfigurationRegistry;
Expand Down Expand Up @@ -504,13 +502,11 @@ static View BuildDefaultTemplate()
HeightRequest = 21,
WidthRequest = 21,
StrokeThickness = 2,
Stroke = RadioButtonThemeColor,
InputTransparent = true
};

var checkMark = new Ellipse
{
Fill = RadioButtonCheckMarkThemeColor,
Aspect = Stretch.Uniform,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Expand All @@ -526,6 +522,55 @@ static View BuildDefaultTemplate()
VerticalOptions = LayoutOptions.Fill
};

object outerEllipseVisualStateLight;
object outerEllipseVisualStateDark;
object checkMarkVisualStateLight;
object checkMarkVisualStateDark;

// First, Light/Dark App themes for outer ellipse, then for the check mark.
// Then check for older themecolor.
// If nothing set, use the defaults for light/dark mode.
if (Application.Current.TryGetResource("RadioButtonOuterEllipseStrokeLight", out var outerLight) &&
dustin-wojciechowski marked this conversation as resolved.
Show resolved Hide resolved
Application.Current.TryGetResource("RadioButtonOuterEllipseStrokeDark", out var outerDark))
{
normalEllipse.SetAppTheme(Ellipse.StrokeProperty, outerLight, outerDark);
outerEllipseVisualStateLight = outerLight;
outerEllipseVisualStateDark = outerDark;
}
else if (Application.Current.TryGetResource("RadioButtonThemeColor", out var themeColor))
{
normalEllipse.SetDynamicResource(Ellipse.StrokeProperty, "RadioButtonThemeColor");
outerEllipseVisualStateLight = outerEllipseVisualStateDark = themeColor;
}
else
{
normalEllipse.SetAppTheme(Ellipse.StrokeProperty, SolidColorBrush.Black, SolidColorBrush.White);
outerEllipseVisualStateLight = SolidColorBrush.Black;
outerEllipseVisualStateDark = SolidColorBrush.White;
}

if (Application.Current.TryGetResource("RadioButtonCheckGlyphStrokeLight", out var checkLight) &&
Application.Current.TryGetResource("RadioButtonCheckGlyphStrokeDark", out var checkDark))
{
checkMark.SetAppTheme(Ellipse.StrokeProperty, checkLight, checkDark);
checkMark.Fill = (Brush)(Application.Current?.RequestedTheme == AppTheme.Dark ? checkDark : checkLight);
checkMarkVisualStateLight = checkLight;
checkMarkVisualStateDark = checkDark;
}
else if (Application.Current.TryGetResource("RadioButtonCheckMarkThemeColor", out var themeColor))
{
checkMark.SetDynamicResource(Ellipse.StrokeProperty, "RadioButtonCheckMarkThemeColor");
checkMark.Fill = (Brush)themeColor;
checkMarkVisualStateLight = checkMarkVisualStateDark = themeColor;
}
else
{
checkMark.SetAppTheme(Ellipse.StrokeProperty, SolidColorBrush.Black, SolidColorBrush.White);
checkMark.Fill = (Application.Current?.RequestedTheme == AppTheme.Dark) ? SolidColorBrush.White : SolidColorBrush.Black;
checkMarkVisualStateLight = SolidColorBrush.Black;
checkMarkVisualStateDark = SolidColorBrush.White;
}

contentPresenter.SetBinding(MarginProperty, new Binding("Padding", source: RelativeBindingSource.TemplatedParent));
contentPresenter.SetBinding(BackgroundColorProperty, new Binding(BackgroundColorProperty.PropertyName,
source: RelativeBindingSource.TemplatedParent));
Expand Down Expand Up @@ -555,12 +600,33 @@ static View BuildDefaultTemplate()

VisualState checkedVisualState = new VisualState() { Name = CheckedVisualState };
checkedVisualState.Setters.Add(new Setter() { Property = OpacityProperty, TargetName = CheckedIndicator, Value = 1 });
checkedVisualState.Setters.Add(new Setter() { Property = Shape.StrokeProperty, TargetName = UncheckedButton, Value = RadioButtonCheckMarkThemeColor });
checkedVisualState.Setters.Add(
new Setter()
{
Property = Shape.StrokeProperty,
TargetName = UncheckedButton,
Value = new AppThemeBinding() { Light = outerEllipseVisualStateLight, Dark = outerEllipseVisualStateDark }
});
checkedVisualState.Setters.Add(
new Setter()
{
Property = Shape.StrokeProperty,
TargetName = CheckedIndicator,
Value = new AppThemeBinding() { Light = checkMarkVisualStateLight, Dark = checkMarkVisualStateDark }
});
checkedStates.States.Add(checkedVisualState);

VisualState uncheckedVisualState = new VisualState() { Name = UncheckedVisualState };
uncheckedVisualState.Setters.Add(new Setter() { Property = OpacityProperty, TargetName = CheckedIndicator, Value = 0 });
uncheckedVisualState.Setters.Add(new Setter() { Property = Shape.StrokeProperty, TargetName = UncheckedButton, Value = RadioButtonThemeColor });

uncheckedVisualState.Setters.Add(
new Setter()
{
Property = Shape.StrokeProperty,
TargetName = UncheckedButton,
Value = new AppThemeBinding() { Light = outerEllipseVisualStateLight, Dark = outerEllipseVisualStateDark }
});

checkedStates.States.Add(uncheckedVisualState);

visualStateGroups.Add(checkedStates);
Expand Down
23 changes: 23 additions & 0 deletions src/Controls/tests/Core.UnitTests/AppThemeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ public void ThemeBindingRemovedOnOneTimeBindablePropertyWhenPropertySet()
Assert.Equal(Colors.Pink, shell.FlyoutBackgroundColor);
}

void validateRadioButtonColors(RadioButton button, SolidColorBrush desiredBrush)
{
var frame = (Frame)button.Children[0];
var grid = (Grid)frame.Children[0];
var outerEllipse = (Shapes.Ellipse)grid.Children[0];
var innerEllipse = (Shapes.Ellipse)grid.Children[1];

Assert.Equal(desiredBrush, outerEllipse.Stroke);
Assert.Equal(desiredBrush, innerEllipse.Fill);
}

[Fact]
public void CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes()
{
validateRadioButtonColors(
new RadioButton() { ControlTemplate = RadioButton.DefaultTemplate },
Brush.Black);
SetAppTheme(AppTheme.Dark);
validateRadioButtonColors(
new RadioButton() { ControlTemplate = RadioButton.DefaultTemplate },
Brush.White);
}

[Fact]
public void NullApplicationCurrentFallsBackToEssentials()
{
Expand Down