diff_stat.rs

 1use crate::prelude::*;
 2
 3#[derive(IntoElement, RegisterComponent)]
 4pub struct DiffStat {
 5    id: ElementId,
 6    added: usize,
 7    removed: usize,
 8}
 9
10impl DiffStat {
11    pub fn new(id: impl Into<ElementId>, added: usize, removed: usize) -> Self {
12        Self {
13            id: id.into(),
14            added,
15            removed,
16        }
17    }
18}
19
20impl RenderOnce for DiffStat {
21    fn render(self, _: &mut Window, _cx: &mut App) -> impl IntoElement {
22        h_flex()
23            .id(self.id)
24            .gap_1()
25            .child(
26                h_flex()
27                    .gap_0p5()
28                    .child(
29                        Icon::new(IconName::Plus)
30                            .size(IconSize::XSmall)
31                            .color(Color::Success),
32                    )
33                    .child(
34                        Label::new(self.added.to_string())
35                            .color(Color::Success)
36                            .size(LabelSize::Small),
37                    ),
38            )
39            .child(
40                h_flex()
41                    .gap_0p5()
42                    .child(
43                        Icon::new(IconName::Dash)
44                            .size(IconSize::XSmall)
45                            .color(Color::Error),
46                    )
47                    .child(
48                        Label::new(self.removed.to_string())
49                            .color(Color::Error)
50                            .size(LabelSize::Small),
51                    ),
52            )
53    }
54}
55
56impl Component for DiffStat {
57    fn scope() -> ComponentScope {
58        ComponentScope::VersionControl
59    }
60
61    fn preview(_window: &mut Window, cx: &mut App) -> Option<AnyElement> {
62        let container = || {
63            h_flex()
64                .py_4()
65                .w_72()
66                .justify_center()
67                .border_1()
68                .border_color(cx.theme().colors().border_variant)
69                .bg(cx.theme().colors().panel_background)
70        };
71
72        let diff_stat_example = vec![single_example(
73            "Default",
74            container()
75                .child(DiffStat::new("id", 1, 2))
76                .into_any_element(),
77        )];
78
79        Some(
80            example_group(diff_stat_example)
81                .vertical()
82                .into_any_element(),
83        )
84    }
85}