DFOptionFieldControl.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Primitives;
  4. using Avalonia.Data;
  5. using Avalonia.Layout;
  6. using Avalonia.Media;
  7. using InABox.Avalonia.Components;
  8. using InABox.Core;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace PRS.Avalonia.DigitalForms;
  15. internal interface IOptionControl
  16. {
  17. IBrush? Background { get; set; }
  18. public string? GetValue();
  19. public void SetValue(string? value);
  20. public bool IsEmpty();
  21. }
  22. public class ButtonsOptionControl : ContentControl, IOptionControl
  23. {
  24. private readonly Action OnChanged;
  25. private ButtonStrip _strip;
  26. IBrush? IOptionControl.Background
  27. {
  28. get => _strip.Background;
  29. set => _strip.Background = value;
  30. }
  31. public ButtonsOptionControl(string[] options, Action onChanged)
  32. {
  33. OnChanged = onChanged;
  34. _strip = new ButtonStrip();
  35. _strip.Classes.Add("TabStrip");
  36. // _tabStrip = new TabStrip();
  37. // _tabStrip.Classes.Add("ButtonsList");
  38. // _tabStrip.Classes.Add("Standard");
  39. foreach (var option in options)
  40. {
  41. var item = new ButtonStripItem
  42. {
  43. Text = option.Replace("\r", "").Replace("\n", ""),
  44. Tag = option
  45. };
  46. _strip.Items.Add(item);
  47. }
  48. _strip.SelectedIndex = -1;
  49. _strip.SelectionChanged += ButtonsOptionControl_SelectionChanged;
  50. Content = this._strip;
  51. }
  52. private void ButtonsOptionControl_SelectionChanged(object? sender, EventArgs e)
  53. {
  54. OnChanged();
  55. }
  56. public string? GetValue()
  57. {
  58. return _strip.SelectedItem as string;
  59. }
  60. public void SetValue(string? value)
  61. {
  62. var item = _strip.Items.FirstOrDefault(x => Equals(x.Tag, value));
  63. _strip.SelectedIndex = item is not null ? _strip.Items.IndexOf(item) : -1;
  64. }
  65. public bool IsEmpty() => GetValue() == null;
  66. }
  67. public class RadioOptionControl : ContentControl, IOptionControl
  68. {
  69. private Action OnChanged;
  70. private Grid _grid;
  71. public RadioOptionControl(string[] options, Action onChanged)
  72. {
  73. _grid = new Grid();
  74. OnChanged = onChanged;
  75. foreach (var option in options)
  76. {
  77. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  78. var button = new RadioButton();
  79. button.Margin = new(5, 0, 5, 0);
  80. button.Content = option.Replace("\r", "").Replace("\n", "");
  81. button.VerticalContentAlignment = VerticalAlignment.Center;
  82. button.HorizontalAlignment = HorizontalAlignment.Stretch;
  83. button.SetValue(Grid.RowProperty, _grid.Children.Count);
  84. button.Tag = option;
  85. button.IsCheckedChanged += Button_IsCheckedChanged;
  86. _grid.Children.Add(button);
  87. }
  88. Content = _grid;
  89. }
  90. private void Button_IsCheckedChanged(object? sender, global::Avalonia.Interactivity.RoutedEventArgs e)
  91. {
  92. OnChanged();
  93. }
  94. public string? GetValue()
  95. {
  96. foreach (var child in _grid.Children)
  97. {
  98. if (child is RadioButton radio)
  99. {
  100. if (radio.IsChecked == true)
  101. {
  102. return radio.Tag as string;
  103. }
  104. }
  105. }
  106. return null;
  107. }
  108. public void SetValue(string? value)
  109. {
  110. foreach (var child in _grid.Children)
  111. {
  112. if (child is RadioButton radio)
  113. {
  114. if (radio.Tag as string == value)
  115. {
  116. radio.IsChecked = true;
  117. }
  118. }
  119. }
  120. }
  121. public bool IsEmpty() => GetValue() == null;
  122. }
  123. public class ComboBoxOptionControl : ContentControl, IOptionControl
  124. {
  125. private readonly Action OnChanged;
  126. private ComboBox _comboBox;
  127. IBrush? IOptionControl.Background
  128. {
  129. get => _comboBox.Background;
  130. set => _comboBox.Background = value;
  131. }
  132. public ComboBoxOptionControl(string[] options, Action onChanged)
  133. {
  134. _comboBox = new ComboBox
  135. {
  136. HorizontalAlignment = HorizontalAlignment.Stretch,
  137. VerticalAlignment = VerticalAlignment.Stretch
  138. };
  139. OnChanged = onChanged;
  140. foreach (var option in options)
  141. _comboBox.Items.Add(option);
  142. VerticalContentAlignment = VerticalAlignment.Center;
  143. _comboBox.SelectionChanged += ComboBoxOptionControl_SelectionChanged;
  144. Content = _comboBox;
  145. }
  146. private void ComboBoxOptionControl_SelectionChanged(object? sender, SelectionChangedEventArgs e)
  147. {
  148. OnChanged();
  149. }
  150. public string? GetValue()
  151. {
  152. return _comboBox.SelectedValue as string;
  153. }
  154. public void SetValue(string? value)
  155. {
  156. _comboBox.SelectedValue = value;
  157. }
  158. public bool IsEmpty() => GetValue().IsNullOrWhiteSpace();
  159. }
  160. class DFOptionControl : DigitalFormFieldControl<DFLayoutOptionField, DFLayoutOptionFieldProperties, string, string?>
  161. {
  162. private IOptionControl OptionControl = null!; // Late-initialised
  163. protected override Control Create()
  164. {
  165. var options = string.IsNullOrWhiteSpace(Field.Properties.Options)
  166. ? new string[] { }
  167. : Field.Properties.Options.Replace(",","\n").Split('\n').Select(x=>x.Trim()).ToArray();
  168. switch (Field.Properties.OptionType)
  169. {
  170. case DFLayoutOptionType.Buttons:
  171. return SetControl(new ButtonsOptionControl(options, ChangeField));
  172. case DFLayoutOptionType.Radio:
  173. return SetControl(new RadioOptionControl(options, ChangeField));
  174. }
  175. return SetControl(new ComboBoxOptionControl(options, ChangeField));
  176. }
  177. private T SetControl<T>(T value)
  178. where T : Control, IOptionControl
  179. {
  180. OptionControl = value;
  181. return value;
  182. }
  183. public override void SetSerializedValue(string? value)
  184. {
  185. SetValue(value);
  186. }
  187. public override string? GetSerializedValue()
  188. {
  189. return OptionControl.GetValue();
  190. }
  191. public override string GetValue() => OptionControl.GetValue() ?? "";
  192. public override void SetValue(string? value) => OptionControl.SetValue(value ?? "");
  193. protected override bool IsEmpty() => OptionControl.IsEmpty();
  194. public override void SetBackground(IBrush brush, bool defaultColour)
  195. {
  196. if(OptionControl is ButtonsOptionControl)
  197. {
  198. if (!defaultColour)
  199. {
  200. Background = brush;
  201. }
  202. }
  203. else
  204. {
  205. OptionControl.Background = brush;
  206. }
  207. }
  208. public override void SetEnabled(bool enabled)
  209. {
  210. IsEnabled = enabled;
  211. }
  212. public override bool GetEnabled() => IsEnabled;
  213. }