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