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

FrmLoading.cs « Forms « UVtools.GUI - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9774a9f9f208c386152a39f76cee1d1f16d925a1 (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
120
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using UVtools.Core;

namespace UVtools.GUI.Forms
{
    public partial class FrmLoading : Form
    {
        public Stopwatch StopWatch { get; } = new Stopwatch();
        public OperationProgress Progress { get; set; }

        //public Task RunningTask { get; set; }

        public string Description
        {
            get => Text;
            set => Text = lbDescription.Text = value;
        }

        public bool CanCancel => Progress?.CanCancel ?? false;

        public FrmLoading()
        {
            InitializeComponent();
        }

        public FrmLoading(string description) : this()
        {
            SetDescription(description);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            e.Handled = true;
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            e.Handled = true;
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            e.Handled = true;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            lbOperation.Text = string.Empty;
            progressBar.Style = ProgressBarStyle.Marquee;
            progressBar.Step = 10;
            StopWatch.Restart();
            timer_Tick(timer, null);
            timer.Start();

            btnCancel.Enabled = CanCancel;
            btnCancel.Text = "Cancel";
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            timer.Stop();
            StopWatch.Stop();
            Progress = null;
        }

        public OperationProgress RestartProgress(bool canCancel = true)
        {
            Progress = new OperationProgress(canCancel);
            return Progress;
        }

        public void SetDescription(string description)
        {
            Text =
            lbDescription.Text = description;
        }

        public void SetProgress(int value)
        {
            progressBar.Style = ProgressBarStyle.Blocks;
            progressBar.Value = value;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            var ts = TimeSpan.FromMilliseconds(StopWatch.ElapsedMilliseconds);
            lbElapsedTime.Text = $"Elapsed Time: {ts.Minutes}m {ts.Seconds}s {ts.Milliseconds}ms";
            btnCancel.Enabled = CanCancel;
            if (ReferenceEquals(Progress, null)) return;

            lbOperation.Text = Progress.ToString();
            if (Progress.ItemCount == 0)
            {
                progressBar.Style = ProgressBarStyle.Marquee;
                progressBar.Step = 10;
            }
            else
            {
                SetProgress(Progress.ProgressStep);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (ReferenceEquals(Progress, null) || Progress.TokenSource.IsCancellationRequested) return;
            Progress.TokenSource.Cancel();
            Progress.CanCancel = false;
            btnCancel.Enabled = false;
            btnCancel.Text = "Canceling...";
        }
    }
}