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

Support for Setting Switch Off State Color #25068

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e79dbb6
fix added
devanathan-vaithiyanathan Oct 3, 2024
e21073d
test added
devanathan-vaithiyanathan Oct 4, 2024
4f1673e
unshipped file changes reverted
devanathan-vaithiyanathan Oct 4, 2024
d784dff
attributes added
devanathan-vaithiyanathan Oct 4, 2024
df3f69f
test case changes added
devanathan-vaithiyanathan Oct 7, 2024
55ff682
ui test changes added
devanathan-vaithiyanathan Oct 7, 2024
e26a041
test case changes added
devanathan-vaithiyanathan Oct 7, 2024
fd48c53
code behind click event removed
devanathan-vaithiyanathan Oct 7, 2024
8887789
UI test files name changed
devanathan-vaithiyanathan Oct 7, 2024
e1a6f1b
API changes and test case changes added
devanathan-vaithiyanathan Oct 11, 2024
9e65be6
modified test sample
devanathan-vaithiyanathan Oct 11, 2024
19c941c
platform condition added in script file
devanathan-vaithiyanathan Oct 11, 2024
0276226
fix added
devanathan-vaithiyanathan Oct 3, 2024
4aaf0a5
test added
devanathan-vaithiyanathan Oct 4, 2024
122a241
unshipped file changes reverted
devanathan-vaithiyanathan Oct 4, 2024
4991269
attributes added
devanathan-vaithiyanathan Oct 4, 2024
265aa78
test case changes added
devanathan-vaithiyanathan Oct 7, 2024
6ef9238
ui test changes added
devanathan-vaithiyanathan Oct 7, 2024
c0f20b4
test case changes added
devanathan-vaithiyanathan Oct 7, 2024
eb6b50d
code behind click event removed
devanathan-vaithiyanathan Oct 7, 2024
ec64aad
UI test files name changed
devanathan-vaithiyanathan Oct 7, 2024
07b64eb
API changes and test case changes added
devanathan-vaithiyanathan Oct 11, 2024
6157b8b
modified test sample
devanathan-vaithiyanathan Oct 11, 2024
eb601dc
platform condition added in script file
devanathan-vaithiyanathan Oct 11, 2024
289705b
OffColor API added in tizen platform
devanathan-vaithiyanathan Oct 21, 2024
de6888f
Merge branch 'Fix-7673' of https://github.com/devanathan-vaithiyanath…
devanathan-vaithiyanathan Oct 21, 2024
f027068
Merge branch 'main' of https://github.com/devanathan-vaithiyanathan/m…
devanathan-vaithiyanathan Oct 25, 2024
54baa32
sender parameter nullable removed
devanathan-vaithiyanathan Oct 28, 2024
3838533
simplified a test case
devanathan-vaithiyanathan Oct 29, 2024
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
Expand Up @@ -146,3 +146,6 @@ static readonly Microsoft.Maui.Controls.TitleBar.TitleProperty -> Microsoft.Maui
static readonly Microsoft.Maui.Controls.TitleBar.TrailingContentProperty -> Microsoft.Maui.Controls.BindableProperty!
static readonly Microsoft.Maui.Controls.Window.TitleBarProperty -> Microsoft.Maui.Controls.BindableProperty!
virtual Microsoft.Maui.Controls.Application.ActivateWindow(Microsoft.Maui.Controls.Window! window) -> void
~static readonly Microsoft.Maui.Controls.Switch.OffColorProperty -> Microsoft.Maui.Controls.BindableProperty
~Microsoft.Maui.Controls.Switch.OffColor.get -> Microsoft.Maui.Graphics.Color
~Microsoft.Maui.Controls.Switch.OffColor.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,6 @@ virtual Microsoft.Maui.Controls.Handlers.Items2.ItemsViewController2<TItemsView>
virtual Microsoft.Maui.Controls.Handlers.Items2.ItemsViewDelegator2<TItemsView, TViewController>.GetVisibleItemsIndex() -> (bool VisibleItems, int First, int Center, int Last)
virtual Microsoft.Maui.Controls.Handlers.Items2.ItemsViewHandler2<TItemsView>.UpdateLayout() -> void
override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.MovedToWindow() -> void
~static readonly Microsoft.Maui.Controls.Switch.OffColorProperty -> Microsoft.Maui.Controls.BindableProperty
~Microsoft.Maui.Controls.Switch.OffColor.get -> Microsoft.Maui.Graphics.Color
~Microsoft.Maui.Controls.Switch.OffColor.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,6 @@ virtual Microsoft.Maui.Controls.Handlers.Items2.ItemsViewController2<TItemsView>
virtual Microsoft.Maui.Controls.Handlers.Items2.ItemsViewDelegator2<TItemsView, TViewController>.GetVisibleItemsIndex() -> (bool VisibleItems, int First, int Center, int Last)
virtual Microsoft.Maui.Controls.Handlers.Items2.ItemsViewHandler2<TItemsView>.UpdateLayout() -> void
override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.MovedToWindow() -> void
~static readonly Microsoft.Maui.Controls.Switch.OffColorProperty -> Microsoft.Maui.Controls.BindableProperty
~Microsoft.Maui.Controls.Switch.OffColor.get -> Microsoft.Maui.Graphics.Color
~Microsoft.Maui.Controls.Switch.OffColor.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,6 @@ static readonly Microsoft.Maui.Controls.TitleBar.TitleProperty -> Microsoft.Maui
static readonly Microsoft.Maui.Controls.TitleBar.TrailingContentProperty -> Microsoft.Maui.Controls.BindableProperty!
static readonly Microsoft.Maui.Controls.Window.TitleBarProperty -> Microsoft.Maui.Controls.BindableProperty!
virtual Microsoft.Maui.Controls.Application.ActivateWindow(Microsoft.Maui.Controls.Window! window) -> void
~static readonly Microsoft.Maui.Controls.Switch.OffColorProperty -> Microsoft.Maui.Controls.BindableProperty
~Microsoft.Maui.Controls.Switch.OffColor.get -> Microsoft.Maui.Graphics.Color
~Microsoft.Maui.Controls.Switch.OffColor.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,6 @@ static readonly Microsoft.Maui.Controls.TitleBar.TitleProperty -> Microsoft.Maui
static readonly Microsoft.Maui.Controls.TitleBar.TrailingContentProperty -> Microsoft.Maui.Controls.BindableProperty!
static readonly Microsoft.Maui.Controls.Window.TitleBarProperty -> Microsoft.Maui.Controls.BindableProperty!
virtual Microsoft.Maui.Controls.Application.ActivateWindow(Microsoft.Maui.Controls.Window! window) -> void
~static readonly Microsoft.Maui.Controls.Switch.OffColorProperty -> Microsoft.Maui.Controls.BindableProperty
~Microsoft.Maui.Controls.Switch.OffColor.get -> Microsoft.Maui.Graphics.Color
~Microsoft.Maui.Controls.Switch.OffColor.set -> void
3 changes: 3 additions & 0 deletions src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,6 @@ static readonly Microsoft.Maui.Controls.TitleBar.TitleProperty -> Microsoft.Maui
static readonly Microsoft.Maui.Controls.TitleBar.TrailingContentProperty -> Microsoft.Maui.Controls.BindableProperty!
static readonly Microsoft.Maui.Controls.Window.TitleBarProperty -> Microsoft.Maui.Controls.BindableProperty!
virtual Microsoft.Maui.Controls.Application.ActivateWindow(Microsoft.Maui.Controls.Window! window) -> void
~static readonly Microsoft.Maui.Controls.Switch.OffColorProperty -> Microsoft.Maui.Controls.BindableProperty
~Microsoft.Maui.Controls.Switch.OffColor.get -> Microsoft.Maui.Graphics.Color
~Microsoft.Maui.Controls.Switch.OffColor.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,6 @@ static readonly Microsoft.Maui.Controls.TitleBar.TitleProperty -> Microsoft.Maui
static readonly Microsoft.Maui.Controls.TitleBar.TrailingContentProperty -> Microsoft.Maui.Controls.BindableProperty!
static readonly Microsoft.Maui.Controls.Window.TitleBarProperty -> Microsoft.Maui.Controls.BindableProperty!
virtual Microsoft.Maui.Controls.Application.ActivateWindow(Microsoft.Maui.Controls.Window! window) -> void
~static readonly Microsoft.Maui.Controls.Switch.OffColorProperty -> Microsoft.Maui.Controls.BindableProperty
~Microsoft.Maui.Controls.Switch.OffColor.get -> Microsoft.Maui.Graphics.Color
~Microsoft.Maui.Controls.Switch.OffColor.set -> void
30 changes: 24 additions & 6 deletions src/Controls/src/Core/Switch/Switch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public partial class Switch : View, IElementConfiguration<Switch>, ISwitch
((IView)bindable)?.Handler?.UpdateValue(nameof(ISwitch.TrackColor));
});

/// <summary>Bindable property for <see cref="OffColor"/>.</summary>
public static readonly BindableProperty OffColorProperty = BindableProperty.Create(nameof(OffColor), typeof(Color), typeof(Switch), null,
propertyChanged: (bindable, oldValue, newValue) =>
{
((IView)bindable)?.Handler?.UpdateValue(nameof(ISwitch.TrackColor));
});

/// <summary>Bindable property for <see cref="ThumbColor"/>.</summary>
public static readonly BindableProperty ThumbColorProperty = BindableProperty.Create(nameof(ThumbColor), typeof(Color), typeof(Switch), null);

Expand All @@ -39,6 +46,17 @@ public Color OnColor
set { SetValue(OnColorProperty, value); }
}

/// <summary>
/// Gets or sets the color of the toggle switch's track when it is in the off state.
/// If not set, the default color will be used for the off-track appearance.
/// </summary>
public Color OffColor
{
get { return (Color) GetValue(OffColorProperty); }
set { SetValue(OffColorProperty, value); }
}


/// <include file="../../docs/Microsoft.Maui.Controls/Switch.xml" path="//Member[@MemberName='ThumbColor']/Docs/*" />
public Color ThumbColor
{
Expand Down Expand Up @@ -91,14 +109,14 @@ Color ISwitch.TrackColor
{
get
{
#if WINDOWS
return OnColor;
#else
if (IsToggled)
{
return OnColor;

return null;
#endif
}
else
{
return OffColor;
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue15695.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue15695">

<StackLayout>
<Switch x:Name="mauiSwitch" AutomationId="Switch" OffColor="Red" OnColor="Green" HorizontalOptions="Center" VerticalOptions="Center"/>
<Button Text="ToggleSwitch" AutomationId="SwitchButton" Clicked="OnButtonClicked"/>
</StackLayout>
</ContentPage>
16 changes: 16 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue15695.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 15695, "Support for Switch OFF State color", PlatformAffected.All)]
public partial class Issue15695 : ContentPage
{
public Issue15695()
{
InitializeComponent();
}

private void OnButtonClicked(object sender, EventArgs e)
{
mauiSwitch.IsToggled = !mauiSwitch.IsToggled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#if !MACCATALYST
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue15695 : _IssuesUITest
{
public override string Issue => "Support for Switch OFF State color";

public Issue15695(TestDevice device) : base(device)
{
}

[Test]
[Category(UITestCategories.Switch)]
public void VerifySwitchOffColor()
{
App.WaitForElement("Switch");
VerifyScreenshot();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending snapshots. Already available on the latest build.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added snapshots for all platforms

}

[Test]
[Category(UITestCategories.Switch)]
public void VerifySwitchOnColor()
{
App.WaitForElement("Switch");
App.Tap("SwitchButton");
VerifyScreenshot();
}

[Test]
[Category(UITestCategories.Switch)]
public void VerifySwitchOffColorAfterToggling()
{
App.WaitForElement("Switch");
App.Tap("SwitchButton");
VerifyScreenshot();
}
}
#endif
7 changes: 2 additions & 5 deletions src/Core/src/Platform/Android/SwitchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ public static void UpdateTrackColor(this ASwitch aSwitch, ISwitch view)
{
var trackColor = view.TrackColor;

if (aSwitch.Checked)
{
if (trackColor != null)
aSwitch.TrackDrawable?.SetColorFilter(trackColor, FilterMode.SrcAtop);
}
if (trackColor is not null)
aSwitch.TrackDrawable?.SetColorFilter(trackColor, FilterMode.SrcAtop);
else
aSwitch.TrackDrawable?.ClearColorFilter();
}
Expand Down
20 changes: 10 additions & 10 deletions src/Core/src/Platform/Windows/SwitchExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.UI.Xaml.Controls;
using Windows.UI;
using Microsoft.UI.Xaml.Media;

namespace Microsoft.Maui.Platform
{
Expand All @@ -19,20 +21,18 @@ public static void UpdateTrackColor(this ToggleSwitch toggleSwitch, ISwitch view
return;
}

if (view.TrackColor == null)
{
return;
}
var trackColor = view.TrackColor?.ToPlatform() ?? new SolidColorBrush(Color.FromArgb(6, 0, 0, 0));

if (view.TrackColor != null)
{
toggleSwitch.TryUpdateResource(
view.TrackColor.ToPlatform(),
toggleSwitch.TryUpdateResource(
trackColor,
"ToggleSwitchFillOn",
"ToggleSwitchFillOnPointerOver",
"ToggleSwitchFillOnPressed",
"ToggleSwitchFillOnDisabled");
}
"ToggleSwitchFillOnDisabled",
"ToggleSwitchFillOff",
"ToggleSwitchFillOffPointerOver",
"ToggleSwitchFillOffPressed",
"ToggleSwitchFillOffDisabled");
}

public static void UpdateThumbColor(this ToggleSwitch toggleSwitch, ISwitch view)
Expand Down
13 changes: 7 additions & 6 deletions src/Core/src/Platform/iOS/SwitchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ public static void UpdateTrackColor(this UISwitch uiSwitch, ISwitch view)
if (uIView is null)
return;

if (!view.IsOn)
if (view.TrackColor is not null)
{
// iOS 13+ uses the UIColor.SecondarySystemFill to support Light and Dark mode
// else, use the RGBA equivalent of UIColor.SecondarySystemFill in Light mode
uIView.BackgroundColor = OperatingSystem.IsIOSVersionAtLeast(13) ? UIColor.SecondarySystemFill : DefaultBackgroundColor;
uiSwitch.OnTintColor = view.TrackColor.ToPlatform();
uIView.BackgroundColor = uiSwitch.OnTintColor;
}

else if (view.TrackColor is not null)
else
{
uiSwitch.OnTintColor = view.TrackColor.ToPlatform();
// iOS 13+ uses the UIColor.SecondarySystemFill to support Light and Dark mode
// else, use the RGBA equivalent of UIColor.SecondarySystemFill in Light mode
uiSwitch.OnTintColor = OperatingSystem.IsIOSVersionAtLeast(13) ? UIColor.SecondarySystemFill : DefaultBackgroundColor;
uIView.BackgroundColor = uiSwitch.OnTintColor;
}
}
Expand Down
Loading