Skip to content

Commit 6a502ec

Browse files
committed
enhance: improve commit time textblock performance
Signed-off-by: leo <longshuang@msn.cn>
1 parent 5f31883 commit 6a502ec

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

src/Views/CommitTimeTextBlock.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
4343
{
4444
base.OnPropertyChanged(change);
4545

46-
if (change.Property == UseAuthorTimeProperty)
46+
if (change.Property == ShowAsDateTimeProperty)
4747
{
48-
SetCurrentValue(TextProperty, GetDisplayText());
49-
}
50-
else if (change.Property == ShowAsDateTimeProperty)
51-
{
52-
SetCurrentValue(TextProperty, GetDisplayText());
48+
UpdateDisplayText();
5349

5450
if (ShowAsDateTime)
5551
{
@@ -62,10 +58,17 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
6258
HorizontalAlignment = HorizontalAlignment.Center;
6359
}
6460
}
61+
else if (change.Property == UseAuthorTimeProperty)
62+
{
63+
UpdateDisplayText();
64+
}
6565
else if (change.Property == DateTimeFormatProperty)
6666
{
6767
if (ShowAsDateTime)
68-
SetCurrentValue(TextProperty, GetDisplayText());
68+
{
69+
_displayTimestamp = 0;
70+
UpdateDisplayText();
71+
}
6972
}
7073
}
7174

@@ -86,7 +89,7 @@ protected override void OnUnloaded(RoutedEventArgs e)
8689
protected override void OnDataContextChanged(EventArgs e)
8790
{
8891
base.OnDataContextChanged(e);
89-
SetCurrentValue(TextProperty, GetDisplayText());
92+
UpdateDisplayText();
9093
}
9194

9295
private void StartTimer()
@@ -96,13 +99,7 @@ private void StartTimer()
9699

97100
_refreshTimer = DispatcherTimer.Run(() =>
98101
{
99-
Dispatcher.UIThread.Invoke(() =>
100-
{
101-
var text = GetDisplayText();
102-
if (!text.Equals(Text, StringComparison.Ordinal))
103-
Text = text;
104-
});
105-
102+
Dispatcher.UIThread.Invoke(UpdateDisplayText);
106103
return true;
107104
}, TimeSpan.FromSeconds(10));
108105
}
@@ -116,15 +113,25 @@ private void StopTimer()
116113
}
117114
}
118115

119-
private string GetDisplayText()
116+
private void UpdateDisplayText()
120117
{
121118
if (DataContext is not Models.Commit commit)
122-
return string.Empty;
119+
return;
120+
121+
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
122+
if (timestamp == _displayTimestamp)
123+
return;
124+
125+
_displayTimestamp = timestamp;
123126

124127
if (ShowAsDateTime)
125-
return UseAuthorTime ? commit.AuthorTimeStr : commit.CommitterTimeStr;
128+
Text = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime().ToString(Models.DateTimeFormat.Active.DateTime);
129+
else
130+
Text = FormatTimePeriod(timestamp);
131+
}
126132

127-
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
133+
private string FormatTimePeriod(ulong timestamp)
134+
{
128135
var now = DateTime.Now;
129136
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
130137
var span = now - localTime;
@@ -168,5 +175,6 @@ private string GetDisplayText()
168175
}
169176

170177
private IDisposable _refreshTimer = null;
178+
private ulong _displayTimestamp = 0;
171179
}
172180
}

0 commit comments

Comments
 (0)