icon_button.rs

 1use gpui2::elements::{div, svg};
 2use gpui2::style::{StyleHelpers, Styleable};
 3use gpui2::{Element, IntoElement, ParentElement, ViewContext};
 4
 5use crate::prelude::*;
 6use crate::theme;
 7
 8#[derive(Element)]
 9pub struct IconButton {
10    path: &'static str,
11    variant: ButtonVariant,
12    state: InteractionState,
13}
14
15pub fn icon_button(path: &'static str) -> IconButton {
16    IconButton {
17        path,
18        variant: ButtonVariant::default(),
19        state: InteractionState::default(),
20    }
21}
22
23impl IconButton {
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 icon_color;
38
39        if self.state == InteractionState::Disabled {
40            icon_color = theme.highest.base.disabled.foreground;
41        } else {
42            icon_color = theme.highest.base.default.foreground;
43        }
44
45        let mut div = div();
46        if self.variant == ButtonVariant::Filled {
47            div = div.fill(theme.highest.on.default.background);
48        }
49
50        div.w_7()
51            .h_6()
52            .flex()
53            .items_center()
54            .justify_center()
55            .rounded_md()
56            .hover()
57            .fill(theme.highest.base.hovered.background)
58            .active()
59            .fill(theme.highest.base.pressed.background)
60            .child(svg().path(self.path).w_4().h_4().fill(icon_color))
61    }
62}