winit 0.29.15

Cross-platform window creation library.
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
use std::{
    cell::{RefCell, RefMut},
    collections::VecDeque,
    fmt::{self, Debug},
    mem,
    rc::{Rc, Weak},
    sync::{
        atomic::{AtomicBool, Ordering},
        mpsc, Arc, Mutex, MutexGuard,
    },
    time::Instant,
};

use core_foundation::runloop::{CFRunLoopGetMain, CFRunLoopWakeUp};
use icrate::Foundation::{is_main_thread, NSSize};
use objc2::rc::{autoreleasepool, Id};
use once_cell::sync::Lazy;

use super::appkit::{NSApp, NSApplication, NSApplicationActivationPolicy, NSEvent};
use super::{
    event_loop::PanicInfo, menu, observer::EventLoopWaker, util::Never, window::WinitWindow,
};
use crate::{
    dpi::PhysicalSize,
    event::{Event, InnerSizeWriter, StartCause, WindowEvent},
    event_loop::{ControlFlow, EventLoopWindowTarget as RootWindowTarget},
    window::WindowId,
};

static HANDLER: Lazy<Handler> = Lazy::new(Default::default);

impl<Never> Event<Never> {
    fn userify<T: 'static>(self) -> Event<T> {
        self.map_nonuser_event()
            // `Never` can't be constructed, so the `UserEvent` variant can't
            // be present here.
            .unwrap_or_else(|_| unreachable!())
    }
}

pub trait EventHandler: Debug {
    // Not sure probably it should accept Event<'static, Never>
    fn handle_nonuser_event(&mut self, event: Event<Never>);
    fn handle_user_events(&mut self);
}

pub(crate) type Callback<T> = RefCell<dyn FnMut(Event<T>, &RootWindowTarget<T>)>;

struct EventLoopHandler<T: 'static> {
    callback: Weak<Callback<T>>,
    window_target: Rc<RootWindowTarget<T>>,
    receiver: Rc<mpsc::Receiver<T>>,
}

impl<T> EventLoopHandler<T> {
    fn with_callback<F>(&mut self, f: F)
    where
        F: FnOnce(&mut EventLoopHandler<T>, RefMut<'_, dyn FnMut(Event<T>, &RootWindowTarget<T>)>),
    {
        // The `NSApp` and our `HANDLER` are global state and so it's possible that
        // we could get a delegate callback after the application has exit an
        // `EventLoop`. If the loop has been exit then our weak `self.callback`
        // will fail to upgrade.
        //
        // We don't want to panic or output any verbose logging if we fail to
        // upgrade the weak reference since it might be valid that the application
        // re-starts the `NSApp` after exiting a Winit `EventLoop`
        if let Some(callback) = self.callback.upgrade() {
            let callback = callback.borrow_mut();
            (f)(self, callback);
        }
    }
}

impl<T> Debug for EventLoopHandler<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("EventLoopHandler")
            .field("window_target", &self.window_target)
            .finish()
    }
}

impl<T> EventHandler for EventLoopHandler<T> {
    fn handle_nonuser_event(&mut self, event: Event<Never>) {
        self.with_callback(|this, mut callback| {
            (callback)(event.userify(), &this.window_target);
        });
    }

    fn handle_user_events(&mut self) {
        self.with_callback(|this, mut callback| {
            for event in this.receiver.try_iter() {
                (callback)(Event::UserEvent(event), &this.window_target);
            }
        });
    }
}

#[derive(Debug)]
enum EventWrapper {
    StaticEvent(Event<Never>),
    ScaleFactorChanged {
        window: Id<WinitWindow>,
        suggested_size: PhysicalSize<u32>,
        scale_factor: f64,
    },
}

#[derive(Default)]
struct Handler {
    stop_app_on_launch: AtomicBool,
    stop_app_before_wait: AtomicBool,
    stop_app_after_wait: AtomicBool,
    stop_app_on_redraw: AtomicBool,
    launched: AtomicBool,
    running: AtomicBool,
    in_callback: AtomicBool,
    control_flow: Mutex<ControlFlow>,
    exit: AtomicBool,
    start_time: Mutex<Option<Instant>>,
    callback: Mutex<Option<Box<dyn EventHandler>>>,
    pending_events: Mutex<VecDeque<EventWrapper>>,
    pending_redraw: Mutex<Vec<WindowId>>,
    wait_timeout: Mutex<Option<Instant>>,
    waker: Mutex<EventLoopWaker>,
}

unsafe impl Send for Handler {}
unsafe impl Sync for Handler {}

impl Handler {
    fn events(&self) -> MutexGuard<'_, VecDeque<EventWrapper>> {
        self.pending_events.lock().unwrap()
    }

    fn redraw(&self) -> MutexGuard<'_, Vec<WindowId>> {
        self.pending_redraw.lock().unwrap()
    }

    fn waker(&self) -> MutexGuard<'_, EventLoopWaker> {
        self.waker.lock().unwrap()
    }

    /// `true` after `ApplicationDelegate::applicationDidFinishLaunching` called
    ///
    /// NB: This is global / `NSApp` state and since the app will only be launched
    /// once but an `EventLoop` may be run more than once then only the first
    /// `EventLoop` will observe the `NSApp` before it is launched.
    fn is_launched(&self) -> bool {
        self.launched.load(Ordering::Acquire)
    }

    /// Set via `ApplicationDelegate::applicationDidFinishLaunching`
    fn set_launched(&self) {
        self.launched.store(true, Ordering::Release);
    }

    /// `true` if an `EventLoop` is currently running
    ///
    /// NB: This is global / `NSApp` state and may persist beyond the lifetime of
    /// a running `EventLoop`.
    ///
    /// # Caveat
    /// This is only intended to be called from the main thread
    fn is_running(&self) -> bool {
        self.running.load(Ordering::Relaxed)
    }

    /// Set when an `EventLoop` starts running, after the `NSApp` is launched
    ///
    /// # Caveat
    /// This is only intended to be called from the main thread
    fn set_running(&self) {
        self.running.store(true, Ordering::Relaxed);
    }

    /// Clears the `running` state and resets the `control_flow` state when an `EventLoop` exits
    ///
    /// Since an `EventLoop` may be run more than once we need make sure to reset the
    /// `control_flow` state back to `Poll` each time the loop exits.
    ///
    /// Note: that if the `NSApp` has been launched then that state is preserved, and we won't
    /// need to re-launch the app if subsequent EventLoops are run.
    ///
    /// # Caveat
    /// This is only intended to be called from the main thread
    fn internal_exit(&self) {
        // Relaxed ordering because we don't actually have multiple threads involved, we just want
        // interiour mutability
        //
        // XXX: As an aside; having each individual bit of state for `Handler` be atomic or wrapped in a
        // `Mutex` for the sake of interior mutability seems a bit odd, and also a potential foot
        // gun in case the state is unwittingly accessed across threads because the fine-grained locking
        // wouldn't ensure that there's interior consistency.
        //
        // Maybe the whole thing should just be put in a static `Mutex<>` to make it clear
        // the we can mutate more than one peice of state while maintaining consistency. (though it
        // looks like there have been recuring re-entrancy issues with callback handling that might
        // make that awkward)
        self.running.store(false, Ordering::Relaxed);
        self.set_stop_app_on_redraw_requested(false);
        self.set_stop_app_before_wait(false);
        self.set_stop_app_after_wait(false);
        self.set_wait_timeout(None);
    }

    pub fn exit(&self) {
        self.exit.store(true, Ordering::Relaxed)
    }

    pub fn clear_exit(&self) {
        self.exit.store(false, Ordering::Relaxed)
    }

    pub fn exiting(&self) -> bool {
        self.exit.load(Ordering::Relaxed)
    }

    pub fn request_stop_app_on_launch(&self) {
        // Relaxed ordering because we don't actually have multiple threads involved, we just want
        // interior mutability
        self.stop_app_on_launch.store(true, Ordering::Relaxed);
    }

    pub fn should_stop_app_on_launch(&self) -> bool {
        // Relaxed ordering because we don't actually have multiple threads involved, we just want
        // interior mutability
        self.stop_app_on_launch.load(Ordering::Relaxed)
    }

    pub fn set_stop_app_before_wait(&self, stop_before_wait: bool) {
        // Relaxed ordering because we don't actually have multiple threads involved, we just want
        // interior mutability
        self.stop_app_before_wait
            .store(stop_before_wait, Ordering::Relaxed);
    }

    pub fn should_stop_app_before_wait(&self) -> bool {
        // Relaxed ordering because we don't actually have multiple threads involved, we just want
        // interior mutability
        self.stop_app_before_wait.load(Ordering::Relaxed)
    }

    pub fn set_stop_app_after_wait(&self, stop_after_wait: bool) {
        // Relaxed ordering because we don't actually have multiple threads involved, we just want
        // interior mutability
        self.stop_app_after_wait
            .store(stop_after_wait, Ordering::Relaxed);
    }

    pub fn set_wait_timeout(&self, new_timeout: Option<Instant>) {
        let mut timeout = self.wait_timeout.lock().unwrap();
        *timeout = new_timeout;
    }

    pub fn wait_timeout(&self) -> Option<Instant> {
        *self.wait_timeout.lock().unwrap()
    }

    pub fn should_stop_app_after_wait(&self) -> bool {
        // Relaxed ordering because we don't actually have multiple threads involved, we just want
        // interior mutability
        self.stop_app_after_wait.load(Ordering::Relaxed)
    }

    pub fn set_stop_app_on_redraw_requested(&self, stop_on_redraw: bool) {
        // Relaxed ordering because we don't actually have multiple threads involved, we just want
        // interior mutability
        self.stop_app_on_redraw
            .store(stop_on_redraw, Ordering::Relaxed);
    }

    pub fn should_stop_app_on_redraw_requested(&self) -> bool {
        // Relaxed ordering because we don't actually have multiple threads involved, we just want
        // interior mutability
        self.stop_app_on_redraw.load(Ordering::Relaxed)
    }

    fn set_control_flow(&self, new_control_flow: ControlFlow) {
        *self.control_flow.lock().unwrap() = new_control_flow
    }

    fn control_flow(&self) -> ControlFlow {
        *self.control_flow.lock().unwrap()
    }

    fn get_start_time(&self) -> Option<Instant> {
        *self.start_time.lock().unwrap()
    }

    fn update_start_time(&self) {
        *self.start_time.lock().unwrap() = Some(Instant::now());
    }

    fn take_events(&self) -> VecDeque<EventWrapper> {
        mem::take(&mut *self.events())
    }

    fn should_redraw(&self) -> Vec<WindowId> {
        mem::take(&mut *self.redraw())
    }

    fn get_in_callback(&self) -> bool {
        self.in_callback.load(Ordering::Acquire)
    }

    fn set_in_callback(&self, in_callback: bool) {
        self.in_callback.store(in_callback, Ordering::Release);
    }

    fn have_callback(&self) -> bool {
        self.callback.lock().unwrap().is_some()
    }

    fn handle_nonuser_event(&self, event: Event<Never>) {
        if let Some(ref mut callback) = *self.callback.lock().unwrap() {
            callback.handle_nonuser_event(event)
        }
    }

    fn handle_user_events(&self) {
        if let Some(ref mut callback) = *self.callback.lock().unwrap() {
            callback.handle_user_events();
        }
    }

    fn handle_scale_factor_changed_event(
        &self,
        window: &WinitWindow,
        suggested_size: PhysicalSize<u32>,
        scale_factor: f64,
    ) {
        if let Some(ref mut callback) = *self.callback.lock().unwrap() {
            let new_inner_size = Arc::new(Mutex::new(suggested_size));
            let scale_factor_changed_event = Event::WindowEvent {
                window_id: WindowId(window.id()),
                event: WindowEvent::ScaleFactorChanged {
                    scale_factor,
                    inner_size_writer: InnerSizeWriter::new(Arc::downgrade(&new_inner_size)),
                },
            };

            callback.handle_nonuser_event(scale_factor_changed_event);

            let physical_size = *new_inner_size.lock().unwrap();
            drop(new_inner_size);
            let logical_size = physical_size.to_logical(scale_factor);
            let size = NSSize::new(logical_size.width, logical_size.height);
            window.setContentSize(size);

            let resized_event = Event::WindowEvent {
                window_id: WindowId(window.id()),
                event: WindowEvent::Resized(physical_size),
            };
            callback.handle_nonuser_event(resized_event);
        }
    }
}

pub(crate) enum AppState {}

impl AppState {
    /// Associate the application's event callback with the (global static) Handler state
    ///
    /// # Safety
    /// This is ignoring the lifetime of the application callback (which may not be 'static)
    /// and can lead to undefined behaviour if the callback is not cleared before the end of
    /// its real lifetime.
    ///
    /// All public APIs that take an event callback (`run`, `run_on_demand`,
    /// `pump_events`) _must_ pair a call to `set_callback` with
    /// a call to `clear_callback` before returning to avoid undefined behaviour.
    pub unsafe fn set_callback<T>(
        callback: Weak<Callback<T>>,
        window_target: Rc<RootWindowTarget<T>>,
        receiver: Rc<mpsc::Receiver<T>>,
    ) {
        *HANDLER.callback.lock().unwrap() = Some(Box::new(EventLoopHandler {
            callback,
            window_target,
            receiver,
        }));
    }

    pub fn clear_callback() {
        HANDLER.callback.lock().unwrap().take();
    }

    pub fn is_launched() -> bool {
        HANDLER.is_launched()
    }

    pub fn is_running() -> bool {
        HANDLER.is_running()
    }

    // If `pump_events` is called to progress the event loop then we bootstrap the event
    // loop via `[NSApp run]` but will use `CFRunLoopRunInMode` for subsequent calls to
    // `pump_events`
    pub fn request_stop_on_launch() {
        HANDLER.request_stop_app_on_launch();
    }

    pub fn set_stop_app_before_wait(stop_before_wait: bool) {
        HANDLER.set_stop_app_before_wait(stop_before_wait);
    }

    pub fn set_stop_app_after_wait(stop_after_wait: bool) {
        HANDLER.set_stop_app_after_wait(stop_after_wait);
    }

    pub fn set_wait_timeout(timeout: Option<Instant>) {
        HANDLER.set_wait_timeout(timeout);
    }

    pub fn set_stop_app_on_redraw_requested(stop_on_redraw: bool) {
        HANDLER.set_stop_app_on_redraw_requested(stop_on_redraw);
    }

    pub fn set_control_flow(control_flow: ControlFlow) {
        HANDLER.set_control_flow(control_flow)
    }

    pub fn control_flow() -> ControlFlow {
        HANDLER.control_flow()
    }

    pub fn internal_exit() {
        HANDLER.set_in_callback(true);
        HANDLER.handle_nonuser_event(Event::LoopExiting);
        HANDLER.set_in_callback(false);
        HANDLER.internal_exit();
        Self::clear_callback();
    }

    pub fn exit() {
        HANDLER.exit()
    }

    pub fn clear_exit() {
        HANDLER.clear_exit()
    }

    pub fn exiting() -> bool {
        HANDLER.exiting()
    }

    pub fn dispatch_init_events() {
        HANDLER.set_in_callback(true);
        HANDLER.handle_nonuser_event(Event::NewEvents(StartCause::Init));
        // NB: For consistency all platforms must emit a 'resumed' event even though macOS
        // applications don't themselves have a formal suspend/resume lifecycle.
        HANDLER.handle_nonuser_event(Event::Resumed);
        HANDLER.set_in_callback(false);
    }

    pub fn start_running() {
        debug_assert!(HANDLER.is_launched());

        HANDLER.set_running();
        Self::dispatch_init_events()
    }

    pub fn launched(
        activation_policy: NSApplicationActivationPolicy,
        create_default_menu: bool,
        activate_ignoring_other_apps: bool,
    ) {
        let app = NSApp();
        // We need to delay setting the activation policy and activating the app
        // until `applicationDidFinishLaunching` has been called. Otherwise the
        // menu bar is initially unresponsive on macOS 10.15.
        app.setActivationPolicy(activation_policy);

        window_activation_hack(&app);
        app.activateIgnoringOtherApps(activate_ignoring_other_apps);

        HANDLER.set_launched();
        HANDLER.waker().start();
        if create_default_menu {
            // The menubar initialization should be before the `NewEvents` event, to allow
            // overriding of the default menu even if it's created
            menu::initialize();
        }

        Self::start_running();

        // If the `NSApp` is being launched via `EventLoop::pump_events()` then we'll
        // want to stop the app once it is launched (and return to the external loop)
        //
        // In this case we still want to consider Winit's `EventLoop` to be "running",
        // so we call `start_running()` above.
        if HANDLER.should_stop_app_on_launch() {
            // Note: the original idea had been to only stop the underlying `RunLoop`
            // for the app but that didn't work as expected (`[NSApp run]` effectively
            // ignored the attempt to stop the RunLoop and re-started it.). So we
            // return from `pump_events` by stopping the `NSApp`
            Self::stop();
        }
    }

    // Called by RunLoopObserver after finishing waiting for new events
    pub fn wakeup(panic_info: Weak<PanicInfo>) {
        let panic_info = panic_info
            .upgrade()
            .expect("The panic info must exist here. This failure indicates a developer error.");

        // Return when in callback due to https://github.com/rust-windowing/winit/issues/1779
        if panic_info.is_panicking()
            || HANDLER.get_in_callback()
            || !HANDLER.have_callback()
            || !HANDLER.is_running()
        {
            return;
        }

        if HANDLER.should_stop_app_after_wait() {
            Self::stop();
        }

        let start = HANDLER.get_start_time().unwrap();
        let cause = match HANDLER.control_flow() {
            ControlFlow::Poll => StartCause::Poll,
            ControlFlow::Wait => StartCause::WaitCancelled {
                start,
                requested_resume: None,
            },
            ControlFlow::WaitUntil(requested_resume) => {
                if Instant::now() >= requested_resume {
                    StartCause::ResumeTimeReached {
                        start,
                        requested_resume,
                    }
                } else {
                    StartCause::WaitCancelled {
                        start,
                        requested_resume: Some(requested_resume),
                    }
                }
            }
        };
        HANDLER.set_in_callback(true);
        HANDLER.handle_nonuser_event(Event::NewEvents(cause));
        HANDLER.set_in_callback(false);
    }

    // This is called from multiple threads at present
    pub fn queue_redraw(window_id: WindowId) {
        let mut pending_redraw = HANDLER.redraw();
        if !pending_redraw.contains(&window_id) {
            pending_redraw.push(window_id);
        }
        unsafe {
            let rl = CFRunLoopGetMain();
            CFRunLoopWakeUp(rl);
        }
    }

    pub fn handle_redraw(window_id: WindowId) {
        // Redraw request might come out of order from the OS.
        // -> Don't go back into the callback when our callstack originates from there
        if !HANDLER.in_callback.swap(true, Ordering::AcqRel) {
            HANDLER.handle_nonuser_event(Event::WindowEvent {
                window_id,
                event: WindowEvent::RedrawRequested,
            });
            HANDLER.set_in_callback(false);

            // `pump_events` will request to stop immediately _after_ dispatching RedrawRequested events
            // as a way to ensure that `pump_events` can't block an external loop indefinitely
            if HANDLER.should_stop_app_on_redraw_requested() {
                AppState::stop();
            }
        }
    }

    pub fn queue_event(event: Event<Never>) {
        if !is_main_thread() {
            panic!("Event queued from different thread: {event:#?}");
        }
        HANDLER.events().push_back(EventWrapper::StaticEvent(event));
    }

    pub fn queue_static_scale_factor_changed_event(
        window: Id<WinitWindow>,
        suggested_size: PhysicalSize<u32>,
        scale_factor: f64,
    ) {
        HANDLER
            .events()
            .push_back(EventWrapper::ScaleFactorChanged {
                window,
                suggested_size,
                scale_factor,
            });
    }

    pub fn stop() {
        let app = NSApp();
        autoreleasepool(|_| {
            app.stop(None);
            // To stop event loop immediately, we need to post some event here.
            app.postEvent_atStart(&NSEvent::dummy(), true);
        });
    }

    // Called by RunLoopObserver before waiting for new events
    pub fn cleared(panic_info: Weak<PanicInfo>) {
        let panic_info = panic_info
            .upgrade()
            .expect("The panic info must exist here. This failure indicates a developer error.");

        // Return when in callback due to https://github.com/rust-windowing/winit/issues/1779
        // XXX: how does it make sense that `get_in_callback()` can ever return `true` here if we're
        // about to return to the `CFRunLoop` to poll for new events?
        if panic_info.is_panicking()
            || HANDLER.get_in_callback()
            || !HANDLER.have_callback()
            || !HANDLER.is_running()
        {
            return;
        }

        HANDLER.set_in_callback(true);
        HANDLER.handle_user_events();
        for event in HANDLER.take_events() {
            match event {
                EventWrapper::StaticEvent(event) => {
                    HANDLER.handle_nonuser_event(event);
                }
                EventWrapper::ScaleFactorChanged {
                    window,
                    suggested_size,
                    scale_factor,
                } => {
                    HANDLER.handle_scale_factor_changed_event(
                        &window,
                        suggested_size,
                        scale_factor,
                    );
                }
            }
        }

        for window_id in HANDLER.should_redraw() {
            HANDLER.handle_nonuser_event(Event::WindowEvent {
                window_id,
                event: WindowEvent::RedrawRequested,
            });
        }

        HANDLER.handle_nonuser_event(Event::AboutToWait);
        HANDLER.set_in_callback(false);

        if HANDLER.exiting() {
            Self::stop();
        }

        if HANDLER.should_stop_app_before_wait() {
            Self::stop();
        }
        HANDLER.update_start_time();
        let wait_timeout = HANDLER.wait_timeout(); // configured by pump_events
        let app_timeout = match HANDLER.control_flow() {
            ControlFlow::Wait => None,
            ControlFlow::Poll => Some(Instant::now()),
            ControlFlow::WaitUntil(instant) => Some(instant),
        };
        HANDLER
            .waker()
            .start_at(min_timeout(wait_timeout, app_timeout));
    }
}

/// Returns the minimum `Option<Instant>`, taking into account that `None`
/// equates to an infinite timeout, not a zero timeout (so can't just use
/// `Option::min`)
fn min_timeout(a: Option<Instant>, b: Option<Instant>) -> Option<Instant> {
    a.map_or(b, |a_timeout| {
        b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout)))
    })
}

/// A hack to make activation of multiple windows work when creating them before
/// `applicationDidFinishLaunching:` / `Event::Event::NewEvents(StartCause::Init)`.
///
/// Alternative to this would be the user calling `window.set_visible(true)` in
/// `StartCause::Init`.
///
/// If this becomes too bothersome to maintain, it can probably be removed
/// without too much damage.
fn window_activation_hack(app: &NSApplication) {
    // TODO: Proper ordering of the windows
    app.windows().into_iter().for_each(|window| {
        // Call `makeKeyAndOrderFront` if it was called on the window in `WinitWindow::new`
        // This way we preserve the user's desired initial visiblity status
        // TODO: Also filter on the type/"level" of the window, and maybe other things?
        if window.isVisible() {
            trace!("Activating visible window");
            window.makeKeyAndOrderFront(None);
        } else {
            trace!("Skipping activating invisible window");
        }
    })
}