1use serde::{Deserialize, Serialize};
4
5use crate::extras::icon::Icon;
6use crate::node::{ActionId, Common, IntoNode, Node};
7use crate::text::Line;
8use serde_json::Value as Json;
9
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub enum ButtonVariant {
13 Primary,
15 #[default]
17 Secondary,
18 Ghost,
20 Danger,
22}
23
24#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
27#[serde(rename_all = "camelCase")]
28pub struct Button {
29 #[serde(flatten)]
31 pub common: Common,
32 pub label: Line,
34 pub action: ActionId,
36 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub value: Option<Json>,
39 #[serde(default, skip_serializing_if = "Option::is_none")]
41 pub variant: Option<ButtonVariant>,
42 #[serde(default, skip_serializing_if = "Option::is_none")]
44 pub icon: Option<Icon>,
45 #[serde(default, skip_serializing_if = "Option::is_none")]
47 pub disabled: Option<bool>,
48 #[serde(default, skip_serializing_if = "Option::is_none")]
50 pub confirm: Option<String>,
51}
52
53impl Button {
54 #[must_use]
56 pub fn new(label: impl Into<Line>, action: impl Into<ActionId>) -> Self {
57 Self {
58 common: Common::new(),
59 label: label.into(),
60 action: action.into(),
61 value: None,
62 variant: None,
63 icon: None,
64 disabled: None,
65 confirm: None,
66 }
67 }
68
69 #[must_use]
71 pub fn value(mut self, value: impl Into<Json>) -> Self {
72 self.value = Some(value.into());
73 self
74 }
75
76 #[must_use]
78 pub fn variant(mut self, variant: ButtonVariant) -> Self {
79 self.variant = Some(variant);
80 self
81 }
82
83 #[must_use]
85 pub fn icon(mut self, icon: Icon) -> Self {
86 self.icon = Some(icon);
87 self
88 }
89
90 #[must_use]
92 pub fn disabled(mut self, disabled: bool) -> Self {
93 self.disabled = Some(disabled);
94 self
95 }
96
97 #[must_use]
99 pub fn confirm(mut self, confirm: impl Into<String>) -> Self {
100 self.confirm = Some(confirm.into());
101 self
102 }
103}
104
105impl IntoNode for Button {}
106impl From<Button> for Node {
107 fn from(b: Button) -> Self {
108 Node::Button(b)
109 }
110}