mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge pull request #2581 from AminSallah/dev
New API Instance (ReQuery)
This commit is contained in:
commit
664996177d
5 changed files with 38 additions and 28 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue