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
180 lines (151 loc) · 6.22 KB
/
Copy pathCommitTimeTextBlock.cs
File metadata and controls
180 lines (151 loc) · 6.22 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
173
174
175
176
177
178
179
180
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 DirectProperty<CommitTimeTextBlock, bool> ShowAsDateTimeProperty =
AvaloniaProperty.RegisterDirect<CommitTimeTextBlock, bool>(
nameof(ShowAsDateTime),
static o => o.ShowAsDateTime,
static (o, v) => o.ShowAsDateTime = v);
public bool ShowAsDateTime
{
get => _showAsDateTime;
set => SetAndRaise(ShowAsDateTimeProperty, ref _showAsDateTime, value);
}
public static readonly DirectProperty<CommitTimeTextBlock, bool> Use24HoursProperty =
AvaloniaProperty.RegisterDirect<CommitTimeTextBlock, bool>(
nameof(Use24Hours),
static o => o.Use24Hours,
static (o, v) => o.Use24Hours = v);
public bool Use24Hours
{
get => _use24Hours;
set => SetAndRaise(Use24HoursProperty, ref _use24Hours, value);
}
public static readonly DirectProperty<CommitTimeTextBlock, int> DateTimeFormatProperty =
AvaloniaProperty.RegisterDirect<CommitTimeTextBlock, int>(
nameof(DateTimeFormat),
static o => o.DateTimeFormat,
static (o, v) => o.DateTimeFormat = v);
public int DateTimeFormat
{
get => _dateTimeFormat;
set => SetAndRaise(DateTimeFormatProperty, ref _dateTimeFormat, value);
}
public static readonly DirectProperty<CommitTimeTextBlock, ulong> TimestampProperty =
AvaloniaProperty.RegisterDirect<CommitTimeTextBlock, ulong>(
nameof(Timestamp),
static o => o.Timestamp,
static (o, v) => o.Timestamp = v);
public ulong Timestamp
{
get => _timestamp;
set => SetAndRaise(TimestampProperty, ref _timestamp, value);
}
protected override Type StyleKeyOverride => typeof(TextBlock);
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == TimestampProperty)
{
SetCurrentValue(TextProperty, GetDisplayText());
}
else if (change.Property == ShowAsDateTimeProperty)
{
SetCurrentValue(TextProperty, GetDisplayText());
if (ShowAsDateTime)
{
_refreshTimer?.Stop();
HorizontalAlignment = HorizontalAlignment.Left;
}
else
{
_refreshTimer?.Start();
HorizontalAlignment = HorizontalAlignment.Center;
}
}
else if (change.Property == DateTimeFormatProperty || change.Property == Use24HoursProperty)
{
if (ShowAsDateTime)
SetCurrentValue(TextProperty, GetDisplayText());
}
}
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
_refreshTimer = new DispatcherTimer();
_refreshTimer.Interval = TimeSpan.FromSeconds(10);
_refreshTimer.Tag = this;
_refreshTimer.Tick += static (o, _) =>
{
if (o is DispatcherTimer { Tag: CommitTimeTextBlock textBlock })
{
var text = textBlock.GetDisplayText();
if (!text.Equals(textBlock.Text, StringComparison.Ordinal))
textBlock.Text = text;
}
};
_refreshTimer.IsEnabled = !ShowAsDateTime;
}
protected override void OnUnloaded(RoutedEventArgs e)
{
_refreshTimer.Tag = null;
_refreshTimer.IsEnabled = false;
base.OnUnloaded(e);
}
protected override void OnDataContextChanged(EventArgs e)
{
base.OnDataContextChanged(e);
SetCurrentValue(TextProperty, GetDisplayText());
}
private string GetDisplayText()
{
var timestamp = Timestamp;
if (ShowAsDateTime)
return Models.DateTimeFormat.Format(timestamp);
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 bool _showAsDateTime = true;
private bool _use24Hours = true;
private int _dateTimeFormat = 0;
private ulong _timestamp = 0;
private DispatcherTimer _refreshTimer = null;
}
}