1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::{common::*, event::InternalEvent, *};

use riddle_common::eventpub::EventPub;

use std::{cell::RefCell, sync::Mutex};

/// The winit platform system core state, along with [`PlatformMainThreadState`].
///
/// Mostly used to lookup [`Window`] by [`WindowId`], and subscribe to [`PlatformEvent`]s.
pub struct PlatformSystem {
    weak_self: PlatformSystemWeak,
    pub(crate) event_proxy: Mutex<winit::event_loop::EventLoopProxy<InternalEvent>>,

    window_map: Mutex<WindowMap>,

    event_pub: EventPub<PlatformEvent>,
}

define_handles!(<PlatformSystem>::weak_self, pub PlatformSystemHandle, pub PlatformSystemWeak);

impl PlatformSystem {
    /// Get the [`PlatformEvent`] publisher, so that other systems can consume events.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use riddle::{*, common::eventpub::*, platform::*};
    /// # fn main() -> Result<(), RiddleError> {
    /// let rdl =  RiddleLib::new()?;
    /// let subscriber: EventSub<PlatformEvent> = EventSub::new();
    ///
    /// // Attach subscriber to the platform event stream
    /// rdl.state().platform().event_pub().attach(&subscriber);
    /// # Ok(()) }
    /// ```
    pub fn event_pub(&self) -> &EventPub<PlatformEvent> {
        &self.event_pub
    }

    /// Get a [`WindowHandle`] associated with a [`WindowId`], if one exists.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use riddle::{*, common::eventpub::*, platform::*};
    /// # fn main() -> Result<(), RiddleError> {
    /// let rdl =  RiddleLib::new()?;
    /// let window = WindowBuilder::new().build(rdl.context())?;
    /// let window_id = window.id();
    /// assert!(WindowHandle::eq(&window,
    ///        &rdl.state().platform().lookup_window(window_id).unwrap()));
    /// # Ok(()) }
    /// ```
    pub fn lookup_window(&self, window_id: WindowId) -> Option<WindowHandle> {
        self.window_map.lock().unwrap().lookup_window(window_id)
    }

    #[inline]
    pub(crate) fn with_window_map<R, F: FnOnce(&WindowMap) -> R>(&self, f: F) -> R {
        f(&self.window_map.lock().unwrap())
    }

    #[inline]
    pub(crate) fn with_window_map_mut<R, F: FnOnce(&mut WindowMap) -> R>(&self, f: F) -> R {
        f(&mut self.window_map.lock().unwrap())
    }

    fn update_windows(&self) {
        let windows = self.window_map.lock().unwrap().windows();
        for window in windows {
            window.update()
        }
    }
}

impl ext::PlatformSystemExt for PlatformSystem {
    fn new_shared() -> (PlatformSystemHandle, PlatformMainThreadState) {
        let event_loop = winit::event_loop::EventLoop::with_user_event();
        let event_proxy = event_loop.create_proxy();
        let system = PlatformSystemHandle::new(|weak_self| PlatformSystem {
            weak_self,
            event_proxy: Mutex::new(event_proxy),

            window_map: WindowMap::new().into(),

            event_pub: EventPub::new(),
        });
        let main_thread_state = PlatformMainThreadState {
            system: system.clone(),
            event_loop: RefCell::new(Some(event_loop)),
        };
        (system, main_thread_state)
    }
}

pub struct PlatformMainThreadState {
    pub(crate) system: PlatformSystemHandle,
    pub(crate) event_loop: RefCell<Option<winit::event_loop::EventLoop<InternalEvent>>>,
}

impl PlatformMainThreadState {
    /// Starts the main even loop for this window system.
    ///
    /// # Panics
    ///
    /// If run has already been invoked, then this function will panic.
    pub fn run<Err: std::fmt::Debug, F>(self, main_loop: F) -> !
    where
        F: FnMut(PlatformContext) -> std::result::Result<(), Err> + 'static,
    {
        let el = std::mem::replace(&mut *self.event_loop.borrow_mut(), None).unwrap();
        let mut main_loop = main_loop;
        let this = self.system.clone_handle();
        el.run(move |event, el, cf| {
            match &event {
                winit::event::Event::UserEvent(InternalEvent::QuitRequested) => {
                    *cf = winit::event_loop::ControlFlow::Exit
                }
                _ => *cf = winit::event_loop::ControlFlow::Poll,
            }

            if let Some(system_event) = event::convert_winit_event(&this, event) {
                let ctx = PlatformContext {
                    main_thread_state: &self,
                    event_loop: Some(el),
                    triggering_event: system_event.clone(),
                };

                this.event_pub.dispatch(system_event);
                this.update_windows();

                main_loop(ctx).unwrap();
            }
        })
    }

    pub fn borrow_context(&self) -> PlatformContext {
        PlatformContext {
            main_thread_state: &self,
            event_loop: None,
            triggering_event: PlatformEvent::Unknown,
        }
    }
}