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) 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); base.OnKeyDown(e);
} }
} }
@ -157,13 +166,11 @@ namespace SourceGit.Views
{ {
if (lastUnselected == -1) if (lastUnselected == -1)
continue; continue;
else
break; break;
}
else
{
lastUnselected = i;
} }
lastUnselected = i;
} }
} }
@ -179,13 +186,11 @@ namespace SourceGit.Views
{ {
if (lastUnselected == -1) if (lastUnselected == -1)
continue; continue;
else
break; break;
}
else
{
lastUnselected = i;
} }
lastUnselected = i;
} }
if (lastUnselected != -1) if (lastUnselected != -1)
@ -239,9 +244,9 @@ namespace SourceGit.Views
_disableSelectionChangingEvent = true; _disableSelectionChangingEvent = true;
var selected = new List<Models.Change>(); 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) if (item is Models.Change c)
selected.Add(c); selected.Add(c);

View file

@ -8,12 +8,12 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.RevisionFileTreeView" x:Class="SourceGit.Views.RevisionFileTreeView"
x:Name="ThisControl"> x:Name="ThisControl">
<ListBox ItemsSource="{Binding #ThisControl.Rows}" <v:RevisionFileRowsListBox ItemsSource="{Binding #ThisControl.Rows}"
Background="Transparent" Background="Transparent"
SelectionMode="Single" SelectionMode="Single"
SelectionChanged="OnRowsSelectionChanged" SelectionChanged="OnRowsSelectionChanged"
ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"> ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.ItemsPanel> <ListBox.ItemsPanel>
<ItemsPanelTemplate> <ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/> <VirtualizingStackPanel Orientation="Vertical"/>
@ -46,5 +46,5 @@
</Grid> </Grid>
</DataTemplate> </DataTemplate>
</ListBox.ItemTemplate> </ListBox.ItemTemplate>
</ListBox> </v:RevisionFileRowsListBox>
</UserControl> </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 partial class RevisionFileTreeView : UserControl
{ {
public static readonly StyledProperty<string> RevisionProperty = public static readonly StyledProperty<string> RevisionProperty =
@ -285,8 +305,8 @@ namespace SourceGit.Views
} }
} }
private List<ViewModels.RevisionFileTreeNode> _tree = new List<ViewModels.RevisionFileTreeNode>(); private List<ViewModels.RevisionFileTreeNode> _tree = [];
private AvaloniaList<ViewModels.RevisionFileTreeNode> _rows = new AvaloniaList<ViewModels.RevisionFileTreeNode>(); private AvaloniaList<ViewModels.RevisionFileTreeNode> _rows = [];
private bool _disableSelectionChangingEvent = false; private bool _disableSelectionChangingEvent = false;
} }
} }

View file

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

View file

@ -25,6 +25,26 @@ namespace SourceGit.Views
e.Handled = true; 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 public partial class Welcome : UserControl
{ {