Skip to main content

djs_model/component/
layout.rs

1//! Layout components: action rows, sections, containers, separators and labels.
2
3use compact_str::CompactString;
4use serde::Serialize;
5
6use super::Component;
7use crate::macros::int_enum;
8
9/// An [action row](https://docs.discord.com/developers/components/reference#action-row): up to
10/// 5 [buttons](super::Button) or a single select menu, at the top level of a message or in a
11/// [container](Container).
12///
13/// In modals, action rows holding text inputs are **deprecated** in favor of [`Label`].
14///
15/// discord.js counterpart: `ActionRow` / `ActionRowBuilder`.
16#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
17#[non_exhaustive]
18pub struct ActionRow {
19    /// The component's numeric identifier (generated by Discord when not set).
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub id: Option<i32>,
22    /// The buttons or the select menu.
23    pub components: Vec<Component>,
24}
25
26impl ActionRow {
27    /// Creates an action row.
28    ///
29    /// discord.js counterpart: `ActionRowBuilder` (`@discordjs/builders`).
30    #[must_use]
31    pub const fn new(components: Vec<Component>) -> Self {
32        Self {
33            id: None,
34            components,
35        }
36    }
37}
38
39/// A [section](https://docs.discord.com/developers/components/reference#section): 1-3
40/// [text displays](super::TextDisplay) next to an accessory ([button](super::Button) or
41/// [thumbnail](super::Thumbnail)). Messages only.
42///
43/// discord.js counterpart: `SectionComponent` / `SectionBuilder`.
44#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
45#[non_exhaustive]
46pub struct Section {
47    /// The component's numeric identifier (generated by Discord when not set).
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub id: Option<i32>,
50    /// The text displays (1-3).
51    pub components: Vec<Component>,
52    /// The button or thumbnail next to the text.
53    pub accessory: Box<Component>,
54}
55
56impl Section {
57    /// Creates a section.
58    ///
59    /// discord.js counterpart: `SectionBuilder` (`@discordjs/builders`).
60    #[must_use]
61    pub fn new(components: Vec<Component>, accessory: Component) -> Self {
62        Self {
63            id: None,
64            components,
65            accessory: Box::new(accessory),
66        }
67    }
68}
69
70/// A [container](https://docs.discord.com/developers/components/reference#container): a
71/// visually grouped box of components with an optional accent color, like an embed. Messages
72/// only.
73///
74/// discord.js counterpart: `ContainerComponent` / `ContainerBuilder`.
75#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
76#[non_exhaustive]
77pub struct Container {
78    /// The component's numeric identifier (generated by Discord when not set).
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub id: Option<i32>,
81    /// The action rows, text displays, sections, media galleries, separators and files inside.
82    pub components: Vec<Component>,
83    /// The accent color on the left, as an RGB integer.
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub accent_color: Option<u32>,
86    /// Whether the container is blurred until clicked.
87    #[serde(skip_serializing_if = "core::ops::Not::not")]
88    pub spoiler: bool,
89}
90
91impl Container {
92    /// Creates a container without accent color.
93    ///
94    /// discord.js counterpart: `ContainerBuilder` (`@discordjs/builders`).
95    #[must_use]
96    pub const fn new(components: Vec<Component>) -> Self {
97        Self {
98            id: None,
99            components,
100            accent_color: None,
101            spoiler: false,
102        }
103    }
104}
105
106/// A [separator](https://docs.discord.com/developers/components/reference#separator): vertical
107/// space between components, with an optional line. Messages only.
108///
109/// discord.js counterpart: `SeparatorComponent` / `SeparatorBuilder`.
110#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
111#[non_exhaustive]
112pub struct Separator {
113    /// The component's numeric identifier (generated by Discord when not set).
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub id: Option<i32>,
116    /// Whether a line is drawn (default `true`).
117    pub divider: bool,
118    /// The amount of space (Discord's default: [`Small`](SeparatorSpacing::Small)).
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub spacing: Option<SeparatorSpacing>,
121}
122
123impl Separator {
124    /// Creates a separator with a line and the default spacing.
125    ///
126    /// discord.js counterpart: `SeparatorBuilder` (`@discordjs/builders`).
127    #[must_use]
128    pub const fn new() -> Self {
129        Self {
130            id: None,
131            divider: true,
132            spacing: None,
133        }
134    }
135}
136
137impl Default for Separator {
138    fn default() -> Self {
139        Self::new()
140    }
141}
142
143int_enum! {
144    /// The [space a separator adds](https://docs.discord.com/developers/components/reference#separator-separator-structure).
145    ///
146    /// discord.js counterpart: `SeparatorSpacingSize`.
147    #[doc(alias = "SeparatorSpacingSize")]
148    pub enum SeparatorSpacing: u8 {
149        /// Small padding.
150        Small = 1,
151        /// Large padding.
152        Large = 2,
153    }
154}
155
156/// A [label](https://docs.discord.com/developers/components/reference#label): a title and
157/// description for one input in a modal (text input, select menu, file upload, radio group,
158/// checkbox group or checkbox).
159///
160/// discord.js counterpart: `LabelComponent` / `LabelBuilder`.
161#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
162#[non_exhaustive]
163pub struct Label {
164    /// The component's numeric identifier (generated by Discord when not set).
165    #[serde(skip_serializing_if = "Option::is_none")]
166    pub id: Option<i32>,
167    /// The label text (max 45 characters).
168    pub label: CompactString,
169    /// An additional description (max 100 characters).
170    #[serde(skip_serializing_if = "Option::is_none")]
171    pub description: Option<CompactString>,
172    /// The labelled input.
173    pub component: Box<Component>,
174}
175
176impl Label {
177    /// Creates a label for `component`.
178    ///
179    /// discord.js counterpart: `LabelBuilder` (`@discordjs/builders`).
180    #[must_use]
181    pub fn new(label: impl Into<CompactString>, component: Component) -> Self {
182        Self {
183            id: None,
184            label: label.into(),
185            description: None,
186            component: Box::new(component),
187        }
188    }
189}