Skip to content

Commit 882b7dc

Browse files
committed
Check ui thread access when calling ui thread
1 parent bd1da94 commit 882b7dc

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

Diff for: Flow.Launcher/ViewModel/MainViewModel.cs

+47-37
Original file line numberDiff line numberDiff line change
@@ -1331,53 +1331,63 @@ private Query ConstructQuery(string queryText, IEnumerable<CustomShortcutModel>
13311331
queryBuilder.Replace('@' + shortcut.Key, shortcut.Expand());
13321332
}
13331333

1334-
var customExpanded = queryBuilder.ToString();
1334+
// Applying builtin shortcuts
1335+
BuildQuery(builtInShortcuts, queryBuilder, queryBuilderTmp);
13351336

1336-
// We must use dispatcher because text here will be used in TextBox
1337-
Application.Current?.Dispatcher.Invoke(() =>
1337+
return QueryBuilder.Build(queryBuilder.ToString().Trim(), PluginManager.NonGlobalPlugins);
1338+
}
1339+
1340+
// We must use dispatcher because text here will be used in TextBox
1341+
private void BuildQuery(IEnumerable<BaseBuiltinShortcutModel> builtInShortcuts,
1342+
StringBuilder queryBuilder, StringBuilder queryBuilderTmp)
1343+
{
1344+
if (!Application.Current.Dispatcher.CheckAccess())
13381345
{
1339-
var queryChanged = false;
1346+
Application.Current.Dispatcher.Invoke(() => BuildQuery(builtInShortcuts, queryBuilder, queryBuilderTmp));
1347+
return;
1348+
}
13401349

1341-
foreach (var shortcut in builtInShortcuts)
1350+
var customExpanded = queryBuilder.ToString();
1351+
1352+
var queryChanged = false;
1353+
1354+
foreach (var shortcut in builtInShortcuts)
1355+
{
1356+
string expansion;
1357+
if (shortcut is BuiltinShortcutModel syncShortcut)
13421358
{
1343-
string expansion;
1344-
if (shortcut is BuiltinShortcutModel syncShortcut)
1345-
{
1346-
expansion = syncShortcut.Expand();
1347-
}
1348-
else if (shortcut is AsyncBuiltinShortcutModel asyncShortcut)
1349-
{
1350-
expansion = App.JTF.Run(() => asyncShortcut.ExpandAsync());
1351-
}
1352-
else
1353-
{
1354-
continue;
1355-
}
1356-
try
1357-
{
1358-
if (customExpanded.Contains(shortcut.Key))
1359-
{
1360-
queryBuilder.Replace(shortcut.Key, expansion);
1361-
queryBuilderTmp.Replace(shortcut.Key, expansion);
1362-
queryChanged = true;
1363-
}
1364-
}
1365-
catch (Exception e)
1359+
expansion = syncShortcut.Expand();
1360+
}
1361+
else if (shortcut is AsyncBuiltinShortcutModel asyncShortcut)
1362+
{
1363+
expansion = App.JTF.Run(() => asyncShortcut.ExpandAsync());
1364+
}
1365+
else
1366+
{
1367+
continue;
1368+
}
1369+
try
1370+
{
1371+
if (customExpanded.Contains(shortcut.Key))
13661372
{
1367-
App.API.LogException(ClassName, $"Error when expanding shortcut {shortcut.Key}", e);
1373+
queryBuilder.Replace(shortcut.Key, expansion);
1374+
queryBuilderTmp.Replace(shortcut.Key, expansion);
1375+
queryChanged = true;
13681376
}
13691377
}
1370-
1371-
if (queryChanged)
1378+
catch (Exception e)
13721379
{
1373-
// show expanded builtin shortcuts
1374-
// use private field to avoid infinite recursion
1375-
_queryText = queryBuilderTmp.ToString();
1376-
OnPropertyChanged(nameof(QueryText));
1380+
App.API.LogException(ClassName, $"Error when expanding shortcut {shortcut.Key}", e);
13771381
}
1378-
});
1382+
}
13791383

1380-
return QueryBuilder.Build(queryBuilder.ToString().Trim(), PluginManager.NonGlobalPlugins);
1384+
if (queryChanged)
1385+
{
1386+
// show expanded builtin shortcuts
1387+
// use private field to avoid infinite recursion
1388+
_queryText = queryBuilderTmp.ToString();
1389+
OnPropertyChanged(nameof(QueryText));
1390+
}
13811391
}
13821392

13831393
private void RemoveOldQueryResults(Query query)

0 commit comments

Comments
 (0)