Skip to main content

goud_engine/core/events/
app.rs

1//! Application lifecycle events.
2//!
3//! Contains events emitted during the engine's startup and shutdown phases.
4
5/// Emitted when the application/engine has fully initialized.
6///
7/// This event fires once after all core systems are ready but before the
8/// first frame begins. Use this for one-time initialization that depends
9/// on the engine being fully set up.
10///
11/// # Example
12///
13/// ```rust
14/// use goud_engine::core::events::AppStarted;
15/// use goud_engine::core::event::Events;
16///
17/// fn on_app_start(events: &Events<AppStarted>) {
18///     let mut reader = events.reader();
19///     for _ in reader.read() {
20///         println!("Application started - performing one-time setup");
21///         // Load initial assets, connect to services, etc.
22///     }
23/// }
24/// ```
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
26pub struct AppStarted;
27
28/// Emitted when the application is about to exit.
29///
30/// This event fires before shutdown begins, giving systems a chance to
31/// perform cleanup, save state, or release resources gracefully.
32///
33/// # Exit Reasons
34///
35/// The `reason` field indicates why the application is exiting:
36/// - `User`: User requested exit (close button, quit command)
37/// - `Error`: Unrecoverable error occurred
38/// - `Programmatic`: Code explicitly requested shutdown
39///
40/// # Example
41///
42/// ```rust
43/// use goud_engine::core::events::{AppExiting, ExitReason};
44/// use goud_engine::core::event::Events;
45///
46/// fn on_app_exit(events: &Events<AppExiting>) {
47///     let mut reader = events.reader();
48///     for event in reader.read() {
49///         match event.reason {
50///             ExitReason::User => println!("User requested exit"),
51///             ExitReason::Error => println!("Exiting due to error"),
52///             ExitReason::Programmatic => println!("Programmatic shutdown"),
53///         }
54///         // Save game state, cleanup resources, etc.
55///     }
56/// }
57/// ```
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
59pub struct AppExiting {
60    /// The reason the application is exiting.
61    pub reason: ExitReason,
62}
63
64impl AppExiting {
65    /// Creates an `AppExiting` event with the specified reason.
66    #[must_use]
67    pub fn new(reason: ExitReason) -> Self {
68        Self { reason }
69    }
70
71    /// Creates an `AppExiting` event for user-initiated exit.
72    #[must_use]
73    pub fn user() -> Self {
74        Self::new(ExitReason::User)
75    }
76
77    /// Creates an `AppExiting` event for error-initiated exit.
78    #[must_use]
79    pub fn error() -> Self {
80        Self::new(ExitReason::Error)
81    }
82
83    /// Creates an `AppExiting` event for programmatic exit.
84    #[must_use]
85    pub fn programmatic() -> Self {
86        Self::new(ExitReason::Programmatic)
87    }
88}
89
90impl Default for AppExiting {
91    fn default() -> Self {
92        Self::new(ExitReason::User)
93    }
94}
95
96/// Reason for application exit.
97///
98/// Used with [`AppExiting`] to indicate why the application is shutting down.
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
100pub enum ExitReason {
101    /// User requested exit (close button, Alt+F4, quit menu, etc.).
102    #[default]
103    User,
104    /// An unrecoverable error occurred.
105    Error,
106    /// Code explicitly requested shutdown.
107    Programmatic,
108}