Accordion.cs 8.3 KB

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