Skip to content

Commit e743f70

Browse files
authored
Code Quality: Fixed issue where Command Palette sometimes froze the UI (#17491)
1 parent b90ad79 commit e743f70

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

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

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -950,68 +950,94 @@ void AddNoResultsItem()
950950

951951
public async Task PopulateOmnibarSuggestionsForCommandPaletteMode()
952952
{
953-
var newSuggestions = new List<NavigationBarSuggestionItem>();
954-
955-
if (ContentPageContext.SelectedItems.Count == 1 && ContentPageContext.SelectedItem is not null && !ContentPageContext.SelectedItem.IsFolder)
953+
var (suggestionsToProcess, commandsToProcess) = await Task.Run(() =>
956954
{
957-
try
958-
{
959-
var selectedItemPath = ContentPageContext.SelectedItem.ItemPath;
960-
var fileActionEntity = ActionManager.Instance.EntityFactory.CreateFileEntity(selectedItemPath);
961-
var actions = ActionManager.Instance.ActionRuntime.ActionCatalog.GetActionsForInputs(new[] { fileActionEntity });
955+
var suggestions = new List<NavigationBarSuggestionItem>();
962956

963-
foreach (var action in actions.Where(a => a.Definition.Description.Contains(OmnibarCommandPaletteModeText, StringComparison.OrdinalIgnoreCase)))
957+
if (ContentPageContext.SelectedItems.Count == 1 && ContentPageContext.SelectedItem is not null && !ContentPageContext.SelectedItem.IsFolder)
958+
{
959+
try
964960
{
965-
var newItem = new NavigationBarSuggestionItem
966-
{
967-
PrimaryDisplay = action.Definition.Description,
968-
SearchText = OmnibarCommandPaletteModeText,
969-
ActionInstance = action
970-
};
961+
var selectedItemPath = ContentPageContext.SelectedItem.ItemPath;
962+
var fileActionEntity = ActionManager.Instance.EntityFactory.CreateFileEntity(selectedItemPath);
963+
var actions = ActionManager.Instance.ActionRuntime.ActionCatalog.GetActionsForInputs(new[] { fileActionEntity });
971964

972-
if (Uri.TryCreate(action.Definition.IconFullPath, UriKind.RelativeOrAbsolute, out Uri? validUri))
965+
foreach (var action in actions.Where(a => a.Definition.Description.Contains(OmnibarCommandPaletteModeText, StringComparison.OrdinalIgnoreCase)))
973966
{
974-
try
967+
var newItem = new NavigationBarSuggestionItem
975968
{
976-
newItem.ActionIconSource = new BitmapImage(validUri);
977-
}
978-
catch (Exception)
969+
PrimaryDisplay = action.Definition.Description,
970+
SearchText = OmnibarCommandPaletteModeText,
971+
ActionInstance = action
972+
};
973+
974+
if (Uri.TryCreate(action.Definition.IconFullPath, UriKind.RelativeOrAbsolute, out Uri? validUri))
979975
{
976+
try
977+
{
978+
newItem.ActionIconSource = new BitmapImage(validUri);
979+
}
980+
catch (Exception)
981+
{
982+
}
980983
}
981-
}
982984

983-
newSuggestions.Add(newItem);
985+
suggestions.Add(newItem);
986+
}
987+
}
988+
catch (Exception ex)
989+
{
990+
App.Logger.LogWarning(ex, ex.Message);
984991
}
985992
}
986-
catch (Exception ex)
987-
{
988-
App.Logger.LogWarning(ex, ex.Message);
989-
}
990-
}
991993

992-
IEnumerable<NavigationBarSuggestionItem> suggestionItems = null!;
993-
994-
await Task.Run(() =>
995-
{
996-
suggestionItems = Commands
997-
.Where(command => command.IsExecutable
998-
&& command.IsAccessibleGlobally
994+
var commandsData = Commands
995+
.Where(command => command.IsAccessibleGlobally
999996
&& (command.Description.Contains(OmnibarCommandPaletteModeText, StringComparison.OrdinalIgnoreCase)
1000997
|| command.Code.ToString().Contains(OmnibarCommandPaletteModeText, StringComparison.OrdinalIgnoreCase)))
1001-
.Select(command => new NavigationBarSuggestionItem
1002-
{
1003-
ThemedIconStyle = command.Glyph.ToThemedIconStyle(),
1004-
Glyph = command.Glyph.BaseGlyph,
1005-
Text = command.Description,
1006-
PrimaryDisplay = command.Description,
1007-
HotKeys = command.HotKeys,
1008-
SearchText = OmnibarCommandPaletteModeText,
1009-
})
1010-
.Where(item => item.Text != Commands.OpenCommandPalette.Description.ToString());
998+
.Where(command => command.Description != Commands.OpenCommandPalette.Description.ToString())
999+
.ToList();
1000+
1001+
return (suggestions, commandsData);
10111002
});
10121003

1013-
newSuggestions.AddRange(suggestionItems);
1004+
var newSuggestions = new List<NavigationBarSuggestionItem>(suggestionsToProcess);
1005+
int processedCount = 0;
1006+
1007+
foreach (var command in commandsToProcess)
1008+
{
1009+
if (!command.IsExecutable)
1010+
{
1011+
processedCount++;
1012+
// To allow UI updates
1013+
if (processedCount % 3 == 0)
1014+
await Task.Yield();
1015+
continue;
1016+
}
10141017

1018+
var newItem = new NavigationBarSuggestionItem
1019+
{
1020+
ThemedIconStyle = command.Glyph.ToThemedIconStyle(),
1021+
Glyph = command.Glyph.BaseGlyph,
1022+
Text = command.Description,
1023+
PrimaryDisplay = command.Description,
1024+
HotKeys = command.HotKeys,
1025+
SearchText = OmnibarCommandPaletteModeText,
1026+
};
1027+
1028+
newSuggestions.Add(newItem);
1029+
processedCount++;
1030+
1031+
// To allow UI updates
1032+
if (processedCount % 3 == 0)
1033+
await Task.Yield();
1034+
}
1035+
1036+
UpdateCommandPaletteSuggestions(newSuggestions);
1037+
}
1038+
1039+
private void UpdateCommandPaletteSuggestions(List<NavigationBarSuggestionItem> newSuggestions)
1040+
{
10151041
if (newSuggestions.Count == 0)
10161042
{
10171043
newSuggestions.Add(new NavigationBarSuggestionItem()

0 commit comments

Comments
 (0)