system_extensions/dialogues/
messagebox.rs

1use crate::core::Bitflagable;
2
3bitflags! {
4/**
5    The type of window the message box should be.
6    A Window Type determines what buttons are shown.
7*/
8    pub struct WindowType: u32 {
9        const OK = 0x00000000;
10        const OK_CANCEL = 0x00000001;
11        const ABORT_RETRY_IGNORE = 0x00000002;
12        const CANCEL_TRY_CONTINUE = 0x00000006;
13        const HELP = 0x00004000;
14        const RETRY_CANCEL = 0x00000005;
15        const YES_NO = 0x00000004;
16        const YES_NO_CANCEL = 0x00000003;
17    }
18}
19
20impl Bitflagable<u32> for WindowType {
21    fn get_bits(self) -> u32 {
22        self.bits
23    }
24}
25
26bitflags! {
27/**
28    The type of Icon for the MessageBox. (Information, Warning, Error).
29    **Note:** The Question icon is determined to be deprecated on Windows and does not exist
30        on other operating systems.
31*/
32     pub struct IconType: u32 {
33    /*
34           Icon Properties
35         */
36        const ICON_WARNING = 0x00000030;
37        const ICON_INFORMATION = 0x00000040;
38        const ICON_QUESTION = 0x00000020;
39        const ICON_ERROR = 0x00000010;
40    }
41}
42
43impl Bitflagable<u32> for IconType {
44    fn get_bits(self) -> u32 {
45        self.bits
46    }
47}
48
49bitflags! {
50/**
51    The Default Button for the window. This defines which button should
52    be selected when the MessageBox opens.
53*/
54    pub struct DefaultButton: u32 {
55     /*
56            Default Buttons
57         */
58        const DEFAULT_BUTTON_ONE = 0x00000000;
59        const DEFAULT_BUTTON_TWO = 0x00000100;
60        const DEFAULT_BUTTON_THREE = 0x00000200;
61        const DEFAULT_BUTTON_FOUR = 0x00000300;
62    }
63}
64
65impl Bitflagable<u32> for DefaultButton {
66    fn get_bits(self) -> u32 {
67        self.bits
68    }
69}
70
71bitflags! {
72/**
73    This depicts what button was pressed in the message box.
74*/
75    pub struct BoxReturn: i32{
76        const ABORT = 3;
77        const CANCEL = 2;
78        const CONTINUE = 11;
79        const IGNORE = 5;
80        const NO = 7;
81        const OK = 1;
82        const RETRY = 4;
83        const TRY_AGAIN = 10;
84        const YES = 6;
85        const NONE = 0;
86    }
87}
88
89impl Bitflagable<i32> for BoxReturn {
90    fn get_bits(self) -> i32 {
91        self.bits
92    }
93}
94
95/**
96    A builder struct to create a MessageBox.
97
98   # Examples
99   Standard Window:
100   ```rust
101   use system_extensions::dialogues::messagebox::{MessageBox, BoxReturn};
102   let result = MessageBox::new("My Title", "The content of the message box!").show();
103
104   if result.unwrap() == BoxReturn::OK {
105       println!("The user acknowledge the message!");
106   }
107   ```
108   Window with Icon:
109   ```rust
110   use system_extensions::dialogues::messagebox::{MessageBox, BoxReturn, IconType};
111   let result = MessageBox::new("My Title", "The content of the message box!")
112       .set_icon_type(IconType::ICON_ERROR)
113       .show();
114
115   if result.unwrap() == BoxReturn::OK {
116       println!("The user acknowledge the error!");
117   }
118   ```
119*/
120#[derive(Clone, Copy, Debug)]
121pub struct MessageBox {
122    pub(crate) title: &'static str,
123    pub(crate) content: &'static str,
124    pub(crate) window_type: WindowType,
125    pub(crate) icon_type: IconType,
126    pub(crate) default_button: DefaultButton,
127}
128
129impl MessageBox {
130    /**
131        Construct a new MessageBox.
132
133      # Params
134      title: &str -> The title of the window.<br>
135      content: &str -> The content of the window.<br>
136
137      # Returns
138      The instance of a default MessageBox.
139    */
140    pub fn new(title: &'static str, content: &'static str) -> MessageBox {
141        MessageBox {
142            title,
143            content,
144            window_type: WindowType::OK_CANCEL,
145            icon_type: IconType::ICON_INFORMATION,
146            default_button: DefaultButton::DEFAULT_BUTTON_ONE,
147        }
148    }
149
150    /**
151        Set the title of the MessageBox.
152
153        # Params
154        title: &str -> The title to set.<br>
155
156        # Returns
157        A mutable instance of the MessageBox.
158    */
159    pub fn set_title(&mut self, title: &'static str) -> &mut Self {
160        self.title = title;
161        self
162    }
163
164    /**
165    Set the content of the MessageBox.
166
167    # Params
168    content: &str -> The content to set.<br>
169
170    # Returns
171    A mutable instance of the MessageBox.
172    */
173    pub fn set_content(&mut self, content: &'static str) -> &mut Self {
174        self.content = content;
175        self
176    }
177
178    /**
179    Set the type of the MessageBox. (The Buttons that are shown).
180
181    # Params
182    window_type: [`WindowType`] -> The WindowType to set.<br>
183
184    # Returns
185    A mutable instance of the MessageBox.
186*/
187    pub fn set_window_type(&mut self, window_type: WindowType) -> &mut Self {
188        self.window_type = window_type;
189        self
190    }
191
192    /**
193    Set the icon type of the MessageBox.
194
195    # Params
196    icon_type: [`IconType`] -> The IconType to set.<br>
197
198    # Returns
199    A mutable instance of the MessageBox.
200*/
201    pub fn set_icon_type(&mut self, icon_type: IconType) -> &mut Self {
202        self.icon_type = icon_type;
203        self
204    }
205
206    /**
207    Set the default button for the MessageBox.
208
209    # Params
210    default_button: [`DefaultButton`] -> The default button of the MessageBox.<br>
211
212    # Returns
213    A mutable instance of the MessageBox.
214*/
215    pub fn set_default_button(&mut self, default_button: DefaultButton) -> &mut Self {
216        self.default_button = default_button;
217        self
218    }
219
220    /**
221    Display the MessageBox.
222
223    # Returns
224    Result<[`BoxReturn`], String> -> The result of the MessageBox.
225*/
226    pub fn show(&self) -> Result<BoxReturn, String> {
227        crate::internal::dialogues::create_message_box(*self)
228    }
229}