Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fixed xamlRoot in GetFocusedElement #13962

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -333,7 +333,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