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                Label::new(format!("+\u{2009}{}", self.added))
34                    .color(Color::Success)
35                    .size(self.label_size),
36            )
37            .child(
38                Label::new(format!("\u{2012}\u{2009}{}", self.removed))
39                    .color(Color::Error)
40                    .size(self.label_size),
41            )
42    }
43}
44
45impl Component for DiffStat {
46    fn scope() -> ComponentScope {
47        ComponentScope::VersionControl
48    }
49
50    fn preview(_window: &mut Window, cx: &mut App) -> Option<AnyElement> {
51        let container = || {
52            h_flex()
53                .py_4()
54                .w_72()
55                .justify_center()
56                .border_1()
57                .border_color(cx.theme().colors().border_variant)
58                .bg(cx.theme().colors().panel_background)
59        };
60
61        let diff_stat_example = vec![single_example(
62            "Default",
63            container()
64                .child(DiffStat::new("id", 1, 2))
65                .into_any_element(),
66        )];
67
68        Some(
69            example_group(diff_stat_example)
70                .vertical()
71                .into_any_element(),
72        )
73    }
74}