diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/VerifySliderColors.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/VerifySliderColors.png new file mode 100644 index 000000000000..af84ad1c4029 Binary files /dev/null and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/VerifySliderColors.png differ diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue25921.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue25921.cs new file mode 100644 index 000000000000..5d79bbc8de17 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue25921.cs @@ -0,0 +1,46 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 25921, "[Windows] Setting BackgroundColor for Slider updates the Maximum Track Color", PlatformAffected.UWP)] +public class Issue25921 : ContentPage +{ + public Issue25921() + { + var layout = new VerticalStackLayout(); + layout.VerticalOptions = LayoutOptions.Center; + layout.Spacing = 20; + var slider = new Slider() + { + WidthRequest = 300, + Maximum = 100, + Minimum = 0, + Value = 50, + BackgroundColor = Colors.Yellow, + MaximumTrackColor = Colors.Red, + MinimumTrackColor = Colors.Fuchsia, + }; + + var secondSlider = new Slider() + { + WidthRequest = 300, + Maximum = 100, + Minimum = 0, + Value = 50, + Background = Colors.Grey, + MaximumTrackColor = Colors.SpringGreen, + MinimumTrackColor = Colors.BlueViolet, + }; + + var button = new Button() + { + Text = "Change Background Color", + AutomationId = "ColorChangeButton", + Command = new Command(() => secondSlider.Background = Colors.Salmon) + }; + + layout.Children.Add(slider); + layout.Children.Add(secondSlider); + layout.Children.Add(button); + + Content = layout; + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/VerifySliderColors.png b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/VerifySliderColors.png new file mode 100644 index 000000000000..decea37521e9 Binary files /dev/null and b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/VerifySliderColors.png differ diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Bugzilla/Bugzilla29128.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Bugzilla/Bugzilla29128.cs index 753309906b34..e610d5af8e17 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Bugzilla/Bugzilla29128.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Bugzilla/Bugzilla29128.cs @@ -1,6 +1,4 @@ -#if TEST_FAILS_ON_WINDOWS //Background Color updates on Slider Track -//For more information : https://github.com/dotnet/maui/issues/25921 -using NUnit.Framework; +using NUnit.Framework; using UITest.Appium; using UITest.Core; @@ -23,5 +21,4 @@ public void Bugzilla29128Test() VerifyScreenshot(); } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue25921.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue25921.cs new file mode 100644 index 000000000000..1f3b03f0945d --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue25921.cs @@ -0,0 +1,23 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue25921 : _IssuesUITest +{ + public override string Issue => "[Windows] Setting BackgroundColor for Slider updates the Maximum Track Color"; + + public Issue25921(TestDevice device) + : base(device) + { } + + [Test] + [Category(UITestCategories.Slider)] + public void VerifySliderColors() + { + App.WaitForElement("ColorChangeButton"); + App.Tap("ColorChangeButton"); + VerifyScreenshot(); + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Bugzilla29128Test.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Bugzilla29128Test.png new file mode 100644 index 000000000000..f18c25a56959 Binary files /dev/null and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Bugzilla29128Test.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_ChangeBackgroundColor_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_ChangeBackgroundColor_VerifyVisualState.png index b7aae5414d0c..bcf19d4efe21 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_ChangeBackgroundColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_ChangeBackgroundColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndIsEnable_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndIsEnable_VerifyVisualState.png index 22b085a5623e..4e7e6521909d 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndIsEnable_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndIsEnable_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndMaxTrackColor_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndMaxTrackColor_VerifyVisualState.png index cbbad1db8895..8a19b8c046a7 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndMaxTrackColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndMaxTrackColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndMinTrackColor_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndMinTrackColor_VerifyVisualState.png index 04b8c89d09a3..853a3c2305dd 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndMinTrackColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndMinTrackColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndThumbColor_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndThumbColor_VerifyVisualState.png index 31328881a028..0323c2b60142 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndThumbColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetBackgroundColorAndThumbColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetIsEnableAndBackgroundColor_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetIsEnableAndBackgroundColor_VerifyVisualState.png index dcb384caf7a0..7959f5202a80 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetIsEnableAndBackgroundColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetIsEnableAndBackgroundColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetIsVisibleAndBackgroundColor_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetIsVisibleAndBackgroundColor_VerifyVisualState.png index f92de483c0ce..acd328e6ee06 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetIsVisibleAndBackgroundColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetIsVisibleAndBackgroundColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetMaxTrackAndBackgroundColor_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetMaxTrackAndBackgroundColor_VerifyVisualState.png index aeff07f752f1..5b735bf4a4de 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetMaxTrackAndBackgroundColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetMaxTrackAndBackgroundColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetMinTrackAndBackgroundColor_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetMinTrackAndBackgroundColor_VerifyVisualState.png index 0fc9de30888c..5835d8467181 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetMinTrackAndBackgroundColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetMinTrackAndBackgroundColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetThumbAndBackgroundColor_VerifyVisualState.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetThumbAndBackgroundColor_VerifyVisualState.png index 4b109eab8915..cc917addf07d 100644 Binary files a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetThumbAndBackgroundColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Slider_SetThumbAndBackgroundColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/VerifySliderColors.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/VerifySliderColors.png new file mode 100644 index 000000000000..0754c4ea2ebf Binary files /dev/null and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/VerifySliderColors.png differ diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/VerifySliderColors.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/VerifySliderColors.png new file mode 100644 index 000000000000..5aa3943a1ff3 Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/VerifySliderColors.png differ diff --git a/src/Core/src/Handlers/Slider/SliderHandler.Windows.cs b/src/Core/src/Handlers/Slider/SliderHandler.Windows.cs index ba2111a38d16..d82162a8972c 100644 --- a/src/Core/src/Handlers/Slider/SliderHandler.Windows.cs +++ b/src/Core/src/Handlers/Slider/SliderHandler.Windows.cs @@ -91,6 +91,14 @@ public static void MapThumbImageSource(ISliderHandler handler, ISlider slider) } } + internal static void MapBackgroundColor(ISliderHandler handler, ISlider slider) + { + if (handler.PlatformView is MauiSlider mauiSlider) + { + mauiSlider.UpdateBackgroundColor(slider); + } + } + void OnPlatformViewLoaded(object sender, RoutedEventArgs e) { var platformView = sender as Slider; diff --git a/src/Core/src/Handlers/Slider/SliderHandler.cs b/src/Core/src/Handlers/Slider/SliderHandler.cs index 096dd3f95f34..e9465b5232f5 100644 --- a/src/Core/src/Handlers/Slider/SliderHandler.cs +++ b/src/Core/src/Handlers/Slider/SliderHandler.cs @@ -24,6 +24,9 @@ public partial class SliderHandler : ISliderHandler [nameof(ISlider.ThumbColor)] = MapThumbColor, [nameof(ISlider.ThumbImageSource)] = MapThumbImageSource, [nameof(ISlider.Value)] = MapValue, +#if WINDOWS + [nameof(ISlider.Background)] = MapBackgroundColor +#endif }; public static CommandMapper CommandMapper = new(ViewCommandMapper) diff --git a/src/Core/src/Platform/Windows/SliderExtensions.cs b/src/Core/src/Platform/Windows/SliderExtensions.cs index 5c0f0e29e0a6..1e005424ffd2 100644 --- a/src/Core/src/Platform/Windows/SliderExtensions.cs +++ b/src/Core/src/Platform/Windows/SliderExtensions.cs @@ -6,6 +6,7 @@ using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls.Primitives; using Microsoft.UI.Xaml.Media.Imaging; +using Microsoft.UI.Xaml.Media; namespace Microsoft.Maui.Platform { @@ -19,7 +20,9 @@ static void UpdateIncrement(this Slider nativeSlider, ISlider slider) // Setting the Slider SmallChange property to 0 would throw an System.ArgumentException. if (difference != 0) + { stepping = Math.Min((difference) / 1000, 1); + } nativeSlider.StepFrequency = stepping; } @@ -39,19 +42,14 @@ public static void UpdateMaximum(this Slider nativeSlider, ISlider slider) public static void UpdateValue(this Slider nativeSlider, ISlider slider) { if (nativeSlider.Value != slider.Value) + { nativeSlider.Value = slider.Value; + } } public static void UpdateMinimumTrackColor(this Slider platformSlider, ISlider slider) { - var brush = slider.MinimumTrackColor?.ToPlatform(); - - if (brush is null) - platformSlider.Resources.RemoveKeys(MinimumTrackColorResourceKeys); - else - platformSlider.Resources.SetValueForAllKey(MinimumTrackColorResourceKeys, brush); - - platformSlider.RefreshThemeResources(); + UpdateColor(platformSlider, MinimumTrackColorResourceKeys, slider.MinimumTrackColor?.ToPlatform()); } static readonly string[] MinimumTrackColorResourceKeys = @@ -64,14 +62,7 @@ public static void UpdateMinimumTrackColor(this Slider platformSlider, ISlider s public static void UpdateMaximumTrackColor(this Slider platformSlider, ISlider slider) { - var brush = slider.MaximumTrackColor?.ToPlatform(); - - if (brush == null) - platformSlider.Resources.RemoveKeys(MaximumTrackColorResourceKeys); - else - platformSlider.Resources.SetValueForAllKey(MaximumTrackColorResourceKeys, brush); - - platformSlider.RefreshThemeResources(); + UpdateColor(platformSlider, MaximumTrackColorResourceKeys, slider.MaximumTrackColor?.ToPlatform()); } static readonly string[] MaximumTrackColorResourceKeys = @@ -84,14 +75,7 @@ public static void UpdateMaximumTrackColor(this Slider platformSlider, ISlider s public static void UpdateThumbColor(this Slider platformSlider, ISlider slider) { - var brush = slider.ThumbColor?.ToPlatform(); - - if (brush is null) - platformSlider.Resources.RemoveKeys(ThumbColorResourceKeys); - else - platformSlider.Resources.SetValueForAllKey(ThumbColorResourceKeys, brush); - - platformSlider.RefreshThemeResources(); + UpdateColor(platformSlider, ThumbColorResourceKeys, slider.ThumbColor?.ToPlatform()); } static readonly string[] ThumbColorResourceKeys = @@ -144,13 +128,43 @@ void OnImageOpened(object sender, RoutedEventArgs e) } if (nativeSlider.Parent is FrameworkElement frameworkElement) + { frameworkElement.InvalidateMeasure(); + } } - ; } nativeSlider.ThumbImageSource = nativeThumbImageSource?.Value; } } + + static readonly string[] BackgroundColorResourceKeys = + { + "SliderContainerBackground", + "SliderContainerBackgroundPointerOver", + "SliderContainerBackgroundPressed", + "SliderContainerBackgroundDisabled", + }; + + internal static void UpdateBackgroundColor(this MauiSlider platformSlider, ISlider slider) + { + UpdateColor(platformSlider, BackgroundColorResourceKeys, slider.Background?.ToPlatform()); + } + + static void UpdateColor(Slider platformSlider, string[] keys, Brush? brush) + { + ResourceDictionary resource = platformSlider.Resources; + + if (brush is null) + { + resource.RemoveKeys(keys); + } + else + { + resource.SetValueForAllKey(keys, brush); + } + + platformSlider.RefreshThemeResources(); + } } } \ No newline at end of file