decorated_icon.rs

 1use gpui::{AnyElement, IntoElement, Point};
 2
 3use crate::{prelude::*, IconDecoration, IconDecorationKind};
 4
 5#[derive(IntoElement, IntoComponent)]
 6pub struct DecoratedIcon {
 7    icon: Icon,
 8    decoration: Option<IconDecoration>,
 9}
10
11impl DecoratedIcon {
12    pub fn new(icon: Icon, decoration: Option<IconDecoration>) -> Self {
13        Self { icon, decoration }
14    }
15}
16
17impl RenderOnce for DecoratedIcon {
18    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
19        div()
20            .relative()
21            .size(self.icon.size)
22            .child(self.icon)
23            .children(self.decoration)
24    }
25}
26
27impl ComponentPreview for DecoratedIcon {
28    fn preview(_window: &mut Window, cx: &App) -> AnyElement {
29        let decoration_x = IconDecoration::new(
30            IconDecorationKind::X,
31            cx.theme().colors().surface_background,
32            cx,
33        )
34        .color(cx.theme().status().error)
35        .position(Point {
36            x: px(-2.),
37            y: px(-2.),
38        });
39
40        let decoration_triangle = IconDecoration::new(
41            IconDecorationKind::Triangle,
42            cx.theme().colors().surface_background,
43            cx,
44        )
45        .color(cx.theme().status().error)
46        .position(Point {
47            x: px(-2.),
48            y: px(-2.),
49        });
50
51        let decoration_dot = IconDecoration::new(
52            IconDecorationKind::Dot,
53            cx.theme().colors().surface_background,
54            cx,
55        )
56        .color(cx.theme().status().error)
57        .position(Point {
58            x: px(-2.),
59            y: px(-2.),
60        });
61
62        v_flex()
63            .gap_6()
64            .children(vec![example_group_with_title(
65                "Decorations",
66                vec![
67                    single_example(
68                        "No Decoration",
69                        DecoratedIcon::new(Icon::new(IconName::FileDoc), None).into_any_element(),
70                    ),
71                    single_example(
72                        "X Decoration",
73                        DecoratedIcon::new(Icon::new(IconName::FileDoc), Some(decoration_x))
74                            .into_any_element(),
75                    ),
76                    single_example(
77                        "Triangle Decoration",
78                        DecoratedIcon::new(Icon::new(IconName::FileDoc), Some(decoration_triangle))
79                            .into_any_element(),
80                    ),
81                    single_example(
82                        "Dot Decoration",
83                        DecoratedIcon::new(Icon::new(IconName::FileDoc), Some(decoration_dot))
84                            .into_any_element(),
85                    ),
86                ],
87            )])
88            .into_any_element()
89    }
90}