uniform_list.rs

 1#![cfg_attr(target_family = "wasm", no_main)]
 2
 3use gpui::{
 4    App, Bounds, Context, Window, WindowBounds, WindowOptions, div, prelude::*, px, rgb, size,
 5    uniform_list,
 6};
 7use gpui_platform::application;
 8
 9struct UniformListExample {}
10
11impl Render for UniformListExample {
12    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
13        div().size_full().bg(rgb(0xffffff)).child(
14            uniform_list(
15                "entries",
16                50,
17                cx.processor(|_this, range, _window, _cx| {
18                    let mut items = Vec::new();
19                    for ix in range {
20                        let item = ix + 1;
21
22                        items.push(
23                            div()
24                                .id(ix)
25                                .px_2()
26                                .cursor_pointer()
27                                .on_click(move |_event, _window, _cx| {
28                                    println!("clicked Item {item:?}");
29                                })
30                                .child(format!("Item {item}")),
31                        );
32                    }
33                    items
34                }),
35            )
36            .h_full(),
37        )
38    }
39}
40
41fn run_example() {
42    application().run(|cx: &mut App| {
43        let bounds = Bounds::centered(None, size(px(300.0), px(300.0)), cx);
44        cx.open_window(
45            WindowOptions {
46                window_bounds: Some(WindowBounds::Windowed(bounds)),
47                ..Default::default()
48            },
49            |_, cx| cx.new(|_| UniformListExample {}),
50        )
51        .unwrap();
52    });
53}
54
55#[cfg(not(target_family = "wasm"))]
56fn main() {
57    run_example();
58}
59
60#[cfg(target_family = "wasm")]
61#[wasm_bindgen::prelude::wasm_bindgen(start)]
62pub fn start() {
63    gpui_platform::web_init();
64    run_example();
65}