Skip to content

Commit c17ac1b

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

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/Views/CommitTimeTextBlock.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,10 @@ 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+
_displayTimestamp = 0;
49+
UpdateDisplayText();
5350

5451
if (ShowAsDateTime)
5552
{
@@ -62,10 +59,17 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
6259
HorizontalAlignment = HorizontalAlignment.Center;
6360
}
6461
}
62+
else if (change.Property == UseAuthorTimeProperty)
63+
{
64+
UpdateDisplayText();
65+
}
6566
else if (change.Property == DateTimeFormatProperty)
6667
{
6768
if (ShowAsDateTime)
68-
SetCurrentValue(TextProperty, GetDisplayText());
69+
{
70+
_displayTimestamp = 0;
71+
UpdateDisplayText();
72+
}
6973
}
7074
}
7175

@@ -86,7 +90,7 @@ protected override void OnUnloaded(RoutedEventArgs e)
8690
protected override void OnDataContextChanged(EventArgs e)
8791
{
8892
base.OnDataContextChanged(e);
89-
SetCurrentValue(TextProperty, GetDisplayText());
93+
UpdateDisplayText();
9094
}
9195

9296
private void StartTimer()
@@ -96,13 +100,7 @@ private void StartTimer()
96100

97101
_refreshTimer = DispatcherTimer.Run(() =>
98102
{
99-
Dispatcher.UIThread.Invoke(() =>
100-
{
101-
var text = GetDisplayText();
102-
if (!text.Equals(Text, StringComparison.Ordinal))
103-
Text = text;
104-
});
105-
103+
Dispatcher.UIThread.Invoke(UpdateDisplayText);
106104
return true;
107105
}, TimeSpan.FromSeconds(10));
108106
}
@@ -116,15 +114,25 @@ private void StopTimer()
116114
}
117115
}
118116

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

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

127-
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
134+
private string FormatTimePeriod(ulong timestamp)
135+
{
128136
var now = DateTime.Now;
129137
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
130138
var span = now - localTime;
@@ -168,5 +176,6 @@ private string GetDisplayText()
168176
}
169177

170178
private IDisposable _refreshTimer = null;
179+
private ulong _displayTimestamp = 0;
171180
}
172181
}

0 commit comments

Comments
 (0)