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
27// View this component preview using `workspace: open component-preview`
28impl ComponentPreview for DecoratedIcon {
29 fn preview(_window: &mut Window, cx: &mut App) -> AnyElement {
30 let decoration_x = IconDecoration::new(
31 IconDecorationKind::X,
32 cx.theme().colors().surface_background,
33 cx,
34 )
35 .color(cx.theme().status().error)
36 .position(Point {
37 x: px(-2.),
38 y: px(-2.),
39 });
40
41 let decoration_triangle = IconDecoration::new(
42 IconDecorationKind::Triangle,
43 cx.theme().colors().surface_background,
44 cx,
45 )
46 .color(cx.theme().status().error)
47 .position(Point {
48 x: px(-2.),
49 y: px(-2.),
50 });
51
52 let decoration_dot = IconDecoration::new(
53 IconDecorationKind::Dot,
54 cx.theme().colors().surface_background,
55 cx,
56 )
57 .color(cx.theme().status().error)
58 .position(Point {
59 x: px(-2.),
60 y: px(-2.),
61 });
62
63 v_flex()
64 .gap_6()
65 .children(vec![example_group_with_title(
66 "Decorations",
67 vec![
68 single_example(
69 "No Decoration",
70 DecoratedIcon::new(Icon::new(IconName::FileDoc), None).into_any_element(),
71 ),
72 single_example(
73 "X Decoration",
74 DecoratedIcon::new(Icon::new(IconName::FileDoc), Some(decoration_x))
75 .into_any_element(),
76 ),
77 single_example(
78 "Triangle Decoration",
79 DecoratedIcon::new(Icon::new(IconName::FileDoc), Some(decoration_triangle))
80 .into_any_element(),
81 ),
82 single_example(
83 "Dot Decoration",
84 DecoratedIcon::new(Icon::new(IconName::FileDoc), Some(decoration_dot))
85 .into_any_element(),
86 ),
87 ],
88 )])
89 .into_any_element()
90 }
91}