Skip to content

Commit 6798a81

Browse files
committed
feat: show CommitRelationTracking on CommitGraph (#940)
- Add SHA property to the Dot class and assign commit SHA to anchor instances in the CommitGraph model. - Add constructor to initialize commit relation tracking with repository path and commit hash, enabling querying and displaying related refs while managing loading state. - Add pointer interaction handlers to the `CommitGraph` class for displaying commit details on click, updating cursor on hover, and resetting cursor on exit.
1 parent dd254eb commit 6798a81

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/Models/CommitGraph.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class Dot
5454
public Point Center;
5555
public int Color;
5656
public bool IsMerged;
57+
public string SHA;
5758
}
5859

5960
public List<Path> Paths { get; } = [];
@@ -149,7 +150,7 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
149150
// Calculate link position of this commit.
150151
var position = new Point(major?.LastX ?? offsetX, offsetY);
151152
var dotColor = major?.Path.Color ?? 0;
152-
var anchor = new Dot() { Center = position, Color = dotColor, IsMerged = isMerged };
153+
var anchor = new Dot() { Center = position, Color = dotColor, IsMerged = isMerged, SHA = commit.SHA };
153154
if (commit.IsCurrentHead)
154155
anchor.Type = DotType.Head;
155156
else if (commit.Parents.Count > 1)

src/Views/CommitRelationTracking.axaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,22 @@ public CommitRelationTracking(ViewModels.CommitDetail detail)
2828
});
2929
});
3030
}
31+
32+
public CommitRelationTracking(string repoPath, string commitHash)
33+
{
34+
InitializeComponent();
35+
36+
LoadingIcon.IsVisible = true;
37+
38+
Task.Run(() =>
39+
{
40+
var containsIn = new Commands.QueryRefsContainsCommit(repoPath, commitHash).Result();
41+
Dispatcher.UIThread.Invoke(() =>
42+
{
43+
Container.ItemsSource = containsIn;
44+
LoadingIcon.IsVisible = false;
45+
});
46+
});
47+
}
3148
}
3249
}

src/Views/Histories.axaml.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,13 @@ static CommitGraph()
520520
AffectsRender<CommitGraph>(GraphProperty, DotBrushProperty, OnlyHighlightCurrentBranchProperty);
521521
}
522522

523+
public CommitGraph()
524+
{
525+
PointerPressed += OnPointerPressed;
526+
PointerMoved += OnPointerMoved;
527+
PointerExited += OnPointerExited;
528+
}
529+
523530
public override void Render(DrawingContext context)
524531
{
525532
base.Render(context);
@@ -705,6 +712,56 @@ private void DrawAnchors(DrawingContext context, Models.CommitGraph graph, doubl
705712
}
706713
}
707714
}
715+
716+
private void OnPointerPressed(object sender, PointerPressedEventArgs e)
717+
{
718+
var point = e.GetPosition(this);
719+
var dot = FindDotAtPosition(point);
720+
var repoView = this.FindAncestorOfType<Repository>();
721+
var repoPath = (repoView?.DataContext as ViewModels.Repository)?.FullPath;
722+
if (dot == null || string.IsNullOrEmpty(repoPath))
723+
return;
724+
725+
var tracking = new CommitRelationTracking(repoPath, dot.SHA);
726+
var flyout = new Flyout { Content = tracking };
727+
flyout.ShowAt(this, true);
728+
}
729+
730+
private void OnPointerMoved(object sender, PointerEventArgs e)
731+
{
732+
var point = e.GetPosition(this);
733+
var dot = FindDotAtPosition(point);
734+
Cursor = dot != null ? new Cursor(StandardCursorType.Hand) : new Cursor(StandardCursorType.Arrow);
735+
}
736+
737+
private void OnPointerExited(object sender, PointerEventArgs e)
738+
{
739+
Cursor = new Cursor(StandardCursorType.Arrow);
740+
}
741+
742+
private Models.CommitGraph.Dot FindDotAtPosition(Point point)
743+
{
744+
var graph = Graph;
745+
if (graph == null)
746+
return null;
747+
748+
// get scroll offset
749+
var histories = this.FindAncestorOfType<Histories>();
750+
var scrollOffset = histories?.CommitListContainer.Scroll?.Offset.Y ?? 0;
751+
752+
// adjust point
753+
var adjustedPoint = new Point(point.X, point.Y + scrollOffset);
754+
755+
foreach (var dot in graph.Dots)
756+
{
757+
var distance = Math.Sqrt(Math.Pow(dot.Center.X - adjustedPoint.X, 2) + Math.Pow(dot.Center.Y - adjustedPoint.Y, 2));
758+
if (distance <= 6)
759+
{
760+
return dot;
761+
}
762+
}
763+
return null;
764+
}
708765
}
709766

710767
public partial class Histories : UserControl

0 commit comments

Comments
 (0)