tree.rs

 1#![cfg_attr(target_family = "wasm", no_main)]
 2//! Renders a div with deep children hierarchy. This example is useful to exemplify that Zed can
 3//! handle deep hierarchies (even though it cannot just yet!).
 4use std::sync::LazyLock;
 5
 6use gpui::{App, Bounds, Context, Window, WindowBounds, WindowOptions, div, prelude::*, px, size};
 7use gpui_platform::application;
 8
 9struct Tree {}
10
11static DEPTH: LazyLock<u64> = LazyLock::new(|| {
12    std::env::var("GPUI_TREE_DEPTH")
13        .ok()
14        .and_then(|depth| depth.parse().ok())
15        .unwrap_or_else(|| 50)
16});
17
18impl Render for Tree {
19    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
20        let mut depth = *DEPTH;
21        static COLORS: [gpui::Hsla; 4] = [gpui::red(), gpui::blue(), gpui::green(), gpui::yellow()];
22        let mut colors = COLORS.iter().cycle().copied();
23        let mut next_div = || div().p_0p5().bg(colors.next().unwrap());
24        let mut innermost_node = next_div();
25        while depth > 0 {
26            innermost_node = next_div().child(innermost_node);
27            depth -= 1;
28        }
29        innermost_node
30    }
31}
32
33fn run_example() {
34    application().run(|cx: &mut App| {
35        let bounds = Bounds::centered(None, size(px(300.0), px(300.0)), cx);
36        cx.open_window(
37            WindowOptions {
38                window_bounds: Some(WindowBounds::Windowed(bounds)),
39                ..Default::default()
40            },
41            |_, cx| cx.new(|_| Tree {}),
42        )
43        .unwrap();
44    });
45}
46
47#[cfg(not(target_family = "wasm"))]
48fn main() {
49    run_example();
50}
51
52#[cfg(target_family = "wasm")]
53#[wasm_bindgen::prelude::wasm_bindgen(start)]
54pub fn start() {
55    gpui_platform::web_init();
56    run_example();
57}