forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommitTimeTextBlock.cs
More file actions
172 lines (141 loc) · 5.42 KB
/
Copy pathCommitTimeTextBlock.cs
File metadata and controls
172 lines (141 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Threading;
namespace SourceGit.Views
{
public class CommitTimeTextBlock : TextBlock
{
public static readonly StyledProperty<bool> ShowAsDateTimeProperty =
AvaloniaProperty.Register<CommitTimeTextBlock, bool>(nameof(ShowAsDateTime), true);
public bool ShowAsDateTime
{
get => GetValue(ShowAsDateTimeProperty);
set => SetValue(ShowAsDateTimeProperty, value);
}
public static readonly StyledProperty<int> DateTimeFormatProperty =
AvaloniaProperty.Register<CommitTimeTextBlock, int>(nameof(DateTimeFormat));
public int DateTimeFormat
{
get => GetValue(DateTimeFormatProperty);
set => SetValue(DateTimeFormatProperty, value);
}
public static readonly StyledProperty<bool> UseAuthorTimeProperty =
AvaloniaProperty.Register<CommitTimeTextBlock, bool>(nameof(UseAuthorTime), true);
public bool UseAuthorTime
{
get => GetValue(UseAuthorTimeProperty);
set => SetValue(UseAuthorTimeProperty, value);
}
protected override Type StyleKeyOverride => typeof(TextBlock);
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == UseAuthorTimeProperty)
{
SetCurrentValue(TextProperty, GetDisplayText());
}
else if (change.Property == ShowAsDateTimeProperty)
{
SetCurrentValue(TextProperty, GetDisplayText());
if (ShowAsDateTime)
{
StopTimer();
HorizontalAlignment = HorizontalAlignment.Left;
}
else
{
StartTimer();
HorizontalAlignment = HorizontalAlignment.Center;
}
}
else if (change.Property == DateTimeFormatProperty)
{
if (ShowAsDateTime)
SetCurrentValue(TextProperty, GetDisplayText());
}
}
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
if (!ShowAsDateTime)
StartTimer();
}
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
StopTimer();
}
protected override void OnDataContextChanged(EventArgs e)
{
base.OnDataContextChanged(e);
SetCurrentValue(TextProperty, GetDisplayText());
}
private void StartTimer()
{
if (_refreshTimer != null)
return;
_refreshTimer = DispatcherTimer.Run(() =>
{
Dispatcher.UIThread.Invoke(() =>
{
var text = GetDisplayText();
if (!text.Equals(Text, StringComparison.Ordinal))
Text = text;
});
return true;
}, TimeSpan.FromSeconds(10));
}
private void StopTimer()
{
if (_refreshTimer != null)
{
_refreshTimer.Dispose();
_refreshTimer = null;
}
}
private string GetDisplayText()
{
if (DataContext is not Models.Commit commit)
return string.Empty;
if (ShowAsDateTime)
return UseAuthorTime ? commit.AuthorTimeStr : commit.CommitterTimeStr;
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
var now = DateTime.Now;
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
var span = now - localTime;
if (span.TotalMinutes < 1)
return App.Text("Period.JustNow");
if (span.TotalHours < 1)
return App.Text("Period.MinutesAgo", (int)span.TotalMinutes);
if (span.TotalDays < 1)
{
var hours = (int)span.TotalHours;
return hours == 1 ? App.Text("Period.HourAgo") : App.Text("Period.HoursAgo", hours);
}
var lastDay = now.AddDays(-1).Date;
if (localTime >= lastDay)
return App.Text("Period.Yesterday");
if ((localTime.Year == now.Year && localTime.Month == now.Month) || span.TotalDays < 28)
{
var diffDay = now.Date - localTime.Date;
return App.Text("Period.DaysAgo", (int)diffDay.TotalDays);
}
var lastMonth = now.AddMonths(-1).Date;
if (localTime.Year == lastMonth.Year && localTime.Month == lastMonth.Month)
return App.Text("Period.LastMonth");
if (localTime.Year == now.Year || localTime > now.AddMonths(-11))
{
var diffMonth = (12 + now.Month - localTime.Month) % 12;
return App.Text("Period.MonthsAgo", diffMonth);
}
var diffYear = now.Year - localTime.Year;
if (diffYear == 1)
return App.Text("Period.LastYear");
return App.Text("Period.YearsAgo", diffYear);
}
private IDisposable _refreshTimer = null;
}
}