Skip to content

Commit 7890f7a

Browse files
committed
refactor: use PointerPressed event instead of ListBox.SelectionChanged event to navigate to commit (#1230)
Signed-off-by: leo <[email protected]>
1 parent 345ad06 commit 7890f7a

File tree

4 files changed

+133
-101
lines changed

4 files changed

+133
-101
lines changed

src/Views/BranchTree.axaml

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32,61 +32,63 @@
3232

3333
<ListBox.ItemTemplate>
3434
<DataTemplate DataType="vm:BranchTreeNode">
35-
<Grid Height="24"
36-
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
37-
ColumnDefinitions="16,*"
38-
ToolTip.Tip="{Binding Tooltip}">
35+
<Border Background="Transparent" PointerPressed="OnNodePointerPressed">
36+
<Grid Height="24"
37+
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
38+
ColumnDefinitions="16,*"
39+
ToolTip.Tip="{Binding Tooltip}">
3940

40-
<!-- Tree Expander -->
41-
<v:BranchTreeNodeToggleButton Grid.Column="0"
42-
Classes="tree_expander"
43-
Focusable="False"
44-
HorizontalAlignment="Center"
45-
IsChecked="{Binding IsExpanded, Mode=OneWay}"
46-
IsVisible="{Binding !IsBranch}"/>
41+
<!-- Tree Expander -->
42+
<v:BranchTreeNodeToggleButton Grid.Column="0"
43+
Classes="tree_expander"
44+
Focusable="False"
45+
HorizontalAlignment="Center"
46+
IsChecked="{Binding IsExpanded, Mode=OneWay}"
47+
IsVisible="{Binding !IsBranch}"/>
4748

48-
<!-- Content Area (allows double-click) -->
49-
<Grid Grid.Column="1"
50-
Background="Transparent"
51-
ColumnDefinitions="18,*,Auto,Auto"
52-
DoubleTapped="OnDoubleTappedBranchNode">
49+
<!-- Content Area (allows double-click) -->
50+
<Grid Grid.Column="1"
51+
Background="Transparent"
52+
ColumnDefinitions="18,*,Auto,Auto"
53+
DoubleTapped="OnDoubleTappedBranchNode">
5354

54-
<!-- Icon -->
55-
<v:BranchTreeNodeIcon Grid.Column="0"
56-
Node="{Binding}"
57-
IsExpanded="{Binding IsExpanded}"/>
55+
<!-- Icon -->
56+
<v:BranchTreeNodeIcon Grid.Column="0"
57+
Node="{Binding}"
58+
IsExpanded="{Binding IsExpanded}"/>
5859

59-
<!-- Name -->
60-
<TextBlock Grid.Column="1"
61-
Classes="primary"
62-
Text="{Binding Name}"
63-
FontWeight="{Binding IsCurrent, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
64-
TextTrimming="CharacterEllipsis"/>
60+
<!-- Name -->
61+
<TextBlock Grid.Column="1"
62+
Classes="primary"
63+
Text="{Binding Name}"
64+
FontWeight="{Binding IsCurrent, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
65+
TextTrimming="CharacterEllipsis"/>
6566

66-
<!-- Upstream invalid tip -->
67-
<Border Grid.Column="2"
68-
Width="12" Height="12"
69-
Margin="8,0"
70-
Background="Transparent"
71-
ToolTip.Tip="{DynamicResource Text.BranchUpstreamInvalid}"
72-
IsVisible="{Binding ShowUpstreamGoneTip}">
73-
<Path Data="{StaticResource Icons.Error}" Fill="DarkOrange"/>
74-
</Border>
67+
<!-- Upstream invalid tip -->
68+
<Border Grid.Column="2"
69+
Width="12" Height="12"
70+
Margin="8,0"
71+
Background="Transparent"
72+
ToolTip.Tip="{DynamicResource Text.BranchUpstreamInvalid}"
73+
IsVisible="{Binding ShowUpstreamGoneTip}">
74+
<Path Data="{StaticResource Icons.Error}" Fill="DarkOrange"/>
75+
</Border>
7576

76-
<!-- Tracking status -->
77-
<v:BranchTreeNodeTrackStatusPresenter Grid.Column="2"
78-
VerticalAlignment="Center"
79-
FontFamily="{DynamicResource Fonts.Monospace}"
80-
FontSize="10"
81-
Foreground="{DynamicResource Brush.BadgeFG}"
82-
Background="{DynamicResource Brush.Badge}"/>
77+
<!-- Tracking status -->
78+
<v:BranchTreeNodeTrackStatusPresenter Grid.Column="2"
79+
VerticalAlignment="Center"
80+
FontFamily="{DynamicResource Fonts.Monospace}"
81+
FontSize="10"
82+
Foreground="{DynamicResource Brush.BadgeFG}"
83+
Background="{DynamicResource Brush.Badge}"/>
8384

84-
<!-- Filter Mode Switcher -->
85-
<v:FilterModeSwitchButton Grid.Column="3"
86-
Margin="0,0,12,0"
87-
Mode="{Binding FilterMode}"/>
85+
<!-- Filter Mode Switcher -->
86+
<v:FilterModeSwitchButton Grid.Column="3"
87+
Margin="0,0,12,0"
88+
Mode="{Binding FilterMode}"/>
89+
</Grid>
8890
</Grid>
89-
</Grid>
91+
</Border>
9092
</DataTemplate>
9193
</ListBox.ItemTemplate>
9294
</ListBox>

src/Views/BranchTree.axaml.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,31 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
318318
}
319319
}
320320

321+
private void OnNodePointerPressed(object sender, PointerPressedEventArgs e)
322+
{
323+
var p = e.GetCurrentPoint(this);
324+
if (!p.Properties.IsLeftButtonPressed)
325+
return;
326+
327+
if (DataContext is not ViewModels.Repository repo)
328+
return;
329+
330+
if (sender is not Border { DataContext: ViewModels.BranchTreeNode node })
331+
return;
332+
333+
if (node.Backend is not Models.Branch branch)
334+
return;
335+
336+
if (BranchesPresenter.SelectedItems is { Count: > 0 })
337+
{
338+
var ctrl = OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control;
339+
if (e.KeyModifiers.HasFlag(ctrl) || e.KeyModifiers.HasFlag(KeyModifiers.Shift))
340+
return;
341+
}
342+
343+
repo.NavigateToCommit(branch.Head);
344+
}
345+
321346
private void OnNodesSelectionChanged(object _, SelectionChangedEventArgs e)
322347
{
323348
if (_disableSelectionChangingEvent)
@@ -343,9 +368,6 @@ private void OnNodesSelectionChanged(object _, SelectionChangedEventArgs e)
343368
if (selected == null || selected.Count == 0)
344369
return;
345370

346-
if (selected.Count == 1 && selected[0] is ViewModels.BranchTreeNode { Backend: Models.Branch branch })
347-
repo.NavigateToCommit(branch.Head);
348-
349371
var prev = null as ViewModels.BranchTreeNode;
350372
foreach (var row in Rows)
351373
{

src/Views/TagsView.axaml

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,36 @@
2626
SelectionChanged="OnRowSelectionChanged">
2727
<ListBox.ItemTemplate>
2828
<DataTemplate DataType="vm:TagTreeNode">
29-
<Grid ColumnDefinitions="16,Auto,*,Auto"
30-
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
31-
Background="Transparent"
32-
ContextRequested="OnRowContextRequested"
33-
DoubleTapped="OnDoubleTappedNode"
34-
ToolTip.Tip="{Binding ToolTip}">
35-
<v:TagTreeNodeToggleButton Grid.Column="0"
36-
Classes="tree_expander"
37-
Focusable="False"
38-
HorizontalAlignment="Center"
39-
IsChecked="{Binding IsExpanded, Mode=OneWay}"
40-
IsVisible="{Binding IsFolder}"/>
29+
<Border Height="24" Background="Transparent" PointerPressed="OnRowPointerPressed" DoubleTapped="OnDoubleTappedNode" ContextRequested="OnRowContextRequested">
30+
<Grid ColumnDefinitions="16,Auto,*,Auto"
31+
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
32+
VerticalAlignment="Center"
33+
ToolTip.Tip="{Binding ToolTip}">
34+
<v:TagTreeNodeToggleButton Grid.Column="0"
35+
Classes="tree_expander"
36+
Focusable="False"
37+
HorizontalAlignment="Center"
38+
IsChecked="{Binding IsExpanded, Mode=OneWay}"
39+
IsVisible="{Binding IsFolder}"/>
4140

42-
<v:TagTreeNodeIcon Grid.Column="1"
43-
Node="{Binding .}"
44-
IsExpanded="{Binding IsExpanded, Mode=OneWay}"/>
41+
<v:TagTreeNodeIcon Grid.Column="1"
42+
Node="{Binding .}"
43+
IsExpanded="{Binding IsExpanded, Mode=OneWay}"/>
4544

46-
<TextBlock Grid.Column="2"
47-
Classes="primary"
48-
Text="{Binding FullPath, Converter={x:Static c:PathConverters.PureFileName}}"
49-
Margin="8,0,0,0"/>
45+
<TextBlock Grid.Column="2"
46+
Classes="primary"
47+
Text="{Binding FullPath, Converter={x:Static c:PathConverters.PureFileName}}"
48+
Margin="8,0,0,0"/>
5049

51-
<ContentControl Grid.Column="3" Content="{Binding Tag}">
52-
<ContentControl.DataTemplates>
53-
<DataTemplate DataType="m:Tag">
54-
<v:FilterModeSwitchButton Margin="0,0,12,0" Mode="{Binding FilterMode}"/>
55-
</DataTemplate>
56-
</ContentControl.DataTemplates>
57-
</ContentControl>
58-
</Grid>
50+
<ContentControl Grid.Column="3" Content="{Binding Tag}">
51+
<ContentControl.DataTemplates>
52+
<DataTemplate DataType="m:Tag">
53+
<v:FilterModeSwitchButton Margin="0,0,12,0" Mode="{Binding FilterMode}"/>
54+
</DataTemplate>
55+
</ContentControl.DataTemplates>
56+
</ContentControl>
57+
</Grid>
58+
</Border>
5959
</DataTemplate>
6060
</ListBox.ItemTemplate>
6161
</ListBox>
@@ -69,23 +69,22 @@
6969
SelectionChanged="OnRowSelectionChanged">
7070
<ListBox.ItemTemplate>
7171
<DataTemplate DataType="m:Tag">
72-
<Grid ColumnDefinitions="Auto,*,Auto"
73-
Background="Transparent"
74-
ContextRequested="OnRowContextRequested"
75-
ToolTip.Tip="{Binding Message}">
76-
<Path Grid.Column="0"
77-
Margin="8,0,0,0"
78-
Width="12" Height="12"
79-
Data="{StaticResource Icons.Tag}"/>
72+
<Border Height="24" Background="Transparent" PointerPressed="OnRowPointerPressed" ContextRequested="OnRowContextRequested">
73+
<Grid ColumnDefinitions="Auto,*,Auto" VerticalAlignment="Center" ToolTip.Tip="{Binding Message}">
74+
<Path Grid.Column="0"
75+
Margin="8,0,0,0"
76+
Width="12" Height="12"
77+
Data="{StaticResource Icons.Tag}"/>
8078

81-
<TextBlock Grid.Column="1"
82-
Classes="primary"
83-
Text="{Binding Name}"
84-
Margin="8,0,0,0"
85-
TextTrimming="CharacterEllipsis"/>
79+
<TextBlock Grid.Column="1"
80+
Classes="primary"
81+
Text="{Binding Name}"
82+
Margin="8,0,0,0"
83+
TextTrimming="CharacterEllipsis"/>
8684

87-
<v:FilterModeSwitchButton Grid.Column="2" Margin="0,0,12,0" Mode="{Binding FilterMode}"/>
88-
</Grid>
85+
<v:FilterModeSwitchButton Grid.Column="2" Margin="0,0,12,0" Mode="{Binding FilterMode}"/>
86+
</Grid>
87+
</Border>
8988
</DataTemplate>
9089
</ListBox.ItemTemplate>
9190
</ListBox>

src/Views/TagsView.axaml.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,27 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
199199

200200
private void OnDoubleTappedNode(object sender, TappedEventArgs e)
201201
{
202-
if (sender is Grid { DataContext: ViewModels.TagTreeNode node })
203-
{
204-
if (node.IsFolder)
205-
ToggleNodeIsExpanded(node);
206-
}
202+
if (sender is Control { DataContext: ViewModels.TagTreeNode { IsFolder: true } node })
203+
ToggleNodeIsExpanded(node);
207204

208205
e.Handled = true;
209206
}
210207

208+
private void OnRowPointerPressed(object sender, PointerPressedEventArgs e)
209+
{
210+
var p = e.GetCurrentPoint(this);
211+
if (!p.Properties.IsLeftButtonPressed)
212+
return;
213+
214+
if (DataContext is not ViewModels.Repository repo)
215+
return;
216+
217+
if (sender is Control { DataContext: Models.Tag tag })
218+
repo.NavigateToCommit(tag.SHA);
219+
else if (sender is Control { DataContext: ViewModels.TagTreeNode { Tag: { } nodeTag } })
220+
repo.NavigateToCommit(nodeTag.SHA);
221+
}
222+
211223
private void OnRowContextRequested(object sender, ContextRequestedEventArgs e)
212224
{
213225
var control = sender as Control;
@@ -240,11 +252,8 @@ private void OnRowSelectionChanged(object sender, SelectionChangedEventArgs _)
240252
else if (selected is Models.Tag tag)
241253
selectedTag = tag;
242254

243-
if (selectedTag != null && DataContext is ViewModels.Repository repo)
244-
{
255+
if (selectedTag != null)
245256
RaiseEvent(new RoutedEventArgs(SelectionChangedEvent));
246-
repo.NavigateToCommit(selectedTag.SHA);
247-
}
248257
}
249258

250259
private void MakeTreeRows(List<ViewModels.TagTreeNode> rows, List<ViewModels.TagTreeNode> nodes)

0 commit comments

Comments
 (0)