Fix handlers to match decompiled production code
- ButtonHandler: Removed MapText/TextColor/Font (not in production), fixed namespace - LabelHandler: Added CharacterSpacing/LayoutAlignment/FormattedText, ConnectHandler gesture logic - EntryHandler: Added CharacterSpacing/ClearButtonVisibility/VerticalTextAlignment - EditorHandler: Created from decompiled (was missing) - SliderHandler: Fixed namespace, added ConnectHandler init calls - SwitchHandler: Added OffTrackColor logic, fixed namespace - CheckBoxHandler: Added VerticalLayoutAlignment/HorizontalLayoutAlignment - ProgressBarHandler: Added ConnectHandler/DisconnectHandler IsVisible tracking - PickerHandler: Created from decompiled with collection changed tracking - ActivityIndicatorHandler: Removed IsEnabled/BackgroundColor (not in production) - All handlers now use namespace Microsoft.Maui.Platform.Linux.Handlers - All handlers have proper null checks on PlatformView - Updated MERGE_TRACKING.md with accurate status 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
16
Views/CheckedChangedEventArgs.cs
Normal file
16
Views/CheckedChangedEventArgs.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
|
||||
public class CheckedChangedEventArgs : EventArgs
|
||||
{
|
||||
public bool IsChecked { get; }
|
||||
|
||||
public CheckedChangedEventArgs(bool isChecked)
|
||||
{
|
||||
IsChecked = isChecked;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// 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.Controls;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
@@ -21,6 +22,7 @@ public class SkiaActivityIndicator : SkiaView
|
||||
typeof(bool),
|
||||
typeof(SkiaActivityIndicator),
|
||||
false,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaActivityIndicator)b).OnIsRunningChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -32,6 +34,7 @@ public class SkiaActivityIndicator : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaActivityIndicator),
|
||||
new SKColor(0x21, 0x96, 0xF3),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaActivityIndicator)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -43,6 +46,7 @@ public class SkiaActivityIndicator : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaActivityIndicator),
|
||||
new SKColor(0xBD, 0xBD, 0xBD),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaActivityIndicator)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -54,6 +58,7 @@ public class SkiaActivityIndicator : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaActivityIndicator),
|
||||
32f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaActivityIndicator)b).InvalidateMeasure());
|
||||
|
||||
/// <summary>
|
||||
@@ -65,6 +70,7 @@ public class SkiaActivityIndicator : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaActivityIndicator),
|
||||
3f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaActivityIndicator)b).InvalidateMeasure());
|
||||
|
||||
/// <summary>
|
||||
@@ -75,7 +81,8 @@ public class SkiaActivityIndicator : SkiaView
|
||||
nameof(RotationSpeed),
|
||||
typeof(float),
|
||||
typeof(SkiaActivityIndicator),
|
||||
360f);
|
||||
360f,
|
||||
BindingMode.TwoWay);
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for ArcCount.
|
||||
@@ -86,6 +93,7 @@ public class SkiaActivityIndicator : SkiaView
|
||||
typeof(int),
|
||||
typeof(SkiaActivityIndicator),
|
||||
12,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaActivityIndicator)b).Invalidate());
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System;
|
||||
using Microsoft.Maui.Controls;
|
||||
using Microsoft.Maui.Platform.Linux.Handlers;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
@@ -14,54 +17,56 @@ public class SkiaBorder : SkiaLayoutView
|
||||
|
||||
public static readonly BindableProperty StrokeThicknessProperty =
|
||||
BindableProperty.Create(nameof(StrokeThickness), typeof(float), typeof(SkiaBorder), 1f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty CornerRadiusProperty =
|
||||
BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(SkiaBorder), 0f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty StrokeProperty =
|
||||
BindableProperty.Create(nameof(Stroke), typeof(SKColor), typeof(SkiaBorder), SKColors.Black,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty PaddingLeftProperty =
|
||||
BindableProperty.Create(nameof(PaddingLeft), typeof(float), typeof(SkiaBorder), 0f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).InvalidateMeasure());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).InvalidateMeasure());
|
||||
|
||||
public static readonly BindableProperty PaddingTopProperty =
|
||||
BindableProperty.Create(nameof(PaddingTop), typeof(float), typeof(SkiaBorder), 0f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).InvalidateMeasure());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).InvalidateMeasure());
|
||||
|
||||
public static readonly BindableProperty PaddingRightProperty =
|
||||
BindableProperty.Create(nameof(PaddingRight), typeof(float), typeof(SkiaBorder), 0f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).InvalidateMeasure());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).InvalidateMeasure());
|
||||
|
||||
public static readonly BindableProperty PaddingBottomProperty =
|
||||
BindableProperty.Create(nameof(PaddingBottom), typeof(float), typeof(SkiaBorder), 0f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).InvalidateMeasure());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).InvalidateMeasure());
|
||||
|
||||
public static readonly BindableProperty HasShadowProperty =
|
||||
BindableProperty.Create(nameof(HasShadow), typeof(bool), typeof(SkiaBorder), false,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty ShadowColorProperty =
|
||||
BindableProperty.Create(nameof(ShadowColor), typeof(SKColor), typeof(SkiaBorder), new SKColor(0, 0, 0, 40),
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty ShadowBlurRadiusProperty =
|
||||
BindableProperty.Create(nameof(ShadowBlurRadius), typeof(float), typeof(SkiaBorder), 4f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty ShadowOffsetXProperty =
|
||||
BindableProperty.Create(nameof(ShadowOffsetX), typeof(float), typeof(SkiaBorder), 2f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty ShadowOffsetYProperty =
|
||||
BindableProperty.Create(nameof(ShadowOffsetY), typeof(float), typeof(SkiaBorder), 2f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBorder)b).Invalidate());
|
||||
|
||||
#endregion
|
||||
|
||||
private bool _isPressed;
|
||||
|
||||
#region Properties
|
||||
|
||||
public float StrokeThickness
|
||||
@@ -138,6 +143,14 @@ public class SkiaBorder : SkiaLayoutView
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
public event EventHandler? Tapped;
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetPadding Methods
|
||||
|
||||
/// <summary>
|
||||
/// Sets uniform padding on all sides.
|
||||
/// </summary>
|
||||
@@ -166,16 +179,18 @@ public class SkiaBorder : SkiaLayoutView
|
||||
PaddingBottom = bottom;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected override void OnDraw(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
var strokeThickness = StrokeThickness;
|
||||
var cornerRadius = CornerRadius;
|
||||
|
||||
var borderRect = new SKRect(
|
||||
bounds.Left + strokeThickness / 2,
|
||||
bounds.Top + strokeThickness / 2,
|
||||
bounds.Right - strokeThickness / 2,
|
||||
bounds.Bottom - strokeThickness / 2);
|
||||
bounds.Left + strokeThickness / 2f,
|
||||
bounds.Top + strokeThickness / 2f,
|
||||
bounds.Right - strokeThickness / 2f,
|
||||
bounds.Bottom - strokeThickness / 2f);
|
||||
|
||||
// Draw shadow if enabled
|
||||
if (HasShadow)
|
||||
@@ -204,7 +219,7 @@ public class SkiaBorder : SkiaLayoutView
|
||||
canvas.DrawRoundRect(new SKRoundRect(borderRect, cornerRadius), bgPaint);
|
||||
|
||||
// Draw border
|
||||
if (strokeThickness > 0)
|
||||
if (strokeThickness > 0f)
|
||||
{
|
||||
using var borderPaint = new SKPaint
|
||||
{
|
||||
@@ -244,16 +259,16 @@ public class SkiaBorder : SkiaLayoutView
|
||||
protected override SKSize MeasureOverride(SKSize availableSize)
|
||||
{
|
||||
var strokeThickness = StrokeThickness;
|
||||
var paddingWidth = PaddingLeft + PaddingRight + strokeThickness * 2;
|
||||
var paddingHeight = PaddingTop + PaddingBottom + strokeThickness * 2;
|
||||
var paddingWidth = PaddingLeft + PaddingRight + strokeThickness * 2f;
|
||||
var paddingHeight = PaddingTop + PaddingBottom + strokeThickness * 2f;
|
||||
|
||||
// Respect explicit size requests
|
||||
var requestedWidth = WidthRequest >= 0 ? (float)WidthRequest : availableSize.Width;
|
||||
var requestedHeight = HeightRequest >= 0 ? (float)HeightRequest : availableSize.Height;
|
||||
var requestedWidth = WidthRequest >= 0.0 ? (float)WidthRequest : availableSize.Width;
|
||||
var requestedHeight = HeightRequest >= 0.0 ? (float)HeightRequest : availableSize.Height;
|
||||
|
||||
var childAvailable = new SKSize(
|
||||
Math.Max(0, requestedWidth - paddingWidth),
|
||||
Math.Max(0, requestedHeight - paddingHeight));
|
||||
Math.Max(0f, requestedWidth - paddingWidth),
|
||||
Math.Max(0f, requestedHeight - paddingHeight));
|
||||
|
||||
var maxChildSize = SKSize.Empty;
|
||||
|
||||
@@ -266,8 +281,8 @@ public class SkiaBorder : SkiaLayoutView
|
||||
}
|
||||
|
||||
// Use requested size if set, otherwise use child size + padding
|
||||
var width = WidthRequest >= 0 ? (float)WidthRequest : maxChildSize.Width + paddingWidth;
|
||||
var height = HeightRequest >= 0 ? (float)HeightRequest : maxChildSize.Height + paddingHeight;
|
||||
var width = WidthRequest >= 0.0 ? (float)WidthRequest : maxChildSize.Width + paddingWidth;
|
||||
var height = HeightRequest >= 0.0 ? (float)HeightRequest : maxChildSize.Height + paddingHeight;
|
||||
|
||||
return new SKSize(width, height);
|
||||
}
|
||||
@@ -290,6 +305,85 @@ public class SkiaBorder : SkiaLayoutView
|
||||
|
||||
return bounds;
|
||||
}
|
||||
|
||||
private bool HasTapGestureRecognizers()
|
||||
{
|
||||
if (MauiView?.GestureRecognizers == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var gestureRecognizer in MauiView.GestureRecognizers)
|
||||
{
|
||||
if (gestureRecognizer is TapGestureRecognizer)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override SkiaView? HitTest(float x, float y)
|
||||
{
|
||||
if (IsVisible && IsEnabled)
|
||||
{
|
||||
var bounds = Bounds;
|
||||
if (bounds.Contains(new SKPoint(x, y)))
|
||||
{
|
||||
if (HasTapGestureRecognizers())
|
||||
{
|
||||
Console.WriteLine("[SkiaBorder.HitTest] Intercepting for gesture - returning self");
|
||||
return this;
|
||||
}
|
||||
return base.HitTest(x, y);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void OnPointerPressed(PointerEventArgs e)
|
||||
{
|
||||
if (HasTapGestureRecognizers())
|
||||
{
|
||||
_isPressed = true;
|
||||
e.Handled = true;
|
||||
Console.WriteLine("[SkiaBorder] OnPointerPressed INTERCEPTED for gesture, MauiView=" + MauiView?.GetType().Name);
|
||||
if (MauiView != null)
|
||||
{
|
||||
GestureManager.ProcessPointerDown(MauiView, e.X, e.Y);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
base.OnPointerPressed(e);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnPointerReleased(PointerEventArgs e)
|
||||
{
|
||||
if (_isPressed)
|
||||
{
|
||||
_isPressed = false;
|
||||
e.Handled = true;
|
||||
Console.WriteLine("[SkiaBorder] OnPointerReleased - processing gesture recognizers, MauiView=" + MauiView?.GetType().Name);
|
||||
if (MauiView != null)
|
||||
{
|
||||
GestureManager.ProcessPointerUp(MauiView, e.X, e.Y);
|
||||
}
|
||||
Tapped?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.OnPointerReleased(e);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnPointerExited(PointerEventArgs e)
|
||||
{
|
||||
base.OnPointerExited(e);
|
||||
_isPressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// 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.Controls;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
@@ -12,11 +13,11 @@ public class SkiaBoxView : SkiaView
|
||||
{
|
||||
public static readonly BindableProperty ColorProperty =
|
||||
BindableProperty.Create(nameof(Color), typeof(SKColor), typeof(SkiaBoxView), SKColors.Transparent,
|
||||
propertyChanged: (b, o, n) => ((SkiaBoxView)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBoxView)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty CornerRadiusProperty =
|
||||
BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(SkiaBoxView), 0f,
|
||||
propertyChanged: (b, o, n) => ((SkiaBoxView)b).Invalidate());
|
||||
BindingMode.TwoWay, propertyChanged: (b, o, n) => ((SkiaBoxView)b).Invalidate());
|
||||
|
||||
public SKColor Color
|
||||
{
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,9 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System;
|
||||
using Microsoft.Maui.Controls;
|
||||
using SkiaSharp;
|
||||
using Microsoft.Maui.Platform.Linux.Rendering;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
|
||||
@@ -13,241 +14,179 @@ public class SkiaCheckBox : SkiaView
|
||||
{
|
||||
#region BindableProperties
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for IsChecked.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty IsCheckedProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(IsChecked),
|
||||
typeof(bool),
|
||||
typeof(SkiaCheckBox),
|
||||
false,
|
||||
BindingMode.TwoWay,
|
||||
BindingMode.OneWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).OnIsCheckedChanged());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for CheckColor.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty CheckColorProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(CheckColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCheckBox),
|
||||
SKColors.White,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for BoxColor.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty BoxColorProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(BoxColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCheckBox),
|
||||
new SKColor(0x21, 0x96, 0xF3),
|
||||
new SKColor(33, 150, 243),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for UncheckedBoxColor.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty UncheckedBoxColorProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(UncheckedBoxColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCheckBox),
|
||||
SKColors.White,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for BorderColor.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty BorderColorProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(BorderColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCheckBox),
|
||||
new SKColor(0x75, 0x75, 0x75),
|
||||
new SKColor(117, 117, 117),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for DisabledColor.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty DisabledColorProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(DisabledColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCheckBox),
|
||||
new SKColor(0xBD, 0xBD, 0xBD),
|
||||
new SKColor(189, 189, 189),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for HoveredBorderColor.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty HoveredBorderColorProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(HoveredBorderColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCheckBox),
|
||||
new SKColor(0x21, 0x96, 0xF3),
|
||||
new SKColor(33, 150, 243),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for BoxSize.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty BoxSizeProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(BoxSize),
|
||||
typeof(float),
|
||||
typeof(SkiaCheckBox),
|
||||
20f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).InvalidateMeasure());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for CornerRadius.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty CornerRadiusProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(CornerRadius),
|
||||
typeof(float),
|
||||
typeof(SkiaCheckBox),
|
||||
3f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for BorderWidth.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty BorderWidthProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(BorderWidth),
|
||||
typeof(float),
|
||||
typeof(SkiaCheckBox),
|
||||
2f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for CheckStrokeWidth.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty CheckStrokeWidthProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(CheckStrokeWidth),
|
||||
typeof(float),
|
||||
typeof(SkiaCheckBox),
|
||||
2.5f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCheckBox)b).Invalidate());
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the checkbox is checked.
|
||||
/// </summary>
|
||||
public bool IsChecked
|
||||
{
|
||||
get => (bool)GetValue(IsCheckedProperty);
|
||||
set => SetValue(IsCheckedProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the check color.
|
||||
/// </summary>
|
||||
public SKColor CheckColor
|
||||
{
|
||||
get => (SKColor)GetValue(CheckColorProperty);
|
||||
set => SetValue(CheckColorProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the box color when checked.
|
||||
/// </summary>
|
||||
public SKColor BoxColor
|
||||
{
|
||||
get => (SKColor)GetValue(BoxColorProperty);
|
||||
set => SetValue(BoxColorProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the box color when unchecked.
|
||||
/// </summary>
|
||||
public SKColor UncheckedBoxColor
|
||||
{
|
||||
get => (SKColor)GetValue(UncheckedBoxColorProperty);
|
||||
set => SetValue(UncheckedBoxColorProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the border color.
|
||||
/// </summary>
|
||||
public SKColor BorderColor
|
||||
{
|
||||
get => (SKColor)GetValue(BorderColorProperty);
|
||||
set => SetValue(BorderColorProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the disabled color.
|
||||
/// </summary>
|
||||
public SKColor DisabledColor
|
||||
{
|
||||
get => (SKColor)GetValue(DisabledColorProperty);
|
||||
set => SetValue(DisabledColorProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the hovered border color.
|
||||
/// </summary>
|
||||
public SKColor HoveredBorderColor
|
||||
{
|
||||
get => (SKColor)GetValue(HoveredBorderColorProperty);
|
||||
set => SetValue(HoveredBorderColorProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the box size.
|
||||
/// </summary>
|
||||
public float BoxSize
|
||||
{
|
||||
get => (float)GetValue(BoxSizeProperty);
|
||||
set => SetValue(BoxSizeProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the corner radius.
|
||||
/// </summary>
|
||||
public float CornerRadius
|
||||
{
|
||||
get => (float)GetValue(CornerRadiusProperty);
|
||||
set => SetValue(CornerRadiusProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the border width.
|
||||
/// </summary>
|
||||
public float BorderWidth
|
||||
{
|
||||
get => (float)GetValue(BorderWidthProperty);
|
||||
set => SetValue(BorderWidthProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the check stroke width.
|
||||
/// </summary>
|
||||
public float CheckStrokeWidth
|
||||
{
|
||||
get => (float)GetValue(CheckStrokeWidthProperty);
|
||||
set => SetValue(CheckStrokeWidthProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the pointer is over the checkbox.
|
||||
/// </summary>
|
||||
public bool IsHovered { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when checked state changes.
|
||||
/// </summary>
|
||||
public event EventHandler<CheckedChangedEventArgs>? CheckedChanged;
|
||||
|
||||
public SkiaCheckBox()
|
||||
@@ -258,7 +197,7 @@ public class SkiaCheckBox : SkiaView
|
||||
private void OnIsCheckedChanged()
|
||||
{
|
||||
CheckedChanged?.Invoke(this, new CheckedChangedEventArgs(IsChecked));
|
||||
SkiaVisualStateManager.GoToState(this, IsChecked ? SkiaVisualStateManager.CommonStates.Checked : SkiaVisualStateManager.CommonStates.Unchecked);
|
||||
SkiaVisualStateManager.GoToState(this, IsChecked ? "Checked" : "Unchecked");
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
@@ -266,13 +205,19 @@ public class SkiaCheckBox : SkiaView
|
||||
{
|
||||
// Center the checkbox box in bounds
|
||||
var boxRect = new SKRect(
|
||||
bounds.Left + (bounds.Width - BoxSize) / 2,
|
||||
bounds.Top + (bounds.Height - BoxSize) / 2,
|
||||
bounds.Left + (bounds.Width - BoxSize) / 2 + BoxSize,
|
||||
bounds.Top + (bounds.Height - BoxSize) / 2 + BoxSize);
|
||||
bounds.Left + (bounds.Width - BoxSize) / 2f,
|
||||
bounds.Top + (bounds.Height - BoxSize) / 2f,
|
||||
bounds.Left + (bounds.Width - BoxSize) / 2f + BoxSize,
|
||||
bounds.Top + (bounds.Height - BoxSize) / 2f + BoxSize);
|
||||
|
||||
var roundRect = new SKRoundRect(boxRect, CornerRadius);
|
||||
|
||||
// Debug logging when checked
|
||||
if (IsChecked)
|
||||
{
|
||||
Console.WriteLine($"[SkiaCheckBox] OnDraw CHECKED - BoxColor=({BoxColor.Red},{BoxColor.Green},{BoxColor.Blue}), UncheckedBoxColor=({UncheckedBoxColor.Red},{UncheckedBoxColor.Green},{UncheckedBoxColor.Blue})");
|
||||
}
|
||||
|
||||
// Draw background
|
||||
using var bgPaint = new SKPaint
|
||||
{
|
||||
@@ -305,10 +250,10 @@ public class SkiaCheckBox : SkiaView
|
||||
Color = BoxColor.WithAlpha(80),
|
||||
IsAntialias = true,
|
||||
Style = SKPaintStyle.Stroke,
|
||||
StrokeWidth = 3
|
||||
StrokeWidth = 3f
|
||||
};
|
||||
var focusRect = new SKRoundRect(boxRect, CornerRadius);
|
||||
focusRect.Inflate(4, 4);
|
||||
focusRect.Inflate(4f, 4f);
|
||||
canvas.DrawRoundRect(focusRect, focusPaint);
|
||||
}
|
||||
|
||||
@@ -323,7 +268,7 @@ public class SkiaCheckBox : SkiaView
|
||||
{
|
||||
using var paint = new SKPaint
|
||||
{
|
||||
Color = CheckColor,
|
||||
Color = SKColors.White,
|
||||
IsAntialias = true,
|
||||
Style = SKPaintStyle.Stroke,
|
||||
StrokeWidth = CheckStrokeWidth,
|
||||
@@ -331,14 +276,12 @@ public class SkiaCheckBox : SkiaView
|
||||
StrokeJoin = SKStrokeJoin.Round
|
||||
};
|
||||
|
||||
// Checkmark path - a simple check
|
||||
var padding = BoxSize * 0.2f;
|
||||
var left = boxRect.Left + padding;
|
||||
var right = boxRect.Right - padding;
|
||||
var top = boxRect.Top + padding;
|
||||
var bottom = boxRect.Bottom - padding;
|
||||
float padding = BoxSize * 0.2f;
|
||||
float left = boxRect.Left + padding;
|
||||
float right = boxRect.Right - padding;
|
||||
float top = boxRect.Top + padding;
|
||||
float bottom = boxRect.Bottom - padding;
|
||||
|
||||
// Check starts from bottom-left, goes to middle-bottom, then to top-right
|
||||
using var path = new SKPath();
|
||||
path.MoveTo(left, boxRect.MidY);
|
||||
path.LineTo(boxRect.MidX - padding * 0.3f, bottom - padding * 0.5f);
|
||||
@@ -349,37 +292,37 @@ public class SkiaCheckBox : SkiaView
|
||||
|
||||
public override void OnPointerEntered(PointerEventArgs e)
|
||||
{
|
||||
if (!IsEnabled) return;
|
||||
IsHovered = true;
|
||||
SkiaVisualStateManager.GoToState(this, SkiaVisualStateManager.CommonStates.PointerOver);
|
||||
Invalidate();
|
||||
if (IsEnabled)
|
||||
{
|
||||
IsHovered = true;
|
||||
SkiaVisualStateManager.GoToState(this, "PointerOver");
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnPointerExited(PointerEventArgs e)
|
||||
{
|
||||
IsHovered = false;
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? SkiaVisualStateManager.CommonStates.Normal : SkiaVisualStateManager.CommonStates.Disabled);
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled");
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
public override void OnPointerPressed(PointerEventArgs e)
|
||||
{
|
||||
if (!IsEnabled) return;
|
||||
IsChecked = !IsChecked;
|
||||
e.Handled = true;
|
||||
if (IsEnabled)
|
||||
{
|
||||
IsChecked = !IsChecked;
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnPointerReleased(PointerEventArgs e)
|
||||
{
|
||||
// Toggle handled in OnPointerPressed
|
||||
}
|
||||
|
||||
public override void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (!IsEnabled) return;
|
||||
|
||||
// Toggle on Space
|
||||
if (e.Key == Key.Space)
|
||||
if (IsEnabled && e.Key == Key.Space)
|
||||
{
|
||||
IsChecked = !IsChecked;
|
||||
e.Handled = true;
|
||||
@@ -389,25 +332,11 @@ public class SkiaCheckBox : SkiaView
|
||||
protected override void OnEnabledChanged()
|
||||
{
|
||||
base.OnEnabledChanged();
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? SkiaVisualStateManager.CommonStates.Normal : SkiaVisualStateManager.CommonStates.Disabled);
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled");
|
||||
}
|
||||
|
||||
protected override SKSize MeasureOverride(SKSize availableSize)
|
||||
{
|
||||
// Add some padding around the box for touch targets
|
||||
return new SKSize(BoxSize + 8, BoxSize + 8);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event args for checked changed events.
|
||||
/// </summary>
|
||||
public class CheckedChangedEventArgs : EventArgs
|
||||
{
|
||||
public bool IsChecked { get; }
|
||||
|
||||
public CheckedChangedEventArgs(bool isChecked)
|
||||
{
|
||||
IsChecked = isChecked;
|
||||
return new SKSize(BoxSize + 8f, BoxSize + 8f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Maui.Controls;
|
||||
using SkiaSharp;
|
||||
using System.Collections;
|
||||
using Microsoft.Maui.Graphics;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
|
||||
@@ -33,203 +35,110 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
{
|
||||
#region BindableProperties
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for SelectionMode.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty SelectionModeProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(SelectionMode),
|
||||
typeof(SkiaSelectionMode),
|
||||
typeof(SkiaCollectionView),
|
||||
SkiaSelectionMode.Single,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).OnSelectionModeChanged());
|
||||
public static readonly BindableProperty SelectionModeProperty = BindableProperty.Create(
|
||||
nameof(SelectionMode),
|
||||
typeof(SkiaSelectionMode),
|
||||
typeof(SkiaCollectionView),
|
||||
SkiaSelectionMode.Single,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).OnSelectionModeChanged());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for SelectedItem.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty SelectedItemProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(SelectedItem),
|
||||
typeof(object),
|
||||
typeof(SkiaCollectionView),
|
||||
null,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).OnSelectedItemChanged(n));
|
||||
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create(
|
||||
nameof(SelectedItem),
|
||||
typeof(object),
|
||||
typeof(SkiaCollectionView),
|
||||
null,
|
||||
BindingMode.OneWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).OnSelectedItemChanged(n));
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for Orientation.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty OrientationProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(Orientation),
|
||||
typeof(ItemsLayoutOrientation),
|
||||
typeof(SkiaCollectionView),
|
||||
ItemsLayoutOrientation.Vertical,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
public static readonly BindableProperty OrientationProperty = BindableProperty.Create(
|
||||
nameof(Orientation),
|
||||
typeof(ItemsLayoutOrientation),
|
||||
typeof(SkiaCollectionView),
|
||||
ItemsLayoutOrientation.Vertical,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for SpanCount.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty SpanCountProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(SpanCount),
|
||||
typeof(int),
|
||||
typeof(SkiaCollectionView),
|
||||
1,
|
||||
coerceValue: (b, v) => Math.Max(1, (int)v),
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
public static readonly BindableProperty SpanCountProperty = BindableProperty.Create(
|
||||
nameof(SpanCount),
|
||||
typeof(int),
|
||||
typeof(SkiaCollectionView),
|
||||
1,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate(),
|
||||
coerceValue: (b, v) => Math.Max(1, (int)v));
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for GridItemWidth.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty GridItemWidthProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(GridItemWidth),
|
||||
typeof(float),
|
||||
typeof(SkiaCollectionView),
|
||||
100f,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
public static readonly BindableProperty GridItemWidthProperty = BindableProperty.Create(
|
||||
nameof(GridItemWidth),
|
||||
typeof(float),
|
||||
typeof(SkiaCollectionView),
|
||||
100f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for Header.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty HeaderProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(Header),
|
||||
typeof(object),
|
||||
typeof(SkiaCollectionView),
|
||||
null,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).OnHeaderChanged(n));
|
||||
public static readonly BindableProperty HeaderProperty = BindableProperty.Create(
|
||||
nameof(Header),
|
||||
typeof(object),
|
||||
typeof(SkiaCollectionView),
|
||||
null,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).OnHeaderChanged(n));
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for Footer.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty FooterProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(Footer),
|
||||
typeof(object),
|
||||
typeof(SkiaCollectionView),
|
||||
null,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).OnFooterChanged(n));
|
||||
public static readonly BindableProperty FooterProperty = BindableProperty.Create(
|
||||
nameof(Footer),
|
||||
typeof(object),
|
||||
typeof(SkiaCollectionView),
|
||||
null,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).OnFooterChanged(n));
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for HeaderHeight.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty HeaderHeightProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(HeaderHeight),
|
||||
typeof(float),
|
||||
typeof(SkiaCollectionView),
|
||||
0f,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
public static readonly BindableProperty HeaderHeightProperty = BindableProperty.Create(
|
||||
nameof(HeaderHeight),
|
||||
typeof(float),
|
||||
typeof(SkiaCollectionView),
|
||||
0f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for FooterHeight.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty FooterHeightProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(FooterHeight),
|
||||
typeof(float),
|
||||
typeof(SkiaCollectionView),
|
||||
0f,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
public static readonly BindableProperty FooterHeightProperty = BindableProperty.Create(
|
||||
nameof(FooterHeight),
|
||||
typeof(float),
|
||||
typeof(SkiaCollectionView),
|
||||
0f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for SelectionColor.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty SelectionColorProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(SelectionColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCollectionView),
|
||||
new SKColor(0x21, 0x96, 0xF3, 0x59),
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
public static readonly BindableProperty SelectionColorProperty = BindableProperty.Create(
|
||||
nameof(SelectionColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCollectionView),
|
||||
new SKColor(33, 150, 243, 89),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for HeaderBackgroundColor.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty HeaderBackgroundColorProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(HeaderBackgroundColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCollectionView),
|
||||
new SKColor(0xF5, 0xF5, 0xF5),
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
public static readonly BindableProperty HeaderBackgroundColorProperty = BindableProperty.Create(
|
||||
nameof(HeaderBackgroundColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCollectionView),
|
||||
new SKColor(245, 245, 245),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
/// Bindable property for FooterBackgroundColor.
|
||||
/// </summary>
|
||||
public static readonly BindableProperty FooterBackgroundColorProperty =
|
||||
BindableProperty.Create(
|
||||
nameof(FooterBackgroundColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCollectionView),
|
||||
new SKColor(0xF5, 0xF5, 0xF5),
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
public static readonly BindableProperty FooterBackgroundColorProperty = BindableProperty.Create(
|
||||
nameof(FooterBackgroundColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaCollectionView),
|
||||
new SKColor(245, 245, 245),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaCollectionView)b).Invalidate());
|
||||
|
||||
#endregion
|
||||
|
||||
private List<object> _selectedItems = new();
|
||||
private List<object> _selectedItems = new List<object>();
|
||||
private int _selectedIndex = -1;
|
||||
|
||||
// Track if heights changed during draw (requires redraw for correct positioning)
|
||||
private bool _isSelectingItem;
|
||||
private bool _heightsChangedDuringDraw;
|
||||
|
||||
// Uses parent's _itemViewCache for virtualization
|
||||
|
||||
protected override void RefreshItems()
|
||||
{
|
||||
// Clear selection when items change to avoid stale references
|
||||
_selectedItems.Clear();
|
||||
SetValue(SelectedItemProperty, null);
|
||||
_selectedIndex = -1;
|
||||
|
||||
base.RefreshItems();
|
||||
}
|
||||
|
||||
private void OnSelectionModeChanged()
|
||||
{
|
||||
var mode = SelectionMode;
|
||||
if (mode == SkiaSelectionMode.None)
|
||||
{
|
||||
ClearSelection();
|
||||
}
|
||||
else if (mode == SkiaSelectionMode.Single && _selectedItems.Count > 1)
|
||||
{
|
||||
// Keep only first selected
|
||||
var first = _selectedItems.FirstOrDefault();
|
||||
ClearSelection();
|
||||
if (first != null)
|
||||
{
|
||||
SelectItem(first);
|
||||
}
|
||||
}
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
private void OnSelectedItemChanged(object? newValue)
|
||||
{
|
||||
if (SelectionMode == SkiaSelectionMode.None) return;
|
||||
|
||||
ClearSelection();
|
||||
if (newValue != null)
|
||||
{
|
||||
SelectItem(newValue);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHeaderChanged(object? newValue)
|
||||
{
|
||||
HeaderHeight = newValue != null ? 44 : 0;
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
private void OnFooterChanged(object? newValue)
|
||||
{
|
||||
FooterHeight = newValue != null ? 44 : 0;
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
public SkiaSelectionMode SelectionMode
|
||||
{
|
||||
get => (SkiaSelectionMode)GetValue(SelectionModeProperty);
|
||||
@@ -249,12 +158,13 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
get => _selectedIndex;
|
||||
set
|
||||
{
|
||||
if (SelectionMode == SkiaSelectionMode.None) return;
|
||||
|
||||
var item = GetItemAt(value);
|
||||
if (item != null)
|
||||
if (SelectionMode != SkiaSelectionMode.None)
|
||||
{
|
||||
SelectedItem = item;
|
||||
var item = GetItemAt(value);
|
||||
if (item != null)
|
||||
{
|
||||
SelectedItem = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -321,11 +231,68 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
|
||||
public event EventHandler<CollectionSelectionChangedEventArgs>? SelectionChanged;
|
||||
|
||||
protected override void RefreshItems()
|
||||
{
|
||||
_selectedItems.Clear();
|
||||
SetValue(SelectedItemProperty, null);
|
||||
_selectedIndex = -1;
|
||||
base.RefreshItems();
|
||||
}
|
||||
|
||||
private void OnSelectionModeChanged()
|
||||
{
|
||||
switch (SelectionMode)
|
||||
{
|
||||
case SkiaSelectionMode.None:
|
||||
ClearSelection();
|
||||
break;
|
||||
case SkiaSelectionMode.Single:
|
||||
if (_selectedItems.Count > 1)
|
||||
{
|
||||
var first = _selectedItems.FirstOrDefault();
|
||||
ClearSelection();
|
||||
if (first != null)
|
||||
{
|
||||
SelectItem(first);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
private void OnSelectedItemChanged(object? newValue)
|
||||
{
|
||||
if (SelectionMode != SkiaSelectionMode.None && !_isSelectingItem)
|
||||
{
|
||||
ClearSelection();
|
||||
if (newValue != null)
|
||||
{
|
||||
SelectItem(newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHeaderChanged(object? newValue)
|
||||
{
|
||||
HeaderHeight = newValue != null ? 44 : 0;
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
private void OnFooterChanged(object? newValue)
|
||||
{
|
||||
FooterHeight = newValue != null ? 44 : 0;
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
private void SelectItem(object item)
|
||||
{
|
||||
if (SelectionMode == SkiaSelectionMode.None) return;
|
||||
if (SelectionMode == SkiaSelectionMode.None)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var oldSelectedItems = _selectedItems.ToList();
|
||||
var previousSelection = _selectedItems.ToList();
|
||||
|
||||
if (SelectionMode == SkiaSelectionMode.Single)
|
||||
{
|
||||
@@ -333,7 +300,6 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
_selectedItems.Add(item);
|
||||
SetValue(SelectedItemProperty, item);
|
||||
|
||||
// Find index
|
||||
for (int i = 0; i < ItemCount; i++)
|
||||
{
|
||||
if (GetItemAt(i) == item)
|
||||
@@ -343,7 +309,7 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
}
|
||||
}
|
||||
}
|
||||
else // Multiple
|
||||
else
|
||||
{
|
||||
if (_selectedItems.Contains(item))
|
||||
{
|
||||
@@ -358,11 +324,10 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
_selectedItems.Add(item);
|
||||
SetValue(SelectedItemProperty, item);
|
||||
}
|
||||
|
||||
_selectedIndex = SelectedItem != null ? GetIndexOf(SelectedItem) : -1;
|
||||
}
|
||||
|
||||
SelectionChanged?.Invoke(this, new CollectionSelectionChangedEventArgs(oldSelectedItems, _selectedItems.ToList()));
|
||||
SelectionChanged?.Invoke(this, new CollectionSelectionChangedEventArgs(previousSelection, _selectedItems.ToList()));
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
@@ -371,51 +336,62 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
for (int i = 0; i < ItemCount; i++)
|
||||
{
|
||||
if (GetItemAt(i) == item)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private void ClearSelection()
|
||||
{
|
||||
var oldItems = _selectedItems.ToList();
|
||||
var previousItems = _selectedItems.ToList();
|
||||
_selectedItems.Clear();
|
||||
SetValue(SelectedItemProperty, null);
|
||||
_selectedIndex = -1;
|
||||
|
||||
if (oldItems.Count > 0)
|
||||
if (previousItems.Count > 0)
|
||||
{
|
||||
SelectionChanged?.Invoke(this, new CollectionSelectionChangedEventArgs(oldItems, new List<object>()));
|
||||
SelectionChanged?.Invoke(this, new CollectionSelectionChangedEventArgs(previousItems, new List<object>()));
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnItemTapped(int index, object item)
|
||||
{
|
||||
if (SelectionMode != SkiaSelectionMode.None)
|
||||
if (_isSelectingItem)
|
||||
{
|
||||
SelectItem(item);
|
||||
return;
|
||||
}
|
||||
|
||||
base.OnItemTapped(index, item);
|
||||
_isSelectingItem = true;
|
||||
try
|
||||
{
|
||||
if (SelectionMode != SkiaSelectionMode.None)
|
||||
{
|
||||
SelectItem(item);
|
||||
}
|
||||
base.OnItemTapped(index, item);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isSelectingItem = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void DrawItem(SKCanvas canvas, object item, int index, SKRect bounds, SKPaint paint)
|
||||
{
|
||||
bool isSelected = _selectedItems.Contains(item);
|
||||
|
||||
// Draw separator (only for vertical list layout)
|
||||
if (Orientation == ItemsLayoutOrientation.Vertical && SpanCount == 1)
|
||||
{
|
||||
paint.Color = new SKColor(0xE0, 0xE0, 0xE0);
|
||||
paint.Color = new SKColor(224, 224, 224);
|
||||
paint.Style = SKPaintStyle.Stroke;
|
||||
paint.StrokeWidth = 1;
|
||||
paint.StrokeWidth = 1f;
|
||||
canvas.DrawLine(bounds.Left, bounds.Bottom, bounds.Right, bounds.Bottom, paint);
|
||||
}
|
||||
|
||||
// Try to use ItemViewCreator for templated rendering (from DataTemplate)
|
||||
if (ItemViewCreator != null)
|
||||
{
|
||||
// Get or create cached view for this index
|
||||
if (!_itemViewCache.TryGetValue(index, out var itemView) || itemView == null)
|
||||
{
|
||||
itemView = ItemViewCreator(item);
|
||||
@@ -430,64 +406,56 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
{
|
||||
try
|
||||
{
|
||||
// Measure with large height to get natural size
|
||||
var availableSize = new SKSize(bounds.Width, float.MaxValue);
|
||||
var measuredSize = itemView.Measure(availableSize);
|
||||
|
||||
// Cap measured height - if item returns infinity/MaxValue, use ItemHeight as default
|
||||
// This happens with Star-sized Grids that have no natural height preference
|
||||
var rawHeight = measuredSize.Height;
|
||||
if (float.IsNaN(rawHeight) || float.IsInfinity(rawHeight) || rawHeight > 10000)
|
||||
if (float.IsNaN(rawHeight) || float.IsInfinity(rawHeight) || rawHeight > 10000f)
|
||||
{
|
||||
rawHeight = ItemHeight;
|
||||
}
|
||||
// Ensure minimum height
|
||||
|
||||
var measuredHeight = Math.Max(rawHeight, ItemHeight);
|
||||
if (!_itemHeights.TryGetValue(index, out var cachedHeight) || Math.Abs(cachedHeight - measuredHeight) > 1)
|
||||
if (!_itemHeights.TryGetValue(index, out var cachedHeight) || Math.Abs(cachedHeight - measuredHeight) > 1f)
|
||||
{
|
||||
_itemHeights[index] = measuredHeight;
|
||||
_heightsChangedDuringDraw = true; // Flag for redraw with correct positions
|
||||
_heightsChangedDuringDraw = true;
|
||||
}
|
||||
|
||||
// Arrange with the actual measured height
|
||||
var actualBounds = new SKRect(bounds.Left, bounds.Top, bounds.Right, bounds.Top + measuredHeight);
|
||||
itemView.Arrange(actualBounds);
|
||||
itemView.Draw(canvas);
|
||||
|
||||
// Draw selection highlight ON TOP of the item content (semi-transparent overlay)
|
||||
if (isSelected)
|
||||
{
|
||||
paint.Color = SelectionColor;
|
||||
paint.Style = SKPaintStyle.Fill;
|
||||
canvas.DrawRoundRect(actualBounds, 12, 12, paint);
|
||||
canvas.DrawRoundRect(actualBounds, 12f, 12f, paint);
|
||||
}
|
||||
|
||||
// Draw checkmark for selected items in multiple selection mode
|
||||
if (isSelected && SelectionMode == SkiaSelectionMode.Multiple)
|
||||
{
|
||||
DrawCheckmark(canvas, new SKRect(actualBounds.Right - 32, actualBounds.MidY - 8, actualBounds.Right - 16, actualBounds.MidY + 8));
|
||||
DrawCheckmark(canvas, new SKRect(actualBounds.Right - 32f, actualBounds.MidY - 8f, actualBounds.Right - 16f, actualBounds.MidY + 8f));
|
||||
}
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[SkiaCollectionView.DrawItem] EXCEPTION: {ex.Message}\n{ex.StackTrace}");
|
||||
Console.WriteLine("[SkiaCollectionView.DrawItem] EXCEPTION: " + ex.Message + "\n" + ex.StackTrace);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Use custom renderer if provided
|
||||
if (ItemRenderer != null)
|
||||
if (ItemRenderer != null && ItemRenderer(item, index, bounds, canvas, paint))
|
||||
{
|
||||
if (ItemRenderer(item, index, bounds, canvas, paint))
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
// Default rendering - fall back to ToString
|
||||
paint.Color = SKColors.Black;
|
||||
paint.Style = SKPaintStyle.Fill;
|
||||
|
||||
using var font = new SKFont(SKTypeface.Default, 14);
|
||||
using var font = new SKFont(SKTypeface.Default, 14f, 1f, 0f);
|
||||
using var textPaint = new SKPaint(font)
|
||||
{
|
||||
Color = SKColors.Black,
|
||||
@@ -498,14 +466,13 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
var textBounds = new SKRect();
|
||||
textPaint.MeasureText(text, ref textBounds);
|
||||
|
||||
var x = bounds.Left + 16;
|
||||
var x = bounds.Left + 16f;
|
||||
var y = bounds.MidY - textBounds.MidY;
|
||||
canvas.DrawText(text, x, y, textPaint);
|
||||
|
||||
// Draw checkmark for selected items in multiple selection mode
|
||||
if (isSelected && SelectionMode == SkiaSelectionMode.Multiple)
|
||||
{
|
||||
DrawCheckmark(canvas, new SKRect(bounds.Right - 32, bounds.MidY - 8, bounds.Right - 16, bounds.MidY + 8));
|
||||
DrawCheckmark(canvas, new SKRect(bounds.Right - 32f, bounds.MidY - 8f, bounds.Right - 16f, bounds.MidY + 8f));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -513,27 +480,25 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
{
|
||||
using var paint = new SKPaint
|
||||
{
|
||||
Color = new SKColor(0x21, 0x96, 0xF3),
|
||||
Color = new SKColor(33, 150, 243),
|
||||
Style = SKPaintStyle.Stroke,
|
||||
StrokeWidth = 2,
|
||||
StrokeWidth = 2f,
|
||||
IsAntialias = true,
|
||||
StrokeCap = SKStrokeCap.Round
|
||||
};
|
||||
|
||||
using var path = new SKPath();
|
||||
path.MoveTo(bounds.Left, bounds.MidY);
|
||||
path.LineTo(bounds.MidX - 2, bounds.Bottom - 2);
|
||||
path.LineTo(bounds.Right, bounds.Top + 2);
|
||||
path.LineTo(bounds.MidX - 2f, bounds.Bottom - 2f);
|
||||
path.LineTo(bounds.Right, bounds.Top + 2f);
|
||||
|
||||
canvas.DrawPath(path, paint);
|
||||
}
|
||||
|
||||
protected override void OnDraw(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
// Reset the heights-changed flag at the start of each draw
|
||||
_heightsChangedDuringDraw = false;
|
||||
|
||||
// Draw background if set
|
||||
if (BackgroundColor != SKColors.Transparent)
|
||||
{
|
||||
using var bgPaint = new SKPaint
|
||||
@@ -544,35 +509,26 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
canvas.DrawRect(bounds, bgPaint);
|
||||
}
|
||||
|
||||
// Draw header if present
|
||||
if (Header != null && HeaderHeight > 0)
|
||||
if (Header != null && HeaderHeight > 0f)
|
||||
{
|
||||
var headerRect = new SKRect(bounds.Left, bounds.Top, bounds.Right, bounds.Top + HeaderHeight);
|
||||
DrawHeader(canvas, headerRect);
|
||||
}
|
||||
|
||||
// Draw footer if present
|
||||
if (Footer != null && FooterHeight > 0)
|
||||
if (Footer != null && FooterHeight > 0f)
|
||||
{
|
||||
var footerRect = new SKRect(bounds.Left, bounds.Bottom - FooterHeight, bounds.Right, bounds.Bottom);
|
||||
DrawFooter(canvas, footerRect);
|
||||
}
|
||||
|
||||
// Adjust content bounds for header/footer
|
||||
var contentBounds = new SKRect(
|
||||
bounds.Left,
|
||||
bounds.Top + HeaderHeight,
|
||||
bounds.Right,
|
||||
bounds.Bottom - FooterHeight);
|
||||
var contentBounds = new SKRect(bounds.Left, bounds.Top + HeaderHeight, bounds.Right, bounds.Bottom - FooterHeight);
|
||||
|
||||
// Draw items
|
||||
if (ItemCount == 0)
|
||||
{
|
||||
DrawEmptyView(canvas, contentBounds);
|
||||
return;
|
||||
}
|
||||
|
||||
// Use grid layout if spanCount > 1
|
||||
if (SpanCount > 1)
|
||||
{
|
||||
DrawGridItems(canvas, contentBounds);
|
||||
@@ -582,8 +538,6 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
DrawListItems(canvas, contentBounds);
|
||||
}
|
||||
|
||||
// If heights changed during this draw, schedule a redraw with correct positions
|
||||
// This will queue another frame to be drawn with the correct cached heights
|
||||
if (_heightsChangedDuringDraw)
|
||||
{
|
||||
_heightsChangedDuringDraw = false;
|
||||
@@ -593,17 +547,18 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
|
||||
private void DrawListItems(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
// Standard list drawing with variable item heights
|
||||
canvas.Save();
|
||||
canvas.ClipRect(bounds);
|
||||
canvas.ClipRect(bounds, SKClipOperation.Intersect, false);
|
||||
|
||||
using var paint = new SKPaint { IsAntialias = true };
|
||||
using var paint = new SKPaint
|
||||
{
|
||||
IsAntialias = true
|
||||
};
|
||||
|
||||
var scrollOffset = GetScrollOffset();
|
||||
|
||||
// Find first visible item by walking through items
|
||||
int firstVisible = 0;
|
||||
float cumulativeOffset = 0;
|
||||
float cumulativeOffset = 0f;
|
||||
for (int i = 0; i < ItemCount; i++)
|
||||
{
|
||||
var itemH = GetItemHeight(i);
|
||||
@@ -615,16 +570,16 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
cumulativeOffset += itemH + ItemSpacing;
|
||||
}
|
||||
|
||||
// Draw visible items using variable heights
|
||||
float currentY = bounds.Top + GetItemOffset(firstVisible) - scrollOffset;
|
||||
for (int i = firstVisible; i < ItemCount; i++)
|
||||
{
|
||||
var itemH = GetItemHeight(i);
|
||||
var itemRect = new SKRect(bounds.Left, currentY, bounds.Right - 8, currentY + itemH);
|
||||
var itemRect = new SKRect(bounds.Left, currentY, bounds.Right - 8f, currentY + itemH);
|
||||
|
||||
// Stop if we've passed the visible area
|
||||
if (itemRect.Top > bounds.Bottom)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (itemRect.Bottom >= bounds.Top)
|
||||
{
|
||||
@@ -640,7 +595,6 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
|
||||
canvas.Restore();
|
||||
|
||||
// Draw scrollbar
|
||||
var totalHeight = TotalContentHeight;
|
||||
if (totalHeight > bounds.Height)
|
||||
{
|
||||
@@ -651,46 +605,51 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
private void DrawGridItems(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
canvas.Save();
|
||||
canvas.ClipRect(bounds);
|
||||
canvas.ClipRect(bounds, SKClipOperation.Intersect, false);
|
||||
|
||||
using var paint = new SKPaint { IsAntialias = true };
|
||||
using var paint = new SKPaint
|
||||
{
|
||||
IsAntialias = true
|
||||
};
|
||||
|
||||
var cellWidth = (bounds.Width - 8) / SpanCount; // -8 for scrollbar
|
||||
var cellWidth = (bounds.Width - 8f) / SpanCount;
|
||||
var cellHeight = ItemHeight;
|
||||
var rowCount = (int)Math.Ceiling((double)ItemCount / SpanCount);
|
||||
var totalHeight = rowCount * (cellHeight + ItemSpacing) - ItemSpacing;
|
||||
|
||||
var scrollOffset = GetScrollOffset();
|
||||
var firstVisibleRow = Math.Max(0, (int)(scrollOffset / (cellHeight + ItemSpacing)));
|
||||
var lastVisibleRow = Math.Min(rowCount - 1,
|
||||
(int)((scrollOffset + bounds.Height) / (cellHeight + ItemSpacing)) + 1);
|
||||
var lastVisibleRow = Math.Min(rowCount - 1, (int)((scrollOffset + bounds.Height) / (cellHeight + ItemSpacing)) + 1);
|
||||
|
||||
for (int row = firstVisibleRow; row <= lastVisibleRow; row++)
|
||||
{
|
||||
var rowY = bounds.Top + (row * (cellHeight + ItemSpacing)) - scrollOffset;
|
||||
var rowY = bounds.Top + row * (cellHeight + ItemSpacing) - scrollOffset;
|
||||
|
||||
for (int col = 0; col < SpanCount; col++)
|
||||
{
|
||||
var index = row * SpanCount + col;
|
||||
if (index >= ItemCount) break;
|
||||
if (index >= ItemCount)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var cellX = bounds.Left + col * cellWidth;
|
||||
var cellRect = new SKRect(cellX + 2, rowY, cellX + cellWidth - 2, rowY + cellHeight);
|
||||
var cellRect = new SKRect(cellX + 2f, rowY, cellX + cellWidth - 2f, rowY + cellHeight);
|
||||
|
||||
if (cellRect.Bottom < bounds.Top || cellRect.Top > bounds.Bottom)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var item = GetItemAt(index);
|
||||
if (item != null)
|
||||
{
|
||||
// Draw cell background
|
||||
using var cellBgPaint = new SKPaint
|
||||
{
|
||||
Color = _selectedItems.Contains(item) ? SelectionColor : new SKColor(0xFA, 0xFA, 0xFA),
|
||||
Color = _selectedItems.Contains(item) ? SelectionColor : new SKColor(250, 250, 250),
|
||||
Style = SKPaintStyle.Fill
|
||||
};
|
||||
canvas.DrawRoundRect(new SKRoundRect(cellRect, 4), cellBgPaint);
|
||||
|
||||
canvas.DrawRoundRect(new SKRoundRect(cellRect, 4f), cellBgPaint);
|
||||
DrawItem(canvas, item, index, cellRect, paint);
|
||||
}
|
||||
}
|
||||
@@ -698,7 +657,6 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
|
||||
canvas.Restore();
|
||||
|
||||
// Draw scrollbar
|
||||
if (totalHeight > bounds.Height)
|
||||
{
|
||||
DrawScrollBarInternal(canvas, bounds, scrollOffset, totalHeight);
|
||||
@@ -710,7 +668,6 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
var scrollBarWidth = 6f;
|
||||
var scrollBarMargin = 2f;
|
||||
|
||||
// Draw scrollbar track (subtle)
|
||||
var trackRect = new SKRect(
|
||||
bounds.Right - scrollBarWidth - scrollBarMargin,
|
||||
bounds.Top + scrollBarMargin,
|
||||
@@ -722,23 +679,17 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
Color = new SKColor(0, 0, 0, 20),
|
||||
Style = SKPaintStyle.Fill
|
||||
};
|
||||
canvas.DrawRoundRect(new SKRoundRect(trackRect, 3), trackPaint);
|
||||
canvas.DrawRoundRect(new SKRoundRect(trackRect, 3f), trackPaint);
|
||||
|
||||
// Calculate thumb position and size
|
||||
var maxOffset = Math.Max(0, totalHeight - bounds.Height);
|
||||
var maxOffset = Math.Max(0f, totalHeight - bounds.Height);
|
||||
var viewportRatio = bounds.Height / totalHeight;
|
||||
var availableTrackHeight = trackRect.Height;
|
||||
var thumbHeight = Math.Max(30, availableTrackHeight * viewportRatio);
|
||||
var scrollRatio = maxOffset > 0 ? scrollOffset / maxOffset : 0;
|
||||
var thumbHeight = Math.Max(30f, availableTrackHeight * viewportRatio);
|
||||
var scrollRatio = maxOffset > 0f ? scrollOffset / maxOffset : 0f;
|
||||
var thumbY = trackRect.Top + (availableTrackHeight - thumbHeight) * scrollRatio;
|
||||
|
||||
var thumbRect = new SKRect(
|
||||
trackRect.Left,
|
||||
thumbY,
|
||||
trackRect.Right,
|
||||
thumbY + thumbHeight);
|
||||
var thumbRect = new SKRect(trackRect.Left, thumbY, trackRect.Right, thumbY + thumbHeight);
|
||||
|
||||
// Draw thumb with more visible color
|
||||
using var thumbPaint = new SKPaint
|
||||
{
|
||||
Color = new SKColor(100, 100, 100, 180),
|
||||
@@ -746,13 +697,11 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
IsAntialias = true
|
||||
};
|
||||
|
||||
canvas.DrawRoundRect(new SKRoundRect(thumbRect, 3), thumbPaint);
|
||||
canvas.DrawRoundRect(new SKRoundRect(thumbRect, 3f), thumbPaint);
|
||||
}
|
||||
|
||||
private float GetScrollOffset()
|
||||
{
|
||||
// Access base class scroll offset through reflection or expose it
|
||||
// For now, use the field directly through internal access
|
||||
return _scrollOffset;
|
||||
}
|
||||
|
||||
@@ -765,11 +714,10 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
};
|
||||
canvas.DrawRect(bounds, bgPaint);
|
||||
|
||||
// Draw header text
|
||||
var text = Header.ToString() ?? "";
|
||||
var text = Header?.ToString() ?? "";
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
using var font = new SKFont(SKTypeface.Default, 16);
|
||||
using var font = new SKFont(SKTypeface.Default, 16f, 1f, 0f);
|
||||
using var textPaint = new SKPaint(font)
|
||||
{
|
||||
Color = SKColors.Black,
|
||||
@@ -779,17 +727,16 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
var textBounds = new SKRect();
|
||||
textPaint.MeasureText(text, ref textBounds);
|
||||
|
||||
var x = bounds.Left + 16;
|
||||
var x = bounds.Left + 16f;
|
||||
var y = bounds.MidY - textBounds.MidY;
|
||||
canvas.DrawText(text, x, y, textPaint);
|
||||
}
|
||||
|
||||
// Draw separator
|
||||
using var sepPaint = new SKPaint
|
||||
{
|
||||
Color = new SKColor(0xE0, 0xE0, 0xE0),
|
||||
Color = new SKColor(224, 224, 224),
|
||||
Style = SKPaintStyle.Stroke,
|
||||
StrokeWidth = 1
|
||||
StrokeWidth = 1f
|
||||
};
|
||||
canvas.DrawLine(bounds.Left, bounds.Bottom, bounds.Right, bounds.Bottom, sepPaint);
|
||||
}
|
||||
@@ -803,23 +750,21 @@ public class SkiaCollectionView : SkiaItemsView
|
||||
};
|
||||
canvas.DrawRect(bounds, bgPaint);
|
||||
|
||||
// Draw separator
|
||||
using var sepPaint = new SKPaint
|
||||
{
|
||||
Color = new SKColor(0xE0, 0xE0, 0xE0),
|
||||
Color = new SKColor(224, 224, 224),
|
||||
Style = SKPaintStyle.Stroke,
|
||||
StrokeWidth = 1
|
||||
StrokeWidth = 1f
|
||||
};
|
||||
canvas.DrawLine(bounds.Left, bounds.Top, bounds.Right, bounds.Top, sepPaint);
|
||||
|
||||
// Draw footer text
|
||||
var text = Footer.ToString() ?? "";
|
||||
var text = Footer?.ToString() ?? "";
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
using var font = new SKFont(SKTypeface.Default, 14);
|
||||
using var font = new SKFont(SKTypeface.Default, 14f, 1f, 0f);
|
||||
using var textPaint = new SKPaint(font)
|
||||
{
|
||||
Color = new SKColor(0x80, 0x80, 0x80),
|
||||
Color = new SKColor(128, 128, 128),
|
||||
IsAntialias = true
|
||||
};
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using SkiaSharp;
|
||||
using System;
|
||||
using Microsoft.Maui.Controls;
|
||||
using Microsoft.Maui.Platform.Linux;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
|
||||
@@ -14,59 +16,70 @@ public class SkiaDatePicker : SkiaView
|
||||
#region BindableProperties
|
||||
|
||||
public static readonly BindableProperty DateProperty =
|
||||
BindableProperty.Create(nameof(Date), typeof(DateTime), typeof(SkiaDatePicker), DateTime.Today, BindingMode.TwoWay,
|
||||
BindableProperty.Create(nameof(Date), typeof(DateTime), typeof(SkiaDatePicker), DateTime.Today, BindingMode.OneWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).OnDatePropertyChanged());
|
||||
|
||||
public static readonly BindableProperty MinimumDateProperty =
|
||||
BindableProperty.Create(nameof(MinimumDate), typeof(DateTime), typeof(SkiaDatePicker), new DateTime(1900, 1, 1),
|
||||
BindableProperty.Create(nameof(MinimumDate), typeof(DateTime), typeof(SkiaDatePicker), new DateTime(1900, 1, 1), BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty MaximumDateProperty =
|
||||
BindableProperty.Create(nameof(MaximumDate), typeof(DateTime), typeof(SkiaDatePicker), new DateTime(2100, 12, 31),
|
||||
BindableProperty.Create(nameof(MaximumDate), typeof(DateTime), typeof(SkiaDatePicker), new DateTime(2100, 12, 31), BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty FormatProperty =
|
||||
BindableProperty.Create(nameof(Format), typeof(string), typeof(SkiaDatePicker), "d",
|
||||
BindableProperty.Create(nameof(Format), typeof(string), typeof(SkiaDatePicker), "d", BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty TextColorProperty =
|
||||
BindableProperty.Create(nameof(TextColor), typeof(SKColor), typeof(SkiaDatePicker), SKColors.Black,
|
||||
BindableProperty.Create(nameof(TextColor), typeof(SKColor), typeof(SkiaDatePicker), SKColors.Black, BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty BorderColorProperty =
|
||||
BindableProperty.Create(nameof(BorderColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(0xBD, 0xBD, 0xBD),
|
||||
BindableProperty.Create(nameof(BorderColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(189, 189, 189), BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty CalendarBackgroundColorProperty =
|
||||
BindableProperty.Create(nameof(CalendarBackgroundColor), typeof(SKColor), typeof(SkiaDatePicker), SKColors.White,
|
||||
BindableProperty.Create(nameof(CalendarBackgroundColor), typeof(SKColor), typeof(SkiaDatePicker), SKColors.White, BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty SelectedDayColorProperty =
|
||||
BindableProperty.Create(nameof(SelectedDayColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(0x21, 0x96, 0xF3),
|
||||
BindableProperty.Create(nameof(SelectedDayColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(33, 150, 243), BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty TodayColorProperty =
|
||||
BindableProperty.Create(nameof(TodayColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(0x21, 0x96, 0xF3, 0x40),
|
||||
BindableProperty.Create(nameof(TodayColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(33, 150, 243, 64), BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty HeaderColorProperty =
|
||||
BindableProperty.Create(nameof(HeaderColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(0x21, 0x96, 0xF3),
|
||||
BindableProperty.Create(nameof(HeaderColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(33, 150, 243), BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty DisabledDayColorProperty =
|
||||
BindableProperty.Create(nameof(DisabledDayColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(0xBD, 0xBD, 0xBD),
|
||||
BindableProperty.Create(nameof(DisabledDayColor), typeof(SKColor), typeof(SkiaDatePicker), new SKColor(189, 189, 189), BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
public static readonly BindableProperty FontSizeProperty =
|
||||
BindableProperty.Create(nameof(FontSize), typeof(float), typeof(SkiaDatePicker), 14f,
|
||||
BindableProperty.Create(nameof(FontSize), typeof(float), typeof(SkiaDatePicker), 14f, BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).InvalidateMeasure());
|
||||
|
||||
public static readonly BindableProperty CornerRadiusProperty =
|
||||
BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(SkiaDatePicker), 4f,
|
||||
BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(SkiaDatePicker), 4f, BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaDatePicker)b).Invalidate());
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
private DateTime _displayMonth;
|
||||
private bool _isOpen;
|
||||
|
||||
private const float CalendarWidth = 280f;
|
||||
private const float CalendarHeight = 320f;
|
||||
private const float HeaderHeight = 48f;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public DateTime Date
|
||||
@@ -156,9 +169,9 @@ public class SkiaDatePicker : SkiaView
|
||||
{
|
||||
_isOpen = value;
|
||||
if (_isOpen)
|
||||
RegisterPopupOverlay(this, DrawCalendarOverlay);
|
||||
SkiaView.RegisterPopupOverlay(this, DrawCalendarOverlay);
|
||||
else
|
||||
UnregisterPopupOverlay(this);
|
||||
SkiaView.UnregisterPopupOverlay(this);
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
@@ -166,44 +179,13 @@ public class SkiaDatePicker : SkiaView
|
||||
|
||||
#endregion
|
||||
|
||||
private DateTime _displayMonth;
|
||||
private bool _isOpen;
|
||||
|
||||
private const float CalendarWidth = 280;
|
||||
private const float CalendarHeight = 320;
|
||||
private const float HeaderHeight = 48;
|
||||
#region Events
|
||||
|
||||
public event EventHandler? DateSelected;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the calendar popup rectangle with edge detection applied.
|
||||
/// </summary>
|
||||
private SKRect GetCalendarRect(SKRect pickerBounds)
|
||||
{
|
||||
// Get window dimensions for edge detection
|
||||
var windowWidth = LinuxApplication.Current?.MainWindow?.Width ?? 800;
|
||||
var windowHeight = LinuxApplication.Current?.MainWindow?.Height ?? 600;
|
||||
#endregion
|
||||
|
||||
// Calculate default position (below the picker)
|
||||
var calendarLeft = pickerBounds.Left;
|
||||
var calendarTop = pickerBounds.Bottom + 4;
|
||||
|
||||
// Edge detection: adjust horizontal position if popup would go off-screen
|
||||
if (calendarLeft + CalendarWidth > windowWidth)
|
||||
{
|
||||
calendarLeft = windowWidth - CalendarWidth - 4;
|
||||
}
|
||||
if (calendarLeft < 0) calendarLeft = 4;
|
||||
|
||||
// Edge detection: show above if popup would go off-screen vertically
|
||||
if (calendarTop + CalendarHeight > windowHeight)
|
||||
{
|
||||
calendarTop = pickerBounds.Top - CalendarHeight - 4;
|
||||
}
|
||||
if (calendarTop < 0) calendarTop = 4;
|
||||
|
||||
return new SKRect(calendarLeft, calendarTop, calendarLeft + CalendarWidth, calendarTop + CalendarHeight);
|
||||
}
|
||||
#region Constructor
|
||||
|
||||
public SkiaDatePicker()
|
||||
{
|
||||
@@ -211,6 +193,38 @@ public class SkiaDatePicker : SkiaView
|
||||
_displayMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private SKRect GetCalendarRect(SKRect pickerBounds)
|
||||
{
|
||||
int windowWidth = LinuxApplication.Current?.MainWindow?.Width ?? 800;
|
||||
int windowHeight = LinuxApplication.Current?.MainWindow?.Height ?? 600;
|
||||
|
||||
float calendarLeft = pickerBounds.Left;
|
||||
float calendarTop = pickerBounds.Bottom + 4f;
|
||||
|
||||
if (calendarLeft + CalendarWidth > windowWidth)
|
||||
{
|
||||
calendarLeft = windowWidth - CalendarWidth - 4f;
|
||||
}
|
||||
if (calendarLeft < 0f)
|
||||
{
|
||||
calendarLeft = 4f;
|
||||
}
|
||||
if (calendarTop + CalendarHeight > windowHeight)
|
||||
{
|
||||
calendarTop = pickerBounds.Top - CalendarHeight - 4f;
|
||||
}
|
||||
if (calendarTop < 0f)
|
||||
{
|
||||
calendarTop = 4f;
|
||||
}
|
||||
|
||||
return new SKRect(calendarLeft, calendarTop, calendarLeft + CalendarWidth, calendarTop + CalendarHeight);
|
||||
}
|
||||
|
||||
private void OnDatePropertyChanged()
|
||||
{
|
||||
_displayMonth = new DateTime(Date.Year, Date.Month, 1);
|
||||
@@ -220,28 +234,26 @@ public class SkiaDatePicker : SkiaView
|
||||
|
||||
private DateTime ClampDate(DateTime date)
|
||||
{
|
||||
if (date < MinimumDate) return MinimumDate;
|
||||
if (date > MaximumDate) return MaximumDate;
|
||||
if (date < MinimumDate)
|
||||
return MinimumDate;
|
||||
if (date > MaximumDate)
|
||||
return MaximumDate;
|
||||
return date;
|
||||
}
|
||||
|
||||
private void DrawCalendarOverlay(SKCanvas canvas)
|
||||
{
|
||||
if (!_isOpen) return;
|
||||
// Use ScreenBounds for popup drawing (accounts for scroll offset)
|
||||
DrawCalendar(canvas, ScreenBounds);
|
||||
}
|
||||
|
||||
protected override void OnDraw(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
DrawPickerButton(canvas, bounds);
|
||||
if (_isOpen)
|
||||
{
|
||||
DrawCalendar(canvas, ScreenBounds);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawPickerButton(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
using var bgPaint = new SKPaint
|
||||
{
|
||||
Color = IsEnabled ? BackgroundColor : new SKColor(0xF5, 0xF5, 0xF5),
|
||||
Color = IsEnabled ? BackgroundColor : new SKColor(245, 245, 245),
|
||||
Style = SKPaintStyle.Fill,
|
||||
IsAntialias = true
|
||||
};
|
||||
@@ -256,19 +268,19 @@ public class SkiaDatePicker : SkiaView
|
||||
};
|
||||
canvas.DrawRoundRect(new SKRoundRect(bounds, CornerRadius), borderPaint);
|
||||
|
||||
using var font = new SKFont(SKTypeface.Default, FontSize);
|
||||
using var font = new SKFont(SKTypeface.Default, FontSize, 1f, 0f);
|
||||
using var textPaint = new SKPaint(font)
|
||||
{
|
||||
Color = IsEnabled ? TextColor : TextColor.WithAlpha(128),
|
||||
IsAntialias = true
|
||||
};
|
||||
|
||||
var dateText = Date.ToString(Format);
|
||||
var textBounds = new SKRect();
|
||||
string dateText = Date.ToString(Format);
|
||||
SKRect textBounds = default;
|
||||
textPaint.MeasureText(dateText, ref textBounds);
|
||||
canvas.DrawText(dateText, bounds.Left + 12, bounds.MidY - textBounds.MidY, textPaint);
|
||||
canvas.DrawText(dateText, bounds.Left + 12f, bounds.MidY - textBounds.MidY, textPaint);
|
||||
|
||||
DrawCalendarIcon(canvas, new SKRect(bounds.Right - 36, bounds.MidY - 10, bounds.Right - 12, bounds.MidY + 10));
|
||||
DrawCalendarIcon(canvas, new SKRect(bounds.Right - 36f, bounds.MidY - 10f, bounds.Right - 12f, bounds.MidY + 10f));
|
||||
}
|
||||
|
||||
private void DrawCalendarIcon(SKCanvas canvas, SKRect bounds)
|
||||
@@ -281,162 +293,230 @@ public class SkiaDatePicker : SkiaView
|
||||
IsAntialias = true
|
||||
};
|
||||
|
||||
var calRect = new SKRect(bounds.Left, bounds.Top + 3, bounds.Right, bounds.Bottom);
|
||||
canvas.DrawRoundRect(new SKRoundRect(calRect, 2), paint);
|
||||
canvas.DrawLine(bounds.Left + 5, bounds.Top, bounds.Left + 5, bounds.Top + 5, paint);
|
||||
canvas.DrawLine(bounds.Right - 5, bounds.Top, bounds.Right - 5, bounds.Top + 5, paint);
|
||||
canvas.DrawLine(bounds.Left, bounds.Top + 8, bounds.Right, bounds.Top + 8, paint);
|
||||
SKRect calRect = new SKRect(bounds.Left, bounds.Top + 3f, bounds.Right, bounds.Bottom);
|
||||
canvas.DrawRoundRect(new SKRoundRect(calRect, 2f), paint);
|
||||
canvas.DrawLine(bounds.Left + 5f, bounds.Top, bounds.Left + 5f, bounds.Top + 5f, paint);
|
||||
canvas.DrawLine(bounds.Right - 5f, bounds.Top, bounds.Right - 5f, bounds.Top + 5f, paint);
|
||||
canvas.DrawLine(bounds.Left, bounds.Top + 8f, bounds.Right, bounds.Top + 8f, paint);
|
||||
|
||||
paint.Style = SKPaintStyle.Fill;
|
||||
for (int row = 0; row < 2; row++)
|
||||
for (int col = 0; col < 3; col++)
|
||||
canvas.DrawCircle(bounds.Left + 4 + col * 6, bounds.Top + 12 + row * 4, 1, paint);
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
canvas.DrawCircle(bounds.Left + 4f + j * 6, bounds.Top + 12f + i * 4, 1f, paint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawCalendar(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
var calendarRect = GetCalendarRect(bounds);
|
||||
SKRect calendarRect = GetCalendarRect(bounds);
|
||||
|
||||
using var shadowPaint = new SKPaint
|
||||
{
|
||||
Color = new SKColor(0, 0, 0, 40),
|
||||
MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 4),
|
||||
MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 4f),
|
||||
Style = SKPaintStyle.Fill
|
||||
};
|
||||
canvas.DrawRoundRect(new SKRoundRect(new SKRect(calendarRect.Left + 2, calendarRect.Top + 2, calendarRect.Right + 2, calendarRect.Bottom + 2), CornerRadius), shadowPaint);
|
||||
canvas.DrawRoundRect(new SKRoundRect(new SKRect(calendarRect.Left + 2f, calendarRect.Top + 2f, calendarRect.Right + 2f, calendarRect.Bottom + 2f), CornerRadius), shadowPaint);
|
||||
|
||||
using var bgPaint = new SKPaint { Color = CalendarBackgroundColor, Style = SKPaintStyle.Fill, IsAntialias = true };
|
||||
using var bgPaint = new SKPaint
|
||||
{
|
||||
Color = CalendarBackgroundColor,
|
||||
Style = SKPaintStyle.Fill,
|
||||
IsAntialias = true
|
||||
};
|
||||
canvas.DrawRoundRect(new SKRoundRect(calendarRect, CornerRadius), bgPaint);
|
||||
|
||||
using var borderPaint = new SKPaint { Color = BorderColor, Style = SKPaintStyle.Stroke, StrokeWidth = 1, IsAntialias = true };
|
||||
using var borderPaint = new SKPaint
|
||||
{
|
||||
Color = BorderColor,
|
||||
Style = SKPaintStyle.Stroke,
|
||||
StrokeWidth = 1f,
|
||||
IsAntialias = true
|
||||
};
|
||||
canvas.DrawRoundRect(new SKRoundRect(calendarRect, CornerRadius), borderPaint);
|
||||
|
||||
DrawCalendarHeader(canvas, new SKRect(calendarRect.Left, calendarRect.Top, calendarRect.Right, calendarRect.Top + HeaderHeight));
|
||||
DrawWeekdayHeaders(canvas, new SKRect(calendarRect.Left, calendarRect.Top + HeaderHeight, calendarRect.Right, calendarRect.Top + HeaderHeight + 30));
|
||||
DrawDays(canvas, new SKRect(calendarRect.Left, calendarRect.Top + HeaderHeight + 30, calendarRect.Right, calendarRect.Bottom));
|
||||
DrawCalendarHeader(canvas, new SKRect(calendarRect.Left, calendarRect.Top, calendarRect.Right, calendarRect.Top + 48f));
|
||||
DrawWeekdayHeaders(canvas, new SKRect(calendarRect.Left, calendarRect.Top + 48f, calendarRect.Right, calendarRect.Top + 48f + 30f));
|
||||
DrawDays(canvas, new SKRect(calendarRect.Left, calendarRect.Top + 48f + 30f, calendarRect.Right, calendarRect.Bottom));
|
||||
}
|
||||
|
||||
private void DrawCalendarHeader(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
using var headerPaint = new SKPaint { Color = HeaderColor, Style = SKPaintStyle.Fill };
|
||||
using var headerPaint = new SKPaint
|
||||
{
|
||||
Color = HeaderColor,
|
||||
Style = SKPaintStyle.Fill
|
||||
};
|
||||
|
||||
canvas.Save();
|
||||
canvas.ClipRoundRect(new SKRoundRect(new SKRect(bounds.Left, bounds.Top, bounds.Right, bounds.Top + CornerRadius * 2), CornerRadius));
|
||||
canvas.ClipRoundRect(new SKRoundRect(new SKRect(bounds.Left, bounds.Top, bounds.Right, bounds.Top + CornerRadius * 2f), CornerRadius), SKClipOperation.Intersect, false);
|
||||
canvas.DrawRect(bounds, headerPaint);
|
||||
canvas.Restore();
|
||||
canvas.DrawRect(new SKRect(bounds.Left, bounds.Top + CornerRadius, bounds.Right, bounds.Bottom), headerPaint);
|
||||
|
||||
using var font = new SKFont(SKTypeface.Default, 16);
|
||||
using var textPaint = new SKPaint(font) { Color = SKColors.White, IsAntialias = true };
|
||||
var monthYear = _displayMonth.ToString("MMMM yyyy");
|
||||
var textBounds = new SKRect();
|
||||
using var font = new SKFont(SKTypeface.Default, 16f, 1f, 0f);
|
||||
using var textPaint = new SKPaint(font)
|
||||
{
|
||||
Color = SKColors.White,
|
||||
IsAntialias = true
|
||||
};
|
||||
|
||||
string monthYear = _displayMonth.ToString("MMMM yyyy");
|
||||
SKRect textBounds = default;
|
||||
textPaint.MeasureText(monthYear, ref textBounds);
|
||||
canvas.DrawText(monthYear, bounds.MidX - textBounds.MidX, bounds.MidY - textBounds.MidY, textPaint);
|
||||
|
||||
using var arrowPaint = new SKPaint { Color = SKColors.White, Style = SKPaintStyle.Stroke, StrokeWidth = 2, IsAntialias = true, StrokeCap = SKStrokeCap.Round };
|
||||
using var arrowPaint = new SKPaint
|
||||
{
|
||||
Color = SKColors.White,
|
||||
Style = SKPaintStyle.Stroke,
|
||||
StrokeWidth = 2f,
|
||||
IsAntialias = true,
|
||||
StrokeCap = SKStrokeCap.Round
|
||||
};
|
||||
|
||||
using var leftPath = new SKPath();
|
||||
leftPath.MoveTo(bounds.Left + 26, bounds.MidY - 6);
|
||||
leftPath.LineTo(bounds.Left + 20, bounds.MidY);
|
||||
leftPath.LineTo(bounds.Left + 26, bounds.MidY + 6);
|
||||
leftPath.MoveTo(bounds.Left + 26f, bounds.MidY - 6f);
|
||||
leftPath.LineTo(bounds.Left + 20f, bounds.MidY);
|
||||
leftPath.LineTo(bounds.Left + 26f, bounds.MidY + 6f);
|
||||
canvas.DrawPath(leftPath, arrowPaint);
|
||||
|
||||
using var rightPath = new SKPath();
|
||||
rightPath.MoveTo(bounds.Right - 26, bounds.MidY - 6);
|
||||
rightPath.LineTo(bounds.Right - 20, bounds.MidY);
|
||||
rightPath.LineTo(bounds.Right - 26, bounds.MidY + 6);
|
||||
rightPath.MoveTo(bounds.Right - 26f, bounds.MidY - 6f);
|
||||
rightPath.LineTo(bounds.Right - 20f, bounds.MidY);
|
||||
rightPath.LineTo(bounds.Right - 26f, bounds.MidY + 6f);
|
||||
canvas.DrawPath(rightPath, arrowPaint);
|
||||
}
|
||||
|
||||
private void DrawWeekdayHeaders(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
var dayNames = new[] { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
|
||||
var cellWidth = bounds.Width / 7;
|
||||
using var font = new SKFont(SKTypeface.Default, 12);
|
||||
using var paint = new SKPaint(font) { Color = new SKColor(0x80, 0x80, 0x80), IsAntialias = true };
|
||||
string[] dayNames = new string[] { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
|
||||
float cellWidth = bounds.Width / 7f;
|
||||
|
||||
using var font = new SKFont(SKTypeface.Default, 12f, 1f, 0f);
|
||||
using var paint = new SKPaint(font)
|
||||
{
|
||||
Color = new SKColor(128, 128, 128),
|
||||
IsAntialias = true
|
||||
};
|
||||
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
var textBounds = new SKRect();
|
||||
SKRect textBounds = default;
|
||||
paint.MeasureText(dayNames[i], ref textBounds);
|
||||
canvas.DrawText(dayNames[i], bounds.Left + i * cellWidth + cellWidth / 2 - textBounds.MidX, bounds.MidY - textBounds.MidY, paint);
|
||||
canvas.DrawText(dayNames[i], bounds.Left + i * cellWidth + cellWidth / 2f - textBounds.MidX, bounds.MidY - textBounds.MidY, paint);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawDays(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
var firstDay = new DateTime(_displayMonth.Year, _displayMonth.Month, 1);
|
||||
var daysInMonth = DateTime.DaysInMonth(_displayMonth.Year, _displayMonth.Month);
|
||||
var startDayOfWeek = (int)firstDay.DayOfWeek;
|
||||
var cellWidth = bounds.Width / 7;
|
||||
var cellHeight = (bounds.Height - 10) / 6;
|
||||
using var font = new SKFont(SKTypeface.Default, 14);
|
||||
using var textPaint = new SKPaint(font) { IsAntialias = true };
|
||||
using var bgPaint = new SKPaint { Style = SKPaintStyle.Fill, IsAntialias = true };
|
||||
var today = DateTime.Today;
|
||||
DateTime firstDay = new DateTime(_displayMonth.Year, _displayMonth.Month, 1);
|
||||
int daysInMonth = DateTime.DaysInMonth(_displayMonth.Year, _displayMonth.Month);
|
||||
int startDayOfWeek = (int)firstDay.DayOfWeek;
|
||||
float cellWidth = bounds.Width / 7f;
|
||||
float cellHeight = (bounds.Height - 10f) / 6f;
|
||||
|
||||
using var font = new SKFont(SKTypeface.Default, 14f, 1f, 0f);
|
||||
using var textPaint = new SKPaint(font)
|
||||
{
|
||||
IsAntialias = true
|
||||
};
|
||||
using var bgPaint = new SKPaint
|
||||
{
|
||||
Style = SKPaintStyle.Fill,
|
||||
IsAntialias = true
|
||||
};
|
||||
|
||||
DateTime today = DateTime.Today;
|
||||
SKRect cellRect = default;
|
||||
|
||||
for (int day = 1; day <= daysInMonth; day++)
|
||||
{
|
||||
var dayDate = new DateTime(_displayMonth.Year, _displayMonth.Month, day);
|
||||
var cellIndex = startDayOfWeek + day - 1;
|
||||
var row = cellIndex / 7;
|
||||
var col = cellIndex % 7;
|
||||
var cellRect = new SKRect(bounds.Left + col * cellWidth + 2, bounds.Top + row * cellHeight + 2, bounds.Left + (col + 1) * cellWidth - 2, bounds.Top + (row + 1) * cellHeight - 2);
|
||||
DateTime dayDate = new DateTime(_displayMonth.Year, _displayMonth.Month, day);
|
||||
int cellIndex = startDayOfWeek + day - 1;
|
||||
int row = cellIndex / 7;
|
||||
int col = cellIndex % 7;
|
||||
|
||||
var isSelected = dayDate.Date == Date.Date;
|
||||
var isToday = dayDate.Date == today;
|
||||
var isDisabled = dayDate < MinimumDate || dayDate > MaximumDate;
|
||||
cellRect = new SKRect(
|
||||
bounds.Left + col * cellWidth + 2f,
|
||||
bounds.Top + row * cellHeight + 2f,
|
||||
bounds.Left + (col + 1) * cellWidth - 2f,
|
||||
bounds.Top + (row + 1) * cellHeight - 2f);
|
||||
|
||||
bool isSelected = dayDate.Date == Date.Date;
|
||||
bool isToday = dayDate.Date == today;
|
||||
bool isDisabled = dayDate < MinimumDate || dayDate > MaximumDate;
|
||||
|
||||
if (isSelected)
|
||||
{
|
||||
bgPaint.Color = SelectedDayColor;
|
||||
canvas.DrawCircle(cellRect.MidX, cellRect.MidY, Math.Min(cellRect.Width, cellRect.Height) / 2 - 2, bgPaint);
|
||||
canvas.DrawCircle(cellRect.MidX, cellRect.MidY, Math.Min(cellRect.Width, cellRect.Height) / 2f - 2f, bgPaint);
|
||||
}
|
||||
else if (isToday)
|
||||
{
|
||||
bgPaint.Color = TodayColor;
|
||||
canvas.DrawCircle(cellRect.MidX, cellRect.MidY, Math.Min(cellRect.Width, cellRect.Height) / 2 - 2, bgPaint);
|
||||
canvas.DrawCircle(cellRect.MidX, cellRect.MidY, Math.Min(cellRect.Width, cellRect.Height) / 2f - 2f, bgPaint);
|
||||
}
|
||||
|
||||
textPaint.Color = isSelected ? SKColors.White : isDisabled ? DisabledDayColor : TextColor;
|
||||
var dayText = day.ToString();
|
||||
var textBounds = new SKRect();
|
||||
textPaint.MeasureText(dayText, ref textBounds);
|
||||
canvas.DrawText(dayText, cellRect.MidX - textBounds.MidX, cellRect.MidY - textBounds.MidY, textPaint);
|
||||
textPaint.Color = isSelected ? SKColors.White : (isDisabled ? DisabledDayColor : TextColor);
|
||||
string dayText = day.ToString();
|
||||
SKRect dayTextBounds = default;
|
||||
textPaint.MeasureText(dayText, ref dayTextBounds);
|
||||
canvas.DrawText(dayText, cellRect.MidX - dayTextBounds.MidX, cellRect.MidY - dayTextBounds.MidY, textPaint);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides
|
||||
|
||||
protected override void OnDraw(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
DrawPickerButton(canvas, bounds);
|
||||
}
|
||||
|
||||
public override void OnPointerPressed(PointerEventArgs e)
|
||||
{
|
||||
if (!IsEnabled) return;
|
||||
if (!IsEnabled)
|
||||
return;
|
||||
|
||||
if (IsOpen)
|
||||
{
|
||||
// Use ScreenBounds for popup coordinate calculations (accounts for scroll offset)
|
||||
var screenBounds = ScreenBounds;
|
||||
var calendarRect = GetCalendarRect(screenBounds);
|
||||
SKRect screenBounds = ScreenBounds;
|
||||
SKRect calendarRect = GetCalendarRect(screenBounds);
|
||||
|
||||
// Check if click is in header area (navigation arrows)
|
||||
var headerRect = new SKRect(calendarRect.Left, calendarRect.Top, calendarRect.Right, calendarRect.Top + HeaderHeight);
|
||||
SKRect headerRect = new SKRect(calendarRect.Left, calendarRect.Top, calendarRect.Right, calendarRect.Top + 48f);
|
||||
if (headerRect.Contains(e.X, e.Y))
|
||||
{
|
||||
if (e.X < calendarRect.Left + 40) { _displayMonth = _displayMonth.AddMonths(-1); Invalidate(); return; }
|
||||
if (e.X > calendarRect.Right - 40) { _displayMonth = _displayMonth.AddMonths(1); Invalidate(); return; }
|
||||
if (e.X < calendarRect.Left + 40f)
|
||||
{
|
||||
_displayMonth = _displayMonth.AddMonths(-1);
|
||||
Invalidate();
|
||||
}
|
||||
else if (e.X > calendarRect.Right - 40f)
|
||||
{
|
||||
_displayMonth = _displayMonth.AddMonths(1);
|
||||
Invalidate();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if click is in days area
|
||||
var daysTop = calendarRect.Top + HeaderHeight + 30;
|
||||
var daysRect = new SKRect(calendarRect.Left, daysTop, calendarRect.Right, calendarRect.Bottom);
|
||||
float daysTop = calendarRect.Top + 48f + 30f;
|
||||
SKRect daysRect = new SKRect(calendarRect.Left, daysTop, calendarRect.Right, calendarRect.Bottom);
|
||||
if (daysRect.Contains(e.X, e.Y))
|
||||
{
|
||||
var cellWidth = CalendarWidth / 7;
|
||||
var cellHeight = (CalendarHeight - HeaderHeight - 40) / 6;
|
||||
var col = (int)((e.X - calendarRect.Left) / cellWidth);
|
||||
var row = (int)((e.Y - daysTop) / cellHeight);
|
||||
var firstDay = new DateTime(_displayMonth.Year, _displayMonth.Month, 1);
|
||||
var dayIndex = row * 7 + col - (int)firstDay.DayOfWeek + 1;
|
||||
var daysInMonth = DateTime.DaysInMonth(_displayMonth.Year, _displayMonth.Month);
|
||||
float cellWidth = 40f;
|
||||
float cellHeight = 38.666668f;
|
||||
int col = (int)((e.X - calendarRect.Left) / cellWidth);
|
||||
int dayIndex = (int)((e.Y - daysTop) / cellHeight) * 7 + col - (int)new DateTime(_displayMonth.Year, _displayMonth.Month, 1).DayOfWeek + 1;
|
||||
int daysInMonth = DateTime.DaysInMonth(_displayMonth.Year, _displayMonth.Month);
|
||||
|
||||
if (dayIndex >= 1 && dayIndex <= daysInMonth)
|
||||
{
|
||||
var selectedDate = new DateTime(_displayMonth.Year, _displayMonth.Month, dayIndex);
|
||||
DateTime selectedDate = new DateTime(_displayMonth.Year, _displayMonth.Month, dayIndex);
|
||||
if (selectedDate >= MinimumDate && selectedDate <= MaximumDate)
|
||||
{
|
||||
Date = selectedDate;
|
||||
@@ -446,27 +526,53 @@ public class SkiaDatePicker : SkiaView
|
||||
return;
|
||||
}
|
||||
|
||||
// Click is outside calendar - check if it's on the picker itself
|
||||
if (screenBounds.Contains(e.X, e.Y))
|
||||
{
|
||||
IsOpen = false;
|
||||
}
|
||||
}
|
||||
else IsOpen = true;
|
||||
else
|
||||
{
|
||||
IsOpen = true;
|
||||
}
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
public override void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (!IsEnabled) return;
|
||||
if (!IsEnabled)
|
||||
return;
|
||||
|
||||
switch (e.Key)
|
||||
{
|
||||
case Key.Enter: case Key.Space: IsOpen = !IsOpen; e.Handled = true; break;
|
||||
case Key.Escape: if (IsOpen) { IsOpen = false; e.Handled = true; } break;
|
||||
case Key.Left: Date = Date.AddDays(-1); e.Handled = true; break;
|
||||
case Key.Right: Date = Date.AddDays(1); e.Handled = true; break;
|
||||
case Key.Up: Date = Date.AddDays(-7); e.Handled = true; break;
|
||||
case Key.Down: Date = Date.AddDays(7); e.Handled = true; break;
|
||||
case Key.Enter:
|
||||
case Key.Space:
|
||||
IsOpen = !IsOpen;
|
||||
e.Handled = true;
|
||||
break;
|
||||
case Key.Escape:
|
||||
if (IsOpen)
|
||||
{
|
||||
IsOpen = false;
|
||||
e.Handled = true;
|
||||
}
|
||||
break;
|
||||
case Key.Left:
|
||||
Date = Date.AddDays(-1.0);
|
||||
e.Handled = true;
|
||||
break;
|
||||
case Key.Right:
|
||||
Date = Date.AddDays(1.0);
|
||||
e.Handled = true;
|
||||
break;
|
||||
case Key.Up:
|
||||
Date = Date.AddDays(-7.0);
|
||||
e.Handled = true;
|
||||
break;
|
||||
case Key.Down:
|
||||
Date = Date.AddDays(7.0);
|
||||
e.Handled = true;
|
||||
break;
|
||||
}
|
||||
Invalidate();
|
||||
}
|
||||
@@ -474,7 +580,6 @@ public class SkiaDatePicker : SkiaView
|
||||
public override void OnFocusLost()
|
||||
{
|
||||
base.OnFocusLost();
|
||||
// Close popup when focus is lost (clicking outside)
|
||||
if (IsOpen)
|
||||
{
|
||||
IsOpen = false;
|
||||
@@ -483,28 +588,23 @@ public class SkiaDatePicker : SkiaView
|
||||
|
||||
protected override SKSize MeasureOverride(SKSize availableSize)
|
||||
{
|
||||
return new SKSize(availableSize.Width < float.MaxValue ? Math.Min(availableSize.Width, 200) : 200, 40);
|
||||
return new SKSize(availableSize.Width < float.MaxValue ? Math.Min(availableSize.Width, 200f) : 200f, 40f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override to include calendar popup area in hit testing.
|
||||
/// </summary>
|
||||
protected override bool HitTestPopupArea(float x, float y)
|
||||
{
|
||||
// Use ScreenBounds for hit testing (accounts for scroll offset)
|
||||
var screenBounds = ScreenBounds;
|
||||
|
||||
// Always include the picker button itself
|
||||
SKRect screenBounds = ScreenBounds;
|
||||
if (screenBounds.Contains(x, y))
|
||||
{
|
||||
return true;
|
||||
|
||||
// When open, also include the calendar area (with edge detection)
|
||||
}
|
||||
if (_isOpen)
|
||||
{
|
||||
var calendarRect = GetCalendarRect(screenBounds);
|
||||
SKRect calendarRect = GetCalendarRect(screenBounds);
|
||||
return calendarRect.Contains(x, y);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -1,19 +1,33 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SkiaSharp;
|
||||
using Microsoft.Maui.Graphics;
|
||||
using Svg.Skia;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
|
||||
/// <summary>
|
||||
/// Skia-rendered image control.
|
||||
/// Skia-rendered image control with SVG support.
|
||||
/// </summary>
|
||||
public class SkiaImage : SkiaView
|
||||
{
|
||||
private SKBitmap? _bitmap;
|
||||
private SKImage? _image;
|
||||
private bool _isLoading;
|
||||
private string? _currentFilePath;
|
||||
private bool _isSvg;
|
||||
private CancellationTokenSource? _loadCts;
|
||||
private readonly object _loadLock = new object();
|
||||
private double _svgLoadedWidth;
|
||||
private double _svgLoadedHeight;
|
||||
private bool _pendingSvgReload;
|
||||
private SKRect _lastArrangedBounds;
|
||||
|
||||
public SKBitmap? Bitmap
|
||||
{
|
||||
@@ -28,14 +42,64 @@ public class SkiaImage : SkiaView
|
||||
}
|
||||
}
|
||||
|
||||
public Aspect Aspect { get; set; } = Aspect.AspectFit;
|
||||
public Aspect Aspect { get; set; }
|
||||
|
||||
public bool IsOpaque { get; set; }
|
||||
|
||||
public bool IsLoading => _isLoading;
|
||||
|
||||
public bool IsAnimationPlaying { get; set; }
|
||||
|
||||
public new double WidthRequest
|
||||
{
|
||||
get => base.WidthRequest;
|
||||
set
|
||||
{
|
||||
base.WidthRequest = value;
|
||||
ScheduleSvgReloadIfNeeded();
|
||||
}
|
||||
}
|
||||
|
||||
public new double HeightRequest
|
||||
{
|
||||
get => base.HeightRequest;
|
||||
set
|
||||
{
|
||||
base.HeightRequest = value;
|
||||
ScheduleSvgReloadIfNeeded();
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler? ImageLoaded;
|
||||
public event EventHandler<ImageLoadingErrorEventArgs>? ImageLoadingError;
|
||||
|
||||
private void ScheduleSvgReloadIfNeeded()
|
||||
{
|
||||
if (_isSvg && !string.IsNullOrEmpty(_currentFilePath))
|
||||
{
|
||||
double widthRequest = WidthRequest;
|
||||
double heightRequest = HeightRequest;
|
||||
if (widthRequest > 0.0 && heightRequest > 0.0 &&
|
||||
(Math.Abs(_svgLoadedWidth - widthRequest) > 0.5 || Math.Abs(_svgLoadedHeight - heightRequest) > 0.5) &&
|
||||
!_pendingSvgReload)
|
||||
{
|
||||
_pendingSvgReload = true;
|
||||
ReloadSvgDebounced();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReloadSvgDebounced()
|
||||
{
|
||||
await Task.Delay(10);
|
||||
_pendingSvgReload = false;
|
||||
if (!string.IsNullOrEmpty(_currentFilePath) && WidthRequest > 0.0 && HeightRequest > 0.0)
|
||||
{
|
||||
Console.WriteLine($"[SkiaImage] Reloading SVG at {WidthRequest}x{HeightRequest} (was {_svgLoadedWidth}x{_svgLoadedHeight})");
|
||||
await LoadSvgAtSizeAsync(_currentFilePath, WidthRequest, HeightRequest);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDraw(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
// Draw background if not opaque
|
||||
@@ -49,14 +113,16 @@ public class SkiaImage : SkiaView
|
||||
canvas.DrawRect(bounds, bgPaint);
|
||||
}
|
||||
|
||||
if (_image == null) return;
|
||||
if (_image == null)
|
||||
return;
|
||||
|
||||
var imageWidth = _image.Width;
|
||||
var imageHeight = _image.Height;
|
||||
int width = _image.Width;
|
||||
int height = _image.Height;
|
||||
|
||||
if (imageWidth <= 0 || imageHeight <= 0) return;
|
||||
if (width <= 0 || height <= 0)
|
||||
return;
|
||||
|
||||
var destRect = CalculateDestRect(bounds, imageWidth, imageHeight);
|
||||
SKRect destRect = CalculateDestRect(bounds, width, height);
|
||||
|
||||
using var paint = new SKPaint
|
||||
{
|
||||
@@ -69,37 +135,37 @@ public class SkiaImage : SkiaView
|
||||
|
||||
private SKRect CalculateDestRect(SKRect bounds, float imageWidth, float imageHeight)
|
||||
{
|
||||
float destX, destY, destWidth, destHeight;
|
||||
|
||||
switch (Aspect)
|
||||
{
|
||||
case Aspect.Fill:
|
||||
// Stretch to fill entire bounds
|
||||
return bounds;
|
||||
|
||||
case Aspect.AspectFit:
|
||||
// Scale to fit while maintaining aspect ratio
|
||||
var fitScale = Math.Min(bounds.Width / imageWidth, bounds.Height / imageHeight);
|
||||
destWidth = imageWidth * fitScale;
|
||||
destHeight = imageHeight * fitScale;
|
||||
destX = bounds.Left + (bounds.Width - destWidth) / 2;
|
||||
destY = bounds.Top + (bounds.Height - destHeight) / 2;
|
||||
{
|
||||
float scale = Math.Min(bounds.Width / imageWidth, bounds.Height / imageHeight);
|
||||
float destWidth = imageWidth * scale;
|
||||
float destHeight = imageHeight * scale;
|
||||
float destX = bounds.Left + (bounds.Width - destWidth) / 2f;
|
||||
float destY = bounds.Top + (bounds.Height - destHeight) / 2f;
|
||||
return new SKRect(destX, destY, destX + destWidth, destY + destHeight);
|
||||
}
|
||||
|
||||
case Aspect.AspectFill:
|
||||
// Scale to fill while maintaining aspect ratio (may crop)
|
||||
var fillScale = Math.Max(bounds.Width / imageWidth, bounds.Height / imageHeight);
|
||||
destWidth = imageWidth * fillScale;
|
||||
destHeight = imageHeight * fillScale;
|
||||
destX = bounds.Left + (bounds.Width - destWidth) / 2;
|
||||
destY = bounds.Top + (bounds.Height - destHeight) / 2;
|
||||
{
|
||||
float scale = Math.Max(bounds.Width / imageWidth, bounds.Height / imageHeight);
|
||||
float destWidth = imageWidth * scale;
|
||||
float destHeight = imageHeight * scale;
|
||||
float destX = bounds.Left + (bounds.Width - destWidth) / 2f;
|
||||
float destY = bounds.Top + (bounds.Height - destHeight) / 2f;
|
||||
return new SKRect(destX, destY, destX + destWidth, destY + destHeight);
|
||||
}
|
||||
|
||||
case Aspect.Center:
|
||||
// Center without scaling
|
||||
destX = bounds.Left + (bounds.Width - imageWidth) / 2;
|
||||
destY = bounds.Top + (bounds.Height - imageHeight) / 2;
|
||||
{
|
||||
float destX = bounds.Left + (bounds.Width - imageWidth) / 2f;
|
||||
float destY = bounds.Top + (bounds.Height - imageHeight) / 2f;
|
||||
return new SKRect(destX, destY, destX + imageWidth, destY + imageHeight);
|
||||
}
|
||||
|
||||
default:
|
||||
return bounds;
|
||||
@@ -110,18 +176,69 @@ public class SkiaImage : SkiaView
|
||||
{
|
||||
_isLoading = true;
|
||||
Invalidate();
|
||||
Console.WriteLine($"[SkiaImage] LoadFromFileAsync: {filePath}, WidthRequest={WidthRequest}, HeightRequest={HeightRequest}");
|
||||
|
||||
try
|
||||
{
|
||||
await Task.Run(() =>
|
||||
List<string> searchPaths = new List<string>
|
||||
{
|
||||
using var stream = File.OpenRead(filePath);
|
||||
var bitmap = SKBitmap.Decode(stream);
|
||||
if (bitmap != null)
|
||||
filePath,
|
||||
Path.Combine(AppContext.BaseDirectory, filePath),
|
||||
Path.Combine(AppContext.BaseDirectory, "Resources", "Images", filePath),
|
||||
Path.Combine(AppContext.BaseDirectory, "Resources", filePath)
|
||||
};
|
||||
|
||||
// Also try SVG if looking for PNG
|
||||
if (filePath.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string svgPath = Path.ChangeExtension(filePath, ".svg");
|
||||
searchPaths.Add(svgPath);
|
||||
searchPaths.Add(Path.Combine(AppContext.BaseDirectory, svgPath));
|
||||
searchPaths.Add(Path.Combine(AppContext.BaseDirectory, "Resources", "Images", svgPath));
|
||||
searchPaths.Add(Path.Combine(AppContext.BaseDirectory, "Resources", svgPath));
|
||||
}
|
||||
|
||||
string? foundPath = null;
|
||||
foreach (string path in searchPaths)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
Bitmap = bitmap;
|
||||
foundPath = path;
|
||||
Console.WriteLine("[SkiaImage] Found file at: " + path);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (foundPath == null)
|
||||
{
|
||||
Console.WriteLine("[SkiaImage] File not found: " + filePath);
|
||||
_isLoading = false;
|
||||
_isSvg = false;
|
||||
_currentFilePath = null;
|
||||
ImageLoadingError?.Invoke(this, new ImageLoadingErrorEventArgs(new FileNotFoundException(filePath)));
|
||||
return;
|
||||
}
|
||||
|
||||
_isSvg = foundPath.EndsWith(".svg", StringComparison.OrdinalIgnoreCase);
|
||||
_currentFilePath = foundPath;
|
||||
|
||||
if (!_isSvg)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
using FileStream stream = File.OpenRead(foundPath);
|
||||
SKBitmap? bitmap = SKBitmap.Decode(stream);
|
||||
if (bitmap != null)
|
||||
{
|
||||
Bitmap = bitmap;
|
||||
Console.WriteLine("[SkiaImage] Loaded image: " + foundPath);
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
await LoadSvgAtSizeAsync(foundPath, WidthRequest, HeightRequest);
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
ImageLoaded?.Invoke(this, EventArgs.Empty);
|
||||
@@ -135,6 +252,69 @@ public class SkiaImage : SkiaView
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
private async Task LoadSvgAtSizeAsync(string svgPath, double targetWidth, double targetHeight)
|
||||
{
|
||||
_loadCts?.Cancel();
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
_loadCts = cts;
|
||||
|
||||
try
|
||||
{
|
||||
SKBitmap? newBitmap = null;
|
||||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
if (cts.Token.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
using var svg = new SKSvg();
|
||||
svg.Load(svgPath);
|
||||
|
||||
if (svg.Picture != null && !cts.Token.IsCancellationRequested)
|
||||
{
|
||||
SKRect cullRect = svg.Picture.CullRect;
|
||||
|
||||
float requestedWidth = (targetWidth > 0.0)
|
||||
? (float)targetWidth
|
||||
: ((cullRect.Width <= 24f) ? 24f : cullRect.Width);
|
||||
|
||||
float requestedHeight = (targetHeight > 0.0)
|
||||
? (float)targetHeight
|
||||
: ((cullRect.Height <= 24f) ? 24f : cullRect.Height);
|
||||
|
||||
float scale = Math.Min(requestedWidth / cullRect.Width, requestedHeight / cullRect.Height);
|
||||
|
||||
int bitmapWidth = Math.Max(1, (int)(cullRect.Width * scale));
|
||||
int bitmapHeight = Math.Max(1, (int)(cullRect.Height * scale));
|
||||
|
||||
newBitmap = new SKBitmap(bitmapWidth, bitmapHeight, false);
|
||||
|
||||
using var canvas = new SKCanvas(newBitmap);
|
||||
canvas.Clear(SKColors.Transparent);
|
||||
canvas.Scale(scale);
|
||||
canvas.DrawPicture(svg.Picture, null);
|
||||
|
||||
Console.WriteLine($"[SkiaImage] Loaded SVG: {svgPath} at {bitmapWidth}x{bitmapHeight} (requested {targetWidth}x{targetHeight})");
|
||||
}
|
||||
}, cts.Token);
|
||||
|
||||
if (!cts.Token.IsCancellationRequested && newBitmap != null)
|
||||
{
|
||||
_svgLoadedWidth = (targetWidth > 0.0) ? targetWidth : newBitmap.Width;
|
||||
_svgLoadedHeight = (targetHeight > 0.0) ? targetHeight : newBitmap.Height;
|
||||
Bitmap = newBitmap;
|
||||
}
|
||||
else
|
||||
{
|
||||
newBitmap?.Dispose();
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Cancellation is expected when reloading SVG at different sizes
|
||||
}
|
||||
}
|
||||
|
||||
public async Task LoadFromStreamAsync(Stream stream)
|
||||
{
|
||||
_isLoading = true;
|
||||
@@ -144,7 +324,7 @@ public class SkiaImage : SkiaView
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var bitmap = SKBitmap.Decode(stream);
|
||||
SKBitmap? bitmap = SKBitmap.Decode(stream);
|
||||
if (bitmap != null)
|
||||
{
|
||||
Bitmap = bitmap;
|
||||
@@ -170,11 +350,9 @@ public class SkiaImage : SkiaView
|
||||
|
||||
try
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
var data = await httpClient.GetByteArrayAsync(uri);
|
||||
|
||||
using var stream = new MemoryStream(data);
|
||||
var bitmap = SKBitmap.Decode(stream);
|
||||
using HttpClient httpClient = new HttpClient();
|
||||
using MemoryStream stream = new MemoryStream(await httpClient.GetByteArrayAsync(uri));
|
||||
SKBitmap? bitmap = SKBitmap.Decode(stream);
|
||||
if (bitmap != null)
|
||||
{
|
||||
Bitmap = bitmap;
|
||||
@@ -196,8 +374,8 @@ public class SkiaImage : SkiaView
|
||||
{
|
||||
try
|
||||
{
|
||||
using var stream = new MemoryStream(data);
|
||||
var bitmap = SKBitmap.Decode(stream);
|
||||
using MemoryStream stream = new MemoryStream(data);
|
||||
SKBitmap? bitmap = SKBitmap.Decode(stream);
|
||||
if (bitmap != null)
|
||||
{
|
||||
Bitmap = bitmap;
|
||||
@@ -217,6 +395,8 @@ public class SkiaImage : SkiaView
|
||||
{
|
||||
try
|
||||
{
|
||||
_isSvg = false;
|
||||
_currentFilePath = null;
|
||||
Bitmap = bitmap;
|
||||
_isLoading = false;
|
||||
ImageLoaded?.Invoke(this, EventArgs.Empty);
|
||||
@@ -229,28 +409,84 @@ public class SkiaImage : SkiaView
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
public override void Arrange(SKRect bounds)
|
||||
{
|
||||
base.Arrange(bounds);
|
||||
|
||||
// If no explicit size requested and this is an SVG, check if we need to reload at larger size
|
||||
if (!(base.WidthRequest > 0.0) || !(base.HeightRequest > 0.0))
|
||||
{
|
||||
if (_isSvg && !string.IsNullOrEmpty(_currentFilePath) && !_isLoading)
|
||||
{
|
||||
float width = bounds.Width;
|
||||
float height = bounds.Height;
|
||||
|
||||
if ((width > _svgLoadedWidth * 1.1 || height > _svgLoadedHeight * 1.1) &&
|
||||
width > 0f && height > 0f &&
|
||||
(width != _lastArrangedBounds.Width || height != _lastArrangedBounds.Height))
|
||||
{
|
||||
_lastArrangedBounds = bounds;
|
||||
Console.WriteLine($"[SkiaImage] Arrange detected larger bounds: {width}x{height} vs loaded {_svgLoadedWidth}x{_svgLoadedHeight}");
|
||||
LoadSvgAtSizeAsync(_currentFilePath, width, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override SKSize MeasureOverride(SKSize availableSize)
|
||||
{
|
||||
double widthRequest = base.WidthRequest;
|
||||
double heightRequest = base.HeightRequest;
|
||||
|
||||
// If both dimensions explicitly requested, use them
|
||||
if (widthRequest > 0.0 && heightRequest > 0.0)
|
||||
{
|
||||
return new SKSize((float)widthRequest, (float)heightRequest);
|
||||
}
|
||||
|
||||
// If no image, return default or requested size
|
||||
if (_image == null)
|
||||
return new SKSize(100, 100); // Default size
|
||||
{
|
||||
if (widthRequest > 0.0)
|
||||
return new SKSize((float)widthRequest, (float)widthRequest);
|
||||
if (heightRequest > 0.0)
|
||||
return new SKSize((float)heightRequest, (float)heightRequest);
|
||||
return new SKSize(100f, 100f);
|
||||
}
|
||||
|
||||
var imageWidth = _image.Width;
|
||||
var imageHeight = _image.Height;
|
||||
float imageWidth = _image.Width;
|
||||
float imageHeight = _image.Height;
|
||||
|
||||
// If we have constraints, respect them
|
||||
// If only width requested, scale height proportionally
|
||||
if (widthRequest > 0.0)
|
||||
{
|
||||
float scale = (float)widthRequest / imageWidth;
|
||||
return new SKSize((float)widthRequest, imageHeight * scale);
|
||||
}
|
||||
|
||||
// If only height requested, scale width proportionally
|
||||
if (heightRequest > 0.0)
|
||||
{
|
||||
float scale = (float)heightRequest / imageHeight;
|
||||
return new SKSize(imageWidth * scale, (float)heightRequest);
|
||||
}
|
||||
|
||||
// Scale to fit available size
|
||||
if (availableSize.Width < float.MaxValue && availableSize.Height < float.MaxValue)
|
||||
{
|
||||
var scale = Math.Min(availableSize.Width / imageWidth, availableSize.Height / imageHeight);
|
||||
float scale = Math.Min(availableSize.Width / imageWidth, availableSize.Height / imageHeight);
|
||||
return new SKSize(imageWidth * scale, imageHeight * scale);
|
||||
}
|
||||
else if (availableSize.Width < float.MaxValue)
|
||||
|
||||
if (availableSize.Width < float.MaxValue)
|
||||
{
|
||||
var scale = availableSize.Width / imageWidth;
|
||||
float scale = availableSize.Width / imageWidth;
|
||||
return new SKSize(availableSize.Width, imageHeight * scale);
|
||||
}
|
||||
else if (availableSize.Height < float.MaxValue)
|
||||
|
||||
if (availableSize.Height < float.MaxValue)
|
||||
{
|
||||
var scale = availableSize.Height / imageHeight;
|
||||
float scale = availableSize.Height / imageHeight;
|
||||
return new SKSize(imageWidth * scale, availableSize.Height);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(string),
|
||||
typeof(SkiaLabel),
|
||||
"",
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).OnTextChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -36,6 +37,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(IList<SkiaTextSpan>),
|
||||
typeof(SkiaLabel),
|
||||
null,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).OnTextChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -47,6 +49,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaLabel),
|
||||
SKColors.Black,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -58,6 +61,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(string),
|
||||
typeof(SkiaLabel),
|
||||
"Sans",
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).OnFontChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -69,6 +73,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaLabel),
|
||||
14f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).OnFontChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -80,6 +85,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(bool),
|
||||
typeof(SkiaLabel),
|
||||
false,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).OnFontChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -91,6 +97,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(bool),
|
||||
typeof(SkiaLabel),
|
||||
false,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).OnFontChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -102,6 +109,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(bool),
|
||||
typeof(SkiaLabel),
|
||||
false,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -113,6 +121,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(bool),
|
||||
typeof(SkiaLabel),
|
||||
false,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -124,6 +133,7 @@ public class SkiaLabel : SkiaView
|
||||
typeof(TextAlignment),
|
||||
typeof(SkiaLabel),
|
||||
TextAlignment.Start,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaLabel)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -419,7 +429,7 @@ public class SkiaLabel : SkiaView
|
||||
{
|
||||
if (System.IO.File.Exists(path))
|
||||
{
|
||||
_cachedTypeface = SKTypeface.FromFile(path);
|
||||
_cachedTypeface = SKTypeface.FromFile(path, 0);
|
||||
if (_cachedTypeface != null) return _cachedTypeface;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(int),
|
||||
typeof(SkiaPicker),
|
||||
-1,
|
||||
BindingMode.TwoWay,
|
||||
BindingMode.OneWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).OnSelectedIndexChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -33,6 +33,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(string),
|
||||
typeof(SkiaPicker),
|
||||
"",
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -44,6 +45,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaPicker),
|
||||
SKColors.Black,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -55,6 +57,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaPicker),
|
||||
new SKColor(0x80, 0x80, 0x80),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -66,6 +69,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaPicker),
|
||||
new SKColor(0xBD, 0xBD, 0xBD),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -77,6 +81,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaPicker),
|
||||
SKColors.White,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -88,6 +93,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaPicker),
|
||||
new SKColor(0x21, 0x96, 0xF3, 0x30),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -99,6 +105,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaPicker),
|
||||
new SKColor(0xE0, 0xE0, 0xE0),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -110,6 +117,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(string),
|
||||
typeof(SkiaPicker),
|
||||
"Sans",
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).InvalidateMeasure());
|
||||
|
||||
/// <summary>
|
||||
@@ -121,6 +129,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaPicker),
|
||||
14f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).InvalidateMeasure());
|
||||
|
||||
/// <summary>
|
||||
@@ -132,6 +141,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaPicker),
|
||||
40f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -143,6 +153,7 @@ public class SkiaPicker : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaPicker),
|
||||
4f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaPicker)b).Invalidate());
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System;
|
||||
using Microsoft.Maui.Controls;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
@@ -21,8 +23,8 @@ public class SkiaProgressBar : SkiaView
|
||||
typeof(double),
|
||||
typeof(SkiaProgressBar),
|
||||
0.0,
|
||||
BindingMode.TwoWay,
|
||||
coerceValue: (b, v) => Math.Clamp((double)v, 0, 1),
|
||||
BindingMode.OneWay,
|
||||
coerceValue: (b, v) => Math.Clamp((double)v, 0.0, 1.0),
|
||||
propertyChanged: (b, o, n) => ((SkiaProgressBar)b).OnProgressChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -33,7 +35,8 @@ public class SkiaProgressBar : SkiaView
|
||||
nameof(TrackColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaProgressBar),
|
||||
new SKColor(0xE0, 0xE0, 0xE0),
|
||||
new SKColor(224, 224, 224),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaProgressBar)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -44,7 +47,8 @@ public class SkiaProgressBar : SkiaView
|
||||
nameof(ProgressColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaProgressBar),
|
||||
new SKColor(0x21, 0x96, 0xF3),
|
||||
new SKColor(33, 150, 243),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaProgressBar)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -55,7 +59,8 @@ public class SkiaProgressBar : SkiaView
|
||||
nameof(DisabledColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaProgressBar),
|
||||
new SKColor(0xBD, 0xBD, 0xBD),
|
||||
new SKColor(189, 189, 189),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaProgressBar)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -67,6 +72,7 @@ public class SkiaProgressBar : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaProgressBar),
|
||||
4f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaProgressBar)b).InvalidateMeasure());
|
||||
|
||||
/// <summary>
|
||||
@@ -78,6 +84,7 @@ public class SkiaProgressBar : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaProgressBar),
|
||||
2f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaProgressBar)b).Invalidate());
|
||||
|
||||
#endregion
|
||||
@@ -153,9 +160,9 @@ public class SkiaProgressBar : SkiaView
|
||||
|
||||
protected override void OnDraw(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
var trackY = bounds.MidY;
|
||||
var trackTop = trackY - BarHeight / 2;
|
||||
var trackBottom = trackY + BarHeight / 2;
|
||||
float midY = bounds.MidY;
|
||||
float trackTop = midY - BarHeight / 2f;
|
||||
float trackBottom = midY + BarHeight / 2f;
|
||||
|
||||
// Draw track
|
||||
using var trackPaint = new SKPaint
|
||||
@@ -171,9 +178,9 @@ public class SkiaProgressBar : SkiaView
|
||||
canvas.DrawRoundRect(trackRect, trackPaint);
|
||||
|
||||
// Draw progress
|
||||
if (Progress > 0)
|
||||
if (Progress > 0.0)
|
||||
{
|
||||
var progressWidth = bounds.Width * (float)Progress;
|
||||
float progressWidth = bounds.Width * (float)Progress;
|
||||
|
||||
using var progressPaint = new SKPaint
|
||||
{
|
||||
@@ -191,7 +198,7 @@ public class SkiaProgressBar : SkiaView
|
||||
|
||||
protected override SKSize MeasureOverride(SKSize availableSize)
|
||||
{
|
||||
return new SKSize(200, BarHeight + 8);
|
||||
return new SKSize(200f, BarHeight + 8f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System;
|
||||
using Microsoft.Maui.Controls;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
@@ -21,6 +23,7 @@ public class SkiaSlider : SkiaView
|
||||
typeof(double),
|
||||
typeof(SkiaSlider),
|
||||
0.0,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSlider)b).OnRangeChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -32,6 +35,7 @@ public class SkiaSlider : SkiaView
|
||||
typeof(double),
|
||||
typeof(SkiaSlider),
|
||||
100.0,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSlider)b).OnRangeChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -43,7 +47,7 @@ public class SkiaSlider : SkiaView
|
||||
typeof(double),
|
||||
typeof(SkiaSlider),
|
||||
0.0,
|
||||
BindingMode.TwoWay,
|
||||
BindingMode.OneWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSlider)b).OnValuePropertyChanged((double)o, (double)n));
|
||||
|
||||
/// <summary>
|
||||
@@ -55,6 +59,7 @@ public class SkiaSlider : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaSlider),
|
||||
new SKColor(0xE0, 0xE0, 0xE0),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSlider)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -66,6 +71,7 @@ public class SkiaSlider : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaSlider),
|
||||
new SKColor(0x21, 0x96, 0xF3),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSlider)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -77,6 +83,7 @@ public class SkiaSlider : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaSlider),
|
||||
new SKColor(0x21, 0x96, 0xF3),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSlider)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -88,6 +95,7 @@ public class SkiaSlider : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaSlider),
|
||||
new SKColor(0xBD, 0xBD, 0xBD),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSlider)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -99,6 +107,7 @@ public class SkiaSlider : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaSlider),
|
||||
4f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSlider)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -110,6 +119,7 @@ public class SkiaSlider : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaSlider),
|
||||
10f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSlider)b).InvalidateMeasure());
|
||||
|
||||
#endregion
|
||||
@@ -318,7 +328,7 @@ public class SkiaSlider : SkiaView
|
||||
_isDragging = true;
|
||||
UpdateValueFromPosition(e.X);
|
||||
DragStarted?.Invoke(this, EventArgs.Empty);
|
||||
SkiaVisualStateManager.GoToState(this, SkiaVisualStateManager.CommonStates.Pressed);
|
||||
SkiaVisualStateManager.GoToState(this, "Pressed");
|
||||
}
|
||||
|
||||
public override void OnPointerMoved(PointerEventArgs e)
|
||||
@@ -333,7 +343,7 @@ public class SkiaSlider : SkiaView
|
||||
{
|
||||
_isDragging = false;
|
||||
DragCompleted?.Invoke(this, EventArgs.Empty);
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? SkiaVisualStateManager.CommonStates.Normal : SkiaVisualStateManager.CommonStates.Disabled);
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,7 +389,7 @@ public class SkiaSlider : SkiaView
|
||||
protected override void OnEnabledChanged()
|
||||
{
|
||||
base.OnEnabledChanged();
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? SkiaVisualStateManager.CommonStates.Normal : SkiaVisualStateManager.CommonStates.Disabled);
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled");
|
||||
}
|
||||
|
||||
protected override SKSize MeasureOverride(SKSize availableSize)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
using System;
|
||||
using Microsoft.Maui.Controls;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Microsoft.Maui.Platform;
|
||||
@@ -21,7 +23,7 @@ public class SkiaSwitch : SkiaView
|
||||
typeof(bool),
|
||||
typeof(SkiaSwitch),
|
||||
false,
|
||||
BindingMode.TwoWay,
|
||||
BindingMode.OneWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSwitch)b).OnIsOnChanged());
|
||||
|
||||
/// <summary>
|
||||
@@ -32,7 +34,8 @@ public class SkiaSwitch : SkiaView
|
||||
nameof(OnTrackColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaSwitch),
|
||||
new SKColor(0x21, 0x96, 0xF3),
|
||||
new SKColor(33, 150, 243),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSwitch)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -43,7 +46,8 @@ public class SkiaSwitch : SkiaView
|
||||
nameof(OffTrackColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaSwitch),
|
||||
new SKColor(0x9E, 0x9E, 0x9E),
|
||||
new SKColor(158, 158, 158),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSwitch)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -55,6 +59,7 @@ public class SkiaSwitch : SkiaView
|
||||
typeof(SKColor),
|
||||
typeof(SkiaSwitch),
|
||||
SKColors.White,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSwitch)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -65,7 +70,8 @@ public class SkiaSwitch : SkiaView
|
||||
nameof(DisabledColor),
|
||||
typeof(SKColor),
|
||||
typeof(SkiaSwitch),
|
||||
new SKColor(0xBD, 0xBD, 0xBD),
|
||||
new SKColor(189, 189, 189),
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSwitch)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -77,6 +83,7 @@ public class SkiaSwitch : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaSwitch),
|
||||
52f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSwitch)b).InvalidateMeasure());
|
||||
|
||||
/// <summary>
|
||||
@@ -88,6 +95,7 @@ public class SkiaSwitch : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaSwitch),
|
||||
32f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSwitch)b).InvalidateMeasure());
|
||||
|
||||
/// <summary>
|
||||
@@ -99,6 +107,7 @@ public class SkiaSwitch : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaSwitch),
|
||||
12f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSwitch)b).Invalidate());
|
||||
|
||||
/// <summary>
|
||||
@@ -110,6 +119,7 @@ public class SkiaSwitch : SkiaView
|
||||
typeof(float),
|
||||
typeof(SkiaSwitch),
|
||||
4f,
|
||||
BindingMode.TwoWay,
|
||||
propertyChanged: (b, o, n) => ((SkiaSwitch)b).Invalidate());
|
||||
|
||||
#endregion
|
||||
@@ -199,7 +209,7 @@ public class SkiaSwitch : SkiaView
|
||||
|
||||
#endregion
|
||||
|
||||
private float _animationProgress; // 0 = off, 1 = on
|
||||
private float _animationProgress;
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when the switch is toggled.
|
||||
@@ -215,14 +225,14 @@ public class SkiaSwitch : SkiaView
|
||||
{
|
||||
_animationProgress = IsOn ? 1f : 0f;
|
||||
Toggled?.Invoke(this, new ToggledEventArgs(IsOn));
|
||||
SkiaVisualStateManager.GoToState(this, IsOn ? SkiaVisualStateManager.CommonStates.On : SkiaVisualStateManager.CommonStates.Off);
|
||||
SkiaVisualStateManager.GoToState(this, IsOn ? "On" : "Off");
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
protected override void OnDraw(SKCanvas canvas, SKRect bounds)
|
||||
{
|
||||
var centerY = bounds.MidY;
|
||||
var trackLeft = bounds.MidX - TrackWidth / 2;
|
||||
var trackLeft = bounds.MidX - TrackWidth / 2f;
|
||||
var trackRight = trackLeft + TrackWidth;
|
||||
|
||||
// Calculate thumb position
|
||||
@@ -244,8 +254,8 @@ public class SkiaSwitch : SkiaView
|
||||
};
|
||||
|
||||
var trackRect = new SKRoundRect(
|
||||
new SKRect(trackLeft, centerY - TrackHeight / 2, trackRight, centerY + TrackHeight / 2),
|
||||
TrackHeight / 2);
|
||||
new SKRect(trackLeft, centerY - TrackHeight / 2f, trackRight, centerY + TrackHeight / 2f),
|
||||
TrackHeight / 2f);
|
||||
canvas.DrawRoundRect(trackRect, trackPaint);
|
||||
|
||||
// Draw thumb shadow
|
||||
@@ -255,15 +265,15 @@ public class SkiaSwitch : SkiaView
|
||||
{
|
||||
Color = new SKColor(0, 0, 0, 40),
|
||||
IsAntialias = true,
|
||||
MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 2)
|
||||
MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 2f)
|
||||
};
|
||||
canvas.DrawCircle(thumbX + 1, centerY + 1, ThumbRadius, shadowPaint);
|
||||
canvas.DrawCircle(thumbX + 1f, centerY + 1f, ThumbRadius, shadowPaint);
|
||||
}
|
||||
|
||||
// Draw thumb
|
||||
using var thumbPaint = new SKPaint
|
||||
{
|
||||
Color = IsEnabled ? ThumbColor : new SKColor(0xF5, 0xF5, 0xF5),
|
||||
Color = IsEnabled ? ThumbColor : new SKColor(245, 245, 245),
|
||||
IsAntialias = true,
|
||||
Style = SKPaintStyle.Fill
|
||||
};
|
||||
@@ -277,10 +287,10 @@ public class SkiaSwitch : SkiaView
|
||||
Color = OnTrackColor.WithAlpha(60),
|
||||
IsAntialias = true,
|
||||
Style = SKPaintStyle.Stroke,
|
||||
StrokeWidth = 3
|
||||
StrokeWidth = 3f
|
||||
};
|
||||
var focusRect = new SKRoundRect(trackRect.Rect, TrackHeight / 2);
|
||||
focusRect.Inflate(3, 3);
|
||||
var focusRect = new SKRoundRect(trackRect.Rect, TrackHeight / 2f);
|
||||
focusRect.Inflate(3f, 3f);
|
||||
canvas.DrawRoundRect(focusRect, focusPaint);
|
||||
}
|
||||
}
|
||||
@@ -296,21 +306,20 @@ public class SkiaSwitch : SkiaView
|
||||
|
||||
public override void OnPointerPressed(PointerEventArgs e)
|
||||
{
|
||||
if (!IsEnabled) return;
|
||||
IsOn = !IsOn;
|
||||
e.Handled = true;
|
||||
if (IsEnabled)
|
||||
{
|
||||
IsOn = !IsOn;
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnPointerReleased(PointerEventArgs e)
|
||||
{
|
||||
// Toggle handled in OnPointerPressed
|
||||
}
|
||||
|
||||
public override void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
if (!IsEnabled) return;
|
||||
|
||||
if (e.Key == Key.Space || e.Key == Key.Enter)
|
||||
if (IsEnabled && (e.Key == Key.Space || e.Key == Key.Enter))
|
||||
{
|
||||
IsOn = !IsOn;
|
||||
e.Handled = true;
|
||||
@@ -320,20 +329,11 @@ public class SkiaSwitch : SkiaView
|
||||
protected override void OnEnabledChanged()
|
||||
{
|
||||
base.OnEnabledChanged();
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? SkiaVisualStateManager.CommonStates.Normal : SkiaVisualStateManager.CommonStates.Disabled);
|
||||
SkiaVisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled");
|
||||
}
|
||||
|
||||
protected override SKSize MeasureOverride(SKSize availableSize)
|
||||
{
|
||||
return new SKSize(TrackWidth + 8, TrackHeight + 8);
|
||||
return new SKSize(TrackWidth + 8f, TrackHeight + 8f);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event args for toggled events.
|
||||
/// </summary>
|
||||
public class ToggledEventArgs : EventArgs
|
||||
{
|
||||
public bool Value { get; }
|
||||
public ToggledEventArgs(bool value) => Value = value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user