revise structure

This commit is contained in:
Hongtao Zhang 2024-03-20 00:47:14 -05:00
parent 99ac5f010d
commit 2d805e53bc
2 changed files with 40 additions and 37 deletions

View file

@ -177,7 +177,8 @@ namespace Flow.Launcher.ViewModel
var token = e.Token == default ? _updateToken : e.Token;
PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query);
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(e.Results, pair.Metadata, e.Query, token)))
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(e.Results, pair.Metadata, e.Query,
token)))
{
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
}
@ -191,7 +192,8 @@ namespace Flow.Launcher.ViewModel
Hide();
await PluginManager.ReloadDataAsync().ConfigureAwait(false);
Notification.Show(InternationalizationManager.Instance.GetTranslation("success"), InternationalizationManager.Instance.GetTranslation("completedSuccessfully"));
Notification.Show(InternationalizationManager.Instance.GetTranslation("success"),
InternationalizationManager.Instance.GetTranslation("completedSuccessfully"));
}
[RelayCommand]
@ -221,8 +223,7 @@ namespace Flow.Launcher.ViewModel
{
if (SelectedIsFromQueryResults())
{
_reselect = reselect;
QueryResults(isReQuery: true);
QueryResults(isReQuery: true, reselect: reselect);
}
}
@ -275,6 +276,7 @@ namespace Flow.Launcher.ViewModel
{
autoCompleteText = $"{result.ActionKeywordAssigned} {defaultSuggestion}";
}
autoCompleteText = SelectedResults.SelectedItem.QuerySuggestionText;
}
@ -296,11 +298,13 @@ namespace Flow.Launcher.ViewModel
{
results.SelectedIndex = int.Parse(index);
}
var result = results.SelectedItem?.Result;
if (result == null)
{
return;
}
var hideWindow = await result.ExecuteAsync(new ActionContext
{
// not null means pressing modifier key + number, should ignore the modifier key
@ -413,6 +417,7 @@ namespace Flow.Launcher.ViewModel
public bool GameModeStatus { get; set; } = false;
private string _queryText;
public string QueryText
{
get => _queryText;
@ -436,6 +441,7 @@ namespace Flow.Launcher.ViewModel
Settings.WindowSize += 100;
Settings.WindowLeft -= 50;
}
OnPropertyChanged();
}
@ -451,6 +457,7 @@ namespace Flow.Launcher.ViewModel
Settings.WindowLeft += 50;
Settings.WindowSize -= 100;
}
OnPropertyChanged();
}
@ -530,18 +537,17 @@ namespace Flow.Launcher.ViewModel
{
if (QueryText != queryText)
{
// re-query is done in QueryText's setter method
QueryText = queryText;
// set to false so the subsequent set true triggers
// PropertyChanged and MoveQueryTextToEnd is called
QueryTextCursorMovedToEnd = false;
}
else if (isReQuery)
{
Query(isReQuery: true);
}
QueryTextCursorMovedToEnd = true;
});
}
@ -611,8 +617,8 @@ namespace Flow.Launcher.ViewModel
public string OpenResultCommandModifiers => Settings.OpenResultModifiers;
public string PreviewHotkey
{
public string PreviewHotkey
{
get
{
// TODO try to patch issue #1755
@ -626,6 +632,7 @@ namespace Flow.Launcher.ViewModel
{
Settings.PreviewHotkey = "F1";
}
return Settings.PreviewHotkey;
}
}
@ -694,7 +701,6 @@ namespace Flow.Launcher.ViewModel
results.Add(ContextMenuTopMost(selected));
results.Add(ContextMenuPluginInfo(selected.PluginID));
}
if (!string.IsNullOrEmpty(query))
@ -713,7 +719,6 @@ namespace Flow.Launcher.ViewModel
r.Score = match.Score;
return true;
}).ToList();
ContextMenu.AddResults(filtered, id);
}
@ -740,10 +745,7 @@ namespace Flow.Launcher.ViewModel
Title = string.Format(title, h.Query),
SubTitle = string.Format(time, h.ExecutedDateTime),
IcoPath = "Images\\history.png",
OriginQuery = new Query
{
RawQuery = h.Query
},
OriginQuery = new Query { RawQuery = h.Query },
Action = _ =>
{
SelectedResults = Results;
@ -771,7 +773,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();
@ -880,20 +882,23 @@ namespace Flow.Launcher.ViewModel
// Task.Yield will force it to run in ThreadPool
await Task.Yield();
IReadOnlyList<Result> results = await PluginManager.QueryForPluginAsync(plugin, query, currentCancellationToken);
IReadOnlyList<Result> results =
await PluginManager.QueryForPluginAsync(plugin, query, currentCancellationToken);
currentCancellationToken.ThrowIfCancellationRequested();
results ??= _emptyResult;
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query, currentCancellationToken)))
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query,
currentCancellationToken, reselect)))
{
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
}
}
}
private Query ConstructQuery(string queryText, IEnumerable<CustomShortcutModel> customShortcuts, IEnumerable<BuiltinShortcutModel> builtInShortcuts)
private Query ConstructQuery(string queryText, IEnumerable<CustomShortcutModel> customShortcuts,
IEnumerable<BuiltinShortcutModel> builtInShortcuts)
{
if (string.IsNullOrWhiteSpace(queryText))
{
@ -930,7 +935,9 @@ namespace Flow.Launcher.ViewModel
}
catch (Exception e)
{
Log.Exception($"{nameof(MainViewModel)}.{nameof(ConstructQuery)}|Error when expanding shortcut {shortcut.Key}", e);
Log.Exception(
$"{nameof(MainViewModel)}.{nameof(ConstructQuery)}|Error when expanding shortcut {shortcut.Key}",
e);
}
}
});
@ -1075,6 +1082,7 @@ namespace Flow.Launcher.ViewModel
{
SelectedResults = Results;
}
switch (Settings.LastQueryMode)
{
case LastQueryMode.Empty:
@ -1122,7 +1130,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;
@ -1161,7 +1169,10 @@ namespace Flow.Launcher.ViewModel
}
}
Results.AddResults(resultsForUpdates, token, _reselect);
// it should be the same for all results
bool reSelect = resultsForUpdates.First().ReSelectFirstResult;
Results.AddResults(resultsForUpdates, token, reSelect);
_reselect = true;
}

View file

@ -4,23 +4,15 @@ 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 string ID { get; } = Metadata.ID;
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 bool ReSelectFirstResult { get; set; }
}
}