Skip to content

Commit ea0a18a

Browse files
committed
Use UI thread for populating suggestions
1 parent 1777986 commit ea0a18a

File tree

2 files changed

+31
-41
lines changed

2 files changed

+31
-41
lines changed

src/Files.App/UserControls/NavigationToolbar.xaml.cs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,15 @@ private async void Omnibar_TextChanged(Omnibar sender, OmnibarTextChangedEventAr
350350

351351
if (Omnibar.CurrentSelectedMode == OmnibarPathMode)
352352
{
353-
await ViewModel.PopulateOmnibarSuggestionsForPathMode();
353+
await DispatcherQueue.EnqueueOrInvokeAsync(ViewModel.PopulateOmnibarSuggestionsForPathMode);
354354
}
355355
else if (Omnibar.CurrentSelectedMode == OmnibarCommandPaletteMode)
356356
{
357-
await DispatcherQueue.EnqueueOrInvokeAsync(() =>
358-
{
359-
ViewModel.PopulateOmnibarSuggestionsForCommandPaletteMode();
360-
});
357+
await DispatcherQueue.EnqueueOrInvokeAsync(ViewModel.PopulateOmnibarSuggestionsForCommandPaletteMode);
361358
}
362359
else if (Omnibar.CurrentSelectedMode == OmnibarSearchMode)
363360
{
364-
await ViewModel.PopulateOmnibarSuggestionsForSearchMode();
361+
await DispatcherQueue.EnqueueOrInvokeAsync(ViewModel.PopulateOmnibarSuggestionsForSearchMode);
365362
}
366363
}
367364

@@ -464,19 +461,13 @@ private async void Omnibar_ModeChanged(object sender, OmnibarModeChangedEventArg
464461
? Constants.UserEnvironmentPaths.HomePath
465462
: ContentPageContext.ShellPage.ShellViewModel.WorkingDirectory;
466463

467-
await DispatcherQueue.EnqueueOrInvokeAsync(async () =>
468-
{
469-
await ViewModel.PopulateOmnibarSuggestionsForPathMode();
470-
});
464+
await DispatcherQueue.EnqueueOrInvokeAsync(ViewModel.PopulateOmnibarSuggestionsForPathMode);
471465
}
472466
else if (e.NewMode == OmnibarCommandPaletteMode)
473467
{
474468
ViewModel.OmnibarCommandPaletteModeText = string.Empty;
475469

476-
await DispatcherQueue.EnqueueOrInvokeAsync(() =>
477-
{
478-
ViewModel.PopulateOmnibarSuggestionsForCommandPaletteMode();
479-
});
470+
await DispatcherQueue.EnqueueOrInvokeAsync(ViewModel.PopulateOmnibarSuggestionsForCommandPaletteMode);
480471
}
481472
else if (e.NewMode == OmnibarSearchMode)
482473
{
@@ -485,7 +476,7 @@ await DispatcherQueue.EnqueueOrInvokeAsync(() =>
485476
else
486477
ViewModel.OmnibarSearchModeText = ViewModel.InstanceViewModel.CurrentSearchQuery;
487478

488-
await ViewModel.PopulateOmnibarSuggestionsForSearchMode();
479+
await DispatcherQueue.EnqueueOrInvokeAsync(ViewModel.PopulateOmnibarSuggestionsForSearchMode);
489480
}
490481
}
491482

@@ -499,23 +490,17 @@ private async void Omnibar_IsFocusedChanged(Omnibar sender, OmnibarIsFocusedChan
499490
? Constants.UserEnvironmentPaths.HomePath
500491
: ContentPageContext.ShellPage.ShellViewModel.WorkingDirectory;
501492

502-
await DispatcherQueue.EnqueueOrInvokeAsync(async () =>
503-
{
504-
await ViewModel.PopulateOmnibarSuggestionsForPathMode();
505-
});
493+
await DispatcherQueue.EnqueueOrInvokeAsync(ViewModel.PopulateOmnibarSuggestionsForPathMode);
506494
}
507495
else if (Omnibar.CurrentSelectedMode == OmnibarCommandPaletteMode)
508496
{
509497
ViewModel.OmnibarCommandPaletteModeText = string.Empty;
510498

511-
await DispatcherQueue.EnqueueOrInvokeAsync(() =>
512-
{
513-
ViewModel.PopulateOmnibarSuggestionsForCommandPaletteMode();
514-
});
499+
await DispatcherQueue.EnqueueOrInvokeAsync(ViewModel.PopulateOmnibarSuggestionsForCommandPaletteMode);
515500
}
516501
else if (Omnibar.CurrentSelectedMode == OmnibarSearchMode)
517502
{
518-
await ViewModel.PopulateOmnibarSuggestionsForSearchMode();
503+
await DispatcherQueue.EnqueueOrInvokeAsync(ViewModel.PopulateOmnibarSuggestionsForSearchMode);
519504
}
520505
}
521506
}

src/Files.App/ViewModels/UserControls/NavigationToolbarViewModel.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ void AddNoResultsItem()
11711171
}
11721172
}
11731173

1174-
public void PopulateOmnibarSuggestionsForCommandPaletteMode()
1174+
public async Task PopulateOmnibarSuggestionsForCommandPaletteMode()
11751175
{
11761176
var newSuggestions = new List<NavigationBarSuggestionItem>();
11771177

@@ -1212,22 +1212,27 @@ public void PopulateOmnibarSuggestionsForCommandPaletteMode()
12121212
}
12131213
}
12141214

1215-
var suggestionItems = Commands
1216-
.Where(command => command.IsExecutable
1217-
&& command.IsAccessibleGlobally
1218-
&& (command.Description.Contains(OmnibarCommandPaletteModeText, StringComparison.OrdinalIgnoreCase)
1219-
|| command.Code.ToString().Contains(OmnibarCommandPaletteModeText, StringComparison.OrdinalIgnoreCase)))
1220-
.Select(command => new NavigationBarSuggestionItem
1221-
{
1222-
ThemedIconStyle = command.Glyph.ToThemedIconStyle(),
1223-
Glyph = command.Glyph.BaseGlyph,
1224-
Text = command.Description,
1225-
PrimaryDisplay = command.Description,
1226-
HotKeys = command.HotKeys,
1227-
SearchText = OmnibarCommandPaletteModeText,
1228-
})
1229-
.Where(item => item.Text != Commands.OpenCommandPalette.Description.ToString()
1230-
&& item.Text != Commands.EditPath.Description.ToString());
1215+
IEnumerable<NavigationBarSuggestionItem> suggestionItems = null!;
1216+
1217+
await Task.Run(() =>
1218+
{
1219+
suggestionItems = Commands
1220+
.Where(command => command.IsExecutable
1221+
&& command.IsAccessibleGlobally
1222+
&& (command.Description.Contains(OmnibarCommandPaletteModeText, StringComparison.OrdinalIgnoreCase)
1223+
|| command.Code.ToString().Contains(OmnibarCommandPaletteModeText, StringComparison.OrdinalIgnoreCase)))
1224+
.Select(command => new NavigationBarSuggestionItem
1225+
{
1226+
ThemedIconStyle = command.Glyph.ToThemedIconStyle(),
1227+
Glyph = command.Glyph.BaseGlyph,
1228+
Text = command.Description,
1229+
PrimaryDisplay = command.Description,
1230+
HotKeys = command.HotKeys,
1231+
SearchText = OmnibarCommandPaletteModeText,
1232+
})
1233+
.Where(item => item.Text != Commands.OpenCommandPalette.Description.ToString()
1234+
&& item.Text != Commands.EditPath.Description.ToString());
1235+
});
12311236

12321237
newSuggestions.AddRange(suggestionItems);
12331238

0 commit comments

Comments
 (0)