Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ProgressWindow.axaml.cs « Windows « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbfa5741a2d6c64050aae3588bee46c36f7bca13 (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
108
109
110
111
112
113
114
115
116
117
118
119
/*
 *                     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 ();

        private LogItem _logItem = new ();

        private Timer _timer = new (200) { AutoReset = true };

        private long _lastTotalSeconds = 0;

        public bool CanCancel
        {
            get => Progress?.CanCancel ?? false;
            set
            {
                Progress.CanCancel = value;
                RaisePropertyChanged();
            }
        }

        public ProgressWindow()
        {
            InitializeComponent();

            Cursor = new Cursor(StandardCursorType.AppStarting);
            
            _timer.Elapsed += (sender, args) =>
            {
                var elapsedSeconds = StopWatch.ElapsedMilliseconds / 1000;
                if (_lastTotalSeconds == elapsedSeconds) return;
                /*Debug.WriteLine(StopWatch.ElapsedMilliseconds);
                Debug.WriteLine(elapsedSeconds);
                Debug.WriteLine(_lastTotalSeconds);*/
                _lastTotalSeconds = elapsedSeconds;


                Dispatcher.UIThread.InvokeAsync(() => Progress.TriggerRefresh(), DispatcherPriority.Render);

            };

            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();
            _lastTotalSeconds = 0;

            if (!_timer.Enabled)
            {
                _timer.Enabled = true;
                _timer.Start();
            }

            return Progress;
        }

        public void Dispose()
        {
            _timer.Close();
            _timer.Dispose();
        }
    }
}