Skip to main content

cranpose_render_common/
lib.rs

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