Skip to main content

cranpose_app_shell/
lib.rs

1#![deny(unsafe_code)]
2#![allow(clippy::type_complexity)]
3
4mod fps_monitor;
5mod hit_path_tracker;
6mod shell_debug;
7mod shell_frame;
8mod shell_input;
9#[cfg(test)]
10use shell_frame::build_draw_refresh_scope;
11
12pub use fps_monitor::FpsStats;
13
14use std::fmt::{Debug, Write};
15use std::rc::Rc;
16use std::sync::{
17    atomic::{AtomicBool, Ordering},
18    Mutex, MutexGuard,
19};
20// Use web_time for cross-platform time support (native + WASM) - compatible with winit
21use web_time::Instant;
22
23use cranpose_core::{
24    enter_event_handler_scope, location_key, run_in_mutable_snapshot, Applier, Composition, Key,
25    MemoryApplier, NodeError, NodeId,
26};
27use cranpose_foundation::{PointerButton, PointerButtons, PointerEvent, PointerEventKind};
28use cranpose_render_common::{HitTestTarget, RenderScene, Renderer};
29use cranpose_runtime_std::StdRuntime;
30use cranpose_ui::{
31    format_layout_tree, format_render_scene, format_screen_summary,
32    has_pending_focus_invalidations, has_pending_pointer_repasses, peek_focus_invalidation,
33    peek_layout_invalidation, peek_pointer_invalidation, peek_render_invalidation,
34    process_focus_invalidations, process_pointer_repasses, request_render_invalidation,
35    take_draw_repass_nodes, take_focus_invalidation, take_layout_invalidation,
36    take_pointer_invalidation, take_render_invalidation, HeadlessRenderer, LayoutBox, LayoutNode,
37    LayoutTree, MeasureLayoutOptions, SemanticsTree, SubcomposeLayoutNode,
38};
39use cranpose_ui_graphics::{Point, Rect, Size};
40use hit_path_tracker::{HitPathTracker, PointerId};
41use std::collections::HashSet;
42
43// Re-export key event types for use by cranpose
44pub use cranpose_ui::{KeyCode, KeyEvent, KeyEventType, Modifiers};
45
46#[cfg(any(test, feature = "test-support"))]
47use cranpose_core::{
48    debug_recompose_scope_registry_stats, MemoryApplierDebugStats,
49    RecomposeScopeRegistryDebugStats, SlotTableDebugStats,
50};
51#[cfg(any(test, feature = "test-support"))]
52use cranpose_core::{
53    runtime::{RuntimeDebugStats, StateArenaDebugStats},
54    snapshot_pinning::{debug_snapshot_pinning_stats, SnapshotPinningDebugStats},
55    snapshot_state_observer::SnapshotStateObserverDebugStats,
56    snapshot_v2::{debug_snapshot_v2_stats, SnapshotV2DebugStats},
57    CompositionPassDebugStats, SlotId,
58};
59
60pub struct AppShell<R>
61where
62    R: Renderer,
63{
64    app_context: Rc<cranpose_ui::AppContext>,
65    runtime: StdRuntime,
66    composition: Composition<MemoryApplier>,
67    content: Box<dyn FnMut()>,
68    renderer: R,
69    cursor: (f32, f32),
70    viewport: (f32, f32),
71    buffer_size: (u32, u32),
72    start_time: Instant,
73    last_frame_time_nanos: u64,
74    layout_tree: Option<LayoutTree>,
75    semantics_tree: Option<SemanticsTree>,
76    semantics_enabled: bool,
77    layout_requested: bool,
78    force_layout_pass: bool,
79    scene_dirty: bool,
80    is_dirty: bool,
81    /// Tracks which mouse buttons are currently pressed
82    buttons_pressed: PointerButtons,
83    /// Tracks which nodes were hit on PointerDown (by stable NodeId).
84    ///
85    /// This follows Jetpack Compose's HitPathTracker pattern:
86    /// - On Down: cache NodeIds, not geometry
87    /// - On Move/Up/Cancel: resolve fresh HitTargets from current scene
88    /// - Handler closures are preserved (same Rc), so internal state survives
89    hit_path_tracker: HitPathTracker,
90    /// Tracks which nodes the pointer is currently hovering over.
91    /// Used to synthesize Enter/Exit events when the hover set changes.
92    hovered_nodes: Vec<NodeId>,
93    /// Persistent clipboard for desktop (Linux X11 requires clipboard to stay alive)
94    #[cfg(all(
95        feature = "clipboard-native",
96        not(target_arch = "wasm32"),
97        not(target_os = "android"),
98        not(target_os = "ios")
99    ))]
100    clipboard: Option<arboard::Clipboard>,
101    /// Dev options for debugging and performance monitoring
102    dev_options: DevOptions,
103    dev_overlay_controls: Vec<DevOverlayControl>,
104    fps_monitor: fps_monitor::FpsMonitor,
105    frame_scheduler: FrameScheduler,
106}
107
108#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
109pub enum FramePacingMode {
110    Vsync,
111    Hard60,
112    Hard120,
113    #[default]
114    NoVsync,
115}
116
117impl FramePacingMode {
118    pub const ALL: [Self; 4] = [Self::Vsync, Self::Hard60, Self::Hard120, Self::NoVsync];
119
120    pub fn label(self) -> &'static str {
121        match self {
122            Self::Vsync => "VSync",
123            Self::Hard60 => "60fps",
124            Self::Hard120 => "120fps",
125            Self::NoVsync => "NoVSync",
126        }
127    }
128
129    pub fn target_fps(self) -> Option<u32> {
130        match self {
131            Self::Hard60 => Some(60),
132            Self::Hard120 => Some(120),
133            Self::Vsync | Self::NoVsync => None,
134        }
135    }
136}
137
138#[derive(Clone, Copy, Debug, PartialEq)]
139pub struct FrameSchedule {
140    pub needs_frame: bool,
141    pub next_deadline: Option<web_time::Instant>,
142}
143
144pub trait PlatformFrameDriver {
145    fn request_frame(&self);
146    fn request_wake_at(&self, deadline: web_time::Instant);
147    fn clear_wake(&self);
148}
149
150#[derive(Debug)]
151pub struct FrameScheduler {
152    frame_pending: AtomicBool,
153    next_deadline: Mutex<Option<web_time::Instant>>,
154}
155
156impl Default for FrameScheduler {
157    fn default() -> Self {
158        Self {
159            frame_pending: AtomicBool::new(false),
160            next_deadline: Mutex::new(None),
161        }
162    }
163}
164
165impl FrameScheduler {
166    fn lock_deadline(&self) -> MutexGuard<'_, Option<web_time::Instant>> {
167        self.next_deadline
168            .lock()
169            .unwrap_or_else(|poisoned| poisoned.into_inner())
170    }
171
172    pub fn record(&self, schedule: FrameSchedule) {
173        self.frame_pending
174            .store(schedule.needs_frame, Ordering::SeqCst);
175        let mut next_deadline = self.lock_deadline();
176        *next_deadline = if schedule.needs_frame {
177            None
178        } else {
179            schedule.next_deadline
180        };
181    }
182
183    pub fn schedule<D>(&self, schedule: FrameSchedule, driver: &D)
184    where
185        D: PlatformFrameDriver + ?Sized,
186    {
187        self.record(schedule);
188        schedule.apply_to(driver);
189    }
190
191    pub fn snapshot(&self) -> FrameSchedule {
192        FrameSchedule {
193            needs_frame: self.frame_pending.load(Ordering::SeqCst),
194            next_deadline: *self.lock_deadline(),
195        }
196    }
197}
198
199impl FrameSchedule {
200    pub fn apply_to<D>(self, driver: &D)
201    where
202        D: PlatformFrameDriver + ?Sized,
203    {
204        if self.needs_frame {
205            driver.clear_wake();
206            driver.request_frame();
207        } else if let Some(deadline) = self.next_deadline {
208            driver.request_wake_at(deadline);
209        } else {
210            driver.clear_wake();
211        }
212    }
213}
214
215#[derive(Clone, Copy, Debug)]
216struct DevOverlayControl {
217    bounds: Rect,
218    mode: FramePacingMode,
219}
220
221/// Development options for debugging and performance monitoring.
222///
223/// These are rendered directly by the renderer (not via composition)
224/// to avoid affecting performance measurements.
225#[derive(Clone, Debug, Default)]
226pub struct DevOptions {
227    /// Show FPS counter overlay
228    pub fps_counter: bool,
229    /// Show recomposition count
230    pub recomposition_counter: bool,
231    /// Show layout timing breakdown
232    pub layout_timing: bool,
233    pub frame_pacing_controls: bool,
234    pub frame_pacing_mode: FramePacingMode,
235}
236
237#[cfg(any(test, feature = "test-support"))]
238#[doc(hidden)]
239#[derive(Clone, Copy, Debug)]
240pub struct RuntimeLeakDebugStats {
241    pub applier_stats: MemoryApplierDebugStats,
242    pub live_node_heap_bytes: usize,
243    pub recycled_node_heap_bytes: usize,
244    pub slot_table_heap_bytes: usize,
245    pub pass_stats: CompositionPassDebugStats,
246    pub slot_stats: SlotTableDebugStats,
247    pub observer_stats: SnapshotStateObserverDebugStats,
248    pub runtime_stats: RuntimeDebugStats,
249    pub state_arena_stats: StateArenaDebugStats,
250    pub recompose_scope_stats: RecomposeScopeRegistryDebugStats,
251    pub snapshot_v2_stats: SnapshotV2DebugStats,
252    pub snapshot_pinning_stats: SnapshotPinningDebugStats,
253}
254
255impl<R> AppShell<R>
256where
257    R: Renderer,
258    R::Error: Debug,
259{
260    pub fn new(renderer: R, root_key: Key, content: impl FnMut() + 'static) -> Self {
261        Self::new_with_size(renderer, root_key, content, (800, 600), (800.0, 600.0))
262    }
263
264    pub fn new_with_size(
265        renderer: R,
266        root_key: Key,
267        content: impl FnMut() + 'static,
268        buffer_size: (u32, u32),
269        viewport: (f32, f32),
270    ) -> Self {
271        Self::new_with_size_and_density(renderer, root_key, content, buffer_size, viewport, 1.0)
272    }
273
274    pub fn new_with_size_and_density(
275        mut renderer: R,
276        root_key: Key,
277        content: impl FnMut() + 'static,
278        buffer_size: (u32, u32),
279        viewport: (f32, f32),
280        density: f32,
281    ) -> Self {
282        let app_context = cranpose_ui::AppContext::new_with_density(density);
283        let runtime = StdRuntime::new();
284        let mut composition = Composition::with_runtime(MemoryApplier::new(), runtime.runtime());
285        let mut build: Box<dyn FnMut()> = Box::new(content);
286        renderer.attach_app_context_services(&app_context);
287        app_context.enter(|| {
288            if let Err(err) = composition.render_stable(root_key, &mut *build) {
289                log::error!("initial render failed: {err}");
290            }
291        });
292        renderer.scene_mut().clear();
293        let mut shell = Self {
294            app_context,
295            runtime,
296            composition,
297            content: build,
298            renderer,
299            cursor: (0.0, 0.0),
300            viewport,
301            buffer_size,
302            start_time: Instant::now(),
303            last_frame_time_nanos: 0,
304            layout_tree: None,
305            semantics_tree: None,
306            semantics_enabled: false,
307            layout_requested: true,
308            force_layout_pass: true,
309            scene_dirty: true,
310            is_dirty: true,
311            buttons_pressed: PointerButtons::NONE,
312            hit_path_tracker: HitPathTracker::new(),
313            hovered_nodes: Vec::new(),
314            #[cfg(all(
315                feature = "clipboard-native",
316                not(target_arch = "wasm32"),
317                not(target_os = "android"),
318                not(target_os = "ios")
319            ))]
320            clipboard: arboard::Clipboard::new().ok(),
321            dev_options: DevOptions::default(),
322            dev_overlay_controls: Vec::new(),
323            fps_monitor: fps_monitor::FpsMonitor::new(),
324            frame_scheduler: FrameScheduler::default(),
325        };
326        shell.process_frame();
327        shell
328    }
329
330    /// Set development options for debugging and performance monitoring.
331    ///
332    /// The FPS counter and other overlays are rendered directly by the renderer
333    /// (not via composition) to avoid affecting performance measurements.
334    pub fn set_dev_options(&mut self, options: DevOptions) {
335        self.dev_options = options;
336        self.mark_dirty();
337    }
338
339    /// Get a reference to the current dev options.
340    pub fn dev_options(&self) -> &DevOptions {
341        &self.dev_options
342    }
343
344    pub fn frame_pacing_mode(&self) -> FramePacingMode {
345        self.dev_options.frame_pacing_mode
346    }
347
348    pub fn current_fps(&self) -> f32 {
349        self.fps_monitor.current_fps()
350    }
351
352    pub fn fps_stats(&self) -> FpsStats {
353        self.fps_monitor.stats()
354    }
355
356    pub fn reset_fps_stats(&mut self) {
357        self.fps_monitor.reset_stats();
358    }
359
360    pub fn set_frame_pacing_mode(&mut self, mode: FramePacingMode) {
361        if self.dev_options.frame_pacing_mode == mode {
362            return;
363        }
364        self.dev_options.frame_pacing_mode = mode;
365        let app_context = Rc::clone(&self.app_context);
366        app_context.enter(request_render_invalidation);
367        self.mark_dirty();
368    }
369
370    pub fn handle_dev_overlay_click(&mut self, x: f32, y: f32) -> Option<FramePacingMode> {
371        if !self.dev_options.frame_pacing_controls {
372            return None;
373        }
374        let mode = self
375            .dev_overlay_controls
376            .iter()
377            .find(|control| control.bounds.contains(x, y))
378            .map(|control| control.mode)?;
379        self.set_frame_pacing_mode(mode);
380        Some(mode)
381    }
382
383    pub fn set_viewport(&mut self, width: f32, height: f32) {
384        self.viewport = (width, height);
385        self.request_forced_layout_pass();
386        self.mark_dirty();
387        self.process_frame();
388    }
389
390    pub fn viewport_size(&self) -> (f32, f32) {
391        self.viewport
392    }
393
394    pub fn set_buffer_size(&mut self, width: u32, height: u32) {
395        self.buffer_size = (width, height);
396    }
397
398    pub fn buffer_size(&self) -> (u32, u32) {
399        self.buffer_size
400    }
401
402    pub fn scene(&self) -> &R::Scene {
403        self.renderer.scene()
404    }
405
406    pub fn renderer(&mut self) -> &mut R {
407        &mut self.renderer
408    }
409
410    #[cfg(not(target_arch = "wasm32"))]
411    pub fn set_frame_waker(&mut self, waker: impl Fn() + Send + Sync + 'static) {
412        self.runtime.set_frame_waker(waker);
413    }
414
415    #[cfg(target_arch = "wasm32")]
416    pub fn set_frame_waker(&mut self, waker: impl Fn() + 'static) {
417        self.runtime.set_frame_waker(waker);
418    }
419
420    pub fn clear_frame_waker(&mut self) {
421        self.runtime.clear_frame_waker();
422    }
423
424    pub fn should_render(&self) -> bool {
425        let app_context = Rc::clone(&self.app_context);
426        app_context.enter(|| {
427            if self.layout_requested
428                || self.scene_dirty
429                || peek_render_invalidation()
430                || peek_pointer_invalidation()
431                || peek_focus_invalidation()
432                || peek_layout_invalidation()
433            {
434                return true;
435            }
436            self.composition.should_render()
437        })
438    }
439
440    /// Returns true if the shell needs to redraw (dirty flag, layout dirty, active animations).
441    /// Note: Cursor blink is now timer-based and uses WaitUntil scheduling, not continuous redraw.
442    pub fn needs_redraw(&self) -> bool {
443        let app_context = Rc::clone(&self.app_context);
444        app_context.enter(|| {
445            if self.is_dirty
446                || self.layout_requested
447                || self.scene_dirty
448                || peek_render_invalidation()
449                || peek_pointer_invalidation()
450                || peek_focus_invalidation()
451                || peek_layout_invalidation()
452                || cranpose_ui::has_pending_layout_repasses()
453                || cranpose_ui::has_pending_draw_repasses()
454                || has_pending_pointer_repasses()
455                || has_pending_focus_invalidations()
456            {
457                return true;
458            }
459
460            self.composition.should_render()
461        })
462    }
463
464    /// Marks the shell as dirty, indicating a redraw is needed.
465    pub fn mark_dirty(&mut self) {
466        self.is_dirty = true;
467    }
468
469    pub fn request_root_render(&mut self) {
470        self.composition.request_root_render();
471        self.request_forced_layout_pass();
472        let app_context = Rc::clone(&self.app_context);
473        app_context.enter(request_render_invalidation);
474        self.mark_dirty();
475    }
476
477    pub fn set_density(&mut self, density: f32) {
478        let app_context = Rc::clone(&self.app_context);
479        let changed = app_context.enter(|| {
480            let previous = cranpose_ui::current_density().to_bits();
481            cranpose_ui::set_density(density);
482            previous != cranpose_ui::current_density().to_bits()
483        });
484        if changed {
485            self.request_forced_layout_pass();
486            self.mark_dirty();
487        }
488    }
489
490    #[cfg(any(test, feature = "test-support"))]
491    #[doc(hidden)]
492    pub fn debug_current_density(&self) -> f32 {
493        let app_context = Rc::clone(&self.app_context);
494        app_context.enter(cranpose_ui::current_density)
495    }
496
497    #[cfg(any(test, feature = "test-support"))]
498    #[doc(hidden)]
499    pub fn debug_enter_app_context<T>(&self, block: impl FnOnce() -> T) -> T {
500        let app_context = Rc::clone(&self.app_context);
501        app_context.enter(block)
502    }
503
504    fn request_layout_pass(&mut self) {
505        self.layout_requested = true;
506    }
507
508    fn request_forced_layout_pass(&mut self) {
509        self.layout_requested = true;
510        self.force_layout_pass = true;
511    }
512
513    /// Returns true if there are active animations or pending recompositions.
514    pub fn has_active_animations(&self) -> bool {
515        self.composition.should_render()
516    }
517
518    /// Returns the next scheduled event time for cursor blink.
519    /// Use this for `ControlFlow::WaitUntil` scheduling.
520    pub fn next_event_time(&self) -> Option<web_time::Instant> {
521        let app_context = Rc::clone(&self.app_context);
522        app_context.enter(cranpose_ui::next_cursor_blink_time)
523    }
524
525    fn compute_frame_schedule(&self) -> FrameSchedule {
526        FrameSchedule {
527            needs_frame: self.needs_redraw() || self.has_active_animations(),
528            next_deadline: self.next_event_time(),
529        }
530    }
531
532    pub fn frame_schedule(&self) -> FrameSchedule {
533        let schedule = self.compute_frame_schedule();
534        self.frame_scheduler.record(schedule);
535        schedule
536    }
537
538    pub fn schedule_platform_frame<D>(&self, driver: &D) -> FrameSchedule
539    where
540        D: PlatformFrameDriver + ?Sized,
541    {
542        let schedule = self.compute_frame_schedule();
543        self.frame_scheduler.schedule(schedule, driver);
544        schedule
545    }
546
547    pub fn frame_scheduler_snapshot(&self) -> FrameSchedule {
548        self.frame_scheduler.snapshot()
549    }
550
551    fn frame_time_nanos_at(&self, now: Instant) -> u64 {
552        now.checked_duration_since(self.start_time)
553            .unwrap_or_default()
554            .as_nanos()
555            .min(u128::from(u64::MAX)) as u64
556    }
557
558    pub fn update_after_frame_interval(&mut self, frame_interval: std::time::Duration) {
559        let wall_frame_time = self.frame_time_nanos_at(Instant::now());
560        let base_frame_time = self.last_frame_time_nanos.max(wall_frame_time);
561        let frame_time = base_frame_time
562            .saturating_add(frame_interval.as_nanos().min(u128::from(u64::MAX)) as u64);
563        self.update_at_frame_time_nanos(frame_time);
564    }
565
566    pub fn update_at_frame_time_nanos(&mut self, frame_time: u64) {
567        let app_context = Rc::clone(&self.app_context);
568        app_context.enter(|| {
569            let frame_time = frame_time.max(self.last_frame_time_nanos);
570            self.last_frame_time_nanos = frame_time;
571            let runtime_handle = self.runtime.runtime_handle();
572            runtime_handle.with_deferred_state_releases(|| {
573                self.runtime.drain_frame_callbacks(frame_time);
574                runtime_handle.drain_ui();
575                let should_render = self.composition.should_render();
576                if should_render {
577                    log::trace!(
578                        target: "cranpose::input",
579                        "update begin: should_render=true layout_requested={} scene_dirty={} is_dirty={}",
580                        self.layout_requested,
581                        self.scene_dirty,
582                        self.is_dirty
583                    );
584                }
585                if should_render {
586                    let Some(root_key) = self.composition.root_key() else {
587                        self.process_frame_in_context();
588                        self.is_dirty = false;
589                        return;
590                    };
591                    match self.composition.reconcile(root_key, &mut *self.content) {
592                        Ok(changed) => {
593                            log::trace!(
594                                target: "cranpose::input",
595                                "reconcile changed={changed}"
596                            );
597                            if changed {
598                                self.fps_monitor.record_recomposition();
599                                self.request_layout_pass();
600                                request_render_invalidation();
601                            }
602                        }
603                        Err(NodeError::Missing { id }) => {
604                            // Node was removed (likely due to conditional render or tab switch)
605                            // This is expected when scopes try to recompose after their nodes are gone
606                            log::debug!("Recomposition skipped: node {} no longer exists", id);
607                            self.request_layout_pass();
608                            request_render_invalidation();
609                        }
610                        Err(err) => {
611                            log::error!("recomposition failed: {err}");
612                            self.request_layout_pass();
613                            request_render_invalidation();
614                        }
615                    }
616                }
617                self.process_frame_in_context();
618                // Clear dirty flag after update (frame has been processed)
619                self.is_dirty = false;
620            });
621        });
622    }
623
624    pub fn update(&mut self) {
625        let frame_time = self.frame_time_nanos_at(Instant::now());
626        self.update_at_frame_time_nanos(frame_time);
627    }
628}
629
630impl<R> Drop for AppShell<R>
631where
632    R: Renderer,
633{
634    fn drop(&mut self) {
635        self.runtime.clear_frame_waker();
636    }
637}
638
639pub fn default_root_key() -> Key {
640    location_key(file!(), line!(), column!())
641}
642
643#[cfg(test)]
644mod frame_pacing_tests {
645    use super::{FramePacingMode, FrameSchedule, FrameScheduler, PlatformFrameDriver};
646    use std::cell::RefCell;
647    use std::panic::{catch_unwind, AssertUnwindSafe};
648    use std::time::Duration;
649    use web_time::Instant;
650
651    #[derive(Clone, Copy, Debug, PartialEq)]
652    enum DriverCall {
653        RequestFrame,
654        RequestWakeAt(Instant),
655        ClearWake,
656    }
657
658    #[derive(Default)]
659    struct RecordingFrameDriver {
660        calls: RefCell<Vec<DriverCall>>,
661    }
662
663    impl RecordingFrameDriver {
664        fn calls(&self) -> Vec<DriverCall> {
665            self.calls.borrow().clone()
666        }
667    }
668
669    impl PlatformFrameDriver for RecordingFrameDriver {
670        fn request_frame(&self) {
671            self.calls.borrow_mut().push(DriverCall::RequestFrame);
672        }
673
674        fn request_wake_at(&self, deadline: Instant) {
675            self.calls
676                .borrow_mut()
677                .push(DriverCall::RequestWakeAt(deadline));
678        }
679
680        fn clear_wake(&self) {
681            self.calls.borrow_mut().push(DriverCall::ClearWake);
682        }
683    }
684
685    #[test]
686    fn frame_pacing_labels_match_overlay_modes() {
687        assert_eq!(FramePacingMode::Vsync.label(), "VSync");
688        assert_eq!(FramePacingMode::Hard60.label(), "60fps");
689        assert_eq!(FramePacingMode::Hard120.label(), "120fps");
690        assert_eq!(FramePacingMode::NoVsync.label(), "NoVSync");
691    }
692
693    #[test]
694    fn only_hard_modes_have_fixed_targets() {
695        assert_eq!(FramePacingMode::Vsync.target_fps(), None);
696        assert_eq!(FramePacingMode::Hard60.target_fps(), Some(60));
697        assert_eq!(FramePacingMode::Hard120.target_fps(), Some(120));
698        assert_eq!(FramePacingMode::NoVsync.target_fps(), None);
699    }
700
701    #[test]
702    fn frame_schedule_requests_immediate_frame_and_clears_deadline() {
703        let driver = RecordingFrameDriver::default();
704        let deadline = Instant::now() + Duration::from_millis(25);
705
706        FrameSchedule {
707            needs_frame: true,
708            next_deadline: Some(deadline),
709        }
710        .apply_to(&driver);
711
712        assert_eq!(
713            driver.calls(),
714            vec![DriverCall::ClearWake, DriverCall::RequestFrame]
715        );
716    }
717
718    #[test]
719    fn frame_schedule_requests_deadline_when_idle_until_timer() {
720        let driver = RecordingFrameDriver::default();
721        let deadline = Instant::now() + Duration::from_millis(25);
722
723        FrameSchedule {
724            needs_frame: false,
725            next_deadline: Some(deadline),
726        }
727        .apply_to(&driver);
728
729        assert_eq!(driver.calls(), vec![DriverCall::RequestWakeAt(deadline)]);
730    }
731
732    #[test]
733    fn frame_schedule_clears_wake_when_fully_idle() {
734        let driver = RecordingFrameDriver::default();
735
736        FrameSchedule {
737            needs_frame: false,
738            next_deadline: None,
739        }
740        .apply_to(&driver);
741
742        assert_eq!(driver.calls(), vec![DriverCall::ClearWake]);
743    }
744
745    #[test]
746    fn frame_scheduler_records_latest_schedule_and_applies_driver() {
747        let scheduler = FrameScheduler::default();
748        let driver = RecordingFrameDriver::default();
749        let deadline = Instant::now() + Duration::from_millis(25);
750
751        scheduler.schedule(
752            FrameSchedule {
753                needs_frame: false,
754                next_deadline: Some(deadline),
755            },
756            &driver,
757        );
758
759        assert_eq!(
760            scheduler.snapshot(),
761            FrameSchedule {
762                needs_frame: false,
763                next_deadline: Some(deadline),
764            }
765        );
766        assert_eq!(driver.calls(), vec![DriverCall::RequestWakeAt(deadline)]);
767    }
768
769    #[test]
770    fn frame_scheduler_clears_deadline_for_immediate_frame() {
771        let scheduler = FrameScheduler::default();
772        let driver = RecordingFrameDriver::default();
773        let deadline = Instant::now() + Duration::from_millis(25);
774
775        scheduler.schedule(
776            FrameSchedule {
777                needs_frame: true,
778                next_deadline: Some(deadline),
779            },
780            &driver,
781        );
782
783        assert_eq!(
784            scheduler.snapshot(),
785            FrameSchedule {
786                needs_frame: true,
787                next_deadline: None,
788            }
789        );
790        assert_eq!(
791            driver.calls(),
792            vec![DriverCall::ClearWake, DriverCall::RequestFrame]
793        );
794    }
795
796    #[test]
797    fn frame_scheduler_recovers_poisoned_deadline_lock() {
798        let scheduler = FrameScheduler::default();
799        let deadline = Instant::now() + Duration::from_millis(25);
800
801        let _ = catch_unwind(AssertUnwindSafe(|| {
802            let _guard = scheduler.lock_deadline();
803            panic!("poison frame scheduler deadline lock");
804        }));
805
806        scheduler.record(FrameSchedule {
807            needs_frame: false,
808            next_deadline: Some(deadline),
809        });
810
811        assert_eq!(
812            scheduler.snapshot(),
813            FrameSchedule {
814                needs_frame: false,
815                next_deadline: Some(deadline),
816            }
817        );
818    }
819}
820
821#[cfg(test)]
822#[path = "tests/app_shell_tests.rs"]
823mod tests;