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
use crate::{Dialog, MessageAlert, MessageConfirm, MessageType, Result}; impl Dialog for MessageAlert<'_> { type Output = (); fn show(self) -> Result<Self::Output> { super::process_init(); message_box(MessageBoxParams { title: self.title, text: self.text, typ: self.typ, ask: false, })?; Ok(()) } } impl Dialog for MessageConfirm<'_> { type Output = bool; fn show(self) -> Result<Self::Output> { super::process_init(); message_box(MessageBoxParams { title: self.title, text: self.text, typ: self.typ, ask: true, }) } } struct MessageBoxParams<'a> { title: &'a str, text: &'a str, typ: MessageType, ask: bool, } fn message_box(params: MessageBoxParams) -> Result<bool> { use std::ffi::OsStr; use std::iter::once; use std::os::windows::ffi::OsStrExt; use std::ptr::null_mut; use winapi::um::winuser::{ MessageBoxW, IDYES, MB_ICONERROR, MB_ICONINFORMATION, MB_ICONWARNING, MB_OK, MB_YESNO, }; let text: Vec<u16> = OsStr::new(params.text) .encode_wide() .chain(once(0)) .collect(); let caption: Vec<u16> = OsStr::new(params.title) .encode_wide() .chain(once(0)) .collect(); let u_type = match params.typ { MessageType::Info => MB_ICONINFORMATION, MessageType::Warning => MB_ICONWARNING, MessageType::Error => MB_ICONERROR, } | if params.ask { MB_YESNO } else { MB_OK }; let ret = super::with_visual_styles(|| unsafe { MessageBoxW(null_mut(), text.as_ptr(), caption.as_ptr(), u_type) }); match ret { 0 => Err(std::io::Error::last_os_error())?, x => Ok(x == IDYES), } }