Button completed

This commit is contained in:
2026-01-16 04:14:34 +00:00
parent a2800464c8
commit 9a49185183
3 changed files with 652 additions and 314 deletions

View File

@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers; using Microsoft.Maui.Handlers;
using Microsoft.Maui.Graphics; using Microsoft.Maui.Graphics;
using SkiaSharp; using SkiaSharp;
@@ -80,13 +81,13 @@ public partial class ButtonHandler : ViewHandler<IButton, SkiaButton>
var strokeColor = button.StrokeColor; var strokeColor = button.StrokeColor;
if (strokeColor is not null) if (strokeColor is not null)
handler.PlatformView.BorderColor = strokeColor.ToSKColor(); handler.PlatformView.BorderColor = strokeColor;
} }
public static void MapStrokeThickness(ButtonHandler handler, IButton button) public static void MapStrokeThickness(ButtonHandler handler, IButton button)
{ {
if (handler.PlatformView is null) return; if (handler.PlatformView is null) return;
handler.PlatformView.BorderWidth = (float)button.StrokeThickness; handler.PlatformView.BorderWidth = button.StrokeThickness;
} }
public static void MapCornerRadius(ButtonHandler handler, IButton button) public static void MapCornerRadius(ButtonHandler handler, IButton button)
@@ -101,8 +102,12 @@ public partial class ButtonHandler : ViewHandler<IButton, SkiaButton>
if (button.Background is SolidPaint solidPaint && solidPaint.Color is not null) if (button.Background is SolidPaint solidPaint && solidPaint.Color is not null)
{ {
// Set ButtonBackgroundColor (used for rendering) not base BackgroundColor // Set BackgroundColor (used for rendering)
handler.PlatformView.ButtonBackgroundColor = solidPaint.Color.ToSKColor(); handler.PlatformView.BackgroundColor = new SKColor(
(byte)(solidPaint.Color.Red * 255),
(byte)(solidPaint.Color.Green * 255),
(byte)(solidPaint.Color.Blue * 255),
(byte)(solidPaint.Color.Alpha * 255));
} }
} }
@@ -111,17 +116,16 @@ public partial class ButtonHandler : ViewHandler<IButton, SkiaButton>
if (handler.PlatformView is null) return; if (handler.PlatformView is null) return;
var padding = button.Padding; var padding = button.Padding;
handler.PlatformView.Padding = new SKRect( handler.PlatformView.Padding = new Thickness(
(float)padding.Left, padding.Left,
(float)padding.Top, padding.Top,
(float)padding.Right, padding.Right,
(float)padding.Bottom); padding.Bottom);
} }
public static void MapIsEnabled(ButtonHandler handler, IButton button) public static void MapIsEnabled(ButtonHandler handler, IButton button)
{ {
if (handler.PlatformView is null) return; if (handler.PlatformView is null) return;
Console.WriteLine($"[ButtonHandler] MapIsEnabled - Text='{handler.PlatformView.Text}', IsEnabled={button.IsEnabled}");
handler.PlatformView.IsEnabled = button.IsEnabled; handler.PlatformView.IsEnabled = button.IsEnabled;
handler.PlatformView.Invalidate(); handler.PlatformView.Invalidate();
} }
@@ -172,7 +176,7 @@ public partial class TextButtonHandler : ButtonHandler
if (handler.PlatformView is null) return; if (handler.PlatformView is null) return;
if (button.TextColor is not null) if (button.TextColor is not null)
handler.PlatformView.TextColor = button.TextColor.ToSKColor(); handler.PlatformView.TextColor = button.TextColor;
} }
public static void MapFont(TextButtonHandler handler, ITextButton button) public static void MapFont(TextButtonHandler handler, ITextButton button)
@@ -181,18 +185,23 @@ public partial class TextButtonHandler : ButtonHandler
var font = button.Font; var font = button.Font;
if (font.Size > 0) if (font.Size > 0)
handler.PlatformView.FontSize = (float)font.Size; handler.PlatformView.FontSize = font.Size;
if (!string.IsNullOrEmpty(font.Family)) if (!string.IsNullOrEmpty(font.Family))
handler.PlatformView.FontFamily = font.Family; handler.PlatformView.FontFamily = font.Family;
handler.PlatformView.IsBold = font.Weight >= FontWeight.Bold; // Convert Font weight/slant to FontAttributes
handler.PlatformView.IsItalic = font.Slant == FontSlant.Italic || font.Slant == FontSlant.Oblique; FontAttributes attrs = FontAttributes.None;
if (font.Weight >= FontWeight.Bold)
attrs |= FontAttributes.Bold;
if (font.Slant == FontSlant.Italic || font.Slant == FontSlant.Oblique)
attrs |= FontAttributes.Italic;
handler.PlatformView.FontAttributes = attrs;
} }
public static void MapCharacterSpacing(TextButtonHandler handler, ITextButton button) public static void MapCharacterSpacing(TextButtonHandler handler, ITextButton button)
{ {
if (handler.PlatformView is null) return; if (handler.PlatformView is null) return;
handler.PlatformView.CharacterSpacing = (float)button.CharacterSpacing; handler.PlatformView.CharacterSpacing = button.CharacterSpacing;
} }
} }

View File

@@ -226,20 +226,20 @@ public static class LinuxProgramHost
var buttonSection = new SkiaStackLayout { Orientation = StackOrientation.Horizontal, Spacing = 10 }; var buttonSection = new SkiaStackLayout { Orientation = StackOrientation.Horizontal, Spacing = 10 };
var btnPrimary = new SkiaButton { Text = "Primary", FontSize = 14 }; var btnPrimary = new SkiaButton { Text = "Primary", FontSize = 14 };
btnPrimary.BackgroundColor = new SKColor(0x21, 0x96, 0xF3); btnPrimary.BackgroundColor = new SKColor(0x21, 0x96, 0xF3); // Uses base SkiaView's SKColor BackgroundColor
btnPrimary.TextColor = SKColors.White; btnPrimary.TextColor = Colors.White;
var clickCount = 0; var clickCount = 0;
btnPrimary.Clicked += (s, e) => { clickCount++; btnPrimary.Text = $"Clicked {clickCount}x"; }; btnPrimary.Clicked += (s, e) => { clickCount++; btnPrimary.Text = $"Clicked {clickCount}x"; };
buttonSection.AddChild(btnPrimary); buttonSection.AddChild(btnPrimary);
var btnSuccess = new SkiaButton { Text = "Success", FontSize = 14 }; var btnSuccess = new SkiaButton { Text = "Success", FontSize = 14 };
btnSuccess.BackgroundColor = new SKColor(0x4C, 0xAF, 0x50); btnSuccess.BackgroundColor = new SKColor(0x4C, 0xAF, 0x50);
btnSuccess.TextColor = SKColors.White; btnSuccess.TextColor = Colors.White;
buttonSection.AddChild(btnSuccess); buttonSection.AddChild(btnSuccess);
var btnDanger = new SkiaButton { Text = "Danger", FontSize = 14 }; var btnDanger = new SkiaButton { Text = "Danger", FontSize = 14 };
btnDanger.BackgroundColor = new SKColor(0xF4, 0x43, 0x36); btnDanger.BackgroundColor = new SKColor(0xF4, 0x43, 0x36);
btnDanger.TextColor = SKColors.White; btnDanger.TextColor = Colors.White;
buttonSection.AddChild(btnDanger); buttonSection.AddChild(btnDanger);
root.AddChild(buttonSection); root.AddChild(buttonSection);

View File

File diff suppressed because it is too large Load Diff