StaticColumnFilter.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.Height = 25;
  89. box.VerticalAlignment = VerticalAlignment.Center;
  90. box.VerticalContentAlignment = VerticalAlignment.Center;
  91. box.HorizontalAlignment = HorizontalAlignment.Stretch;
  92. box.Bind<FilterItem, string?>(
  93. CheckBox.ContentProperty, x => x.Display,
  94. mode: BindingMode.OneWay);
  95. box.Bind<FilterItem, bool>(
  96. CheckBox.IsCheckedProperty, x => x.IsChecked,
  97. mode: BindingMode.OneWay);
  98. box.Bind<FilterItem, object?>(CheckBox.TagProperty, x => x);
  99. box.Checked += Box_Checked;
  100. box.Unchecked += Box_Unchecked;
  101. return box;
  102. });
  103. itemList.HorizontalContentAlignment = HorizontalAlignment.Stretch;
  104. itemList.VerticalContentAlignment = VerticalAlignment.Center;
  105. itemList.ItemsSource = CoreUtils.One(_selectAll).Concat(_items);
  106. searchBox.TextChanged += (o, e) =>
  107. {
  108. itemList.ItemsSource = CoreUtils.One(_selectAll)
  109. .Concat(_items.Where(x => x.Display.Contains(searchBox.Text, StringComparison.CurrentCultureIgnoreCase)));
  110. };
  111. grid.AddChild(searchBox, 0, 0);
  112. grid.AddChild(itemList, 1, 0);
  113. return grid;
  114. }
  115. private bool _updating = false;
  116. private void Box_Unchecked(object sender, RoutedEventArgs e)
  117. {
  118. if (_updating || sender is not CheckBox box || box.Tag is not FilterItem tag) return;
  119. if(box.Tag == _selectAll)
  120. {
  121. if(SelectedFilters is null || SelectedFilters.Count > 0)
  122. {
  123. SelectedFilters = new();
  124. _updating = true;
  125. foreach(var item in _items)
  126. {
  127. item.UpdateChecked();
  128. }
  129. _selectAll.UpdateChecked();
  130. _updating = false;
  131. FilterChanged?.Invoke(this);
  132. }
  133. }
  134. else
  135. {
  136. if(SelectedFilters is null)
  137. {
  138. SelectedFilters = _items.Select(x => x.Value).ToHashSet();
  139. SelectedFilters.Remove(tag.Value);
  140. _updating = true;
  141. tag.UpdateChecked();
  142. _selectAll.UpdateChecked();
  143. _updating = false;
  144. FilterChanged?.Invoke(this);
  145. }
  146. else if(SelectedFilters.Remove(tag.Value))
  147. {
  148. _updating = true;
  149. tag.UpdateChecked();
  150. _selectAll.UpdateChecked();
  151. _updating = false;
  152. FilterChanged?.Invoke(this);
  153. }
  154. }
  155. }
  156. private void Box_Checked(object sender, RoutedEventArgs e)
  157. {
  158. if (_updating || sender is not CheckBox box || box.Tag is not FilterItem tag) return;
  159. if(tag == _selectAll)
  160. {
  161. if(SelectedFilters is not null)
  162. {
  163. SelectedFilters = null;
  164. _updating = true;
  165. foreach(var item in _items)
  166. {
  167. item.UpdateChecked();
  168. }
  169. _selectAll.UpdateChecked();
  170. _updating = false;
  171. FilterChanged?.Invoke(this);
  172. }
  173. }
  174. else
  175. {
  176. if(SelectedFilters is not null && SelectedFilters.Add(tag.Value))
  177. {
  178. _updating = true;
  179. tag.UpdateChecked();
  180. _updating = false;
  181. FilterChanged?.Invoke(this);
  182. }
  183. }
  184. }
  185. public void ClearFilter()
  186. {
  187. if(SelectedFilters is null)
  188. {
  189. return;
  190. }
  191. SelectedFilters = null;
  192. _updating = true;
  193. foreach(var item in _items)
  194. {
  195. item.UpdateChecked();
  196. }
  197. _selectAll.UpdateChecked();
  198. _updating = false;
  199. FilterChanged?.Invoke(this);
  200. }
  201. public bool IsFiltered()
  202. {
  203. return SelectedFilters is not null;
  204. }
  205. }
  206. public class FuncCheckBoxDynamicGridColumnFilter(
  207. Func<CoreRow, Predicate<object?>, bool> filter,
  208. Func<IEnumerable<Tuple<string, object?>>> data) : CheckBoxDynamicGridColumnFilter
  209. {
  210. protected override FilterItem[] GetData()
  211. {
  212. return data().Select(x => new FilterItem(x.Item2, x.Item1, this)).ToArray();
  213. }
  214. public override bool FilterRow(CoreRow row)
  215. {
  216. return filter(row, x => SelectedFilters is null || SelectedFilters.Contains(x));
  217. }
  218. }
  219. public class StaticColumnFilter<T>(
  220. Func<CoreRow, T> rowData,
  221. Tuple<string, T>[] data) : CheckBoxDynamicGridColumnFilter
  222. {
  223. protected override FilterItem[] GetData()
  224. {
  225. return data.ToArray(x => new FilterItem(x.Item2, x.Item1, this));
  226. }
  227. public override bool FilterRow(CoreRow row)
  228. {
  229. return SelectedFilters is null || SelectedFilters.Contains(rowData(row));
  230. }
  231. }
  232. public class DynamicColumnFilter<T>(
  233. Func<CoreRow, T> rowData,
  234. Func<DynamicColumnFilter<T>, IEnumerable<Tuple<string, T>>> data) : CheckBoxDynamicGridColumnFilter
  235. {
  236. protected override FilterItem[] GetData()
  237. {
  238. return data(this).Select(x => new FilterItem(x.Item2, x.Item1, this)).ToArray();
  239. }
  240. public override bool FilterRow(CoreRow row)
  241. {
  242. return SelectedFilters is null || SelectedFilters.Contains(rowData(row));
  243. }
  244. }