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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::{
io,
ptr::NonNull,
};
use winapi::{
shared::{
windef::HWND__,
winerror::S_OK,
},
um::{
shobjidl_core::{
ITaskbarList3,
TBPF_ERROR,
TBPF_INDETERMINATE,
TBPF_NOPROGRESS,
TBPF_NORMAL,
TBPF_PAUSED,
TBPFLAG,
},
wincon::GetConsoleWindow,
},
};
use wio::com::ComPtr;
use crate::{
com::ComInterface,
custom_hresult_err,
};
pub struct Window(NonNull<HWND__>);
impl Window {
pub fn get_console_window() -> Option<Self> {
let handle = unsafe { GetConsoleWindow() };
NonNull::new(handle).map(Into::into)
}
}
impl From<NonNull<HWND__>> for Window {
fn from(handle: NonNull<HWND__>) -> Self {
Self(handle)
}
}
impl From<Window> for NonNull<HWND__> {
fn from(window: Window) -> Self {
window.0
}
}
#[derive(Copy, Clone)]
#[repr(u32)]
pub enum ProgressState {
NoProgress = TBPF_NOPROGRESS,
Indeterminate = TBPF_INDETERMINATE,
Normal = TBPF_NORMAL,
Error = TBPF_ERROR,
Paused = TBPF_PAUSED,
}
impl Default for ProgressState {
fn default() -> Self {
ProgressState::NoProgress
}
}
pub struct Taskbar {
taskbar_list_3: ComPtr<ITaskbarList3>,
}
impl Taskbar {
pub fn new() -> io::Result<Self> {
let result = Taskbar {
taskbar_list_3: ITaskbarList3::new_instance()?,
};
Ok(result)
}
pub fn set_progress_state(
&mut self,
window: &mut Window,
state: ProgressState,
) -> io::Result<()> {
unsafe {
match self
.taskbar_list_3
.SetProgressState(window.0.as_ptr(), state as TBPFLAG)
{
S_OK => Ok(()),
err_code => custom_hresult_err("Error setting progress state", err_code),
}
}
}
pub fn set_progress_value(
&mut self,
window: &mut Window,
completed: u64,
total: u64,
) -> io::Result<()> {
unsafe {
match self
.taskbar_list_3
.SetProgressValue(window.0.as_ptr(), completed, total)
{
S_OK => Ok(()),
err_code => custom_hresult_err("Error setting progress value", err_code),
}
}
}
}