Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ProgressBar.cs
1using System;
2using System.ComponentModel;
3using Microsoft.Maui.Controls;
4using Microsoft.Maui.Graphics;
5using SkiaSharp;
6using SkiaSharp.Views.Maui;
7using SkiaSharp.Views.Maui.Controls;
8
10{
15 public class ProgressBar : SKCanvasView
16 {
20 public static readonly BindableProperty ProgressProperty =
21 BindableProperty.Create(
22 nameof(Progress),
23 typeof(double),
24 typeof(ProgressBar),
25 0.0,
26 propertyChanged: (Bindable, OldValue, NewValue) => ((ProgressBar)Bindable).InvalidateSurface());
27
31 public double Progress
32 {
33 get => (double)this.GetValue(ProgressProperty);
34 set => this.SetValue(ProgressProperty, value);
35 }
36
40 public static readonly BindableProperty BarBrushProperty =
41 BindableProperty.Create(
42 nameof(BarBrush),
43 typeof(Brush),
44 typeof(ProgressBar),
45 default(Brush),
46 propertyChanged: (Bindable, OldValue, NewValue) => ((ProgressBar)Bindable).InvalidateSurface());
47
52 [TypeConverter(typeof(BrushTypeConverter))]
53 public Brush BarBrush
54 {
55 get => (Brush)this.GetValue(BarBrushProperty);
56 set => this.SetValue(BarBrushProperty, value);
57 }
58
62 public static readonly BindableProperty TrackBrushProperty =
63 BindableProperty.Create(
64 nameof(TrackBrush),
65 typeof(Brush),
66 typeof(ProgressBar),
67 default(Brush),
68 propertyChanged: (Bindable, OldValue, NewValue) => ((ProgressBar)Bindable).InvalidateSurface());
69
74 [TypeConverter(typeof(BrushTypeConverter))]
75 public Brush TrackBrush
76 {
77 get => (Brush)this.GetValue(TrackBrushProperty);
78 set => this.SetValue(TrackBrushProperty, value);
79 }
80
84 public static readonly BindableProperty CornerRadiusProperty =
85 BindableProperty.Create(
86 nameof(CornerRadius),
87 typeof(float),
88 typeof(ProgressBar),
89 4f,
90 propertyChanged: (Bindable, OldValue, NewValue) => ((ProgressBar)Bindable).InvalidateSurface());
91
95 public float CornerRadius
96 {
97 get => (float)this.GetValue(CornerRadiusProperty);
98 set => this.SetValue(CornerRadiusProperty, value);
99 }
100
104 public ProgressBar()
105 {
106 this.HeightRequest = 10;
107 this.Progress = 0;
108 this.CornerRadius = 4f;
109 this.IgnorePixelScaling = false;
110 this.EnableTouchEvents = false;
111 this.PaintSurface += this.OnPaintSurface;
112 }
113
119 private void OnPaintSurface(object? Sender, SKPaintSurfaceEventArgs E)
120 {
121 SKCanvas Canvas = E.Surface.Canvas;
122 Canvas.Clear();
123
124 float Width = E.Info.Width;
125 float Height = E.Info.Height;
126 float Radius = this.CornerRadius;
127
128 // Draw the background (track)
129 using (SKPaint TrackPaint = new()
130 { IsAntialias = true })
131 {
132 TrackPaint.Shader = ProgressBar.ToShader(this.TrackBrush, Width, Height)
133 ?? SKShader.CreateColor(SKColors.LightGray);
134
135 SKRoundRect TrackRect = new(new SKRect(0, 0, Width, Height), Radius, Radius);
136 Canvas.DrawRoundRect(TrackRect, TrackPaint);
137 }
138
139 // Draw the progress bar (fill)
140 float ProgressWidth = (float)Math.Max(0, Math.Min(1, this.Progress)) * Width;
141 if (ProgressWidth > 0.1f)
142 {
143 using SKPaint BarPaint = new() { IsAntialias = true };
144 BarPaint.Shader = ProgressBar.ToShader(this.BarBrush, ProgressWidth, Height)
145 ?? SKShader.CreateColor(SKColors.Blue);
146
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);
149 }
150 }
151
159 private static SKShader? ToShader(Brush Brush, float Width, float Height)
160 {
161 if (Brush is null)
162 return null;
163
164 if (Brush is SolidColorBrush SolidBrush)
165 return SKShader.CreateColor(SolidBrush.Color.ToSKColor());
166
167 if (Brush is LinearGradientBrush LinearBrush)
168 {
169 GradientStopCollection Stops = LinearBrush.GradientStops;
170 SKColor[] Colors = new SKColor[Stops.Count];
171 float[] Positions = new float[Stops.Count];
172
173 for (int i = 0; i < Stops.Count; i++)
174 {
175 Colors[i] = Stops[i].Color.ToSKColor();
176 Positions[i] = Stops[i].Offset;
177 }
178
179 SKPoint StartPoint = new(
180 (float)(LinearBrush.StartPoint.X * Width),
181 (float)(LinearBrush.StartPoint.Y * Height));
182
183 SKPoint EndPoint = new(
184 (float)(LinearBrush.EndPoint.X * Width),
185 (float)(LinearBrush.EndPoint.Y * Height));
186
187 return SKShader.CreateLinearGradient(StartPoint, EndPoint, Colors, Positions, SKShaderTileMode.Clamp);
188 }
189
190 if (Brush is RadialGradientBrush RadialBrush)
191 {
192 GradientStopCollection Stops = RadialBrush.GradientStops;
193 SKColor[] Colors = new SKColor[Stops.Count];
194 float[] Positions = new float[Stops.Count];
195
196 for (int i = 0; i < Stops.Count; i++)
197 {
198 Colors[i] = Stops[i].Color.ToSKColor();
199 Positions[i] = Stops[i].Offset;
200 }
201
202 SKPoint CenterPoint = new(
203 (float)(RadialBrush.Center.X * Width),
204 (float)(RadialBrush.Center.Y * Height));
205
206 float Radius = Math.Max(Width, Height) * 0.5f;
207 return SKShader.CreateRadialGradient(CenterPoint, Radius, Colors, Positions, SKShaderTileMode.Clamp);
208 }
209
210 // Fallback: solid black (should never hit in normal usage)
211 return SKShader.CreateColor(SKColors.Black);
212 }
213 }
214}
A cross-platform, highly customizable progress bar rendered with SkiaSharp for pixel-perfect appearan...
Definition: ProgressBar.cs:16
static readonly BindableProperty ProgressProperty
Identifies the Progress bindable property.
Definition: ProgressBar.cs:20
float CornerRadius
Gets or sets the corner radius for the progress bar in device-independent pixels.
Definition: ProgressBar.cs:96
Brush TrackBrush
Gets or sets the brush used to paint the background (track) of the bar. Supports solid color,...
Definition: ProgressBar.cs:76
ProgressBar()
Initializes a new instance of the ProgressBar class.
Definition: ProgressBar.cs:104
double Progress
Gets or sets the progress, as a value between 0.0 and 1.0.
Definition: ProgressBar.cs:32
static readonly BindableProperty BarBrushProperty
Identifies the BarBrush bindable property.
Definition: ProgressBar.cs:40
Brush BarBrush
Gets or sets the brush used to paint the progress portion of the bar. Supports solid color,...
Definition: ProgressBar.cs:54
static readonly BindableProperty CornerRadiusProperty
Identifies the CornerRadius bindable property.
Definition: ProgressBar.cs:84
static readonly BindableProperty TrackBrushProperty
Identifies the TrackBrush bindable property.
Definition: ProgressBar.cs:62