Skip to main content

gpui_platform/
prompt.rs

1//! File dialog prompt options shared by `gpui` and its platform backends.
2
3use gpui_shared_string::SharedString;
4
5/// The options that can be configured for a file dialog prompt
6#[derive(Clone, Debug)]
7pub struct PathPromptOptions {
8    /// Should the prompt allow files to be selected?
9    pub files: bool,
10    /// Should the prompt allow directories to be selected?
11    pub directories: bool,
12    /// Should the prompt allow multiple files to be selected?
13    pub multiple: bool,
14    /// The prompt to show to a user when selecting a path
15    pub prompt: Option<SharedString>,
16}
17
18/// The options that can be configured for a file dialog prompt
19/// What kind of prompt styling to show
20#[derive(Copy, Clone, Debug, PartialEq)]
21pub enum PromptLevel {
22    /// A prompt that is shown when the user should be notified of something
23    Info,
24
25    /// A prompt that is shown when the user needs to be warned of a potential problem
26    Warning,
27
28    /// A prompt that is shown when a critical problem has occurred
29    Critical,
30}
31
32/// Prompt Button
33#[derive(Clone, Debug, PartialEq)]
34pub enum PromptButton {
35    /// Ok button
36    Ok(SharedString),
37    /// Cancel button
38    Cancel(SharedString),
39    /// Other button
40    Other(SharedString),
41}
42
43impl PromptButton {
44    /// Create a button with label
45    pub fn new(label: impl Into<SharedString>) -> Self {
46        PromptButton::Other(label.into())
47    }
48
49    /// Create an Ok button
50    pub fn ok(label: impl Into<SharedString>) -> Self {
51        PromptButton::Ok(label.into())
52    }
53
54    /// Create a Cancel button
55    pub fn cancel(label: impl Into<SharedString>) -> Self {
56        PromptButton::Cancel(label.into())
57    }
58
59    /// Returns true if this button is a cancel button.
60    #[allow(dead_code)]
61    pub fn is_cancel(&self) -> bool {
62        matches!(self, PromptButton::Cancel(_))
63    }
64
65    /// Returns the label of the button
66    pub fn label(&self) -> &SharedString {
67        match self {
68            PromptButton::Ok(label) => label,
69            PromptButton::Cancel(label) => label,
70            PromptButton::Other(label) => label,
71        }
72    }
73}
74
75impl From<&str> for PromptButton {
76    fn from(value: &str) -> Self {
77        match value.to_lowercase().as_str() {
78            "ok" => PromptButton::Ok("OK".into()),
79            "cancel" => PromptButton::Cancel("Cancel".into()),
80            _ => PromptButton::Other(SharedString::from(value.to_owned())),
81        }
82    }
83}