Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue28901.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 28901, "[Windows] Switch control is not sizing properly", PlatformAffected.UWP)]
public class Issue28901 : ContentPage
{
public Issue28901()
{
var switchControl = new Switch
{
IsToggled = true,
HorizontalOptions = LayoutOptions.End,
AutomationId = "SwitchControl"
};

var switchControl2 = new Switch();

var label = new Label
{
Text = "Switch Control",
VerticalOptions = LayoutOptions.Center,
};

var settingsLabel = new Label
{
Text = "Change View",
VerticalOptions = LayoutOptions.Center,
};

var horizontalStackLayout = new HorizontalStackLayout
{
Children =
{
label,
switchControl2,
settingsLabel
},
};

Content = new VerticalStackLayout
{
VerticalOptions = LayoutOptions.Center,
Children =
{
switchControl,
horizontalStackLayout,
}
};
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue28901 : _IssuesUITest
{
public override string Issue => "[Windows] Switch control is not sizing properly";

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

[Test]
[Category(UITestCategories.Switch)]
public void VerifySwitchControlSize()
{
App.WaitForElement("SwitchControl");
VerifyScreenshot();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions src/Core/src/Handlers/Switch/SwitchHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,71 @@ public static void MapIsOn(ISwitchHandler handler, ISwitch view)
public static void MapTrackColor(ISwitchHandler handler, ISwitch view)
{
if (handler is SwitchHandler)
{
handler.PlatformView?.UpdateTrackColor(view);
}
}

public static void MapThumbColor(ISwitchHandler handler, ISwitch view)
{
if (handler is SwitchHandler)
{
handler.PlatformView?.UpdateThumbColor(view);
}
}

protected override void DisconnectHandler(ToggleSwitch platformView)
{
base.DisconnectHandler(platformView);
platformView.Toggled -= OnToggled;
platformView.Loaded -= OnLoaded;
}

protected override void ConnectHandler(ToggleSwitch platformView)
{
base.ConnectHandler(platformView);
platformView.Toggled += OnToggled;
platformView.Loaded += OnLoaded;
}

void OnLoaded(object sender, UI.Xaml.RoutedEventArgs e)
{
var toggleSwitch = (ToggleSwitch)sender;

if (toggleSwitch is null)
{
return;
}

var rootGrid = toggleSwitch.GetDescendantByName<Grid>("SwitchAreaGrid")?.Parent as Grid;
if (rootGrid is not null && rootGrid.ColumnDefinitions.Count > 0)
{
// In the default ToggleSwitch template, the second column (index 1) is only used for spacing
// between the toggle knob and the On/Off content area (which is defined in the third column).
// Since MAUI does not support OnContent/OffContent, this spacing is unnecessary
// so we set its width to 0 to reduce unwanted layout padding.
rootGrid.ColumnDefinitions[1].Width = new UI.Xaml.GridLength(0);
}
}

// TODO: Make it public in .NET 10.0
internal static void MapSwitchMinimumWidth(IViewHandler handler, IView view)
{
// Update the native ToggleSwitch MinWidth to reflect the MAUI view's MinimumWidth,
// overriding the default WinUI MinWidth (154) since we're not supporting OnContent and OffContent.
// This ensures the control does not reserve unnecessary space for labels.
if (view is ISwitch switchView && handler is SwitchHandler switchHandler)
{
switchHandler.PlatformView?.UpdateMinWidth(switchView);
}
}

void OnToggled(object sender, UI.Xaml.RoutedEventArgs e)
{
if (VirtualView is null || PlatformView is null || VirtualView.IsOn == PlatformView.IsOn)
{
return;
}

VirtualView.IsOn = PlatformView.IsOn;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Core/src/Handlers/Switch/SwitchHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public partial class SwitchHandler : ISwitchHandler
[nameof(ISwitch.IsOn)] = MapIsOn,
[nameof(ISwitch.ThumbColor)] = MapThumbColor,
[nameof(ISwitch.TrackColor)] = MapTrackColor,
#if WINDOWS
[nameof(IView.MinimumWidth)] = MapSwitchMinimumWidth,
#endif
};

public static CommandMapper<ISwitch, ISwitchHandler> CommandMapper = new(ViewCommandMapper)
Expand Down
13 changes: 13 additions & 0 deletions src/Core/src/Platform/Windows/SwitchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,18 @@ public static void UpdateThumbColor(this ToggleSwitch toggleSwitch, ISwitch view
"ToggleSwitchKnobFillOffDisabled");
}
}

internal static void UpdateMinWidth(this ToggleSwitch toggleSwitch, ISwitch view)
{
double minWidth = view.MinimumWidth;
if (double.IsNaN(minWidth))
{
toggleSwitch.MinWidth = 0;
}
else
{
toggleSwitch.MinWidth = minWidth;
}
}
}
}
Loading