refactor (ui): Enable auto-refresh in column-text filters
feat (mview): Enable auto-refresh in security filter This behavior is (more) consistent with other modern search uis
This commit is contained in:
1 parent
95595c4cbf
commit
e00eae1129
4 files changed
+97
-34
No files matched your search
@@ -67,6 +67,7 @@
|
||||
DiscreteValuesCollectionView = "DiscreteValuesCollectionViewTemplate",
|
||||
EnumValues = "EnumValuesTemplate",
|
||||
Regex = "RegexTemplate",
|
||||
RegexAutoRefresh = "RegexAutoRefreshTemplate",
|
||||
YesNo = "YesNoTemplate";
|
||||
public const string ExpirationDate = "ExpirationDate";
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using Spark.Core;
|
||||
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace mbts.spark.filters {
|
||||
static class RegexFilterUtils {
|
||||
@@ -13,16 +16,45 @@ namespace mbts.spark.filters {
|
||||
}
|
||||
}
|
||||
class RegexFilterVM<TRow> : FilterVMBase<TRow, string, string> {
|
||||
readonly DeferrableAction _deferrable_refresh;
|
||||
|
||||
string _text = string.Empty;
|
||||
Regex _regex;
|
||||
TimeSpan? _refresh_interval;
|
||||
|
||||
public RegexFilterVM(Func<TRow, string> get_row_val, string prop_name = null) : base(get_row_val, prop_name) { }
|
||||
public RegexFilterVM(Func<TRow, string> get_row_val, string prop_name = null)
|
||||
: base(get_row_val, prop_name) {
|
||||
_deferrable_refresh = new(invoke_deferred_refresh);
|
||||
refresh_interval = TimeSpan.FromSeconds(0.4);
|
||||
|
||||
void invoke_deferred_refresh() {
|
||||
_raise_filter_changed();
|
||||
}
|
||||
}
|
||||
|
||||
public string text {
|
||||
get => _text;
|
||||
set => _regex = RegexFilterUtils.build_regex(_text = (value ?? string.Empty));
|
||||
set => _on_text_input(value);
|
||||
}
|
||||
|
||||
public TimeSpan? refresh_interval {
|
||||
get => _refresh_interval;
|
||||
set {
|
||||
var had_value = _refresh_interval.HasValue;
|
||||
if (!Swap.toggle_to(ref _refresh_interval, value)) {
|
||||
return;
|
||||
}
|
||||
if (value.HasValue && !had_value && _deferrable_refresh.is_execution_deferred) {
|
||||
// Flush deferred (delayed) refreshes immediately
|
||||
_deferrable_refresh.cancel();
|
||||
_raise_filter_changed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IDisposable supress_auto_refresh() =>
|
||||
_deferrable_refresh.supress();
|
||||
|
||||
public override string get_state() => _text ?? string.Empty;
|
||||
|
||||
protected override bool _get_active() => _regex != null;
|
||||
@@ -31,7 +63,11 @@ namespace mbts.spark.filters {
|
||||
_regex.IsMatch(value) ||
|
||||
(!string.IsNullOrEmpty(value) && _regex.IsMatch(value.Replace(" ", ""))));
|
||||
|
||||
protected override void _load_state_internal(string state) => text = state;
|
||||
protected override void _load_state_internal(string state) {
|
||||
_text = state ?? string.Empty;
|
||||
_regex = RegexFilterUtils.build_regex(_text);
|
||||
}
|
||||
|
||||
protected override void _clear() {
|
||||
_close();
|
||||
_text = string.Empty;
|
||||
@@ -39,5 +75,24 @@ namespace mbts.spark.filters {
|
||||
RaisePropertyChanged(nameof(text));
|
||||
_raise_filter_changed();
|
||||
}
|
||||
|
||||
void _on_text_input(string value) {
|
||||
if (_text == value) {
|
||||
return;
|
||||
}
|
||||
_load_state_internal(value);
|
||||
if (!_refresh_interval.HasValue) {
|
||||
return;
|
||||
}
|
||||
delay_refresh();
|
||||
|
||||
async void delay_refresh() {
|
||||
using var _ = _deferrable_refresh.defer();
|
||||
/// Invoke the refresh before the delay, so the fact that a refresh is scheduled can be
|
||||
/// seen via <see cref="DeferrableAction.is_execution_deferred"/>
|
||||
_deferrable_refresh.Invoke();
|
||||
await Task.Delay(_refresh_interval.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -250,36 +250,25 @@
|
||||
</usp:FilterContentControl>
|
||||
</DataTemplate>
|
||||
<DataTemplate x:Key="RegexTemplate">
|
||||
<usp:FilterContentControl
|
||||
ApplyCommandParameter="{Binding}"
|
||||
ApplyCommand="{x:Static c:DataGridFilterProperties.CommitCommand}"
|
||||
ClearCommandParameter="{Binding}"
|
||||
ClearCommand="{x:Static c:DataGridFilterProperties.ClearCommand}"
|
||||
>
|
||||
<Grid Width="160">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Text="Search"/>
|
||||
<TextBox
|
||||
x:Name="Filter"
|
||||
Grid.Column="1" Margin="5,0,0,0"
|
||||
HorizontalAlignment="Stretch"
|
||||
Text="{Binding text}"
|
||||
u:Behavior.FocusOnLoaded="True"
|
||||
u:TextBoxBehavior.CommitOnEnter="True"
|
||||
>
|
||||
<u:Behavior.BindingUpdateGroups>
|
||||
<u:BindingUpdateGroupCollection>
|
||||
<u:BindingUpdateGroup Source="{Binding DataContext, Source={u:DirectReference Filter}}" Target="{Binding Source={u:DirectReference Filter}}">
|
||||
<u:PropertyTuple PropertyName="Text" Type="{x:Type TextBox}"/>
|
||||
</u:BindingUpdateGroup>
|
||||
</u:BindingUpdateGroupCollection>
|
||||
</u:Behavior.BindingUpdateGroups>
|
||||
</TextBox>
|
||||
</Grid>
|
||||
</usp:FilterContentControl>
|
||||
<Grid Width="160">
|
||||
<TextBox
|
||||
x:Name="Filter"
|
||||
Grid.Column="1"
|
||||
Margin="5,3"
|
||||
HorizontalAlignment="Stretch"
|
||||
Text="{Binding text, UpdateSourceTrigger=PropertyChanged}"
|
||||
u:TextBoxBehavior.TextPrompt="Search (press 'Enter' to keep)"
|
||||
u:Behavior.FocusOnLoaded="True"
|
||||
u:TextBoxBehavior.CommitOnEnter="True">
|
||||
<u:Behavior.BindingUpdateGroups>
|
||||
<u:BindingUpdateGroupCollection>
|
||||
<u:BindingUpdateGroup Source="{Binding DataContext, Source={u:DirectReference Filter}}" Target="{Binding Source={u:DirectReference Filter}}">
|
||||
<u:PropertyTuple PropertyName="Text" Type="{x:Type TextBox}"/>
|
||||
</u:BindingUpdateGroup>
|
||||
</u:BindingUpdateGroupCollection>
|
||||
</u:Behavior.BindingUpdateGroups>
|
||||
</TextBox>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
<DataTemplate x:Key="SecurityCriteriaTemmplate">
|
||||
<msp:ContractPickerView DataContext="{Binding vm}" CloseOnOK="False"/>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#nullable enable
|
||||
|
||||
namespace Spark.Core {
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Swap {
|
||||
public static bool toggle_to<T>(ref T target, T value) =>
|
||||
toggle_to(ref target, value, EqualityComparer<T>.Default);
|
||||
|
||||
public static bool toggle_to<T>(ref T target, T value, IEqualityComparer<T> comparer) {
|
||||
if (comparer.Equals(target, value)) {
|
||||
return false;
|
||||
}
|
||||
target = value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user