Skip to content

Commit

Permalink
RichCommands: Search (#11837)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrariofilippo authored Mar 28, 2023
1 parent 9a9fd97 commit 5ed2483
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 46 deletions.
49 changes: 49 additions & 0 deletions src/Files.App/Actions/Global/SearchAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using System.ComponentModel;
using System.Threading.Tasks;
using Windows.System;

namespace Files.App.Actions
{
internal class SearchAction : ObservableObject, IAction
{
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public string Label { get; } = "Search".GetLocalizedResource();

public string Description { get; } = "TODO: Need to be described.";

public HotKey HotKey { get; } = new(VirtualKey.F, VirtualKeyModifiers.Control);

public HotKey SecondHotKey { get; } = new(VirtualKey.F3);

public RichGlyph Glyph { get; } = new();

public bool IsExecutable => !context.IsSearchBoxVisible;

public SearchAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
context.ShellPage!.ToolbarViewModel.SwitchSearchBoxVisibility();
return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IContentPageContext.IsSearchBoxVisible):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}
1 change: 1 addition & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum CommandCodes
EnterCompactOverlay,
ExitCompactOverlay,
ToggleCompactOverlay,
Search,

// Show
ToggleShowHiddenItems,
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal class CommandManager : ICommandManager
public IRichCommand EnterCompactOverlay => commands[CommandCodes.EnterCompactOverlay];
public IRichCommand ExitCompactOverlay => commands[CommandCodes.ExitCompactOverlay];
public IRichCommand ToggleCompactOverlay => commands[CommandCodes.ToggleCompactOverlay];
public IRichCommand Search => commands[CommandCodes.Search];
public IRichCommand ToggleShowHiddenItems => commands[CommandCodes.ToggleShowHiddenItems];
public IRichCommand ToggleShowFileExtensions => commands[CommandCodes.ToggleShowFileExtensions];
public IRichCommand TogglePreviewPane => commands[CommandCodes.TogglePreviewPane];
Expand Down Expand Up @@ -159,6 +160,7 @@ public CommandManager()
[CommandCodes.EnterCompactOverlay] = new EnterCompactOverlayAction(),
[CommandCodes.ExitCompactOverlay] = new ExitCompactOverlayAction(),
[CommandCodes.ToggleCompactOverlay] = new ToggleCompactOverlayAction(),
[CommandCodes.Search] = new SearchAction(),
[CommandCodes.ToggleShowHiddenItems] = new ToggleShowHiddenItemsAction(),
[CommandCodes.ToggleShowFileExtensions] = new ToggleShowFileExtensionsAction(),
[CommandCodes.TogglePreviewPane] = new TogglePreviewPaneAction(),
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand EnterCompactOverlay { get; }
IRichCommand ExitCompactOverlay { get; }
IRichCommand ToggleCompactOverlay { get; }
IRichCommand Search { get; }

IRichCommand ToggleShowHiddenItems { get; }
IRichCommand ToggleShowFileExtensions { get; }
Expand Down
18 changes: 18 additions & 0 deletions src/Files.App/Contexts/ContentPage/ContentPageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Files.App.Filesystem;
using Files.App.UserControls.MultitaskingControl;
using Files.App.ViewModels;
using Files.App.Views.LayoutModes;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand All @@ -21,6 +22,8 @@ internal class ContentPageContext : ObservableObject, IContentPageContext

public IShellPage? ShellPage => context?.PaneOrColumn;

public Type PageLayoutType => ShellPage?.CurrentPageType ?? typeof(DetailsLayoutBrowser);

private ContentPageTypes pageType = ContentPageTypes.None;
public ContentPageTypes PageType => pageType;

Expand All @@ -42,6 +45,8 @@ internal class ContentPageContext : ObservableObject, IContentPageContext

public bool CanNavigateToParent => ShellPage is not null && ShellPage.ToolbarViewModel.CanNavigateToParent;

public bool IsSearchBoxVisible => ShellPage is not null && ShellPage.ToolbarViewModel.IsSearchBoxVisible;

public ContentPageContext()
{
context.Changing += Context_Changing;
Expand All @@ -53,6 +58,7 @@ private void Context_Changing(object? sender, EventArgs e)
{
if (ShellPage is IShellPage page)
{
page.PropertyChanged -= Page_PropertyChanged;
page.ContentChanged -= Page_ContentChanged;
page.InstanceViewModel.PropertyChanged -= InstanceViewModel_PropertyChanged;
page.ToolbarViewModel.PropertyChanged -= ToolbarViewModel_PropertyChanged;
Expand All @@ -68,6 +74,7 @@ private void Context_Changed(object? sender, EventArgs e)
{
if (ShellPage is IShellPage page)
{
page.PropertyChanged += Page_PropertyChanged;
page.ContentChanged += Page_ContentChanged;
page.InstanceViewModel.PropertyChanged += InstanceViewModel_PropertyChanged;
page.ToolbarViewModel.PropertyChanged += ToolbarViewModel_PropertyChanged;
Expand All @@ -81,6 +88,16 @@ private void Context_Changed(object? sender, EventArgs e)
OnPropertyChanged(nameof(ShellPage));
}

private void Page_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(ShellPage.CurrentPageType):
OnPropertyChanged(nameof(PageLayoutType));
break;
}
}

private void Page_ContentChanged(object? sender, TabItemArguments e) => Update();

private void InstanceViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down Expand Up @@ -109,6 +126,7 @@ private void ToolbarViewModel_PropertyChanged(object? sender, PropertyChangedEve
case nameof(ToolbarViewModel.CanNavigateToParent):
case nameof(ToolbarViewModel.HasItem):
case nameof(ToolbarViewModel.CanRefresh):
case nameof(ToolbarViewModel.IsSearchBoxVisible):
OnPropertyChanged(e.PropertyName);
break;
case nameof(ToolbarViewModel.SelectedItems):
Expand Down
5 changes: 5 additions & 0 deletions src/Files.App/Contexts/ContentPage/IContentPageContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Files.App.Filesystem;
using System;
using System.Collections.Generic;
using System.ComponentModel;

Expand All @@ -10,6 +11,8 @@ public interface IContentPageContext : INotifyPropertyChanged

ContentPageTypes PageType { get; }

Type PageLayoutType { get; }

ListedItem? Folder { get; }

bool HasItem { get; }
Expand All @@ -21,5 +24,7 @@ public interface IContentPageContext : INotifyPropertyChanged
bool CanGoBack { get; }
bool CanGoForward { get; }
bool CanNavigateToParent { get; }

bool IsSearchBoxVisible { get; }
}
}
2 changes: 1 addition & 1 deletion src/Files.App/IShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Files.App
{
public interface IShellPage : ITabItemContent, IMultiPaneInfo, IDisposable
public interface IShellPage : ITabItemContent, IMultiPaneInfo, IDisposable, INotifyPropertyChanged
{
ItemViewModel FilesystemViewModel { get; }

Expand Down
3 changes: 0 additions & 3 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="NavSearchButton.ToolTipService.ToolTip" xml:space="preserve">
<value>Search (Ctrl+F)</value>
</data>
<data name="ItemAlreadyExistsDialogContent" xml:space="preserve">
<value>An item with this name already exists in this directory.</value>
</data>
Expand Down
13 changes: 4 additions & 9 deletions src/Files.App/UserControls/AddressToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,13 @@
<Button
x:Name="SearchButton"
AccessKey="I"
AutomationProperties.Name="{helpers:ResourceString Name=Search}"
Click="SearchButton_Click"
AutomationProperties.Name="{x:Bind ViewModel.Commands.Search.Label}"
Command="{x:Bind ViewModel.Commands.Search, Mode=OneWay}"
IsEnabled="{x:Bind ViewModel.Commands.Search.IsExecutable}"
Style="{StaticResource AddressToolbarButtonStyle}"
ToolTipService.ToolTip="{helpers:ResourceString Name=NavSearchButton/ToolTipService/ToolTip}"
ToolTipService.ToolTip="{x:Bind ViewModel.Commands.Search.LabelWithHotKey}"
Visibility="Collapsed">
<FontIcon FontSize="14" Glyph="{x:Bind ViewModel.SearchButtonGlyph, Mode=OneWay}" />
<Button.KeyboardAccelerators>
<KeyboardAccelerator
Key="F"
IsEnabled="False"
Modifiers="Control" />
</Button.KeyboardAccelerators>
</Button>

<Button
Expand Down
1 change: 0 additions & 1 deletion src/Files.App/UserControls/AddressToolbar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ private void VisiblePath_LostFocus(object _, RoutedEventArgs e)
VisiblePath.Focus(FocusState.Programmatic);
}

private void SearchButton_Click(object _, RoutedEventArgs e) => ViewModel.SwitchSearchBoxVisibility();
private void SearchRegion_OnGotFocus(object sender, RoutedEventArgs e) => ViewModel.SearchRegion_GotFocus(sender, e);
private void SearchRegion_LostFocus(object sender, RoutedEventArgs e) => ViewModel.SearchRegion_LostFocus(sender, e);
private void SearchRegion_AccessKeyInvoked(UIElement sender, AccessKeyInvokedEventArgs args) => sender.Focus(FocusState.Keyboard);
Expand Down
10 changes: 8 additions & 2 deletions src/Files.App/ViewModels/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CommunityToolkit.WinUI;
using CommunityToolkit.WinUI.UI;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Filesystem;
using Files.App.Filesystem.StorageItems;
Expand Down Expand Up @@ -504,8 +505,7 @@ public void SwitchSearchBoxVisibility()
{
if (IsSearchBoxVisible)
{
SearchBox.Query = string.Empty;
IsSearchBoxVisible = false;
CloseSearchBox();
}
else
{
Expand Down Expand Up @@ -536,6 +536,12 @@ public void CloseSearchBox()
{
SearchBox.Query = string.Empty;
IsSearchBoxVisible = false;

var page = Ioc.Default.GetRequiredService<IContentPageContext>().ShellPage?.SlimContentPage;
if (page is StandardViewBase svb && svb.IsLoaded)
page.ItemManipulationModel.FocusFileList();
else
AddressToolbar?.Focus(FocusState.Programmatic);
}
}

Expand Down
9 changes: 0 additions & 9 deletions src/Files.App/Views/ColumnShellPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,6 @@
Invoked="KeyboardAccelerator_Invoked"
IsEnabled="{x:Bind IsCurrentInstance, Mode=OneWay}"
Modifiers="Menu" />
<KeyboardAccelerator
Key="F"
Invoked="KeyboardAccelerator_Invoked"
IsEnabled="{x:Bind IsCurrentInstance, Mode=OneWay}"
Modifiers="Control" />
<KeyboardAccelerator
Key="F3"
Invoked="KeyboardAccelerator_Invoked"
IsEnabled="{x:Bind IsCurrentInstance, Mode=OneWay}" />
</local:BaseShellPage.KeyboardAccelerators>
<Grid
x:Name="RootGrid"
Expand Down
5 changes: 0 additions & 5 deletions src/Files.App/Views/ColumnShellPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ private async void KeyboardAccelerator_Invoked(KeyboardAccelerator sender, Keybo
await storageHistoryHelpers.TryRedo();
break;

case (false, false, false, true, VirtualKey.F3): //f3
case (true, false, false, true, VirtualKey.F): // ctrl + f
ToolbarViewModel.SwitchSearchBoxVisibility();
break;

case (true, true, false, true, VirtualKey.N): // ctrl + shift + n, new item
if (InstanceViewModel.CanCreateFileInPage)
{
Expand Down
9 changes: 0 additions & 9 deletions src/Files.App/Views/ModernShellPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@
Invoked="KeyboardAccelerator_Invoked"
IsEnabled="{x:Bind IsCurrentInstance, Mode=OneWay}"
Modifiers="Menu" />
<KeyboardAccelerator
Key="F"
Invoked="KeyboardAccelerator_Invoked"
IsEnabled="{x:Bind IsCurrentInstance, Mode=OneWay}"
Modifiers="Control" />
<KeyboardAccelerator
Key="F3"
Invoked="KeyboardAccelerator_Invoked"
IsEnabled="{x:Bind IsCurrentInstance, Mode=OneWay}" />
</local:BaseShellPage.KeyboardAccelerators>

<Frame
Expand Down
7 changes: 0 additions & 7 deletions src/Files.App/Views/ModernShellPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,6 @@ private async void KeyboardAccelerator_Invoked(KeyboardAccelerator sender, Keybo

break;

case (false, false, false, _, VirtualKey.F3): //f3
case (true, false, false, _, VirtualKey.F): // ctrl + f
if (tabInstance || CurrentPageType == typeof(WidgetsPage))
ToolbarViewModel.SwitchSearchBoxVisibility();

break;

case (true, true, false, true, VirtualKey.N): // ctrl + shift + n, new item
if (InstanceViewModel.CanCreateFileInPage)
{
Expand Down

0 comments on commit 5ed2483

Please sign in to comment.