Skip to main content

cranpose_render_common/
lib.rs

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