-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathCommandLogTime.cs
More file actions
74 lines (60 loc) · 1.89 KB
/
CommandLogTime.cs
File metadata and controls
74 lines (60 loc) · 1.89 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
using System;
using System.Threading;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
namespace SourceGit.Views
{
public class CommandLogTime : TextBlock
{
protected override Type StyleKeyOverride => typeof(TextBlock);
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
StopTimer();
}
protected override void OnDataContextChanged(EventArgs e)
{
base.OnDataContextChanged(e);
StopTimer();
if (DataContext 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 static string GetDisplayText(ViewModels.CommandLog log)
{
var endTime = log.IsComplete ? log.EndTime : DateTime.Now;
var duration = endTime - log.StartTime;
if (duration.TotalMinutes >= 1)
return $"{duration.TotalMinutes:G3} min";
if (duration.TotalSeconds >= 1)
return $"{duration.TotalSeconds:G3} s";
return $"{duration.TotalMilliseconds:G3} ms";
}
private Timer _refreshTimer = null;
}
}