Skip to content

Commit b2e01f0

Browse files
committed
feature: add tooltip for SHA in commit message presenter (#734)
Signed-off-by: leo <[email protected]>
1 parent 637f138 commit b2e01f0

File tree

4 files changed

+80
-11
lines changed

4 files changed

+80
-11
lines changed

src/Models/OpenAI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public OpenAIChatResponse Chat(string prompt, string question, CancellationToken
175175
var body = reader.Result;
176176
if (!rsp.IsSuccessStatusCode)
177177
{
178-
throw new Exception($"AI service returns error code {rsp.StatusCode}. Body: {body??string.Empty}");
178+
throw new Exception($"AI service returns error code {rsp.StatusCode}. Body: {body ?? string.Empty}");
179179
}
180180

181181
return JsonSerializer.Deserialize(reader.Result, JsonCodeGen.Default.OpenAIChatResponse);

src/Views/CommitBaseInfo.axaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@
211211
<Setter Property="MaxWidth" Value="800"/>
212212
</Style>
213213
</v:CommitMessagePresenter.Styles>
214+
215+
<v:CommitMessagePresenter.DataTemplates>
216+
<DataTemplate DataType="m:Commit">
217+
<StackPanel MinWidth="400" Orientation="Vertical">
218+
<Grid ColumnDefinitions="Auto,*,Auto">
219+
<v:Avatar Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
220+
<TextBlock Grid.Column="1" Classes="primary" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
221+
<TextBlock Grid.Column="2" Classes="primary" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
222+
</Grid>
223+
224+
<TextBlock Classes="primary" Margin="0,8,0,0" Text="{Binding Subject}" TextWrapping="Wrap"/>
225+
</StackPanel>
226+
</DataTemplate>
227+
</v:CommitMessagePresenter.DataTemplates>
214228
</v:CommitMessagePresenter>
215229
</Grid>
216230
</StackPanel>

src/Views/CommitMessagePresenter.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text.RegularExpressions;
4+
using System.Threading.Tasks;
45

56
using Avalonia;
67
using Avalonia.Collections;
78
using Avalonia.Controls;
89
using Avalonia.Controls.Documents;
910
using Avalonia.Input;
11+
using Avalonia.Threading;
1012
using Avalonia.VisualTree;
1113

1214
namespace SourceGit.Views
@@ -43,7 +45,9 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
4345
if (change.Property == MessageProperty || change.Property == IssueTrackerRulesProperty)
4446
{
4547
Inlines!.Clear();
48+
_inlineCommits.Clear();
4649
_matches = null;
50+
_lastHover = null;
4751
ClearHoveredIssueLink();
4852

4953
var message = Message;
@@ -154,6 +158,10 @@ protected override void OnPointerMoved(PointerEventArgs e)
154158
ToolTip.SetTip(this, match.Link);
155159
ToolTip.SetIsOpen(this, true);
156160
}
161+
else
162+
{
163+
ProcessHoverCommitLink(match);
164+
}
157165

158166
return;
159167
}
@@ -256,6 +264,52 @@ protected override void OnPointerExited(PointerEventArgs e)
256264
ClearHoveredIssueLink();
257265
}
258266

267+
private void ProcessHoverCommitLink(Models.Hyperlink link)
268+
{
269+
var sha = link.Link;
270+
271+
// If we have already queried this SHA, just use it.
272+
if (_inlineCommits.TryGetValue(sha, out var exist))
273+
{
274+
if (exist != null)
275+
{
276+
ToolTip.SetTip(this, exist);
277+
ToolTip.SetIsOpen(this, true);
278+
}
279+
280+
return;
281+
}
282+
283+
var parentView = this.FindAncestorOfType<CommitBaseInfo>();
284+
if (parentView is { DataContext: ViewModels.CommitDetail detail })
285+
{
286+
// Record the SHA of current viewing commit in the CommitDetail panel to determine if it is changed after
287+
// asynchronous queries.
288+
var lastDetailCommit = detail.Commit.SHA;
289+
Task.Run(() =>
290+
{
291+
var c = detail.GetParent(sha);
292+
Dispatcher.UIThread.Invoke(() =>
293+
{
294+
// Make sure the DataContext of CommitBaseInfo is not changed.
295+
var currentParent = this.FindAncestorOfType<CommitBaseInfo>();
296+
if (currentParent is { DataContext: ViewModels.CommitDetail currentDetail } &&
297+
currentDetail.Commit.SHA == lastDetailCommit)
298+
{
299+
_inlineCommits.Add(sha, c);
300+
301+
// Make sure user still hovers the target SHA.
302+
if (_lastHover == link)
303+
{
304+
ToolTip.SetTip(this, c);
305+
ToolTip.SetIsOpen(this, true);
306+
}
307+
}
308+
});
309+
});
310+
}
311+
}
312+
259313
private void ClearHoveredIssueLink()
260314
{
261315
if (_lastHover != null)
@@ -268,5 +322,6 @@ private void ClearHoveredIssueLink()
268322

269323
private List<Models.Hyperlink> _matches = null;
270324
private Models.Hyperlink _lastHover = null;
325+
private Dictionary<string, Models.Commit> _inlineCommits = new();
271326
}
272327
}

src/Views/TextDiffView.axaml.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public TextDiffViewChunk SelectedChunk
476476
get => GetValue(SelectedChunkProperty);
477477
set => SetValue(SelectedChunkProperty, value);
478478
}
479-
479+
480480
public static readonly StyledProperty<TextDiffViewRange> DisplayRangeProperty =
481481
AvaloniaProperty.Register<ThemedTextDiffPresenter, TextDiffViewRange>(nameof(DisplayRange), new TextDiffViewRange(0, 0));
482482

@@ -523,7 +523,7 @@ public void GotoPrevChange()
523523
var firstLineIdx = DisplayRange.StartIdx;
524524
if (firstLineIdx <= 1)
525525
return;
526-
526+
527527
var lines = GetLines();
528528
var firstLineType = lines[firstLineIdx].Type;
529529
var prevLineType = lines[firstLineIdx - 1].Type;
@@ -761,7 +761,7 @@ private void OnTextViewVisualLinesChanged(object sender, EventArgs e)
761761
if (start > index)
762762
start = index;
763763
}
764-
764+
765765
SetCurrentValue(DisplayRangeProperty, new TextDiffViewRange(start, start + count));
766766
}
767767

@@ -1313,9 +1313,9 @@ private void OnTextAreaPointerWheelChanged(object sender, PointerWheelEventArgs
13131313

13141314
private ScrollViewer _scrollViewer = null;
13151315
}
1316-
1316+
13171317
public class TextDiffViewMinimap : Control
1318-
{
1318+
{
13191319
public static readonly StyledProperty<IBrush> AddedLineBrushProperty =
13201320
AvaloniaProperty.Register<TextDiffViewMinimap, IBrush>(nameof(AddedLineBrush), new SolidColorBrush(Color.FromArgb(60, 0, 255, 0)));
13211321

@@ -1333,7 +1333,7 @@ public IBrush DeletedLineBrush
13331333
get => GetValue(DeletedLineBrushProperty);
13341334
set => SetValue(DeletedLineBrushProperty, value);
13351335
}
1336-
1336+
13371337
public static readonly StyledProperty<TextDiffViewRange> DisplayRangeProperty =
13381338
AvaloniaProperty.Register<TextDiffViewMinimap, TextDiffViewRange>(nameof(DisplayRange), new TextDiffViewRange(0, 0));
13391339

@@ -1342,7 +1342,7 @@ public TextDiffViewRange DisplayRange
13421342
get => GetValue(DisplayRangeProperty);
13431343
set => SetValue(DisplayRangeProperty, value);
13441344
}
1345-
1345+
13461346
public static readonly StyledProperty<Color> DisplayRangeColorProperty =
13471347
AvaloniaProperty.Register<TextDiffViewMinimap, Color>(nameof(DisplayRangeColor), Colors.RoyalBlue);
13481348

@@ -1376,7 +1376,7 @@ public override void Render(DrawingContext context)
13761376
total = diff.Lines.Count;
13771377
RenderSingleSide(context, diff.Lines, 0, Bounds.Width);
13781378
}
1379-
1379+
13801380
var range = DisplayRange;
13811381
if (range.EndIdx == 0)
13821382
return;
@@ -1416,7 +1416,7 @@ private void RenderSingleSide(DrawingContext context, List<Models.TextDiffLine>
14161416
lastLineTypeStart = i;
14171417
}
14181418
}
1419-
1419+
14201420
RenderBlock(context, lastLineType, lastLineTypeStart, total - lastLineTypeStart, total, x, width);
14211421
}
14221422

@@ -1431,7 +1431,7 @@ private void RenderBlock(DrawingContext context, Models.TextDiffLineType type, i
14311431
}
14321432
}
14331433
}
1434-
1434+
14351435
public partial class TextDiffView : UserControl
14361436
{
14371437
public static readonly StyledProperty<bool> UseSideBySideDiffProperty =

0 commit comments

Comments
 (0)