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);
}
}
}
}
}