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    clear_transient_scroll_motion_contexts, format_layout_tree, format_render_scene,
32    format_screen_summary, has_pending_focus_invalidations, has_pending_pointer_repasses,
33    peek_focus_invalidation, peek_layout_invalidation, peek_pointer_invalidation,
34    peek_render_invalidation, process_focus_invalidations, process_pointer_repasses,
35    request_render_invalidation, take_draw_repass_nodes, take_focus_invalidation,
36    take_layout_invalidation, take_pointer_invalidation, take_render_invalidation,
37    HeadlessRenderer, LayoutBox, LayoutNode, LayoutTree, MeasureLayoutOptions, SemanticsTree,
38    SubcomposeLayoutNode,
39};
40use cranpose_ui_graphics::{Point, Rect, Size};
41use hit_path_tracker::{HitPathTracker, PointerId};
42use std::collections::HashSet;
43
44// Re-export key event types for use by cranpose
45pub use cranpose_ui::{KeyCode, KeyEvent, KeyEventType, Modifiers};
46
47#[cfg(any(test, feature = "test-support"))]
48use cranpose_core::{
49    debug_recompose_scope_registry_stats, MemoryApplierDebugStats,
50    RecomposeScopeRegistryDebugStats, SlotTableDebugStats,
51};
52#[cfg(any(test, feature = "test-support"))]
53use cranpose_core::{
54    runtime::{RuntimeDebugStats, StateArenaDebugStats},
55    snapshot_pinning::{debug_snapshot_pinning_stats, SnapshotPinningDebugStats},
56    snapshot_state_observer::SnapshotStateObserverDebugStats,
57    snapshot_v2::{debug_snapshot_v2_stats, SnapshotV2DebugStats},
58    CompositionPassDebugStats, SlotId,
59};
60
61pub struct AppShell<R>
62where
63    R: Renderer,
64{
65    app_context: Rc<cranpose_ui::AppContext>,
66    runtime: StdRuntime,
67    composition: Composition<MemoryApplier>,
68    content: Box<dyn FnMut()>,
69    renderer: R,
70    cursor: (f32, f32),
71    viewport: (f32, f32),
72    buffer_size: (u32, u32),
73    start_time: Instant,
74    last_frame_time_nanos: u64,
75    layout_tree: Option<LayoutTree>,
76    semantics_tree: Option<SemanticsTree>,
77    semantics_enabled: bool,
78    layout_requested: bool,
79    force_layout_pass: bool,
80    scene_dirty: bool,
81    scoped_layout_scene_nodes: Vec<NodeId>,
82    is_dirty: bool,
83    /// Tracks which mouse buttons are currently pressed
84    buttons_pressed: PointerButtons,
85    /// Tracks which nodes were hit on PointerDown (by stable NodeId).
86    ///
87    /// This follows Jetpack Compose's HitPathTracker pattern:
88    /// - On Down: cache NodeIds, not geometry
89    /// - On Move/Up/Cancel: resolve fresh HitTargets from current scene
90    /// - Handler closures are preserved (same Rc), so internal state survives
91    hit_path_tracker: HitPathTracker,
92    /// Tracks which nodes the pointer is currently hovering over.
93    /// Used to synthesize Enter/Exit events when the hover set changes.
94    hovered_nodes: Vec<NodeId>,
95    /// Persistent clipboard for desktop (Linux X11 requires clipboard to stay alive)
96    #[cfg(all(
97        feature = "clipboard-native",
98        not(target_arch = "wasm32"),
99        not(target_os = "android"),
100        not(target_os = "ios")
101    ))]
102    clipboard: Option<arboard::Clipboard>,
103    /// Dev options for debugging and performance monitoring
104    dev_options: DevOptions,
105    dev_overlay_controls: Vec<DevOverlayControl>,
106    dev_overlay_text: String,
107    dev_overlay_last_refresh: Option<Instant>,
108    dev_overlay_viewport: Option<Size>,
109    fps_monitor: fps_monitor::FpsMonitor,
110    frame_scheduler: FrameScheduler,
111}
112
113fn update_stage_telemetry_threshold_ms() -> Option<f64> {
114    static THRESHOLD_MS: std::sync::OnceLock<Option<f64>> = std::sync::OnceLock::new();
115    *THRESHOLD_MS.get_or_init(|| {
116        std::env::var("CRANPOSE_UPDATE_STAGE_TELEMETRY_MS")
117            .ok()
118            .and_then(|value| value.parse::<f64>().ok())
119            .filter(|value| value.is_finite() && *value >= 0.0)
120    })
121}
122
123#[derive(Clone, Copy, Debug)]
124struct UpdateStageTelemetry {
125    started_at: Instant,
126    after_frame_callbacks: Instant,
127    after_ui_drain: Instant,
128    after_reconcile: Instant,
129    after_process_frame: Instant,
130    should_render: bool,
131    reconcile_attempted: bool,
132    reconcile_changed: bool,
133}
134
135fn log_update_stage_telemetry(telemetry: UpdateStageTelemetry) {
136    let Some(threshold_ms) = update_stage_telemetry_threshold_ms() else {
137        return;
138    };
139    let total_ms = telemetry
140        .after_process_frame
141        .duration_since(telemetry.started_at)
142        .as_secs_f64()
143        * 1000.0;
144    if total_ms < threshold_ms {
145        return;
146    }
147
148    let frame_callbacks_ms = telemetry
149        .after_frame_callbacks
150        .duration_since(telemetry.started_at)
151        .as_secs_f64()
152        * 1000.0;
153    let ui_drain_ms = telemetry
154        .after_ui_drain
155        .duration_since(telemetry.after_frame_callbacks)
156        .as_secs_f64()
157        * 1000.0;
158    let reconcile_ms = telemetry
159        .after_reconcile
160        .duration_since(telemetry.after_ui_drain)
161        .as_secs_f64()
162        * 1000.0;
163    let process_frame_ms = telemetry
164        .after_process_frame
165        .duration_since(telemetry.after_reconcile)
166        .as_secs_f64()
167        * 1000.0;
168    eprintln!(
169        "[update-stage-telemetry] total_ms={total_ms:.2} frame_callbacks_ms={frame_callbacks_ms:.2} ui_drain_ms={ui_drain_ms:.2} reconcile_ms={reconcile_ms:.2} process_frame_ms={process_frame_ms:.2} should_render={} reconcile_attempted={} reconcile_changed={}",
170        telemetry.should_render,
171        telemetry.reconcile_attempted,
172        telemetry.reconcile_changed
173    );
174}
175
176#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
177pub enum FramePacingMode {
178    /// Pace frames to the display refresh interval. The production default:
179    /// animations advance once per vsync instead of re-rendering uncapped.
180    #[default]
181    Vsync,
182    Hard60,
183    Hard120,
184    /// Render as fast as possible. For perf harnesses and robot drivers that
185    /// measure work throughput; saturates the GPU if used in a real app.
186    NoVsync,
187}
188
189impl FramePacingMode {
190    pub const ALL: [Self; 4] = [Self::Vsync, Self::Hard60, Self::Hard120, Self::NoVsync];
191
192    pub fn label(self) -> &'static str {
193        match self {
194            Self::Vsync => "VSync",
195            Self::Hard60 => "60fps",
196            Self::Hard120 => "120fps",
197            Self::NoVsync => "NoVSync",
198        }
199    }
200
201    pub fn target_fps(self) -> Option<u32> {
202        match self {
203            Self::Hard60 => Some(60),
204            Self::Hard120 => Some(120),
205            Self::Vsync | Self::NoVsync => None,
206        }
207    }
208}
209
210#[derive(Clone, Copy, Debug, PartialEq)]
211pub struct FrameSchedule {
212    pub needs_update: bool,
213    pub needs_frame: bool,
214    pub next_deadline: Option<web_time::Instant>,
215}
216
217#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
218pub struct FrameUpdateResult {
219    pub visual_changed: bool,
220    pub structure_changed: bool,
221}
222
223pub trait PlatformFrameDriver {
224    fn request_frame(&self);
225    fn request_wake_at(&self, deadline: web_time::Instant);
226    fn clear_wake(&self);
227}
228
229#[derive(Debug)]
230pub struct FrameScheduler {
231    update_pending: AtomicBool,
232    frame_pending: AtomicBool,
233    next_deadline: Mutex<Option<web_time::Instant>>,
234}
235
236impl Default for FrameScheduler {
237    fn default() -> Self {
238        Self {
239            update_pending: AtomicBool::new(false),
240            frame_pending: AtomicBool::new(false),
241            next_deadline: Mutex::new(None),
242        }
243    }
244}
245
246impl FrameScheduler {
247    fn lock_deadline(&self) -> MutexGuard<'_, Option<web_time::Instant>> {
248        self.next_deadline
249            .lock()
250            .unwrap_or_else(|poisoned| poisoned.into_inner())
251    }
252
253    pub fn record(&self, schedule: FrameSchedule) {
254        self.update_pending
255            .store(schedule.needs_update, Ordering::SeqCst);
256        self.frame_pending
257            .store(schedule.needs_frame, Ordering::SeqCst);
258        let mut next_deadline = self.lock_deadline();
259        *next_deadline = if schedule.needs_update {
260            None
261        } else {
262            schedule.next_deadline
263        };
264    }
265
266    pub fn schedule<D>(&self, schedule: FrameSchedule, driver: &D)
267    where
268        D: PlatformFrameDriver + ?Sized,
269    {
270        self.record(schedule);
271        schedule.apply_to(driver);
272    }
273
274    pub fn snapshot(&self) -> FrameSchedule {
275        FrameSchedule {
276            needs_update: self.update_pending.load(Ordering::SeqCst),
277            needs_frame: self.frame_pending.load(Ordering::SeqCst),
278            next_deadline: *self.lock_deadline(),
279        }
280    }
281}
282
283impl FrameSchedule {
284    pub fn apply_to<D>(self, driver: &D)
285    where
286        D: PlatformFrameDriver + ?Sized,
287    {
288        if self.needs_frame {
289            driver.clear_wake();
290            driver.request_frame();
291        } else if self.needs_update {
292            driver.request_wake_at(web_time::Instant::now());
293        } else if let Some(deadline) = self.next_deadline {
294            driver.request_wake_at(deadline);
295        } else {
296            driver.clear_wake();
297        }
298    }
299}
300
301#[derive(Clone, Copy, Debug)]
302struct DevOverlayControl {
303    bounds: Rect,
304    mode: FramePacingMode,
305}
306
307/// Development options for debugging and performance monitoring.
308///
309/// These are rendered directly by the renderer (not via composition)
310/// to avoid affecting performance measurements.
311#[derive(Clone, Debug, Default)]
312pub struct DevOptions {
313    /// Show FPS counter overlay
314    pub fps_counter: bool,
315    /// Show recomposition count
316    pub recomposition_counter: bool,
317    /// Show layout timing breakdown
318    pub layout_timing: bool,
319    pub frame_pacing_controls: bool,
320    pub frame_pacing_mode: FramePacingMode,
321}
322
323#[cfg(any(test, feature = "test-support"))]
324#[doc(hidden)]
325#[derive(Clone, Copy, Debug)]
326pub struct RuntimeLeakDebugStats {
327    pub applier_stats: MemoryApplierDebugStats,
328    pub live_node_heap_bytes: usize,
329    pub recycled_node_heap_bytes: usize,
330    pub slot_table_heap_bytes: usize,
331    pub pass_stats: CompositionPassDebugStats,
332    pub slot_stats: SlotTableDebugStats,
333    pub observer_stats: SnapshotStateObserverDebugStats,
334    pub runtime_stats: RuntimeDebugStats,
335    pub state_arena_stats: StateArenaDebugStats,
336    pub recompose_scope_stats: RecomposeScopeRegistryDebugStats,
337    pub snapshot_v2_stats: SnapshotV2DebugStats,
338    pub snapshot_pinning_stats: SnapshotPinningDebugStats,
339}
340
341impl<R> AppShell<R>
342where
343    R: Renderer,
344    R::Error: Debug,
345{
346    pub fn new(renderer: R, root_key: Key, content: impl FnMut() + 'static) -> Self {
347        Self::new_with_size(renderer, root_key, content, (800, 600), (800.0, 600.0))
348    }
349
350    pub fn new_with_size(
351        renderer: R,
352        root_key: Key,
353        content: impl FnMut() + 'static,
354        buffer_size: (u32, u32),
355        viewport: (f32, f32),
356    ) -> Self {
357        Self::new_with_size_and_density(renderer, root_key, content, buffer_size, viewport, 1.0)
358    }
359
360    pub fn new_with_size_and_density(
361        mut renderer: R,
362        root_key: Key,
363        content: impl FnMut() + 'static,
364        buffer_size: (u32, u32),
365        viewport: (f32, f32),
366        density: f32,
367    ) -> Self {
368        let app_context = cranpose_ui::AppContext::new_with_density(density);
369        let runtime = StdRuntime::new();
370        let mut composition = Composition::with_runtime(MemoryApplier::new(), runtime.runtime());
371        let mut build: Box<dyn FnMut()> = Box::new(content);
372        renderer.attach_app_context_services(&app_context);
373        app_context.enter(|| {
374            if let Err(err) = composition.render_stable(root_key, &mut *build) {
375                log::error!("initial render failed: {err}");
376            }
377        });
378        renderer.scene_mut().clear();
379        let mut shell = Self {
380            app_context,
381            runtime,
382            composition,
383            content: build,
384            renderer,
385            cursor: (0.0, 0.0),
386            viewport,
387            buffer_size,
388            start_time: Instant::now(),
389            last_frame_time_nanos: 0,
390            layout_tree: None,
391            semantics_tree: None,
392            semantics_enabled: false,
393            layout_requested: true,
394            force_layout_pass: true,
395            scene_dirty: true,
396            scoped_layout_scene_nodes: Vec::new(),
397            is_dirty: true,
398            buttons_pressed: PointerButtons::NONE,
399            hit_path_tracker: HitPathTracker::new(),
400            hovered_nodes: Vec::new(),
401            #[cfg(all(
402                feature = "clipboard-native",
403                not(target_arch = "wasm32"),
404                not(target_os = "android"),
405                not(target_os = "ios")
406            ))]
407            clipboard: arboard::Clipboard::new().ok(),
408            dev_options: DevOptions::default(),
409            dev_overlay_controls: Vec::new(),
410            dev_overlay_text: String::new(),
411            dev_overlay_last_refresh: None,
412            dev_overlay_viewport: None,
413            fps_monitor: fps_monitor::FpsMonitor::new(),
414            frame_scheduler: FrameScheduler::default(),
415        };
416        shell.process_frame();
417        shell
418    }
419
420    /// Set development options for debugging and performance monitoring.
421    ///
422    /// The FPS counter and other overlays are rendered directly by the renderer
423    /// (not via composition) to avoid affecting performance measurements.
424    pub fn set_dev_options(&mut self, options: DevOptions) {
425        self.dev_options = options;
426        self.invalidate_dev_overlay_text();
427        let app_context = Rc::clone(&self.app_context);
428        app_context.enter(request_render_invalidation);
429        self.mark_dirty();
430    }
431
432    /// Get a reference to the current dev options.
433    pub fn dev_options(&self) -> &DevOptions {
434        &self.dev_options
435    }
436
437    pub fn frame_pacing_mode(&self) -> FramePacingMode {
438        self.dev_options.frame_pacing_mode
439    }
440
441    pub fn current_fps(&self) -> f32 {
442        self.fps_monitor.current_fps()
443    }
444
445    pub fn fps_stats(&self) -> FpsStats {
446        self.fps_monitor.stats()
447    }
448
449    pub fn reset_fps_stats(&mut self) {
450        self.fps_monitor.reset_stats();
451        self.invalidate_dev_overlay_text();
452    }
453
454    pub fn record_presented_frame(
455        &mut self,
456        frame_started_at: Instant,
457        frame_finished_at: Instant,
458    ) {
459        self.fps_monitor
460            .record_frame_work(frame_started_at, frame_finished_at);
461    }
462
463    #[cfg(any(test, feature = "test-support"))]
464    #[doc(hidden)]
465    pub fn record_presented_frame_for_test(
466        &mut self,
467        frame_started_nanos: u64,
468        frame_finished_nanos: u64,
469    ) {
470        let started = self.start_time + std::time::Duration::from_nanos(frame_started_nanos);
471        let finished = self.start_time + std::time::Duration::from_nanos(frame_finished_nanos);
472        self.record_presented_frame(started, finished);
473    }
474
475    pub fn set_frame_pacing_mode(&mut self, mode: FramePacingMode) {
476        if self.dev_options.frame_pacing_mode == mode {
477            return;
478        }
479        self.dev_options.frame_pacing_mode = mode;
480        self.invalidate_dev_overlay_text();
481        let app_context = Rc::clone(&self.app_context);
482        app_context.enter(request_render_invalidation);
483        self.mark_dirty();
484    }
485
486    pub fn handle_dev_overlay_click(&mut self, x: f32, y: f32) -> Option<FramePacingMode> {
487        if !self.dev_options.frame_pacing_controls {
488            return None;
489        }
490        let mode = self
491            .dev_overlay_controls
492            .iter()
493            .find(|control| control.bounds.contains(x, y))
494            .map(|control| control.mode)?;
495        self.set_frame_pacing_mode(mode);
496        Some(mode)
497    }
498
499    fn invalidate_dev_overlay_text(&mut self) {
500        self.dev_overlay_text.clear();
501        self.dev_overlay_last_refresh = None;
502        self.dev_overlay_viewport = None;
503    }
504
505    pub fn set_viewport(&mut self, width: f32, height: f32) {
506        self.viewport = (width, height);
507        self.request_forced_layout_pass();
508        self.mark_dirty();
509        self.process_frame();
510    }
511
512    pub fn viewport_size(&self) -> (f32, f32) {
513        self.viewport
514    }
515
516    pub fn set_buffer_size(&mut self, width: u32, height: u32) {
517        self.buffer_size = (width, height);
518    }
519
520    pub fn buffer_size(&self) -> (u32, u32) {
521        self.buffer_size
522    }
523
524    pub fn scene(&self) -> &R::Scene {
525        self.renderer.scene()
526    }
527
528    pub fn renderer(&mut self) -> &mut R {
529        &mut self.renderer
530    }
531
532    #[cfg(not(target_arch = "wasm32"))]
533    pub fn set_frame_waker(&mut self, waker: impl Fn() + Send + Sync + 'static) {
534        self.runtime.set_frame_waker(waker);
535    }
536
537    #[cfg(target_arch = "wasm32")]
538    pub fn set_frame_waker(&mut self, waker: impl Fn() + 'static) {
539        self.runtime.set_frame_waker(waker);
540    }
541
542    pub fn clear_frame_waker(&mut self) {
543        self.runtime.clear_frame_waker();
544    }
545
546    pub fn should_render(&self) -> bool {
547        let app_context = Rc::clone(&self.app_context);
548        app_context.enter(|| {
549            if self.layout_requested
550                || self.scene_dirty
551                || peek_render_invalidation()
552                || peek_pointer_invalidation()
553                || peek_focus_invalidation()
554                || peek_layout_invalidation()
555            {
556                return true;
557            }
558            self.composition.should_render()
559        })
560    }
561
562    fn needs_ui_update_in_context(&self) -> bool {
563        if self.is_dirty
564            || self.layout_requested
565            || self.scene_dirty
566            || peek_render_invalidation()
567            || peek_pointer_invalidation()
568            || peek_focus_invalidation()
569            || peek_layout_invalidation()
570            || cranpose_ui::has_pending_layout_repasses()
571            || cranpose_ui::has_pending_draw_repasses()
572            || has_pending_pointer_repasses()
573            || has_pending_focus_invalidations()
574        {
575            return true;
576        }
577
578        self.composition.should_render()
579    }
580
581    pub fn needs_update(&self) -> bool {
582        let app_context = Rc::clone(&self.app_context);
583        app_context.enter(|| self.needs_ui_update_in_context())
584    }
585
586    /// Returns true if the shell needs to redraw (dirty flag, layout dirty, active animations).
587    /// Note: Cursor blink is now timer-based and uses WaitUntil scheduling, not continuous redraw.
588    pub fn needs_redraw(&self) -> bool {
589        let app_context = Rc::clone(&self.app_context);
590        app_context
591            .enter(|| self.needs_ui_update_in_context() || self.renderer.needs_frame_warmup())
592    }
593
594    /// Marks the shell as dirty, indicating a redraw is needed.
595    pub fn mark_dirty(&mut self) {
596        self.is_dirty = true;
597    }
598
599    pub fn request_root_render(&mut self) {
600        self.composition.request_root_render();
601        self.request_forced_layout_pass();
602        let app_context = Rc::clone(&self.app_context);
603        app_context.enter(request_render_invalidation);
604        self.mark_dirty();
605    }
606
607    pub fn set_density(&mut self, density: f32) {
608        let app_context = Rc::clone(&self.app_context);
609        let changed = app_context.enter(|| {
610            let previous = cranpose_ui::current_density().to_bits();
611            cranpose_ui::set_density(density);
612            previous != cranpose_ui::current_density().to_bits()
613        });
614        if changed {
615            self.request_forced_layout_pass();
616            self.mark_dirty();
617        }
618    }
619
620    #[cfg(any(test, feature = "test-support"))]
621    #[doc(hidden)]
622    pub fn debug_current_density(&self) -> f32 {
623        let app_context = Rc::clone(&self.app_context);
624        app_context.enter(cranpose_ui::current_density)
625    }
626
627    #[cfg(any(test, feature = "test-support"))]
628    #[doc(hidden)]
629    pub fn debug_enter_app_context<T>(&self, block: impl FnOnce() -> T) -> T {
630        let app_context = Rc::clone(&self.app_context);
631        app_context.enter(block)
632    }
633
634    fn request_layout_pass(&mut self) {
635        self.layout_requested = true;
636    }
637
638    fn request_forced_layout_pass(&mut self) {
639        self.layout_requested = true;
640        self.force_layout_pass = true;
641    }
642
643    fn composition_tree_needs_layout(&mut self) -> bool {
644        let Some(root) = self.composition.root() else {
645            return true;
646        };
647        let mut applier = self.composition.applier_mut();
648        cranpose_ui::tree_needs_layout(&mut *applier, root).unwrap_or_else(|err| {
649            log::warn!(
650                "Cannot check layout dirty status for root #{}: {}",
651                root,
652                err
653            );
654            true
655        })
656    }
657
658    /// Returns true if there are active animations or pending recompositions.
659    pub fn has_active_animations(&self) -> bool {
660        self.composition.should_render()
661    }
662
663    pub fn has_active_pointer_gesture(&self) -> bool {
664        self.buttons_pressed != PointerButtons::NONE
665            && self.hit_path_tracker.has_path(PointerId::PRIMARY)
666    }
667
668    /// Returns the next scheduled event time for cursor blink.
669    /// Use this for `ControlFlow::WaitUntil` scheduling.
670    pub fn next_event_time(&self) -> Option<web_time::Instant> {
671        let app_context = Rc::clone(&self.app_context);
672        app_context.enter(cranpose_ui::next_cursor_blink_time)
673    }
674
675    fn compute_frame_schedule(&self) -> FrameSchedule {
676        let needs_update = self.needs_update();
677        let needs_frame = needs_update
678            || self.has_active_animations()
679            || self.has_active_pointer_gesture()
680            || self.renderer.needs_frame_warmup();
681        FrameSchedule {
682            needs_update,
683            needs_frame,
684            next_deadline: self.next_event_time(),
685        }
686    }
687
688    pub fn frame_schedule(&self) -> FrameSchedule {
689        let schedule = self.compute_frame_schedule();
690        self.frame_scheduler.record(schedule);
691        schedule
692    }
693
694    pub fn schedule_platform_frame<D>(&self, driver: &D) -> FrameSchedule
695    where
696        D: PlatformFrameDriver + ?Sized,
697    {
698        let schedule = self.compute_frame_schedule();
699        self.frame_scheduler.schedule(schedule, driver);
700        schedule
701    }
702
703    pub fn frame_scheduler_snapshot(&self) -> FrameSchedule {
704        self.frame_scheduler.snapshot()
705    }
706
707    fn frame_time_nanos_at(&self, now: Instant) -> u64 {
708        now.checked_duration_since(self.start_time)
709            .unwrap_or_default()
710            .as_nanos()
711            .min(u128::from(u64::MAX)) as u64
712    }
713
714    pub fn update_after_frame_interval(
715        &mut self,
716        frame_interval: std::time::Duration,
717    ) -> FrameUpdateResult {
718        let wall_frame_time = self.frame_time_nanos_at(Instant::now());
719        let base_frame_time = self.last_frame_time_nanos.max(wall_frame_time);
720        let frame_time = base_frame_time
721            .saturating_add(frame_interval.as_nanos().min(u128::from(u64::MAX)) as u64);
722        self.update_at_frame_time_nanos(frame_time)
723    }
724
725    pub fn update_at_frame_time_nanos(&mut self, frame_time: u64) -> FrameUpdateResult {
726        let app_context = Rc::clone(&self.app_context);
727        app_context.enter(|| {
728            let update_started_at = Instant::now();
729            let frame_time = frame_time.max(self.last_frame_time_nanos);
730            self.last_frame_time_nanos = frame_time;
731            let runtime_handle = self.runtime.runtime_handle();
732            runtime_handle.with_deferred_state_releases(|| {
733                self.runtime.drain_frame_callbacks(frame_time);
734                let after_frame_callbacks = Instant::now();
735                runtime_handle.drain_ui();
736                let after_ui_drain = Instant::now();
737                let should_render = self.composition.should_render();
738                let mut reconcile_attempted = false;
739                let mut reconcile_changed = false;
740                if should_render {
741                    log::trace!(
742                        target: "cranpose::input",
743                        "update begin: should_render=true layout_requested={} scene_dirty={} is_dirty={}",
744                        self.layout_requested,
745                        self.scene_dirty,
746                        self.is_dirty
747                    );
748                }
749                if should_render {
750                    let Some(root_key) = self.composition.root_key() else {
751                        let result = self.process_frame_in_context(reconcile_changed);
752                        let after_process_frame = Instant::now();
753                        log_update_stage_telemetry(UpdateStageTelemetry {
754                            started_at: update_started_at,
755                            after_frame_callbacks,
756                            after_ui_drain,
757                            after_reconcile: after_ui_drain,
758                            after_process_frame,
759                            should_render,
760                            reconcile_attempted,
761                            reconcile_changed,
762                        });
763                        self.is_dirty = false;
764                        return result;
765                    };
766                    reconcile_attempted = true;
767                    match self.composition.reconcile(root_key, &mut *self.content) {
768                        Ok(changed) => {
769                            reconcile_changed = changed;
770                            log::trace!(
771                                target: "cranpose::input",
772                                "reconcile changed={changed}"
773                            );
774                            if changed {
775                                self.fps_monitor.record_recomposition();
776                                if self.composition_tree_needs_layout() {
777                                    self.request_layout_pass();
778                                }
779                                request_render_invalidation();
780                            }
781                        }
782                        Err(NodeError::Missing { id }) => {
783                            log::debug!("Recomposition skipped: node {} no longer exists", id);
784                            self.request_layout_pass();
785                            request_render_invalidation();
786                        }
787                        Err(err) => {
788                            log::error!("recomposition failed: {err}");
789                            self.request_layout_pass();
790                            request_render_invalidation();
791                        }
792                    }
793                }
794                let after_reconcile = Instant::now();
795                let result = self.process_frame_in_context(reconcile_changed);
796                let after_process_frame = Instant::now();
797                log_update_stage_telemetry(UpdateStageTelemetry {
798                    started_at: update_started_at,
799                    after_frame_callbacks,
800                    after_ui_drain,
801                    after_reconcile,
802                    after_process_frame,
803                    should_render,
804                    reconcile_attempted,
805                    reconcile_changed,
806                });
807                self.is_dirty = false;
808                result
809            })
810        })
811    }
812
813    pub fn update(&mut self) -> FrameUpdateResult {
814        let frame_time = self.frame_time_nanos_at(Instant::now());
815        self.update_at_frame_time_nanos(frame_time)
816    }
817}
818
819impl<R> Drop for AppShell<R>
820where
821    R: Renderer,
822{
823    fn drop(&mut self) {
824        self.runtime.clear_frame_waker();
825    }
826}
827
828pub fn default_root_key() -> Key {
829    location_key(file!(), line!(), column!())
830}
831
832#[cfg(test)]
833mod frame_pacing_tests {
834    use super::{FramePacingMode, FrameSchedule, FrameScheduler, PlatformFrameDriver};
835    use std::cell::RefCell;
836    use std::panic::{catch_unwind, AssertUnwindSafe};
837    use std::time::Duration;
838    use web_time::Instant;
839
840    #[derive(Clone, Copy, Debug, PartialEq)]
841    enum DriverCall {
842        RequestFrame,
843        RequestWakeAt(Instant),
844        ClearWake,
845    }
846
847    #[derive(Default)]
848    struct RecordingFrameDriver {
849        calls: RefCell<Vec<DriverCall>>,
850    }
851
852    impl RecordingFrameDriver {
853        fn calls(&self) -> Vec<DriverCall> {
854            self.calls.borrow().clone()
855        }
856    }
857
858    impl PlatformFrameDriver for RecordingFrameDriver {
859        fn request_frame(&self) {
860            self.calls.borrow_mut().push(DriverCall::RequestFrame);
861        }
862
863        fn request_wake_at(&self, deadline: Instant) {
864            self.calls
865                .borrow_mut()
866                .push(DriverCall::RequestWakeAt(deadline));
867        }
868
869        fn clear_wake(&self) {
870            self.calls.borrow_mut().push(DriverCall::ClearWake);
871        }
872    }
873
874    #[test]
875    fn frame_pacing_labels_match_overlay_modes() {
876        assert_eq!(FramePacingMode::Vsync.label(), "VSync");
877        assert_eq!(FramePacingMode::Hard60.label(), "60fps");
878        assert_eq!(FramePacingMode::Hard120.label(), "120fps");
879        assert_eq!(FramePacingMode::NoVsync.label(), "NoVSync");
880    }
881
882    #[test]
883    fn only_hard_modes_have_fixed_targets() {
884        assert_eq!(FramePacingMode::Vsync.target_fps(), None);
885        assert_eq!(FramePacingMode::Hard60.target_fps(), Some(60));
886        assert_eq!(FramePacingMode::Hard120.target_fps(), Some(120));
887        assert_eq!(FramePacingMode::NoVsync.target_fps(), None);
888    }
889
890    #[test]
891    fn frame_schedule_requests_immediate_frame_and_clears_deadline() {
892        let driver = RecordingFrameDriver::default();
893        let deadline = Instant::now() + Duration::from_millis(25);
894
895        FrameSchedule {
896            needs_update: true,
897            needs_frame: true,
898            next_deadline: Some(deadline),
899        }
900        .apply_to(&driver);
901
902        assert_eq!(
903            driver.calls(),
904            vec![DriverCall::ClearWake, DriverCall::RequestFrame]
905        );
906    }
907
908    #[test]
909    fn frame_schedule_requests_deadline_when_idle_until_timer() {
910        let driver = RecordingFrameDriver::default();
911        let deadline = Instant::now() + Duration::from_millis(25);
912
913        FrameSchedule {
914            needs_update: false,
915            needs_frame: false,
916            next_deadline: Some(deadline),
917        }
918        .apply_to(&driver);
919
920        assert_eq!(driver.calls(), vec![DriverCall::RequestWakeAt(deadline)]);
921    }
922
923    #[test]
924    fn frame_schedule_wakes_without_requesting_frame_for_update_only_work() {
925        let driver = RecordingFrameDriver::default();
926        let before = Instant::now();
927
928        FrameSchedule {
929            needs_update: true,
930            needs_frame: false,
931            next_deadline: None,
932        }
933        .apply_to(&driver);
934
935        let calls = driver.calls();
936        assert_eq!(calls.len(), 1);
937        match calls[0] {
938            DriverCall::RequestWakeAt(deadline) => {
939                assert!(deadline >= before);
940            }
941            other => panic!("update-only work must wake without requesting a frame: {other:?}"),
942        }
943    }
944
945    #[test]
946    fn frame_schedule_clears_wake_when_fully_idle() {
947        let driver = RecordingFrameDriver::default();
948
949        FrameSchedule {
950            needs_update: false,
951            needs_frame: false,
952            next_deadline: None,
953        }
954        .apply_to(&driver);
955
956        assert_eq!(driver.calls(), vec![DriverCall::ClearWake]);
957    }
958
959    #[test]
960    fn frame_scheduler_records_latest_schedule_and_applies_driver() {
961        let scheduler = FrameScheduler::default();
962        let driver = RecordingFrameDriver::default();
963        let deadline = Instant::now() + Duration::from_millis(25);
964
965        scheduler.schedule(
966            FrameSchedule {
967                needs_update: false,
968                needs_frame: false,
969                next_deadline: Some(deadline),
970            },
971            &driver,
972        );
973
974        assert_eq!(
975            scheduler.snapshot(),
976            FrameSchedule {
977                needs_update: false,
978                needs_frame: false,
979                next_deadline: Some(deadline),
980            }
981        );
982        assert_eq!(driver.calls(), vec![DriverCall::RequestWakeAt(deadline)]);
983    }
984
985    #[test]
986    fn frame_scheduler_clears_deadline_for_immediate_frame() {
987        let scheduler = FrameScheduler::default();
988        let driver = RecordingFrameDriver::default();
989        let deadline = Instant::now() + Duration::from_millis(25);
990
991        scheduler.schedule(
992            FrameSchedule {
993                needs_update: true,
994                needs_frame: true,
995                next_deadline: Some(deadline),
996            },
997            &driver,
998        );
999
1000        assert_eq!(
1001            scheduler.snapshot(),
1002            FrameSchedule {
1003                needs_update: true,
1004                needs_frame: true,
1005                next_deadline: None,
1006            }
1007        );
1008        assert_eq!(
1009            driver.calls(),
1010            vec![DriverCall::ClearWake, DriverCall::RequestFrame]
1011        );
1012    }
1013
1014    #[test]
1015    fn frame_scheduler_recovers_poisoned_deadline_lock() {
1016        let scheduler = FrameScheduler::default();
1017        let deadline = Instant::now() + Duration::from_millis(25);
1018
1019        let _ = catch_unwind(AssertUnwindSafe(|| {
1020            let _guard = scheduler.lock_deadline();
1021            panic!("poison frame scheduler deadline lock");
1022        }));
1023
1024        scheduler.record(FrameSchedule {
1025            needs_update: false,
1026            needs_frame: false,
1027            next_deadline: Some(deadline),
1028        });
1029
1030        assert_eq!(
1031            scheduler.snapshot(),
1032            FrameSchedule {
1033                needs_update: false,
1034                needs_frame: false,
1035                next_deadline: Some(deadline),
1036            }
1037        );
1038    }
1039}
1040
1041#[cfg(test)]
1042#[path = "tests/app_shell_tests.rs"]
1043mod tests;