ButtonStrip.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Metadata;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.LogicalTree;
  6. using Avalonia.Media;
  7. using Avalonia.Metadata;
  8. using Avalonia.Threading;
  9. using CommunityToolkit.Mvvm.Input;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Collections.Specialized;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows.Input;
  19. namespace InABox.Avalonia.Components;
  20. public partial class ButtonStrip : TemplatedControl
  21. {
  22. public static StyledProperty<IBrush?> SelectedBackgroundProperty = AvaloniaProperty.Register<ButtonStripItem, IBrush?>(
  23. nameof(SelectedBackground));
  24. public static StyledProperty<IBrush?> SelectedForegroundProperty = AvaloniaProperty.Register<ButtonStripItem, IBrush?>(
  25. nameof(SelectedForeground));
  26. public static StyledProperty<double> ItemSpacingProperty = AvaloniaProperty.Register<ButtonStripItem, double>(
  27. nameof(ItemSpacing));
  28. public static StyledProperty<ObservableCollection<ButtonStripItem>> ItemsProperty =
  29. AvaloniaProperty.Register<ButtonStripItem, ObservableCollection<ButtonStripItem>>(nameof(Items));
  30. public static StyledProperty<IEnumerable?> ItemsSourceProperty =
  31. AvaloniaProperty.Register<ButtonStripItem, IEnumerable?>(nameof(ItemsSource));
  32. public static StyledProperty<ICommand> SelectedCommandProperty = AvaloniaProperty.Register<ButtonStrip, ICommand>(nameof(SelectedCommand));
  33. public static StyledProperty<object?> SelectedItemProperty = AvaloniaProperty.Register<ButtonStrip, object?>(nameof(SelectedItem));
  34. public IBrush? SelectedBackground
  35. {
  36. get => GetValue(SelectedBackgroundProperty);
  37. set => SetValue(SelectedBackgroundProperty, value);
  38. }
  39. public IBrush? SelectedForeground
  40. {
  41. get => GetValue(SelectedForegroundProperty);
  42. set => SetValue(SelectedForegroundProperty, value);
  43. }
  44. public double ItemSpacing
  45. {
  46. get => GetValue(ItemSpacingProperty);
  47. set => SetValue(ItemSpacingProperty, value);
  48. }
  49. public ICommand SelectedCommand
  50. {
  51. get => GetValue(SelectedCommandProperty);
  52. set => SetValue(SelectedCommandProperty, value);
  53. }
  54. public IEnumerable? ItemsSource
  55. {
  56. get => GetValue(ItemsSourceProperty);
  57. set => SetValue(ItemsSourceProperty, value);
  58. }
  59. [Content]
  60. public ObservableCollection<ButtonStripItem> Items
  61. {
  62. get => GetValue(ItemsProperty);
  63. set => SetValue(ItemsProperty, value);
  64. }
  65. private ButtonStripItem? SelectedButton
  66. {
  67. get => Items.FirstOrDefault(x => x.Selected);
  68. set
  69. {
  70. foreach(var item in Items)
  71. {
  72. item.Selected = item == value;
  73. }
  74. SelectedItem = value?.Tag;
  75. }
  76. }
  77. public object? SelectedItem
  78. {
  79. get => GetValue(SelectedItemProperty);
  80. set => SetValue(SelectedItemProperty, value);
  81. }
  82. public event EventHandler? SelectionChanged;
  83. static ButtonStrip()
  84. {
  85. ItemsProperty.Changed.AddClassHandler<ButtonStrip>(Items_Changed);
  86. ItemsSourceProperty.Changed.AddClassHandler<ButtonStrip>(ItemsSource_Changed);
  87. }
  88. private static void ItemsSource_Changed(ButtonStrip strip, AvaloniaPropertyChangedEventArgs args)
  89. {
  90. if(strip.ItemsSource is INotifyCollectionChanged notify)
  91. {
  92. notify.CollectionChanged += (sender, e) => strip.RebuildItems();
  93. }
  94. strip.RebuildItems();
  95. }
  96. private void RebuildItems()
  97. {
  98. Items.Clear();
  99. if(ItemsSource is not null)
  100. {
  101. foreach(var item in ItemsSource)
  102. {
  103. if(item is ButtonStripItem btn)
  104. {
  105. Items.Add(btn);
  106. }
  107. else
  108. {
  109. Items.Add(new(item?.ToString() ?? "") { Tag = item });
  110. }
  111. }
  112. }
  113. }
  114. private static void Items_Changed(ButtonStrip strip, AvaloniaPropertyChangedEventArgs args)
  115. {
  116. strip.LogicalChildren.Clear();
  117. if (strip.Items is not null)
  118. {
  119. strip.SelectedButton = strip.Items.FirstOrDefault();
  120. strip.LogicalChildren.AddRange(strip.Items);
  121. foreach(var item in strip.Items)
  122. {
  123. item.Command = strip.ItemSelectedCommand;
  124. }
  125. }
  126. }
  127. public ButtonStrip()
  128. {
  129. Items = new();
  130. Items.CollectionChanged += Items_CollectionChanged;
  131. }
  132. private void Items_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  133. {
  134. SelectedButton ??= Items.FirstOrDefault();
  135. switch (e.Action)
  136. {
  137. case NotifyCollectionChangedAction.Add:
  138. AddControlItemsToLogicalChildren(e.NewItems);
  139. break;
  140. case NotifyCollectionChangedAction.Remove:
  141. RemoveControlItemsFromLogicalChildren(e.OldItems);
  142. break;
  143. }
  144. foreach(var item in Items)
  145. {
  146. item.Command = ItemSelectedCommand;
  147. }
  148. }
  149. private void AddControlItemsToLogicalChildren(IEnumerable? items)
  150. {
  151. if (items is null) return;
  152. List<ILogical>? toAdd = null;
  153. foreach(var i in items)
  154. {
  155. if(i is Control control && !LogicalChildren.Contains(control))
  156. {
  157. toAdd ??= new();
  158. toAdd.Add(control);
  159. }
  160. }
  161. if(toAdd is not null)
  162. {
  163. LogicalChildren.AddRange(toAdd);
  164. }
  165. }
  166. private void RemoveControlItemsFromLogicalChildren(IEnumerable? items)
  167. {
  168. if (items is null) return;
  169. List<ILogical>? toRemove = null;
  170. foreach(var i in items)
  171. {
  172. if(i is Control control)
  173. {
  174. toRemove ??= new();
  175. toRemove.Add(control);
  176. }
  177. }
  178. if(toRemove is not null)
  179. {
  180. LogicalChildren.RemoveAll(toRemove);
  181. }
  182. }
  183. [RelayCommand]
  184. private void ItemSelected(ButtonStripItem item)
  185. {
  186. var children = this.GetLogicalChildren().ToArray();
  187. SelectedButton = item;
  188. SelectionChanged?.Invoke(this, new());
  189. SelectedCommand?.Execute(item.Tag);
  190. }
  191. public void AddRange(IEnumerable<string> items)
  192. {
  193. foreach(var item in items)
  194. {
  195. Items.Add(new(item));
  196. }
  197. }
  198. }
  199. [PseudoClasses(":selected")]
  200. public class ButtonStripItem : TemplatedControl
  201. {
  202. public static StyledProperty<string> TextProperty = AvaloniaProperty.Register<ButtonStripItem, string>(nameof(Text));
  203. public static StyledProperty<bool> SelectedProperty = AvaloniaProperty.Register<ButtonStripItem, bool>(nameof(Selected));
  204. public static StyledProperty<int> IndexProperty = AvaloniaProperty.Register<ButtonStripItem, int>(nameof(Index));
  205. public static StyledProperty<ICommand> CommandProperty = AvaloniaProperty.Register<ButtonStripItem, ICommand>(nameof(Command));
  206. public string Text
  207. {
  208. get => GetValue(TextProperty);
  209. set => SetValue(TextProperty, value);
  210. }
  211. public bool Selected
  212. {
  213. get => GetValue(SelectedProperty);
  214. set => SetValue(SelectedProperty, value);
  215. }
  216. public int Index
  217. {
  218. get => GetValue(IndexProperty);
  219. set => SetValue(IndexProperty, value);
  220. }
  221. public ICommand Command
  222. {
  223. get => GetValue(CommandProperty);
  224. set => SetValue(CommandProperty, value);
  225. }
  226. public ButtonStripItem()
  227. {
  228. }
  229. public ButtonStripItem(string text)
  230. {
  231. Text = text;
  232. }
  233. static ButtonStripItem()
  234. {
  235. SelectedProperty.Changed.AddClassHandler<ButtonStripItem>(Selected_Changed);
  236. }
  237. private static void Selected_Changed(ButtonStripItem item, AvaloniaPropertyChangedEventArgs args)
  238. {
  239. item.PseudoClasses.Set(":selected", item.Selected);
  240. }
  241. }