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