1extern crate webview2_com_sys;
2pub use webview2_com_sys::Microsoft;
3
4#[macro_use]
5extern crate webview2_com_macros;
6
7mod callback;
8mod options;
9mod pwstr;
10
11use std::{fmt, sync::mpsc};
12
13use windows::{
14 core::HRESULT,
15 Win32::UI::WindowsAndMessaging::{self, MSG},
16};
17
18pub use callback::*;
19pub use options::*;
20pub use pwstr::*;
21
22#[derive(Debug)]
23pub enum Error {
24 WindowsError(windows::core::Error),
25 CallbackError(String),
26 TaskCanceled,
27 SendError,
28}
29
30impl fmt::Display for Error {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 write!(f, "{self:?}")
33 }
34}
35
36impl From<windows::core::Error> for Error {
37 fn from(err: windows::core::Error) -> Self {
38 Self::WindowsError(err)
39 }
40}
41
42impl From<HRESULT> for Error {
43 fn from(err: HRESULT) -> Self {
44 Self::WindowsError(windows::core::Error::from(err))
45 }
46}
47
48pub type Result<T> = std::result::Result<T, Error>;
49
50pub fn wait_with_pump<T>(rx: mpsc::Receiver<T>) -> Result<T> {
59 let mut msg = MSG::default();
60
61 loop {
62 if let Ok(result) = rx.try_recv() {
63 return Ok(result);
64 }
65
66 unsafe {
67 match WindowsAndMessaging::GetMessageA(&mut msg, None, 0, 0).0 {
68 -1 => {
69 return Err(windows::core::Error::from_win32().into());
70 }
71 0 => return Err(Error::TaskCanceled),
72 _ => {
73 let _ = WindowsAndMessaging::TranslateMessage(&msg);
74 WindowsAndMessaging::DispatchMessageA(&msg);
75 }
76 }
77 }
78 }
79}