From cb432776442d107962e1816c328f89557354ab3f Mon Sep 17 00:00:00 2001 From: Yair <39923744+yaira2@users.noreply.github.com> Date: Sun, 19 Nov 2023 22:57:01 -0500 Subject: [PATCH] Fix: Fixed xamlRoot in GetFocusedElement (#13962) --- src/Files.App/UserControls/AddressToolbar.xaml.cs | 2 +- .../Views/LayoutModes/DetailsLayoutBrowser.xaml.cs | 2 +- .../Views/LayoutModes/GridViewBrowser.xaml.cs | 2 +- .../Views/LayoutModes/StandardLayoutMode.cs | 13 +++++++++---- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Files.App/UserControls/AddressToolbar.xaml.cs b/src/Files.App/UserControls/AddressToolbar.xaml.cs index cbc267b6803e..dd1936de1880 100644 --- a/src/Files.App/UserControls/AddressToolbar.xaml.cs +++ b/src/Files.App/UserControls/AddressToolbar.xaml.cs @@ -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; diff --git a/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs b/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs index 332cbd05241c..b33b58faebe4 100644 --- a/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs +++ b/src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs @@ -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(focusedElement) is not null; var isFooterFocused = focusedElement is HyperlinkButton; diff --git a/src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs b/src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs index 6b15bcfc5120..6fbe508ea1c4 100644 --- a/src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs +++ b/src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs @@ -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) diff --git a/src/Files.App/Views/LayoutModes/StandardLayoutMode.cs b/src/Files.App/Views/LayoutModes/StandardLayoutMode.cs index a7ac63e31cbc..27668dafe862 100644 --- a/src/Files.App/Views/LayoutModes/StandardLayoutMode.cs +++ b/src/Files.App/Views/LayoutModes/StandardLayoutMode.cs @@ -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; @@ -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; @@ -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) @@ -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(focusedElement) == ItemsControl; if (!isFileListFocused) ListViewBase.Focus(FocusState.Programmatic); @@ -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); @@ -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(focusedElement) is not null; if (InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Enter) == CoreVirtualKeyStates.Down || (focusedElement is Button && !isHeaderFocused) || // Allow jumpstring when header is focused