1use gpui::{AnyView, DefiniteLength};
2
3use crate::prelude::*;
4use crate::{ButtonCommon, ButtonLike, ButtonSize, ButtonStyle, Label, LineHeightStyle};
5
6#[derive(IntoElement)]
7pub struct Button {
8 base: ButtonLike,
9 label: SharedString,
10 label_color: Option<Color>,
11}
12
13impl Button {
14 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
15 Self {
16 base: ButtonLike::new(id),
17 label: label.into(),
18 label_color: None,
19 }
20 }
21
22 pub fn color(mut self, label_color: impl Into<Option<Color>>) -> Self {
23 self.label_color = label_color.into();
24 self
25 }
26}
27
28impl Selectable for Button {
29 fn selected(mut self, selected: bool) -> Self {
30 self.base = self.base.selected(selected);
31 self
32 }
33}
34
35impl Disableable for Button {
36 fn disabled(mut self, disabled: bool) -> Self {
37 self.base = self.base.disabled(disabled);
38 self
39 }
40}
41
42impl Clickable for Button {
43 fn on_click(
44 mut self,
45 handler: impl Fn(&gpui::ClickEvent, &mut WindowContext) + 'static,
46 ) -> Self {
47 self.base = self.base.on_click(handler);
48 self
49 }
50}
51
52impl FixedWidth for Button {
53 fn width(mut self, width: DefiniteLength) -> Self {
54 self.base = self.base.width(width);
55 self
56 }
57
58 fn full_width(mut self) -> Self {
59 self.base = self.base.full_width();
60 self
61 }
62}
63
64impl ButtonCommon for Button {
65 fn id(&self) -> &ElementId {
66 self.base.id()
67 }
68
69 fn style(mut self, style: ButtonStyle) -> Self {
70 self.base = self.base.style(style);
71 self
72 }
73
74 fn size(mut self, size: ButtonSize) -> Self {
75 self.base = self.base.size(size);
76 self
77 }
78
79 fn tooltip(mut self, tooltip: impl Fn(&mut WindowContext) -> AnyView + 'static) -> Self {
80 self.base = self.base.tooltip(tooltip);
81 self
82 }
83}
84
85impl RenderOnce for Button {
86 type Rendered = ButtonLike;
87
88 fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
89 let label_color = if self.base.disabled {
90 Color::Disabled
91 } else if self.base.selected {
92 Color::Selected
93 } else {
94 self.label_color.unwrap_or_default()
95 };
96
97 self.base.child(
98 Label::new(self.label)
99 .color(label_color)
100 .line_height_style(LineHeightStyle::UILabel),
101 )
102 }
103}