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 System;
|
|
|
|
|
using Microsoft.Maui.Graphics;
|
2025-12-19 09:30:16 +00:00
|
|
|
using Microsoft.Maui.Handlers;
|
|
|
|
|
using SkiaSharp;
|
|
|
|
|
|
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 Button control.
|
|
|
|
|
/// </summary>
|
2026-01-01 13:51:12 -05:00
|
|
|
public class ButtonHandler : ViewHandler<IButton, SkiaButton>
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
|
|
|
|
public static IPropertyMapper<IButton, ButtonHandler> Mapper = new PropertyMapper<IButton, ButtonHandler>(ViewHandler.ViewMapper)
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
["StrokeColor"] = MapStrokeColor,
|
|
|
|
|
["StrokeThickness"] = MapStrokeThickness,
|
|
|
|
|
["CornerRadius"] = MapCornerRadius,
|
|
|
|
|
["Background"] = MapBackground,
|
|
|
|
|
["Padding"] = MapPadding,
|
|
|
|
|
["IsEnabled"] = MapIsEnabled
|
2025-12-19 09:30:16 +00:00
|
|
|
};
|
|
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
public static CommandMapper<IButton, ButtonHandler> CommandMapper = new(ViewHandler.ViewCommandMapper);
|
2025-12-19 09:30:16 +00:00
|
|
|
|
|
|
|
|
public ButtonHandler() : base(Mapper, CommandMapper)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
public ButtonHandler(IPropertyMapper? mapper, CommandMapper? commandMapper = null)
|
2025-12-19 09:30:16 +00:00
|
|
|
: base(mapper ?? Mapper, commandMapper ?? CommandMapper)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override SkiaButton CreatePlatformView()
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
return new SkiaButton();
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void ConnectHandler(SkiaButton platformView)
|
|
|
|
|
{
|
|
|
|
|
base.ConnectHandler(platformView);
|
|
|
|
|
platformView.Clicked += OnClicked;
|
|
|
|
|
platformView.Pressed += OnPressed;
|
|
|
|
|
platformView.Released += OnReleased;
|
2025-12-21 13:26:56 -05:00
|
|
|
|
|
|
|
|
if (VirtualView != null)
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
MapStrokeColor(this, VirtualView);
|
|
|
|
|
MapStrokeThickness(this, VirtualView);
|
|
|
|
|
MapCornerRadius(this, VirtualView);
|
2025-12-21 13:26:56 -05:00
|
|
|
MapBackground(this, VirtualView);
|
|
|
|
|
MapPadding(this, VirtualView);
|
|
|
|
|
MapIsEnabled(this, VirtualView);
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void DisconnectHandler(SkiaButton platformView)
|
|
|
|
|
{
|
|
|
|
|
platformView.Clicked -= OnClicked;
|
|
|
|
|
platformView.Pressed -= OnPressed;
|
|
|
|
|
platformView.Released -= OnReleased;
|
|
|
|
|
base.DisconnectHandler(platformView);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnClicked(object? sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
VirtualView?.Clicked();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPressed(object? sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
VirtualView?.Pressed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnReleased(object? sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
VirtualView?.Released();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
public static void MapStrokeColor(ButtonHandler handler, IButton button)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null)
|
|
|
|
|
{
|
|
|
|
|
var strokeColor = button.StrokeColor;
|
|
|
|
|
if (strokeColor != null)
|
|
|
|
|
{
|
|
|
|
|
handler.PlatformView.BorderColor = strokeColor.ToSKColor();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
public static void MapStrokeThickness(ButtonHandler handler, IButton button)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
handler.PlatformView.BorderWidth = (float)button.StrokeThickness;
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
public static void MapCornerRadius(ButtonHandler handler, IButton button)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
handler.PlatformView.CornerRadius = button.CornerRadius;
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 13:51:12 -05:00
|
|
|
public static void MapBackground(ButtonHandler handler, IButton button)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
var background = button.Background;
|
|
|
|
|
if (background is SolidPaint solidPaint && solidPaint.Color != null)
|
|
|
|
|
{
|
|
|
|
|
handler.PlatformView.ButtonBackgroundColor = solidPaint.Color.ToSKColor();
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void MapPadding(ButtonHandler handler, IButton button)
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
var padding = button.Padding;
|
|
|
|
|
handler.PlatformView.Padding = new SKRect(
|
|
|
|
|
(float)padding.Left,
|
|
|
|
|
(float)padding.Top,
|
|
|
|
|
(float)padding.Right,
|
|
|
|
|
(float)padding.Bottom);
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void MapIsEnabled(ButtonHandler handler, IButton button)
|
|
|
|
|
{
|
2026-01-01 13:51:12 -05:00
|
|
|
if (handler.PlatformView != null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"[ButtonHandler] MapIsEnabled - Text='{handler.PlatformView.Text}', IsEnabled={button.IsEnabled}");
|
|
|
|
|
handler.PlatformView.IsEnabled = button.IsEnabled;
|
|
|
|
|
handler.PlatformView.Invalidate();
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
}
|