2025-12-19 09:30:16 +00:00
|
|
|
// Licensed to the .NET Foundation under one or more agreements.
|
|
|
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
using System;
|
|
|
|
|
using Microsoft.Maui.Controls;
|
|
|
|
|
using Microsoft.Maui.Graphics;
|
2025-12-19 09:30:16 +00:00
|
|
|
using SkiaSharp;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Maui.Platform;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Skia-rendered stepper control with increment/decrement buttons.
|
2026-01-16 05:01:27 +00:00
|
|
|
/// Implements IStepper interface requirements:
|
|
|
|
|
/// - Minimum, Maximum, Value, Increment properties
|
|
|
|
|
/// - ValueChanged event with old/new values
|
2025-12-19 09:30:16 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public class SkiaStepper : SkiaView
|
|
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
#region SKColor Helper
|
|
|
|
|
|
|
|
|
|
private static SKColor ToSKColor(Color? color)
|
|
|
|
|
{
|
|
|
|
|
if (color == null) return SKColors.Transparent;
|
2026-01-17 03:36:37 +00:00
|
|
|
return color.ToSKColor();
|
2026-01-16 05:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2025-12-21 13:26:56 -05:00
|
|
|
#region BindableProperties
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty ValueProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(Value),
|
|
|
|
|
typeof(double),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
0.0,
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).OnValuePropertyChanged((double)o, (double)n));
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty MinimumProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(Minimum),
|
|
|
|
|
typeof(double),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
0.0,
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).OnRangeChanged());
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty MaximumProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(Maximum),
|
|
|
|
|
typeof(double),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
100.0,
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).OnRangeChanged());
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty IncrementProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(Increment),
|
|
|
|
|
typeof(double),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
1.0,
|
|
|
|
|
BindingMode.TwoWay);
|
2025-12-21 13:26:56 -05:00
|
|
|
|
|
|
|
|
public static readonly BindableProperty ButtonBackgroundColorProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(ButtonBackgroundColor),
|
|
|
|
|
typeof(Color),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
Color.FromRgb(0xE0, 0xE0, 0xE0),
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).Invalidate());
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty ButtonPressedColorProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(ButtonPressedColor),
|
|
|
|
|
typeof(Color),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
Color.FromRgb(0xBD, 0xBD, 0xBD),
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).Invalidate());
|
2025-12-19 09:30:16 +00:00
|
|
|
|
2025-12-21 13:26:56 -05:00
|
|
|
public static readonly BindableProperty ButtonDisabledColorProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(ButtonDisabledColor),
|
|
|
|
|
typeof(Color),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
Color.FromRgb(0xF5, 0xF5, 0xF5),
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).Invalidate());
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty BorderColorProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(BorderColor),
|
|
|
|
|
typeof(Color),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
Color.FromRgb(0xBD, 0xBD, 0xBD),
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).Invalidate());
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty SymbolColorProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(SymbolColor),
|
|
|
|
|
typeof(Color),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
Colors.Black,
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).Invalidate());
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty SymbolDisabledColorProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(SymbolDisabledColor),
|
|
|
|
|
typeof(Color),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
Color.FromRgb(0xBD, 0xBD, 0xBD),
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).Invalidate());
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty CornerRadiusProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(CornerRadius),
|
|
|
|
|
typeof(double),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
4.0,
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).Invalidate());
|
|
|
|
|
|
|
|
|
|
public static readonly BindableProperty ButtonWidthProperty =
|
2026-01-16 05:01:27 +00:00
|
|
|
BindableProperty.Create(
|
|
|
|
|
nameof(ButtonWidth),
|
|
|
|
|
typeof(double),
|
|
|
|
|
typeof(SkiaStepper),
|
|
|
|
|
40.0,
|
|
|
|
|
BindingMode.TwoWay,
|
2025-12-21 13:26:56 -05:00
|
|
|
propertyChanged: (b, o, n) => ((SkiaStepper)b).InvalidateMeasure());
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Properties
|
2025-12-19 09:30:16 +00:00
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the current value.
|
|
|
|
|
/// </summary>
|
2025-12-19 09:30:16 +00:00
|
|
|
public double Value
|
|
|
|
|
{
|
2025-12-21 13:26:56 -05:00
|
|
|
get => (double)GetValue(ValueProperty);
|
|
|
|
|
set => SetValue(ValueProperty, Math.Clamp(value, Minimum, Maximum));
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the minimum value.
|
|
|
|
|
/// </summary>
|
2025-12-19 09:30:16 +00:00
|
|
|
public double Minimum
|
|
|
|
|
{
|
2025-12-21 13:26:56 -05:00
|
|
|
get => (double)GetValue(MinimumProperty);
|
|
|
|
|
set => SetValue(MinimumProperty, value);
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the maximum value.
|
|
|
|
|
/// </summary>
|
2025-12-19 09:30:16 +00:00
|
|
|
public double Maximum
|
|
|
|
|
{
|
2025-12-21 13:26:56 -05:00
|
|
|
get => (double)GetValue(MaximumProperty);
|
|
|
|
|
set => SetValue(MaximumProperty, value);
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the increment amount.
|
|
|
|
|
/// </summary>
|
2025-12-19 09:30:16 +00:00
|
|
|
public double Increment
|
|
|
|
|
{
|
2025-12-21 13:26:56 -05:00
|
|
|
get => (double)GetValue(IncrementProperty);
|
|
|
|
|
set => SetValue(IncrementProperty, Math.Max(0.001, value));
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the button background color.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Color ButtonBackgroundColor
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
get => (Color)GetValue(ButtonBackgroundColorProperty);
|
2025-12-21 13:26:56 -05:00
|
|
|
set => SetValue(ButtonBackgroundColorProperty, value);
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the button pressed color.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Color ButtonPressedColor
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
get => (Color)GetValue(ButtonPressedColorProperty);
|
2025-12-21 13:26:56 -05:00
|
|
|
set => SetValue(ButtonPressedColorProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the button disabled color.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Color ButtonDisabledColor
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
get => (Color)GetValue(ButtonDisabledColorProperty);
|
2025-12-21 13:26:56 -05:00
|
|
|
set => SetValue(ButtonDisabledColorProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the border color.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Color BorderColor
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
get => (Color)GetValue(BorderColorProperty);
|
2025-12-21 13:26:56 -05:00
|
|
|
set => SetValue(BorderColorProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the symbol color.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Color SymbolColor
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
get => (Color)GetValue(SymbolColorProperty);
|
2025-12-21 13:26:56 -05:00
|
|
|
set => SetValue(SymbolColorProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the symbol disabled color.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Color SymbolDisabledColor
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
get => (Color)GetValue(SymbolDisabledColorProperty);
|
2025-12-21 13:26:56 -05:00
|
|
|
set => SetValue(SymbolDisabledColorProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the corner radius.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double CornerRadius
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
get => (double)GetValue(CornerRadiusProperty);
|
2025-12-21 13:26:56 -05:00
|
|
|
set => SetValue(CornerRadiusProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the button width.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double ButtonWidth
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
get => (double)GetValue(ButtonWidthProperty);
|
2025-12-21 13:26:56 -05:00
|
|
|
set => SetValue(ButtonWidthProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets whether the minus button is currently pressed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsMinusPressed { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets whether the plus button is currently pressed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsPlusPressed { get; private set; }
|
|
|
|
|
|
2025-12-21 13:26:56 -05:00
|
|
|
#endregion
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
#region Events
|
2025-12-21 13:26:56 -05:00
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Event raised when the value changes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<ValueChangedEventArgs>? ValueChanged;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructor
|
2025-12-19 09:30:16 +00:00
|
|
|
|
|
|
|
|
public SkiaStepper()
|
|
|
|
|
{
|
|
|
|
|
IsFocusable = true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Event Handlers
|
|
|
|
|
|
2025-12-21 13:26:56 -05:00
|
|
|
private void OnValuePropertyChanged(double oldValue, double newValue)
|
|
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
ValueChanged?.Invoke(this, new ValueChangedEventArgs(oldValue, newValue));
|
2025-12-21 13:26:56 -05:00
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnRangeChanged()
|
|
|
|
|
{
|
|
|
|
|
var clamped = Math.Clamp(Value, Minimum, Maximum);
|
2026-01-16 05:01:27 +00:00
|
|
|
if (Math.Abs(Value - clamped) > double.Epsilon)
|
2025-12-21 13:26:56 -05:00
|
|
|
{
|
|
|
|
|
Value = clamped;
|
|
|
|
|
}
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Rendering
|
|
|
|
|
|
2025-12-19 09:30:16 +00:00
|
|
|
protected override void OnDraw(SKCanvas canvas, SKRect bounds)
|
|
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
var buttonWidth = (float)ButtonWidth;
|
|
|
|
|
var cornerRadius = (float)CornerRadius;
|
|
|
|
|
|
|
|
|
|
var minusRect = new SKRect(bounds.Left, bounds.Top, bounds.Left + buttonWidth, bounds.Bottom);
|
|
|
|
|
var plusRect = new SKRect(bounds.Right - buttonWidth, bounds.Top, bounds.Right, bounds.Bottom);
|
|
|
|
|
|
|
|
|
|
// Get colors
|
|
|
|
|
var buttonBgColorSK = ToSKColor(ButtonBackgroundColor);
|
|
|
|
|
var buttonPressedColorSK = ToSKColor(ButtonPressedColor);
|
|
|
|
|
var buttonDisabledColorSK = ToSKColor(ButtonDisabledColor);
|
|
|
|
|
var borderColorSK = ToSKColor(BorderColor);
|
|
|
|
|
var symbolColorSK = ToSKColor(SymbolColor);
|
|
|
|
|
var symbolDisabledColorSK = ToSKColor(SymbolDisabledColor);
|
|
|
|
|
|
|
|
|
|
// Draw focus ring
|
|
|
|
|
if (IsFocused)
|
|
|
|
|
{
|
|
|
|
|
using var focusPaint = new SKPaint
|
|
|
|
|
{
|
|
|
|
|
Color = ToSKColor(Color.FromRgb(0x21, 0x96, 0xF3)).WithAlpha(60),
|
|
|
|
|
Style = SKPaintStyle.Stroke,
|
|
|
|
|
StrokeWidth = 3,
|
|
|
|
|
IsAntialias = true
|
|
|
|
|
};
|
|
|
|
|
var focusRect = new SKRect(bounds.Left - 2, bounds.Top - 2, bounds.Right + 2, bounds.Bottom + 2);
|
|
|
|
|
canvas.DrawRoundRect(new SKRoundRect(focusRect, cornerRadius + 2), focusPaint);
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
DrawButton(canvas, minusRect, "-", IsMinusPressed, !CanDecrement(),
|
|
|
|
|
buttonBgColorSK, buttonPressedColorSK, buttonDisabledColorSK,
|
|
|
|
|
symbolColorSK, symbolDisabledColorSK, cornerRadius, true);
|
2025-12-19 09:30:16 +00:00
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
DrawButton(canvas, plusRect, "+", IsPlusPressed, !CanIncrement(),
|
|
|
|
|
buttonBgColorSK, buttonPressedColorSK, buttonDisabledColorSK,
|
|
|
|
|
symbolColorSK, symbolDisabledColorSK, cornerRadius, false);
|
|
|
|
|
|
|
|
|
|
// Draw border
|
2025-12-19 09:30:16 +00:00
|
|
|
using var borderPaint = new SKPaint
|
|
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
Color = borderColorSK,
|
2025-12-19 09:30:16 +00:00
|
|
|
Style = SKPaintStyle.Stroke,
|
|
|
|
|
StrokeWidth = 1,
|
|
|
|
|
IsAntialias = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var totalRect = new SKRect(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom);
|
2026-01-16 05:01:27 +00:00
|
|
|
canvas.DrawRoundRect(new SKRoundRect(totalRect, cornerRadius), borderPaint);
|
2025-12-19 09:30:16 +00:00
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
// Draw center divider
|
2025-12-19 09:30:16 +00:00
|
|
|
var centerX = bounds.MidX;
|
|
|
|
|
canvas.DrawLine(centerX, bounds.Top, centerX, bounds.Bottom, borderPaint);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
private void DrawButton(SKCanvas canvas, SKRect rect, string symbol, bool isPressed, bool isDisabled,
|
|
|
|
|
SKColor bgColor, SKColor pressedColor, SKColor disabledColor,
|
|
|
|
|
SKColor symbolColor, SKColor symbolDisabledColor, float cornerRadius, bool isLeft)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
// Draw background with rounded corners on the appropriate side
|
2025-12-19 09:30:16 +00:00
|
|
|
using var bgPaint = new SKPaint
|
|
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
Color = isDisabled ? disabledColor : (isPressed ? pressedColor : bgColor),
|
2025-12-19 09:30:16 +00:00
|
|
|
Style = SKPaintStyle.Fill,
|
|
|
|
|
IsAntialias = true
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
// Create path for rounded corners on one side only
|
|
|
|
|
using var path = new SKPath();
|
|
|
|
|
if (isLeft)
|
|
|
|
|
{
|
|
|
|
|
path.MoveTo(rect.Left + cornerRadius, rect.Top);
|
|
|
|
|
path.LineTo(rect.Right, rect.Top);
|
|
|
|
|
path.LineTo(rect.Right, rect.Bottom);
|
|
|
|
|
path.LineTo(rect.Left + cornerRadius, rect.Bottom);
|
|
|
|
|
path.ArcTo(new SKRect(rect.Left, rect.Bottom - cornerRadius * 2, rect.Left + cornerRadius * 2, rect.Bottom), 90, 90, false);
|
|
|
|
|
path.LineTo(rect.Left, rect.Top + cornerRadius);
|
|
|
|
|
path.ArcTo(new SKRect(rect.Left, rect.Top, rect.Left + cornerRadius * 2, rect.Top + cornerRadius * 2), 180, 90, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
path.MoveTo(rect.Left, rect.Top);
|
|
|
|
|
path.LineTo(rect.Right - cornerRadius, rect.Top);
|
|
|
|
|
path.ArcTo(new SKRect(rect.Right - cornerRadius * 2, rect.Top, rect.Right, rect.Top + cornerRadius * 2), 270, 90, false);
|
|
|
|
|
path.LineTo(rect.Right, rect.Bottom - cornerRadius);
|
|
|
|
|
path.ArcTo(new SKRect(rect.Right - cornerRadius * 2, rect.Bottom - cornerRadius * 2, rect.Right, rect.Bottom), 0, 90, false);
|
|
|
|
|
path.LineTo(rect.Left, rect.Bottom);
|
|
|
|
|
}
|
|
|
|
|
path.Close();
|
|
|
|
|
canvas.DrawPath(path, bgPaint);
|
|
|
|
|
|
|
|
|
|
// Draw symbol
|
2025-12-19 09:30:16 +00:00
|
|
|
using var font = new SKFont(SKTypeface.Default, 20);
|
|
|
|
|
using var textPaint = new SKPaint(font)
|
|
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
Color = isDisabled ? symbolDisabledColor : symbolColor,
|
2025-12-19 09:30:16 +00:00
|
|
|
IsAntialias = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var textBounds = new SKRect();
|
|
|
|
|
textPaint.MeasureText(symbol, ref textBounds);
|
|
|
|
|
canvas.DrawText(symbol, rect.MidX - textBounds.MidX, rect.MidY - textBounds.MidY, textPaint);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Helper Methods
|
|
|
|
|
|
2025-12-21 13:26:56 -05:00
|
|
|
private bool CanIncrement() => IsEnabled && Value < Maximum;
|
|
|
|
|
private bool CanDecrement() => IsEnabled && Value > Minimum;
|
2025-12-19 09:30:16 +00:00
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Pointer Events
|
|
|
|
|
|
2025-12-19 09:30:16 +00:00
|
|
|
public override void OnPointerPressed(PointerEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!IsEnabled) return;
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
var buttonWidth = (float)ButtonWidth;
|
|
|
|
|
|
|
|
|
|
if (e.X < buttonWidth)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
IsMinusPressed = true;
|
2025-12-21 13:26:56 -05:00
|
|
|
if (CanDecrement()) Value -= Increment;
|
2026-01-16 05:01:27 +00:00
|
|
|
SkiaVisualStateManager.GoToState(this, SkiaVisualStateManager.CommonStates.Pressed);
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
2026-01-16 05:01:27 +00:00
|
|
|
else if (e.X > Bounds.Width - buttonWidth)
|
2025-12-19 09:30:16 +00:00
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
IsPlusPressed = true;
|
2025-12-21 13:26:56 -05:00
|
|
|
if (CanIncrement()) Value += Increment;
|
2026-01-16 05:01:27 +00:00
|
|
|
SkiaVisualStateManager.GoToState(this, SkiaVisualStateManager.CommonStates.Pressed);
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
2026-01-16 05:01:27 +00:00
|
|
|
|
|
|
|
|
e.Handled = true;
|
2025-12-19 09:30:16 +00:00
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnPointerReleased(PointerEventArgs e)
|
|
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
if (IsMinusPressed || IsPlusPressed)
|
|
|
|
|
{
|
|
|
|
|
IsMinusPressed = false;
|
|
|
|
|
IsPlusPressed = false;
|
|
|
|
|
SkiaVisualStateManager.GoToState(this, IsEnabled
|
|
|
|
|
? SkiaVisualStateManager.CommonStates.Normal
|
|
|
|
|
: SkiaVisualStateManager.CommonStates.Disabled);
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
public override void OnPointerEntered(PointerEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (IsEnabled)
|
|
|
|
|
{
|
|
|
|
|
SkiaVisualStateManager.GoToState(this, SkiaVisualStateManager.CommonStates.PointerOver);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnPointerExited(PointerEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SkiaVisualStateManager.GoToState(this, IsEnabled
|
|
|
|
|
? SkiaVisualStateManager.CommonStates.Normal
|
|
|
|
|
: SkiaVisualStateManager.CommonStates.Disabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Keyboard Events
|
|
|
|
|
|
2025-12-19 09:30:16 +00:00
|
|
|
public override void OnKeyDown(KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!IsEnabled) return;
|
|
|
|
|
|
|
|
|
|
switch (e.Key)
|
|
|
|
|
{
|
|
|
|
|
case Key.Up:
|
|
|
|
|
case Key.Right:
|
2025-12-21 13:26:56 -05:00
|
|
|
if (CanIncrement()) Value += Increment;
|
2025-12-19 09:30:16 +00:00
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
case Key.Down:
|
|
|
|
|
case Key.Left:
|
2025-12-21 13:26:56 -05:00
|
|
|
if (CanDecrement()) Value -= Increment;
|
2025-12-19 09:30:16 +00:00
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
2026-01-16 05:01:27 +00:00
|
|
|
case Key.PageUp:
|
|
|
|
|
if (CanIncrement()) Value += Increment * 10;
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
case Key.PageDown:
|
|
|
|
|
if (CanDecrement()) Value -= Increment * 10;
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
case Key.Home:
|
|
|
|
|
Value = Minimum;
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
case Key.End:
|
|
|
|
|
Value = Maximum;
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 05:01:27 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Lifecycle
|
|
|
|
|
|
|
|
|
|
protected override void OnEnabledChanged()
|
|
|
|
|
{
|
|
|
|
|
base.OnEnabledChanged();
|
|
|
|
|
SkiaVisualStateManager.GoToState(this, IsEnabled
|
|
|
|
|
? SkiaVisualStateManager.CommonStates.Normal
|
|
|
|
|
: SkiaVisualStateManager.CommonStates.Disabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Layout
|
|
|
|
|
|
2025-12-19 09:30:16 +00:00
|
|
|
protected override SKSize MeasureOverride(SKSize availableSize)
|
|
|
|
|
{
|
2026-01-16 05:01:27 +00:00
|
|
|
var buttonWidth = (float)ButtonWidth;
|
|
|
|
|
return new SKSize(buttonWidth * 2 + 1, 32);
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|
2026-01-16 05:01:27 +00:00
|
|
|
|
|
|
|
|
#endregion
|
2025-12-19 09:30:16 +00:00
|
|
|
}
|