blob: 79611e8e2fd5c5ae4edefea7376ddfe4b7e22d1b (
plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.Diagnostics;
using System.Timers;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using UVtools.Core.Operations;
using UVtools.WPF.Controls;
using UVtools.WPF.Structures;
namespace UVtools.WPF.Windows
{
public class ProgressWindow : WindowEx, IDisposable
{
public Stopwatch StopWatch => Progress.StopWatch;
public OperationProgress Progress { get; } = new OperationProgress();
private LogItem _logItem = new LogItem();
private Timer _timer = new Timer(100) { AutoReset = true };
public bool CanCancel
{
get => Progress?.CanCancel ?? false;
set
{
Progress.CanCancel = value;
RaisePropertyChanged();
}
}
public ProgressWindow()
{
InitializeComponent();
Cursor = new Cursor(StandardCursorType.AppStarting);
_timer.Elapsed += (sender, args) =>
{
Progress.TriggerRefresh();
};
DataContext = this;
}
public ProgressWindow(string title) : this()
{
SetTitle(title);
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_timer.Stop();
Progress.StopWatch.Stop();
_logItem.ElapsedTime = Math.Round(Progress.StopWatch.Elapsed.TotalSeconds, 2);
App.MainWindow.AddLog(_logItem);
}
public void OnClickCancel()
{
if (!CanCancel) return;
DialogResult = DialogResults.Cancel;
RaisePropertyChanged(nameof(CanCancel));
Progress.TokenSource.Cancel();
}
public void SetTitle(string title)
{
Progress.Title = title;
_logItem.Description = title;
}
public OperationProgress RestartProgress(bool canCancel = true)
{
CanCancel = canCancel;
Progress.StopWatch.Restart();
if (!_timer.Enabled)
{
_timer.Enabled = true;
_timer.Start();
}
return Progress;
}
public void Dispose()
{
_timer.Close();
_timer.Dispose();
}
}
}
|