Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TumbShape for Slider #16

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/AlohaKit/Controls/Slider/Slider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ public Brush ThumbBrush
set => SetValue(ThumbBrushProperty, value);
}

public static readonly BindableProperty ThumbShapeProperty =
BindableProperty.Create(nameof(ThumbShape), typeof(ThumbShape), typeof(Slider), ThumbShape.Circle,
propertyChanged: (bindableObject, oldValue, newValue) =>
{
if (newValue != null && bindableObject is Slider slider)
{
slider.UpdateThumbShape();
}
});

public ThumbShape ThumbShape
{
get => (ThumbShape)GetValue(ThumbShapeProperty);
set => SetValue(ThumbShapeProperty, value);
}

public event EventHandler<ValueChangedEventArgs> ValueChanged;

protected override void OnParentSet()
Expand All @@ -143,6 +159,7 @@ protected override void OnParentSet()
UpdateMinimumBrush();
UpdateMaximumBrush();
UpdateThumbBrush();
UpdateThumbShape();
}
}

Expand Down Expand Up @@ -216,6 +233,16 @@ void UpdateThumbBrush()
Invalidate();
}

private void UpdateThumbShape()
{
if (SliderDrawable == null)
return;

SliderDrawable.ThumbShape = ThumbShape;

Invalidate();
}

void OnSliderStartInteraction(object sender, TouchEventArgs args)
{
var touchPoint = args.Touches[0];
Expand Down
19 changes: 16 additions & 3 deletions src/AlohaKit/Controls/Slider/SliderDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ namespace AlohaKit.Controls
{
public class SliderDrawable : IDrawable
{
const int cornerRadius = 5;
public Paint BackgroundPaint { get; set; }
public double Minimum { get; set; }
public double Maximum { get; set; }
public double Value { get; set; }
public Paint MinimumPaint { get; set; }
public Paint MaximumPaint { get; set; }
public Paint ThumbPaint { get; set; }
public ThumbShape ThumbShape { get; set; }

public void Draw(ICanvas canvas, RectF dirtyRect)
public void Draw(ICanvas canvas, RectF dirtyRect)
{
DrawBackground(canvas, dirtyRect);
DrawTrackBackground(canvas, dirtyRect);
Expand Down Expand Up @@ -96,9 +98,20 @@ public virtual void DrawThumb(ICanvas canvas, RectF dirtyRect)
if (ThumbPaint != null)
canvas.SetFillPaint(ThumbPaint, dirtyRect);

canvas.FillEllipse(x, y, ThumbSize, ThumbSize);
switch (ThumbShape)
{
case ThumbShape.Circle:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be nice to include a Path, and allow to create any shape too.

canvas.FillEllipse(x, y, ThumbSize, ThumbSize);
break;
case ThumbShape.Rectangle:
canvas.FillRectangle(x, y, ThumbSize, ThumbSize);
break;
case ThumbShape.RoundedRectangle:
canvas.FillRoundedRectangle(x, y, ThumbSize, ThumbSize, cornerRadius);
break;
}

canvas.RestoreState();
}
}
}
}
8 changes: 8 additions & 0 deletions src/AlohaKit/Controls/Slider/ThumbShape.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AlohaKit.Controls;

public enum ThumbShape
{
Circle,
Rectangle,
RoundedRectangle
}