StaticColumnFilter.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Media;
  14. namespace InABox.DynamicGrid;
  15. public abstract class CheckBoxDynamicGridColumnFilter : IDynamicGridColumnFilter
  16. {
  17. protected HashSet<object?>? SelectedFilters = null;
  18. private FilterItem[] _items = [];
  19. private FilterItem _selectAll;
  20. public event Action<IDynamicGridColumnFilter>? FilterChanged;
  21. public CheckBoxDynamicGridColumnFilter()
  22. {
  23. _selectAll = new(new(), "Select All", this);
  24. }
  25. #region Abstract Interface
  26. protected abstract FilterItem[] GetData();
  27. public abstract bool FilterRow(CoreRow row);
  28. #endregion
  29. protected class FilterItem(object? value, string display, CheckBoxDynamicGridColumnFilter parent) : INotifyPropertyChanged
  30. {
  31. private object? _value = value;
  32. public object? Value
  33. {
  34. get => _value;
  35. set
  36. {
  37. _value = value;
  38. OnPropertyChanged();
  39. }
  40. }
  41. private string _display = display;
  42. public string Display
  43. {
  44. get => _display;
  45. set
  46. {
  47. _display = value;
  48. OnPropertyChanged();
  49. }
  50. }
  51. public bool IsChecked
  52. {
  53. get => this == parent._selectAll
  54. ? (parent.SelectedFilters is null)
  55. : (parent.SelectedFilters is null || parent.SelectedFilters.Contains(Value));
  56. }
  57. public event PropertyChangedEventHandler? PropertyChanged;
  58. private void OnPropertyChanged([CallerMemberName] string propertyName = "")
  59. {
  60. PropertyChanged?.Invoke(this, new(propertyName));
  61. }
  62. public void UpdateChecked()
  63. {
  64. OnPropertyChanged(nameof(IsChecked));
  65. }
  66. }
  67. public FrameworkElement CreateControl()
  68. {
  69. var grid = new Grid();
  70. grid.AddRow(GridUnitType.Auto);
  71. grid.AddRow(GridUnitType.Star);
  72. var searchBox = new TextBox
  73. {
  74. Background = Colors.LightYellow.ToBrush(),
  75. Margin = new(0.0, 0.0, 0.0, 5.0),
  76. Padding = new(5.0),
  77. VerticalContentAlignment = VerticalAlignment.Center
  78. };
  79. new TextBoxPlaceholderBehaviour { Text = "Search..." }.Attach(searchBox);
  80. _items = GetData();
  81. var itemList = new ListBox
  82. {
  83. Margin = new(0.0, 0.0, 0.0, 0.0)
  84. };
  85. itemList.ItemTemplate = TemplateGenerator.CreateDataTemplate(() =>
  86. {
  87. var box = new CheckBox();
  88. box.Padding = new(5.0);
  89. box.VerticalContentAlignment = VerticalAlignment.Center;
  90. box.HorizontalAlignment = HorizontalAlignment.Stretch;
  91. box.Bind<FilterItem, string?>(
  92. CheckBox.ContentProperty, x => x.Display,
  93. mode: BindingMode.OneWay);
  94. box.Bind<FilterItem, bool>(
  95. CheckBox.IsCheckedProperty, x => x.IsChecked,
  96. mode: BindingMode.OneWay);
  97. box.Bind<FilterItem, object?>(CheckBox.TagProperty, x => x);
  98. box.Checked += Box_Checked;
  99. box.Unchecked += Box_Unchecked;
  100. return box;
  101. });
  102. itemList.HorizontalContentAlignment = HorizontalAlignment.Stretch;
  103. itemList.ItemsSource = CoreUtils.One(_selectAll).Concat(_items);
  104. searchBox.TextChanged += (o, e) =>
  105. {
  106. itemList.ItemsSource = CoreUtils.One(_selectAll)
  107. .Concat(_items.Where(x => x.Display.Contains(searchBox.Text, StringComparison.CurrentCultureIgnoreCase)));
  108. };
  109. grid.AddChild(searchBox, 0, 0);
  110. grid.AddChild(itemList, 1, 0);
  111. return grid;
  112. }
  113. private bool _updating = false;
  114. private void Box_Unchecked(object sender, RoutedEventArgs e)
  115. {
  116. if (_updating || sender is not CheckBox box || box.Tag is not FilterItem tag) return;
  117. if(box.Tag == _selectAll)
  118. {
  119. if(SelectedFilters is null || SelectedFilters.Count > 0)
  120. {
  121. SelectedFilters = new();
  122. _updating = true;
  123. foreach(var item in _items)
  124. {
  125. item.UpdateChecked();
  126. }
  127. _selectAll.UpdateChecked();
  128. _updating = false;
  129. FilterChanged?.Invoke(this);
  130. }
  131. }
  132. else
  133. {
  134. if(SelectedFilters is null)
  135. {
  136. SelectedFilters = _items.Select(x => x.Value).ToHashSet();
  137. SelectedFilters.Remove(tag.Value);
  138. _updating = true;
  139. tag.UpdateChecked();
  140. _selectAll.UpdateChecked();
  141. _updating = false;
  142. FilterChanged?.Invoke(this);
  143. }
  144. else if(SelectedFilters.Remove(tag.Value))
  145. {
  146. _updating = true;
  147. tag.UpdateChecked();
  148. _selectAll.UpdateChecked();
  149. _updating = false;
  150. FilterChanged?.Invoke(this);
  151. }
  152. }
  153. }
  154. private void Box_Checked(object sender, RoutedEventArgs e)
  155. {
  156. if (_updating || sender is not CheckBox box || box.Tag is not FilterItem tag) return;
  157. if(tag == _selectAll)
  158. {
  159. if(SelectedFilters is not null)
  160. {
  161. SelectedFilters = null;
  162. _updating = true;
  163. foreach(var item in _items)
  164. {
  165. item.UpdateChecked();
  166. }
  167. _selectAll.UpdateChecked();
  168. _updating = false;
  169. FilterChanged?.Invoke(this);
  170. }
  171. }
  172. else
  173. {
  174. if(SelectedFilters is not null && SelectedFilters.Add(tag.Value))
  175. {
  176. _updating = true;
  177. tag.UpdateChecked();
  178. _updating = false;
  179. FilterChanged?.Invoke(this);
  180. }
  181. }
  182. }
  183. public void ClearFilter()
  184. {
  185. if(SelectedFilters is null)
  186. {
  187. return;
  188. }
  189. SelectedFilters = null;
  190. _updating = true;
  191. foreach(var item in _items)
  192. {
  193. item.UpdateChecked();
  194. }
  195. _selectAll.UpdateChecked();
  196. _updating = false;
  197. FilterChanged?.Invoke(this);
  198. }
  199. public bool IsFiltered()
  200. {
  201. return SelectedFilters is not null;
  202. }
  203. }
  204. public class FuncCheckBoxDynamicGridColumnFilter(
  205. Func<CoreRow, Predicate<object?>, bool> filter,
  206. Func<IEnumerable<Tuple<string, object?>>> data) : CheckBoxDynamicGridColumnFilter
  207. {
  208. protected override FilterItem[] GetData()
  209. {
  210. return data().Select(x => new FilterItem(x.Item2, x.Item1, this)).ToArray();
  211. }
  212. public override bool FilterRow(CoreRow row)
  213. {
  214. return filter(row, x => SelectedFilters is null || SelectedFilters.Contains(x));
  215. }
  216. }
  217. public class StaticColumnFilter<T>(
  218. Func<CoreRow, T> rowData,
  219. Tuple<string, T>[] data) : CheckBoxDynamicGridColumnFilter
  220. {
  221. protected override FilterItem[] GetData()
  222. {
  223. return data.ToArray(x => new FilterItem(x.Item2, x.Item1, this));
  224. }
  225. public override bool FilterRow(CoreRow row)
  226. {
  227. return SelectedFilters is null || SelectedFilters.Contains(rowData(row));
  228. }
  229. }
  230. public class DynamicColumnFilter<T>(
  231. Func<CoreRow, T> rowData,
  232. Func<IEnumerable<Tuple<string, T>>> data) : CheckBoxDynamicGridColumnFilter
  233. {
  234. protected override FilterItem[] GetData()
  235. {
  236. return data().Select(x => new FilterItem(x.Item2, x.Item1, this)).ToArray();
  237. }
  238. public override bool FilterRow(CoreRow row)
  239. {
  240. return SelectedFilters is null || SelectedFilters.Contains(rowData(row));
  241. }
  242. }