@@ -20,7 +20,6 @@ public sealed partial class Toolbar : UserControl
2020 private readonly IModifiableCommandManager ModifiableCommands = Ioc . Default . GetRequiredService < IModifiableCommandManager > ( ) ;
2121 private readonly IAddItemService addItemService = Ioc . Default . GetRequiredService < IAddItemService > ( ) ;
2222 private bool isToolbarRefreshQueued ;
23- private HashSet < string > lastActiveContexts = new ( StringComparer . Ordinal ) ;
2423
2524 [ GeneratedDependencyProperty ]
2625 public partial NavigationToolbarViewModel ? ViewModel { get ; set ; }
@@ -65,10 +64,11 @@ private void Command_PropertyChanged(object? sender, PropertyChangedEventArgs e)
6564 if ( e . PropertyName is not nameof ( IRichCommand . IsExecutable ) || sender is not IRichCommand cmd
6665 || cmd . Code is CommandCodes . None || ! cmd . IsAccessibleGlobally )
6766 return ;
68- var ctxId = ToolbarItemDescriptor . ResolveToolbarSectionId ( cmd . Code . ToString ( ) , Commands ) ;
69- if ( ctxId != ToolbarDefaultsTemplate . AlwaysVisibleContextId
70- && lastActiveContexts . Contains ( ctxId ) != IsContextActive ( ctxId ) )
71- RequestToolbarRefresh ( ) ;
67+ // Debouncing in RequestToolbarRefresh() coalesces rapid IsExecutable changes,
68+ // so we don't need to check if the context visibility actually changed here.
69+ // Checking IsContextActive() loops through all commands and is expensive during
70+ // file selection when many commands' executability changes rapidly.
71+ RequestToolbarRefresh ( ) ;
7272 }
7373
7474 private void InstanceViewModel_PropertyChanged ( object ? sender , PropertyChangedEventArgs e )
@@ -103,7 +103,6 @@ private void PopulateToolbarItems()
103103 if ( ContextCommandBar is null ) return ;
104104 ContextCommandBar . PrimaryCommands . Clear ( ) ;
105105 var active = GetActiveToolbarContexts ( ) ;
106- lastActiveContexts = active ;
107106 var itemsByContext = ToolbarDefaultsTemplate . ResolveToolbarItemsByContext ( UserSettingsService . AppearanceSettingsService ) ;
108107 foreach ( var contextId in ToolbarDefaultsTemplate . ContextOrder )
109108 {
@@ -137,18 +136,6 @@ private HashSet<string> GetActiveToolbarContexts()
137136 return active ;
138137 }
139138
140- private bool IsContextActive ( string contextId )
141- {
142- foreach ( var cmd in Commands )
143- {
144- if ( cmd . Code is CommandCodes . None || ! cmd . IsAccessibleGlobally || ! cmd . IsExecutable ) continue ;
145- var resolved = ToolbarItemDescriptor . ResolveToolbarSectionId ( cmd . Code . ToString ( ) , Commands ) ;
146- if ( resolved == ToolbarDefaultsTemplate . RecycleBinContextId && ViewModel ? . InstanceViewModel ? . IsPageTypeRecycleBin != true ) continue ;
147- if ( resolved == contextId ) return true ;
148- }
149- return false ;
150- }
151-
152139 private static bool ShouldShowContext ( string contextId , ISet < string > active )
153140 => contextId == ToolbarDefaultsTemplate . OtherContextsContextId
154141 ? ! active . Any ( c => c is not ToolbarDefaultsTemplate . AlwaysVisibleContextId and not ToolbarDefaultsTemplate . OtherContextsContextId )
0 commit comments