Skip to main content

cranpose_ui/modifier/
slices.rs

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