diff --git a/src/Files.App/Actions/Global/SearchAction.cs b/src/Files.App/Actions/Global/SearchAction.cs index 619fd9f998e0..9b197332ee22 100644 --- a/src/Files.App/Actions/Global/SearchAction.cs +++ b/src/Files.App/Actions/Global/SearchAction.cs @@ -23,16 +23,35 @@ public HotKey SecondHotKey public RichGlyph Glyph => new(themedIconStyle: "App.ThemedIcons.Omnibar.Search"); + public bool IsExecutable + => context.ShellPage is not null; + public SearchAction() { context = Ioc.Default.GetRequiredService(); + + context.PropertyChanged += Context_PropertyChanged; } public Task ExecuteAsync(object? parameter = null) { - context.ShellPage!.ToolbarViewModel.SwitchToSearchMode(); + // Check if ShellPage is available before executing the action + if (context.ShellPage is null) + return Task.CompletedTask; + + context.ShellPage.ToolbarViewModel.SwitchToSearchMode(); return Task.CompletedTask; } + + private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) + { + switch (e.PropertyName) + { + case nameof(IContentPageContext.ShellPage): + OnPropertyChanged(nameof(IsExecutable)); + break; + } + } } }