From 94b6d06a7214d7e568ceaa8b3d9a88cab2025c7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E5=AE=87=E8=88=AA?= Date: Fri, 6 Sep 2024 02:24:10 +0800 Subject: [PATCH] gpui: Add example for `uniform_list` (#17421) GPUI: Add example for `uniform_list` - Added example for `uniform_list` - Run `cargo run --example uniform_list` to fire up the example - https://github.com/user-attachments/assets/bb554fbc-c097-4ce5-8077-782dc4c5c398 Release Notes: - N/A https://github.com/user-attachments/assets/bb554fbc-c097-4ce5-8077-782dc4c5c398 --------- Co-authored-by: Marshall Bowers --- crates/gpui/Cargo.toml | 4 +++ crates/gpui/examples/uniform_list.rs | 43 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 crates/gpui/examples/uniform_list.rs diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index 3ce9b509f0..4ce44ffce0 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -192,3 +192,7 @@ path = "examples/text_wrapper.rs" [[example]] name = "opacity" path = "examples/opacity.rs" + +[[example]] +name = "uniform_list" +path = "examples/uniform_list.rs" diff --git a/crates/gpui/examples/uniform_list.rs b/crates/gpui/examples/uniform_list.rs new file mode 100644 index 0000000000..2994c0d8fc --- /dev/null +++ b/crates/gpui/examples/uniform_list.rs @@ -0,0 +1,43 @@ +use gpui::*; + +struct UniformListExample {} + +impl Render for UniformListExample { + fn render(&mut self, cx: &mut ViewContext) -> impl IntoElement { + div().size_full().bg(rgb(0xffffff)).child( + uniform_list(cx.view().clone(), "entries", 50, |_this, range, _cx| { + let mut items = Vec::new(); + for ix in range { + let item = ix + 1; + + items.push( + div() + .id(ix) + .px_2() + .cursor_pointer() + .on_click(move |_event, _cx| { + println!("clicked Item {item:?}"); + }) + .child(format!("Item {item}")), + ); + } + items + }) + .h_full(), + ) + } +} + +fn main() { + App::new().run(|cx: &mut AppContext| { + let bounds = Bounds::centered(None, size(px(300.0), px(300.0)), cx); + cx.open_window( + WindowOptions { + window_bounds: Some(WindowBounds::Windowed(bounds)), + ..Default::default() + }, + |cx| cx.new_view(|_cx| UniformListExample {}), + ) + .unwrap(); + }); +}