gpui_material_components/button/
action_button.rs1use std::rc::Rc;
2
3use gpui::{App, ElementId, IntoElement, RenderOnce, SharedString, Window};
4
5use crate::action::{Action, InvokeAction};
6
7use super::{
8 appearance::button_appearance,
9 appearance_builders, common_builders, slot_builders,
10 slots::{Slots, loading_overlay},
11 types::{ButtonVariant, Common},
12};
13
14#[derive(IntoElement)]
17pub struct Button {
18 common: Common,
19 pub(super) variant: ButtonVariant,
20 slots: Slots,
21 on_activate: Option<InvokeAction>,
22 scale: f32,
23}
24
25impl Button {
26 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
28 Self {
29 common: Common::new(id, label),
30 variant: ButtonVariant::default(),
31 slots: Slots::default(),
32 on_activate: None,
33 scale: 1.,
34 }
35 }
36
37 pub fn filled(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
39 Self::new(id, label).variant(ButtonVariant::Filled)
40 }
41
42 pub fn tonal(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
44 Self::new(id, label).variant(ButtonVariant::FilledTonal)
45 }
46
47 pub fn elevated(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
49 Self::new(id, label).variant(ButtonVariant::Elevated)
50 }
51
52 pub fn outlined(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
54 Self::new(id, label).variant(ButtonVariant::Outlined)
55 }
56
57 pub fn text(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
59 Self::new(id, label).variant(ButtonVariant::Text)
60 }
61
62 common_builders!();
63 appearance_builders!();
64 slot_builders!();
65
66 pub fn variant(mut self, variant: ButtonVariant) -> Self {
68 self.variant = variant;
69 self
70 }
71
72 pub fn loading(mut self, loading: bool) -> Self {
75 self.common.loading = loading;
76 self
77 }
78
79 #[cfg(feature = "feedback")]
80 pub(crate) fn scale(mut self, scale: f32) -> Self {
81 self.scale = scale;
82 self
83 }
84
85 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
89 self.on_activate = Some(InvokeAction::Full(Rc::new(handler)));
90 self
91 }
92
93 pub fn on_accessible_activate(
101 mut self,
102 handler: impl Fn(&mut Window, &mut App) + 'static,
103 ) -> Self {
104 self.on_activate = Some(InvokeAction::AccessibleOnly(Rc::new(handler)));
105 self
106 }
107}
108
109impl RenderOnce for Button {
110 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
111 let metrics = self.common.size.metrics();
112 let appearance = button_appearance(self.variant, None, self.common.tone);
113 let style = appearance.style(
114 metrics,
115 self.common.shape.corner(metrics.square, false),
116 58.,
117 );
118 let tint = self
119 .common
120 .appearance
121 .colors
122 .content(self.common.disabled, false)
123 .unwrap_or_else(|| style.foreground(self.common.disabled, window, cx));
124 let resting_content = self.slots.render(
125 self.common.label.clone(),
126 metrics,
127 self.scale,
128 window,
129 tint,
130 cx,
131 );
132 let content = if self.common.loading {
133 loading_overlay(
134 &self.common.id,
135 resting_content,
136 metrics.icon * self.scale,
137 tint,
138 window,
139 cx,
140 )
141 } else {
142 resting_content
143 };
144 self.common.render_scaled(
145 content,
146 style,
147 Action::Invoke(self.on_activate),
148 self.scale,
149 window,
150 cx,
151 )
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 #[test]
160 fn button_activation_modes_replace_each_other() {
161 let button = Button::new("b", "Open").on_accessible_activate(|_, _| {});
162 assert!(matches!(
163 button.on_activate,
164 Some(InvokeAction::AccessibleOnly(_))
165 ));
166 let button = button.on_activate(|_, _| {});
167 assert!(matches!(button.on_activate, Some(InvokeAction::Full(_))));
168 let button = button.on_accessible_activate(|_, _| {});
169 assert!(matches!(
170 button.on_activate,
171 Some(InvokeAction::AccessibleOnly(_))
172 ));
173 }
174}