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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
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;
/// Synchronous Message Dialog. Supported platforms:
/// * Windows
/// * macOS
/// * Linux (GTK only)
/// * WASM
#[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>,
}
// Oh god, I don't like sending RawWindowHandle between threads but here we go anyways...
// fingers crossed
unsafe impl Send for MessageDialog {}
impl MessageDialog {
pub fn new() -> Self {
Default::default()
}
/// Set level of a dialog
///
/// Depending on the system it can result in level specific icon to show up,
/// the will inform user it message is a error, warning or just information.
pub fn set_level(mut self, level: MessageLevel) -> Self {
self.level = level;
self
}
/// Set title of a dialog
pub fn set_title(mut self, text: impl Into<String>) -> Self {
self.title = text.into();
self
}
/// Set description of a dialog
///
/// Description is a content of a dialog
pub fn set_description(mut self, text: impl Into<String>) -> Self {
self.description = text.into();
self
}
/// Set the set of button that will be displayed on the dialog
///
/// - `Ok` dialog is a single `Ok` button
/// - `OkCancel` dialog, will display 2 buttons: ok and cancel.
/// - `YesNo` dialog, will display 2 buttons: yes and no.
/// - `YesNoCancel` dialog, will display 3 buttons: yes, no, and cancel.
pub fn set_buttons(mut self, btn: MessageButtons) -> Self {
self.buttons = btn;
self
}
/// Set parent windows explicitly (optional)
/// Suported in: `macos` and `windows`
pub fn set_parent<W: HasWindowHandle>(mut self, parent: &W) -> Self {
self.parent = parent.window_handle().ok().map(|x| x.as_raw());
self
}
/// Shows a message dialog and returns the button that was pressed.
pub fn show(self) -> MessageDialogResult {
MessageDialogImpl::show(self)
}
}
/// Asynchronous Message Dialog. Supported platforms:
/// * Windows
/// * macOS
/// * Linux (GTK only)
/// * WASM
#[derive(Default, Debug, Clone)]
pub struct AsyncMessageDialog(MessageDialog);
impl AsyncMessageDialog {
pub fn new() -> Self {
Default::default()
}
/// Set level of a dialog
///
/// Depending on the system it can result in level specific icon to show up,
/// the will inform user it message is a error, warning or just information.
pub fn set_level(mut self, level: MessageLevel) -> Self {
self.0 = self.0.set_level(level);
self
}
/// Set title of a dialog
pub fn set_title(mut self, text: impl Into<String>) -> Self {
self.0 = self.0.set_title(text);
self
}
/// Set description of a dialog
///
/// Description is a content of a dialog
pub fn set_description(mut self, text: impl Into<String>) -> Self {
self.0 = self.0.set_description(text);
self
}
/// Set the set of button that will be displayed on the dialog
///
/// - `Ok` dialog is a single `Ok` button
/// - `OkCancel` dialog, will display 2 buttons ok and cancel.
/// - `YesNo` dialog, will display 2 buttons yes and no.
/// - `YesNoCancel` dialog, will display 3 buttons: yes, no, and cancel.
pub fn set_buttons(mut self, btn: MessageButtons) -> Self {
self.0 = self.0.set_buttons(btn);
self
}
/// Set parent windows explicitly (optional)
/// Suported in: `macos` and `windows`
pub fn set_parent<W: HasWindowHandle>(mut self, parent: &W) -> Self {
self.0 = self.0.set_parent(parent);
self
}
/// Shows a message dialog and returns the button that was pressed.
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,
/// One customizable button.
/// Notice that in Windows, this only works with the feature *common-controls-v6* enabled
OkCustom(String),
/// Two customizable buttons.
/// Notice that in Windows, this only works with the feature *common-controls-v6* enabled
OkCancelCustom(String, String),
/// Three customizable buttons.
/// Notice that in Windows, this only works with the feature *common-controls-v6* enabled
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})"),
}
)
}
}