Skip to main content

zaun_client/ui/
select.rs

1//! Selects (types 3, 5–8) and action rows (type 1).
2
3use zaun_community_protocol::command::object::{
4    ActionRow as Row, Component, EntitySelect, SelectOption, StringSelect,
5};
6
7use super::Button;
8
9/// What an entity select offers.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum EntityKind {
12    /// Members (type 5).
13    User,
14    /// Roles (type 6).
15    Role,
16    /// Members and roles (type 7).
17    Mentionable,
18    /// Channels (type 8).
19    Channel,
20}
21
22/// A select menu.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct Select(pub(crate) Component);
25
26impl Select {
27    /// Choices of `(label, value)`.
28    #[must_use]
29    pub fn strings(custom_id: impl Into<String>, options: &[(&str, &str)]) -> Self {
30        let options = options
31            .iter()
32            .map(|(label, value)| SelectOption {
33                label: (*label).to_owned(),
34                value: (*value).to_owned(),
35                description: None,
36                emoji: None,
37                default: false,
38            })
39            .collect();
40        Self(Component::StringSelect(StringSelect {
41            id: None,
42            custom_id: custom_id.into(),
43            options,
44            placeholder: None,
45            min_values: None,
46            max_values: None,
47            required: None,
48            disabled: false,
49        }))
50    }
51
52    /// Members, roles or channels.
53    #[must_use]
54    pub fn entities(kind: EntityKind, custom_id: impl Into<String>) -> Self {
55        let s = EntitySelect {
56            id: None,
57            custom_id: custom_id.into(),
58            placeholder: None,
59            min_values: None,
60            max_values: None,
61            default_values: Vec::new(),
62            channel_types: Vec::new(),
63            required: None,
64            disabled: false,
65        };
66        Self(match kind {
67            EntityKind::User => Component::UserSelect(s),
68            EntityKind::Role => Component::RoleSelect(s),
69            EntityKind::Mentionable => Component::MentionableSelect(s),
70            EntityKind::Channel => Component::ChannelSelect(s),
71        })
72    }
73
74    /// The text shown before a choice.
75    #[must_use]
76    pub fn placeholder(mut self, text: impl Into<String>) -> Self {
77        let text = Some(text.into());
78        match &mut self.0 {
79            Component::StringSelect(s) => s.placeholder = text,
80            Component::UserSelect(s)
81            | Component::RoleSelect(s)
82            | Component::MentionableSelect(s)
83            | Component::ChannelSelect(s) => s.placeholder = text,
84            _ => {}
85        }
86        self
87    }
88
89    /// How many may be chosen.
90    #[must_use]
91    pub fn range(mut self, min: u32, max: u32) -> Self {
92        match &mut self.0 {
93            Component::StringSelect(s) => (s.min_values, s.max_values) = (Some(min), Some(max)),
94            Component::UserSelect(s)
95            | Component::RoleSelect(s)
96            | Component::MentionableSelect(s)
97            | Component::ChannelSelect(s) => (s.min_values, s.max_values) = (Some(min), Some(max)),
98            _ => {}
99        }
100        self
101    }
102
103    /// The component.
104    #[must_use]
105    pub fn build(self) -> Component {
106        self.0
107    }
108}
109
110/// A row of up to five buttons, or one select.
111#[derive(Debug, Clone, Default, PartialEq, Eq)]
112pub struct ActionRow(Vec<Component>);
113
114impl ActionRow {
115    /// An empty row.
116    #[must_use]
117    pub fn new() -> Self {
118        Self::default()
119    }
120
121    /// A row of buttons.
122    #[must_use]
123    pub fn buttons(buttons: impl IntoIterator<Item = Button>) -> Self {
124        Self(buttons.into_iter().map(Button::build).collect())
125    }
126
127    /// A row holding one select.
128    #[must_use]
129    pub fn select(select: Select) -> Self {
130        Self(vec![select.build()])
131    }
132
133    /// Adds a button.
134    #[must_use]
135    pub fn button(mut self, button: Button) -> Self {
136        self.0.push(button.build());
137        self
138    }
139
140    /// The component.
141    #[must_use]
142    pub fn build(self) -> Component {
143        Component::ActionRow(Row {
144            id: None,
145            components: self.0,
146        })
147    }
148}