-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathprogress_taskbar.h
80 lines (66 loc) · 2.22 KB
/
progress_taskbar.h
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
/**
* Part of WinLamb - Win32 API Lambda Library
* https://github.com/rodrigocfd/winlamb
* Copyright 2017-present Rodrigo Cesar de Freitas Dias
* This library is released under the MIT License
*/
#pragma once
#include <stdexcept>
#include "com.h"
#include "wnd.h"
#include <ShObjIdl.h>
namespace wl {
// Allows to show a progress bar in the taskbar button of the window, in green, yellow or red.
class progress_taskbar final {
private:
HWND _hWnd = nullptr;
com::lib _comLib{com::lib::init::LATER};
com::ptr<ITaskbarList3> _bar;
public:
progress_taskbar() = default;
progress_taskbar(progress_taskbar&&) = default;
progress_taskbar& operator=(progress_taskbar&&) = default; // movable only
progress_taskbar& init(HWND hOwner) {
if (this->_bar) {
throw std::logic_error("Trying to init a progress taskbar twice.");
} else {
this->_hWnd = hOwner;
this->_comLib.initialize();
this->_bar.co_create_instance(CLSID_TaskbarList);
}
return *this;
}
progress_taskbar& init(const wnd* owner) {
return this->init(owner->hwnd());
}
// Value is 0-100.
progress_taskbar& set_pos(size_t percent, size_t total) noexcept {
this->_bar->SetProgressValue(this->_hWnd, static_cast<ULONGLONG>(percent),
static_cast<ULONGLONG>(total));
return *this;
}
// Value is 0-100.
progress_taskbar& set_pos(double percent) noexcept {
return this->set_pos(static_cast<size_t>(percent + 0.5), 100);
}
progress_taskbar& set_waiting(bool isWaiting) noexcept {
return this->_set_state(isWaiting ? TBPF_INDETERMINATE : TBPF_NORMAL);
}
progress_taskbar& set_pause(bool isPaused) noexcept {
return this->_set_state(isPaused ? TBPF_PAUSED : TBPF_NORMAL);
}
progress_taskbar& set_error(bool hasError) noexcept {
return this->_set_state(hasError ? TBPF_ERROR : TBPF_NORMAL);
}
// Removes the status from the taskbar button.
progress_taskbar& clear() noexcept {
return this->_set_state(TBPF_NOPROGRESS);
}
private:
progress_taskbar& _set_state(TBPFLAG state) noexcept {
// Apparently this doesn't work within WM_CREATE processing.
this->_bar->SetProgressState(this->_hWnd, state);
return *this;
}
};
}//namespace wl