Skip to main content

oino_ui/extras/
empty.rs

1//! `Empty` node (§4.5.2).
2
3use serde::{Deserialize, Serialize};
4
5use crate::extras::button::Button;
6use crate::extras::icon::Icon;
7use crate::node::{Common, IntoNode, Node};
8
9/// An empty state (§4.5.2): dim centred title / message with an optional action
10/// button.
11#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
12#[serde(rename_all = "camelCase")]
13pub struct Empty {
14    /// Common node props (`key`, `style`, `block`, `fallback`).
15    #[serde(flatten)]
16    pub common: Common,
17    /// Title.
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    pub title: Option<String>,
20    /// Message under the title.
21    #[serde(default, skip_serializing_if = "Option::is_none")]
22    pub message: Option<String>,
23    /// Leading icon.
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub icon: Option<Icon>,
26    /// Action button — a `Button` node (addressed by the `"action"` path
27    /// segment, §4.6).
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub action: Option<Box<Node>>,
30}
31
32impl Empty {
33    /// An empty state.
34    #[must_use]
35    pub fn new() -> Self {
36        Self {
37            common: Common::new(),
38            title: None,
39            message: None,
40            icon: None,
41            action: None,
42        }
43    }
44
45    /// Sets the title.
46    #[must_use]
47    pub fn title(mut self, title: impl Into<String>) -> Self {
48        self.title = Some(title.into());
49        self
50    }
51
52    /// Sets the message.
53    #[must_use]
54    pub fn message(mut self, message: impl Into<String>) -> Self {
55        self.message = Some(message.into());
56        self
57    }
58
59    /// Sets the icon.
60    #[must_use]
61    pub fn icon(mut self, icon: Icon) -> Self {
62        self.icon = Some(icon);
63        self
64    }
65
66    /// Sets the action button (stored as a tagged `Button` node).
67    #[must_use]
68    pub fn action(mut self, action: Button) -> Self {
69        self.action = Some(Box::new(Node::Button(action)));
70        self
71    }
72}
73
74impl Default for Empty {
75    fn default() -> Self {
76        Self::new()
77    }
78}
79
80impl IntoNode for Empty {}
81impl From<Empty> for Node {
82    fn from(e: Empty) -> Self {
83        Node::Empty(e)
84    }
85}