Skip to main content

cranpose_render_common/
lib.rs

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