A simple CPU rendered GUI IDE experience.
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/// Common boilerplate for setting up a winit application.
use std::marker::PhantomData;
use std::sync::Arc as Rc;

use winit::application::ApplicationHandler;
use winit::event::{DeviceEvent, DeviceId, WindowEvent};
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::window::{Window, WindowAttributes, WindowId};

/// Run a Winit application.
#[allow(unused_mut)]
pub(crate) fn run_app(
    event_loop: EventLoop,
    mut app: impl ApplicationHandler + 'static,
) {
    #[cfg(not(target_family = "wasm"))]
    event_loop.run_app(app).unwrap();

    #[cfg(target_family = "wasm")]
    winit::platform::web::EventLoopExtWebSys::spawn_app(event_loop, app);
}

/// Create a window from a set of window attributes.
#[allow(dead_code)]
pub(crate) fn make_window(
    elwt: &dyn ActiveEventLoop,
    f: impl FnOnce(WindowAttributes) -> WindowAttributes,
) -> Rc<dyn Window> {
    let attributes = f(WindowAttributes::default());
    #[cfg(target_family = "wasm")]
    let attributes =
        winit::platform::web::WindowAttributesExtWebSys::with_append(
            attributes, true,
        );

    let window = elwt.create_window(attributes);
    window.unwrap().into()
}

/// Easily constructable winit application.
pub(crate) struct WinitApp<
    T,
    S,
    Init,
    InitSurface,
    Handler,
    DeviceEventHandler,
    AboutToWaitHandler,
> {
    /// Closure to initialize `state`.
    init: Init,

    /// Closure to initialize `surface_state`.
    init_surface: InitSurface,

    /// Closure to run on window events.
    event: Handler,

    /// Closure to run on device events.
    device_event: DeviceEventHandler,

    /// Closure to run on about_to_wait events.
    about_to_wait: AboutToWaitHandler,

    /// Contained state.
    state: Option<T>,

    /// Contained surface state.
    surface_state: Option<S>,
}

/// Builder that makes it so we don't have to name `T`.
pub(crate) struct WinitAppBuilder<T, S, Init, InitSurface> {
    /// Closure to initialize `state`.
    init: Init,

    /// Closure to initialize `surface_state`.
    init_surface: InitSurface,

    /// Eat the type parameter.
    _marker: PhantomData<(Option<T>, Option<S>)>,
}

impl<T, S, Init, InitSurface> WinitAppBuilder<T, S, Init, InitSurface>
where
    Init: FnMut(&dyn ActiveEventLoop) -> T,
    InitSurface: FnMut(&dyn ActiveEventLoop, &mut T) -> S,
{
    /// Create with an "init" closure.
    pub(crate) fn with_init(
        init: Init,
        init_surface: InitSurface,
    ) -> Self {
        Self { init, init_surface, _marker: PhantomData }
    }

    /// Build a new application.
    #[allow(clippy::type_complexity)]
    pub(crate) fn with_event_handler<F>(
        self,
        handler: F,
    ) -> WinitApp<
        T,
        S,
        Init,
        InitSurface,
        F,
        impl FnMut(&mut T, Option<&mut S>, DeviceEvent, &dyn ActiveEventLoop),
        impl FnMut(&mut T, Option<&mut S>, &dyn ActiveEventLoop),
    >
    where
        F: FnMut(
            &mut T,
            Option<&mut S>,
            WindowId,
            WindowEvent,
            &dyn ActiveEventLoop,
        ),
    {
        WinitApp::new(
            self.init,
            self.init_surface,
            handler,
            |_, _, _, _| {},
            |_, _, _| {},
        )
    }
}

impl<
    T,
    S,
    Init,
    InitSurface,
    Handler,
    DeviceEventHandler,
    AboutToWaitHandler,
>
    WinitApp<
        T,
        S,
        Init,
        InitSurface,
        Handler,
        DeviceEventHandler,
        AboutToWaitHandler,
    >
where
    Init: FnMut(&dyn ActiveEventLoop) -> T,
    InitSurface: FnMut(&dyn ActiveEventLoop, &mut T) -> S,
    Handler: FnMut(
        &mut T,
        Option<&mut S>,
        WindowId,
        WindowEvent,
        &dyn ActiveEventLoop,
    ),
    DeviceEventHandler:
        FnMut(&mut T, Option<&mut S>, DeviceEvent, &dyn ActiveEventLoop),
    AboutToWaitHandler:
        FnMut(&mut T, Option<&mut S>, &dyn ActiveEventLoop),
{
    /// Create a new application.
    pub(crate) fn new(
        init: Init,
        init_surface: InitSurface,
        event: Handler,
        device_event: DeviceEventHandler,
        about_to_wait: AboutToWaitHandler,
    ) -> Self {
        Self {
            init,
            init_surface,
            event,
            device_event,
            about_to_wait,
            state: None,
            surface_state: None,
        }
    }

    /// Build a new application.
    #[allow(dead_code)]
    pub(crate) fn with_about_to_wait_handler<F>(
        self,
        about_to_wait: F,
    ) -> WinitApp<T, S, Init, InitSurface, Handler, DeviceEventHandler, F>
    where
        F: FnMut(&mut T, Option<&mut S>, &dyn ActiveEventLoop),
    {
        WinitApp::new(
            self.init,
            self.init_surface,
            self.event,
            self.device_event,
            about_to_wait,
        )
    }

    /// Build a new application.
    #[allow(dead_code)]
    pub(crate) fn with_device_event_handler<F>(
        self,
        device_event: F,
    ) -> WinitApp<T, S, Init, InitSurface, Handler, F, AboutToWaitHandler>
    where
        F: FnMut(
            &mut T,
            Option<&mut S>,
            DeviceEvent,
            &dyn ActiveEventLoop,
        ),
    {
        WinitApp::new(
            self.init,
            self.init_surface,
            self.event,
            device_event,
            self.about_to_wait,
        )
    }
}

impl<
    T,
    S,
    Init,
    InitSurface,
    Handler,
    DeviceEventHandler,
    AboutToWaitHandler,
> ApplicationHandler
    for WinitApp<
        T,
        S,
        Init,
        InitSurface,
        Handler,
        DeviceEventHandler,
        AboutToWaitHandler,
    >
where
    Init: FnMut(&dyn ActiveEventLoop) -> T,
    InitSurface: FnMut(&dyn ActiveEventLoop, &mut T) -> S,
    Handler: FnMut(
        &mut T,
        Option<&mut S>,
        WindowId,
        WindowEvent,
        &dyn ActiveEventLoop,
    ),
    DeviceEventHandler:
        FnMut(&mut T, Option<&mut S>, DeviceEvent, &dyn ActiveEventLoop),
    AboutToWaitHandler:
        FnMut(&mut T, Option<&mut S>, &dyn ActiveEventLoop),
{
    fn resumed(&mut self, el: &dyn ActiveEventLoop) {
        debug_assert!(self.state.is_none());
        let mut state = (self.init)(el);
        self.surface_state = Some((self.init_surface)(el, &mut state));
        self.state = Some(state);
    }

    fn suspended(&mut self, _event_loop: &dyn ActiveEventLoop) {
        let surface_state = self.surface_state.take();
        debug_assert!(surface_state.is_some());
        drop(surface_state);
    }

    fn window_event(
        &mut self,
        event_loop: &dyn ActiveEventLoop,
        window_id: WindowId,
        event: WindowEvent,
    ) {
        let state = self.state.as_mut().unwrap();
        let surface_state = self.surface_state.as_mut();
        (self.event)(state, surface_state, window_id, event, event_loop);
    }

    fn about_to_wait(&mut self, event_loop: &dyn ActiveEventLoop) {
        if let Some(state) = self.state.as_mut() {
            let surface_state = self.surface_state.as_mut();
            (self.about_to_wait)(state, surface_state, event_loop);
        }
    }

    fn device_event(
        &mut self,
        event_loop: &dyn ActiveEventLoop,
        _device_id: Option<DeviceId>,
        event: DeviceEvent,
    ) {
        let state = self.state.as_mut().unwrap();
        let surface_state = self.surface_state.as_mut();
        (self.device_event)(state, surface_state, event, event_loop);
    }

    fn can_create_surfaces(&mut self, el: &dyn ActiveEventLoop) {
        let mut state = (self.init)(el);
        self.surface_state = Some((self.init_surface)(el, &mut state));
        self.state = Some(state);
    }
}