Skip to content

Commit 529a219

Browse files
committed
Change serach delay method
1 parent bebe86d commit 529a219

File tree

3 files changed

+59
-96
lines changed

3 files changed

+59
-96
lines changed

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,6 @@ public bool SearchQueryResultsWithDelay
288288
}
289289
public int SearchDelay { get; set; } = 120;
290290

291-
// TODO: Remove debug codes.
292-
public const int SearchDelayInterval = 30 * 60;
293-
294291
[JsonIgnore]
295292
public List<int> SearchDelayRange { get; } = new()
296293
{

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public partial class MainWindow
5959
// Window Animation
6060
private const double DefaultRightMargin = 66; //* this value from base.xaml
6161
private bool _animating;
62-
private bool _isClockPanelAnimating = false; // 애니메이션 실행 중인지 여부
62+
private bool _isClockPanelAnimating = false;
63+
64+
// Search Delay
65+
private IDisposable _reactiveSubscription;
6366

6467
#endregion
6568

@@ -96,6 +99,9 @@ private void OnSourceInitialized(object sender, EventArgs e)
9699

97100
private async void OnLoaded(object sender, RoutedEventArgs _)
98101
{
102+
// Setup search text box reactiveness
103+
SetupSearchTextBoxReactiveness(_settings.SearchQueryResultsWithDelay);
104+
99105
// Check first launch
100106
if (_settings.FirstLaunch)
101107
{
@@ -144,6 +150,7 @@ private async void OnLoaded(object sender, RoutedEventArgs _)
144150
// Since the default main window visibility is visible, so we need set focus during startup
145151
QueryTextBox.Focus();
146152

153+
// View model property changed event
147154
_viewModel.PropertyChanged += (o, e) =>
148155
{
149156
switch (e.PropertyName)
@@ -194,6 +201,7 @@ private async void OnLoaded(object sender, RoutedEventArgs _)
194201
}
195202
};
196203

204+
// Settings property changed event
197205
_settings.PropertyChanged += (o, e) =>
198206
{
199207
switch (e.PropertyName)
@@ -917,7 +925,6 @@ private void UpdateClockPanelVisibility()
917925
}
918926
}
919927

920-
921928
private static double GetOpacityFromStyle(Style style, double defaultOpacity = 1.0)
922929
{
923930
if (style == null)
@@ -1001,8 +1008,6 @@ private void QueryTextBox_OnPreviewDragOver(object sender, DragEventArgs e)
10011008

10021009
// Edited from: https://github.com/microsoft/PowerToys
10031010

1004-
private IDisposable _reactiveSubscription;
1005-
10061011
private void SetupSearchTextBoxReactiveness(bool showResultsWithDelay)
10071012
{
10081013
if (_reactiveSubscription != null)
@@ -1020,17 +1025,7 @@ private void SetupSearchTextBoxReactiveness(bool showResultsWithDelay)
10201025
add => QueryTextBox.TextChanged += add,
10211026
remove => QueryTextBox.TextChanged -= remove)
10221027
.Throttle(TimeSpan.FromMilliseconds(_settings.SearchDelay * 10))
1023-
.Do(@event => Dispatcher.Invoke(() => PerformSearchQuery(0, (TextBox)@event.Sender)))
1024-
.Throttle(TimeSpan.FromMilliseconds(Settings.SearchDelayInterval))
1025-
.Do(@event => Dispatcher.Invoke(() => PerformSearchQuery(30, (TextBox)@event.Sender)))
1026-
.Throttle(TimeSpan.FromMilliseconds(Settings.SearchDelayInterval))
1027-
.Do(@event => Dispatcher.Invoke(() => PerformSearchQuery(60, (TextBox)@event.Sender)))
1028-
.Throttle(TimeSpan.FromMilliseconds(Settings.SearchDelayInterval))
1029-
.Do(@event => Dispatcher.Invoke(() => PerformSearchQuery(90, (TextBox)@event.Sender)))
1030-
.Throttle(TimeSpan.FromMilliseconds(Settings.SearchDelayInterval))
1031-
.Do(@event => Dispatcher.Invoke(() => PerformSearchQuery(120, (TextBox)@event.Sender)))
1032-
.Throttle(TimeSpan.FromMilliseconds(Settings.SearchDelayInterval))
1033-
.Do(@event => Dispatcher.Invoke(() => PerformSearchQuery(150, (TextBox)@event.Sender)))
1028+
.Do(@event => Dispatcher.Invoke(() => PerformSearchQuery(true, (TextBox)@event.Sender)))
10341029
.Subscribe();
10351030
}
10361031
else
@@ -1042,18 +1037,13 @@ private void SetupSearchTextBoxReactiveness(bool showResultsWithDelay)
10421037
private void QueryTextBox_TextChanged(object sender, TextChangedEventArgs e)
10431038
{
10441039
var textBox = (TextBox)sender;
1045-
PerformSearchQuery(null, textBox);
1040+
PerformSearchQuery(false, textBox);
10461041
}
10471042

1048-
// If delayInputTime is null, we will query plugins with all plugin search delay times
1049-
private void PerformSearchQuery(int? searchDelay, TextBox textBox)
1043+
private void PerformSearchQuery(bool searchDelay, TextBox textBox)
10501044
{
1051-
// Only update query text when search delay is null or 0
1052-
if (searchDelay.GetValueOrDefault(0) == 0)
1053-
{
1054-
var text = textBox.Text;
1055-
_viewModel.QueryText = text;
1056-
}
1045+
var text = textBox.Text;
1046+
_viewModel.QueryText = text;
10571047
_viewModel.Query(searchDelay);
10581048
}
10591049

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 45 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ public void ReQuery()
282282
{
283283
if (SelectedIsFromQueryResults())
284284
{
285-
_ = QueryResultsAsync(null, isReQuery: true);
285+
_ = QueryResultsAsync(false, isReQuery: true);
286286
}
287287
}
288288

289289
public void ReQuery(bool reselect)
290290
{
291291
BackToQueryResults();
292-
_ = QueryResultsAsync(null, isReQuery: true, reSelect: reselect);
292+
_ = QueryResultsAsync(false, isReQuery: true, reSelect: reselect);
293293
}
294294

295295
[RelayCommand]
@@ -622,14 +622,14 @@ public void ChangeQueryText(string queryText, bool isReQuery = false)
622622
{
623623
// re-query is done in QueryText's setter method
624624
QueryText = queryText;
625-
Query(null);
625+
Query(false);
626626
// set to false so the subsequent set true triggers
627627
// PropertyChanged and MoveQueryTextToEnd is called
628628
QueryTextCursorMovedToEnd = false;
629629
}
630630
else if (isReQuery)
631631
{
632-
Query(null, isReQuery: true);
632+
Query(false, isReQuery: true);
633633
}
634634

635635
QueryTextCursorMovedToEnd = true;
@@ -679,15 +679,8 @@ private ResultsViewModel SelectedResults
679679
// setter won't be called when property value is not changed.
680680
// so we need manually call Query()
681681
// http://stackoverflow.com/posts/25895769/revisions
682-
if (string.IsNullOrEmpty(QueryText))
683-
{
684-
Query(null);
685-
}
686-
else
687-
{
688-
QueryText = string.Empty;
689-
Query(null);
690-
}
682+
QueryText = string.Empty;
683+
Query(false);
691684
}
692685

693686
_selectedResults.Visibility = Visibility.Visible;
@@ -955,27 +948,19 @@ private bool CanExternalPreviewSelectedResult(out string path)
955948

956949
#region Query
957950

958-
public void Query(int? searchDelay, bool isReQuery = false)
951+
public void Query(bool searchDelay, bool isReQuery = false)
959952
{
960953
if (SelectedIsFromQueryResults())
961954
{
962955
_ = QueryResultsAsync(searchDelay, isReQuery);
963956
}
964957
else if (ContextMenuSelected())
965958
{
966-
// Only query history when search delay is null or 0
967-
if (searchDelay.GetValueOrDefault(0) == 0)
968-
{
969-
QueryContextMenu();
970-
}
959+
QueryContextMenu();
971960
}
972961
else if (HistorySelected())
973962
{
974-
// Only query history when search delay is null or 0
975-
if (searchDelay.GetValueOrDefault(0) == 0)
976-
{
977-
QueryHistory();
978-
}
963+
QueryHistory();
979964
}
980965
}
981966

@@ -1065,8 +1050,9 @@ private void QueryHistory()
10651050

10661051
private readonly IReadOnlyList<Result> _emptyResult = new List<Result>();
10671052

1068-
private async Task QueryResultsAsync(int? searchDelay, bool isReQuery = false, bool reSelect = true)
1053+
private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, bool reSelect = true)
10691054
{
1055+
// TODO: Remove debug codes.
10701056
System.Diagnostics.Debug.WriteLine("!!!QueryResults");
10711057

10721058
_updateSource?.Cancel();
@@ -1130,60 +1116,36 @@ private async Task QueryResultsAsync(int? searchDelay, bool isReQuery = false, b
11301116
}
11311117

11321118
_ = Task.Delay(200, _updateSource.Token).ContinueWith(_ =>
1133-
{
1134-
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
1135-
if (!_updateSource.Token.IsCancellationRequested && _isQueryRunning)
11361119
{
1137-
ProgressBarVisibility = Visibility.Visible;
1138-
}
1120+
// start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
1121+
if (!_updateSource.Token.IsCancellationRequested && _isQueryRunning)
1122+
{
1123+
ProgressBarVisibility = Visibility.Visible;
1124+
}
11391125
},
11401126
_updateSource.Token,
11411127
TaskContinuationOptions.NotOnCanceled,
11421128
TaskScheduler.Default);
11431129

11441130
// plugins is ICollection, meaning LINQ will get the Count and preallocate Array
11451131

1146-
Task[] tasks;
1147-
if (searchDelay.HasValue)
1132+
var tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
11481133
{
1149-
var searchDelayValue = searchDelay.Value;
1150-
tasks = plugins.Select(plugin => (plugin.Metadata.Disabled || plugin.Metadata.SearchDelay != searchDelayValue) switch
1151-
{
1152-
false => QueryTaskAsync(plugin, reSelect),
1153-
true => Task.CompletedTask
1154-
}).ToArray();
1134+
false => QueryTaskAsync(plugin, searchDelay, reSelect, _updateSource.Token),
1135+
true => Task.CompletedTask
1136+
}).ToArray();
11551137

1156-
// TODO: Remove debug codes.
1157-
System.Diagnostics.Debug.Write($"!!!{query.RawQuery} Querying {searchDelayValue}ms");
1158-
foreach (var plugin in plugins)
1159-
{
1160-
if (!(plugin.Metadata.Disabled || plugin.Metadata.SearchDelay != searchDelayValue))
1161-
{
1162-
System.Diagnostics.Debug.Write($"{plugin.Metadata.Name}");
1163-
}
1164-
}
1165-
System.Diagnostics.Debug.Write("\n");
1166-
}
1167-
else
1138+
// TODO: Remove debug codes.
1139+
System.Diagnostics.Debug.Write($"!!!Querying {query.RawQuery}: search dalay {searchDelay}");
1140+
foreach (var plugin in plugins)
11681141
{
1169-
tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
1170-
{
1171-
false => QueryTaskAsync(plugin, reSelect),
1172-
true => Task.CompletedTask
1173-
}).ToArray();
1174-
1175-
// TODO: Remove debug codes.
1176-
System.Diagnostics.Debug.Write($"!!!{query.RawQuery} Querying null ms");
1177-
foreach (var plugin in plugins)
1142+
if (!plugin.Metadata.Disabled)
11781143
{
1179-
if (!plugin.Metadata.Disabled)
1180-
{
1181-
System.Diagnostics.Debug.Write($"{plugin.Metadata.Name}");
1182-
}
1144+
System.Diagnostics.Debug.Write($"{plugin.Metadata.Name}, ");
11831145
}
1184-
System.Diagnostics.Debug.Write("\n");
11851146
}
1186-
1147+
System.Diagnostics.Debug.Write("\n");
1148+
11871149
try
11881150
{
11891151
// Check the code, WhenAll will translate all type of IEnumerable or Collection to Array, so make an array at first
@@ -1207,15 +1169,29 @@ private async Task QueryResultsAsync(int? searchDelay, bool isReQuery = false, b
12071169
}
12081170

12091171
// Local function
1210-
async Task QueryTaskAsync(bool searchDelay, PluginPair plugin, bool reSelect, CancellationToken token)
1211-
{
1212-
if (!searchDelay)
1172+
async Task QueryTaskAsync(PluginPair plugin, bool searchDelay, bool reSelect, CancellationToken token)
12131173
{
1174+
if (searchDelay)
1175+
{
1176+
// TODO: Remove debug codes.
1177+
System.Diagnostics.Debug.WriteLine($"!!!{plugin.Metadata.Name} Waiting {plugin.Metadata.SearchDelay} ms");
1178+
1179+
// TODO: Remove debug codes.
1180+
await Task.Delay(plugin.Metadata.SearchDelay * 60, token);
1181+
1182+
// TODO: Remove debug codes.
1183+
System.Diagnostics.Debug.WriteLine($"!!!{plugin.Metadata.Name} Waited {plugin.Metadata.SearchDelay} ms");
1184+
1185+
if (token.IsCancellationRequested)
1186+
return;
1187+
}
1188+
12141189
// Since it is wrapped within a ThreadPool Thread, the synchronous context is null
12151190
// Task.Yield will force it to run in ThreadPool
12161191
await Task.Yield();
1217-
}
12181192

1193+
// TODO: Remove debug codes.
1194+
System.Diagnostics.Debug.WriteLine($"!!!{query.RawQuery} Querying {plugin.Metadata.Name}");
12191195
IReadOnlyList<Result> results =
12201196
await PluginManager.QueryForPluginAsync(plugin, query, token);
12211197

0 commit comments

Comments
 (0)