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