Skip to main content

zaun_client/ui/
layout.rs

1//! Components V2 layout (types 9–14, 17). Pictures and files are attachments of the same
2//! message, never URLs.
3#![allow(
4    clippy::new_ret_no_self,
5    reason = "unit builders named as in the zaun-bot SDK; each `new` is the component itself"
6)]
7
8use zaun_community_protocol::command::object::{
9    Component, Container as Box_, FileComponent, MediaGallery as Gallery, MediaItem,
10    Section as Sec, Separator as Sep, TextDisplay as Text, Thumbnail as Thumb,
11};
12use zaun_community_protocol::ids::AttachmentId;
13
14use super::{ActionRow, Button};
15
16/// Markdown text (type 10).
17#[derive(Debug, Clone, Copy)]
18pub struct TextDisplay;
19
20impl TextDisplay {
21    /// The component.
22    #[must_use]
23    pub fn new(content: impl Into<String>) -> Component {
24        Component::TextDisplay(Text {
25            id: None,
26            content: content.into(),
27        })
28    }
29}
30
31/// A picture beside a section (type 11).
32#[derive(Debug, Clone, Copy)]
33pub struct Thumbnail;
34
35impl Thumbnail {
36    /// The component; `media` is an image attachment of the message.
37    #[must_use]
38    pub const fn new(media: AttachmentId) -> Component {
39        Component::Thumbnail(Thumb {
40            id: None,
41            media,
42            description: None,
43            spoiler: false,
44        })
45    }
46}
47
48/// Up to ten pictures (type 12).
49#[derive(Debug, Clone, Copy)]
50pub struct MediaGallery;
51
52impl MediaGallery {
53    /// The component.
54    #[must_use]
55    pub fn new(media: &[AttachmentId]) -> Component {
56        let items = media
57            .iter()
58            .map(|m| MediaItem {
59                media: *m,
60                description: None,
61                spoiler: false,
62            })
63            .collect();
64        Component::MediaGallery(Gallery { id: None, items })
65    }
66}
67
68/// One attached file (type 13).
69#[derive(Debug, Clone, Copy)]
70pub struct File;
71
72impl File {
73    /// The component.
74    #[must_use]
75    pub const fn new(file: AttachmentId) -> Component {
76        Component::File(FileComponent {
77            id: None,
78            file,
79            spoiler: false,
80        })
81    }
82}
83
84/// A line or a gap (type 14).
85#[derive(Debug, Clone, Copy)]
86pub struct Separator;
87
88impl Separator {
89    /// A divider line.
90    #[must_use]
91    pub const fn line() -> Component {
92        Component::Separator(Sep {
93            id: None,
94            divider: None,
95            spacing: None,
96        })
97    }
98}
99
100/// One to three texts with a button or thumbnail beside them (type 9).
101#[derive(Debug, Clone, Copy)]
102pub struct Section;
103
104impl Section {
105    /// Texts with a button.
106    #[must_use]
107    pub fn with_button(texts: &[&str], button: Button) -> Component {
108        Self::with(texts, button.build())
109    }
110
111    /// Texts with `accessory` (a button or a [`Thumbnail`]).
112    #[must_use]
113    pub fn with(texts: &[&str], accessory: Component) -> Component {
114        let components = texts.iter().map(|t| TextDisplay::new(*t)).collect();
115        Component::Section(Sec {
116            id: None,
117            components,
118            accessory: Box::new(accessory),
119        })
120    }
121}
122
123/// A box around other components (type 17).
124#[derive(Debug, Clone, Default)]
125pub struct Container {
126    components: Vec<Component>,
127    accent: Option<u32>,
128}
129
130impl Container {
131    /// An empty container.
132    #[must_use]
133    pub fn new() -> Self {
134        Self::default()
135    }
136
137    /// Its accent colour, `0xRRGGBB`.
138    #[must_use]
139    pub const fn accent(mut self, rgb: u32) -> Self {
140        self.accent = Some(rgb);
141        self
142    }
143
144    /// Adds markdown text.
145    #[must_use]
146    pub fn text(mut self, content: impl Into<String>) -> Self {
147        self.components.push(TextDisplay::new(content));
148        self
149    }
150
151    /// Adds any layout component (section, gallery, file, separator).
152    #[must_use]
153    pub fn push(mut self, component: Component) -> Self {
154        self.components.push(component);
155        self
156    }
157
158    /// Adds a row.
159    #[must_use]
160    pub fn row(mut self, row: ActionRow) -> Self {
161        self.components.push(row.build());
162        self
163    }
164
165    /// The component.
166    #[must_use]
167    pub fn build(self) -> Component {
168        Component::Container(Box_ {
169            id: None,
170            components: self.components,
171            accent_color: self.accent,
172            spoiler: false,
173        })
174    }
175}