// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.Maui.Handlers; using Microsoft.Maui.Graphics; using SkiaSharp; namespace Microsoft.Maui.Platform.Linux.Handlers; /// /// Handler for Button on Linux using Skia rendering. /// Maps IButton interface to SkiaButton platform view. /// public partial class ButtonHandler : ViewHandler { public static IPropertyMapper Mapper = new PropertyMapper(ViewHandler.ViewMapper) { [nameof(IButtonStroke.StrokeColor)] = MapStrokeColor, [nameof(IButtonStroke.StrokeThickness)] = MapStrokeThickness, [nameof(IButtonStroke.CornerRadius)] = MapCornerRadius, [nameof(IView.Background)] = MapBackground, [nameof(IPadding.Padding)] = MapPadding, [nameof(IView.IsEnabled)] = MapIsEnabled, }; public static CommandMapper CommandMapper = new(ViewHandler.ViewCommandMapper) { }; public ButtonHandler() : base(Mapper, CommandMapper) { } public ButtonHandler(IPropertyMapper? mapper, CommandMapper? commandMapper = null) : base(mapper ?? Mapper, commandMapper ?? CommandMapper) { } protected override SkiaButton CreatePlatformView() { var button = new SkiaButton(); return button; } protected override void ConnectHandler(SkiaButton platformView) { base.ConnectHandler(platformView); platformView.Clicked += OnClicked; platformView.Pressed += OnPressed; platformView.Released += OnReleased; // Manually map all properties on connect since MAUI may not trigger updates // for properties that were set before handler connection if (VirtualView != null) { MapStrokeColor(this, VirtualView); MapStrokeThickness(this, VirtualView); MapCornerRadius(this, VirtualView); MapBackground(this, VirtualView); MapPadding(this, VirtualView); MapIsEnabled(this, VirtualView); } } 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(); public static void MapStrokeColor(ButtonHandler handler, IButton button) { if (handler.PlatformView is null) return; var strokeColor = button.StrokeColor; if (strokeColor is not null) handler.PlatformView.BorderColor = strokeColor.ToSKColor(); } public static void MapStrokeThickness(ButtonHandler handler, IButton button) { if (handler.PlatformView is null) return; handler.PlatformView.BorderWidth = (float)button.StrokeThickness; } public static void MapCornerRadius(ButtonHandler handler, IButton button) { if (handler.PlatformView is null) return; handler.PlatformView.CornerRadius = button.CornerRadius; } public static void MapBackground(ButtonHandler handler, IButton button) { if (handler.PlatformView is null) return; if (button.Background is SolidPaint solidPaint && solidPaint.Color is not null) { // Set ButtonBackgroundColor (used for rendering) not base BackgroundColor handler.PlatformView.ButtonBackgroundColor = solidPaint.Color.ToSKColor(); } } public static void MapPadding(ButtonHandler handler, IButton button) { if (handler.PlatformView is null) return; var padding = button.Padding; handler.PlatformView.Padding = new SKRect( (float)padding.Left, (float)padding.Top, (float)padding.Right, (float)padding.Bottom); } public static void MapIsEnabled(ButtonHandler handler, IButton button) { if (handler.PlatformView is null) return; Console.WriteLine($"[ButtonHandler] MapIsEnabled - Text='{handler.PlatformView.Text}', IsEnabled={button.IsEnabled}"); handler.PlatformView.IsEnabled = button.IsEnabled; handler.PlatformView.Invalidate(); } } /// /// Handler for TextButton on Linux - extends ButtonHandler with text support. /// Maps ITextButton interface (which includes IText properties). /// public partial class TextButtonHandler : ButtonHandler { public static new IPropertyMapper Mapper = new PropertyMapper(ButtonHandler.Mapper) { [nameof(IText.Text)] = MapText, [nameof(ITextStyle.TextColor)] = MapTextColor, [nameof(ITextStyle.Font)] = MapFont, [nameof(ITextStyle.CharacterSpacing)] = MapCharacterSpacing, }; public TextButtonHandler() : base(Mapper) { } protected override void ConnectHandler(SkiaButton platformView) { base.ConnectHandler(platformView); // Manually map text properties on connect since MAUI may not trigger updates // for properties that were set before handler connection if (VirtualView is ITextButton textButton) { MapText(this, textButton); MapTextColor(this, textButton); MapFont(this, textButton); MapCharacterSpacing(this, textButton); } } public static void MapText(TextButtonHandler handler, ITextButton button) { if (handler.PlatformView is null) return; handler.PlatformView.Text = button.Text ?? string.Empty; } public static void MapTextColor(TextButtonHandler handler, ITextButton button) { if (handler.PlatformView is null) return; if (button.TextColor is not null) handler.PlatformView.TextColor = button.TextColor.ToSKColor(); } public static void MapFont(TextButtonHandler handler, ITextButton button) { if (handler.PlatformView is null) return; var font = button.Font; if (font.Size > 0) handler.PlatformView.FontSize = (float)font.Size; if (!string.IsNullOrEmpty(font.Family)) handler.PlatformView.FontFamily = font.Family; handler.PlatformView.IsBold = font.Weight >= FontWeight.Bold; handler.PlatformView.IsItalic = font.Slant == FontSlant.Italic || font.Slant == FontSlant.Oblique; } public static void MapCharacterSpacing(TextButtonHandler handler, ITextButton button) { if (handler.PlatformView is null) return; handler.PlatformView.CharacterSpacing = (float)button.CharacterSpacing; } }