Skip to content

Commit

Permalink
Fix: Fixed xamlRoot in GetFocusedElement (#13962)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaira2 authored Nov 20, 2023
1 parent 4091748 commit cb43277
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Files.App/UserControls/AddressToolbar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void VisiblePath_KeyDown(object _, KeyRoutedEventArgs e)
}
private void VisiblePath_LostFocus(object _, RoutedEventArgs e)
{
var element = FocusManager.GetFocusedElement(XamlRoot);
var element = FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot);
if (element is FlyoutBase or AppBarButton or Popup)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ protected override async void FileList_PreviewKeyDown(object sender, KeyRoutedEv

var ctrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
var shiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(XamlRoot);
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot);
var isHeaderFocused = DependencyObjectHelpers.FindParent<DataGridHeader>(focusedElement) is not null;
var isFooterFocused = focusedElement is HyperlinkButton;

Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ protected override async void FileList_PreviewKeyDown(object sender, KeyRoutedEv

var ctrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
var shiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
var focusedElement = FocusManager.GetFocusedElement(XamlRoot) as FrameworkElement;
var focusedElement = FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot) as FrameworkElement;
var isFooterFocused = focusedElement is HyperlinkButton;

if (ctrlPressed && e.Key is VirtualKey.A)
Expand Down
13 changes: 9 additions & 4 deletions src/Files.App/Views/LayoutModes/StandardLayoutMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.WinUI.UI;
using Files.App.Data.Commands;
using Files.App.ViewModels.LayoutModes;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -85,6 +84,9 @@ protected virtual async void ItemManipulationModel_RefreshItemThumbnail(object?

protected virtual async Task ReloadSelectedItemIconAsync()
{
if (ParentShellPageInstance?.SlimContentPage?.SelectedItem is null)
return;

ParentShellPageInstance.FilesystemViewModel.CancelExtendedPropertiesLoading();
ParentShellPageInstance.SlimContentPage.SelectedItem.ItemPropertiesInitialized = false;

Expand All @@ -93,6 +95,9 @@ protected virtual async Task ReloadSelectedItemIconAsync()

protected virtual async Task ReloadSelectedItemsIconAsync()
{
if (ParentShellPageInstance?.SlimContentPage?.SelectedItems is null)
return;

ParentShellPageInstance.FilesystemViewModel.CancelExtendedPropertiesLoading();

foreach (var selectedItem in ParentShellPageInstance.SlimContentPage.SelectedItems)
Expand All @@ -104,7 +109,7 @@ protected virtual async Task ReloadSelectedItemsIconAsync()

protected virtual void ItemManipulationModel_FocusFileListInvoked(object? sender, EventArgs e)
{
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(XamlRoot);
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot);
var isFileListFocused = DependencyObjectHelpers.FindParent<ListViewBase>(focusedElement) == ItemsControl;
if (!isFileListFocused)
ListViewBase.Focus(FocusState.Programmatic);
Expand Down Expand Up @@ -224,7 +229,7 @@ protected virtual async Task CommitRenameAsync(TextBox textBox)
protected virtual async void RenameTextBox_LostFocus(object sender, RoutedEventArgs e)
{
// This check allows the user to use the text box context menu without ending the rename
if (!(FocusManager.GetFocusedElement(XamlRoot) is AppBarButton or Popup))
if (!(FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot) is AppBarButton or Popup))
{
TextBox textBox = (TextBox)e.OriginalSource;
await CommitRenameAsync(textBox);
Expand Down Expand Up @@ -315,7 +320,7 @@ protected override void Page_CharacterReceived(UIElement sender, CharacterReceiv
return;

// Don't block the various uses of enter key (key 13)
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(XamlRoot);
var focusedElement = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot);
var isHeaderFocused = DependencyObjectHelpers.FindParent<DataGridHeader>(focusedElement) is not null;
if (InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Enter) == CoreVirtualKeyStates.Down ||
(focusedElement is Button && !isHeaderFocused) || // Allow jumpstring when header is focused
Expand Down

0 comments on commit cb43277

Please sign in to comment.