Skip to content

Commit 8af75be

Browse files
committed
refactor: use converters instead of store so many data in Models.Commit
Signed-off-by: leo <[email protected]>
1 parent 8e9c820 commit 8af75be

File tree

7 files changed

+27
-35
lines changed

7 files changed

+27
-35
lines changed

src/App.axaml.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,6 @@ public static void SetTheme(string theme, string themeOverridesFile)
247247
else
248248
Models.CommitGraph.SetDefaultPens(overrides.GraphPenThickness);
249249

250-
Models.Commit.OpacityForNotMerged = overrides.OpacityForNotMergedCommits;
251-
252250
app.Resources.MergedDictionaries.Add(resDic);
253251
app._themeOverrides = resDic;
254252
}

src/Converters/BoolConverters.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public static class BoolConverters
99
new FuncValueConverter<bool, double>(x => x ? 200 : double.NaN);
1010

1111
public static readonly FuncValueConverter<bool, FontWeight> IsBoldToFontWeight =
12-
new FuncValueConverter<bool, FontWeight>(x => x ? FontWeight.Bold : FontWeight.Normal);
12+
new FuncValueConverter<bool, FontWeight>(x => x ? FontWeight.Bold : FontWeight.Regular);
13+
14+
public static readonly FuncValueConverter<bool, double> IsMergedToOpacity =
15+
new FuncValueConverter<bool, double>(x => x ? 1 : 0.65);
1316
}
1417
}

src/Converters/DoubleConverters.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Avalonia.Data.Converters;
1+
using Avalonia;
2+
using Avalonia.Data.Converters;
23

34
namespace SourceGit.Converters
45
{
@@ -15,5 +16,8 @@ public static class DoubleConverters
1516

1617
public static readonly FuncValueConverter<double, string> OneMinusToPercentage =
1718
new FuncValueConverter<double, string>(v => ((1.0 - v) * 100).ToString("F3") + "%");
19+
20+
public static readonly FuncValueConverter<double, Thickness> ToLeftMargin =
21+
new FuncValueConverter<double, Thickness>(v => new Thickness(v, 0, 0, 0));
1822
}
1923
}

src/Models/Commit.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33

4-
using Avalonia;
5-
using Avalonia.Media;
6-
74
namespace SourceGit.Models
85
{
96
public enum CommitSearchMethod
@@ -18,15 +15,8 @@ public enum CommitSearchMethod
1815

1916
public class Commit
2017
{
21-
// As retrieved by: git mktree </dev/null
2218
public const string EmptyTreeSHA1 = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
2319

24-
public static double OpacityForNotMerged
25-
{
26-
get;
27-
set;
28-
} = 0.65;
29-
3020
public string SHA { get; set; } = string.Empty;
3121
public User Author { get; set; } = User.Invalid;
3222
public ulong AuthorTime { get; set; } = 0;
@@ -35,22 +25,19 @@ public static double OpacityForNotMerged
3525
public string Subject { get; set; } = string.Empty;
3626
public List<string> Parents { get; set; } = new();
3727
public List<Decorator> Decorators { get; set; } = new();
38-
public bool HasDecorators => Decorators.Count > 0;
28+
29+
public bool IsMerged { get; set; } = false;
30+
public int Color { get; set; } = 0;
31+
public double LeftMargin { get; set; } = 0;
3932

4033
public string AuthorTimeStr => DateTime.UnixEpoch.AddSeconds(AuthorTime).ToLocalTime().ToString(DateTimeFormat.Active.DateTime);
4134
public string CommitterTimeStr => DateTime.UnixEpoch.AddSeconds(CommitterTime).ToLocalTime().ToString(DateTimeFormat.Active.DateTime);
4235
public string AuthorTimeShortStr => DateTime.UnixEpoch.AddSeconds(AuthorTime).ToLocalTime().ToString(DateTimeFormat.Active.DateOnly);
4336
public string CommitterTimeShortStr => DateTime.UnixEpoch.AddSeconds(CommitterTime).ToLocalTime().ToString(DateTimeFormat.Active.DateOnly);
4437

45-
public bool IsMerged { get; set; } = false;
4638
public bool IsCommitterVisible => !Author.Equals(Committer) || AuthorTime != CommitterTime;
4739
public bool IsCurrentHead => Decorators.Find(x => x.Type is DecoratorType.CurrentBranchHead or DecoratorType.CurrentCommitHead) != null;
48-
49-
public int Color { get; set; } = 0;
50-
public double Opacity => IsMerged ? 1 : OpacityForNotMerged;
51-
public FontWeight FontWeight => IsCurrentHead ? FontWeight.Bold : FontWeight.Regular;
52-
public Thickness Margin { get; set; } = new(0);
53-
public IBrush Brush => CommitGraph.Pens[Color].Brush;
40+
public bool HasDecorators => Decorators.Count > 0;
5441

5542
public string GetFriendlyName()
5643
{

src/Models/CommitGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
199199

200200
// Margins & merge state (used by Views.Histories).
201201
commit.IsMerged = isMerged;
202-
commit.Margin = new Thickness(Math.Max(offsetX, maxOffsetOld) + halfWidth + 2, 0, 0, 0);
203202
commit.Color = dotColor;
203+
commit.LeftMargin = Math.Max(offsetX, maxOffsetOld) + halfWidth + 2;
204204
}
205205

206206
// Deal with curves haven't ended yet.

src/Views/CommitRefsPresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ protected override Size MeasureOverride(Size availableSize)
183183
var typeface = new Typeface(FontFamily);
184184
var typefaceBold = new Typeface(FontFamily, FontStyle.Normal, FontWeight.Bold);
185185
var fg = Foreground;
186-
var normalBG = UseGraphColor ? commit.Brush : Brushes.Gray;
186+
var normalBG = UseGraphColor ? Models.CommitGraph.Pens[commit.Color].Brush : Brushes.Gray;
187187
var labelSize = FontSize;
188188
var requiredHeight = 16.0;
189189
var x = 0.0;

src/Views/Histories.axaml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103

104104
<DataGridTemplateColumn.CellTemplate>
105105
<DataTemplate DataType="m:Commit">
106-
<Border Padding="{Binding Margin}" ClipToBounds="True" Background="Transparent">
106+
<Border Padding="{Binding LeftMargin, Converter={x:Static c:DoubleConverters.ToLeftMargin}}" ClipToBounds="True" Background="Transparent">
107107
<Grid ColumnDefinitions="Auto,Auto,Auto,*" Margin="2,0,4,0" ClipToBounds="True">
108108
<v:CommitStatusIndicator Grid.Column="0"
109109
CurrentBranch="{Binding $parent[v:Histories].CurrentBranch}"
@@ -140,8 +140,8 @@
140140
LinkForeground="{DynamicResource Brush.Link}"
141141
Subject="{Binding Subject}"
142142
IssueTrackers="{Binding $parent[v:Histories].IssueTrackers}"
143-
FontWeight="{Binding FontWeight}"
144-
Opacity="{Binding Opacity}"/>
143+
FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
144+
Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
145145
</Grid>
146146
</Border>
147147
</DataTemplate>
@@ -167,15 +167,15 @@
167167
HorizontalAlignment="Left"
168168
VerticalAlignment="Center"
169169
User="{Binding Author}"
170-
Opacity="{Binding Opacity}"
170+
Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"
171171
IsHitTestVisible="False"/>
172172

173173
<Border Grid.Column="1" Padding="8,0,4,0" ClipToBounds="True">
174174
<TextBlock Classes="primary"
175175
Text="{Binding Author.Name}"
176-
FontWeight="{Binding FontWeight}"
177-
HorizontalAlignment="Left"
178-
Opacity="{Binding Opacity}"/>
176+
FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
177+
Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"
178+
HorizontalAlignment="Left"/>
179179
</Border>
180180
</Grid>
181181
</DataTemplate>
@@ -192,8 +192,8 @@
192192
<Border Padding="8,0,0,0" Background="Transparent" ClipToBounds="True">
193193
<TextBlock Classes="primary"
194194
Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
195-
FontWeight="{Binding FontWeight}"
196-
Opacity="{Binding Opacity}"/>
195+
FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
196+
Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"/>
197197
</Border>
198198
</DataTemplate>
199199
</DataGridTemplateColumn.CellTemplate>
@@ -217,8 +217,8 @@
217217
<DataTemplate DataType="m:Commit">
218218
<Border Padding="8,0" Background="Transparent" ClipToBounds="True">
219219
<v:CommitTimeTextBlock Classes="primary"
220-
FontWeight="{Binding FontWeight}"
221-
Opacity="{Binding Opacity}"
220+
FontWeight="{Binding IsCurrentHead, Converter={x:Static c:BoolConverters.IsBoldToFontWeight}}"
221+
Opacity="{Binding IsMerged, Converter={x:Static c:BoolConverters.IsMergedToOpacity}}"
222222
UseAuthorTime="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowAuthorTimeInGraph, Mode=OneWay}"
223223
ShowAsDateTime="{Binding Source={x:Static vm:Preferences.Instance}, Path=!DisplayTimeAsPeriodInHistories}"
224224
DateTimeFormat="{Binding Source={x:Static vm:Preferences.Instance}, Path=DateTimeFormat}"/>

0 commit comments

Comments
 (0)