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