Skip to content

Commit a66c7a9

Browse files
committed
Code Quality: Focus correct filter header in dual pane
1 parent 8474fc2 commit a66c7a9

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/Files.App/Actions/Show/ToggleFilterHeaderAction.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4-
using CommunityToolkit.WinUI;
5-
using Microsoft.UI.Xaml;
6-
using Microsoft.UI.Xaml.Controls;
7-
84
namespace Files.App.Actions
95
{
106
internal sealed partial class ToggleFilterHeaderAction : ObservableObject, IToggleAction
117
{
128
private readonly IGeneralSettingsService generalSettingsService = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
9+
private readonly IContentPageContext ContentPageContext = Ioc.Default.GetRequiredService<IContentPageContext>();
1310

1411
public string Label
1512
=> Strings.ToggleFilterHeader.GetLocalizedResource();
@@ -28,17 +25,14 @@ public ToggleFilterHeaderAction()
2825
generalSettingsService.PropertyChanged += GeneralSettingsService_PropertyChanged;
2926
}
3027

31-
public async Task ExecuteAsync(object? parameter = null)
28+
public Task ExecuteAsync(object? parameter = null)
3229
{
3330
generalSettingsService.ShowFilterHeader = !IsOn;
3431

3532
if (IsOn)
36-
{
37-
// Delay to ensure the UI updates before focusing
38-
await Task.Delay(100);
39-
var filterTextBox = (MainWindow.Instance.Content as Frame)?.FindDescendant("FilterTextBox") as AutoSuggestBox;
40-
filterTextBox?.Focus(FocusState.Programmatic);
41-
}
33+
ContentPageContext.ShellPage!.ShellViewModel.InvokeFocusFilterHeader();
34+
35+
return Task.CompletedTask;
4236
}
4337

4438
private void GeneralSettingsService_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ public GitProperties EnabledGitProperties
161161
private CancellationTokenSource searchCTS;
162162
private CancellationTokenSource updateTagGroupCTS;
163163

164+
public event EventHandler FocusFilterHeader;
165+
164166
public event EventHandler DirectoryInfoUpdated;
165167

166168
public event EventHandler GitDirectoryUpdated;
@@ -190,6 +192,11 @@ public GitProperties EnabledGitProperties
190192

191193
public event ItemLoadStatusChangedEventHandler ItemLoadStatusChanged;
192194

195+
public void InvokeFocusFilterHeader()
196+
{
197+
FocusFilterHeader.Invoke(this, null);
198+
}
199+
193200
public async Task SetWorkingDirectoryAsync(string? value)
194201
{
195202
if (string.IsNullOrWhiteSpace(value))
@@ -740,10 +747,10 @@ public void UpdateEmptyTextType()
740747
}
741748

742749
private string? _filesAndFoldersFilter;
743-
public string? FilesAndFoldersFilter
744-
{
745-
get => _filesAndFoldersFilter;
746-
set => SetProperty(ref _filesAndFoldersFilter, value);
750+
public string? FilesAndFoldersFilter
751+
{
752+
get => _filesAndFoldersFilter;
753+
set => SetProperty(ref _filesAndFoldersFilter, value);
747754
}
748755

749756

src/Files.App/Views/Shells/ModernShellPage.xaml.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Microsoft.UI.Xaml;
45
using Microsoft.UI.Xaml.Controls;
56
using Microsoft.UI.Xaml.Input;
67
using Microsoft.UI.Xaml.Media.Animation;
@@ -45,6 +46,7 @@ public ModernShellPage() : base(new CurrentInstanceViewModel())
4546
ShellViewModel.OnSelectionRequestedEvent += FilesystemViewModel_OnSelectionRequestedEvent;
4647
ShellViewModel.GitDirectoryUpdated += FilesystemViewModel_GitDirectoryUpdated;
4748
ShellViewModel.DirectoryInfoUpdated += ShellViewModel_DirectoryInfoUpdated;
49+
ShellViewModel.FocusFilterHeader += ShellViewModel_FocusFilterHeader;
4850

4951
ToolbarViewModel.PathControlDisplayText = Strings.Home.GetLocalizedResource();
5052
ToolbarViewModel.RefreshWidgetsRequested += ModernShellPage_RefreshWidgetsRequested;
@@ -53,6 +55,14 @@ public ModernShellPage() : base(new CurrentInstanceViewModel())
5355
_navigationInteractionTracker.NavigationRequested += OverscrollNavigationRequested;
5456
}
5557

58+
private async void ShellViewModel_FocusFilterHeader(object sender, EventArgs e)
59+
{
60+
// Delay to ensure the UI is ready for focus
61+
await Task.Delay(100);
62+
if (FilterTextBox?.IsLoaded ?? false)
63+
FilterTextBox.Focus(FocusState.Programmatic);
64+
}
65+
5666
private void ShellViewModel_DirectoryInfoUpdated(object sender, EventArgs e)
5767
{
5868
// Regular binding causes issues when refreshing the directory so we set the text manually
@@ -151,7 +161,7 @@ protected override async void ViewModel_WorkingDirectoryModified(object sender,
151161
private async void ItemDisplayFrame_Navigated(object sender, NavigationEventArgs e)
152162
{
153163
ContentPage = await GetContentOrNullAsync();
154-
164+
155165
ToolbarViewModel.UpdateAdditionalActions();
156166
if (ItemDisplayFrame.CurrentSourcePageType == (typeof(DetailsLayoutPage))
157167
|| ItemDisplayFrame.CurrentSourcePageType == typeof(GridLayoutPage))

0 commit comments

Comments
 (0)