| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Controls.Primitives;
- using Avalonia.Data;
- using Avalonia.Layout;
- using Avalonia.Media;
- using InABox.Avalonia.Components;
- using InABox.Core;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PRS.Avalonia.DigitalForms;
- internal interface IOptionControl
- {
- IBrush? Background { get; set; }
- public string? GetValue();
- public void SetValue(string? value);
- public bool IsEmpty();
- }
- public class ButtonsOptionControl : ContentControl, IOptionControl
- {
- private readonly Action OnChanged;
- private ButtonStrip _strip;
- IBrush? IOptionControl.Background
- {
- get => _strip.Background;
- set => _strip.Background = value;
- }
- public ButtonsOptionControl(string[] options, Action onChanged)
- {
- OnChanged = onChanged;
- _strip = new ButtonStrip();
- _strip.Classes.Add("TabStrip");
- // _tabStrip = new TabStrip();
- // _tabStrip.Classes.Add("ButtonsList");
- // _tabStrip.Classes.Add("Standard");
- foreach (var option in options)
- {
- var item = new ButtonStripItem
- {
- Text = option.Replace("\r", "").Replace("\n", ""),
- Tag = option
- };
- _strip.Items.Add(item);
- }
- _strip.SelectedIndex = -1;
- _strip.SelectionChanged += ButtonsOptionControl_SelectionChanged;
- Content = this._strip;
- }
- private void ButtonsOptionControl_SelectionChanged(object? sender, EventArgs e)
- {
- OnChanged();
- }
- public string? GetValue()
- {
- return _strip.SelectedItem as string;
- }
- public void SetValue(string? value)
- {
- var item = _strip.Items.FirstOrDefault(x => Equals(x.Tag, value));
- _strip.SelectedIndex = item is not null ? _strip.Items.IndexOf(item) : -1;
- }
- public bool IsEmpty() => GetValue() == null;
- }
- public class RadioOptionControl : ContentControl, IOptionControl
- {
- private Action OnChanged;
- private Grid _grid;
- public RadioOptionControl(string[] options, Action onChanged)
- {
- _grid = new Grid();
-
- OnChanged = onChanged;
- foreach (var option in options)
- {
- _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
- var button = new RadioButton();
- button.Margin = new(5, 0, 5, 0);
- button.Content = option.Replace("\r", "").Replace("\n", "");
- button.VerticalContentAlignment = VerticalAlignment.Center;
- button.HorizontalAlignment = HorizontalAlignment.Stretch;
- button.SetValue(Grid.RowProperty, _grid.Children.Count);
- button.Tag = option;
- button.IsCheckedChanged += Button_IsCheckedChanged;
- _grid.Children.Add(button);
- }
- Content = _grid;
- }
- private void Button_IsCheckedChanged(object? sender, global::Avalonia.Interactivity.RoutedEventArgs e)
- {
- OnChanged();
- }
- public string? GetValue()
- {
- foreach (var child in _grid.Children)
- {
- if (child is RadioButton radio)
- {
- if (radio.IsChecked == true)
- {
- return radio.Tag as string;
- }
- }
- }
- return null;
- }
- public void SetValue(string? value)
- {
- foreach (var child in _grid.Children)
- {
- if (child is RadioButton radio)
- {
- if (radio.Tag as string == value)
- {
- radio.IsChecked = true;
- }
- }
- }
- }
- public bool IsEmpty() => GetValue() == null;
- }
- public class ComboBoxOptionControl : ContentControl, IOptionControl
- {
- private readonly Action OnChanged;
- private ComboBox _comboBox;
- IBrush? IOptionControl.Background
- {
- get => _comboBox.Background;
- set => _comboBox.Background = value;
- }
- public ComboBoxOptionControl(string[] options, Action onChanged)
- {
- _comboBox = new ComboBox
- {
- HorizontalAlignment = HorizontalAlignment.Stretch,
- VerticalAlignment = VerticalAlignment.Stretch
- };
- OnChanged = onChanged;
- foreach (var option in options)
- _comboBox.Items.Add(option);
- VerticalContentAlignment = VerticalAlignment.Center;
- _comboBox.SelectionChanged += ComboBoxOptionControl_SelectionChanged;
- Content = _comboBox;
- }
- private void ComboBoxOptionControl_SelectionChanged(object? sender, SelectionChangedEventArgs e)
- {
- OnChanged();
- }
- public string? GetValue()
- {
- return _comboBox.SelectedValue as string;
- }
- public void SetValue(string? value)
- {
- _comboBox.SelectedValue = value;
- }
- public bool IsEmpty() => GetValue().IsNullOrWhiteSpace();
- }
- class DFOptionControl : DigitalFormFieldControl<DFLayoutOptionField, DFLayoutOptionFieldProperties, string, string?>
- {
- private IOptionControl OptionControl = null!; // Late-initialised
- protected override Control Create()
- {
- var options = string.IsNullOrWhiteSpace(Field.Properties.Options)
- ? new string[] { }
- : Field.Properties.Options.Replace(",","\n").Split('\n').Select(x=>x.Trim()).ToArray();
- switch (Field.Properties.OptionType)
- {
- case DFLayoutOptionType.Buttons:
- return SetControl(new ButtonsOptionControl(options, ChangeField));
- case DFLayoutOptionType.Radio:
- return SetControl(new RadioOptionControl(options, ChangeField));
- }
- return SetControl(new ComboBoxOptionControl(options, ChangeField));
- }
- private T SetControl<T>(T value)
- where T : Control, IOptionControl
- {
- OptionControl = value;
- return value;
- }
- public override void SetSerializedValue(string? value)
- {
- SetValue(value);
- }
- public override string? GetSerializedValue()
- {
- return OptionControl.GetValue();
- }
- public override string GetValue() => OptionControl.GetValue() ?? "";
- public override void SetValue(string? value) => OptionControl.SetValue(value ?? "");
- protected override bool IsEmpty() => OptionControl.IsEmpty();
- public override void SetBackground(IBrush brush, bool defaultColour)
- {
- if(OptionControl is ButtonsOptionControl)
- {
- if (!defaultColour)
- {
- Background = brush;
- }
- }
- else
- {
- OptionControl.Background = brush;
- }
- }
- public override void SetEnabled(bool enabled)
- {
- IsEnabled = enabled;
- }
- public override bool GetEnabled() => IsEnabled;
- }
|