Files
maui-linux/Handlers/DatePickerHandler.cs

147 lines
5.0 KiB
C#
Raw Normal View History

// 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 Microsoft.Maui.Controls;
using Microsoft.Maui.Platform;
using SkiaSharp;
namespace Microsoft.Maui.Platform.Linux.Handlers;
/// <summary>
/// Handler for DatePicker on Linux using Skia rendering.
/// </summary>
public partial class DatePickerHandler : ViewHandler<IDatePicker, SkiaDatePicker>
{
public static IPropertyMapper<IDatePicker, DatePickerHandler> Mapper =
new PropertyMapper<IDatePicker, DatePickerHandler>(ViewHandler.ViewMapper)
{
[nameof(IDatePicker.Date)] = MapDate,
[nameof(IDatePicker.MinimumDate)] = MapMinimumDate,
[nameof(IDatePicker.MaximumDate)] = MapMaximumDate,
[nameof(IDatePicker.Format)] = MapFormat,
[nameof(IDatePicker.TextColor)] = MapTextColor,
[nameof(IDatePicker.CharacterSpacing)] = MapCharacterSpacing,
2026-01-16 05:36:22 +00:00
[nameof(ITextStyle.Font)] = MapFont,
[nameof(IView.Background)] = MapBackground,
};
public static CommandMapper<IDatePicker, DatePickerHandler> CommandMapper =
new(ViewHandler.ViewCommandMapper)
{
};
public DatePickerHandler() : base(Mapper, CommandMapper)
{
}
public DatePickerHandler(IPropertyMapper? mapper, CommandMapper? commandMapper = null)
: base(mapper ?? Mapper, commandMapper ?? CommandMapper)
{
}
protected override SkiaDatePicker CreatePlatformView()
{
return new SkiaDatePicker();
}
protected override void ConnectHandler(SkiaDatePicker platformView)
{
base.ConnectHandler(platformView);
platformView.DateSelected += OnDateSelected;
// Apply dark theme colors if dark mode is active
var current = Application.Current;
if (current != null && (int)current.UserAppTheme == 2) // Dark theme
{
2026-01-16 05:14:04 +00:00
platformView.CalendarBackgroundColor = Color.FromRgb(30, 30, 30);
platformView.TextColor = Color.FromRgb(224, 224, 224);
platformView.BorderColor = Color.FromRgb(97, 97, 97);
platformView.DisabledDayColor = Color.FromRgb(97, 97, 97);
platformView.BackgroundColor = Color.FromRgb(45, 45, 45).ToSKColor();
}
}
protected override void DisconnectHandler(SkiaDatePicker platformView)
{
platformView.DateSelected -= OnDateSelected;
base.DisconnectHandler(platformView);
}
2026-01-16 05:14:04 +00:00
private void OnDateSelected(object? sender, DateChangedEventArgs e)
{
if (VirtualView is null || PlatformView is null) return;
2026-01-16 05:14:04 +00:00
VirtualView.Date = e.NewDate;
}
public static void MapDate(DatePickerHandler handler, IDatePicker datePicker)
{
if (handler.PlatformView is null) return;
handler.PlatformView.Date = datePicker.Date;
}
public static void MapMinimumDate(DatePickerHandler handler, IDatePicker datePicker)
{
if (handler.PlatformView is null) return;
handler.PlatformView.MinimumDate = datePicker.MinimumDate;
}
public static void MapMaximumDate(DatePickerHandler handler, IDatePicker datePicker)
{
if (handler.PlatformView is null) return;
handler.PlatformView.MaximumDate = datePicker.MaximumDate;
}
public static void MapFormat(DatePickerHandler handler, IDatePicker datePicker)
{
if (handler.PlatformView is null) return;
handler.PlatformView.Format = datePicker.Format ?? "d";
}
public static void MapTextColor(DatePickerHandler handler, IDatePicker datePicker)
{
if (handler.PlatformView is null) return;
if (datePicker.TextColor is not null)
{
2026-01-16 05:14:04 +00:00
handler.PlatformView.TextColor = datePicker.TextColor;
}
}
public static void MapCharacterSpacing(DatePickerHandler handler, IDatePicker datePicker)
{
2026-01-16 05:36:22 +00:00
if (handler.PlatformView is null) return;
handler.PlatformView.CharacterSpacing = datePicker.CharacterSpacing;
}
public static void MapFont(DatePickerHandler handler, IDatePicker datePicker)
{
if (handler.PlatformView is null) return;
var font = datePicker.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;
// Note: Font.Slant for italic would require checking FontSlant
handler.PlatformView.FontAttributes = attrs;
}
public static void MapBackground(DatePickerHandler handler, IDatePicker datePicker)
{
if (handler.PlatformView is null) return;
if (datePicker.Background is SolidPaint solidPaint && solidPaint.Color is not null)
{
handler.PlatformView.BackgroundColor = solidPaint.Color.ToSKColor();
}
}
}