Skip to main content

zaun_client/ui/
button.rs

1//! A button (type 2).
2
3use zaun_community_protocol::command::object::{Button as Data, ButtonStyle, Component};
4
5/// A button: one that sends an interaction (`custom_id`), or a link the reader confirms.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct Button(pub(crate) Data);
8
9impl Button {
10    fn styled(
11        style: ButtonStyle,
12        custom_id: Option<String>,
13        url: Option<String>,
14        label: String,
15    ) -> Self {
16        Self(Data {
17            id: None,
18            style,
19            label: Some(label),
20            emoji: None,
21            custom_id,
22            url,
23            disabled: false,
24        })
25    }
26
27    /// A blue button.
28    #[must_use]
29    pub fn primary(custom_id: impl Into<String>, label: impl Into<String>) -> Self {
30        Self::styled(
31            ButtonStyle::Primary,
32            Some(custom_id.into()),
33            None,
34            label.into(),
35        )
36    }
37
38    /// A grey button.
39    #[must_use]
40    pub fn secondary(custom_id: impl Into<String>, label: impl Into<String>) -> Self {
41        Self::styled(
42            ButtonStyle::Secondary,
43            Some(custom_id.into()),
44            None,
45            label.into(),
46        )
47    }
48
49    /// A green button.
50    #[must_use]
51    pub fn success(custom_id: impl Into<String>, label: impl Into<String>) -> Self {
52        Self::styled(
53            ButtonStyle::Success,
54            Some(custom_id.into()),
55            None,
56            label.into(),
57        )
58    }
59
60    /// A red button.
61    #[must_use]
62    pub fn danger(custom_id: impl Into<String>, label: impl Into<String>) -> Self {
63        Self::styled(
64            ButtonStyle::Danger,
65            Some(custom_id.into()),
66            None,
67            label.into(),
68        )
69    }
70
71    /// A link (`https://` only); readers confirm before it opens.
72    #[must_use]
73    pub fn link(url: impl Into<String>, label: impl Into<String>) -> Self {
74        Self::styled(ButtonStyle::Link, None, Some(url.into()), label.into())
75    }
76
77    /// Adds a Unicode emoji.
78    #[must_use]
79    pub fn emoji(mut self, emoji: impl Into<String>) -> Self {
80        self.0.emoji = Some(emoji.into());
81        self
82    }
83
84    /// Shown but not clickable.
85    #[must_use]
86    pub const fn disabled(mut self, disabled: bool) -> Self {
87        self.0.disabled = disabled;
88        self
89    }
90
91    /// The component.
92    #[must_use]
93    pub fn build(self) -> Component {
94        Component::Button(self.0)
95    }
96}