use crate::backend::AsyncMessageDialogImpl;
use crate::backend::MessageDialogImpl;
use std::fmt::{Display, Formatter};
use std::future::Future;
use raw_window_handle::HasWindowHandle;
use raw_window_handle::RawWindowHandle;
#[derive(Default, Debug, Clone)]
pub struct MessageDialog {
pub(crate) title: String,
pub(crate) description: String,
pub(crate) level: MessageLevel,
pub(crate) buttons: MessageButtons,
pub(crate) parent: Option<RawWindowHandle>,
}
unsafe impl Send for MessageDialog {}
impl MessageDialog {
pub fn new() -> Self {
Default::default()
}
pub fn set_level(mut self, level: MessageLevel) -> Self {
self.level = level;
self
}
pub fn set_title(mut self, text: impl Into<String>) -> Self {
self.title = text.into();
self
}
pub fn set_description(mut self, text: impl Into<String>) -> Self {
self.description = text.into();
self
}
pub fn set_buttons(mut self, btn: MessageButtons) -> Self {
self.buttons = btn;
self
}
pub fn set_parent<W: HasWindowHandle>(mut self, parent: &W) -> Self {
self.parent = parent.window_handle().ok().map(|x| x.as_raw());
self
}
pub fn show(self) -> MessageDialogResult {
MessageDialogImpl::show(self)
}
}
#[derive(Default, Debug, Clone)]
pub struct AsyncMessageDialog(MessageDialog);
impl AsyncMessageDialog {
pub fn new() -> Self {
Default::default()
}
pub fn set_level(mut self, level: MessageLevel) -> Self {
self.0 = self.0.set_level(level);
self
}
pub fn set_title(mut self, text: impl Into<String>) -> Self {
self.0 = self.0.set_title(text);
self
}
pub fn set_description(mut self, text: impl Into<String>) -> Self {
self.0 = self.0.set_description(text);
self
}
pub fn set_buttons(mut self, btn: MessageButtons) -> Self {
self.0 = self.0.set_buttons(btn);
self
}
pub fn set_parent<W: HasWindowHandle>(mut self, parent: &W) -> Self {
self.0 = self.0.set_parent(parent);
self
}
pub fn show(self) -> impl Future<Output = MessageDialogResult> {
AsyncMessageDialogImpl::show_async(self.0)
}
}
#[derive(Debug, Clone, Copy)]
pub enum MessageLevel {
Info,
Warning,
Error,
}
impl Default for MessageLevel {
fn default() -> Self {
Self::Info
}
}
#[derive(Debug, Clone)]
pub enum MessageButtons {
Ok,
OkCancel,
YesNo,
YesNoCancel,
OkCustom(String),
OkCancelCustom(String, String),
YesNoCancelCustom(String, String, String),
}
impl Default for MessageButtons {
fn default() -> Self {
Self::Ok
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum MessageDialogResult {
Yes,
No,
Ok,
#[default]
Cancel,
Custom(String),
}
impl Display for MessageDialogResult {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Yes => "Yes".to_string(),
Self::No => "No".to_string(),
Self::Ok => "Ok".to_string(),
Self::Cancel => "Cancel".to_string(),
Self::Custom(custom) => format!("Custom({custom})"),
}
)
}
}