1#![cfg_attr(doctest, doc = include_str!("../README.md"))]
6
7pub mod app;
8pub use app::module as app_module;
9pub mod drag;
10pub mod editing;
11pub mod ext;
12pub mod focus;
13pub mod focus_query;
14pub mod hotreload;
15#[cfg(all(not(target_arch = "wasm32"), feature = "dynamic-loader"))]
16pub mod loader;
17pub mod persistence;
18pub mod profiler;
19pub mod runtime_app;
20pub mod store_backend;
21pub mod wake;
22pub mod tracker;
23
24pub use nativeview_abi as abi;
25use nativeview_abi::{DecodeError, DurationUs, FrameId, NodeId, SurfaceId};
26use nativeview_store::{ChangeSet, NodeStore};
27
28pub use app::{
29 clear_last_ffi_panic, default_measure_constraints, last_ffi_panic, take_last_ffi_panic, NvApp,
30};
31pub use app_module::NvAppModule;
32pub use ext::frame::{
33 FrameTickHook, FrameTickRegistry, FrameWaker, SlotWaker, TickDemand, TickHookId, TickPriority,
34 WallClock,
35};
36pub use ext::vsync::{
37 interval_us_from_millihertz, sanitize_refresh_rate_millihertz, ManualVsyncHandle,
38 ManualVsyncPacer, TimerVsyncPacer, VsyncPacer, DEFAULT_REFRESH_RATE_MILLIHERTZ,
39};
40pub use runtime_app::{AppFrame, CapabilityUnsupported, RuntimeApp};
41pub use store_backend::{StoreBackend, StoreWireReplica};
42pub use wake::{AppWake, WakeBridge, WakeCallback, WakeDeferGuard, WakeToken};
43pub use focus::{
44 first_focusable_in_subtree, first_matching_in_subtree, is_composite_aria_group,
45 is_focusable_node, is_focusable_self, is_subtree_closed_overlay, is_subtree_disabled,
46 is_tabbable_self, is_text_entry_node, is_within_subtree, resolve_focus_recovery,
47 snapshot_focus_anchor, topmost_open_modal, FocusAnchor, FocusRecoveryResult,
48};
49pub use focus_query::press_focus_target;
50pub use tracker::InteractionTracker;
51
52pub use hotreload::{
53 HotPatchDecodeError, HotPatchMsg, HOT_PATCH_VERSION,
54 HOT_PATCH_WIRE_MAGIC,
55};
56pub use persistence::{
57 write_file_atomic, AppBuildId, Frame0File, Frame0Footer, Frame0Meta, Frame0Miss,
58 PersistenceError, StateByteLen, StateFile, StateRecoveryStats, StateSaveOutcome,
59 DEFAULT_FRAME0_FILE_NAME, DEFAULT_STATE_FILE_NAME, FRAME0_FOOTER_SIZE, MAX_FRAME0_FILE_BYTES,
60 MAX_STATE_FILE_BYTES, STATE_FILE_HEADER_SIZE, STATE_FILE_MAGIC,
61};
62pub use profiler::{FrameProfiler, FrameTiming};
63
64#[derive(Debug, Clone, Copy, PartialEq)]
65pub struct Viewport {
66 pub width: f32,
67 pub height: f32,
68 pub scale_factor: f32,
69}
70
71impl Default for Viewport {
72 fn default() -> Self {
73 Self {
74 width: 393.0,
75 height: 852.0,
76 scale_factor: 3.0,
77 }
78 }
79}
80
81#[derive(Debug, Clone, Copy, PartialEq, Default)]
82pub struct LayoutRect {
83 pub x: f32,
84 pub y: f32,
85 pub width: f32,
86 pub height: f32,
87}
88
89#[derive(Debug, Clone, Copy, Default)]
90pub struct BackendApplyStats {
91 pub op_count: u32,
92 pub mutation_count: u32,
93 pub decode_us: DurationUs,
94 pub layout_us: DurationUs,
95 pub diff_us: DurationUs,
96 pub mount_us: DurationUs,
97}
98
99#[derive(Debug)]
100pub enum BackendError {
101 Decode(DecodeError),
102 Wire(nativeview_store::wire::WireError),
103 NeedsKeyframe(Option<nativeview_store::wire::WireError>),
105 InvalidNode(NodeId),
106 Internal(String),
107}
108
109impl From<DecodeError> for BackendError {
110 fn from(e: DecodeError) -> Self {
111 Self::Decode(e)
112 }
113}
114
115impl From<nativeview_store::wire::WireError> for BackendError {
116 fn from(e: nativeview_store::wire::WireError) -> Self {
117 Self::Wire(e)
118 }
119}
120
121impl std::fmt::Display for BackendError {
122 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123 write!(f, "{self:?}")
124 }
125}
126
127impl std::error::Error for BackendError {}
128
129pub struct SurfaceRunner<B: StoreBackend> {
131 surface_id: SurfaceId,
132 next_frame_id: FrameId,
133 backend: B,
134 profiler: FrameProfiler,
135}
136
137impl<B: StoreBackend> SurfaceRunner<B> {
138 pub fn new(surface_id: SurfaceId, viewport: Viewport, mut backend: B) -> Self {
139 backend.set_viewport(surface_id, viewport);
140 Self {
141 surface_id,
142 next_frame_id: FrameId(1),
143 backend,
144 profiler: FrameProfiler::new(512),
145 }
146 }
147
148 pub fn surface_id(&self) -> SurfaceId {
149 self.surface_id
150 }
151
152 pub fn backend(&self) -> &B {
153 &self.backend
154 }
155
156 pub fn backend_mut(&mut self) -> &mut B {
157 &mut self.backend
158 }
159
160 pub fn profiler(&self) -> &FrameProfiler {
161 &self.profiler
162 }
163
164 pub fn next_frame_id(&mut self) -> FrameId {
165 let id = self.next_frame_id;
166 self.next_frame_id = self.next_frame_id.next();
167 id
168 }
169
170 pub fn commit_store(
173 &mut self,
174 frame_id: FrameId,
175 reconcile_us: u64,
176 store: &NodeStore,
177 changes: &ChangeSet,
178 ) -> Result<FrameTiming, BackendError> {
179 let stats = self.backend.commit(store, changes)?;
180 let timing = FrameTiming {
181 frame_id,
182 op_count: stats.op_count,
183 mutation_count: stats.mutation_count,
184 reconcile_us: DurationUs(reconcile_us),
185 encode_us: DurationUs(0),
186 decode_us: stats.decode_us,
187 layout_us: stats.layout_us,
188 diff_us: stats.diff_us,
189 mount_us: stats.mount_us,
190 };
191 self.profiler.record(timing);
192 Ok(timing)
193 }
194
195 pub fn commit_with<F>(&mut self, flush: F) -> Result<FrameTiming, BackendError>
197 where
198 F: FnOnce(FrameId, &mut B) -> Result<(u64, BackendApplyStats), BackendError>,
199 {
200 let frame_id = self.next_frame_id();
201 let (reconcile_us, stats) = flush(frame_id, &mut self.backend)?;
202 let timing = FrameTiming {
203 frame_id,
204 op_count: stats.op_count,
205 mutation_count: stats.mutation_count,
206 reconcile_us: DurationUs(reconcile_us),
207 encode_us: DurationUs(0),
208 decode_us: stats.decode_us,
209 layout_us: stats.layout_us,
210 diff_us: stats.diff_us,
211 mount_us: stats.mount_us,
212 };
213 self.profiler.record(timing);
214 Ok(timing)
215 }
216}