Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SvgButton.cs
1using CommunityToolkit.Maui.Markup;
4
6{
7 class SvgButton : TemplatedButton, IDisposable
8 {
9 private readonly SvgView innerSvg;
10 private readonly Label innerLabel;
11 private readonly Border innerBorder;
12 private readonly Grid innerGrid;
13
15 public static readonly BindableProperty SvgSourceProperty = BindableProperty.Create(
16 nameof(SvgSource),
17 typeof(string),
18 typeof(SvgButton),
19 propertyChanged: OnSvgSourceChanged);
20
22 public static readonly BindableProperty SvgAspectProperty = BindableProperty.Create(
23 nameof(SvgAspect),
24 typeof(Aspect),
25 typeof(SvgButton),
26 propertyChanged: OnSvgAspectChanged);
27
29 public static readonly BindableProperty SvgTintColorProperty = BindableProperty.Create(
30 nameof(SvgTintColor),
31 typeof(Color),
32 typeof(SvgButton),
33 propertyChanged: OnSvgTintColorChanged);
34
36 public static readonly BindableProperty SvgStyleProperty = BindableProperty.Create(
37 nameof(SvgStyle),
38 typeof(Style),
39 typeof(SvgButton),
40 propertyChanged: OnSvgStyleChanged);
41
42 public static readonly BindableProperty LabelTextProperty = BindableProperty.Create(
43 nameof(LabelText),
44 typeof(string),
45 typeof(SvgButton),
46 propertyChanged: OnLabelTextChanged);
47
48 public static readonly BindableProperty LabelStyleProperty = BindableProperty.Create(
49 nameof(LabelStyle),
50 typeof(Style),
51 typeof(SvgButton),
52 propertyChanged: OnLabelStyleChanged);
53
55 public static readonly BindableProperty LabelPositionProperty = BindableProperty.Create(
56 nameof(LabelPosition),
57 typeof(LabelPosition),
58 typeof(SvgButton),
59 defaultValue: LabelPosition.Left,
60 propertyChanged: OnLabelPositionChanged);
61
63 public static readonly BindableProperty BorderStyleProperty = BindableProperty.Create(
64 nameof(BorderStyle),
65 typeof(Style),
66 typeof(SvgButton),
67 propertyChanged: OnBorderStyleChanged);
68
69 public static void OnSvgSourceChanged(BindableObject Bindable, object OldValue, object NewValue)
70 {
71 ((SvgButton)Bindable).innerSvg.Source = (string)NewValue;
72 }
73
74 public static void OnSvgAspectChanged(BindableObject Bindable, object OldValue, object NewValue)
75 {
76 ((SvgButton)Bindable).innerSvg.Aspect = (Aspect)NewValue;
77 }
78
79 public static void OnSvgTintColorChanged(BindableObject Bindable, object OldValue, object NewValue)
80 {
81 ((SvgButton)Bindable).innerSvg.TintColor = (Color)NewValue;
82 }
83
84 public static void OnSvgStyleChanged(BindableObject Bindable, object OldValue, object NewValue)
85 {
86 ((SvgButton)Bindable).innerSvg.Style = (Style)NewValue;
87 }
88
89 public static void OnLabelTextChanged(BindableObject Bindable, object OldValue, object NewValue)
90 {
91 SvgButton Button = (SvgButton)Bindable;
92 Button.innerLabel.Text = (string)NewValue;
93
94 if (string.IsNullOrEmpty(Button.innerLabel.Text))
95 {
96 Button.innerGrid.RowSpacing = 0;
97 Button.innerGrid.ColumnSpacing = 0;
98 Button.innerLabel.IsVisible = false;
99 }
100 else
101 {
102 Button.innerLabel.VerticalOptions = LayoutOptions.Center;
103 Button.innerLabel.IsVisible = true;
104 switch (Button.LabelPosition)
105 {
106 case LabelPosition.Top:
107 Button.innerLabel.Row(0);
108 Button.innerLabel.Column(0);
109 Button.innerSvg.Row(1);
110 Button.innerSvg.Column(0);
111 Button.innerGrid.ColumnSpacing = 0;
112 Button.innerGrid.RowSpacing = 8;
113 break;
114 case LabelPosition.Bottom:
115 Button.innerLabel.Row(1);
116 Button.innerLabel.Column(0);
117 Button.innerSvg.Row(0);
118 Button.innerSvg.Column(0);
119 Button.innerGrid.ColumnSpacing = 0;
120 Button.innerGrid.RowSpacing = 8;
121 break;
122 case LabelPosition.Left:
123 Button.innerLabel.Column(0);
124 Button.innerLabel.Row(0);
125 Button.innerSvg.Column(1);
126 Button.innerSvg.Row(0);
127 Button.innerGrid.ColumnSpacing = 8;
128 Button.innerGrid.RowSpacing = 0;
129 break;
130 case LabelPosition.Right:
131 Button.innerLabel.Column(1);
132 Button.innerLabel.Row(0);
133 Button.innerSvg.Column(0);
134 Button.innerSvg.Row(0);
135 Button.innerGrid.ColumnSpacing = 8;
136 Button.innerGrid.RowSpacing = 0;
137 break;
138 }
139 }
140 }
141
142 public static void OnLabelStyleChanged(BindableObject Bindable, object OldValue, object NewValue)
143 {
144 ((SvgButton)Bindable).innerLabel.Style = (Style)NewValue;
145 ((SvgButton)Bindable).innerLabel.VerticalOptions = LayoutOptions.Center;
146 ((SvgButton)Bindable).innerLabel.HorizontalOptions = LayoutOptions.Center;
147 }
148
149 public static void OnLabelPositionChanged(BindableObject Bindable, object OldValue, object NewValue)
150 {
151 SvgButton Button = (SvgButton)Bindable;
152
153 LabelPosition NewPosition = (LabelPosition)NewValue;
154 Button.LabelPosition = NewPosition;
155
156 switch (NewPosition)
157 {
158 case LabelPosition.Top:
159 Button.innerLabel.Row(0);
160 Button.innerLabel.Column(0);
161 Button.innerSvg.Row(1);
162 Button.innerSvg.Column(0);
163 Button.innerGrid.ColumnSpacing = 0;
164 Button.innerGrid.RowSpacing = 8;
165 break;
166 case LabelPosition.Bottom:
167 Button.innerLabel.Row(1);
168 Button.innerLabel.Column(0);
169 Button.innerSvg.Row(0);
170 Button.innerSvg.Column(0);
171 Button.innerGrid.ColumnSpacing = 0;
172 Button.innerGrid.RowSpacing = 8;
173 break;
174 case LabelPosition.Left:
175 Button.innerLabel.Column(0);
176 Button.innerLabel.Row(0);
177 Button.innerSvg.Column(1);
178 Button.innerSvg.Row(0);
179 Button.innerGrid.ColumnSpacing = 8;
180 Button.innerGrid.RowSpacing = 0;
181 break;
182 case LabelPosition.Right:
183 Button.innerLabel.Column(1);
184 Button.innerLabel.Row(0);
185 Button.innerSvg.Column(0);
186 Button.innerSvg.Row(0);
187 Button.innerGrid.ColumnSpacing = 8;
188 Button.innerGrid.RowSpacing = 0;
189 break;
190 }
191
192 if (string.IsNullOrEmpty(Button.LabelText))
193 {
194 Button.innerGrid.ColumnSpacing = 0;
195 Button.innerGrid.RowSpacing = 0;
196 }
197 else
198 {
199 Button.innerLabel.VerticalOptions = LayoutOptions.Center;
200 }
201 }
202
203 public static void OnBorderStyleChanged(BindableObject Bindable, object OldValue, object NewValue)
204 {
205 ((SvgButton)Bindable).innerBorder.Style = (Style)NewValue;
206 }
207
208 public string SvgSource
209 {
210 get => (string)this.GetValue(SvgSourceProperty);
211 set => this.SetValue(SvgSourceProperty, value);
212 }
213
214 public Aspect SvgAspect
215 {
216 get => (Aspect)this.GetValue(SvgAspectProperty);
217 set => this.SetValue(SvgAspectProperty, value);
218 }
219
220 public Color SvgTintColor
221 {
222 get => (Color)this.GetValue(SvgTintColorProperty);
223 set => this.SetValue(SvgTintColorProperty, value);
224 }
225
226 public Style SvgStyle
227 {
228 get => (Style)this.GetValue(SvgStyleProperty);
229 set => this.SetValue(SvgStyleProperty, value);
230 }
231
232 public string LabelText
233 {
234 get => (string)this.GetValue(LabelTextProperty);
235 set => this.SetValue(LabelTextProperty, value);
236 }
237
238 public Style LabelStyle
239 {
240 get => (Style)this.GetValue(LabelStyleProperty);
241 set => this.SetValue(LabelStyleProperty, value);
242 }
243
244 public LabelPosition LabelPosition
245 {
246 get => (LabelPosition)this.GetValue(LabelPositionProperty);
247 set => this.SetValue(LabelPositionProperty, value);
248 }
249
250 public Style BorderStyle
251 {
252 get => (Style)this.GetValue(BorderDataElement.BorderStyleProperty);
253 set => this.SetValue(BorderDataElement.BorderStyleProperty, value);
254 }
255
256 public SvgButton()
257 : base()
258 {
259 this.innerSvg = new()
260 {
261 Aspect = this.SvgAspect,
262 Style = this.SvgStyle,
263 TintColor = this.SvgTintColor,
264 Source = this.SvgSource
265 };
266 this.innerLabel = new()
267 {
268 Text = this.LabelText,
269 Style = this.LabelStyle,
270 VerticalOptions = LayoutOptions.Center,
271 HorizontalOptions = LayoutOptions.Center
272 };
273 this.innerGrid = new()
274 {
275 ColumnDefinitions =
276 {
277 new ColumnDefinition(width: GridLength.Auto),
278 new ColumnDefinition(width: GridLength.Auto)
279 },
280 RowDefinitions =
281 {
282 new RowDefinition(height: GridLength.Auto),
283 new RowDefinition(height: GridLength.Auto)
284 },
285 HorizontalOptions = LayoutOptions.Center,
286 VerticalOptions = LayoutOptions.Center
287 };
288
289 this.innerGrid.Add(this.innerLabel);
290 this.innerGrid.Add(this.innerSvg);
291
292 if (string.IsNullOrEmpty(this.LabelText))
293 {
294 this.innerGrid.RowSpacing = 0;
295 this.innerGrid.ColumnSpacing = 0;
296 this.innerLabel.IsVisible = false;
297 }
298 else
299 {
300 switch (this.LabelPosition)
301 {
302 case LabelPosition.Top:
303 this.innerLabel.Row(0);
304 this.innerLabel.Column(0);
305 this.innerSvg.Row(1);
306 this.innerSvg.Column(0);
307 this.innerGrid.ColumnSpacing = 0;
308 this.innerGrid.RowSpacing = 8;
309 break;
310 case LabelPosition.Bottom:
311 this.innerLabel.Row(1);
312 this.innerLabel.Column(0);
313 this.innerSvg.Row(0);
314 this.innerSvg.Column(0);
315 this.innerGrid.ColumnSpacing = 0;
316 this.innerGrid.RowSpacing = 8;
317 break;
318 case LabelPosition.Left:
319 this.innerLabel.Column(0);
320 this.innerLabel.Row(0);
321 this.innerSvg.Column(1);
322 this.innerSvg.Row(0);
323 this.innerGrid.ColumnSpacing = 8;
324 this.innerGrid.RowSpacing = 0;
325 break;
326 case LabelPosition.Right:
327 this.innerLabel.Column(1);
328 this.innerLabel.Row(0);
329 this.innerSvg.Column(0);
330 this.innerSvg.Row(0);
331 this.innerGrid.ColumnSpacing = 8;
332 this.innerGrid.RowSpacing = 0;
333 break;
334 }
335 }
336
337 this.innerBorder = new()
338 {
339 Style = this.BorderStyle,
340 Content = this.innerGrid
341 };
342
343 this.Content = this.innerBorder;
344 }
345
349 public void Dispose()
350 {
351 this.innerSvg.Dispose();
352 GC.SuppressFinalize(this);
353 }
354 }
355
356 public enum LabelPosition
357 {
358 Top,
359 Bottom,
360 Left,
361 Right
362 }
363}
void Dispose()
Releases all resources used by the SvgView control.
Definition: SvgButton.cs:349
static readonly BindableProperty BorderStyleProperty
Bindable property for BorderStyle.
Definition: SvgButton.cs:63
static readonly BindableProperty SvgStyleProperty
Bindable property for SvgSource.
Definition: SvgButton.cs:36
static readonly BindableProperty SvgSourceProperty
Bindable property for SvgSource.
Definition: SvgButton.cs:15
static readonly BindableProperty SvgAspectProperty
Bindable property for SvgSource.
Definition: SvgButton.cs:22
static readonly BindableProperty SvgTintColorProperty
Bindable property for SvgSource.
Definition: SvgButton.cs:29
static readonly BindableProperty LabelPositionProperty
Bindable property for LabelPosition.
Definition: SvgButton.cs:55
A custom .NET MAUI control for rendering Scalable Vector Graphics (SVG) files using SkiaSharp and Svg...
Definition: SvgView.cs:122
TemplatedButton represents a generalization of a Button whose appearance is defined by a ControlTempl...