feature: support using left/right arrow to expand/collapse tree node (#463)

This commit is contained in:
leo 2024-09-22 20:48:36 +08:00
parent 01380ff194
commit 07cba1cf5f
No known key found for this signature in database
5 changed files with 82 additions and 37 deletions

View file

@ -33,7 +33,16 @@ namespace SourceGit.Views
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key != Key.Space)
if (SelectedItems is [ViewModels.ChangeTreeNode { IsFolder: true } node] && e.KeyModifiers == KeyModifiers.None)
{
if ((node.IsExpanded && e.Key == Key.Left) || (!node.IsExpanded && e.Key == Key.Right))
{
this.FindAncestorOfType<ChangeCollectionView>()?.ToggleNodeIsExpanded(node);
e.Handled = true;
}
}
if (!e.Handled && e.Key != Key.Space)
base.OnKeyDown(e);
}
}
@ -157,13 +166,11 @@ namespace SourceGit.Views
{
if (lastUnselected == -1)
continue;
else
break;
}
else
{
lastUnselected = i;
break;
}
lastUnselected = i;
}
}
@ -179,13 +186,11 @@ namespace SourceGit.Views
{
if (lastUnselected == -1)
continue;
else
break;
}
else
{
lastUnselected = i;
break;
}
lastUnselected = i;
}
if (lastUnselected != -1)
@ -239,9 +244,9 @@ namespace SourceGit.Views
_disableSelectionChangingEvent = true;
var selected = new List<Models.Change>();
if (sender is ListBox list)
if (sender is ListBox { SelectedItems: {} selectedItems })
{
foreach (var item in list.SelectedItems)
foreach (var item in selectedItems)
{
if (item is Models.Change c)
selected.Add(c);

View file

@ -8,12 +8,12 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.RevisionFileTreeView"
x:Name="ThisControl">
<ListBox ItemsSource="{Binding #ThisControl.Rows}"
Background="Transparent"
SelectionMode="Single"
SelectionChanged="OnRowsSelectionChanged"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<v:RevisionFileRowsListBox ItemsSource="{Binding #ThisControl.Rows}"
Background="Transparent"
SelectionMode="Single"
SelectionChanged="OnRowsSelectionChanged"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
@ -46,5 +46,5 @@
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</v:RevisionFileRowsListBox>
</UserControl>

View file

@ -103,6 +103,26 @@ namespace SourceGit.Views
}
}
public class RevisionFileRowsListBox : ListBox
{
protected override Type StyleKeyOverride => typeof(ListBox);
protected override void OnKeyDown(KeyEventArgs e)
{
if (SelectedItems is [ViewModels.RevisionFileTreeNode { IsFolder: true } node] && e.KeyModifiers == KeyModifiers.None)
{
if ((node.IsExpanded && e.Key == Key.Left) || (!node.IsExpanded && e.Key == Key.Right))
{
this.FindAncestorOfType<RevisionFileTreeView>()?.ToggleNodeIsExpanded(node);
e.Handled = true;
}
}
if (!e.Handled)
base.OnKeyDown(e);
}
}
public partial class RevisionFileTreeView : UserControl
{
public static readonly StyledProperty<string> RevisionProperty =
@ -285,8 +305,8 @@ namespace SourceGit.Views
}
}
private List<ViewModels.RevisionFileTreeNode> _tree = new List<ViewModels.RevisionFileTreeNode>();
private AvaloniaList<ViewModels.RevisionFileTreeNode> _rows = new AvaloniaList<ViewModels.RevisionFileTreeNode>();
private List<ViewModels.RevisionFileTreeNode> _tree = [];
private AvaloniaList<ViewModels.RevisionFileTreeNode> _rows = [];
private bool _disableSelectionChangingEvent = false;
}
}

View file

@ -45,18 +45,18 @@
</TextBox>
<!-- Repository Tree -->
<ListBox Grid.Row="1"
x:Name="TreeContainer"
Margin="0,8,8,0"
Focusable="True"
Background="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Rows}"
SelectionMode="Single"
Loaded="SetupTreeViewDragAndDrop"
LostFocus="OnTreeViewLostFocus"
KeyDown="OnTreeViewKeyDown">
<v:RepositoryListBox Grid.Row="1"
x:Name="TreeContainer"
Margin="0,8,8,0"
Focusable="True"
Background="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Rows}"
SelectionMode="Single"
Loaded="SetupTreeViewDragAndDrop"
LostFocus="OnTreeViewLostFocus"
KeyDown="OnTreeViewKeyDown">
<ListBox.Styles>
<Style Selector="ListBox">
<Setter Property="FocusAdorner">
@ -145,7 +145,7 @@
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</v:RepositoryListBox>
</Grid>
</Grid>

View file

@ -25,6 +25,26 @@ namespace SourceGit.Views
e.Handled = true;
}
}
public class RepositoryListBox : ListBox
{
protected override Type StyleKeyOverride => typeof(ListBox);
protected override void OnKeyDown(KeyEventArgs e)
{
if (SelectedItems is [ViewModels.RepositoryNode { IsRepository: false } node] && e.KeyModifiers == KeyModifiers.None)
{
if ((node.IsExpanded && e.Key == Key.Left) || (!node.IsExpanded && e.Key == Key.Right))
{
ViewModels.Welcome.Instance.ToggleNodeIsExpanded(node);
e.Handled = true;
}
}
if (!e.Handled)
base.OnKeyDown(e);
}
}
public partial class Welcome : UserControl
{