text_button.rs

 1use crate::prelude::{ButtonVariant, InteractionState};
 2use crate::theme::theme;
 3use gpui2::style::{StyleHelpers, Styleable};
 4use gpui2::{elements::div, IntoElement};
 5use gpui2::{Element, ParentElement, ViewContext};
 6
 7#[derive(Element)]
 8pub struct TextButton {
 9    label: &'static str,
10    variant: ButtonVariant,
11    state: InteractionState,
12}
13
14pub fn text_button(label: &'static str) -> TextButton {
15    TextButton {
16        label,
17        variant: ButtonVariant::default(),
18        state: InteractionState::default(),
19    }
20}
21
22impl TextButton {
23    pub fn variant(mut self, variant: ButtonVariant) -> Self {
24        self.variant = variant;
25        self
26    }
27
28    pub fn state(mut self, state: InteractionState) -> Self {
29        self.state = state;
30        self
31    }
32
33    fn render<V: 'static>(&mut self, _: &mut V, cx: &mut ViewContext<V>) -> impl IntoElement<V> {
34        let theme = theme(cx);
35
36        let text_color_default;
37        let text_color_hover;
38        let text_color_active;
39
40        let background_color_default;
41        let background_color_hover;
42        let background_color_active;
43
44        let div = div();
45
46        match self.variant {
47            ButtonVariant::Ghost => {
48                text_color_default = theme.lowest.base.default.foreground;
49                text_color_hover = theme.lowest.base.hovered.foreground;
50                text_color_active = theme.lowest.base.pressed.foreground;
51                background_color_default = theme.lowest.base.default.background;
52                background_color_hover = theme.lowest.base.hovered.background;
53                background_color_active = theme.lowest.base.pressed.background;
54            }
55            ButtonVariant::Filled => {
56                text_color_default = theme.lowest.base.default.foreground;
57                text_color_hover = theme.lowest.base.hovered.foreground;
58                text_color_active = theme.lowest.base.pressed.foreground;
59                background_color_default = theme.lowest.on.default.background;
60                background_color_hover = theme.lowest.on.hovered.background;
61                background_color_active = theme.lowest.on.pressed.background;
62            }
63        };
64        div.h_6()
65            .px_1()
66            .flex()
67            .items_center()
68            .justify_center()
69            .rounded_md()
70            .text_xs()
71            .text_color(text_color_default)
72            .fill(background_color_default)
73            .hover()
74            .text_color(text_color_hover)
75            .fill(background_color_hover)
76            .active()
77            .text_color(text_color_active)
78            .fill(background_color_active)
79            .child(self.label.clone())
80    }
81}