Skip to main content

cranpose_app_shell/
lib.rs

1#![deny(missing_docs)]
2#![doc = include_str!("../README.md")]
3
4mod focus_reveal;
5mod fps_monitor;
6mod frame_rate_boost;
7mod hit_path_tracker;
8pub mod inspector;
9mod modal_focus;
10#[cfg(debug_assertions)]
11mod semantics_check;
12mod shell_debug;
13mod shell_frame;
14mod shell_input;
15mod surface;
16mod touch_router;
17mod wheel;
18use std::{
19    cell::{Cell, RefCell},
20    fmt::{Debug, Write},
21    rc::Rc,
22};
23
24use cranpose_core::{
25    Applier, Composition, Key, MemoryApplier, NodeError, NodeId, NodeMarks,
26    SceneNodeAttachmentScratch, collections::map::HashSet, enter_event_handler_scope, location_key,
27    run_in_mutable_snapshot,
28};
29pub use cranpose_foundation::{
30    DEFAULT_ROTARY_SCROLL_FACTOR_DP, Modifiers, PointerSource, RotaryScrollEvent,
31    rotary_scroll_pixels_from_detents,
32};
33use cranpose_foundation::{PointerButton, PointerButtons, PointerEvent, PointerEventKind};
34use cranpose_render_common::{HitTestTarget, RenderScene, Renderer, SceneUpdates};
35use cranpose_runtime_std::StdRuntime;
36use cranpose_ui::{
37    HeadlessRenderer, LayoutBox, LayoutNode, LayoutTree, MeasureLayoutOptions, SemanticsTree,
38    SubcomposeLayoutNode, WindowRootEntry, format_layout_tree, format_render_scene,
39    format_screen_summary, process_focus_invalidations, process_pointer_repasses,
40    process_semantics_invalidations, request_render_invalidation,
41};
42pub use cranpose_ui::{KeyCode, KeyEvent, KeyEventType};
43use cranpose_ui_graphics::{Point, PointerIcon, Rect, Size};
44pub use fps_monitor::FpsStats;
45pub use frame_rate_boost::FrameRateBoost;
46use hit_path_tracker::PointerId;
47#[cfg(test)]
48use shell_frame::build_draw_refresh_scope;
49pub use surface::{RootId, RootSurface, SurfaceMut};
50use surface::{
51    SurfaceDirtyLane, TextInputRouter, TextInputRoutes, route_geometry_scene_nodes,
52    route_nodes_by_surface,
53};
54pub use touch_router::TouchRouter;
55use web_time::Instant;
56pub use wheel::WheelScroll;
57
58#[cfg(all(
59    feature = "clipboard-native",
60    not(target_arch = "wasm32"),
61    not(target_os = "android"),
62    not(target_os = "ios")
63))]
64#[derive(Default)]
65struct ShellClipboard {
66    /// The system clipboard, opened at the first copy or paste: opened at
67    /// launch, it held the main thread ~0.8 ms on an Apple M5, for apps that
68    /// never touch it too.
69    inner: std::cell::OnceCell<std::cell::RefCell<Option<arboard::Clipboard>>>,
70}
71
72#[cfg(all(
73    feature = "clipboard-native",
74    not(target_arch = "wasm32"),
75    not(target_os = "android"),
76    not(target_os = "ios")
77))]
78impl ShellClipboard {
79    /// `use_clipboard`'s answer for the system clipboard, opened on first
80    /// use; `None` when it cannot be opened.
81    fn with<T>(
82        &self,
83        use_clipboard: impl FnOnce(&mut arboard::Clipboard) -> Option<T>,
84    ) -> Option<T> {
85        self.inner
86            .get_or_init(|| std::cell::RefCell::new(arboard::Clipboard::new().ok()))
87            .borrow_mut()
88            .as_mut()
89            .and_then(use_clipboard)
90    }
91}
92
93#[cfg(all(
94    feature = "clipboard-native",
95    not(target_arch = "wasm32"),
96    not(target_os = "android"),
97    not(target_os = "ios")
98))]
99impl cranpose_ui::clipboard_session::PlatformClipboard for ShellClipboard {
100    fn write_text(&self, text: &str) {
101        self.with(|clipboard| clipboard.set_text(text).ok());
102    }
103
104    fn read_text(&self) -> Option<String> {
105        self.with(|clipboard| clipboard.get_text().ok())
106    }
107}
108#[cfg(any(test, feature = "test-support"))]
109use cranpose_core::{
110    CompositionPassDebugStats, SlotId,
111    runtime::{RuntimeDebugStats, StateArenaDebugStats},
112    snapshot_pinning::{SnapshotPinningDebugStats, debug_snapshot_pinning_stats},
113    snapshot_v2::{SnapshotV2DebugStats, debug_snapshot_v2_stats},
114};
115#[cfg(any(test, feature = "test-support"))]
116use cranpose_core::{
117    MemoryApplierDebugStats, RecomposeScopeRegistryDebugStats, SlotTableDebugStats,
118    debug_recompose_scope_registry_stats,
119};
120pub use cranpose_ui::{ImeEditorState, PlatformTextInputHandler};
121#[cfg(any(test, feature = "test-support"))]
122pub mod accessibility_audit;
123#[cfg(any(test, feature = "test-support"))]
124pub mod placed_semantics;
125
126/// How the platform should vote the display's frame rate on behalf of the app.
127///
128/// Compose apps get 120 Hz gameplay on a 120 Hz panel not by presenting faster
129/// but because HWUI votes a rate on the window while gestures run, content
130/// moves or the window redraws continuously, and clears it when they stop. A window that never votes is pinned by
131/// SurfaceFlinger's cadence inference instead — which also throttles the app's
132/// choreographer, so the inference reinforces itself. `Auto` reproduces the
133/// HWUI behaviour; the platform backends read it every frame and vote through
134/// the native window when the desired rate changes.
135#[derive(Debug, Clone, Copy, PartialEq, Default)]
136pub enum FrameRatePreference {
137    /// Ask for the panel's fastest rate while [`FrameRateBoost`] holds, a
138    /// quiet rate while frames arrive only intermittently, no preference
139    /// when the scene is still. This is the default, matching what Compose/HWUI do for every
140    /// app without the app's involvement.
141    #[default]
142    Auto,
143    /// Never vote; the OS infers a rate from presentation cadence.
144    NoPreference,
145    /// Always vote exactly this rate in Hz. Values `<= 0` behave like
146    /// [`FrameRatePreference::NoPreference`].
147    Exact(f32),
148}
149
150impl FrameRatePreference {
151    /// The baseline `Auto` votes while frames arrive without a boost — the
152    /// same rate HWUI's NORMAL frame-rate category resolves to on phone
153    /// panels. The quiet vote cannot simply be "no vote": SurfaceFlinger
154    /// infers a non-voting window's rate from whatever cadence it last
155    /// observed and pins it, so an app that ever ran the panel's fast rate
156    /// would stay there forever (measured on a Pixel 9 Pro, both directions).
157    pub const AUTO_QUIET_RATE_HZ: f32 = 60.0;
158
159    /// The rate the platform should vote right now, in Hz, where `0.0` means
160    /// "clear the vote". `producing_frames` is whether the frame loop has a
161    /// frame scheduled, `boosted` what [`FrameRateBoost::boosted`] says, and
162    /// `panel_max_hz` the display's fastest supported rate when the platform
163    /// knows it.
164    ///
165    /// While boosted, `Auto` holds the boost even through moments with no
166    /// frame scheduled: a gesture sequence crosses still screens (a tap lands,
167    /// the old scene stops animating, the new one hasn't started), and letting
168    /// each of those instantly clear the vote flapped the display between the
169    /// boost rate and no-vote several times per second on device. This mirrors
170    /// SurfaceFlinger's own touch boost, which also outlives the touch by
171    /// seconds regardless of what the app presents in between.
172    pub fn desired_rate_hz(
173        self,
174        producing_frames: bool,
175        boosted: bool,
176        panel_max_hz: Option<f32>,
177    ) -> f32 {
178        match self {
179            FrameRatePreference::Auto => {
180                if boosted {
181                    panel_max_hz
182                        .filter(|rate| *rate > 0.0)
183                        .unwrap_or(Self::AUTO_QUIET_RATE_HZ)
184                } else if producing_frames {
185                    Self::AUTO_QUIET_RATE_HZ
186                } else {
187                    0.0
188                }
189            }
190            FrameRatePreference::NoPreference => 0.0,
191            FrameRatePreference::Exact(rate) if rate > 0.0 => rate,
192            FrameRatePreference::Exact(_) => 0.0,
193        }
194    }
195}
196
197pub(crate) struct ShellApp {
198    pub(crate) app_context: Rc<cranpose_ui::AppContext>,
199    pub(crate) runtime: StdRuntime,
200    pub(crate) composition: Composition<MemoryApplier>,
201    pub(crate) content: Box<dyn FnMut()>,
202    pub(crate) start_time: Instant,
203    pub(crate) last_frame_time_nanos: u64,
204    pub(crate) semantics_enabled: bool,
205    pub(crate) semantics_snapshot_revision: u64,
206    pub(crate) revealed_focus: Option<NodeId>,
207    pub(crate) layout_requested: bool,
208    pub(crate) content_moved: bool,
209    pub(crate) force_layout_pass: bool,
210    pub(crate) modifiers: Option<Modifiers>,
211    pub(crate) rotary_scroll_factor: f32,
212    #[cfg(all(feature = "clipboard-native", target_os = "linux"))]
213    pub(crate) clipboard: Option<arboard::Clipboard>,
214    pub(crate) dev_options: DevOptions,
215    pub(crate) inspector_projector: Option<inspector::InspectorProjector>,
216    pub(crate) fps_monitor: fps_monitor::FpsMonitor,
217    pub(crate) text_input_routes: Rc<RefCell<TextInputRoutes>>,
218    pub(crate) text_input_router_installed: bool,
219    pub(crate) window_roots_seen: Option<u64>,
220    /// Frames with a rebuilt scene since draw observations were last pruned.
221    pub(crate) rebuilt_frames_since_observation_prune: u32,
222    /// The installed-font generation the renderers last took in.
223    #[cfg(feature = "installed-fonts")]
224    pub(crate) installed_fonts: usize,
225}
226
227/// The application: one runtime, one composition, and a surface per window
228/// it shows.
229///
230/// Methods that name no root act on the primary surface, so a platform with
231/// one window uses the shell as it always has. A platform showing more
232/// windows reaches the others through [`AppShell::surface`], after giving
233/// each window root a renderer with [`AppShell::add_window_surface`].
234pub struct AppShell<R>
235where
236    R: Renderer,
237{
238    pub(crate) app: ShellApp,
239    pub(crate) surfaces: Vec<RootSurface<R>>,
240    pub(crate) routing_scratch: SurfaceRoutingScratch,
241    pub(crate) pending_dirty_nodes: Vec<NodeId>,
242    pub(crate) pending_layer_property_nodes: Vec<NodeId>,
243    pub(crate) geometry_scene_nodes: cranpose_ui::GeometrySceneNodes,
244}
245
246#[derive(Default)]
247pub(crate) struct SurfaceRoutingScratch {
248    pub(crate) attached: Vec<Option<NodeId>>,
249    pub(crate) owners: Vec<Option<NodeId>>,
250    pub(crate) seen: HashSet<(usize, NodeId)>,
251    pub(crate) attachment: SceneNodeAttachmentScratch,
252    pub(crate) window_roots: cranpose_ui::WindowRootRoutingScratch,
253    /// The nodes a layout lane routing gave the surfaces, while the moved
254    /// lane after it is routed.
255    pub(crate) laid_out: NodeMarks,
256    /// Each surface's count of layout lane nodes before that routing.
257    pub(crate) laid_out_starts: Vec<usize>,
258    /// The moved nodes the moved lane routes.
259    pub(crate) moved: Vec<NodeId>,
260}
261
262#[derive(Clone, Copy, Debug, PartialEq, Eq)]
263/// Platform and animation-clock timestamps for one pointer sample.
264pub struct PointerEventTime {
265    /// Timestamp supplied by the platform, in its millisecond clock domain.
266    pub platform_time_ms: Option<i64>,
267    /// Timestamp in the animation frame-clock domain.
268    pub animation_time_nanos: u64,
269}
270
271fn update_stage_telemetry_threshold_ms() -> Option<f64> {
272    static THRESHOLD_MS: std::sync::OnceLock<Option<f64>> = std::sync::OnceLock::new();
273    *THRESHOLD_MS.get_or_init(|| {
274        std::env::var("CRANPOSE_UPDATE_STAGE_TELEMETRY_MS")
275            .ok()
276            .and_then(|value| value.parse::<f64>().ok())
277            .filter(|value| value.is_finite() && *value >= 0.0)
278    })
279}
280
281/// The stages of an update that its telemetry times: start, frame
282/// callbacks, UI drain, reconcile and frame processing.
283type UpdateStages = cranpose_core::StageTimes<5>;
284
285struct UpdateOutcome {
286    should_render: bool,
287    reconcile_attempted: bool,
288    reconcile_changed: bool,
289}
290
291fn log_update_stage_telemetry(stages: &UpdateStages, outcome: UpdateOutcome) {
292    let Some(threshold_ms) = update_stage_telemetry_threshold_ms() else {
293        return;
294    };
295    let Some(
296        [
297            started_at,
298            after_frame_callbacks,
299            after_ui_drain,
300            after_reconcile,
301            after_process_frame,
302        ],
303    ) = stages.times()
304    else {
305        return;
306    };
307    let ms = cranpose_core::stage_ms;
308    let total_ms = ms(started_at, after_process_frame);
309    if total_ms < threshold_ms {
310        return;
311    }
312    eprintln!(
313        "[update-stage-telemetry] total_ms={total_ms:.2} frame_callbacks_ms={:.2} ui_drain_ms={:.2} reconcile_ms={:.2} process_frame_ms={:.2} should_render={} reconcile_attempted={} reconcile_changed={}",
314        ms(started_at, after_frame_callbacks),
315        ms(after_frame_callbacks, after_ui_drain),
316        ms(after_ui_drain, after_reconcile),
317        ms(after_reconcile, after_process_frame),
318        outcome.should_render,
319        outcome.reconcile_attempted,
320        outcome.reconcile_changed
321    );
322}
323
324/// How the shell paces its frames against the display.
325#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
326pub enum FramePacingMode {
327    /// Pace frames to the display refresh interval. The production default:
328    /// animations advance once per vsync instead of re-rendering uncapped.
329    #[default]
330    Vsync,
331    /// Cap frames at 60 per second with a timer, whatever the display refresh rate is.
332    Hard60,
333    /// Cap frames at 120 per second with a timer, whatever the display refresh rate is.
334    Hard120,
335    /// Render as fast as possible. For perf harnesses and robot drivers that
336    /// measure work throughput; saturates the GPU if used in a real app.
337    NoVsync,
338}
339
340impl FramePacingMode {
341    /// Every pacing mode, in the order the dev overlay lists them.
342    pub const ALL: [Self; 4] = [Self::Vsync, Self::Hard60, Self::Hard120, Self::NoVsync];
343
344    /// Returns the short name the dev overlay shows for this mode.
345    pub fn label(self) -> &'static str {
346        match self {
347            Self::Vsync => "VSync",
348            Self::Hard60 => "60fps",
349            Self::Hard120 => "120fps",
350            Self::NoVsync => "NoVSync",
351        }
352    }
353
354    /// Returns the frames per second this mode caps at with a timer, or `None` when the mode sets
355    /// no cap.
356    pub fn target_fps(self) -> Option<u32> {
357        match self {
358            Self::Hard60 => Some(60),
359            Self::Hard120 => Some(120),
360            Self::Vsync | Self::NoVsync => None,
361        }
362    }
363}
364
365/// What the shell needs from the platform loop next: an update now, a display frame, or a wake at a
366/// later time. The default needs nothing.
367#[derive(Clone, Copy, Debug, Default, PartialEq)]
368pub struct FrameSchedule {
369    /// Whether queued UI tasks, state changes, layout or semantics work can run now, without
370    /// waiting for a display frame.
371    pub needs_update: bool,
372    /// Whether the shell wants a display frame because something on screen changed or is animating.
373    pub needs_frame: bool,
374    /// When the next timed event, such as a cursor blink, is due. `None` when no timed event is
375    /// waiting.
376    pub next_deadline: Option<web_time::Instant>,
377}
378
379/// What one shell update changed.
380#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
381pub struct FrameUpdateResult {
382    /// Whether the update changed what the surface draws, so a new frame needs to be presented.
383    pub visual_changed: bool,
384    /// Whether the update rebuilt the scene because layout or tree structure changed, not only
385    /// drawing.
386    pub structure_changed: bool,
387    /// Whether this frame's layout moved or resized a node: content in
388    /// motion, such as a scroll or a size animation. Compose votes its high
389    /// frame-rate category on exactly these changes, so the platforms boost
390    /// the display rate on them the way they do for input.
391    pub content_moved: bool,
392    /// Whether this frame drew new content rather than presenting the scene
393    /// it had: something drew, moved or changed, so the scene was rebuilt.
394    pub content_redrawn: bool,
395}
396
397/// The platform's way of waking its loop, used by [`FrameScheduler`] to carry out a
398/// [`FrameSchedule`].
399pub trait PlatformFrameDriver {
400    /// Asks the platform to deliver a display frame.
401    fn request_frame(&self);
402    /// Asks the platform to wake its loop at `deadline`. A deadline of the current time means wake
403    /// as soon as possible.
404    fn request_wake_at(&self, deadline: web_time::Instant);
405    /// Cancels the wake requested by `request_wake_at`.
406    fn clear_wake(&self);
407}
408
409/// Stores the latest [`FrameSchedule`] of a surface for the platform code that drives it. A
410/// surface lives on its shell's thread, so the schedule sits in a `Cell`: on ESP-IDF a `Mutex`
411/// is a FreeRTOS mutex, which every frame took.
412#[derive(Debug, Default)]
413pub struct FrameScheduler {
414    latest: Cell<FrameSchedule>,
415}
416
417impl FrameScheduler {
418    /// Stores `schedule` as the latest one. A schedule that needs an update drops its deadline.
419    pub fn record(&self, schedule: FrameSchedule) {
420        self.latest.set(FrameSchedule {
421            next_deadline: if schedule.needs_update {
422                None
423            } else {
424                schedule.next_deadline
425            },
426            ..schedule
427        });
428    }
429
430    /// Stores `schedule` and asks `driver` for the frame or wake it calls for.
431    pub fn schedule<D>(&self, schedule: FrameSchedule, driver: &D)
432    where
433        D: PlatformFrameDriver + ?Sized,
434    {
435        self.record(schedule);
436        schedule.apply_to(driver);
437    }
438
439    /// Returns the latest stored schedule.
440    pub fn snapshot(&self) -> FrameSchedule {
441        self.latest.get()
442    }
443}
444
445impl FrameSchedule {
446    /// Whether the shell asks for nothing more: no frame, no update and no timed event.
447    pub fn is_idle(self) -> bool {
448        !self.needs_frame && !self.needs_update && self.next_deadline.is_none()
449    }
450
451    /// Asks `driver` for what this schedule calls for: a frame, an immediate wake, a wake at the
452    /// deadline, or no wake.
453    pub fn apply_to<D>(self, driver: &D)
454    where
455        D: PlatformFrameDriver + ?Sized,
456    {
457        if self.needs_frame {
458            driver.clear_wake();
459            driver.request_frame();
460        } else if self.needs_update {
461            driver.request_wake_at(web_time::Instant::now());
462        } else if let Some(deadline) = self.next_deadline {
463            driver.request_wake_at(deadline);
464        } else {
465            driver.clear_wake();
466        }
467    }
468}
469
470#[derive(Clone, Copy, Debug)]
471pub(crate) struct DevOverlayControl {
472    bounds: Rect,
473    mode: FramePacingMode,
474}
475
476/// Development options for debugging and performance monitoring.
477///
478/// These are rendered directly by the renderer (not via composition)
479/// to avoid affecting performance measurements.
480#[derive(Clone, Debug, Default)]
481pub struct DevOptions {
482    /// Show FPS counter overlay
483    pub fps_counter: bool,
484    /// Show recomposition count
485    pub recomposition_counter: bool,
486    /// Show layout timing breakdown
487    pub layout_timing: bool,
488    /// Show buttons in the dev overlay that switch the frame pacing mode.
489    pub frame_pacing_controls: bool,
490    /// How the shell paces its frames.
491    pub frame_pacing_mode: FramePacingMode,
492}
493
494#[cfg(any(test, feature = "test-support"))]
495#[doc(hidden)]
496#[derive(Clone, Copy, Debug)]
497pub struct RuntimeLeakDebugStats {
498    pub applier_stats: MemoryApplierDebugStats,
499    pub live_node_heap_bytes: usize,
500    pub recycled_node_heap_bytes: usize,
501    pub slot_table_heap_bytes: usize,
502    pub pass_stats: CompositionPassDebugStats,
503    pub slot_stats: SlotTableDebugStats,
504    pub runtime_stats: RuntimeDebugStats,
505    pub state_arena_stats: StateArenaDebugStats,
506    pub recompose_scope_stats: RecomposeScopeRegistryDebugStats,
507    pub snapshot_v2_stats: SnapshotV2DebugStats,
508    pub snapshot_pinning_stats: SnapshotPinningDebugStats,
509}
510
511impl ShellApp {
512    pub(crate) fn flush_semantics_invalidations(&mut self) {
513        if self.app_context.has_pending_semantics_invalidations() {
514            let mut applier = self.composition.applier_mut();
515            process_semantics_invalidations(|node_id| {
516                cranpose_core::bubble_semantics_dirty(&mut *applier, node_id);
517            });
518        }
519    }
520
521    pub(crate) fn request_layout_pass(&mut self) {
522        self.layout_requested = true;
523    }
524
525    pub(crate) fn request_forced_layout_pass(&mut self) {
526        self.layout_requested = true;
527        self.force_layout_pass = true;
528    }
529
530    pub(crate) fn composition_tree_needs_layout(&mut self) -> bool {
531        let Some(root) = self.composition.root() else {
532            return true;
533        };
534        let mut applier = self.composition.applier_mut();
535        cranpose_ui::tree_needs_layout(&mut *applier, root).unwrap_or_else(|err| {
536            log::warn!("Cannot check layout dirty status for root #{root}: {err}");
537            true
538        })
539    }
540
541    pub(crate) fn has_stale_work_in_context(&self) -> bool {
542        self.layout_requested || self.app_context.has_stale_work()
543    }
544
545    pub(crate) fn wants_frame_in_context(&self) -> bool {
546        self.layout_requested
547            || self.app_context.has_pending_frame_work()
548            || self.composition.should_render()
549    }
550
551    pub(crate) fn needs_ui_update_in_context(&self, surfaces_dirty: bool) -> bool {
552        surfaces_dirty
553            || self.has_stale_work_in_context()
554            || self.composition.runtime().has_pending_ui()
555            || self.app_context.has_pending_semantics_invalidations()
556            || self.composition.should_render()
557    }
558
559    pub(crate) fn next_event_time(&self) -> Option<web_time::Instant> {
560        self.app_context.enter(cranpose_ui::next_cursor_blink_time)
561    }
562
563    pub(crate) fn frame_time_nanos_at(&self, now: Instant) -> u64 {
564        now.checked_duration_since(self.start_time)
565            .unwrap_or_default()
566            .as_nanos()
567            .min(u128::from(u64::MAX)) as u64
568    }
569
570    pub(crate) fn realtime_pointer_event_time(
571        &self,
572        platform_time_ms: Option<i64>,
573    ) -> PointerEventTime {
574        PointerEventTime {
575            platform_time_ms,
576            animation_time_nanos: self
577                .frame_time_nanos_at(Instant::now())
578                .max(self.last_frame_time_nanos),
579        }
580    }
581
582    pub(crate) fn install_text_input_router(&mut self) {
583        if self.text_input_router_installed {
584            return;
585        }
586        let router = Rc::new(TextInputRouter {
587            routes: Rc::clone(&self.text_input_routes),
588        });
589        self.app_context
590            .enter(|| cranpose_ui::text_input_session::set_platform_text_input_handler(router));
591        self.text_input_router_installed = true;
592    }
593}
594
595impl<R> AppShell<R>
596where
597    R: Renderer,
598    R::Error: Debug,
599{
600    /// Creates a shell that composes `content` under `root_key`, with an 800 by 600 logical pixel
601    /// viewport and a buffer of the same size in pixels at density 1.0.
602    pub fn new(renderer: R, root_key: Key, content: impl FnMut() + 'static) -> Self {
603        Self::new_with_size(renderer, root_key, content, (800, 600), (800.0, 600.0))
604    }
605
606    /// Creates a shell that composes `content` under `root_key`, with a `buffer_size` in pixels and
607    /// a `viewport` in logical pixels at density 1.0.
608    pub fn new_with_size(
609        renderer: R,
610        root_key: Key,
611        content: impl FnMut() + 'static,
612        buffer_size: (u32, u32),
613        viewport: (f32, f32),
614    ) -> Self {
615        Self::new_with_size_and_density(renderer, root_key, content, buffer_size, viewport, 1.0)
616    }
617
618    /// A shell that lays out `viewport` at `density` and gives its primary
619    /// renderer the same density as its root scale, for `buffer_size` pixels.
620    pub fn new_with_size_and_density(
621        mut renderer: R,
622        root_key: Key,
623        content: impl FnMut() + 'static,
624        buffer_size: (u32, u32),
625        viewport: (f32, f32),
626        density: f32,
627    ) -> Self {
628        renderer.set_root_scale(density);
629        let app_context = cranpose_ui::AppContext::new_with_density(density);
630        let runtime = StdRuntime::new();
631        let mut composition = Composition::with_runtime(MemoryApplier::new(), runtime.runtime());
632        let app_content = Rc::new(std::cell::RefCell::new(content));
633        let mut build: Box<dyn FnMut()> = Box::new(move || {
634            let app_content = Rc::clone(&app_content);
635            cranpose_ui::density::ProvideDensity(cranpose_ui::Density::from_host(), move || {
636                cranpose_ui::widgets::PopupHost(move || {
637                    (app_content.borrow_mut())();
638                });
639            });
640        });
641        renderer.attach_app_context_services(&app_context);
642        app_context.enter(|| {
643            #[cfg(all(
644                feature = "clipboard-native",
645                not(target_arch = "wasm32"),
646                not(target_os = "android"),
647                not(target_os = "ios")
648            ))]
649            {
650                cranpose_ui::clipboard_session::set_platform_clipboard(std::rc::Rc::new(
651                    ShellClipboard::default(),
652                ));
653            }
654            if let Err(err) = composition.render_stable(root_key, &mut *build) {
655                log::error!("initial render failed: {err}");
656            }
657        });
658        renderer.scene_mut().clear();
659        let app = ShellApp {
660            app_context,
661            runtime,
662            composition,
663            content: build,
664            start_time: Instant::now(),
665            last_frame_time_nanos: 0,
666            semantics_enabled: false,
667            semantics_snapshot_revision: 0,
668            revealed_focus: None,
669            layout_requested: true,
670            content_moved: false,
671            force_layout_pass: true,
672            modifiers: None,
673            rotary_scroll_factor: DEFAULT_ROTARY_SCROLL_FACTOR_DP,
674            #[cfg(all(feature = "clipboard-native", target_os = "linux"))]
675            clipboard: arboard::Clipboard::new().ok(),
676            dev_options: DevOptions::default(),
677            inspector_projector: None,
678            fps_monitor: fps_monitor::FpsMonitor::new(),
679            text_input_routes: Rc::new(RefCell::new(TextInputRoutes::default())),
680            text_input_router_installed: false,
681            window_roots_seen: None,
682            rebuilt_frames_since_observation_prune: 0,
683            #[cfg(feature = "installed-fonts")]
684            installed_fonts: 0,
685        };
686        let mut shell = Self {
687            app,
688            surfaces: vec![RootSurface::new(
689                RootId::Primary,
690                renderer,
691                buffer_size,
692                viewport,
693            )],
694            routing_scratch: SurfaceRoutingScratch::default(),
695            pending_dirty_nodes: Vec::new(),
696            pending_layer_property_nodes: Vec::new(),
697            geometry_scene_nodes: cranpose_ui::GeometrySceneNodes::default(),
698        };
699        shell.process_frame();
700        shell
701    }
702
703    /// The shell's [`AppContext`](cranpose_ui::AppContext). Platform backends
704    /// use it to register per-context services (such as the OS clipboard) that
705    /// need UIKit/JNI access the shell itself does not have: enter the context
706    /// and call the relevant `set_platform_*` installer.
707    pub fn app_context(&self) -> &Rc<cranpose_ui::AppContext> {
708        &self.app.app_context
709    }
710
711    /// The surface drawing `root`, or `None` when no surface was added for
712    /// it. The primary surface always exists.
713    pub fn surface(&mut self, root: RootId) -> Option<SurfaceMut<'_, R>> {
714        let index = self.surface_index(root)?;
715        Some(SurfaceMut::new(self, index))
716    }
717
718    /// The surface drawing the composition root.
719    pub fn primary(&mut self) -> SurfaceMut<'_, R> {
720        SurfaceMut::new(self, 0)
721    }
722
723    /// The root whose surface now draws the node that took the primary
724    /// press, or `None` while no press is being held.
725    ///
726    /// A press is taken by a node, not by a rectangle. When the application
727    /// moves that node into a window of its own — a tab pulled out of a
728    /// strip, a pane pulled off a stack — the press belongs to that window
729    /// from then on, and a platform can hand it over on the strength of this
730    /// rather than on where the pointer happens to be.
731    pub fn root_holding_the_press(&mut self) -> Option<RootId> {
732        let pressed = self
733            .surfaces
734            .iter()
735            .find_map(|surface| surface.hit_path_tracker.dispatch_order(PointerId::PRIMARY))?;
736        let primary_root = self.app.composition.root()?;
737        let mut applier = self.app.composition.applier_mut();
738        let holder = pressed.into_iter().find_map(|node| {
739            let node = applier.scene_node_attached_to(node, primary_root)?;
740            Some(cranpose_ui::nearest_window_root(&mut applier, node))
741        })?;
742        drop(applier);
743        self.surfaces
744            .iter()
745            .find(|surface| surface.owns_nodes_under(holder))
746            .map(|surface| surface.id)
747    }
748
749    fn surface_index(&self, root: RootId) -> Option<usize> {
750        self.surfaces.iter().position(|surface| surface.id == root)
751    }
752
753    /// Gives the window root registered under `window` a surface of its
754    /// own, drawn by `renderer` into a framebuffer of `buffer_size` physical
755    /// pixels and `viewport` logical pixels. The surface follows the window
756    /// root node while it stays in the tree, and draws nothing while it is
757    /// out. Returns the renderer of the surface this one replaces, if the
758    /// window already had one.
759    ///
760    /// The renderer joins an app whose services the primary renderer
761    /// already installed; nothing is attached to the app context again.
762    pub fn add_window_surface(
763        &mut self,
764        window: u64,
765        renderer: R,
766        buffer_size: (u32, u32),
767        viewport: (f32, f32),
768    ) -> Option<R> {
769        let previous = self.remove_window_surface(window);
770        self.surfaces.push(RootSurface::new(
771            RootId::Window(window),
772            renderer,
773            buffer_size,
774            viewport,
775        ));
776        self.app.window_roots_seen = None;
777        self.sync_window_roots();
778        previous
779    }
780
781    /// Takes the surface of `window` away, handing back its renderer.
782    pub fn remove_window_surface(&mut self, window: u64) -> Option<R> {
783        let root = RootId::Window(window);
784        let index = self.surface_index(root)?;
785        self.app.text_input_routes.borrow_mut().remove(root);
786        Some(self.surfaces.remove(index).renderer)
787    }
788
789    /// Every surface's root, the primary first.
790    pub fn surface_ids(&self) -> Vec<RootId> {
791        self.surfaces.iter().map(|surface| surface.id).collect()
792    }
793
794    /// The window roots attached in the composition right now. A platform
795    /// reads this after an update to open a window for each new entry and
796    /// close the windows whose entries left.
797    pub fn window_roots(&self) -> Vec<WindowRootEntry> {
798        self.app.app_context.enter(cranpose_ui::window_roots)
799    }
800
801    /// Changes whenever a window root attaches, detaches or is updated; a
802    /// platform that stored the last value it acted on skips
803    /// [`Self::window_roots`] while it reads the same.
804    pub fn window_roots_revision(&self) -> u64 {
805        self.app
806            .app_context
807            .enter(cranpose_ui::window_roots_revision)
808    }
809
810    /// Whether the primary root lays out anything of its own: a node with
811    /// area outside every window root. A platform hides the primary window
812    /// while this is false, because every visible node is then in a window
813    /// of its own.
814    pub fn primary_has_content(&mut self) -> bool {
815        self.read_primary_boxes(cranpose_ui::has_placed_content)
816            .unwrap_or(false)
817    }
818
819    /// The extent of what the primary root lays out: the far right and
820    /// bottom edges of its nodes with area, measured from the root's origin
821    /// and leaving out every window root's subtree. `None` while it lays
822    /// out nothing. A platform whose primary window wraps its content sizes
823    /// the window to this after every update.
824    pub fn primary_content_size(&mut self) -> Option<Size> {
825        self.read_primary_boxes(cranpose_ui::placed_content_extent)
826            .flatten()
827    }
828
829    /// Reads the boxes placed under the primary root with `read`, straight
830    /// off the applier: these run after every update, so they build no
831    /// layout snapshot.
832    fn read_primary_boxes<T>(
833        &mut self,
834        read: impl FnOnce(&mut MemoryApplier, NodeId) -> Result<T, NodeError>,
835    ) -> Option<T> {
836        let _app_context = self.app.app_context.enter_scope();
837        let root = self.surfaces[0].root_node(&self.app)?;
838        let mut applier = self.app.composition.applier_mut();
839        read(&mut applier, root)
840            .inspect_err(|err| log::debug!("failed to read the primary root's boxes: {err}"))
841            .ok()
842    }
843
844    /// The `drag_and_drop_target` under `point` and where the point falls in
845    /// that target's surface. A point on the screen is looked up in every
846    /// surface whose window position the platform told, later windows
847    /// first and the primary last; a point without one only in `source`,
848    /// the surface holding the press.
849    pub(crate) fn drag_and_drop_target_at(
850        &self,
851        point: cranpose_ui::DragAndDropPoint,
852        source: usize,
853    ) -> Option<(NodeId, cranpose_ui::Point)> {
854        let drag_and_drop = self.app.app_context.drag_and_drop();
855        let target_in = |index: usize, local: cranpose_ui::Point| {
856            self.surfaces[index]
857                .renderer
858                .scene()
859                .hit_test_nodes(local.x, local.y)
860                .into_iter()
861                .find(|node| drag_and_drop.is_target(*node))
862                .map(|node| (node, local))
863        };
864        match point.screen {
865            Some(screen) => (0..self.surfaces.len()).rev().find_map(|index| {
866                let local = self.surfaces[index].screen_point_inside(screen)?;
867                target_in(index, local)
868            }),
869            None => target_in(source, point.local),
870        }
871    }
872
873    /// Names the surface the platform considers focused, which is where the
874    /// soft keyboard belongs. Pointer presses activate their surface on
875    /// their own; a platform calls this for focus it grants otherwise.
876    pub fn set_active_root(&mut self, root: RootId) {
877        self.app.text_input_routes.borrow_mut().set_active(root);
878    }
879
880    /// The surface the platform considers focused.
881    pub fn active_root(&self) -> RootId {
882        self.app.text_input_routes.borrow().active()
883    }
884
885    fn sync_window_roots(&mut self) {
886        let _app_context = self.app.app_context.enter_scope();
887        self.sync_window_roots_in_context();
888    }
889
890    pub(crate) fn sync_window_roots_in_context(&mut self) {
891        let revision = self.app.app_context.window_roots().revision();
892        if self.app.window_roots_seen == Some(revision) {
893            return;
894        }
895        self.app.window_roots_seen = Some(revision);
896        let entries = cranpose_ui::window_roots();
897        for surface in &mut self.surfaces {
898            let RootId::Window(id) = surface.id else {
899                continue;
900            };
901            let root = entries
902                .iter()
903                .find(|entry| entry.node as u64 == id)
904                .map(|entry| entry.node);
905            surface.set_root(root);
906        }
907    }
908
909    pub(crate) fn any_surface_dirty(&self) -> bool {
910        self.surfaces
911            .iter()
912            .any(|surface| surface.is_dirty || surface.scene_dirty)
913    }
914
915    pub(crate) fn mark_all_dirty(&mut self) {
916        for surface in &mut self.surfaces {
917            surface.is_dirty = true;
918        }
919    }
920
921    fn clear_surface_dirt(&mut self) {
922        for surface in &mut self.surfaces {
923            surface.is_dirty = false;
924        }
925    }
926
927    fn invalidate_dev_overlay_text(&mut self) {
928        for surface in &mut self.surfaces {
929            surface.invalidate_dev_overlay_text();
930        }
931    }
932
933    /// Set development options for debugging and performance monitoring.
934    ///
935    /// The FPS counter and other overlays are rendered directly by the renderer
936    /// (not via composition) to avoid affecting performance measurements.
937    pub fn set_dev_options(&mut self, options: DevOptions) {
938        self.app.dev_options = options;
939        self.invalidate_dev_overlay_text();
940        self.app.app_context.enter(request_render_invalidation);
941        self.mark_all_dirty();
942    }
943
944    /// Get a reference to the current dev options.
945    pub fn dev_options(&self) -> &DevOptions {
946        &self.app.dev_options
947    }
948
949    /// Returns the frame pacing mode set in the dev options.
950    pub fn frame_pacing_mode(&self) -> FramePacingMode {
951        self.app.dev_options.frame_pacing_mode
952    }
953
954    /// Returns the presented-frame cadence in frames per second, as the dev overlay shows it.
955    pub fn current_fps(&self) -> f32 {
956        self.app.fps_monitor.current_fps()
957    }
958
959    /// Returns the frame interval and frame work statistics of the recent presented frames.
960    pub fn fps_stats(&self) -> FpsStats {
961        self.app.fps_monitor.stats()
962    }
963
964    /// Clears the frame statistics and the recomposition count shown by the dev overlay.
965    pub fn reset_fps_stats(&mut self) {
966        self.app.fps_monitor.reset_stats();
967        self.invalidate_dev_overlay_text();
968    }
969
970    /// Records when the work for one presented frame started and finished. Platforms call it once
971    /// per presented frame to feed the frame statistics.
972    pub fn record_presented_frame(
973        &mut self,
974        frame_started_at: Instant,
975        frame_finished_at: Instant,
976    ) {
977        self.app
978            .fps_monitor
979            .record_frame_work(frame_started_at, frame_finished_at);
980    }
981
982    #[cfg(any(test, feature = "test-support"))]
983    #[doc(hidden)]
984    pub fn record_presented_frame_for_test(
985        &mut self,
986        frame_started_nanos: u64,
987        frame_finished_nanos: u64,
988    ) {
989        let started = self.app.start_time + std::time::Duration::from_nanos(frame_started_nanos);
990        let finished = self.app.start_time + std::time::Duration::from_nanos(frame_finished_nanos);
991        self.record_presented_frame(started, finished);
992    }
993
994    /// Sets the frame pacing mode and redraws the dev overlay. Platforms read the mode back with
995    /// `frame_pacing_mode`.
996    pub fn set_frame_pacing_mode(&mut self, mode: FramePacingMode) {
997        if self.app.dev_options.frame_pacing_mode == mode {
998            return;
999        }
1000        self.app.dev_options.frame_pacing_mode = mode;
1001        self.invalidate_dev_overlay_text();
1002        self.app.app_context.enter(request_render_invalidation);
1003        self.mark_all_dirty();
1004    }
1005
1006    /// Where the dev overlay draws the control for `mode`, in logical pixels.
1007    ///
1008    /// The overlay is drawn by the renderer rather than composed, so it has no
1009    /// semantics for a test to search. Without this a test that wants to press
1010    /// a pacing control has to hard-code a coordinate and silently starts
1011    /// passing against empty space the moment the overlay's text changes.
1012    pub fn dev_overlay_control_center(&self, mode: FramePacingMode) -> Option<(f32, f32)> {
1013        self.surfaces[0].dev_overlay_control_center(mode)
1014    }
1015
1016    /// Sets the primary surface's viewport and lays out and renders at once.
1017    /// The size the surface already has changes nothing.
1018    pub fn set_viewport(&mut self, width: f32, height: f32) {
1019        if self.surfaces[0].viewport == (width, height) {
1020            return;
1021        }
1022        self.primary().set_viewport(width, height);
1023        self.process_frame();
1024    }
1025
1026    /// Returns the width and height of the primary surface's viewport in logical pixels.
1027    pub fn viewport_size(&self) -> (f32, f32) {
1028        self.surfaces[0].viewport
1029    }
1030
1031    /// Tells the shell where the primary window sits on the screen. See
1032    /// [`SurfaceMut::set_screen_origin`].
1033    pub fn set_screen_origin(&mut self, origin: Option<cranpose_ui_graphics::Point>) {
1034        self.surfaces[0].screen_origin = origin;
1035    }
1036
1037    /// Sets the size in pixels of the buffer the primary surface renders into.
1038    pub fn set_buffer_size(&mut self, width: u32, height: u32) {
1039        self.surfaces[0].buffer_size = (width, height);
1040    }
1041
1042    /// Returns the width and height in pixels of the buffer the primary surface renders into.
1043    pub fn buffer_size(&self) -> (u32, u32) {
1044        self.surfaces[0].buffer_size
1045    }
1046
1047    /// Returns the render scene of the primary surface.
1048    pub fn scene(&self) -> &R::Scene {
1049        self.surfaces[0].renderer.scene()
1050    }
1051
1052    /// Returns the renderer of the primary surface.
1053    pub fn renderer(&mut self) -> &mut R {
1054        &mut self.surfaces[0].renderer
1055    }
1056
1057    /// Sets the callback the runtime calls when it schedules a frame. Platforms use it to wake
1058    /// their loop. Other threads can call it, so it must be `Send` and `Sync`.
1059    #[cfg(not(target_arch = "wasm32"))]
1060    pub fn set_frame_waker(&mut self, waker: impl Fn() + Send + Sync + 'static) {
1061        self.app.runtime.set_frame_waker(waker);
1062    }
1063
1064    /// Sets the callback the runtime calls when it schedules a frame. Platforms use it to wake
1065    /// their loop. Wasm runs on one thread, so the callback need not be `Send`.
1066    #[cfg(target_arch = "wasm32")]
1067    pub fn set_frame_waker(&mut self, waker: impl Fn() + 'static) {
1068        self.app.runtime.set_frame_waker(waker);
1069    }
1070
1071    /// Removes the callback set by `set_frame_waker`.
1072    pub fn clear_frame_waker(&mut self) {
1073        self.app.runtime.clear_frame_waker();
1074    }
1075
1076    /// Returns whether the shell wants a rendered frame: a frame callback or animation is active,
1077    /// or a surface's scene is out of date.
1078    pub fn should_render(&self) -> bool {
1079        self.app.app_context.enter(|| {
1080            self.app.wants_frame_in_context()
1081                || self.surfaces.iter().any(|surface| surface.scene_dirty)
1082        })
1083    }
1084
1085    /// Returns whether the shell has work to run: queued UI tasks, state changes, layout, semantics
1086    /// or an active animation.
1087    pub fn needs_update(&self) -> bool {
1088        self.app.app_context.enter(|| {
1089            self.app
1090                .needs_ui_update_in_context(self.any_surface_dirty())
1091        })
1092    }
1093
1094    /// Returns whether queued UI, state, layout, semantics, or due cursor work can be processed
1095    /// without advancing the frame clock. Awaiting a display frame alone is false.
1096    pub fn needs_update_without_frame(&self) -> bool {
1097        self.app.app_context.enter(|| {
1098            self.any_surface_dirty()
1099                || self.app.has_stale_work_in_context()
1100                || self.app.composition.should_recompose()
1101                || self.app.composition.runtime().has_pending_ui()
1102                || self.app.app_context.has_pending_semantics_invalidations()
1103                || cranpose_ui::next_cursor_blink_time().is_some_and(|at| at <= Instant::now())
1104                || self
1105                    .surfaces
1106                    .iter()
1107                    .any(|surface| surface.renderer_warmup_due(&self.app))
1108        })
1109    }
1110
1111    /// Returns true when the runtime holds work for the UI thread: posted tasks,
1112    /// continuations from worker threads, or async tasks ready to poll.
1113    ///
1114    /// This is the part of [`needs_update`](Self::needs_update) that another
1115    /// thread is waiting on, told apart from the part that only wants the screen
1116    /// redrawn. A platform backend running with no surface uses it to compose
1117    /// for work and stay asleep for animation.
1118    pub fn has_pending_ui(&self) -> bool {
1119        self.app.composition.runtime().has_pending_ui()
1120    }
1121
1122    /// Runs the tasks woken since they last ran, now rather than at the next
1123    /// frame. A coroutine whose `delay` ran out resumes when its timer fires,
1124    /// as Compose's main dispatcher resumes one, and the next frame draws what
1125    /// it changed. Waiting for the frame instead would resume a 16 ms delay
1126    /// only every other frame, whenever the timer fires just after one starts.
1127    /// A platform calls this whenever its loop wakes.
1128    pub fn run_pending_tasks(&mut self) {
1129        // The tasks run in the app's context; a wake that brought none, as a
1130        // frame request does, needs no context.
1131        if !self.app.composition.runtime().has_pending_ui() {
1132            return;
1133        }
1134        let runtime_handle = self.app.composition.runtime_handle();
1135        self.app.app_context.enter(|| {
1136            runtime_handle.with_deferred_state_releases(|| runtime_handle.drain_ui());
1137        });
1138    }
1139
1140    /// Returns true if the primary surface owes the display a frame: stale
1141    /// pixels, or a renderer that has not warmed its swapchain yet.
1142    ///
1143    /// An app that merely holds an open `next_frame()` await - a game loop, a
1144    /// polling effect - keeps [`Self::needs_update`] true forever without
1145    /// changing a single pixel. Such an app must still be *ticked* every frame,
1146    /// but the frame it produces is byte-identical to the last one, and
1147    /// presenting it costs a full swapchain rotation and pins the panel at its
1148    /// maximum refresh rate. Callers pair this with
1149    /// [`FrameUpdateResult::visual_changed`] from the update they just ran,
1150    /// which reports the work that update actually did.
1151    /// Note: Cursor blink is now timer-based and uses WaitUntil scheduling, not continuous redraw.
1152    pub fn needs_redraw(&self) -> bool {
1153        self.app
1154            .app_context
1155            .enter(|| self.surfaces[0].needs_redraw_in_context(&self.app))
1156    }
1157
1158    /// Marks the primary surface as dirty, indicating a redraw is needed.
1159    pub fn mark_dirty(&mut self) {
1160        self.surfaces[0].is_dirty = true;
1161    }
1162
1163    /// Asks for the root content to render again from the top on the next update, with a forced
1164    /// layout pass.
1165    pub fn request_root_render(&mut self) {
1166        self.app.composition.request_root_render();
1167        self.app.request_forced_layout_pass();
1168        self.app.app_context.enter(request_render_invalidation);
1169        self.mark_all_dirty();
1170    }
1171
1172    /// Lays the composition out at `density` from now on, and has the primary
1173    /// renderer draw at that root scale.
1174    pub fn set_density(&mut self, density: f32) {
1175        self.surfaces[0].renderer.set_root_scale(density);
1176        let changed = self.app.app_context.enter(|| {
1177            let previous = cranpose_ui::current_density().to_bits();
1178            cranpose_ui::set_density(density);
1179            previous != cranpose_ui::current_density().to_bits()
1180        });
1181        if changed {
1182            self.request_root_render();
1183        }
1184    }
1185
1186    /// Reports the system font scale the platform is showing text at.
1187    ///
1188    /// Hosts call this at startup and on every configuration change. Sizes in
1189    /// `Sp` follow it, sizes in `Dp` do not, which is what lets an app grow its
1190    /// text with the user's setting while its layout stays where it was.
1191    ///
1192    /// This takes the setting as a plain multiplier. A host whose platform
1193    /// converts `Sp` through a table of its own — Android 14 and up does — calls
1194    /// [`AppShell::set_font_scale_curve`] with what the platform answered
1195    /// instead, because the multiplication is not what that platform does.
1196    pub fn set_font_scale(&mut self, font_scale: f32) {
1197        self.set_font_scale_curve(cranpose_ui::FontScaleCurve::linear(font_scale));
1198    }
1199
1200    /// Reports the system font scale together with the conversion behind it.
1201    ///
1202    /// See [`cranpose_ui::font_scale`]: above a threshold setting Android
1203    /// resolves a size in `Sp` through a piecewise-linear table rather than by
1204    /// multiplying, so a host that can read that table hands it over here and
1205    /// every `Sp` in the app resolves the way the platform's own text does.
1206    pub fn set_font_scale_curve(&mut self, curve: cranpose_ui::FontScaleCurve) {
1207        let changed = self.app.app_context.enter(|| {
1208            let previous = cranpose_ui::current_font_scale_curve();
1209            cranpose_ui::set_font_scale_curve(curve);
1210            previous != cranpose_ui::current_font_scale_curve()
1211        });
1212        if changed {
1213            self.request_root_render();
1214        }
1215    }
1216
1217    #[cfg(any(test, feature = "test-support"))]
1218    #[doc(hidden)]
1219    pub fn debug_current_density(&self) -> f32 {
1220        self.app.app_context.enter(cranpose_ui::current_density)
1221    }
1222
1223    #[cfg(any(test, feature = "test-support"))]
1224    #[doc(hidden)]
1225    pub fn debug_current_font_scale(&self) -> f32 {
1226        self.app.app_context.enter(cranpose_ui::current_font_scale)
1227    }
1228
1229    #[cfg(any(test, feature = "test-support"))]
1230    #[doc(hidden)]
1231    pub fn debug_current_font_scale_curve(&self) -> cranpose_ui::FontScaleCurve {
1232        self.app
1233            .app_context
1234            .enter(cranpose_ui::current_font_scale_curve)
1235    }
1236
1237    #[cfg(any(test, feature = "test-support"))]
1238    #[doc(hidden)]
1239    pub fn debug_enter_app_context<T>(&self, block: impl FnOnce() -> T) -> T {
1240        self.app.app_context.enter(block)
1241    }
1242
1243    /// Returns true if there are active animations or pending recompositions.
1244    pub fn has_active_animations(&self) -> bool {
1245        self.app.composition.should_render()
1246    }
1247
1248    /// Returns `true` while a one-shot frame callback waits for the next frame. Perpetual frame
1249    /// callbacks do not count.
1250    pub fn has_transient_frame_callbacks(&self) -> bool {
1251        self.app
1252            .composition
1253            .runtime()
1254            .has_transient_frame_callbacks()
1255    }
1256
1257    /// Returns `true` while a pointer button is held down on a control of the primary surface.
1258    pub fn has_active_pointer_gesture(&self) -> bool {
1259        self.surfaces[0].has_active_pointer_gesture()
1260    }
1261
1262    /// Primary-surface form of [`SurfaceMut::has_active_primary_gesture`].
1263    pub fn has_active_primary_gesture(&self) -> bool {
1264        self.surfaces[0].has_active_primary_gesture()
1265    }
1266
1267    /// Primary-surface form of [`SurfaceMut::frame_owed`].
1268    pub fn frame_owed(&self) -> bool {
1269        self.surfaces[0].frame_owed
1270    }
1271
1272    /// Primary-surface form of [`SurfaceMut::take_frame_owed`].
1273    pub fn take_frame_owed(&mut self) -> bool {
1274        std::mem::take(&mut self.surfaces[0].frame_owed)
1275    }
1276
1277    /// Returns the next scheduled event time for cursor blink.
1278    /// Use this for `ControlFlow::WaitUntil` scheduling.
1279    pub fn next_event_time(&self) -> Option<web_time::Instant> {
1280        self.app.next_event_time()
1281    }
1282
1283    fn compute_frame_schedule(&self) -> FrameSchedule {
1284        self.surfaces[0].compute_frame_schedule(&self.app, self.any_surface_dirty())
1285    }
1286
1287    /// Computes the frame schedule of the primary surface, stores it in the surface's scheduler and
1288    /// returns it.
1289    pub fn frame_schedule(&self) -> FrameSchedule {
1290        let schedule = self.compute_frame_schedule();
1291        self.surfaces[0].frame_scheduler.record(schedule);
1292        schedule
1293    }
1294
1295    /// Computes and stores the frame schedule of the primary surface, then asks `driver` for the
1296    /// frame or wake it calls for. Returns the schedule.
1297    pub fn schedule_platform_frame<D>(&self, driver: &D) -> FrameSchedule
1298    where
1299        D: PlatformFrameDriver + ?Sized,
1300    {
1301        let schedule = self.compute_frame_schedule();
1302        self.surfaces[0].frame_scheduler.schedule(schedule, driver);
1303        schedule
1304    }
1305
1306    /// Returns the schedule last stored by `frame_schedule` or `schedule_platform_frame`, without
1307    /// computing a new one.
1308    pub fn frame_scheduler_snapshot(&self) -> FrameSchedule {
1309        self.surfaces[0].frame_scheduler.snapshot()
1310    }
1311
1312    /// Timestamp a live input sample against the current animation clock.
1313    pub fn realtime_pointer_event_time(&self, platform_time_ms: Option<i64>) -> PointerEventTime {
1314        self.app.realtime_pointer_event_time(platform_time_ms)
1315    }
1316
1317    /// Timestamp deterministic input at the most recently processed frame.
1318    pub fn exact_pointer_event_time(&self, platform_time_ms: Option<i64>) -> PointerEventTime {
1319        PointerEventTime {
1320            platform_time_ms,
1321            animation_time_nanos: self.app.last_frame_time_nanos,
1322        }
1323    }
1324
1325    /// Advances the frame clock by `frame_interval` past the later of the last frame time and the
1326    /// current time, and runs one update there.
1327    pub fn update_after_frame_interval(
1328        &mut self,
1329        frame_interval: std::time::Duration,
1330    ) -> FrameUpdateResult {
1331        let wall_frame_time = self.app.frame_time_nanos_at(Instant::now());
1332        let base_frame_time = self.app.last_frame_time_nanos.max(wall_frame_time);
1333        let frame_time = base_frame_time
1334            .saturating_add(frame_interval.as_nanos().min(u128::from(u64::MAX)) as u64);
1335        self.update_at_frame_time_nanos(frame_time)
1336    }
1337
1338    /// Advance the frame clock by EXACTLY `frame_interval` past the last
1339    /// frame — no wall anchoring — and run one update there. Robot keyframe
1340    /// captures ride this to sample animations deterministically: while the
1341    /// advanced clock is ahead of wall time, interleaved wall-clocked
1342    /// updates clamp to it (dt 0) instead of fast-forwarding animations.
1343    pub fn update_after_exact_interval(
1344        &mut self,
1345        frame_interval: std::time::Duration,
1346    ) -> FrameUpdateResult {
1347        let frame_time = self
1348            .app
1349            .last_frame_time_nanos
1350            .saturating_add(frame_interval.as_nanos().min(u128::from(u64::MAX)) as u64);
1351        self.update_at_frame_time_nanos(frame_time)
1352    }
1353
1354    /// Delivers a display frame at a monotonic platform instant, then runs the
1355    /// continuations it wakes. This is independent of renderer availability.
1356    /// The instant is normalized to the shell's start, sharing the input clock's epoch.
1357    /// Repeated or older frame timestamps do not wake frame awaiters again.
1358    /// Use [`Self::update_without_frame`] to compose and draw the resulting state.
1359    pub fn dispatch_frame_at(&mut self, frame_time: Instant) {
1360        let frame_time = self.app.frame_time_nanos_at(frame_time);
1361        let _app_context = self.app.app_context.enter_scope();
1362        let runtime_handle = self.app.runtime.runtime_handle();
1363        runtime_handle.with_deferred_state_releases(|| {
1364            self.app.last_frame_time_nanos = frame_time.max(self.app.last_frame_time_nanos);
1365            self.app
1366                .runtime
1367                .drain_frame_callbacks(self.app.last_frame_time_nanos);
1368            self.apply_pointer_claim();
1369            runtime_handle.drain_ui();
1370            self.dispatch_requested_mouse_moves(self.app.last_frame_time_nanos);
1371        });
1372    }
1373
1374    /// Processes pending state, input, layout, drawing, and semantics work without
1375    /// waking frame awaiters. Platforms use this for work between display frames.
1376    pub fn update_without_frame(&mut self) -> FrameUpdateResult {
1377        self.update_with_frame_time(None)
1378    }
1379
1380    /// Processes one update at a frame timestamp in nanoseconds since this shell
1381    /// started. Duplicate or older timestamps process UI work without delivering
1382    /// another frame callback.
1383    pub fn update_at_frame_time_nanos(&mut self, frame_time: u64) -> FrameUpdateResult {
1384        self.update_with_frame_time(Some(frame_time))
1385    }
1386
1387    fn update_with_frame_time(&mut self, frame_time: Option<u64>) -> FrameUpdateResult {
1388        let _app_context = self.app.app_context.enter_scope();
1389        let mut stages = UpdateStages::start(update_stage_telemetry_threshold_ms().is_some());
1390        let runtime_handle = self.app.runtime.runtime_handle();
1391        runtime_handle.with_deferred_state_releases(|| {
1392            if let Some(frame_time) = frame_time {
1393                self.app.last_frame_time_nanos = frame_time.max(self.app.last_frame_time_nanos);
1394                self.app.runtime.drain_frame_callbacks(self.app.last_frame_time_nanos);
1395                self.apply_pointer_claim();
1396            }
1397            stages.reached();
1398            runtime_handle.drain_ui();
1399            self.dispatch_requested_mouse_moves(self.app.last_frame_time_nanos);
1400            stages.reached();
1401            let should_render = self.app.composition.should_recompose();
1402            let mut reconcile_attempted = false;
1403            let mut reconcile_changed = false;
1404            if should_render {
1405                log::trace!(
1406                    target: "cranpose::input",
1407                    "update begin: should_render=true layout_requested={} scene_dirty={} is_dirty={}",
1408                    self.app.layout_requested,
1409                    self.surfaces[0].scene_dirty,
1410                    self.surfaces[0].is_dirty
1411                );
1412                (reconcile_attempted, reconcile_changed) = self.reconcile_in_context();
1413            }
1414            stages.reached();
1415            let result = self.process_frame_in_context(reconcile_changed);
1416            stages.reached();
1417            log_update_stage_telemetry(
1418                &stages,
1419                UpdateOutcome {
1420                    should_render,
1421                    reconcile_attempted,
1422                    reconcile_changed,
1423                },
1424            );
1425            self.clear_surface_dirt();
1426            result
1427        })
1428    }
1429
1430    /// Gives a held press that a node claimed during the frame, as a long
1431    /// press does, to that node alone.
1432    fn apply_pointer_claim(&mut self) {
1433        let Some(node) = self.app.app_context.take_pointer_claim() else {
1434            return;
1435        };
1436        for index in 0..self.surfaces.len() {
1437            SurfaceMut::new(self, index).give_press_to(node);
1438        }
1439    }
1440
1441    fn dispatch_requested_mouse_moves(&mut self, frame_time: u64) {
1442        use cranpose_ui::mouse_input::MouseInputTarget;
1443        let pending = self.app.app_context.pending_mouse_moves();
1444        for _ in 0..pending {
1445            let Some(request) = self.app.app_context.take_mouse_move() else {
1446                break;
1447            };
1448            let root = match request.target {
1449                MouseInputTarget::Primary => RootId::Primary,
1450                MouseInputTarget::Window(id) => RootId::Window(id),
1451            };
1452            if let Some(mut surface) = self.surface(root) {
1453                surface.set_pointer_source(PointerSource::Mouse);
1454                surface.set_cursor_at_event_time(request.position.x, request.position.y, PointerEventTime {
1455                    platform_time_ms: Some((frame_time / 1_000_000).min(i64::MAX as u64) as i64),
1456                    animation_time_nanos: frame_time,
1457                });
1458            }
1459        }
1460    }
1461
1462    fn reconcile_in_context(&mut self) -> (bool, bool) {
1463        let Some(root_key) = self.app.composition.root_key() else {
1464            return (false, false);
1465        };
1466        match self
1467            .app
1468            .composition
1469            .reconcile(root_key, &mut *self.app.content)
1470        {
1471            Ok(changed) => {
1472                log::trace!(
1473                    target: "cranpose::input",
1474                    "reconcile changed={changed}"
1475                );
1476                if changed {
1477                    self.app.fps_monitor.record_recomposition();
1478                    if self.app.composition_tree_needs_layout() {
1479                        self.app.request_layout_pass();
1480                    }
1481                    request_render_invalidation();
1482                }
1483                (true, changed)
1484            }
1485            Err(NodeError::Missing { id }) => {
1486                log::debug!("Recomposition skipped: node {id} no longer exists");
1487                self.app.request_layout_pass();
1488                request_render_invalidation();
1489                (true, false)
1490            }
1491            Err(err) => {
1492                log::error!("recomposition failed: {err}");
1493                self.app.request_layout_pass();
1494                request_render_invalidation();
1495                (true, false)
1496            }
1497        }
1498    }
1499
1500    /// Runs one update at the current time: state, input, layout, drawing and semantics. Returns
1501    /// what changed.
1502    pub fn update(&mut self) -> FrameUpdateResult {
1503        let frame_time = self.app.frame_time_nanos_at(Instant::now());
1504        self.update_at_frame_time_nanos(frame_time)
1505    }
1506}
1507
1508impl<R> Drop for AppShell<R>
1509where
1510    R: Renderer,
1511{
1512    fn drop(&mut self) {
1513        self.app.runtime.clear_frame_waker();
1514    }
1515}
1516
1517/// Returns the standard root key to pass to the shell constructors. It is the same on every call.
1518pub fn default_root_key() -> Key {
1519    location_key(file!(), line!(), column!())
1520}
1521
1522#[cfg(test)]
1523#[path = "tests/app_shell_frame_pacing_tests.rs"]
1524mod frame_pacing_tests;
1525
1526#[cfg(test)]
1527#[path = "tests/app_shell_tests.rs"]
1528mod tests;
1529
1530#[cfg(test)]
1531#[path = "tests/surface_tests.rs"]
1532mod surface_tests;