Skip to content

Commit 515c550

Browse files
committed
refactor: add responsive control DateTimePresenter instead of TextBlock to display datetime (#2183)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 1b82c09 commit 515c550

18 files changed

Lines changed: 175 additions & 83 deletions

src/Models/Commit.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public class Commit
3030
public int Color { get; set; } = 0;
3131
public double LeftMargin { get; set; } = 0;
3232

33-
public string AuthorTimeStr => DateTime.UnixEpoch.AddSeconds(AuthorTime).ToLocalTime().ToString(DateTimeFormat.Active.DateTime);
34-
public string CommitterTimeStr => DateTime.UnixEpoch.AddSeconds(CommitterTime).ToLocalTime().ToString(DateTimeFormat.Active.DateTime);
35-
public string AuthorTimeShortStr => DateTime.UnixEpoch.AddSeconds(AuthorTime).ToLocalTime().ToString(DateTimeFormat.Active.DateOnly);
36-
public string CommitterTimeShortStr => DateTime.UnixEpoch.AddSeconds(CommitterTime).ToLocalTime().ToString(DateTimeFormat.Active.DateOnly);
37-
3833
public bool IsCommitterVisible => !Author.Equals(Committer) || AuthorTime != CommitterTime;
3934
public bool IsCurrentHead => Decorators.Find(x => x.Type is DecoratorType.CurrentBranchHead or DecoratorType.CurrentCommitHead) != null;
4035
public bool HasDecorators => Decorators.Count > 0;

src/Models/DiffOption.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ public DiffOption(Commit commit, string file)
8686
/// <param name="ver"></param>
8787
public DiffOption(FileVersion ver)
8888
{
89-
if (string.IsNullOrEmpty(ver.Change.OriginalPath))
89+
if (string.IsNullOrEmpty(ver.OriginalPath))
9090
{
9191
_revisions.Add(ver.HasParent ? $"{ver.SHA}^" : Commit.EmptyTreeSHA1);
9292
_revisions.Add(ver.SHA);
9393
_path = ver.Path;
9494
}
9595
else
9696
{
97-
_revisions.Add($"{ver.SHA}^:{ver.Change.OriginalPath.Quoted()}");
97+
_revisions.Add($"{ver.SHA}^:{ver.OriginalPath.Quoted()}");
9898
_revisions.Add($"{ver.SHA}:{ver.Path.Quoted()}");
9999
_path = ver.Path;
100100
_orgPath = ver.Change.OriginalPath;

src/Models/FileVersion.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace SourceGit.Models
1+
namespace SourceGit.Models
42
{
53
public class FileVersion
64
{
@@ -10,9 +8,7 @@ public class FileVersion
108
public ulong AuthorTime { get; set; } = 0;
119
public string Subject { get; set; } = string.Empty;
1210
public Change Change { get; set; } = new();
13-
1411
public string Path => Change.Path;
15-
public string AuthorTimeStr => DateTime.UnixEpoch.AddSeconds(AuthorTime).ToLocalTime().ToString(DateTimeFormat.Active.DateTime);
16-
public string AuthorTimeShortStr => DateTime.UnixEpoch.AddSeconds(AuthorTime).ToLocalTime().ToString(DateTimeFormat.Active.DateOnly);
12+
public string OriginalPath => Change.OriginalPath;
1713
}
1814
}

src/Models/Stash.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32

43
namespace SourceGit.Models
54
{
@@ -10,24 +9,6 @@ public class Stash
109
public List<string> Parents { get; set; } = [];
1110
public ulong Time { get; set; } = 0;
1211
public string Message { get; set; } = "";
13-
14-
public string Subject
15-
{
16-
get
17-
{
18-
return Message.Split('\n', 2)[0].Trim();
19-
}
20-
}
21-
22-
public string TimeStr
23-
{
24-
get
25-
{
26-
return DateTime.UnixEpoch
27-
.AddSeconds(Time)
28-
.ToLocalTime()
29-
.ToString(DateTimeFormat.Active.DateTime);
30-
}
31-
}
12+
public string Subject => Message.Split('\n', 2)[0].Trim();
3213
}
3314
}

src/Models/Tag.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace SourceGit.Models
1+
namespace SourceGit.Models
42
{
53
public enum TagSortMode
64
{
@@ -16,10 +14,5 @@ public class Tag
1614
public User Creator { get; set; } = null;
1715
public ulong CreatorDate { get; set; } = 0;
1816
public string Message { get; set; } = string.Empty;
19-
20-
public string CreatorDateStr
21-
{
22-
get => DateTime.UnixEpoch.AddSeconds(CreatorDate).ToLocalTime().ToString(DateTimeFormat.Active.DateTime);
23-
}
2417
}
2518
}

src/ViewModels/TagCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public class TagToolTip
1313
public string Name { get; private set; }
1414
public bool IsAnnotated { get; private set; }
1515
public Models.User Creator { get; private set; }
16-
public string CreatorDateStr { get; private set; }
16+
public ulong CreatorDate { get; private set; }
1717
public string Message { get; private set; }
1818

1919
public TagToolTip(Models.Tag t)
2020
{
2121
Name = t.Name;
2222
IsAnnotated = t.IsAnnotated;
2323
Creator = t.Creator;
24-
CreatorDateStr = t.CreatorDateStr;
24+
CreatorDate = t.CreatorDate;
2525
Message = t.Message;
2626
}
2727
}

src/Views/CommitBaseInfo.axaml

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
<Run Text="{Binding Author.Email, Mode=OneWay}"/>
2626
</TextBlock>
2727
</Border>
28-
<TextBlock Text="{Binding AuthorTimeStr}"
29-
Margin="2,0,0,0"
30-
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
31-
Foreground="{DynamicResource Brush.FG2}"/>
28+
<v:DateTimePresenter Margin="2,0,0,0"
29+
Timestamp="{Binding AuthorTime}"
30+
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
31+
Foreground="{DynamicResource Brush.FG2}"/>
3232
</StackPanel>
3333
</Grid>
3434

@@ -43,10 +43,10 @@
4343
<Run Text="{Binding Committer.Email, Mode=OneWay}"/>
4444
</TextBlock>
4545
</Border>
46-
<TextBlock Text="{Binding CommitterTimeStr}"
47-
Margin="2,0,0,0"
48-
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
49-
Foreground="{DynamicResource Brush.FG2}"/>
46+
<v:DateTimePresenter Margin="2,0,0,0"
47+
Timestamp="{Binding CommitterTime}"
48+
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Decrease}}"
49+
Foreground="{DynamicResource Brush.FG2}"/>
5050
</StackPanel>
5151
</Grid>
5252
</UniformGrid>
@@ -127,7 +127,10 @@
127127
<Grid ColumnDefinitions="Auto,*,Auto">
128128
<v:Avatar Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
129129
<TextBlock Grid.Column="1" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
130-
<TextBlock Grid.Column="2" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
130+
<v:DateTimePresenter Grid.Column="2"
131+
Margin="8,0,0,0"
132+
Timestamp="{Binding CommitterTime}"
133+
Foreground="{DynamicResource Brush.FG2}"/>
131134
</Grid>
132135

133136
<TextBlock Margin="0,8,0,0" Text="{Binding Subject}" TextWrapping="Wrap"/>
@@ -171,7 +174,10 @@
171174
<Grid ColumnDefinitions="Auto,*,Auto">
172175
<v:Avatar Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
173176
<TextBlock Grid.Column="1" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
174-
<TextBlock Grid.Column="2" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
177+
<v:DateTimePresenter Grid.Column="2"
178+
Margin="8,0,0,0"
179+
Timestamp="{Binding CommitterTime}"
180+
Foreground="{DynamicResource Brush.FG2}"/>
175181
</Grid>
176182

177183
<TextBlock Margin="0,8,0,0" Text="{Binding Subject}" TextWrapping="Wrap"/>
@@ -213,7 +219,10 @@
213219
<Grid ColumnDefinitions="Auto,*,Auto">
214220
<v:Avatar Grid.Column="0" Width="16" Height="16" VerticalAlignment="Center" IsHitTestVisible="False" User="{Binding Author}"/>
215221
<TextBlock Grid.Column="1" Text="{Binding Author.Name}" Margin="8,0,0,0"/>
216-
<TextBlock Grid.Column="2" Text="{Binding CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
222+
<v:DateTimePresenter Grid.Column="2"
223+
Margin="8,0,0,0"
224+
Timestamp="{Binding CommitterTime}"
225+
Foreground="{DynamicResource Brush.FG2}"/>
217226
</Grid>
218227

219228
<TextBlock Margin="0,8,0,0" Text="{Binding Subject}" TextWrapping="Wrap"/>

src/Views/CommitTimeTextBlock.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,13 @@ private string GetDisplayText()
130130
if (DataContext is not Models.Commit commit)
131131
return string.Empty;
132132

133+
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
133134
if (ShowAsDateTime)
134-
return UseAuthorTime ? commit.AuthorTimeStr : commit.CommitterTimeStr;
135+
{
136+
var format = Models.DateTimeFormat.Active.DateTime;
137+
return DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime().ToString(format);
138+
}
135139

136-
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
137140
var now = DateTime.Now;
138141
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
139142
var span = now - localTime;

src/Views/Compare.axaml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@
5353
<TextBlock Text="{Binding BaseName}" Margin="4,0" Foreground="#FFF2F2F2"/>
5454
</StackPanel>
5555
</Border>
56-
<TextBlock Grid.Column="3" Text="{Binding BaseHead.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0" TextDecorations="Underline" Cursor="Hand" PointerPressed="OnPressedSHA"/>
57-
<TextBlock Grid.Column="4" Text="{Binding BaseHead.CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
56+
<TextBlock Grid.Column="3"
57+
Text="{Binding BaseHead.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}"
58+
Foreground="DarkOrange" Margin="8,0,0,0"
59+
TextDecorations="Underline"
60+
Cursor="Hand"
61+
PointerPressed="OnPressedSHA"/>
62+
<v:DateTimePresenter Grid.Column="4"
63+
Margin="8,0,0,0"
64+
Timestamp="{Binding BaseHead.CommitterTime}"
65+
Foreground="{DynamicResource Brush.FG2}"/>
5866
</Grid>
5967

6068
<TextBlock Grid.Row="1" Text="{Binding BaseHead.Subject}" VerticalAlignment="Bottom"/>
@@ -81,7 +89,10 @@
8189
</StackPanel>
8290
</Border>
8391
<TextBlock Grid.Column="3" Text="{Binding ToHead.SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="8,0,0,0" TextDecorations="Underline" Cursor="Hand" PointerPressed="OnPressedSHA"/>
84-
<TextBlock Grid.Column="4" Text="{Binding ToHead.CommitterTimeStr}" Foreground="{DynamicResource Brush.FG2}" Margin="8,0,0,0"/>
92+
<v:DateTimePresenter Grid.Column="4"
93+
Margin="8,0,0,0"
94+
Timestamp="{Binding ToHead.CommitterTime}"
95+
Foreground="{DynamicResource Brush.FG2}"/>
8596
</Grid>
8697

8798
<TextBlock Grid.Row="1" Text="{Binding ToHead.Subject}" VerticalAlignment="Bottom"/>

src/Views/DateTimePresenter.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
3+
using Avalonia;
4+
using Avalonia.Controls;
5+
using Avalonia.Data;
6+
7+
namespace SourceGit.Views
8+
{
9+
public class DateTimePresenter : TextBlock
10+
{
11+
public static readonly StyledProperty<bool> ShowDateOnlyProperty =
12+
AvaloniaProperty.Register<DateTimePresenter, bool>(nameof(ShowDateOnly), false);
13+
14+
public bool ShowDateOnly
15+
{
16+
get => GetValue(ShowDateOnlyProperty);
17+
set => SetValue(ShowDateOnlyProperty, value);
18+
}
19+
20+
public static readonly StyledProperty<bool> Use24HoursProperty =
21+
AvaloniaProperty.Register<DateTimePresenter, bool>(nameof(Use24Hours), true);
22+
23+
public bool Use24Hours
24+
{
25+
get => GetValue(Use24HoursProperty);
26+
set => SetValue(Use24HoursProperty, value);
27+
}
28+
29+
public static readonly StyledProperty<int> DateTimeFormatProperty =
30+
AvaloniaProperty.Register<DateTimePresenter, int>(nameof(DateTimeFormat));
31+
32+
public int DateTimeFormat
33+
{
34+
get => GetValue(DateTimeFormatProperty);
35+
set => SetValue(DateTimeFormatProperty, value);
36+
}
37+
38+
public static readonly StyledProperty<ulong> TimestampProperty =
39+
AvaloniaProperty.Register<DateTimePresenter, ulong>(nameof(Timestamp), 0);
40+
41+
public ulong Timestamp
42+
{
43+
get => GetValue(TimestampProperty);
44+
set => SetValue(TimestampProperty, value);
45+
}
46+
47+
protected override Type StyleKeyOverride => typeof(TextBlock);
48+
49+
public DateTimePresenter()
50+
{
51+
Bind(Use24HoursProperty, new Binding()
52+
{
53+
Mode = BindingMode.OneWay,
54+
Source = ViewModels.Preferences.Instance,
55+
Path = "Use24Hours"
56+
});
57+
58+
Bind(DateTimeFormatProperty, new Binding()
59+
{
60+
Mode = BindingMode.OneWay,
61+
Source = ViewModels.Preferences.Instance,
62+
Path = "DateTimeFormat"
63+
});
64+
}
65+
66+
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
67+
{
68+
base.OnPropertyChanged(change);
69+
70+
if (change.Property == ShowDateOnlyProperty ||
71+
change.Property == Use24HoursProperty ||
72+
change.Property == DateTimeFormatProperty ||
73+
change.Property == TimestampProperty)
74+
{
75+
var active = Models.DateTimeFormat.Active;
76+
var format = ShowDateOnly ? active.DateOnly : active.DateTime;
77+
var text = DateTime.UnixEpoch.AddSeconds(Timestamp).ToLocalTime().ToString(format);
78+
SetCurrentValue(TextProperty, text);
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)