Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CompositeEntry.cs
1using System.Windows.Input;
2using Microsoft.Maui.Controls.Shapes;
4using PathShape = Microsoft.Maui.Controls.Shapes.Path;
5
7{
8 public class CompositeEntry : ContentView
9 {
10 private readonly Border innerBorder;
11 private readonly Grid innerGrid;
12 private readonly PathShape innerPath;
13 private readonly PathShape clickablePath;
14 private readonly Entry innerEntry;
15
19 public static readonly BindableProperty FocusedCommandProperty = BindableProperty.Create(
20 nameof(FocusedCommand),
21 typeof(ICommand),
22 typeof(CompositeEntry));
23
27 public ICommand FocusedCommand
28 {
29 get => (ICommand)this.GetValue(FocusedCommandProperty);
30 set => this.SetValue(FocusedCommandProperty, value);
31 }
32
36 public static readonly BindableProperty UnfocusedCommandProperty = BindableProperty.Create(
37 nameof(UnfocusedCommand),
38 typeof(ICommand),
39 typeof(CompositeEntry));
40
44 public ICommand UnfocusedCommand
45 {
46 get => (ICommand)this.GetValue(UnfocusedCommandProperty);
47 set => this.SetValue(UnfocusedCommandProperty, value);
48 }
49
53 public static readonly BindableProperty PathClickedCommandProperty = BindableProperty.Create(
54 nameof(PathClickedCommand),
55 typeof(ICommand),
56 typeof(CompositeEntry));
57
61 public ICommand PathClickedCommand
62 {
63 get => (ICommand)this.GetValue(PathClickedCommandProperty);
64 set => this.SetValue(PathClickedCommandProperty, value);
65 }
66
70 public static readonly BindableProperty ClickablePathDataProperty = BindableProperty.Create(
71 nameof(ClickablePathData),
72 typeof(Geometry),
73 typeof(CompositeEntry),
74 defaultValue: null,
75 propertyChanged: (bindable, oldValue, newValue) =>
76 {
77 ((CompositeEntry)bindable).OnClickablePathDataPropertyChanged((Geometry)oldValue, (Geometry)newValue);
78 });
79
83 public Geometry ClickablePathData
84 {
85 get => (Geometry)this.GetValue(ClickablePathDataProperty);
86 set => this.SetValue(ClickablePathDataProperty, value);
87 }
88
92 public static readonly BindableProperty KeyboardProperty = BindableProperty.Create(
93 nameof(Keyboard),
94 typeof(Keyboard),
95 typeof(CompositeEntry),
96 defaultValue: Keyboard.Default,
97 propertyChanged: (bindable, oldValue, newValue) =>
98 {
99 ((CompositeEntry)bindable).OnKeyboardPropertyChanged((Keyboard)oldValue, (Keyboard)newValue);
100 });
101
106 {
107 get => (Keyboard)this.GetValue(KeyboardProperty);
108 set => this.SetValue(KeyboardProperty, value);
109 }
110
114 public static readonly BindableProperty BorderStyleProperty = BindableProperty.Create(
115 nameof(BorderStyle),
116 typeof(Style),
117 typeof(CompositeEntry),
118 defaultValue: null,
119 propertyChanged: (bindable, oldValue, newValue) =>
120 {
121 ((CompositeEntry)bindable).OnBorderStylePropertyChanged((Style)oldValue, (Style)newValue);
122 });
123
127 public Style BorderStyle
128 {
129 get => (Style)this.GetValue(BorderStyleProperty);
130 set => this.SetValue(BorderStyleProperty, value);
131 }
132
136 public static readonly BindableProperty StackSpacingProperty = BindableProperty.Create(
137 nameof(StackSpacing),
138 typeof(double),
139 typeof(CompositeEntry),
140 defaultValue: 0.0,
141 propertyChanged: (bindable, oldValue, newValue) =>
142 {
143 ((CompositeEntry)bindable).OnStackSpacingPropertyChanged((double)oldValue, (double)newValue);
144 });
145
149 public double StackSpacing
150 {
151 get => (double)this.GetValue(StackSpacingProperty);
152 set => this.SetValue(StackSpacingProperty, value);
153 }
154
158 public static readonly BindableProperty PathDataProperty = BindableProperty.Create(
159 nameof(PathData),
160 typeof(Geometry),
161 typeof(CompositeEntry),
162 defaultValue: null,
163 propertyChanged: (bindable, oldValue, newValue) =>
164 {
165 ((CompositeEntry)bindable).OnPathDataPropertyChanged((Geometry)oldValue, (Geometry)newValue);
166 });
167
171 public Geometry PathData
172 {
173 get => (Geometry)this.GetValue(PathDataProperty);
174 set => this.SetValue(PathDataProperty, value);
175 }
176
180 public static readonly BindableProperty PathStyleProperty = BindableProperty.Create(
181 nameof(PathStyle),
182 typeof(Style),
183 typeof(CompositeEntry),
184 defaultValue: null,
185 propertyChanged: (bindable, oldValue, newValue) =>
186 {
187 ((CompositeEntry)bindable).OnPathStylePropertyChanged((Style)oldValue, (Style)newValue);
188 });
189
193 public Style PathStyle
194 {
195 get => (Style)this.GetValue(PathStyleProperty);
196 set => this.SetValue(PathStyleProperty, value);
197 }
198
202 public static readonly BindableProperty EntryDataProperty = BindableProperty.Create(
203 nameof(EntryData),
204 typeof(string),
205 typeof(CompositeEntry),
206 defaultValue: string.Empty,
207 defaultBindingMode: BindingMode.TwoWay);
208
212 public string EntryData
213 {
214 get => (string)this.GetValue(EntryDataProperty);
215 set => this.SetValue(EntryDataProperty, value);
216 }
217
221 public static readonly BindableProperty EntryHintProperty = BindableProperty.Create(
222 nameof(EntryHint),
223 typeof(string),
224 typeof(CompositeEntry),
225 defaultValue: string.Empty,
226 propertyChanged: (bindable, oldValue, newValue) =>
227 {
228 ((CompositeEntry)bindable).OnEntryHintPropertyChanged((string)oldValue, (string)newValue);
229 });
230
234 public string EntryHint
235 {
236 get => (string)this.GetValue(EntryHintProperty);
237 set => this.SetValue(EntryHintProperty, value);
238 }
239
243 public static readonly BindableProperty EntryStyleProperty = BindableProperty.Create(
244 nameof(EntryStyle),
245 typeof(Style),
246 typeof(CompositeEntry),
247 defaultValue: null,
248 propertyChanged: (bindable, oldValue, newValue) =>
249 {
250 ((CompositeEntry)bindable).OnEntryStylePropertyChanged((Style)oldValue, (Style)newValue);
251 });
252
256 public Style EntryStyle
257 {
258 get => (Style)this.GetValue(EntryStyleProperty);
259 set => this.SetValue(EntryStyleProperty, value);
260 }
261
265 public static readonly BindableProperty ReturnCommandProperty = BindableProperty.Create(
266 nameof(ReturnCommand),
267 typeof(ICommand),
268 typeof(CompositeEntry),
269 defaultValue: null,
270 propertyChanged: (bindable, oldValue, newValue) =>
271 {
272 ((CompositeEntry)bindable).OnReturnCommandPropertyChanged((ICommand)oldValue, (ICommand)newValue);
273 });
274
278 public ICommand ReturnCommand
279 {
280 get => (ICommand)this.GetValue(ReturnCommandProperty);
281 set => this.SetValue(ReturnCommandProperty, value);
282 }
283
287 public static readonly BindableProperty IsPasswordProperty = BindableProperty.Create(
288 nameof(IsPassword),
289 typeof(bool),
290 typeof(CompositeEntry),
291 defaultValue: false,
292 propertyChanged: (bindable, oldValue, newValue) =>
293 {
294 ((CompositeEntry)bindable).OnIsPasswordPropertyChanged((bool)oldValue, (bool)newValue);
295 });
296
300 public bool IsPassword
301 {
302 get => (bool)this.GetValue(IsPasswordProperty);
303 set => this.SetValue(IsPasswordProperty, value);
304 }
305
309 public static readonly BindableProperty IsReadOnlyProperty = BindableProperty.Create(
310 nameof(IsReadOnly),
311 typeof(bool),
312 typeof(CompositeEntry),
313 defaultValue: false,
314 propertyChanged: (bindable, oldValue, newValue) =>
315 {
316 ((CompositeEntry)bindable).OnIsReadOnlyPropertyChanged((bool)oldValue, (bool)newValue);
317 });
318
322 public bool IsReadOnly
323 {
324 get => (bool)this.GetValue(IsReadOnlyProperty);
325 set => this.SetValue(IsReadOnlyProperty, value);
326 }
327
331 public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(
332 nameof(Placeholder),
333 typeof(string),
334 typeof(CompositeEntry),
335 defaultValue: string.Empty,
336 propertyChanged: (bindable, oldValue, newValue) =>
337 {
338 ((CompositeEntry)bindable).OnPlaceholderPropertyChanged((string)oldValue, (string)newValue);
339 });
340
344 public string Placeholder
345 {
346 get => (string)this.GetValue(PlaceholderProperty);
347 set => this.SetValue(PlaceholderProperty, value);
348 }
349
353 public static readonly BindableProperty TextColorProperty = BindableProperty.Create(
354 nameof(TextColor),
355 typeof(Color),
356 typeof(CompositeEntry),
357 defaultValue: Colors.Black,
358 propertyChanged: (bindable, oldValue, newValue) =>
359 {
360 ((CompositeEntry)bindable).OnTextColorPropertyChanged((Color)oldValue, (Color)newValue);
361 });
362
366 public Color TextColor
367 {
368 get => (Color)this.GetValue(TextColorProperty);
369 set => this.SetValue(TextColorProperty, value);
370 }
371
375 public event EventHandler Completed;
376
380 public Entry Entry => this.innerEntry;
381
385 public Border Border => this.innerBorder;
386
391 {
392 this.innerPath = new PathShape
393 {
394 VerticalOptions = LayoutOptions.Center,
395 IsVisible = this.PathData != null,
396 HeightRequest = 24,
397 WidthRequest = 24,
398 Aspect = Stretch.Uniform
399 };
400
401 this.clickablePath = new PathShape
402 {
403 VerticalOptions = LayoutOptions.Center,
404 IsVisible = this.ClickablePathData != null,
405 HeightRequest = 24,
406 WidthRequest = 24,
407 Aspect = Stretch.Uniform
408 };
409
410 TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer();
411 tapGestureRecognizer.Tapped += (s, e) =>
412 {
413 if (this.PathClickedCommand?.CanExecute(null) ?? false)
414 this.PathClickedCommand.Execute(null);
415 };
416 this.clickablePath.GestureRecognizers.Add(tapGestureRecognizer);
417
418 this.innerEntry = new Entry
419 {
420 VerticalOptions = LayoutOptions.Center,
421 HorizontalOptions = LayoutOptions.Fill,
422 BackgroundColor = this.BackgroundColor,
423 TextColor = this.TextColor
424 };
425
426 this.innerEntry.Completed += this.InnerEntry_Completed;
427
428 this.innerGrid = new Grid
429 {
430 HorizontalOptions = LayoutOptions.Fill,
431 ColumnDefinitions =
432 {
433 new ColumnDefinition { Width = GridLength.Auto },
434 new ColumnDefinition { Width = GridLength.Star },
435 new ColumnDefinition { Width = GridLength.Auto },
436 },
437 RowDefinitions =
438 {
439 new RowDefinition { Height = GridLength.Auto },
440 }
441 };
442
443 this.innerGrid.Add(this.innerPath, 0, 0);
444 this.innerGrid.Add(this.innerEntry, 1, 0);
445 this.innerGrid.Add(this.clickablePath, 2, 0);
446
447 this.innerBorder = new Border
448 {
449 StrokeThickness = 2,
450 Content = this.innerGrid,
451 BackgroundColor = this.BackgroundColor
452 };
453
454 this.Content = this.innerBorder;
455
456 // Bind Entry properties to the CompositeEntry properties
457 this.innerEntry.SetBinding(Entry.TextProperty, new Binding(nameof(this.EntryData), source: this, mode: BindingMode.TwoWay));
458 this.innerEntry.SetBinding(Entry.KeyboardProperty, new Binding(nameof(this.Keyboard), source: this));
459 this.innerEntry.SetBinding(Entry.PlaceholderProperty, new Binding(nameof(this.Placeholder), source: this));
460 this.innerEntry.SetBinding(Entry.IsPasswordProperty, new Binding(nameof(this.IsPassword), source: this));
461 this.innerEntry.SetBinding(Entry.IsReadOnlyProperty, new Binding(nameof(this.IsReadOnly), source: this));
462 this.innerEntry.SetBinding(Entry.ReturnCommandProperty, new Binding(nameof(this.ReturnCommand), source: this));
463 this.innerEntry.SetBinding(Entry.StyleProperty, new Binding(nameof(this.EntryStyle), source: this));
464 this.innerEntry.SetBinding(Entry.TextColorProperty, new Binding(nameof(this.TextColor), source: this));
465
466 // Handle focus events
467 this.innerEntry.Focused += this.OnEntryFocused;
468 this.innerEntry.Unfocused += this.OnEntryUnfocused;
469 }
470
471 private void OnKeyboardPropertyChanged(Keyboard oldValue, Keyboard newValue)
472 {
473 this.innerEntry.Keyboard = newValue;
474 }
475
476 private void OnBorderStylePropertyChanged(Style oldValue, Style newValue)
477 {
478 this.innerBorder.Style = newValue;
479 }
480
481 private void OnStackSpacingPropertyChanged(double oldValue, double newValue)
482 {
483 this.innerGrid.ColumnSpacing = (this.innerPath.IsVisible || this.clickablePath.IsVisible) ? newValue : 0;
484 }
485
486 private void OnPathDataPropertyChanged(Geometry oldValue, Geometry newValue)
487 {
488 this.innerPath.Data = newValue;
489 this.innerPath.IsVisible = newValue != null;
490 this.innerGrid.ColumnSpacing = (this.innerPath.IsVisible || this.clickablePath.IsVisible) ? this.StackSpacing : 0;
491 }
492
493 private void OnClickablePathDataPropertyChanged(Geometry oldValue, Geometry newValue)
494 {
495 this.clickablePath.Data = newValue;
496 this.clickablePath.IsVisible = newValue != null;
497 this.innerGrid.ColumnSpacing = (this.innerPath.IsVisible || this.clickablePath.IsVisible) ? this.StackSpacing : 0;
498 }
499
500 private void OnPathStylePropertyChanged(Style oldValue, Style newValue)
501 {
502 this.innerPath.Style = newValue;
503 this.clickablePath.Style = newValue;
504 }
505
506 private void OnEntryHintPropertyChanged(string oldValue, string newValue)
507 {
508 this.innerEntry.Placeholder = newValue;
509 }
510
511 private void OnEntryStylePropertyChanged(Style oldValue, Style newValue)
512 {
513 this.innerEntry.Style = newValue;
514 }
515
516 private void OnReturnCommandPropertyChanged(ICommand oldValue, ICommand newValue)
517 {
518 this.innerEntry.ReturnCommand = newValue;
519 }
520
521 private void OnIsPasswordPropertyChanged(bool oldValue, bool newValue)
522 {
523 this.innerEntry.IsPassword = newValue;
524 }
525
526 private void OnIsReadOnlyPropertyChanged(bool oldValue, bool newValue)
527 {
528 this.innerEntry.IsReadOnly = newValue;
529 }
530
531 private void OnPlaceholderPropertyChanged(string oldValue, string newValue)
532 {
533 this.innerEntry.Placeholder = newValue;
534 }
535
536 private void OnTextColorPropertyChanged(Color oldValue, Color newValue)
537 {
538 this.innerEntry.TextColor = newValue;
539 }
540
541 private void OnEntryFocused(object sender, FocusEventArgs e)
542 {
543 if (this.FocusedCommand?.CanExecute(e) ?? false)
544 {
545 this.FocusedCommand.Execute(e);
546 }
547 }
548
549 private void OnEntryUnfocused(object sender, FocusEventArgs e)
550 {
551 if (this.UnfocusedCommand?.CanExecute(e) ?? false)
552 {
553 this.UnfocusedCommand.Execute(e);
554 }
555 }
556
557 private void InnerEntry_Completed(object sender, EventArgs e)
558 {
559 try
560 {
561 this.Completed?.Invoke(sender, e);
562 }
563 catch (Exception ex)
564 {
565 ServiceRef.LogService.LogException(ex);
566 }
567 }
568
569 public override string ToString()
570 {
571 return this.innerEntry.Text;
572 }
573 }
574}
Base class that references services in the app.
Definition: ServiceRef.cs:31
static ILogService LogService
Log service.
Definition: ServiceRef.cs:91
static readonly BindableProperty PathClickedCommandProperty
Bindable property for the command to be executed when the clickable PathData is tapped.
string EntryData
Gets or sets the data for the Entry.
static readonly BindableProperty PathDataProperty
Bindable property for the PathData to be displayed.
Geometry ClickablePathData
Gets or sets the clickable PathData to be displayed.
Keyboard Keyboard
Gets or sets the Keyboard type for the Entry.
bool IsReadOnly
Gets or sets whether the Entry is read-only.
double StackSpacing
Gets or sets the spacing between elements in the stack.
string Placeholder
Gets or sets the placeholder text for the Entry.
Style BorderStyle
Gets or sets the style for the Border.
string EntryHint
Gets or sets the hint (placeholder) for the Entry.
static readonly BindableProperty FocusedCommandProperty
Bindable property for the command to be executed when the Entry gains focus.
static readonly BindableProperty EntryDataProperty
Bindable property for the Entry data.
ICommand ReturnCommand
Gets or sets the command executed when the return key is pressed.
static readonly BindableProperty IsPasswordProperty
Bindable property for IsPassword.
ICommand UnfocusedCommand
Gets or sets the command to be executed when the Entry loses focus.
Entry Entry
Embedded Entry control.
Style EntryStyle
Gets or sets the style for the Entry.
static readonly BindableProperty ReturnCommandProperty
Bindable property for the return command.
static readonly BindableProperty EntryStyleProperty
Bindable property for the Entry style.
CompositeEntry()
Initializes a new instance of the CompositeEntry class.
ICommand PathClickedCommand
Gets or sets the command to be executed when the clickable PathData is tapped.
Border Border
Embedded Border encapsulating the Entry.
Color TextColor
Gets or sets the text color for the Entry.
static readonly BindableProperty ClickablePathDataProperty
Bindable property for the clickable PathData to be displayed.
ICommand FocusedCommand
Gets or sets the command to be executed when the Entry gains focus.
static readonly BindableProperty PlaceholderProperty
Bindable property for the Entry placeholder.
EventHandler Completed
Occurs when the user finalizes the text in an entry with the return key.
static readonly BindableProperty UnfocusedCommandProperty
Bindable property for the command to be executed when the Entry loses focus.
Geometry PathData
Gets or sets the PathData to be displayed.
static readonly BindableProperty StackSpacingProperty
Bindable property for the spacing between elements in the stack.
bool IsPassword
Gets or sets whether the Entry should mask the input.
Style PathStyle
Gets or sets the style for the Path.
static readonly BindableProperty PathStyleProperty
Bindable property for the Path style.
static readonly BindableProperty EntryHintProperty
Bindable property for the Entry hint (placeholder).
static readonly BindableProperty BorderStyleProperty
Bindable property for the border style.
static readonly BindableProperty IsReadOnlyProperty
Bindable property for IsReadOnly.
static readonly BindableProperty KeyboardProperty
Bindable property for the Keyboard type.
static readonly BindableProperty TextColorProperty
Bindable property for the Entry text color.