diff_stat.rs

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