ailloli_ui_winit/
host.rs

1//! Provider-neutral application driver hosted by the winit event loop.
2//!
3//! The driver owns application state and command scheduling. Only this module
4//! sees winit callbacks; high-level façade crates communicate through
5//! [`HostDriver`] and [`HostOutcome`].
6
7use std::sync::Arc;
8use std::time::Instant;
9
10use ailloli_ui_runtime::app::{RuntimeHandle, RuntimeInbox, UiWake, UiWakeError};
11use winit::application::ApplicationHandler;
12use winit::event::WindowEvent;
13use winit::event_loop::{ActiveEventLoop, ControlFlow};
14use winit::window::WindowId;
15
16use crate::event_loop::{new_event_loop_allow_any_thread, run_app_on_event_loop};
17use crate::{UiApp, UiAppError};
18
19#[cfg(feature = "test_support")]
20/// Event-loop-thread callback injected by native integration tests.
21type TestService<A> = Box<dyn FnMut(&mut UiApp<A>)>;
22
23/// Work requested by a provider-neutral application driver after a host callback.
24///
25/// The default outcome keeps running, requests no redraw, and supplies no wake
26/// deadline. When both `exit` and redraw/deadline fields are set, exit wins.
27///
28/// # Examples
29///
30/// ```
31/// use ailloli_ui_winit::HostOutcome;
32/// let idle = HostOutcome::default();
33/// assert!(!idle.exit && !idle.redraw_all && idle.next_wake.is_none());
34/// ```
35#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
36pub struct HostOutcome {
37    /// Exit the native event loop.
38    pub exit: bool,
39    /// Request a redraw for every attached presentation.
40    pub redraw_all: bool,
41    /// Earliest application deadline that must wake an otherwise sleeping loop.
42    pub next_wake: Option<Instant>,
43}
44
45/// Convenience constructors for the two immediate host effects.
46impl HostOutcome {
47    /// Requests event-loop termination.
48    ///
49    /// # Examples
50    ///
51    /// ```
52    /// let outcome = ailloli_ui_winit::HostOutcome::exit();
53    /// assert!(outcome.exit);
54    /// assert!(!outcome.redraw_all);
55    /// ```
56    pub const fn exit() -> Self {
57        Self {
58            exit: true,
59            redraw_all: false,
60            next_wake: None,
61        }
62    }
63
64    /// Requests a redraw of every presentation.
65    ///
66    /// # Examples
67    ///
68    /// ```
69    /// let outcome = ailloli_ui_winit::HostOutcome::redraw_all();
70    /// assert!(outcome.redraw_all);
71    /// assert!(!outcome.exit);
72    /// ```
73    pub const fn redraw_all() -> Self {
74        Self {
75            exit: false,
76            redraw_all: true,
77            next_wake: None,
78        }
79    }
80}
81
82/// Provider-neutral application logic serviced after native host callbacks.
83///
84/// Implementations run on the event-loop thread after UI sources, runtime
85/// inbox messages, capture requests, and optional devtools commands are serviced.
86///
87/// # Examples
88///
89/// ```
90/// use std::time::Instant;
91/// use ailloli_ui_runtime::app::RuntimeHandle;
92/// use ailloli_ui_winit::{HostDriver, HostOutcome};
93/// struct Driver;
94/// impl HostDriver<()> for Driver {
95///     fn service(&mut self, _runtime: &RuntimeHandle<()>, _now: Instant) -> HostOutcome {
96///         HostOutcome::default()
97///     }
98/// }
99/// ```
100pub trait HostDriver<A>: 'static {
101    /// Drains application work and returns host-level effects.
102    ///
103    /// `now` is sampled immediately before the call. A `next_wake` deadline is
104    /// passed to the UI host, while redraw and exit effects are applied immediately.
105    ///
106    /// # Examples
107    ///
108    /// ```
109    /// use std::time::Instant;
110    /// use ailloli_ui_runtime::app::RuntimeHandle;
111    /// use ailloli_ui_winit::{HostDriver, HostOutcome};
112    /// struct ExitDriver;
113    /// impl HostDriver<()> for ExitDriver {
114    ///     fn service(&mut self, _: &RuntimeHandle<()>, _: Instant) -> HostOutcome {
115    ///         HostOutcome::exit()
116    ///     }
117    /// }
118    /// ```
119    fn service(&mut self, runtime: &RuntimeHandle<A>, now: Instant) -> HostOutcome;
120}
121
122/// Driver used by low-level integrations that route actions themselves.
123///
124/// # Examples
125///
126/// ```
127/// let _: ailloli_ui_winit::NoopHostDriver = Default::default();
128/// ```
129#[derive(Debug, Default, Clone, Copy)]
130pub struct NoopHostDriver;
131
132/// Always returns the idle [`HostOutcome`].
133impl<A> HostDriver<A> for NoopHostDriver {
134    /// Performs no application work and requests no host effect.
135    fn service(&mut self, _runtime: &RuntimeHandle<A>, _now: Instant) -> HostOutcome {
136        HostOutcome::default()
137    }
138}
139
140/// The sole `ApplicationHandler` used by the high-level Ailloli UI application path.
141///
142/// A host owns one retained [`UiApp`], one provider-neutral driver, and at most
143/// one bounded runtime inbox. All native, capture, inbox, and devtools wake
144/// sources share the event-loop proxy installed by [`run_winit_host`].
145///
146/// # Examples
147///
148/// ```
149/// use ailloli_ui_winit::{NoopHostDriver, UiApp, WinitHost};
150/// let host = WinitHost::new(UiApp::<()>::new(), NoopHostDriver);
151/// assert!(host.ui().window_snapshots().is_empty());
152/// ```
153pub struct WinitHost<A, D> {
154    /// Retained UI state and native presentations.
155    ui: UiApp<A>,
156    /// Provider-neutral application service.
157    driver: D,
158    /// Optional bounded cross-thread mailbox.
159    runtime_inbox: Option<RuntimeInbox<A>>,
160    /// First mailbox wake/drain failure, consumed by its accessor.
161    inbox_wake_error: Option<UiWakeError>,
162    /// First host-level capture wake failure, consumed by its accessor.
163    capture_wake_error: Option<UiWakeError>,
164    #[cfg(feature = "devtools")]
165    /// First remote-devtools wake failure, consumed by its accessor.
166    devtools_wake_error: Option<UiWakeError>,
167    #[cfg(feature = "test_support")]
168    /// Optional deterministic native-test callback.
169    test_service: Option<TestService<A>>,
170}
171
172/// Host construction, attachment, state access, and diagnostic drains.
173impl<A: 'static, D> WinitHost<A, D> {
174    /// Wraps a retained UI application and a provider-neutral driver.
175    ///
176    /// No inbox, wake proxy, test service, or stored wake error is installed.
177    ///
178    /// # Examples
179    ///
180    /// ```
181    /// use ailloli_ui_winit::{NoopHostDriver, UiApp, WinitHost};
182    /// let mut host: WinitHost<(), NoopHostDriver> = WinitHost::new(UiApp::new(), NoopHostDriver);
183    /// assert!(host.take_error().is_none());
184    /// ```
185    pub fn new(ui: UiApp<A>, driver: D) -> Self {
186        Self {
187            ui,
188            driver,
189            runtime_inbox: None,
190            inbox_wake_error: None,
191            capture_wake_error: None,
192            #[cfg(feature = "devtools")]
193            devtools_wake_error: None,
194            #[cfg(feature = "test_support")]
195            test_service: None,
196        }
197    }
198
199    /// Installs an event-loop-thread service used only by native test drivers.
200    ///
201    /// The callback runs after the UI host has processed each native callback,
202    /// so deterministic event envelopes can be injected into live windows
203    /// before their capture frame. This hook is intentionally unavailable
204    /// without `test_support` and is not re-exported by the facade.
205    /// Repeated calls replace the previous callback.
206    ///
207    /// # Examples
208    ///
209    /// ```
210    /// use ailloli_ui_winit::{NoopHostDriver, UiApp, WinitHost};
211    /// let host = WinitHost::new(UiApp::<()>::new(), NoopHostDriver)
212    ///     .test_service(|ui| ui.request_redraw_all());
213    /// let _ = host;
214    /// ```
215    #[cfg(feature = "test_support")]
216    pub fn test_service(mut self, service: impl FnMut(&mut UiApp<A>) + 'static) -> Self {
217        self.test_service = Some(Box::new(service));
218        self
219    }
220
221    /// Attaches the single bounded mailbox drained by this host.
222    ///
223    /// In debug builds, attaching a second inbox to the same builder value
224    /// panics. Each host callback drains at most the inbox's configured service
225    /// budget (the runtime default is 256 messages).
226    ///
227    /// # Examples
228    ///
229    /// ```
230    /// use std::num::NonZeroUsize;
231    /// use ailloli_ui_runtime::app::RuntimeInbox;
232    /// use ailloli_ui_winit::{NoopHostDriver, UiApp, WinitHost};
233    /// let (_sender, inbox) = RuntimeInbox::<()>::channel(NonZeroUsize::new(4).unwrap());
234    /// let host = WinitHost::new(UiApp::new(), NoopHostDriver).runtime_inbox(inbox);
235    /// let _ = host;
236    /// ```
237    pub fn runtime_inbox(mut self, inbox: RuntimeInbox<A>) -> Self {
238        debug_assert!(self.runtime_inbox.is_none());
239        self.runtime_inbox = Some(inbox);
240        self
241    }
242
243    /// Retained UI application.
244    ///
245    /// # Examples
246    ///
247    /// ```
248    /// let host = ailloli_ui_winit::WinitHost::new(
249    ///     ailloli_ui_winit::UiApp::<()>::new(), ailloli_ui_winit::NoopHostDriver);
250    /// let _: &ailloli_ui_winit::UiApp<()> = host.ui();
251    /// ```
252    pub fn ui(&self) -> &UiApp<A> {
253        &self.ui
254    }
255
256    /// Mutable retained UI application.
257    ///
258    /// # Examples
259    ///
260    /// ```
261    /// let mut host = ailloli_ui_winit::WinitHost::new(
262    ///     ailloli_ui_winit::UiApp::<()>::new(), ailloli_ui_winit::NoopHostDriver);
263    /// host.ui_mut().request_redraw_all();
264    /// ```
265    pub fn ui_mut(&mut self) -> &mut UiApp<A> {
266        &mut self.ui
267    }
268
269    /// Provider-neutral driver.
270    ///
271    /// # Examples
272    ///
273    /// ```
274    /// let host = ailloli_ui_winit::WinitHost::new(
275    ///     ailloli_ui_winit::UiApp::<()>::new(), ailloli_ui_winit::NoopHostDriver);
276    /// let _: &ailloli_ui_winit::NoopHostDriver = host.driver();
277    /// ```
278    pub fn driver(&self) -> &D {
279        &self.driver
280    }
281
282    /// Mutable provider-neutral driver.
283    ///
284    /// # Examples
285    ///
286    /// ```
287    /// let mut host = ailloli_ui_winit::WinitHost::new(
288    ///     ailloli_ui_winit::UiApp::<()>::new(), ailloli_ui_winit::NoopHostDriver);
289    /// let _: &mut ailloli_ui_winit::NoopHostDriver = host.driver_mut();
290    /// ```
291    pub fn driver_mut(&mut self) -> &mut D {
292        &mut self.driver
293    }
294
295    /// Takes the first UI host error, if any.
296    ///
297    /// The slot is destructive: a second call returns `None` unless another
298    /// error has occurred.
299    ///
300    /// # Examples
301    ///
302    /// ```
303    /// let mut host = ailloli_ui_winit::WinitHost::new(
304    ///     ailloli_ui_winit::UiApp::<()>::new(), ailloli_ui_winit::NoopHostDriver);
305    /// assert!(host.take_error().is_none());
306    /// ```
307    pub fn take_error(&mut self) -> Option<UiAppError> {
308        self.ui.take_error()
309    }
310
311    /// Takes the first non-fatal mailbox wake error observed by the host.
312    ///
313    /// # Examples
314    ///
315    /// ```
316    /// let mut host = ailloli_ui_winit::WinitHost::new(
317    ///     ailloli_ui_winit::UiApp::<()>::new(), ailloli_ui_winit::NoopHostDriver);
318    /// assert!(host.take_inbox_wake_error().is_none());
319    /// ```
320    pub fn take_inbox_wake_error(&mut self) -> Option<UiWakeError> {
321        self.inbox_wake_error.take()
322    }
323
324    /// Takes the first non-fatal capture wake error observed by the host.
325    ///
326    /// A host-latched installation error has priority over an error still held
327    /// by the capture handle. The selected error is consumed.
328    ///
329    /// # Examples
330    ///
331    /// ```
332    /// let mut host = ailloli_ui_winit::WinitHost::new(
333    ///     ailloli_ui_winit::UiApp::<()>::new(), ailloli_ui_winit::NoopHostDriver);
334    /// assert!(host.take_capture_wake_error().is_none());
335    /// ```
336    pub fn take_capture_wake_error(&mut self) -> Option<UiWakeError> {
337        let handle_error = self
338            .ui
339            .capture_handle_for_host()
340            .and_then(|capture| capture.take_wake_error());
341        self.capture_wake_error.take().or(handle_error)
342    }
343
344    /// Takes the first non-fatal remote-devtools wake error observed by the host.
345    ///
346    /// A host-latched error has priority over the remote subsystem's slot. The
347    /// selected error is consumed.
348    ///
349    /// # Examples
350    ///
351    /// ```
352    /// let mut host = ailloli_ui_winit::WinitHost::new(
353    ///     ailloli_ui_winit::UiApp::<()>::new(), ailloli_ui_winit::NoopHostDriver);
354    /// assert!(host.take_devtools_wake_error().is_none());
355    /// ```
356    #[cfg(feature = "devtools")]
357    pub fn take_devtools_wake_error(&mut self) -> Option<UiWakeError> {
358        self.devtools_wake_error
359            .take()
360            .or_else(|| self.ui.take_devtools_wake_error())
361    }
362
363    /// Installs the same late-bound wake in inbox, capture, devtools, and UI sources.
364    ///
365    /// The first error from each subsystem is latched without discarding queued work.
366    fn install_host_wake(&mut self, wake: Arc<dyn UiWake>) {
367        if let Some(inbox) = self.runtime_inbox.as_ref() {
368            if let Err(error) = inbox.install_wake(wake.clone()) {
369                self.inbox_wake_error.get_or_insert(error);
370            }
371        }
372        if let Some(capture) = self.ui.capture_handle_for_host() {
373            if let Err(error) = capture.install_wake(wake.clone()) {
374                self.capture_wake_error.get_or_insert(error);
375            }
376        }
377        self.ui.install_host_wake(wake);
378    }
379}
380
381/// Ordered per-callback service pipeline for an executable host driver.
382impl<A: 'static, D: HostDriver<A>> WinitHost<A, D> {
383    /// Drains one bounded inbox budget and applies global/targeted redraw requests.
384    fn service_inbox(&mut self) {
385        let Some(inbox) = self.runtime_inbox.as_mut() else {
386            return;
387        };
388        match inbox.drain(&self.ui.runtime()) {
389            Ok(drain) => {
390                if drain.redraw_all {
391                    self.ui.request_redraw_all();
392                }
393                for logical_window_id in drain.redraw_windows {
394                    self.ui.request_window_redraw(&logical_window_id);
395                }
396            }
397            Err(error) => {
398                self.inbox_wake_error.get_or_insert(error);
399            }
400        }
401    }
402
403    /// Marks capture service active, requests redraw for pending work, and latches wake errors.
404    fn service_capture_requests(&mut self) {
405        let Some(capture) = self.ui.capture_handle_for_host() else {
406            return;
407        };
408        capture.begin_host_service();
409        for logical_window_id in capture.pending_window_ids() {
410            self.ui
411                .request_window_redraw(&ailloli_ui_core::LogicalWindowId::new(logical_window_id));
412        }
413        if let Some(error) = capture.wake_error() {
414            self.capture_wake_error.get_or_insert(error);
415        }
416    }
417
418    /// Services test/devtools/inbox/capture/UI sources before the application driver.
419    ///
420    /// Redraw and close effects are applied before returning the driver's deadline.
421    fn service_driver(&mut self, event_loop: &ActiveEventLoop) -> HostOutcome {
422        #[cfg(feature = "test_support")]
423        if let Some(mut service) = self.test_service.take() {
424            service(&mut self.ui);
425            self.test_service = Some(service);
426        }
427        #[cfg(feature = "devtools")]
428        {
429            for (logical_window_id, presentation_generation) in
430                self.ui.begin_devtools_host_service()
431            {
432                self.ui
433                    .request_presentation_redraw(&logical_window_id, presentation_generation);
434            }
435            if let Some(error) = self.ui.take_devtools_wake_error() {
436                self.devtools_wake_error.get_or_insert(error);
437            }
438        }
439        self.service_inbox();
440        self.service_capture_requests();
441        let _ = self.ui.runtime().service_ui_sources();
442        let outcome = self.driver.service(&self.ui.runtime(), Instant::now());
443        if outcome.redraw_all {
444            self.ui.request_redraw_all();
445        }
446        self.ui.request_pending_presentation_redraws();
447        if outcome.exit || self.ui.runtime().take_close_requested() {
448            event_loop.exit();
449        }
450        outcome
451    }
452}
453
454#[derive(Debug)]
455/// Wake adapter that sends the unit user event into winit's loop.
456struct WinitUiWake(winit::event_loop::EventLoopProxy<()>);
457
458/// Converts a closed event-loop proxy into the provider-neutral wake error.
459impl UiWake for WinitUiWake {
460    /// Enqueues one coalescible wake-only unit event.
461    ///
462    /// # Errors
463    ///
464    /// Returns [`UiWakeError::TargetClosed`] when winit's event-loop receiver no
465    /// longer accepts user events.
466    fn wake(&self) -> Result<(), UiWakeError> {
467        self.0.send_event(()).map_err(|_| UiWakeError::TargetClosed)
468    }
469}
470
471/// Routes every winit callback through the UI host, then the ordered service pipeline.
472impl<A: 'static, D: HostDriver<A>> ApplicationHandler for WinitHost<A, D> {
473    /// Recreates native presentations before servicing application work.
474    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
475        self.ui.host_resumed(event_loop);
476        self.service_driver(event_loop);
477    }
478
479    /// Detaches native presentations before servicing application work.
480    fn suspended(&mut self, event_loop: &ActiveEventLoop) {
481        self.ui.host_suspended(event_loop);
482        self.service_driver(event_loop);
483    }
484
485    /// Routes one native window event before servicing application work.
486    fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
487        self.ui.host_window_event(event_loop, id, event);
488        self.service_driver(event_loop);
489    }
490
491    /// Treats unit user events as wake-only notifications, then services work.
492    fn user_event(&mut self, event_loop: &ActiveEventLoop, event: ()) {
493        self.ui.host_user_event(event_loop, event);
494        self.service_driver(event_loop);
495    }
496
497    /// Services work and configures the next event-loop sleep deadline.
498    fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
499        let outcome = self.service_driver(event_loop);
500        self.ui.host_about_to_wait(event_loop, outcome.next_wake);
501    }
502}
503
504/// Creates a native event loop and runs a [`WinitHost`] in wait mode.
505///
506/// The loop may be created on a worker thread on Linux. Before entering it, one
507/// shared wake-only proxy is installed into every attached host subsystem. The
508/// call blocks until exit.
509///
510/// # Errors
511///
512/// Propagates event-loop construction and non-normalized run failures.
513///
514/// # Examples
515///
516/// ```no_run
517/// use ailloli_ui_winit::{run_winit_host, NoopHostDriver, UiApp, WinitHost};
518/// let mut host = WinitHost::new(UiApp::<()>::new(), NoopHostDriver);
519/// run_winit_host(&mut host)?;
520/// # Ok::<(), winit::error::EventLoopError>(())
521/// ```
522pub fn run_winit_host<A: 'static, D: HostDriver<A>>(
523    host: &mut WinitHost<A, D>,
524) -> Result<(), winit::error::EventLoopError> {
525    let event_loop = new_event_loop_allow_any_thread()?;
526    let wake: Arc<dyn UiWake> = Arc::new(WinitUiWake(event_loop.create_proxy()));
527    host.install_host_wake(wake);
528    // Runtime inbox, capture, remote devtools, and native_overlay events all
529    // share this wake-only host proxy. Linux shutdown keeps only the signal
530    // handler which targets the same native event loop.
531    run_app_on_event_loop(event_loop, host, ControlFlow::Wait)
532}
533
534#[cfg(test)]
535/// Inbox ordering/budget, redraw, shared wake, and failure-retention scenarios.
536mod tests {
537    use super::*;
538    use std::num::NonZeroUsize;
539    use std::sync::atomic::{AtomicUsize, Ordering};
540
541    #[derive(Default)]
542    /// Wake test double counting successful wake requests.
543    struct CountingWake(AtomicUsize);
544
545    /// Relaxed counter implementation sufficient for single-test observation.
546    impl UiWake for CountingWake {
547        fn wake(&self) -> Result<(), UiWakeError> {
548            self.0.fetch_add(1, Ordering::Relaxed);
549            Ok(())
550        }
551    }
552
553    /// Wake test double that always reports a closed target.
554    struct FailingWake;
555
556    /// Deterministic failure implementation used to verify queued work retention.
557    impl UiWake for FailingWake {
558        fn wake(&self) -> Result<(), UiWakeError> {
559            Err(UiWakeError::TargetClosed)
560        }
561    }
562
563    #[test]
564    fn host_outcome_constructors_are_provider_neutral() {
565        fn assert_application_handler<T: ApplicationHandler>() {}
566
567        assert_application_handler::<WinitHost<(), NoopHostDriver>>();
568        assert!(HostOutcome::exit().exit);
569        assert!(HostOutcome::redraw_all().redraw_all);
570        assert_eq!(HostOutcome::default().next_wake, None);
571    }
572
573    #[test]
574    fn runtime_wake_host_drains_inbox_before_servicing_driver() {
575        let (sender, inbox) = RuntimeInbox::channel(NonZeroUsize::new(4).unwrap());
576        let ui = UiApp::<u32>::new();
577        let mut host = WinitHost::new(ui, NoopHostDriver).runtime_inbox(inbox);
578
579        sender.dispatch(42).unwrap();
580        host.service_inbox();
581
582        assert_eq!(host.ui().runtime().take_actions(), vec![42]);
583        assert_eq!(sender.stats().current_depth, 0);
584    }
585
586    #[test]
587    fn host_applies_global_and_targeted_redraw_messages_without_windows() {
588        let (sender, inbox) = RuntimeInbox::<()>::channel(NonZeroUsize::new(4).unwrap());
589        let ui = UiApp::<()>::new();
590        let mut host = WinitHost::new(ui, NoopHostDriver).runtime_inbox(inbox);
591
592        sender.request_redraw().unwrap();
593        sender.request_window_redraw("detached-window").unwrap();
594        host.service_inbox();
595
596        assert_eq!(sender.stats().current_depth, 0);
597    }
598
599    #[test]
600    fn one_host_service_respects_the_256_message_inbox_budget() {
601        let (sender, inbox) = RuntimeInbox::channel(NonZeroUsize::new(300).unwrap());
602        let ui = UiApp::<u32>::new();
603        let mut host = WinitHost::new(ui, NoopHostDriver).runtime_inbox(inbox);
604
605        for action in 0..257 {
606            sender.dispatch(action).unwrap();
607        }
608        host.service_inbox();
609
610        assert_eq!(
611            host.ui().runtime().take_actions(),
612            (0..256).collect::<Vec<_>>()
613        );
614        assert_eq!(sender.stats().current_depth, 1);
615
616        host.service_inbox();
617        assert_eq!(host.ui().runtime().take_actions(), vec![256]);
618        assert_eq!(sender.stats().current_depth, 0);
619    }
620
621    #[test]
622    fn runtime_wake_shares_one_late_bound_wake_with_inbox_and_capture() {
623        let (sender, inbox) = RuntimeInbox::channel(NonZeroUsize::new(4).unwrap());
624        let capture = crate::CaptureHandle::new();
625        sender.dispatch(7_u32).unwrap();
626        capture.request_window("main");
627        let ui = UiApp::new().capture_handle(capture.clone());
628        let mut host = WinitHost::new(ui, NoopHostDriver).runtime_inbox(inbox);
629        let wake = Arc::new(CountingWake::default());
630
631        host.install_host_wake(wake.clone());
632        assert_eq!(wake.0.load(Ordering::Relaxed), 2);
633
634        host.service_inbox();
635        host.service_capture_requests();
636        capture.request_window("requested-after-idle");
637        assert_eq!(wake.0.load(Ordering::Relaxed), 3);
638    }
639
640    #[test]
641    fn runtime_wake_failure_keeps_queued_work_observable() {
642        let (sender, inbox) = RuntimeInbox::channel(NonZeroUsize::new(4).unwrap());
643        let capture = crate::CaptureHandle::new();
644        sender.dispatch(7_u32).unwrap();
645        capture.request_window("main");
646        let ui = UiApp::new().capture_handle(capture.clone());
647        let mut host = WinitHost::new(ui, NoopHostDriver).runtime_inbox(inbox);
648
649        host.install_host_wake(Arc::new(FailingWake));
650
651        assert_eq!(
652            host.take_inbox_wake_error(),
653            Some(UiWakeError::TargetClosed)
654        );
655        assert_eq!(
656            host.take_capture_wake_error(),
657            Some(UiWakeError::TargetClosed)
658        );
659        assert_eq!(host.take_capture_wake_error(), None);
660        assert!(capture.has_pending_for_window("main"));
661        host.service_inbox();
662        assert_eq!(host.ui().runtime().take_actions(), vec![7]);
663    }
664}