indent_guides.rs

 1use std::ops::Range;
 2
 3use gpui::{Entity, Render, div, uniform_list};
 4use gpui::{prelude::*, *};
 5use ui::{AbsoluteLength, Color, DefiniteLength, Label, LabelCommon, px, v_flex};
 6
 7use story::Story;
 8
 9const LENGTH: usize = 100;
10
11pub struct IndentGuidesStory {
12    depths: Vec<usize>,
13}
14
15impl IndentGuidesStory {
16    pub fn model(_window: &mut Window, cx: &mut App) -> Entity<Self> {
17        let mut depths = Vec::new();
18        depths.push(0);
19        depths.push(1);
20        depths.push(2);
21        for _ in 0..LENGTH - 6 {
22            depths.push(3);
23        }
24        depths.push(2);
25        depths.push(1);
26        depths.push(0);
27
28        cx.new(|_cx| Self { depths })
29    }
30}
31
32impl Render for IndentGuidesStory {
33    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
34        Story::container(cx)
35            .child(Story::title("Indent guides", cx))
36            .child(
37                v_flex().size_full().child(
38                    uniform_list(
39                        "some-list",
40                        self.depths.len(),
41                        cx.processor(move |this, range: Range<usize>, _window, _cx| {
42                            this.depths
43                                .iter()
44                                .enumerate()
45                                .skip(range.start)
46                                .take(range.end - range.start)
47                                .map(|(i, depth)| {
48                                    div()
49                                        .pl(DefiniteLength::Absolute(AbsoluteLength::Pixels(px(
50                                            16. * (*depth as f32),
51                                        ))))
52                                        .child(Label::new(format!("Item {}", i)).color(Color::Info))
53                                })
54                                .collect()
55                        }),
56                    )
57                    .with_sizing_behavior(gpui::ListSizingBehavior::Infer)
58                    .with_decoration(
59                        ui::indent_guides(
60                            px(16.),
61                            ui::IndentGuideColors {
62                                default: Color::Info.color(cx),
63                                hover: Color::Accent.color(cx),
64                                active: Color::Accent.color(cx),
65                            },
66                        )
67                        .with_compute_indents_fn(
68                            cx.entity(),
69                            |this, range, _cx, _context| {
70                                this.depths
71                                    .iter()
72                                    .skip(range.start)
73                                    .take(range.end - range.start)
74                                    .cloned()
75                                    .collect()
76                            },
77                        ),
78                    ),
79                ),
80            )
81    }
82}