kludgine_app/
application.rs

1use std::marker::PhantomData;
2
3use crate::runtime::Runtime;
4use crate::window::{RuntimeWindow, Window, WindowCreator};
5
6/// A trait that describes the application's behavior.
7pub trait Application: Sized + Send + Sync {
8    /// Executed upon application launch.
9    fn initialize(&mut self) {}
10
11    /// Return true if the app should exit. Default implementation returns true
12    /// once [`Application::open_window_count()`] returns zero.
13    fn should_exit(&mut self) -> bool {
14        Self::open_window_count() == 0
15    }
16
17    /// Returns the number of open windows.
18    #[must_use]
19    fn open_window_count() -> usize {
20        RuntimeWindow::count()
21    }
22}
23
24/// An [`Application`] implementation that begins with a single window.
25///
26/// If feature `multiwindow` is enabled, multiple windows can still be opened.
27/// This structure just provides a way to run an app without explicitly
28/// implementing [`Application`] on one of your types.
29pub struct SingleWindowApplication<T> {
30    phantom: PhantomData<T>,
31}
32
33impl<T> Application for SingleWindowApplication<T> where T: Window + WindowCreator + 'static {}
34
35impl<T> SingleWindowApplication<T>
36where
37    T: Window + WindowCreator + 'static,
38{
39    /// Runs the app. Does not return.
40    pub fn run(window: T) -> ! {
41        let app = Self {
42            phantom: PhantomData::default(),
43        };
44        Runtime::new(app).run(window.get_window_builder(), window)
45    }
46}