cranpose_render_common/lib.rs
1//! Common rendering contracts shared between renderer backends.
2
3pub mod font_layout;
4pub mod geometry;
5pub mod graph;
6mod graph_hash;
7pub mod graph_scene;
8pub mod hit_graph;
9pub mod image_compare;
10pub mod layer_composition;
11pub mod layer_shadow;
12pub mod layer_transform;
13pub mod primitive_emit;
14pub mod raster_cache;
15pub mod render_contract;
16pub mod scene_builder;
17pub mod software_text_raster;
18pub mod style_shared;
19pub mod text_hyphenation;
20
21use cranpose_foundation::nodes::input::PointerEvent;
22use cranpose_ui::LayoutTree;
23use cranpose_ui_graphics::Size;
24
25pub use cranpose_ui_graphics::Brush;
26
27/// Trait implemented by hit-test targets stored inside a [`RenderScene`].
28pub trait HitTestTarget {
29 /// Dispatches a pointer event to this target's handlers.
30 fn dispatch(&self, event: PointerEvent);
31
32 /// Returns the NodeId associated with this hit target.
33 /// Used by HitPathTracker to cache stable identity instead of geometry.
34 fn node_id(&self) -> cranpose_core::NodeId;
35}
36
37/// Trait describing the minimal surface area required by the application
38/// shell to process pointer events and refresh the frame graph.
39pub trait RenderScene {
40 type HitTarget: HitTestTarget + Clone;
41
42 fn clear(&mut self);
43
44 /// Performs hit testing at the given coordinates.
45 /// Returns hit targets ordered by z-index (top-to-bottom).
46 fn hit_test(&self, x: f32, y: f32) -> Vec<Self::HitTarget>;
47
48 /// Returns NodeIds of all hit regions at the given coordinates.
49 /// This is a convenience method equivalent to `hit_test().map(|h| h.node_id())`.
50 fn hit_test_nodes(&self, x: f32, y: f32) -> Vec<cranpose_core::NodeId> {
51 self.hit_test(x, y)
52 .into_iter()
53 .map(|h| h.node_id())
54 .collect()
55 }
56
57 /// Finds a hit target by NodeId with fresh geometry from the current scene.
58 ///
59 /// This is the key method for HitPathTracker-style gesture handling:
60 /// - On PointerDown, we cache NodeIds (not geometry)
61 /// - On Move/Up/Cancel, we call this to get fresh HitTarget with current geometry
62 /// - Handler closures are preserved (same Rc), so internal state survives
63 ///
64 /// Returns None if the node no longer exists in the scene (e.g., removed during gesture).
65 fn find_target(&self, node_id: cranpose_core::NodeId) -> Option<Self::HitTarget>;
66}
67
68/// Abstraction implemented by concrete renderer backends.
69pub trait Renderer {
70 type Scene: RenderScene;
71 type Error;
72
73 fn scene(&self) -> &Self::Scene;
74 fn scene_mut(&mut self) -> &mut Self::Scene;
75
76 fn rebuild_scene(
77 &mut self,
78 layout_tree: &LayoutTree,
79 viewport: Size,
80 ) -> Result<(), Self::Error>;
81
82 /// Rebuilds the scene by traversing the LayoutNode tree directly via Applier.
83 ///
84 /// This is the new architecture that eliminates per-frame LayoutTree reconstruction.
85 /// Implementors must read layout state from LayoutNode.layout_state() directly.
86 fn rebuild_scene_from_applier(
87 &mut self,
88 applier: &mut cranpose_core::MemoryApplier,
89 root: cranpose_core::NodeId,
90 viewport: Size,
91 ) -> Result<(), Self::Error>;
92
93 /// Draw a development overlay (e.g., FPS counter) on top of the scene.
94 ///
95 /// This is called after rebuild_scene when dev options are enabled.
96 /// The text is drawn directly by the renderer without affecting composition.
97 ///
98 /// Default implementation does nothing.
99 fn draw_dev_overlay(&mut self, _text: &str, _viewport: Size) {
100 // Default: no-op
101 }
102}