TimePicker completed

This commit is contained in:
2026-01-16 05:36:36 +00:00
parent c21ff803fe
commit 59bcb9244e
2 changed files with 78 additions and 2 deletions

View File

@@ -21,6 +21,7 @@ public partial class TimePickerHandler : ViewHandler<ITimePicker, SkiaTimePicker
[nameof(ITimePicker.Format)] = MapFormat, [nameof(ITimePicker.Format)] = MapFormat,
[nameof(ITimePicker.TextColor)] = MapTextColor, [nameof(ITimePicker.TextColor)] = MapTextColor,
[nameof(ITimePicker.CharacterSpacing)] = MapCharacterSpacing, [nameof(ITimePicker.CharacterSpacing)] = MapCharacterSpacing,
[nameof(ITextStyle.Font)] = MapFont,
[nameof(IView.Background)] = MapBackground, [nameof(IView.Background)] = MapBackground,
}; };
@@ -95,7 +96,26 @@ public partial class TimePickerHandler : ViewHandler<ITimePicker, SkiaTimePicker
public static void MapCharacterSpacing(TimePickerHandler handler, ITimePicker timePicker) public static void MapCharacterSpacing(TimePickerHandler handler, ITimePicker timePicker)
{ {
// Character spacing would require custom text rendering if (handler.PlatformView is null) return;
handler.PlatformView.CharacterSpacing = timePicker.CharacterSpacing;
}
public static void MapFont(TimePickerHandler handler, ITimePicker timePicker)
{
if (handler.PlatformView is null) return;
var font = timePicker.Font;
if (font.Size > 0)
handler.PlatformView.FontSize = font.Size;
if (!string.IsNullOrEmpty(font.Family))
handler.PlatformView.FontFamily = font.Family;
// Map FontAttributes from the Font weight/slant
var attrs = FontAttributes.None;
if (font.Weight >= FontWeight.Bold)
attrs |= FontAttributes.Bold;
handler.PlatformView.FontAttributes = attrs;
} }
public static void MapBackground(TimePickerHandler handler, ITimePicker timePicker) public static void MapBackground(TimePickerHandler handler, ITimePicker timePicker)

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.Graphics; using Microsoft.Maui.Graphics;
using Microsoft.Maui.Platform.Linux; using Microsoft.Maui.Platform.Linux;
using SkiaSharp; using SkiaSharp;
@@ -62,10 +63,22 @@ public class SkiaTimePicker : SkiaView
BindableProperty.Create(nameof(HeaderColor), typeof(Color), typeof(SkiaTimePicker), Color.FromRgb(33, 150, 243), BindingMode.TwoWay, BindableProperty.Create(nameof(HeaderColor), typeof(Color), typeof(SkiaTimePicker), Color.FromRgb(33, 150, 243), BindingMode.TwoWay,
propertyChanged: (b, o, n) => ((SkiaTimePicker)b).Invalidate()); propertyChanged: (b, o, n) => ((SkiaTimePicker)b).Invalidate());
public static readonly BindableProperty FontFamilyProperty =
BindableProperty.Create(nameof(FontFamily), typeof(string), typeof(SkiaTimePicker), string.Empty, BindingMode.TwoWay,
propertyChanged: (b, o, n) => ((SkiaTimePicker)b).Invalidate());
public static readonly BindableProperty FontSizeProperty = public static readonly BindableProperty FontSizeProperty =
BindableProperty.Create(nameof(FontSize), typeof(double), typeof(SkiaTimePicker), 14.0, BindingMode.TwoWay, BindableProperty.Create(nameof(FontSize), typeof(double), typeof(SkiaTimePicker), 14.0, BindingMode.TwoWay,
propertyChanged: (b, o, n) => ((SkiaTimePicker)b).InvalidateMeasure()); propertyChanged: (b, o, n) => ((SkiaTimePicker)b).InvalidateMeasure());
public static readonly BindableProperty FontAttributesProperty =
BindableProperty.Create(nameof(FontAttributes), typeof(FontAttributes), typeof(SkiaTimePicker), FontAttributes.None, BindingMode.TwoWay,
propertyChanged: (b, o, n) => ((SkiaTimePicker)b).Invalidate());
public static readonly BindableProperty CharacterSpacingProperty =
BindableProperty.Create(nameof(CharacterSpacing), typeof(double), typeof(SkiaTimePicker), 0.0, BindingMode.TwoWay,
propertyChanged: (b, o, n) => ((SkiaTimePicker)b).Invalidate());
public static readonly BindableProperty CornerRadiusProperty = public static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create(nameof(CornerRadius), typeof(double), typeof(SkiaTimePicker), 4.0, BindingMode.TwoWay, BindableProperty.Create(nameof(CornerRadius), typeof(double), typeof(SkiaTimePicker), 4.0, BindingMode.TwoWay,
propertyChanged: (b, o, n) => ((SkiaTimePicker)b).Invalidate()); propertyChanged: (b, o, n) => ((SkiaTimePicker)b).Invalidate());
@@ -122,12 +135,30 @@ public class SkiaTimePicker : SkiaView
set => SetValue(HeaderColorProperty, value); set => SetValue(HeaderColorProperty, value);
} }
public string FontFamily
{
get => (string)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public double FontSize public double FontSize
{ {
get => (double)GetValue(FontSizeProperty); get => (double)GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value); set => SetValue(FontSizeProperty, value);
} }
public FontAttributes FontAttributes
{
get => (FontAttributes)GetValue(FontAttributesProperty);
set => SetValue(FontAttributesProperty, value);
}
public double CharacterSpacing
{
get => (double)GetValue(CharacterSpacingProperty);
set => SetValue(CharacterSpacingProperty, value);
}
public double CornerRadius public double CornerRadius
{ {
get => (double)GetValue(CornerRadiusProperty); get => (double)GetValue(CornerRadiusProperty);
@@ -282,7 +313,32 @@ public class SkiaTimePicker : SkiaView
}; };
canvas.DrawRoundRect(new SKRoundRect(bounds, cornerRadius), borderPaint); canvas.DrawRoundRect(new SKRoundRect(bounds, cornerRadius), borderPaint);
using var font = new SKFont(SKTypeface.Default, fontSize); // Get typeface based on FontFamily and FontAttributes
SKTypeface typeface = SKTypeface.Default;
if (!string.IsNullOrEmpty(FontFamily))
{
var style = FontAttributes switch
{
FontAttributes.Bold => SKFontStyle.Bold,
FontAttributes.Italic => SKFontStyle.Italic,
FontAttributes.Bold | FontAttributes.Italic => SKFontStyle.BoldItalic,
_ => SKFontStyle.Normal
};
typeface = SKTypeface.FromFamilyName(FontFamily, style) ?? SKTypeface.Default;
}
else if (FontAttributes != FontAttributes.None)
{
var style = FontAttributes switch
{
FontAttributes.Bold => SKFontStyle.Bold,
FontAttributes.Italic => SKFontStyle.Italic,
FontAttributes.Bold | FontAttributes.Italic => SKFontStyle.BoldItalic,
_ => SKFontStyle.Normal
};
typeface = SKTypeface.FromFamilyName(null, style) ?? SKTypeface.Default;
}
using var font = new SKFont(typeface, fontSize);
using var textPaint = new SKPaint(font) using var textPaint = new SKPaint(font)
{ {
Color = IsEnabled ? textColor : textColor.WithAlpha(128), Color = IsEnabled ? textColor : textColor.WithAlpha(128),