mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-22 04:34:59 +00:00
feature: add a toggle button to change the commit time display mode to time period it is passed from now (#259)
This commit is contained in:
parent
9b21269844
commit
1eb77a5e49
5 changed files with 167 additions and 8 deletions
|
@ -3,7 +3,9 @@ using System;
|
|||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
namespace SourceGit.Views
|
||||
|
@ -67,6 +69,123 @@ 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<ulong> TimestampProperty =
|
||||
AvaloniaProperty.Register<CommitTimeTextBlock, ulong>(nameof(Timestamp));
|
||||
|
||||
public ulong Timestamp
|
||||
{
|
||||
get => GetValue(TimestampProperty);
|
||||
set => SetValue(TimestampProperty, 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)
|
||||
StopTimer();
|
||||
else
|
||||
StartTimer();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnLoaded(RoutedEventArgs e)
|
||||
{
|
||||
base.OnLoaded(e);
|
||||
|
||||
if (!ShowAsDateTime)
|
||||
StartTimer();
|
||||
}
|
||||
|
||||
protected override void OnUnloaded(RoutedEventArgs e)
|
||||
{
|
||||
base.OnUnloaded(e);
|
||||
StopTimer();
|
||||
}
|
||||
|
||||
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(4));
|
||||
}
|
||||
|
||||
private void StopTimer()
|
||||
{
|
||||
if (_refreshTimer != null)
|
||||
{
|
||||
_refreshTimer.Dispose();
|
||||
_refreshTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
private string GetDisplayText()
|
||||
{
|
||||
if (ShowAsDateTime)
|
||||
return DateTime.UnixEpoch.AddSeconds(Timestamp).ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss");
|
||||
|
||||
var today = DateTime.Today;
|
||||
var committerTime = DateTime.UnixEpoch.AddSeconds(Timestamp).ToLocalTime();
|
||||
|
||||
if (committerTime >= today)
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var timespan = now - committerTime;
|
||||
if (timespan.TotalHours > 1)
|
||||
return $"{(int)timespan.TotalHours} hours ago";
|
||||
|
||||
return timespan.TotalMinutes < 1 ? "Just now" : $"{(int)timespan.TotalMinutes} minutes ago";
|
||||
}
|
||||
|
||||
var diffYear = today.Year - committerTime.Year;
|
||||
if (diffYear == 0)
|
||||
{
|
||||
var diffMonth = today.Month - committerTime.Month;
|
||||
if (diffMonth > 0)
|
||||
return diffMonth == 1 ? "Last month" : $"{diffMonth} months ago";
|
||||
|
||||
var diffDay = today.Day - committerTime.Day;
|
||||
return diffDay == 1 ? "Yesterday" : $"{diffDay} days ago";
|
||||
}
|
||||
|
||||
return diffYear == 1 ? "Last year" : $"{diffYear} years ago";
|
||||
}
|
||||
|
||||
private IDisposable _refreshTimer = null;
|
||||
}
|
||||
|
||||
public class CommitGraph : Control
|
||||
{
|
||||
public static readonly StyledProperty<Models.CommitGraph> GraphProperty =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue