Repository navigation
Expand file tree
/
Copy pathdelay-capture.cpp
More file actions
77 lines (77 loc) · 1.87 KB
/
Copy pathdelay-capture.cpp
File metadata and controls
77 lines (77 loc) · 1.87 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
75
76
77
#include "delay-capture.hpp"
#include <algorithm>
DelayCapture::DelayCapture(QObject *parent, std::function<qint64()> clock)
: QObject(parent), m_clock(std::move(clock)) {
m_elapsed.start();
if (!m_clock)
m_clock = [this] { return m_elapsed.elapsed(); };
m_timer.setInterval(50);
connect(&m_timer, &QTimer::timeout, this, &DelayCapture::tick);
}
void DelayCapture::begin(int seconds) {
if (active() || seconds < 1 || seconds > 30)
return;
++m_generation;
m_seconds = seconds;
m_remaining = seconds;
m_phase = Hiding;
emit changed();
emit hideRequested(m_generation);
}
void DelayCapture::desktopCleared(quint64 generation, bool success) {
if (!current(generation) || m_phase != Hiding)
return;
if (!success) {
cancel();
emit failed();
return;
}
m_deadline = m_clock() + m_seconds * 1000;
m_phase = Countdown;
m_timer.start();
emit changed();
emit badgeRequested();
}
void DelayCapture::tick() {
if (m_phase != Countdown)
return;
const qint64 left = std::max(qint64(0), m_deadline - m_clock());
const int remaining = int((left + 999) / 1000);
if (remaining != m_remaining) {
m_remaining = remaining;
emit changed();
}
if (left == 0) {
m_timer.stop();
m_phase = Clearing;
emit clearRequested(m_generation);
}
}
void DelayCapture::badgeCleared(quint64 generation, bool success) {
if (!current(generation) || m_phase != Clearing)
return;
if (!success) {
cancel();
emit failed();
return;
}
m_phase = Capturing;
emit grabRequested(m_generation);
}
void DelayCapture::cancel() {
if (!active())
return;
++m_generation;
m_phase = Idle;
m_timer.stop();
m_remaining = 0;
emit changed();
emit cancelled();
}
void DelayCapture::complete(quint64 generation) {
if (!current(generation) || m_phase != Capturing)
return;
m_phase = Idle;
m_remaining = 0;
emit changed();
}