Skip to content

Commit c596427

Browse files
committed
fix: ahead/behind indicator of commit in histories view not updated after upstream changed
1 parent ce74203 commit c596427

File tree

6 files changed

+99
-29
lines changed

6 files changed

+99
-29
lines changed

src/Models/Commit.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public static double OpacityForNotMerged
2525
public bool HasDecorators => Decorators.Count > 0;
2626

2727
public bool IsMerged { get; set; } = false;
28-
public bool CanPushToUpstream { get; set; } = false;
29-
public bool CanPullFromUpstream { get; set; } = false;
3028
public Thickness Margin { get; set; } = new Thickness(0);
3129

3230
public string AuthorTimeStr => DateTime.UnixEpoch.AddSeconds(AuthorTime).ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss");

src/Models/CommitGraph.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static void SetPens(List<Color> colors, double thickness)
128128
_penCount = colors.Count;
129129
}
130130

131-
public static CommitGraph Parse(List<Commit> commits, HashSet<string> canPushCommits, HashSet<string> canPullCommits)
131+
public static CommitGraph Parse(List<Commit> commits)
132132
{
133133
double UNIT_WIDTH = 12;
134134
double HALF_WIDTH = 6;
@@ -148,9 +148,6 @@ public static CommitGraph Parse(List<Commit> commits, HashSet<string> canPushCom
148148
var isMerged = commit.IsMerged;
149149
var oldCount = unsolved.Count;
150150

151-
commit.CanPushToUpstream = canPushCommits.Remove(commit.SHA);
152-
commit.CanPullFromUpstream = canPullCommits.Remove(commit.SHA);
153-
154151
// Update current y offset
155152
offsetY += UNIT_HEIGHT;
156153

src/ViewModels/Repository.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -749,20 +749,8 @@ public void RefreshCommits()
749749
limits += "--branches --remotes --tags";
750750
}
751751

752-
var canPushCommits = new HashSet<string>();
753-
var canPullCommits = new HashSet<string>();
754-
var currentBranch = _branches.Find(x => x.IsCurrent);
755-
if (currentBranch != null)
756-
{
757-
foreach (var sha in currentBranch.TrackStatus.Ahead)
758-
canPushCommits.Add(sha);
759-
760-
foreach (var sha in currentBranch.TrackStatus.Behind)
761-
canPullCommits.Add(sha);
762-
}
763-
764752
var commits = new Commands.QueryCommits(_fullpath, limits).Result();
765-
var graph = Models.CommitGraph.Parse(commits, canPushCommits, canPullCommits);
753+
var graph = Models.CommitGraph.Parse(commits);
766754

767755
Dispatcher.UIThread.Invoke(() =>
768756
{

src/Views/Histories.axaml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,10 @@
6262
<DataTemplate x:DataType="{x:Type m:Commit}">
6363
<Border Margin="{Binding Margin}">
6464
<StackPanel Orientation="Horizontal" Margin="2,0,0,0">
65-
<Ellipse Width="5" Height="5"
66-
Margin="0,0,4,0"
67-
Fill="{DynamicResource Brush.Accent}"
68-
IsVisible="{Binding CanPushToUpstream}"/>
69-
70-
<Ellipse Width="5" Height="5"
71-
Margin="0,0,4,0"
72-
Fill="{DynamicResource Brush.FG1}"
73-
IsVisible="{Binding CanPullFromUpstream}"/>
65+
<v:CommitStatusIndicator CurrentBranch="{Binding $parent[v:Histories].CurrentBranch}"
66+
AheadBrush="{DynamicResource Brush.Accent}"
67+
BehindBrush="{DynamicResource Brush.FG1}"
68+
VerticalAlignment="Center"/>
7469

7570
<v:CommitRefsPresenter IsVisible="{Binding HasDecorators}"
7671
IconBackground="{DynamicResource Brush.DecoratorIconBG}"

src/Views/Histories.axaml.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,88 @@ private void RefreshLayout()
7474
}
7575
}
7676

77+
public class CommitStatusIndicator : Control
78+
{
79+
public static readonly StyledProperty<Models.Branch> CurrentBranchProperty =
80+
AvaloniaProperty.Register<CommitStatusIndicator, Models.Branch>(nameof(CurrentBranch));
81+
82+
public Models.Branch CurrentBranch
83+
{
84+
get => GetValue(CurrentBranchProperty);
85+
set => SetValue(CurrentBranchProperty, value);
86+
}
87+
88+
public static readonly StyledProperty<IBrush> AheadBrushProperty =
89+
AvaloniaProperty.Register<CommitStatusIndicator, IBrush>(nameof(AheadBrush));
90+
91+
public IBrush AheadBrush
92+
{
93+
get => GetValue(AheadBrushProperty);
94+
set => SetValue(AheadBrushProperty, value);
95+
}
96+
97+
public static readonly StyledProperty<IBrush> BehindBrushProperty =
98+
AvaloniaProperty.Register<CommitStatusIndicator, IBrush>(nameof(BehindBrush));
99+
100+
public IBrush BehindBrush
101+
{
102+
get => GetValue(BehindBrushProperty);
103+
set => SetValue(BehindBrushProperty, value);
104+
}
105+
106+
enum Status
107+
{
108+
Normal,
109+
Ahead,
110+
Behind,
111+
}
112+
113+
public override void Render(DrawingContext context)
114+
{
115+
if (_status == Status.Normal)
116+
return;
117+
118+
context.DrawEllipse(_status == Status.Ahead ? AheadBrush : BehindBrush, null, new Rect(0, 0, 5, 5));
119+
}
120+
121+
protected override Size MeasureOverride(Size availableSize)
122+
{
123+
if (DataContext is Models.Commit commit && CurrentBranch is not null)
124+
{
125+
var sha = commit.SHA;
126+
var track = CurrentBranch.TrackStatus;
127+
128+
if (track.Ahead.Contains(sha))
129+
_status = Status.Ahead;
130+
else if (track.Behind.Contains(sha))
131+
_status = Status.Behind;
132+
else
133+
_status = Status.Normal;
134+
}
135+
else
136+
{
137+
_status = Status.Normal;
138+
}
139+
140+
return _status == Status.Normal ? new Size(0, 0) : new Size(9, 5);
141+
}
142+
143+
protected override void OnDataContextChanged(EventArgs e)
144+
{
145+
base.OnDataContextChanged(e);
146+
InvalidateMeasure();
147+
}
148+
149+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
150+
{
151+
base.OnPropertyChanged(change);
152+
if (change.Property == CurrentBranchProperty)
153+
InvalidateMeasure();
154+
}
155+
156+
private Status _status = Status.Normal;
157+
}
158+
77159
public class CommitSubjectPresenter : TextBlock
78160
{
79161
public static readonly StyledProperty<string> SubjectProperty =
@@ -450,6 +532,15 @@ private void DrawCurves(DrawingContext context, double top, double bottom)
450532

451533
public partial class Histories : UserControl
452534
{
535+
public static readonly StyledProperty<Models.Branch> CurrentBranchProperty =
536+
AvaloniaProperty.Register<Histories, Models.Branch>(nameof(CurrentBranch));
537+
538+
public Models.Branch CurrentBranch
539+
{
540+
get => GetValue(CurrentBranchProperty);
541+
set => SetValue(CurrentBranchProperty, value);
542+
}
543+
453544
public static readonly StyledProperty<long> NavigationIdProperty =
454545
AvaloniaProperty.Register<Histories, long>(nameof(NavigationId));
455546

src/Views/Repository.axaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@
728728
<ContentControl Grid.Row="2" Content="{Binding SelectedView}">
729729
<ContentControl.DataTemplates>
730730
<DataTemplate DataType="vm:Histories">
731-
<v:Histories NavigationId="{Binding NavigationId}"/>
731+
<v:Histories CurrentBranch="{Binding $parent[v:Repository].((vm:Repository)DataContext).CurrentBranch}"
732+
NavigationId="{Binding NavigationId}"/>
732733
</DataTemplate>
733734

734735
<DataTemplate DataType="vm:WorkingCopy">

0 commit comments

Comments
 (0)