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,15 +166,13 @@ namespace SourceGit.Views
{
if (lastUnselected == -1)
continue;
else
break;
}
else
{
lastUnselected = i;
}
}
}
if (lastUnselected != -1)
return tree.Rows[lastUnselected].Change;
@ -179,14 +186,12 @@ namespace SourceGit.Views
{
if (lastUnselected == -1)
continue;
else
break;
}
else
{
lastUnselected = i;
}
}
if (lastUnselected != -1)
return changes[lastUnselected];
@ -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,7 +8,7 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.RevisionFileTreeView"
x:Name="ThisControl">
<ListBox ItemsSource="{Binding #ThisControl.Rows}"
<v:RevisionFileRowsListBox ItemsSource="{Binding #ThisControl.Rows}"
Background="Transparent"
SelectionMode="Single"
SelectionChanged="OnRowsSelectionChanged"
@ -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,7 +45,7 @@
</TextBox>
<!-- Repository Tree -->
<ListBox Grid.Row="1"
<v:RepositoryListBox Grid.Row="1"
x:Name="TreeContainer"
Margin="0,8,8,0"
Focusable="True"
@ -145,7 +145,7 @@
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</v:RepositoryListBox>
</Grid>
</Grid>

View file

@ -26,6 +26,26 @@ namespace SourceGit.Views
}
}
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
{
public Welcome()