text_button.rs

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