1
use std::marker::PhantomData;
2

            
3
use crate::runtime::Runtime;
4
use crate::window::{RuntimeWindow, Window, WindowCreator};
5

            
6
/// A trait that describes the application's behavior.
7
pub 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.
29
pub struct SingleWindowApplication<T> {
30
    phantom: PhantomData<T>,
31
}
32

            
33
impl<T> Application for SingleWindowApplication<T> where T: Window + WindowCreator + 'static {}
34

            
35
impl<T> SingleWindowApplication<T>
36
where
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
}