AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.41k stars 2.2k forks source link

TreeView always adjusts Horizontal Scrollbar even when Selected Node is already fully visible #15335

Open Washi1337 opened 5 months ago

Washi1337 commented 5 months ago

Describe the bug

When selecting a node in a tree view with a horizontal scrollbar, it always seems it adjusts this scrollbar to the level of indentation of the selected node, even when the node was already fully visible. This makes for a rather annoying motions where all tree view items are constantly moving around unnecessarily.

To Reproduce

  1. Create a new Avalonia project with a treeview and add some random nodes to them. Example project files below:

    MainWindow.axaml: ```xml ```
    MainWindow.axaml.cs: ```csharp using Avalonia.Controls; namespace AvaloniaTreeViewTest; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); for (int i = 0; i < 10; i++) MainTreeView.Items.Add(CreateRandomNode(i, 0)); } private TreeViewItem CreateRandomNode(int index, int depth) { var result = new TreeViewItem(); result.Header = new string((char) ('A' + index), (index + 1) * 5); if (depth < 4) { for (int i = 0; i < 10; i++) result.Items.Add(CreateRandomNode(i, depth + 1)); } return result; } } ```
  2. Adjust the tree view width or expand some of the tree nodes such that a horizontal scrollbar appears.
  3. Put the horizontal scrollbar back at position 0
  4. Click one of the already fully visible nodes.
  5. Observe the horizontal scrollbar adjusts even though the node was already fully visible.

Demo video:

https://github.com/AvaloniaUI/Avalonia/assets/3613449/62b6ad98-25b1-4524-8a47-5326119dd776

Expected behavior

The tree view should be more conservative with actually adjusting the scroll bars, by only adjusting it when the node is not already (fully) visible.

Avalonia version

11.0.10

OS

Windows, Linux

Additional context

Consider the case where the tree view is used for visualizing a folder structure, and a user wants to quickly browse through the folders to find a file they are looking for. Having the tree view then jump around unnecessarily a lot makes for a rather bad user experience, especially when there are a lot of items in the tree view.

I could disable the auto scrolling by setting AutoScrollToSelectedItem to False, but this is not a great solution. Especially when trying to programmatically set the selected item (think of a folder being opened in the right pane of the demo window), I would want the selected tree view item to be highlighted and visible.

jp2masa commented 5 months ago

Probably related to #14418/#14419.