From 7f26402bd363552d63f170b4a3a7c7e5a23e8061 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 2 Jun 2023 11:22:24 +0200 Subject: [PATCH 1/3] Set PointerOverRoot. Fixes DevTools not working when pressing Ctrl+Shift (there's a check that `vm.PointerOverRoot` is not null in `MainWindow.xaml.cs`). --- src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs index 3adad38ac6..f993d2c957 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/MainViewModel.cs @@ -44,6 +44,7 @@ namespace Avalonia.Diagnostics.ViewModels SelectedTab = 0; if (root is TopLevel topLevel) { + _pointerOverRoot = topLevel; _pointerOverSubscription = topLevel.GetObservable(TopLevel.PointerOverElementProperty) .Subscribe(x => PointerOverElement = x); From f912d4ca282cfa448f4f446db71ed099f63f895b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 2 Jun 2023 13:09:03 +0200 Subject: [PATCH 2/3] Improve TreeView.AutoScrollToSelectedItem. It's still of very limited use as the selected item's ancestors need to be manually expanded, but at least when the selected item is realized it should now be scrolled into view. Fixes #10449 --- src/Avalonia.Controls/TreeView.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Avalonia.Controls/TreeView.cs b/src/Avalonia.Controls/TreeView.cs index 7bf8d3bb68..0b22042652 100644 --- a/src/Avalonia.Controls/TreeView.cs +++ b/src/Avalonia.Controls/TreeView.cs @@ -84,6 +84,11 @@ namespace Avalonia.Controls /// /// Gets or sets a value indicating whether to automatically scroll to newly selected items. /// + /// + /// This property is of limited use with as it will only scroll + /// to realized items. To scroll to a non-expanded item, you need to ensure that its + /// ancestors are expanded. + /// public bool AutoScrollToSelectedItem { get => GetValue(AutoScrollToSelectedItemProperty); @@ -531,6 +536,12 @@ namespace Avalonia.Controls // The IsSelected property is not set on the container: update the container // selection based on the current selection as understood by this control. MarkContainerSelected(container, SelectedItems.Contains(item)); + + // If the newly realized container is the selected container, scroll to it after layout. + if (AutoScrollToSelectedItem && SelectedItem == item) + { + Dispatcher.UIThread.Post(container.BringIntoView, DispatcherPriority.Loaded); + } } /// From 994fb351d6f291b0a2a66f038d857b956389bd46 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 2 Jun 2023 14:02:11 +0200 Subject: [PATCH 3/3] Fix scroll to selected item logic. - Use `TreeContainerFromItem` when trying to find the container for the selected item, it can be anywhere in the tree - Only scroll to `SelectedItem`, not another item in `SelectedItems` --- src/Avalonia.Controls/TreeView.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/TreeView.cs b/src/Avalonia.Controls/TreeView.cs index 0b22042652..56a5999f29 100644 --- a/src/Avalonia.Controls/TreeView.cs +++ b/src/Avalonia.Controls/TreeView.cs @@ -358,9 +358,13 @@ namespace Avalonia.Controls SelectedItemsAdded(e.NewItems!.Cast().ToArray()); - if (AutoScrollToSelectedItem) + var selectedItem = SelectedItem; + + if (AutoScrollToSelectedItem && + selectedItem is not null && + e.NewItems![0] == selectedItem) { - var container = ContainerFromItem(e.NewItems![0]!); + var container = TreeContainerFromItem(selectedItem); container?.BringIntoView(); }