refactor: use PointerPressed event instead of ListBox.SelectionChanged event to navigate to commit (#1230)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-04-23 10:17:14 +08:00
parent 345ad06aba
commit 7890f7abbf
No known key found for this signature in database
4 changed files with 133 additions and 101 deletions

View file

@ -32,6 +32,7 @@
<ListBox.ItemTemplate>
<DataTemplate DataType="vm:BranchTreeNode">
<Border Background="Transparent" PointerPressed="OnNodePointerPressed">
<Grid Height="24"
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
ColumnDefinitions="16,*"
@ -87,6 +88,7 @@
Mode="{Binding FilterMode}"/>
</Grid>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

View file

@ -318,6 +318,31 @@ namespace SourceGit.Views
}
}
private void OnNodePointerPressed(object sender, PointerPressedEventArgs e)
{
var p = e.GetCurrentPoint(this);
if (!p.Properties.IsLeftButtonPressed)
return;
if (DataContext is not ViewModels.Repository repo)
return;
if (sender is not Border { DataContext: ViewModels.BranchTreeNode node })
return;
if (node.Backend is not Models.Branch branch)
return;
if (BranchesPresenter.SelectedItems is { Count: > 0 })
{
var ctrl = OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control;
if (e.KeyModifiers.HasFlag(ctrl) || e.KeyModifiers.HasFlag(KeyModifiers.Shift))
return;
}
repo.NavigateToCommit(branch.Head);
}
private void OnNodesSelectionChanged(object _, SelectionChangedEventArgs e)
{
if (_disableSelectionChangingEvent)
@ -343,9 +368,6 @@ namespace SourceGit.Views
if (selected == null || selected.Count == 0)
return;
if (selected.Count == 1 && selected[0] is ViewModels.BranchTreeNode { Backend: Models.Branch branch })
repo.NavigateToCommit(branch.Head);
var prev = null as ViewModels.BranchTreeNode;
foreach (var row in Rows)
{

View file

@ -26,11 +26,10 @@
SelectionChanged="OnRowSelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate DataType="vm:TagTreeNode">
<Border Height="24" Background="Transparent" PointerPressed="OnRowPointerPressed" DoubleTapped="OnDoubleTappedNode" ContextRequested="OnRowContextRequested">
<Grid ColumnDefinitions="16,Auto,*,Auto"
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
Background="Transparent"
ContextRequested="OnRowContextRequested"
DoubleTapped="OnDoubleTappedNode"
VerticalAlignment="Center"
ToolTip.Tip="{Binding ToolTip}">
<v:TagTreeNodeToggleButton Grid.Column="0"
Classes="tree_expander"
@ -56,6 +55,7 @@
</ContentControl.DataTemplates>
</ContentControl>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
@ -69,10 +69,8 @@
SelectionChanged="OnRowSelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate DataType="m:Tag">
<Grid ColumnDefinitions="Auto,*,Auto"
Background="Transparent"
ContextRequested="OnRowContextRequested"
ToolTip.Tip="{Binding Message}">
<Border Height="24" Background="Transparent" PointerPressed="OnRowPointerPressed" ContextRequested="OnRowContextRequested">
<Grid ColumnDefinitions="Auto,*,Auto" VerticalAlignment="Center" ToolTip.Tip="{Binding Message}">
<Path Grid.Column="0"
Margin="8,0,0,0"
Width="12" Height="12"
@ -86,6 +84,7 @@
<v:FilterModeSwitchButton Grid.Column="2" Margin="0,0,12,0" Mode="{Binding FilterMode}"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

View file

@ -199,15 +199,27 @@ namespace SourceGit.Views
private void OnDoubleTappedNode(object sender, TappedEventArgs e)
{
if (sender is Grid { DataContext: ViewModels.TagTreeNode node })
{
if (node.IsFolder)
if (sender is Control { DataContext: ViewModels.TagTreeNode { IsFolder: true } node })
ToggleNodeIsExpanded(node);
}
e.Handled = true;
}
private void OnRowPointerPressed(object sender, PointerPressedEventArgs e)
{
var p = e.GetCurrentPoint(this);
if (!p.Properties.IsLeftButtonPressed)
return;
if (DataContext is not ViewModels.Repository repo)
return;
if (sender is Control { DataContext: Models.Tag tag })
repo.NavigateToCommit(tag.SHA);
else if (sender is Control { DataContext: ViewModels.TagTreeNode { Tag: { } nodeTag } })
repo.NavigateToCommit(nodeTag.SHA);
}
private void OnRowContextRequested(object sender, ContextRequestedEventArgs e)
{
var control = sender as Control;
@ -240,11 +252,8 @@ namespace SourceGit.Views
else if (selected is Models.Tag tag)
selectedTag = tag;
if (selectedTag != null && DataContext is ViewModels.Repository repo)
{
if (selectedTag != null)
RaiseEvent(new RoutedEventArgs(SelectionChangedEvent));
repo.NavigateToCommit(selectedTag.SHA);
}
}
private void MakeTreeRows(List<ViewModels.TagTreeNode> rows, List<ViewModels.TagTreeNode> nodes)