Skip to main content

cranpose_ui/modifier/
slices.rs

1use std::fmt;
2use std::mem::size_of;
3use std::rc::Rc;
4
5use cranpose_foundation::{ModifierNodeChain, NodeCapabilities, PointerEvent};
6use cranpose_ui_graphics::{ColorFilter, GraphicsLayer, RenderEffect, RoundedCornerShape};
7
8use super::{ModifierChainHandle, Point};
9use crate::draw::DrawCommand;
10use crate::modifier::scroll::{MotionContextAnimatedNode, TranslatedContentContextNode};
11use crate::modifier::Modifier;
12use crate::modifier_nodes::{
13    BackgroundNode, ClipToBoundsNode, CornerShapeNode, DrawCommandNode, GraphicsLayerNode,
14    PaddingNode,
15};
16use crate::text::{TextLayoutOptions, TextStyle};
17use crate::text_field_modifier_node::TextFieldModifierNode;
18use crate::text_modifier_node::{TextModifierNode, TextPreparedLayoutHandle};
19use cranpose_ui_graphics::EdgeInsets;
20
21/// Snapshot of modifier node slices that impact draw and pointer subsystems.
22#[derive(Default)]
23pub struct ModifierNodeSlices {
24    draw_commands: Vec<DrawCommand>,
25    pointer_inputs: Vec<Rc<dyn Fn(PointerEvent)>>,
26    click_handlers: Vec<Rc<dyn Fn(Point)>>,
27    clip_to_bounds: bool,
28    motion_context_animated: bool,
29    translated_content_context: bool,
30    translated_content_context_identity: Option<usize>,
31    translated_content_offset_reader: Option<Rc<dyn Fn() -> Point>>,
32    text_content: Option<Rc<crate::text::AnnotatedString>>,
33    text_style: Option<TextStyle>,
34    text_layout_options: Option<TextLayoutOptions>,
35    prepared_text_layout: Option<TextPreparedLayoutHandle>,
36    graphics_layer: Option<GraphicsLayer>,
37    graphics_layer_resolver: Option<Rc<dyn Fn() -> GraphicsLayer>>,
38    corner_shape: Option<RoundedCornerShape>,
39    chain_guard: Option<Rc<ChainGuard>>,
40}
41
42struct ChainGuard {
43    _handle: ModifierChainHandle,
44}
45
46#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
47pub struct ModifierNodeSlicesDebugStats {
48    pub draw_command_count: usize,
49    pub draw_command_capacity: usize,
50    pub pointer_input_count: usize,
51    pub pointer_input_capacity: usize,
52    pub click_handler_count: usize,
53    pub click_handler_capacity: usize,
54    pub has_text_content: bool,
55    pub has_text_style: bool,
56    pub has_text_layout_options: bool,
57    pub has_prepared_text_layout: bool,
58    pub has_graphics_layer: bool,
59    pub has_graphics_layer_resolver: bool,
60    pub heap_bytes: usize,
61}
62
63impl Clone for ModifierNodeSlices {
64    fn clone(&self) -> Self {
65        Self {
66            draw_commands: self.draw_commands.clone(),
67            pointer_inputs: self.pointer_inputs.clone(),
68            click_handlers: self.click_handlers.clone(),
69            clip_to_bounds: self.clip_to_bounds,
70            motion_context_animated: self.motion_context_animated,
71            translated_content_context: self.translated_content_context,
72            translated_content_context_identity: self.translated_content_context_identity,
73            translated_content_offset_reader: self.translated_content_offset_reader.clone(),
74            text_content: self.text_content.clone(),
75            text_style: self.text_style.clone(),
76            text_layout_options: self.text_layout_options,
77            prepared_text_layout: self.prepared_text_layout.clone(),
78            graphics_layer: self.graphics_layer.clone(),
79            graphics_layer_resolver: self.graphics_layer_resolver.clone(),
80            corner_shape: self.corner_shape,
81            chain_guard: self.chain_guard.clone(),
82        }
83    }
84}
85
86/// Compose two nested graphics layers (`base` outer, `overlay` inner) into one
87/// flattened layer snapshot used by render pipelines.
88///
89/// Composition rules follow how nested transforms/effects behave visually:
90/// - Multiplicative: `alpha`, `scale`, `scale_x`, `scale_y`
91/// - Additive: `rotation_*`, `translation_*`
92/// - Boolean OR: `clip`
93/// - Overlay-wins when explicitly set: camera distance, transform origin,
94///   shadow elevation/colors, shape, compositing strategy, blend mode
95/// - Filters/effects are composed in draw order:
96///   - color filters are multiplied where possible
97///   - render effects chain inner-first then outer (`inner.then(outer)`)
98/// - Backdrop effect keeps the most local explicit backdrop effect because
99///   backdrop sampling cannot be safely flattened as a deterministic chain.
100fn merge_graphics_layers(base: GraphicsLayer, overlay: GraphicsLayer) -> GraphicsLayer {
101    GraphicsLayer {
102        alpha: (base.alpha * overlay.alpha).clamp(0.0, 1.0),
103        scale: base.scale * overlay.scale,
104        scale_x: base.scale_x * overlay.scale_x,
105        scale_y: base.scale_y * overlay.scale_y,
106        rotation_x: base.rotation_x + overlay.rotation_x,
107        rotation_y: base.rotation_y + overlay.rotation_y,
108        rotation_z: base.rotation_z + overlay.rotation_z,
109        camera_distance: overlay.camera_distance,
110        transform_origin: overlay.transform_origin,
111        translation_x: base.translation_x + overlay.translation_x,
112        translation_y: base.translation_y + overlay.translation_y,
113        shadow_elevation: overlay.shadow_elevation,
114        ambient_shadow_color: overlay.ambient_shadow_color,
115        spot_shadow_color: overlay.spot_shadow_color,
116        shape: overlay.shape,
117        clip: base.clip || overlay.clip,
118        compositing_strategy: overlay.compositing_strategy,
119        blend_mode: overlay.blend_mode,
120        color_filter: compose_color_filters(base.color_filter, overlay.color_filter),
121        // Modifiers are traversed outer -> inner. Layer effects therefore compose
122        // inner-first, then outer, matching nested layer rendering semantics.
123        render_effect: compose_render_effects(base.render_effect, overlay.render_effect),
124        // Backdrop effects cannot be represented as a deterministic chain on a
125        // flattened single layer, so keep the most local explicit backdrop.
126        backdrop_effect: overlay.backdrop_effect.or(base.backdrop_effect),
127    }
128}
129
130fn compose_render_effects(
131    outer: Option<RenderEffect>,
132    inner: Option<RenderEffect>,
133) -> Option<RenderEffect> {
134    match (outer, inner) {
135        (None, None) => None,
136        (Some(effect), None) | (None, Some(effect)) => Some(effect),
137        (Some(outer_effect), Some(inner_effect)) => Some(inner_effect.then(outer_effect)),
138    }
139}
140
141fn compose_color_filters(
142    base: Option<ColorFilter>,
143    overlay: Option<ColorFilter>,
144) -> Option<ColorFilter> {
145    match (base, overlay) {
146        (None, None) => None,
147        (Some(filter), None) | (None, Some(filter)) => Some(filter),
148        (Some(filter), Some(next)) => Some(filter.compose(next)),
149    }
150}
151
152impl ModifierNodeSlices {
153    pub fn draw_commands(&self) -> &[DrawCommand] {
154        &self.draw_commands
155    }
156
157    pub fn pointer_inputs(&self) -> &[Rc<dyn Fn(PointerEvent)>] {
158        &self.pointer_inputs
159    }
160
161    pub fn click_handlers(&self) -> &[Rc<dyn Fn(Point)>] {
162        &self.click_handlers
163    }
164
165    pub fn clip_to_bounds(&self) -> bool {
166        self.clip_to_bounds
167    }
168
169    pub fn motion_context_animated(&self) -> bool {
170        self.motion_context_animated
171    }
172
173    pub fn translated_content_context(&self) -> bool {
174        self.translated_content_context
175    }
176
177    pub fn translated_content_context_identity(&self) -> Option<usize> {
178        self.translated_content_context_identity
179    }
180
181    pub fn translated_content_offset(&self) -> Option<Point> {
182        self.translated_content_offset_reader
183            .as_ref()
184            .map(|reader| reader())
185    }
186
187    pub fn text_content(&self) -> Option<&str> {
188        self.text_content.as_ref().map(|a| a.text.as_str())
189    }
190
191    pub fn annotated_text(&self) -> Option<&crate::text::AnnotatedString> {
192        self.text_content.as_deref()
193    }
194
195    pub fn annotated_string(&self) -> Option<crate::text::AnnotatedString> {
196        self.annotated_text().cloned()
197    }
198
199    pub fn text_style(&self) -> Option<&TextStyle> {
200        self.text_style.as_ref()
201    }
202
203    pub fn text_layout_options(&self) -> Option<TextLayoutOptions> {
204        self.text_layout_options
205    }
206
207    pub fn prepare_text_layout(
208        &self,
209        max_width: Option<f32>,
210    ) -> Option<crate::text::PreparedTextLayout> {
211        if let Some(handle) = &self.prepared_text_layout {
212            return Some(handle.prepare(max_width));
213        }
214
215        let text = self.annotated_text()?;
216        let style = self.text_style.clone().unwrap_or_default();
217        Some(crate::text::prepare_text_layout(
218            text,
219            &style,
220            self.text_layout_options.unwrap_or_default(),
221            max_width,
222        ))
223    }
224
225    pub fn graphics_layer(&self) -> Option<GraphicsLayer> {
226        if let Some(resolve) = &self.graphics_layer_resolver {
227            Some(resolve())
228        } else {
229            self.graphics_layer.clone()
230        }
231    }
232
233    pub fn corner_shape(&self) -> Option<RoundedCornerShape> {
234        self.corner_shape
235    }
236
237    fn push_graphics_layer(
238        &mut self,
239        layer: GraphicsLayer,
240        resolver: Option<Rc<dyn Fn() -> GraphicsLayer>>,
241    ) {
242        let existing_snapshot = self.graphics_layer.clone();
243        let next_snapshot = existing_snapshot
244            .as_ref()
245            .map(|current| merge_graphics_layers(current.clone(), layer.clone()))
246            .unwrap_or_else(|| layer.clone());
247        let existing_resolver = self.graphics_layer_resolver.clone();
248
249        self.graphics_layer = Some(next_snapshot);
250        self.graphics_layer_resolver = match (existing_resolver, resolver) {
251            (None, None) => None,
252            (Some(current_resolver), None) => {
253                let layer = layer.clone();
254                Some(Rc::new(move || {
255                    merge_graphics_layers(current_resolver(), layer.clone())
256                }))
257            }
258            (None, Some(next_resolver)) => {
259                let base = existing_snapshot.unwrap_or_default();
260                Some(Rc::new(move || {
261                    merge_graphics_layers(base.clone(), next_resolver())
262                }))
263            }
264            (Some(current_resolver), Some(next_resolver)) => Some(Rc::new(move || {
265                merge_graphics_layers(current_resolver(), next_resolver())
266            })),
267        };
268    }
269
270    pub fn with_chain_guard(mut self, handle: ModifierChainHandle) -> Self {
271        self.chain_guard = Some(Rc::new(ChainGuard { _handle: handle }));
272        self
273    }
274
275    pub fn debug_stats(&self) -> ModifierNodeSlicesDebugStats {
276        let draw_command_bytes = self.draw_commands.capacity() * size_of::<DrawCommand>();
277        let pointer_input_bytes =
278            self.pointer_inputs.capacity() * size_of::<Rc<dyn Fn(PointerEvent)>>();
279        let click_handler_bytes = self.click_handlers.capacity() * size_of::<Rc<dyn Fn(Point)>>();
280        ModifierNodeSlicesDebugStats {
281            draw_command_count: self.draw_commands.len(),
282            draw_command_capacity: self.draw_commands.capacity(),
283            pointer_input_count: self.pointer_inputs.len(),
284            pointer_input_capacity: self.pointer_inputs.capacity(),
285            click_handler_count: self.click_handlers.len(),
286            click_handler_capacity: self.click_handlers.capacity(),
287            has_text_content: self.text_content.is_some(),
288            has_text_style: self.text_style.is_some(),
289            has_text_layout_options: self.text_layout_options.is_some(),
290            has_prepared_text_layout: self.prepared_text_layout.is_some(),
291            has_graphics_layer: self.graphics_layer.is_some(),
292            has_graphics_layer_resolver: self.graphics_layer_resolver.is_some(),
293            heap_bytes: draw_command_bytes + pointer_input_bytes + click_handler_bytes,
294        }
295    }
296
297    /// Resets the slice collection for reuse, retaining vector capacity.
298    pub fn clear(&mut self) {
299        self.draw_commands.clear();
300        self.pointer_inputs.clear();
301        self.click_handlers.clear();
302        self.clip_to_bounds = false;
303        self.motion_context_animated = false;
304        self.translated_content_context = false;
305        self.translated_content_context_identity = None;
306        self.translated_content_offset_reader = None;
307        self.text_content = None;
308        self.text_style = None;
309        self.text_layout_options = None;
310        self.prepared_text_layout = None;
311        self.graphics_layer = None;
312        self.graphics_layer_resolver = None;
313        self.corner_shape = None;
314        self.chain_guard = None;
315    }
316}
317
318impl fmt::Debug for ModifierNodeSlices {
319    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
320        f.debug_struct("ModifierNodeSlices")
321            .field("draw_commands", &self.draw_commands.len())
322            .field("pointer_inputs", &self.pointer_inputs.len())
323            .field("click_handlers", &self.click_handlers.len())
324            .field("clip_to_bounds", &self.clip_to_bounds)
325            .field("motion_context_animated", &self.motion_context_animated)
326            .field(
327                "translated_content_context",
328                &self.translated_content_context,
329            )
330            .field(
331                "translated_content_context_identity",
332                &self.translated_content_context_identity,
333            )
334            .field(
335                "translated_content_offset",
336                &self.translated_content_offset(),
337            )
338            .field("text_content", &self.text_content)
339            .field("text_style", &self.text_style)
340            .field("text_layout_options", &self.text_layout_options)
341            .field("prepared_text_layout", &self.prepared_text_layout.is_some())
342            .field("graphics_layer", &self.graphics_layer)
343            .field(
344                "graphics_layer_resolver",
345                &self.graphics_layer_resolver.is_some(),
346            )
347            .field("corner_shape", &self.corner_shape)
348            .finish()
349    }
350}
351
352/// Collects modifier node slices directly from a reconciled [`ModifierNodeChain`].
353pub fn collect_modifier_slices(chain: &ModifierNodeChain) -> ModifierNodeSlices {
354    let mut slices = ModifierNodeSlices::default();
355    collect_modifier_slices_into(chain, &mut slices);
356    slices
357}
358
359/// Collects modifier node slices into an existing buffer to reuse allocations.
360///
361/// Single-pass: iterates the chain once instead of 4 separate capability-filtered
362/// traversals, reducing per-node `RefCell::borrow()` overhead.
363pub fn collect_modifier_slices_into(chain: &ModifierNodeChain, slices: &mut ModifierNodeSlices) {
364    slices.clear();
365
366    let caps = chain.capabilities();
367    let has_pointer = caps.intersects(NodeCapabilities::POINTER_INPUT);
368    let has_draw = caps.intersects(NodeCapabilities::DRAW);
369    let has_layout = caps.intersects(NodeCapabilities::LAYOUT);
370
371    if !has_pointer && !has_draw && !has_layout {
372        return;
373    }
374
375    let mut background_color = None;
376    let mut background_insert_index = None::<usize>;
377    let mut corner_shape = None;
378    let mut padding = EdgeInsets::default();
379
380    for node_ref in chain.head_to_tail() {
381        let node_caps = node_ref.kind_set();
382
383        node_ref.with_node(|node| {
384            let any = node.as_any();
385
386            // POINTER_INPUT collection
387            if has_pointer && node_caps.intersects(NodeCapabilities::POINTER_INPUT) {
388                if let Some(handler) = node
389                    .as_pointer_input_node()
390                    .and_then(|n| n.pointer_input_handler())
391                {
392                    slices.pointer_inputs.push(handler);
393                }
394            }
395
396            // DRAW collection
397            if has_draw && node_caps.intersects(NodeCapabilities::DRAW) {
398                if let Some(bg_node) = any.downcast_ref::<BackgroundNode>() {
399                    background_color = Some(bg_node.color());
400                    background_insert_index = Some(slices.draw_commands.len());
401                    if bg_node.shape().is_some() {
402                        corner_shape = bg_node.shape();
403                    }
404                }
405
406                if let Some(shape_node) = any.downcast_ref::<CornerShapeNode>() {
407                    corner_shape = Some(shape_node.shape());
408                }
409
410                if let Some(commands) = any.downcast_ref::<DrawCommandNode>() {
411                    slices.draw_commands.extend(commands.observed_commands());
412                }
413
414                if let Some(draw_node) = node.as_draw_node() {
415                    if let Some(closure) = draw_node.create_draw_closure() {
416                        slices.draw_commands.push(DrawCommand::Overlay(closure));
417                    } else {
418                        use cranpose_ui_graphics::{DrawScope as _, DrawScopeDefault};
419                        let mut scope = DrawScopeDefault::new(crate::modifier::Size {
420                            width: 0.0,
421                            height: 0.0,
422                        });
423                        draw_node.draw(&mut scope);
424                        let primitives = scope.into_primitives();
425                        if !primitives.is_empty() {
426                            let draw_cmd =
427                                Rc::new(move |_size: crate::modifier::Size| primitives.clone());
428                            slices.draw_commands.push(DrawCommand::Overlay(draw_cmd));
429                        }
430                    }
431                }
432
433                if let Some(layer_node) = any.downcast_ref::<GraphicsLayerNode>() {
434                    slices.push_graphics_layer(
435                        layer_node.layer_snapshot(),
436                        layer_node.layer_resolver(),
437                    );
438                }
439
440                if any.is::<ClipToBoundsNode>() {
441                    slices.clip_to_bounds = true;
442                }
443            }
444
445            // LAYOUT collection (padding + text)
446            if has_layout && node_caps.intersects(NodeCapabilities::LAYOUT) {
447                if let Some(padding_node) = any.downcast_ref::<PaddingNode>() {
448                    let p = padding_node.padding();
449                    padding.left += p.left;
450                    padding.top += p.top;
451                    padding.right += p.right;
452                    padding.bottom += p.bottom;
453                }
454
455                if let Some(motion_context_node) = any.downcast_ref::<MotionContextAnimatedNode>() {
456                    slices.motion_context_animated = motion_context_node.is_active();
457                }
458
459                if let Some(translated_content_node) =
460                    any.downcast_ref::<TranslatedContentContextNode>()
461                {
462                    slices.translated_content_context = translated_content_node.is_active();
463                    slices.translated_content_context_identity =
464                        Some(translated_content_node.identity());
465                    slices.translated_content_offset_reader =
466                        translated_content_node.content_offset_reader();
467                }
468
469                if let Some(text_node) = any.downcast_ref::<TextModifierNode>() {
470                    slices.text_content = Some(text_node.annotated_text());
471                    slices.text_style = Some(text_node.style().clone());
472                    slices.text_layout_options = Some(text_node.options());
473                    slices.prepared_text_layout = Some(text_node.prepared_layout_handle());
474                }
475
476                if let Some(text_field_node) = any.downcast_ref::<TextFieldModifierNode>() {
477                    let text = text_field_node.text();
478                    slices.text_content = Some(Rc::new(crate::text::AnnotatedString::from(text)));
479                    slices.text_style = Some(text_field_node.style().clone());
480                    slices.text_layout_options = Some(TextLayoutOptions::default());
481                    slices.prepared_text_layout = None;
482
483                    text_field_node.set_content_offset(padding.left);
484                    text_field_node.set_content_y_offset(padding.top);
485                }
486            }
487        });
488    }
489
490    slices.corner_shape = corner_shape;
491
492    // Convert background + shape into a draw command
493    if let Some(color) = background_color {
494        let draw_cmd = Rc::new(move |size: crate::modifier::Size| {
495            use crate::modifier::{Brush, Rect};
496            use cranpose_ui_graphics::DrawPrimitive;
497
498            let brush = Brush::solid(color);
499            let rect = Rect {
500                x: 0.0,
501                y: 0.0,
502                width: size.width,
503                height: size.height,
504            };
505
506            if let Some(shape) = corner_shape {
507                let radii = shape.resolve(size.width, size.height);
508                vec![DrawPrimitive::RoundRect { rect, brush, radii }]
509            } else {
510                vec![DrawPrimitive::Rect { rect, brush }]
511            }
512        });
513
514        let insert_index = background_insert_index
515            .unwrap_or(0)
516            .min(slices.draw_commands.len());
517        slices
518            .draw_commands
519            .insert(insert_index, DrawCommand::Behind(draw_cmd));
520    }
521}
522
523/// Collects modifier node slices by instantiating a temporary node chain from a [`Modifier`].
524pub fn collect_slices_from_modifier(modifier: &Modifier) -> ModifierNodeSlices {
525    let mut handle = ModifierChainHandle::new();
526    let _ = handle.update(modifier);
527    collect_modifier_slices(handle.chain()).with_chain_guard(handle)
528}