rich_sdl2_rust/event/
window.rs

1//! Events for the window.
2
3use crate::{
4    bind,
5    geo::{Point, Size},
6    EnumInt,
7};
8
9/// The details what occurred in [`WindowEvent`].
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum WindowEventDetails {
13    /// The window became to be shown.
14    Shown,
15    /// The window became to be hidden.
16    Hidden,
17    /// The window was exposed.
18    Exposed,
19    /// The windows was moved.
20    Moved(Point),
21    /// The window was resized by manually.
22    Resized(Size),
23    /// The window was resized by programmatically.
24    SizeChanged(Size),
25    /// The window was minimized.
26    Minimized,
27    /// The window was maximized.
28    Maximized,
29    /// The window was restored from max/min.
30    Restored,
31    /// The cursor was entered in the window.
32    Enter,
33    /// The cursor was left from the window.
34    Leave,
35    /// The window was focused.
36    FocusGained,
37    /// The window lost focused.
38    FocusLost,
39    /// The window was closed.
40    Close,
41    /// The window given focus. When this event was occurred, you should call [`crate::video::window::Window::set_input_focus`] on the window.
42    TakeFocus,
43}
44
45/// An event on interacting to the window.
46#[derive(Debug, Clone)]
47pub struct WindowEvent {
48    /// When this event occurred.
49    pub timestamp: u32,
50    /// The id of the window focused.
51    pub window_id: u32,
52    /// The kind of the event.
53    pub details: WindowEventDetails,
54}
55
56impl From<bind::SDL_WindowEvent> for WindowEvent {
57    fn from(
58        bind::SDL_WindowEvent {
59            timestamp,
60            windowID: window_id,
61            event,
62            data1,
63            data2,
64            ..
65        }: bind::SDL_WindowEvent,
66    ) -> Self {
67        Self {
68            timestamp,
69            window_id,
70            details: match event as EnumInt {
71                bind::SDL_WINDOWEVENT_SHOWN => WindowEventDetails::Shown,
72                bind::SDL_WINDOWEVENT_HIDDEN => WindowEventDetails::Hidden,
73                bind::SDL_WINDOWEVENT_EXPOSED => WindowEventDetails::Exposed,
74                bind::SDL_WINDOWEVENT_MOVED => {
75                    WindowEventDetails::Moved(Point { x: data1, y: data2 })
76                }
77                bind::SDL_WINDOWEVENT_RESIZED => WindowEventDetails::Resized(Size {
78                    width: data1 as u32,
79                    height: data2 as u32,
80                }),
81                bind::SDL_WINDOWEVENT_SIZE_CHANGED => WindowEventDetails::SizeChanged(Size {
82                    width: data1 as u32,
83                    height: data2 as u32,
84                }),
85                bind::SDL_WINDOWEVENT_MINIMIZED => WindowEventDetails::Minimized,
86                bind::SDL_WINDOWEVENT_MAXIMIZED => WindowEventDetails::Maximized,
87                bind::SDL_WINDOWEVENT_RESTORED => WindowEventDetails::Restored,
88                bind::SDL_WINDOWEVENT_ENTER => WindowEventDetails::Enter,
89                bind::SDL_WINDOWEVENT_LEAVE => WindowEventDetails::Leave,
90                bind::SDL_WINDOWEVENT_FOCUS_GAINED => WindowEventDetails::FocusGained,
91                bind::SDL_WINDOWEVENT_FOCUS_LOST => WindowEventDetails::FocusLost,
92                bind::SDL_WINDOWEVENT_CLOSE => WindowEventDetails::Close,
93                bind::SDL_WINDOWEVENT_TAKE_FOCUS => WindowEventDetails::TakeFocus,
94                other => todo!("{other} is not implemented"),
95            },
96        }
97    }
98}