Skip to content

Commit 892cadb

Browse files
authored
Fix: Fixed an issue where the focus was lost when quickly switching tabs (#15449)
Co-authored-by: d2dyno006 <53011783+d2dyno006@users.noreply.github.com>
1 parent 27758c6 commit 892cadb

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

src/Files.App/Actions/Navigation/NextTabAction.cs

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

4+
using Microsoft.UI.Xaml;
5+
using Microsoft.UI.Xaml.Controls;
6+
47
namespace Files.App.Actions
58
{
69
internal sealed class NextTabAction : ObservableObject, IAction
@@ -26,11 +29,15 @@ public NextTabAction()
2629
multitaskingContext.PropertyChanged += MultitaskingContext_PropertyChanged;
2730
}
2831

29-
public Task ExecuteAsync(object? parameter = null)
32+
public async Task ExecuteAsync(object? parameter = null)
3033
{
3134
App.AppModel.TabStripSelectedIndex = (App.AppModel.TabStripSelectedIndex + 1) % multitaskingContext.TabCount;
3235

33-
return Task.CompletedTask;
36+
// Small delay for the UI to load
37+
await Task.Delay(500);
38+
39+
// Refocus on the file list
40+
(multitaskingContext.CurrentTabItem.TabItemContent as Control)?.Focus(FocusState.Programmatic);
3441
}
3542

3643
private void MultitaskingContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/Actions/Navigation/PreviousTabAction.cs

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

4+
using Microsoft.UI.Xaml;
5+
using Microsoft.UI.Xaml.Controls;
6+
47
namespace Files.App.Actions
58
{
69
internal sealed class PreviousTabAction : ObservableObject, IAction
@@ -26,14 +29,18 @@ public PreviousTabAction()
2629
multitaskingContext.PropertyChanged += MultitaskingContext_PropertyChanged;
2730
}
2831

29-
public Task ExecuteAsync(object? parameter = null)
32+
public async Task ExecuteAsync(object? parameter = null)
3033
{
3134
if (App.AppModel.TabStripSelectedIndex is 0)
3235
App.AppModel.TabStripSelectedIndex = multitaskingContext.TabCount - 1;
3336
else
3437
App.AppModel.TabStripSelectedIndex--;
3538

36-
return Task.CompletedTask;
39+
// Small delay for the UI to load
40+
await Task.Delay(500);
41+
42+
// Refocus on the file list
43+
(multitaskingContext.CurrentTabItem.TabItemContent as Control)?.Focus(FocusState.Programmatic);
3744
}
3845

3946
private void MultitaskingContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/Data/Models/AppModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public int TabStripSelectedIndex
3737

3838
if (value >= 0 && value < MainPageViewModel.AppInstances.Count)
3939
{
40-
Frame rootFrame = (Frame)MainWindow.Instance.Content;
40+
var rootFrame = (Frame)MainWindow.Instance.Content;
4141
var mainView = (MainPage)rootFrame.Content;
4242
mainView.ViewModel.SelectedTabItem = MainPageViewModel.AppInstances[value];
4343
}

src/Files.App/ViewModels/MainPageViewModel.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.UI.Xaml.Navigation;
99
using System.Windows.Input;
1010
using Windows.System;
11+
using Microsoft.UI.Xaml.Controls;
1112

1213
namespace Files.App.ViewModels
1314
{
@@ -228,9 +229,9 @@ await Task.WhenAll(
228229

229230
// Command methods
230231

231-
private void ExecuteNavigateToNumberedTabKeyboardAcceleratorCommand(KeyboardAcceleratorInvokedEventArgs? e)
232+
private async void ExecuteNavigateToNumberedTabKeyboardAcceleratorCommand(KeyboardAcceleratorInvokedEventArgs? e)
232233
{
233-
int indexToSelect = e!.KeyboardAccelerator.Key switch
234+
var indexToSelect = e!.KeyboardAccelerator.Key switch
234235
{
235236
VirtualKey.Number1 => 0,
236237
VirtualKey.Number2 => 1,
@@ -246,8 +247,16 @@ private void ExecuteNavigateToNumberedTabKeyboardAcceleratorCommand(KeyboardAcce
246247

247248
// Only select the tab if it is in the list
248249
if (indexToSelect < AppInstances.Count)
250+
{
249251
App.AppModel.TabStripSelectedIndex = indexToSelect;
250252

253+
// Small delay for the UI to load
254+
await Task.Delay(500);
255+
256+
// Refocus on the file list
257+
(SelectedTabItem?.TabItemContent as Control)?.Focus(FocusState.Programmatic);
258+
}
259+
251260
e.Handled = true;
252261
}
253262

0 commit comments

Comments
 (0)