Merge pull request #2581 from AminSallah/dev

New API Instance (ReQuery)
This commit is contained in:
Kevin Zhang 2024-03-31 01:37:46 -05:00 committed by GitHub
commit 664996177d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 38 additions and 28 deletions

View file

@ -291,5 +291,12 @@ namespace Flow.Launcher.Plugin
/// </summary>
/// <returns></returns>
public bool IsGameModeOn();
/// <summary>
/// Reloads the query.
/// This method should run
/// </summary>
/// <param name="reselect">Choose the first result after reload if true; keep the last selected result if false. Default is true.</param>
public void ReQuery(bool reselect = true);
}
}

View file

@ -315,6 +315,8 @@ namespace Flow.Launcher
public void RegisterGlobalKeyboardCallback(Func<int, int, SpecialKeyState, bool> callback) => _globalKeyboardHandlers.Add(callback);
public void RemoveGlobalKeyboardCallback(Func<int, int, SpecialKeyState, bool> callback) => _globalKeyboardHandlers.Remove(callback);
public void ReQuery(bool reselect = true) => _mainVM.ReQuery(reselect);
#endregion
#region Private Methods

View file

@ -210,7 +210,7 @@ namespace Flow.Launcher.ViewModel
}
[RelayCommand]
private void ReQuery()
public void ReQuery()
{
if (SelectedIsFromQueryResults())
{
@ -218,6 +218,14 @@ namespace Flow.Launcher.ViewModel
}
}
public void ReQuery(bool reselect)
{
if (SelectedIsFromQueryResults())
{
QueryResults(isReQuery: true, reSelect: reselect);
}
}
[RelayCommand]
private void LoadContextMenu()
{
@ -775,7 +783,7 @@ namespace Flow.Launcher.ViewModel
private readonly IReadOnlyList<Result> _emptyResult = new List<Result>();
private async void QueryResults(bool isReQuery = false)
private async void QueryResults(bool isReQuery = false, bool reSelect = true)
{
_updateSource?.Cancel();
@ -850,7 +858,7 @@ namespace Flow.Launcher.ViewModel
var tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
{
false => QueryTask(plugin),
false => QueryTask(plugin, reSelect),
true => Task.CompletedTask
}).ToArray();
@ -878,7 +886,7 @@ namespace Flow.Launcher.ViewModel
}
// Local function
async Task QueryTask(PluginPair plugin)
async Task QueryTask(PluginPair plugin, bool reSelect = true)
{
// Since it is wrapped within a ThreadPool Thread, the synchronous context is null
// Task.Yield will force it to run in ThreadPool
@ -892,7 +900,7 @@ namespace Flow.Launcher.ViewModel
results ??= _emptyResult;
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query,
currentCancellationToken)))
currentCancellationToken, reSelect)))
{
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
}
@ -1133,7 +1141,7 @@ namespace Flow.Launcher.ViewModel
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </summary>
public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates)
public void UpdateResultView(ICollection<ResultsForUpdate> resultsForUpdates)
{
if (!resultsForUpdates.Any())
return;
@ -1172,7 +1180,10 @@ namespace Flow.Launcher.ViewModel
}
}
Results.AddResults(resultsForUpdates, token);
// it should be the same for all results
bool reSelect = resultsForUpdates.First().ReSelectFirstResult;
Results.AddResults(resultsForUpdates, token, reSelect);
}
#endregion

View file

@ -4,23 +4,13 @@ using System.Threading;
namespace Flow.Launcher.ViewModel
{
public struct ResultsForUpdate
public record struct ResultsForUpdate(
IReadOnlyList<Result> Results,
PluginMetadata Metadata,
Query Query,
CancellationToken Token,
bool ReSelectFirstResult = true)
{
public IReadOnlyList<Result> Results { get; }
public PluginMetadata Metadata { get; }
public string ID { get; }
public Query Query { get; }
public CancellationToken Token { get; }
public ResultsForUpdate(IReadOnlyList<Result> results, PluginMetadata metadata, Query query, CancellationToken token)
{
Results = results;
Metadata = metadata;
Query = query;
Token = token;
ID = metadata.ID;
}
public string ID { get; } = Metadata.ID;
}
}

View file

@ -147,23 +147,23 @@ namespace Flow.Launcher.ViewModel
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </summary>
public void AddResults(IEnumerable<ResultsForUpdate> resultsForUpdates, CancellationToken token)
public void AddResults(IEnumerable<ResultsForUpdate> resultsForUpdates, CancellationToken token, bool reselect = true)
{
var newResults = NewResults(resultsForUpdates);
if (token.IsCancellationRequested)
return;
UpdateResults(newResults, token);
UpdateResults(newResults, token, reselect);
}
private void UpdateResults(List<ResultViewModel> newResults, CancellationToken token = default)
private void UpdateResults(List<ResultViewModel> newResults, CancellationToken token = default, bool reselect = true)
{
lock (_collectionLock)
{
// update UI in one run, so it can avoid UI flickering
Results.Update(newResults, token);
if (Results.Any())
if (reselect && Results.Any())
SelectedItem = Results[0];
}