fix: TreeView do NOT support NavigationMethod.Direction with invisible nodes (#391)

This commit is contained in:
leo 2024-08-22 18:11:25 +08:00
parent 71d36698f8
commit af6d2cc725
No known key found for this signature in database
3 changed files with 93 additions and 6 deletions

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
@ -76,6 +77,30 @@ namespace SourceGit.ViewModels
Preference.Instance.MoveNode(from, to); Preference.Instance.MoveNode(from, to);
} }
public RepositoryNode GetPrevVisible(RepositoryNode node)
{
var visibleRows = new List<RepositoryNode>();
CollectVisibleRows(visibleRows, RepositoryNodes);
var idx = visibleRows.IndexOf(node);
if (idx <= 1)
return null;
return visibleRows[idx - 1];
}
public RepositoryNode GetNextVisible(RepositoryNode node)
{
var visibleRows = new List<RepositoryNode>();
CollectVisibleRows(visibleRows, RepositoryNodes);
var idx = visibleRows.IndexOf(node);
if (idx < 0 || idx >= visibleRows.Count - 1)
return null;
return visibleRows[idx + 1];
}
public ContextMenu CreateContextMenu(RepositoryNode node) public ContextMenu CreateContextMenu(RepositoryNode node)
{ {
var menu = new ContextMenu(); var menu = new ContextMenu();
@ -212,6 +237,20 @@ namespace SourceGit.ViewModels
} }
} }
private void CollectVisibleRows(List<RepositoryNode> visible, AvaloniaList<RepositoryNode> collection)
{
foreach (var node in collection)
{
if (node.IsVisible)
{
visible.Add(node);
if (!node.IsRepository)
CollectVisibleRows(visible, node.SubNodes);
}
}
}
private static Welcome _instance = new Welcome(); private static Welcome _instance = new Welcome();
private string _searchFilter = string.Empty; private string _searchFilter = string.Empty;
} }

View file

@ -42,6 +42,7 @@
ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
Loaded="SetupTreeViewDragAndDrop" Loaded="SetupTreeViewDragAndDrop"
LostFocus="OnTreeViewLostFocus" LostFocus="OnTreeViewLostFocus"
SelectionChanged="OnTreeViewSelectionChanged"
KeyDown="OnTreeViewKeyDown"> KeyDown="OnTreeViewKeyDown">
<TreeView.ContextMenu> <TreeView.ContextMenu>
<ContextMenu> <ContextMenu>

View file

@ -48,7 +48,6 @@ namespace SourceGit.Views
if (c is TreeViewItem { IsVisible: true } item) if (c is TreeViewItem { IsVisible: true } item)
{ {
ReposTree.SelectedItem = item.DataContext; ReposTree.SelectedItem = item.DataContext;
item.Focus();
break; break;
} }
} }
@ -59,13 +58,41 @@ namespace SourceGit.Views
private void OnTreeViewKeyDown(object sender, KeyEventArgs e) private void OnTreeViewKeyDown(object sender, KeyEventArgs e)
{ {
if (e.Key == Key.Space && ReposTree.SelectedItem is ViewModels.RepositoryNode { IsRepository: true } node) if (ReposTree.SelectedItem is ViewModels.RepositoryNode node)
{ {
var parent = this.FindAncestorOfType<Launcher>(); if (e.Key == Key.Space && node.IsRepository)
if (parent?.DataContext is ViewModels.Launcher launcher) {
launcher.OpenRepositoryInTab(node, null); var parent = this.FindAncestorOfType<Launcher>();
if (parent?.DataContext is ViewModels.Launcher launcher)
launcher.OpenRepositoryInTab(node, null);
e.Handled = true; e.Handled = true;
}
else if (e.Key == Key.Down)
{
var next = ViewModels.Welcome.Instance.GetNextVisible(node);
if (next != null)
ReposTree.SelectedItem = next;
e.Handled = true;
}
else if (e.Key == Key.Up)
{
var prev = ViewModels.Welcome.Instance.GetPrevVisible(node);
if (prev != null)
ReposTree.SelectedItem = prev;
e.Handled = true;
}
}
}
private void OnTreeViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ReposTree.SelectedItem is ViewModels.RepositoryNode node)
{
var item = FindTreeViewItemByNode(node, ReposTree);
item?.Focus(NavigationMethod.Directional);
} }
} }
@ -264,6 +291,26 @@ namespace SourceGit.Views
launcher?.OpenRepositoryInTab(node, launcher.ActivePage); launcher?.OpenRepositoryInTab(node, launcher.ActivePage);
} }
private TreeViewItem FindTreeViewItemByNode(ViewModels.RepositoryNode node, ItemsControl container)
{
var items = container.GetRealizedContainers();
foreach (var item in items)
{
if (item is TreeViewItem { DataContext: ViewModels.RepositoryNode test } treeViewItem)
{
if (test == node)
return treeViewItem;
var child = FindTreeViewItemByNode(node, treeViewItem);
if (child != null)
return child;
}
}
return null;
}
private bool _pressedTreeNode = false; private bool _pressedTreeNode = false;
private Point _pressedTreeNodePosition = new Point(); private Point _pressedTreeNodePosition = new Point();
private bool _startDragTreeNode = false; private bool _startDragTreeNode = false;