forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLogTime.cs
More file actions
80 lines (67 loc) · 2.17 KB
/
Copy pathCommandLogTime.cs
File metadata and controls
80 lines (67 loc) · 2.17 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
using System;
using System.Threading;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
namespace SourceGit.Views
{
public class CommandLogTime : TextBlock
{
public static readonly StyledProperty<ViewModels.CommandLog> LogProperty =
AvaloniaProperty.Register<CommandLogTime, ViewModels.CommandLog>(nameof(Log));
public ViewModels.CommandLog Log
{
get => GetValue(LogProperty);
set => SetValue(LogProperty, value);
}
protected override Type StyleKeyOverride => typeof(TextBlock);
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
StopTimer();
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == LogProperty)
{
StopTimer();
if (change.NewValue is ViewModels.CommandLog log)
SetupCommandLog(log);
else
Text = string.Empty;
}
}
private void SetupCommandLog(ViewModels.CommandLog log)
{
Text = GetDisplayText(log);
if (log.IsComplete)
return;
_refreshTimer = new Timer(_ =>
{
Dispatcher.UIThread.Invoke(() =>
{
Text = GetDisplayText(log);
if (log.IsComplete)
StopTimer();
});
}, null, 0, 100);
}
private void StopTimer()
{
if (_refreshTimer is not null)
{
_refreshTimer.Dispose();
_refreshTimer = null;
}
}
private string GetDisplayText(ViewModels.CommandLog log)
{
var endTime = log.IsComplete ? log.EndTime : DateTime.Now;
var duration = (endTime - log.StartTime).ToString(@"hh\:mm\:ss\.fff");
return $"{log.StartTime:T} ({duration})";
}
private Timer _refreshTimer = null;
}
}