paths_bench.rs

 1use gpui::{
 2    Background, Bounds, ColorSpace, Context, Path, PathBuilder, Pixels, Render, TitlebarOptions,
 3    Window, WindowBounds, WindowOptions, canvas, div, linear_color_stop, linear_gradient, point,
 4    prelude::*, px, rgb, size,
 5};
 6use gpui_platform::application;
 7
 8const DEFAULT_WINDOW_WIDTH: Pixels = px(1024.0);
 9const DEFAULT_WINDOW_HEIGHT: Pixels = px(768.0);
10
11struct PaintingViewer {
12    default_lines: Vec<(Path<Pixels>, Background)>,
13    _painting: bool,
14}
15
16impl PaintingViewer {
17    fn new(_window: &mut Window, _cx: &mut Context<Self>) -> Self {
18        let mut lines = vec![];
19
20        // draw a lightening bolt ⚡
21        for _ in 0..2000 {
22            // draw a ⭐
23            let mut builder = PathBuilder::fill();
24            builder.move_to(point(px(350.), px(100.)));
25            builder.line_to(point(px(370.), px(160.)));
26            builder.line_to(point(px(430.), px(160.)));
27            builder.line_to(point(px(380.), px(200.)));
28            builder.line_to(point(px(400.), px(260.)));
29            builder.line_to(point(px(350.), px(220.)));
30            builder.line_to(point(px(300.), px(260.)));
31            builder.line_to(point(px(320.), px(200.)));
32            builder.line_to(point(px(270.), px(160.)));
33            builder.line_to(point(px(330.), px(160.)));
34            builder.line_to(point(px(350.), px(100.)));
35            let path = builder.build().unwrap();
36            lines.push((
37                path,
38                linear_gradient(
39                    180.,
40                    linear_color_stop(rgb(0xFACC15), 0.7),
41                    linear_color_stop(rgb(0xD56D0C), 1.),
42                )
43                .color_space(ColorSpace::Oklab),
44            ));
45        }
46
47        Self {
48            default_lines: lines,
49            _painting: false,
50        }
51    }
52}
53
54impl Render for PaintingViewer {
55    fn render(&mut self, window: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
56        window.request_animation_frame();
57        let lines = self.default_lines.clone();
58        div().size_full().child(
59            canvas(
60                move |_, _, _| {},
61                move |_, _, window, _| {
62                    for (path, color) in lines {
63                        window.paint_path(path, color);
64                    }
65                },
66            )
67            .size_full(),
68        )
69    }
70}
71
72fn main() {
73    application().run(|cx| {
74        cx.open_window(
75            WindowOptions {
76                titlebar: Some(TitlebarOptions {
77                    title: Some("Vulkan".into()),
78                    ..Default::default()
79                }),
80                focus: true,
81                window_bounds: Some(WindowBounds::Windowed(Bounds::centered(
82                    None,
83                    size(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT),
84                    cx,
85                ))),
86                ..Default::default()
87            },
88            |window, cx| cx.new(|cx| PaintingViewer::new(window, cx)),
89        )
90        .unwrap();
91        cx.activate(true);
92    });
93}