Skip to content

Commit 26307e2

Browse files
committed
refactor: new tooltip for change
Signed-off-by: leo <[email protected]>
1 parent db5bb0a commit 26307e2

File tree

4 files changed

+60
-30
lines changed

4 files changed

+60
-30
lines changed

src/Models/Change.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public class Change
5353
public bool IsConflicted => WorkTree == ChangeState.Conflicted;
5454
public string ConflictMarker => CONFLICT_MARKERS[(int)ConflictReason];
5555
public string ConflictDesc => CONFLICT_DESCS[(int)ConflictReason];
56+
57+
public string WorkTreeDesc => TYPE_DESCS[(int)WorkTree];
58+
public string IndexDesc => TYPE_DESCS[(int)Index];
5659

5760
public void Set(ChangeState index, ChangeState workTree = ChangeState.None)
5861
{
@@ -84,7 +87,19 @@ public void Set(ChangeState index, ChangeState workTree = ChangeState.None)
8487
if (!string.IsNullOrEmpty(OriginalPath) && OriginalPath[0] == '"')
8588
OriginalPath = OriginalPath.Substring(1, OriginalPath.Length - 2);
8689
}
87-
90+
91+
private static readonly string[] TYPE_DESCS =
92+
[
93+
"Unknown",
94+
"Modified",
95+
"Type Changed",
96+
"Added",
97+
"Deleted",
98+
"Renamed",
99+
"Copied",
100+
"Untracked",
101+
"Conflict"
102+
];
88103
private static readonly string[] CONFLICT_MARKERS =
89104
[
90105
string.Empty,

src/Views/ChangeCollectionView.axaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
4242
Background="Transparent"
4343
DoubleTapped="OnRowDoubleTapped"
44-
ToolTip.Tip="{Binding FullPath}">
44+
DataContextChanged="OnRowDataContextChanged">
4545
<v:ChangeTreeNodeToggleButton Grid.Column="0"
4646
Classes="tree_expander"
4747
Focusable="False"
@@ -85,7 +85,7 @@
8585
<Grid ColumnDefinitions="Auto,Auto,Auto,*"
8686
Background="Transparent"
8787
DoubleTapped="OnRowDoubleTapped"
88-
ToolTip.Tip="{Binding Path}">
88+
DataContextChanged="OnRowDataContextChanged">
8989
<v:ChangeStatusIcon Grid.Column="0"
9090
Width="14" Height="14"
9191
Margin="4,0,0,0"
@@ -118,7 +118,7 @@
118118
<Grid ColumnDefinitions="Auto,Auto,*"
119119
Background="Transparent"
120120
DoubleTapped="OnRowDoubleTapped"
121-
ToolTip.Tip="{Binding Path}">
121+
DataContextChanged="OnRowDataContextChanged">
122122
<v:ChangeStatusIcon Grid.Column="0"
123123
Width="14" Height="14"
124124
Margin="4,0,0,0"

src/Views/ChangeCollectionView.axaml.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
using Avalonia;
55
using Avalonia.Controls;
6+
using Avalonia.Controls.Documents;
67
using Avalonia.Controls.Primitives;
78
using Avalonia.Input;
89
using Avalonia.Interactivity;
10+
using Avalonia.Media;
911
using Avalonia.VisualTree;
1012

1113
namespace SourceGit.Views
@@ -86,7 +88,7 @@ public List<Models.Change> Changes
8688
}
8789

8890
public static readonly StyledProperty<bool> AutoSelectFirstChangeProperty =
89-
AvaloniaProperty.Register<ChangeCollectionView, bool>(nameof(AutoSelectFirstChange), false);
91+
AvaloniaProperty.Register<ChangeCollectionView, bool>(nameof(AutoSelectFirstChange));
9092

9193
public bool AutoSelectFirstChange
9294
{
@@ -229,6 +231,28 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
229231
UpdateSelection();
230232
}
231233

234+
private void OnRowDataContextChanged(object sender, EventArgs e)
235+
{
236+
if (sender is not Control control)
237+
return;
238+
239+
if (control.DataContext is ViewModels.ChangeTreeNode node)
240+
{
241+
if (node.Change is {} c)
242+
UpdateRowTips(control, c);
243+
else
244+
ToolTip.SetTip(control, node.FullPath);
245+
}
246+
else if (control.DataContext is Models.Change change)
247+
{
248+
UpdateRowTips(control, change);
249+
}
250+
else
251+
{
252+
ToolTip.SetTip(control, null);
253+
}
254+
}
255+
232256
private void OnRowDoubleTapped(object sender, TappedEventArgs e)
233257
{
234258
var grid = sender as Grid;
@@ -466,6 +490,21 @@ private void CollectChangesInNode(List<Models.Change> outs, ViewModels.ChangeTre
466490
}
467491
}
468492

493+
private void UpdateRowTips(Control control, Models.Change change)
494+
{
495+
var tip = new TextBlock() { TextWrapping = TextWrapping.Wrap };
496+
tip.Inlines!.Add(new Run(change.Path));
497+
tip.Inlines!.Add(new Run(" • ") { Foreground = Brushes.Gray });
498+
tip.Inlines!.Add(new Run(IsUnstagedChange ? change.WorkTreeDesc : change.IndexDesc) { Foreground = Brushes.Gray });
499+
if (change.IsConflicted)
500+
{
501+
tip.Inlines!.Add(new Run(" • ") { Foreground = Brushes.Gray });
502+
tip.Inlines!.Add(new Run(change.ConflictDesc) { Foreground = Brushes.Gray });
503+
}
504+
505+
ToolTip.SetTip(control, tip);
506+
}
507+
469508
private bool _disableSelectionChangingEvent = false;
470509
}
471510
}

src/Views/ChangeStatusIcon.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace SourceGit.Views
99
{
1010
public class ChangeStatusIcon : Control
1111
{
12+
private static readonly string[] INDICATOR = ["?", "±", "T", "+", "−", "➜", "❏", "★", "!"];
1213
private static readonly IBrush[] BACKGROUNDS = [
1314
Brushes.Transparent,
1415
new LinearGradientBrush
@@ -56,9 +57,6 @@ public class ChangeStatusIcon : Control
5657
Brushes.OrangeRed,
5758
];
5859

59-
private static readonly string[] INDICATOR = ["?", "±", "T", "+", "−", "➜", "❏", "★", "!"];
60-
private static readonly string[] TIPS = ["Unknown", "Modified", "Type Changed", "Added", "Deleted", "Renamed", "Copied", "Untracked", "Conflict"];
61-
6260
public static readonly StyledProperty<bool> IsUnstagedChangeProperty =
6361
AvaloniaProperty.Register<ChangeStatusIcon, bool>(nameof(IsUnstagedChange));
6462

@@ -116,29 +114,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
116114
base.OnPropertyChanged(change);
117115

118116
if (change.Property == IsUnstagedChangeProperty || change.Property == ChangeProperty)
119-
{
120-
var isUnstaged = IsUnstagedChange;
121-
var c = Change;
122-
if (c == null)
123-
{
124-
ToolTip.SetTip(this, null);
125-
return;
126-
}
127-
128-
if (isUnstaged)
129-
{
130-
if (c.IsConflicted)
131-
ToolTip.SetTip(this, $"Conflict ({c.ConflictDesc})");
132-
else
133-
ToolTip.SetTip(this, TIPS[(int)c.WorkTree]);
134-
}
135-
else
136-
{
137-
ToolTip.SetTip(this, TIPS[(int)c.Index]);
138-
}
139-
140117
InvalidateVisual();
141-
}
142118
}
143119
}
144120
}

0 commit comments

Comments
 (0)