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
extern crate webview2_com_sys;
pub use webview2_com_sys::Microsoft;
#[macro_use]
extern crate webview2_com_macros;
mod callback;
mod options;
mod pwstr;
use std::{fmt, sync::mpsc};
use windows::{
core::HRESULT,
Win32::{
Foundation::HWND,
UI::WindowsAndMessaging::{self, MSG},
},
};
pub use callback::*;
pub use options::*;
pub use pwstr::*;
#[derive(Debug)]
pub enum Error {
WindowsError(windows::core::Error),
CallbackError(String),
TaskCanceled,
SendError,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl From<windows::core::Error> for Error {
fn from(err: windows::core::Error) -> Self {
Self::WindowsError(err)
}
}
impl From<HRESULT> for Error {
fn from(err: HRESULT) -> Self {
Self::WindowsError(windows::core::Error::from(err))
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub fn wait_with_pump<T>(rx: mpsc::Receiver<T>) -> Result<T> {
let mut msg = MSG::default();
let hwnd = HWND::default();
loop {
if let Ok(result) = rx.try_recv() {
return Ok(result);
}
unsafe {
match WindowsAndMessaging::GetMessageA(&mut msg, hwnd, 0, 0).0 {
-1 => {
return Err(windows::core::Error::from_win32().into());
}
0 => return Err(Error::TaskCanceled),
_ => {
WindowsAndMessaging::TranslateMessage(&msg);
WindowsAndMessaging::DispatchMessageA(&msg);
}
}
}
}
}