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

MainWindow.Progress.cs « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14f5eaf9cc69754d6f9040e1ce44a29831904144 (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
/*
 *                     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.Timers;
using Avalonia.Threading;
using UVtools.Core.Operations;
using UVtools.WPF.Structures;

namespace UVtools.WPF
{
    public partial class MainWindow
    {
        #region Members
        public OperationProgress Progress { get; } = new();
        private readonly Timer _progressTimer = new(200) { AutoReset = true };
        private long _progressLastTotalSeconds;
        private LogItem _progressLogItem;
        private bool _isProgressVisible;

        #endregion

        #region Properties

        public bool IsProgressVisible
        {
            get => _isProgressVisible;
            set => RaiseAndSetIfChanged(ref _isProgressVisible, value);
        }

        #endregion

        public void InitProgress()
        {
            _progressTimer.Elapsed += (sender, args) =>
            {
                var elapsedSeconds = Progress.StopWatch.ElapsedMilliseconds / 1000;
                if (_progressLastTotalSeconds == elapsedSeconds) return;
                /*Debug.WriteLine(StopWatch.ElapsedMilliseconds);
                Debug.WriteLine(elapsedSeconds);
                Debug.WriteLine(_lastTotalSeconds);*/
                _progressLastTotalSeconds = elapsedSeconds;


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

            };
        }

        public void ProgressOnClickCancel()
        {
            if (!Progress.CanCancel) return;
            DialogResult = DialogResults.Cancel;
            Progress.CanCancel = false;
            Progress.TokenSource.Cancel();
        }

        public void ProgressShow(string title, bool canCancel = true)
        {
            IsGUIEnabled = false;
            Progress.Init(canCancel);
            Progress.Title = title;
            _progressLogItem = new(title);

            Progress.StopWatch.Restart();
            _progressLastTotalSeconds = 0;

            if (!_progressTimer.Enabled)
            {
                _progressTimer.Start();
            }

            Progress.TriggerRefresh();

            IsProgressVisible = true;

            InvalidateVisual();
        }

        public void ProgressFinish()
        {
            _progressTimer.Stop();
            Progress.StopWatch.Stop();
            _progressLogItem.ElapsedTime = Math.Round(Progress.StopWatch.Elapsed.TotalSeconds, 2);
            App.MainWindow.AddLog(_progressLogItem);
            IsProgressVisible = false;
            InvalidateVisual();
        }
    }
}