Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz committed May 22, 2022
2 parents f3ea35a + 969634d commit 5686a92
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
16 changes: 14 additions & 2 deletions src/AlohaKit.Gallery/Views/ProgressRadialView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
<Setter Property="ThumbColor" Value="{AppThemeBinding Light={StaticResource LightAccentColor}, Dark={StaticResource DarkAccentColor}}" />
<Setter Property="Margin" Value="0, 0, 12, 0" />
</Style>

<Style x:Key="SettingsTextStyle" TargetType="Label">
<Setter Property="FontSize" Value="9" />
</Style>

<Style x:Key="SettingsEntryStyle" TargetType="Entry">
<Setter Property="VerticalOptions" Value="Center" />
<Setter Property="WidthRequest" Value="80" />
Expand Down Expand Up @@ -124,11 +124,23 @@
TextChanged="OnTextColorEntryTextChanged"
Style="{StaticResource SettingsEntryStyle}"/>
</StackLayout>
<StackLayout
Orientation="Horizontal"
Margin="0, 6">
<Label
Text="LeftToRight"
VerticalOptions="Center"
Style="{StaticResource SettingsTextStyle}"/>
<controls:CheckBox
x:Name="checkBox"
StrokeThickness="3"/>
</StackLayout>
</StackLayout>
<controls:ProgressRadial
Grid.Row="3"
x:Name="ProgressRadial"
Maximum="10"
Direction="RightToLeft"
Value="{Binding Source={x:Reference ValueSlider}, Path=Value}"
HorizontalOptions="Center"
VerticalOptions="Center"/>
Expand Down
16 changes: 12 additions & 4 deletions src/AlohaKit.Gallery/Views/ProgressRadialView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using AlohaKit.Controls;

namespace AlohaKit.Gallery;

public partial class ProgressRadialView : ContentPage
{
public ProgressRadialView()
{
InitializeComponent();

public ProgressRadialView()
{
InitializeComponent();
UpdateColors();
checkBox.CheckedChanged += CheckBox_CheckedChanged;
checkBox.IsChecked = ProgressRadial.Direction == ProgressRadialDirection.LeftToRight ? true : false;
}

private void CheckBox_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
ProgressRadial.Direction = e.Value ? ProgressRadialDirection.LeftToRight : ProgressRadialDirection.RightToLeft;
}

void OnBackgroundColorEntryTextChanged(object sender, TextChangedEventArgs e)
Expand Down
38 changes: 32 additions & 6 deletions src/AlohaKit/Controls/ProgressRadial/ProgressRadial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public ProgressRadial()
{
HeightRequest = 150;
WidthRequest = 150;

Drawable = ProgressRadialDrawable = new ProgressRadialDrawable();
}

Expand Down Expand Up @@ -45,7 +45,7 @@ public Color ProgressColor
set => SetValue(ProgressColorProperty, value);
}

public static readonly BindableProperty TextColorProperty =
public static readonly BindableProperty TextColorProperty =
BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(ProgressRadial), Colors.Black, BindingMode.TwoWay,
propertyChanged: (bindableObject, oldValue, newValue) =>
{
Expand Down Expand Up @@ -77,6 +77,22 @@ public double FontSize
set => SetValue(FontSizeProperty, value);
}

public static readonly BindableProperty DirectionProperty =
BindableProperty.Create(nameof(Direction), typeof(ProgressRadialDirection), typeof(ProgressRadial), ProgressRadialDirection.RightToLeft,
propertyChanged: (bindableObject, oldValue, newValue) =>
{
if (newValue != null && bindableObject is ProgressRadial progressRadial)
{
progressRadial.UpdateDirection();
}
});

public ProgressRadialDirection Direction
{
get => (ProgressRadialDirection)GetValue(DirectionProperty);
set => SetValue(DirectionProperty, value);
}

public static readonly BindableProperty MinimumProperty =
BindableProperty.Create(nameof(Minimum), typeof(int), typeof(ProgressRadial), 0);

Expand All @@ -95,8 +111,8 @@ public int Maximum
set => SetValue(MaximumProperty, value);
}

public static readonly BindableProperty ValueProperty =
BindableProperty.Create(nameof(Value), typeof(int), typeof(ProgressRadial), 0, BindingMode.TwoWay,
public static readonly BindableProperty ValueProperty =
BindableProperty.Create(nameof(Value), typeof(int), typeof(ProgressRadial), 0, BindingMode.TwoWay,
propertyChanged: (bindableObject, oldValue, newValue) =>
{
if (newValue != null && bindableObject is ProgressRadial progressRadial)
Expand All @@ -111,7 +127,7 @@ public int Value
get => (int)GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}

public event EventHandler<ValueChangedEventArgs> ValueChanged;

protected override void OnParentChanged()
Expand All @@ -121,10 +137,11 @@ protected override void OnParentChanged()
if (Parent != null)
{
UpdateBackgroundColor();
UpdateStrokeColor();
UpdateStrokeColor();
UpdateProgressColor();
UpdateTextColor();
UpdateFontSize();
UpdateDirection();
UpdateValue();
}
}
Expand Down Expand Up @@ -174,6 +191,15 @@ void UpdateFontSize()
Invalidate();
}

void UpdateDirection()
{
if (ProgressRadialDrawable == null)
return;

ProgressRadialDrawable.Direction = Direction;
Invalidate();
}

void UpdateValue()
{
if (ProgressRadialDrawable == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AlohaKit.Controls
{
public enum ProgressRadialDirection
{
LeftToRight,
RightToLeft
}
}
10 changes: 8 additions & 2 deletions src/AlohaKit/Controls/ProgressRadial/ProgressRadialDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ProgressRadialDrawable : IDrawable

public string ProgressText { get; set; }
public float ProgressAngle { get; set; }
public ProgressRadialDirection Direction { get; set; }

public void Draw(ICanvas canvas, RectF dirtyRect)
{
Expand Down Expand Up @@ -73,7 +74,10 @@ public virtual void DrawProgress(ICanvas canvas, RectF dirtyRect)
var rY = dirtyRect.Height / 2;

// Rotate the canvas
canvas.Rotate((float)45.0f, rX, rY);
var degrees = Direction == ProgressRadialDirection.RightToLeft ? (float)45.0f : (float)135.0f;
rX = Direction == ProgressRadialDirection.RightToLeft ? rX : rX - (float)2.4f;
rY = Direction == ProgressRadialDirection.RightToLeft ? rY : rY - (float)6f;
canvas.Rotate(degrees, rX, rY);

canvas.StrokeColor = ProgressColor;
canvas.StrokeLineJoin = LineJoin.Round;
Expand All @@ -87,7 +91,9 @@ public virtual void DrawProgress(ICanvas canvas, RectF dirtyRect)
};

PathF progressCurrentPath = new PathF();
progressCurrentPath.AddArc(progressRect.X, progressRect.Y, progressRect.Width, progressRect.Height, 0, ProgressAngle, false);
var startAngle = Direction == ProgressRadialDirection.RightToLeft ? (float)0.0f : (float)ProgressAngle * -1;
var endAngle = Direction == ProgressRadialDirection.RightToLeft ? (float)ProgressAngle : (float)0.0f;
progressCurrentPath.AddArc(progressRect.X, progressRect.Y, progressRect.Width, progressRect.Height, startAngle, endAngle, false);

// Draw the progress arc
canvas.DrawPath(progressCurrentPath);
Expand Down

0 comments on commit 5686a92

Please sign in to comment.