Skip to content

Commit 6649961

Browse files
authored
Merge pull request #2581 from AminSallah/dev
New API Instance (ReQuery)
2 parents 0cb80c4 + 627a4dc commit 6649961

File tree

5 files changed

+38
-28
lines changed

5 files changed

+38
-28
lines changed

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,5 +291,12 @@ public interface IPublicAPI
291291
/// </summary>
292292
/// <returns></returns>
293293
public bool IsGameModeOn();
294+
295+
/// <summary>
296+
/// Reloads the query.
297+
/// This method should run
298+
/// </summary>
299+
/// <param name="reselect">Choose the first result after reload if true; keep the last selected result if false. Default is true.</param>
300+
public void ReQuery(bool reselect = true);
294301
}
295302
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ public bool IsGameModeOn()
315315
public void RegisterGlobalKeyboardCallback(Func<int, int, SpecialKeyState, bool> callback) => _globalKeyboardHandlers.Add(callback);
316316
public void RemoveGlobalKeyboardCallback(Func<int, int, SpecialKeyState, bool> callback) => _globalKeyboardHandlers.Remove(callback);
317317

318+
public void ReQuery(bool reselect = true) => _mainVM.ReQuery(reselect);
319+
318320
#endregion
319321

320322
#region Private Methods

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,22 @@ private void LoadHistory()
210210
}
211211

212212
[RelayCommand]
213-
private void ReQuery()
213+
public void ReQuery()
214214
{
215215
if (SelectedIsFromQueryResults())
216216
{
217217
QueryResults(isReQuery: true);
218218
}
219219
}
220220

221+
public void ReQuery(bool reselect)
222+
{
223+
if (SelectedIsFromQueryResults())
224+
{
225+
QueryResults(isReQuery: true, reSelect: reselect);
226+
}
227+
}
228+
221229
[RelayCommand]
222230
private void LoadContextMenu()
223231
{
@@ -775,7 +783,7 @@ private void QueryHistory()
775783

776784
private readonly IReadOnlyList<Result> _emptyResult = new List<Result>();
777785

778-
private async void QueryResults(bool isReQuery = false)
786+
private async void QueryResults(bool isReQuery = false, bool reSelect = true)
779787
{
780788
_updateSource?.Cancel();
781789

@@ -850,7 +858,7 @@ private async void QueryResults(bool isReQuery = false)
850858

851859
var tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
852860
{
853-
false => QueryTask(plugin),
861+
false => QueryTask(plugin, reSelect),
854862
true => Task.CompletedTask
855863
}).ToArray();
856864

@@ -878,7 +886,7 @@ private async void QueryResults(bool isReQuery = false)
878886
}
879887

880888
// Local function
881-
async Task QueryTask(PluginPair plugin)
889+
async Task QueryTask(PluginPair plugin, bool reSelect = true)
882890
{
883891
// Since it is wrapped within a ThreadPool Thread, the synchronous context is null
884892
// Task.Yield will force it to run in ThreadPool
@@ -892,7 +900,7 @@ async Task QueryTask(PluginPair plugin)
892900
results ??= _emptyResult;
893901

894902
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query,
895-
currentCancellationToken)))
903+
currentCancellationToken, reSelect)))
896904
{
897905
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
898906
}
@@ -1133,7 +1141,7 @@ public void Save()
11331141
/// <summary>
11341142
/// To avoid deadlock, this method should not called from main thread
11351143
/// </summary>
1136-
public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates)
1144+
public void UpdateResultView(ICollection<ResultsForUpdate> resultsForUpdates)
11371145
{
11381146
if (!resultsForUpdates.Any())
11391147
return;
@@ -1172,7 +1180,10 @@ public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates)
11721180
}
11731181
}
11741182

1175-
Results.AddResults(resultsForUpdates, token);
1183+
// it should be the same for all results
1184+
bool reSelect = resultsForUpdates.First().ReSelectFirstResult;
1185+
1186+
Results.AddResults(resultsForUpdates, token, reSelect);
11761187
}
11771188

11781189
#endregion

Flow.Launcher/ViewModel/ResultsForUpdate.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,13 @@
44

55
namespace Flow.Launcher.ViewModel
66
{
7-
public struct ResultsForUpdate
7+
public record struct ResultsForUpdate(
8+
IReadOnlyList<Result> Results,
9+
PluginMetadata Metadata,
10+
Query Query,
11+
CancellationToken Token,
12+
bool ReSelectFirstResult = true)
813
{
9-
public IReadOnlyList<Result> Results { get; }
10-
11-
public PluginMetadata Metadata { get; }
12-
public string ID { get; }
13-
14-
public Query Query { get; }
15-
public CancellationToken Token { get; }
16-
17-
public ResultsForUpdate(IReadOnlyList<Result> results, PluginMetadata metadata, Query query, CancellationToken token)
18-
{
19-
Results = results;
20-
Metadata = metadata;
21-
Query = query;
22-
Token = token;
23-
ID = metadata.ID;
24-
}
14+
public string ID { get; } = Metadata.ID;
2515
}
2616
}

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,23 @@ public void AddResults(List<Result> newRawResults, string resultId)
147147
/// <summary>
148148
/// To avoid deadlock, this method should not called from main thread
149149
/// </summary>
150-
public void AddResults(IEnumerable<ResultsForUpdate> resultsForUpdates, CancellationToken token)
150+
public void AddResults(IEnumerable<ResultsForUpdate> resultsForUpdates, CancellationToken token, bool reselect = true)
151151
{
152152
var newResults = NewResults(resultsForUpdates);
153153

154154
if (token.IsCancellationRequested)
155155
return;
156156

157-
UpdateResults(newResults, token);
157+
UpdateResults(newResults, token, reselect);
158158
}
159159

160-
private void UpdateResults(List<ResultViewModel> newResults, CancellationToken token = default)
160+
private void UpdateResults(List<ResultViewModel> newResults, CancellationToken token = default, bool reselect = true)
161161
{
162162
lock (_collectionLock)
163163
{
164164
// update UI in one run, so it can avoid UI flickering
165165
Results.Update(newResults, token);
166-
if (Results.Any())
166+
if (reselect && Results.Any())
167167
SelectedItem = Results[0];
168168
}
169169

0 commit comments

Comments
 (0)