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};
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 {
pub fn event_pub(&self) -> &EventPub<PlatformEvent> {
&self.event_pub
}
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 {
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,
}
}
}