Skip to main content

violet/
app.rs

1use flax::World;
2use winit::{
3    event::{Event, WindowEvent},
4    event_loop::EventLoopBuilder,
5    window::WindowBuilder,
6};
7
8use crate::{executor::Executor, Frame, Widget};
9
10pub struct App {}
11
12impl App {
13    pub fn new() -> Self {
14        Self {}
15    }
16
17    pub fn run(self, root: impl Widget) -> anyhow::Result<()> {
18        let mut ex = Executor::new();
19
20        let spawner = ex.spawner();
21
22        let mut frame = Frame {
23            world: World::new(),
24            spawner,
25        };
26
27        let event_loop = EventLoopBuilder::new().build();
28
29        let window = WindowBuilder::new().build(&event_loop)?;
30
31        // Mount the root widget
32        root.mount(&mut frame);
33
34        event_loop.run(move |event, _, ctl| {
35            let _window = &window;
36
37            match event {
38                Event::MainEventsCleared => {
39                    ex.tick(&mut frame);
40                }
41                Event::WindowEvent { window_id, event } => match event {
42                    WindowEvent::CloseRequested => {
43                        *ctl = winit::event_loop::ControlFlow::Exit;
44                    }
45                    event => {
46                        tracing::debug!(?event, ?window_id, "Window event")
47                    }
48                },
49                event => {
50                    tracing::debug!(?event, "Event")
51                }
52            }
53        })
54    }
55}
56
57impl Default for App {
58    fn default() -> Self {
59        Self::new()
60    }
61}