Skip to content

Commit a49c465

Browse files
committed
Add async buildin shortcuts in mainvm
1 parent e0a651c commit a49c465

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ private async Task QueryResultsAsync(bool isReQuery = false, bool reSelect = tru
11551155
{
11561156
_updateSource?.Cancel();
11571157

1158-
var query = ConstructQuery(QueryText, Settings.CustomShortcuts, Settings.BuiltinShortcuts);
1158+
var query = await ConstructQueryAsync(QueryText, Settings.CustomShortcuts, Settings.BuiltinShortcuts);
11591159

11601160
if (query == null) // shortcut expanded
11611161
{
@@ -1284,8 +1284,8 @@ async Task QueryTaskAsync(PluginPair plugin, bool reSelect = true)
12841284
}
12851285
}
12861286

1287-
private Query ConstructQuery(string queryText, IEnumerable<CustomShortcutModel> customShortcuts,
1288-
IEnumerable<BuiltinShortcutModel> builtInShortcuts)
1287+
private async Task<Query> ConstructQueryAsync(string queryText, IEnumerable<CustomShortcutModel> customShortcuts,
1288+
IEnumerable<BaseBuiltinShortcutModel> builtInShortcuts)
12891289
{
12901290
if (string.IsNullOrWhiteSpace(queryText))
12911291
{
@@ -1306,36 +1306,48 @@ private Query ConstructQuery(string queryText, IEnumerable<CustomShortcutModel>
13061306
queryBuilder.Replace('@' + shortcut.Key, shortcut.Expand());
13071307
}
13081308

1309-
string customExpanded = queryBuilder.ToString();
1309+
var customExpanded = queryBuilder.ToString();
1310+
var queryChanged = false;
13101311

1311-
Application.Current.Dispatcher.Invoke(() =>
1312+
foreach (var shortcut in builtInShortcuts)
13121313
{
1313-
foreach (var shortcut in builtInShortcuts)
1314+
string expansion;
1315+
if (shortcut is BuiltinShortcutModel syncShortcut)
13141316
{
1315-
try
1316-
{
1317-
if (customExpanded.Contains(shortcut.Key))
1318-
{
1319-
var expansion = shortcut.Expand();
1320-
queryBuilder.Replace(shortcut.Key, expansion);
1321-
queryBuilderTmp.Replace(shortcut.Key, expansion);
1322-
}
1323-
}
1324-
catch (Exception e)
1317+
expansion = syncShortcut.Expand();
1318+
}
1319+
else if (shortcut is AsyncBuiltinShortcutModel asyncShortcut)
1320+
{
1321+
expansion = await asyncShortcut.ExpandAsync();
1322+
}
1323+
else
1324+
{
1325+
continue;
1326+
}
1327+
try
1328+
{
1329+
if (customExpanded.Contains(shortcut.Key))
13251330
{
1326-
Log.Exception(
1327-
$"{nameof(MainViewModel)}.{nameof(ConstructQuery)}|Error when expanding shortcut {shortcut.Key}",
1328-
e);
1331+
queryBuilder.Replace(shortcut.Key, expansion);
1332+
queryBuilderTmp.Replace(shortcut.Key, expansion);
1333+
queryChanged = true;
13291334
}
13301335
}
1331-
});
1336+
catch (Exception e)
1337+
{
1338+
App.API.LogException(nameof(MainViewModel), $"Error when expanding shortcut {shortcut.Key}", e);
1339+
}
1340+
}
13321341

1333-
// show expanded builtin shortcuts
1334-
// use private field to avoid infinite recursion
1335-
_queryText = queryBuilderTmp.ToString();
1342+
if (queryChanged)
1343+
{
1344+
// show expanded builtin shortcuts
1345+
// use private field to avoid infinite recursion
1346+
_queryText = queryBuilderTmp.ToString();
1347+
OnPropertyChanged(nameof(QueryText));
1348+
}
13361349

1337-
var query = QueryBuilder.Build(queryBuilder.ToString().Trim(), PluginManager.NonGlobalPlugins);
1338-
return query;
1350+
return QueryBuilder.Build(queryBuilder.ToString().Trim(), PluginManager.NonGlobalPlugins);
13391351
}
13401352

13411353
private void RemoveOldQueryResults(Query query)

0 commit comments

Comments
 (0)