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