2using System.ComponentModel;
3using Microsoft.Maui.Controls;
4using Microsoft.Maui.Graphics;
6using SkiaSharp.Views.Maui;
7using SkiaSharp.Views.Maui.Controls;
21 BindableProperty.Create(
26 propertyChanged: (Bindable, OldValue, NewValue) => ((
ProgressBar)Bindable).InvalidateSurface());
41 BindableProperty.Create(
46 propertyChanged: (Bindable, OldValue, NewValue) => ((
ProgressBar)Bindable).InvalidateSurface());
52 [TypeConverter(typeof(BrushTypeConverter))]
63 BindableProperty.Create(
68 propertyChanged: (Bindable, OldValue, NewValue) => ((
ProgressBar)Bindable).InvalidateSurface());
74 [TypeConverter(typeof(BrushTypeConverter))]
85 BindableProperty.Create(
90 propertyChanged: (Bindable, OldValue, NewValue) => ((
ProgressBar)Bindable).InvalidateSurface());
106 this.HeightRequest = 10;
108 this.CornerRadius = 4f;
109 this.IgnorePixelScaling =
false;
110 this.EnableTouchEvents =
false;
111 this.PaintSurface += this.OnPaintSurface;
119 private void OnPaintSurface(
object? Sender, SKPaintSurfaceEventArgs E)
121 SKCanvas Canvas = E.Surface.Canvas;
124 float Width = E.Info.Width;
125 float Height = E.Info.Height;
129 using (SKPaint TrackPaint =
new()
130 { IsAntialias =
true })
133 ?? SKShader.CreateColor(SKColors.LightGray);
135 SKRoundRect TrackRect =
new(
new SKRect(0, 0, Width, Height), Radius, Radius);
136 Canvas.DrawRoundRect(TrackRect, TrackPaint);
140 float ProgressWidth = (float)Math.Max(0, Math.Min(1,
this.Progress)) * Width;
141 if (ProgressWidth > 0.1f)
143 using SKPaint BarPaint =
new() { IsAntialias =
true };
145 ?? SKShader.CreateColor(SKColors.Blue);
147 SKRoundRect BarRect =
new(
new SKRect(Height/3, Height/3, ProgressWidth - Height/3, 2*Height/3), Radius/2, Radius/2);
148 Canvas.DrawRoundRect(BarRect, BarPaint);
159 private static SKShader? ToShader(Brush Brush,
float Width,
float Height)
164 if (Brush is SolidColorBrush SolidBrush)
165 return SKShader.CreateColor(SolidBrush.Color.ToSKColor());
167 if (Brush is LinearGradientBrush LinearBrush)
169 GradientStopCollection Stops = LinearBrush.GradientStops;
170 SKColor[] Colors =
new SKColor[Stops.Count];
171 float[] Positions =
new float[Stops.Count];
173 for (
int i = 0; i < Stops.Count; i++)
175 Colors[i] = Stops[i].Color.ToSKColor();
176 Positions[i] = Stops[i].Offset;
179 SKPoint StartPoint =
new(
180 (float)(LinearBrush.StartPoint.X * Width),
181 (float)(LinearBrush.StartPoint.Y * Height));
183 SKPoint EndPoint =
new(
184 (float)(LinearBrush.EndPoint.X * Width),
185 (float)(LinearBrush.EndPoint.Y * Height));
187 return SKShader.CreateLinearGradient(StartPoint, EndPoint, Colors, Positions, SKShaderTileMode.Clamp);
190 if (Brush is RadialGradientBrush RadialBrush)
192 GradientStopCollection Stops = RadialBrush.GradientStops;
193 SKColor[] Colors =
new SKColor[Stops.Count];
194 float[] Positions =
new float[Stops.Count];
196 for (
int i = 0; i < Stops.Count; i++)
198 Colors[i] = Stops[i].Color.ToSKColor();
199 Positions[i] = Stops[i].Offset;
202 SKPoint CenterPoint =
new(
203 (float)(RadialBrush.Center.X * Width),
204 (float)(RadialBrush.Center.Y * Height));
206 float Radius = Math.Max(Width, Height) * 0.5f;
207 return SKShader.CreateRadialGradient(CenterPoint, Radius, Colors, Positions, SKShaderTileMode.Clamp);
211 return SKShader.CreateColor(SKColors.Black);
A cross-platform, highly customizable progress bar rendered with SkiaSharp for pixel-perfect appearan...
static readonly BindableProperty ProgressProperty
Identifies the Progress bindable property.
float CornerRadius
Gets or sets the corner radius for the progress bar in device-independent pixels.
Brush TrackBrush
Gets or sets the brush used to paint the background (track) of the bar. Supports solid color,...
ProgressBar()
Initializes a new instance of the ProgressBar class.
double Progress
Gets or sets the progress, as a value between 0.0 and 1.0.
static readonly BindableProperty BarBrushProperty
Identifies the BarBrush bindable property.
Brush BarBrush
Gets or sets the brush used to paint the progress portion of the bar. Supports solid color,...
static readonly BindableProperty CornerRadiusProperty
Identifies the CornerRadius bindable property.
static readonly BindableProperty TrackBrushProperty
Identifies the TrackBrush bindable property.