2025-12-19 09:30:16 +00:00
|
|
|
// Licensed to the .NET Foundation under one or more agreements.
|
|
|
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
using Microsoft.Maui.Graphics;
|
2025-12-19 09:30:16 +00:00
|
|
|
using Microsoft.Maui.Handlers;
|
2026-01-01 13:51:12 -05:00
|
|
|
using SkiaSharp;
|
2025-12-19 09:30:16 +00:00
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
namespace Microsoft.Maui.Platform.Linux.Handlers;
|
2025-12-19 09:30:16 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Linux handler for Switch control.
|
|
|
|
|
/// </summary>
|
2026-01-01 13:51:12 -05:00
|
|
|
public class SwitchHandler : ViewHandler<ISwitch, SkiaSwitch>
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
|
|
|
|
public static IPropertyMapper<ISwitch, SwitchHandler> Mapper = new PropertyMapper<ISwitch, SwitchHandler>(ViewHandler.ViewMapper)
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
["IsOn"] = MapIsOn,
|
|
|
|
|
["TrackColor"] = MapTrackColor,
|
|
|
|
|
["ThumbColor"] = MapThumbColor,
|
|
|
|
|
["Background"] = MapBackground,
|
|
|
|
|
["IsEnabled"] = MapIsEnabled
|
2025-12-19 09:30:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static CommandMapper<ISwitch, SwitchHandler> CommandMapper = new(ViewHandler.ViewCommandMapper);
|
|
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
public SwitchHandler() : base(Mapper, CommandMapper)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SwitchHandler(IPropertyMapper? mapper, CommandMapper? commandMapper = null)
|
|
|
|
|
: base(mapper ?? Mapper, commandMapper ?? CommandMapper)
|
|
|
|
|
{
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
protected override SkiaSwitch CreatePlatformView()
|
|
|
|
|
{
|
|
|
|
|
return new SkiaSwitch();
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
|
|
|
|
|
protected override void ConnectHandler(SkiaSwitch platformView)
|
|
|
|
|
{
|
|
|
|
|
base.ConnectHandler(platformView);
|
|
|
|
|
platformView.Toggled += OnToggled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void DisconnectHandler(SkiaSwitch platformView)
|
|
|
|
|
{
|
|
|
|
|
platformView.Toggled -= OnToggled;
|
|
|
|
|
base.DisconnectHandler(platformView);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnToggled(object? sender, ToggledEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (VirtualView != null && VirtualView.IsOn != e.Value)
|
|
|
|
|
{
|
|
|
|
|
VirtualView.IsOn = e.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void MapIsOn(SwitchHandler handler, ISwitch @switch)
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
|
|
|
|
handler.PlatformView.IsOn = @switch.IsOn;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void MapTrackColor(SwitchHandler handler, ISwitch @switch)
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null && @switch.TrackColor != null)
|
|
|
|
|
{
|
|
|
|
|
var onTrackColor = @switch.TrackColor.ToSKColor();
|
|
|
|
|
handler.PlatformView.OnTrackColor = onTrackColor;
|
|
|
|
|
handler.PlatformView.OffTrackColor = onTrackColor.WithAlpha(128);
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void MapThumbColor(SwitchHandler handler, ISwitch @switch)
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null && @switch.ThumbColor != null)
|
|
|
|
|
{
|
2025-12-19 09:30:16 +00:00
|
|
|
handler.PlatformView.ThumbColor = @switch.ThumbColor.ToSKColor();
|
2026-01-01 13:51:12 -05:00
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
2025-12-21 13:26:56 -05:00
|
|
|
|
|
|
|
|
public static void MapBackground(SwitchHandler handler, ISwitch @switch)
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null)
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (@switch.Background is SolidPaint solidPaint && solidPaint.Color != null)
|
|
|
|
|
{
|
|
|
|
|
handler.PlatformView.BackgroundColor = solidPaint.Color.ToSKColor();
|
|
|
|
|
}
|
2025-12-21 13:26:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
public static void MapIsEnabled(SwitchHandler handler, ISwitch @switch)
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null)
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
handler.PlatformView.IsEnabled = @switch.IsEnabled;
|
2025-12-21 13:26:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|