Skip to main content

hypersig_desktop/
message.rs

1//! The system's message boxes, which tell the person something or ask
2//! them to choose, for [`Os::tell`](crate::Os::tell),
3//! [`Os::confirm`](crate::Os::confirm), and [`Os::ask`](crate::Os::ask).
4
5/// How serious a [`Message`] is, which picks the icon the system shows.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum Level {
9    /// Something the person should know.
10    Info,
11    /// Something that may go wrong, such as losing unsaved changes.
12    Warning,
13    /// Something that went wrong.
14    Error,
15}
16
17/// A message box's title and text, for [`Os::tell`](crate::Os::tell) and the others.
18#[derive(Debug, Clone, PartialEq, Eq)]
19#[must_use = "a message shows only when it is given to `Os::tell`, `Os::confirm`, or `Os::ask`"]
20pub struct Message {
21    level: Level,
22    title: String,
23    text: Option<String>,
24}
25
26impl Message {
27    /// A message of `level` titled `title`, such as "Discard the changes?".
28    pub fn new(level: Level, title: impl Into<String>) -> Self {
29        Self {
30            level,
31            title: title.into(),
32            text: None,
33        }
34    }
35
36    /// Writes `text` under the title, such as what is lost.
37    pub fn body(mut self, text: impl Into<String>) -> Self {
38        self.text = Some(text.into());
39        self
40    }
41
42    /// The level the message was made with.
43    #[must_use]
44    pub const fn level(&self) -> Level {
45        self.level
46    }
47
48    /// The title the message shows.
49    #[must_use]
50    pub fn title(&self) -> &str {
51        &self.title
52    }
53
54    /// The text under the title, if it has one.
55    #[must_use]
56    pub fn text(&self) -> Option<&str> {
57        self.text.as_deref()
58    }
59
60    /// The system's message box, with `buttons`.
61    pub(crate) fn system(self, buttons: rfd::MessageButtons) -> rfd::AsyncMessageDialog {
62        let level = match self.level {
63            Level::Info => rfd::MessageLevel::Info,
64            Level::Warning => rfd::MessageLevel::Warning,
65            Level::Error => rfd::MessageLevel::Error,
66        };
67        let dialog = rfd::AsyncMessageDialog::new()
68            .set_level(level)
69            .set_title(self.title)
70            .set_buttons(buttons);
71        match self.text {
72            Some(text) => dialog.set_description(text),
73            None => dialog,
74        }
75    }
76}
77
78/// The button the person chose in [`Os::ask`](crate::Os::ask)'s message box.
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
80#[expect(
81    clippy::exhaustive_enums,
82    reason = "the box has these three buttons, and closing it counts as Cancel"
83)]
84pub enum Answer {
85    /// Yes, such as saving the changes.
86    Yes,
87    /// No, such as discarding them.
88    No,
89    /// Cancel, or the box closed without a choice.
90    Cancel,
91}
92
93impl Answer {
94    /// The answer the system's box gave.
95    pub(crate) fn from_system(result: &rfd::MessageDialogResult) -> Self {
96        match result {
97            rfd::MessageDialogResult::Yes => Self::Yes,
98            rfd::MessageDialogResult::No => Self::No,
99            _ => Self::Cancel,
100        }
101    }
102}