|
1 | 1 | using System.ComponentModel; |
2 | 2 | using System.Linq; |
3 | 3 | using System.Text.RegularExpressions; |
| 4 | +using System.Windows; |
4 | 5 |
|
5 | 6 | namespace SimpleDataGrid.Pagination; |
6 | 7 |
|
@@ -33,12 +34,30 @@ public int PageSize |
33 | 34 | private Func<T, string>? _searchSelector; |
34 | 35 | private string? _searchTerm; |
35 | 36 | private bool _useWildcards; |
| 37 | + private System.Threading.Timer? _debounceTimer; |
| 38 | + private bool _isSearching; |
36 | 39 |
|
37 | 40 | /// <summary> |
38 | 41 | /// Gets a value indicating whether the collection is sorted. |
39 | 42 | /// </summary> |
40 | 43 | public bool IsSorted => _sorts.Count > 0; |
41 | 44 |
|
| 45 | + /// <summary> |
| 46 | + /// Gets a value indicating whether a search operation is currently in progress. |
| 47 | + /// </summary> |
| 48 | + public bool IsSearching |
| 49 | + { |
| 50 | + get => _isSearching; |
| 51 | + private set |
| 52 | + { |
| 53 | + if (_isSearching != value) |
| 54 | + { |
| 55 | + _isSearching = value; |
| 56 | + OnPropertyChanged(nameof(IsSearching)); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
42 | 61 | /// <summary> |
43 | 62 | /// Occurs when the sorting changes. |
44 | 63 | /// </summary> |
@@ -142,11 +161,38 @@ public void ClearFilters() |
142 | 161 | /// <param name="selector">A function that returns the string representation of the object to search.</param> |
143 | 162 | /// <param name="term">The search term.</param> |
144 | 163 | /// <param name="useWildcards">A value indicating whether to use wildcards in the search term.</param> |
145 | | - public void SetSearch(Func<T, string> selector, string? term, bool useWildcards = false) |
| 164 | + /// <param name="debounceMilliseconds">Optional. The number of milliseconds to debounce the search. If 0, no debouncing occurs.</param> |
| 165 | + public void SetSearch(Func<T, string> selector, string? term, bool useWildcards = false, int debounceMilliseconds = 0) |
146 | 166 | { |
147 | 167 | _searchSelector = selector; |
148 | 168 | _searchTerm = term; |
149 | 169 | _useWildcards = useWildcards; |
| 170 | + |
| 171 | + if (debounceMilliseconds > 0) |
| 172 | + { |
| 173 | + IsSearching = true; |
| 174 | + _debounceTimer?.Dispose(); |
| 175 | + _debounceTimer = new System.Threading.Timer(_ => |
| 176 | + { |
| 177 | + System.Windows.Application.Current.Dispatcher.Invoke(ApplyFiltering); |
| 178 | + IsSearching = false; |
| 179 | + }, null, debounceMilliseconds, System.Threading.Timeout.Infinite); |
| 180 | + } |
| 181 | + else |
| 182 | + { |
| 183 | + ApplyFiltering(); |
| 184 | + SearchChanged?.Invoke(this, EventArgs.Empty); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + /// <summary> |
| 189 | + /// Clears the search criteria from the collection. |
| 190 | + /// </summary> |
| 191 | + public void ClearSearch() |
| 192 | + { |
| 193 | + _searchSelector = null; |
| 194 | + _searchTerm = null; |
| 195 | + _useWildcards = false; |
150 | 196 | ApplyFiltering(); |
151 | 197 | SearchChanged?.Invoke(this, EventArgs.Empty); |
152 | 198 | } |
|
0 commit comments