Skip to main content

repose_core/
modifier.rs

1use std::cell::Cell;
2use std::rc::Rc;
3
4use taffy::{AlignContent, AlignItems, AlignSelf, FlexDirection, FlexWrap, JustifyContent};
5
6use crate::animation::AnimationSpec;
7use crate::{Brush, Color, PointerEvent, Size, Transform, Vec2};
8
9/// State-driven colors for interactive components.
10/// The layout engine selects the appropriate color based on hover/press/disabled state
11/// and animates transitions between them.
12#[derive(Clone, Copy, Debug)]
13pub struct StateColors {
14    pub default: Color,
15    pub hovered: Color,
16    pub pressed: Color,
17    pub disabled: Color,
18}
19
20/// State-driven elevation for interactive components.
21#[derive(Clone, Copy, Debug)]
22pub struct StateElevation {
23    pub default: f32,
24    pub hovered: f32,
25    pub pressed: f32,
26    pub disabled: f32,
27}
28
29macro_rules! merge_opts {
30    ($dst:ident, $src:ident; $($f:ident),+ $(,)?) => {
31        $( $dst.$f = $src.$f.or($dst.$f); )+
32    };
33}
34macro_rules! merge_flags {
35    ($dst:ident, $src:ident; $($f:ident),+ $(,)?) => {
36        $( $dst.$f |= $src.$f; )+
37    };
38}
39
40macro_rules! impl_option_fields {
41    ($ty:ty, $fn:ident) => {
42        impl $ty {
43            $fn!(replace);
44        }
45    };
46    ($ty:ident) => {
47        impl $ty {
48            /// Chain another modifier's settings onto this one.
49            /// Useful for creating reusable modifier templates.
50            pub fn then(mut self, other: Self) -> Self {
51                merge_opts!(self, other;
52                    key, size, width, height,
53                    padding, padding_values,
54                    min_width, min_height, max_width, max_height,
55                    background, state_colors, state_elevation, border,
56                    flex_grow, flex_shrink, flex_basis, flex_wrap, flex_dir,
57                    gap, row_gap, column_gap,
58                    align_self, justify_content, align_items_container, align_content,
59                    clip_rounded, render_z_index,
60                    on_scroll,
61                    on_pointer_down, on_pointer_move, on_pointer_up,
62                    on_pointer_enter, on_pointer_leave,
63                    semantics, alpha, transform,
64                    grid, grid_col_span, grid_row_span,
65                    position_type,
66                    offset_left, offset_right, offset_top, offset_bottom,
67                    margin_left, margin_right, margin_top, margin_bottom,
68                    aspect_ratio, painter,
69                    on_drag_start, on_drag_end, on_drag_enter, on_drag_over, on_drag_leave, on_drop,
70                    on_action, cursor, animate_content_size, focus_requester, on_focus_changed,
71                    text_input,
72                );
73                merge_flags!(self, other;
74                    fill_max, fill_max_w, fill_max_h,
75                    hit_passthrough, input_blocker, repaint_boundary, click, disabled,
76                );
77                if other.z_index != 0.0 {
78                    self.z_index = other.z_index;
79                }
80                self
81            }
82        }
83    };
84}
85
86#[derive(Clone, Debug)]
87pub struct Border {
88    pub width: f32,
89    pub color: Color,
90    pub radius: f32,
91}
92
93#[derive(Clone, Copy, Debug, Default)]
94pub struct PaddingValues {
95    pub left: f32,
96    pub right: f32,
97    pub top: f32,
98    pub bottom: f32,
99}
100
101#[derive(Clone, Debug)]
102pub struct GridConfig {
103    pub columns: usize,
104    pub row_gap: f32,
105    pub column_gap: f32,
106}
107
108/// Drop-shadow parameters applied to a graphics layer.
109///
110/// `blur_radius` is the Gaussian blur radius in dp (1.0 = subtle, 8.0 = soft,
111/// 16.0 = very diffuse). `offset_y` is the vertical offset of the shadow in dp
112/// (positive = below the layer). `color` is the shadow color (premultiplied
113/// alpha controls shadow darkness).
114#[derive(Clone, Copy, Debug)]
115pub struct ShadowSpec {
116    pub blur_radius: f32,
117    pub offset_y: f32,
118    pub color: Color,
119}
120
121#[derive(Clone, Copy, Debug)]
122#[non_exhaustive]
123pub enum PositionType {
124    Relative,
125    Absolute,
126}
127
128/// Configuration for a text input field.
129#[derive(Clone)]
130pub struct TextInputConfig {
131    pub hint: String,
132    pub multiline: bool,
133    pub on_change: Option<Rc<dyn Fn(String)>>,
134    pub on_submit: Option<Rc<dyn Fn(String)>>,
135    pub focus_tracker: Option<Rc<Cell<bool>>>,
136    pub value: String,
137    pub visual_transformation: Option<Rc<dyn crate::text::VisualTransformation>>,
138    pub keyboard_type: Option<crate::text::KeyboardType>,
139    pub ime_action: Option<crate::text::ImeAction>,
140}
141
142impl std::fmt::Debug for TextInputConfig {
143    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144        let mut s = f.debug_struct("TextInputConfig");
145        s.field("hint", &self.hint);
146        s.field("multiline", &self.multiline);
147        if self.on_change.is_some() { s.field("on_change", &"…"); }
148        if self.on_submit.is_some() { s.field("on_submit", &"…"); }
149        if self.focus_tracker.is_some() { s.field("focus_tracker", &"…"); }
150        s.field("value", &self.value);
151        if self.visual_transformation.is_some() { s.field("visual_transformation", &"…"); }
152        s.field("keyboard_type", &self.keyboard_type);
153        s.field("ime_action", &self.ime_action);
154        s.finish()
155    }
156}
157
158#[derive(Clone, Default)]
159pub struct Modifier {
160    /// Optional stable identity key for this view node.
161    ///
162    /// If set, `layout_and_paint` will prefer this over child index when assigning stable ViewIds.
163    /// This is the “escape hatch” for dynamic lists / conditional UI where index-based identity
164    /// would otherwise shift.
165    pub key: Option<u64>,
166
167    pub size: Option<Size>,
168    pub width: Option<f32>,
169    pub height: Option<f32>,
170    pub fill_max: bool,
171    pub fill_max_w: bool,
172    pub fill_max_h: bool,
173    pub padding: Option<f32>,
174    pub padding_values: Option<PaddingValues>,
175    pub min_width: Option<f32>,
176    pub min_height: Option<f32>,
177    pub max_width: Option<f32>,
178    pub max_height: Option<f32>,
179    pub background: Option<Brush>,
180    pub state_colors: Option<StateColors>,
181    pub state_elevation: Option<StateElevation>,
182
183    pub border: Option<Border>,
184    pub flex_grow: Option<f32>,
185    pub flex_shrink: Option<f32>,
186    pub flex_basis: Option<f32>,
187    pub flex_wrap: Option<FlexWrap>,
188    pub flex_dir: Option<FlexDirection>,
189    pub gap: Option<f32>,
190    pub row_gap: Option<f32>,
191    pub column_gap: Option<f32>,
192    pub align_self: Option<AlignSelf>,
193    pub justify_content: Option<JustifyContent>,
194    pub align_items_container: Option<AlignItems>,
195    pub align_content: Option<AlignContent>,
196    pub clip_rounded: Option<f32>,
197    /// Z-index for hit-testing order (higher = receives events first).
198    pub z_index: f32,
199    /// Z-index for render order (higher = painted on top). If None, uses tree order.
200    pub render_z_index: Option<f32>,
201    /// If true, this view does not create hit regions.
202    pub hit_passthrough: bool,
203    /// If true, this view blocks pointer/touch input for hits below it.
204    pub input_blocker: bool,
205    pub repaint_boundary: bool,
206    pub click: bool,
207    /// When true, the component ignores pointer events and appears disabled.
208    pub disabled: bool,
209    pub on_scroll: Option<Rc<dyn Fn(Vec2) -> Vec2>>,
210    pub on_pointer_down: Option<Rc<dyn Fn(PointerEvent)>>,
211    pub on_pointer_move: Option<Rc<dyn Fn(PointerEvent)>>,
212    pub on_pointer_up: Option<Rc<dyn Fn(PointerEvent)>>,
213    pub on_pointer_enter: Option<Rc<dyn Fn(PointerEvent)>>,
214    pub on_pointer_leave: Option<Rc<dyn Fn(PointerEvent)>>,
215    pub semantics: Option<crate::Semantics>,
216    pub alpha: Option<f32>,
217    pub graphics_layer: Option<f32>,
218    pub shadow: Option<ShadowSpec>,
219    pub transform: Option<Transform>,
220    pub grid: Option<GridConfig>,
221    pub grid_col_span: Option<u16>,
222    pub grid_row_span: Option<u16>,
223    pub position_type: Option<PositionType>,
224    pub offset_left: Option<f32>,
225    pub offset_right: Option<f32>,
226    pub offset_top: Option<f32>,
227    pub offset_bottom: Option<f32>,
228    pub margin_left: Option<f32>,
229    pub margin_right: Option<f32>,
230    pub margin_top: Option<f32>,
231    pub margin_bottom: Option<f32>,
232    pub aspect_ratio: Option<f32>,
233    pub painter: Option<Rc<dyn Fn(&mut crate::Scene, crate::Rect, f32)>>,
234
235    // Drag-drop (internal)
236    pub on_drag_start: Option<Rc<dyn Fn(crate::dnd::DragStart) -> Option<crate::dnd::DragPayload>>>,
237    pub on_drag_end: Option<Rc<dyn Fn(crate::dnd::DragEnd)>>,
238    pub on_drag_enter: Option<Rc<dyn Fn(crate::dnd::DragOver)>>,
239    pub on_drag_over: Option<Rc<dyn Fn(crate::dnd::DragOver)>>,
240    pub on_drag_leave: Option<Rc<dyn Fn(crate::dnd::DragOver)>>,
241    pub on_drop: Option<Rc<dyn Fn(crate::dnd::DropEvent) -> bool>>,
242
243    pub on_action: Option<Rc<dyn Fn(crate::shortcuts::Action) -> bool>>,
244
245    /// Cursor icon hint for desktop/web runners.
246    pub cursor: Option<crate::CursorIcon>,
247
248    /// If set, the size of this node will smoothly animate to its target size
249    /// whenever content size changes. Uses the provided animation spec.
250    pub animate_content_size: Option<AnimationSpec>,
251
252    /// A `FocusRequester` handle that will be associated with this view.
253    /// When the requester's `request_focus()` is called, keyboard focus will
254    /// move to this view.
255    pub focus_requester: Option<crate::runtime::FocusRequester>,
256
257    /// Called when this view gains or loses focus. The boolean parameter is
258    /// `true` when focused, `false` when unfocused.
259    pub on_focus_changed: Option<Rc<dyn Fn(bool)>>,
260
261    /// Text input configuration. When set, this box acts as a text input field.
262    pub text_input: Option<TextInputConfig>,
263}
264
265impl std::fmt::Debug for Modifier {
266    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
267        let mut s = f.debug_struct("Modifier");
268
269        macro_rules! opt_val {
270            ($($name:ident),+ $(,)?) => {
271                $( if self.$name.is_some() { s.field(stringify!($name), &self.$name); } )+
272            };
273        }
274        opt_val!(
275            key,
276            size,
277            width,
278            height,
279            padding,
280            padding_values,
281            min_width,
282            min_height,
283            max_width,
284            max_height,
285            background,
286            state_colors,
287            state_elevation,
288            border,
289            flex_grow,
290            flex_shrink,
291            flex_basis,
292            flex_wrap,
293            flex_dir,
294            gap,
295            row_gap,
296            column_gap,
297            align_self,
298            justify_content,
299            align_items_container,
300            align_content,
301            clip_rounded,
302            render_z_index,
303            semantics,
304            alpha,
305            transform,
306            grid,
307            grid_col_span,
308            grid_row_span,
309            position_type,
310            offset_left,
311            offset_right,
312            offset_top,
313            offset_bottom,
314            margin_left,
315            margin_right,
316            margin_top,
317            margin_bottom,
318            aspect_ratio,
319            cursor,
320            animate_content_size,
321        );
322
323        macro_rules! opt_cb {
324            ($($name:ident),+ $(,)?) => {
325                $( if self.$name.is_some() { s.field(stringify!($name), &"…"); } )+
326            };
327        }
328        opt_cb!(
329            on_scroll,
330            on_pointer_down,
331            on_pointer_move,
332            on_pointer_up,
333            on_pointer_enter,
334            on_pointer_leave,
335            painter,
336            on_drag_start,
337            on_drag_end,
338            on_drag_enter,
339            on_drag_over,
340            on_drag_leave,
341            on_drop,
342            on_action,
343            on_focus_changed,
344            text_input,
345        );
346
347        macro_rules! flag {
348            ($($name:ident),+ $(,)?) => {
349                $( if self.$name { s.field(stringify!($name), &true); } )+
350            };
351        }
352        flag!(
353            fill_max,
354            fill_max_w,
355            fill_max_h,
356            hit_passthrough,
357            input_blocker,
358            repaint_boundary,
359            click,
360            disabled,
361        );
362
363        if self.z_index != 0.0 {
364            s.field("z_index", &self.z_index);
365        }
366
367        s.finish()
368    }
369}
370
371impl_option_fields!(Modifier);
372
373impl Modifier {
374    pub fn new() -> Self {
375        Self::default()
376    }
377
378    /// Attaches a stable identity key to this view node.
379    /// Use for dynamic lists / conditional UI where index-based identity can shift.
380    pub fn key(mut self, key: u64) -> Self {
381        self.key = Some(key);
382        self
383    }
384
385    pub fn size(mut self, w: f32, h: f32) -> Self {
386        self.size = Some(Size {
387            width: w,
388            height: h,
389        });
390        self
391    }
392    pub fn width(mut self, w: f32) -> Self {
393        self.width = Some(w);
394        self
395    }
396    pub fn height(mut self, h: f32) -> Self {
397        self.height = Some(h);
398        self
399    }
400    pub fn fill_max_size(mut self) -> Self {
401        self.fill_max = true;
402        self
403    }
404    pub fn fill_max_width(mut self) -> Self {
405        self.fill_max_w = true;
406        self
407    }
408    pub fn fill_max_height(mut self) -> Self {
409        self.fill_max_h = true;
410        self
411    }
412    pub fn padding(mut self, v: f32) -> Self {
413        self.padding = Some(v);
414        self
415    }
416    pub fn padding_values(mut self, padding: PaddingValues) -> Self {
417        self.padding_values = Some(padding);
418        self
419    }
420    /// Add padding equal to the current IME (soft keyboard) bottom inset.
421    /// Combine with `system_bars_padding()` to handle both system bars and keyboard.
422    pub fn ime_padding(mut self) -> Self {
423        let insets = crate::locals::window_insets();
424        let scale = crate::locals::density().scale * crate::locals::ui_scale().0;
425        let mut p = self.padding_values.unwrap_or_default();
426        p.bottom += insets.ime_bottom / scale;
427        self.padding_values = Some(p);
428        self
429    }
430    /// Add padding equal to the current system bar insets (status bar top, nav bar bottom).
431    pub fn system_bars_padding(mut self) -> Self {
432        let insets = crate::locals::window_insets();
433        let scale = crate::locals::density().scale * crate::locals::ui_scale().0;
434        let mut p = self.padding_values.unwrap_or_default();
435        p.top += insets.top / scale;
436        p.bottom += insets.bottom / scale;
437        self.padding_values = Some(p);
438        self
439    }
440    /// Add status bar inset as top padding.
441    pub fn status_bars_padding(mut self) -> Self {
442        let insets = crate::locals::window_insets();
443        let scale = crate::locals::density().scale * crate::locals::ui_scale().0;
444        let mut p = self.padding_values.unwrap_or_default();
445        p.top += insets.top / scale;
446        self.padding_values = Some(p);
447        self
448    }
449    /// Add navigation bar inset as bottom padding.
450    pub fn navigation_bars_padding(mut self) -> Self {
451        let insets = crate::locals::window_insets();
452        let scale = crate::locals::density().scale * crate::locals::ui_scale().0;
453        let mut p = self.padding_values.unwrap_or_default();
454        p.bottom += insets.bottom / scale;
455        self.padding_values = Some(p);
456        self
457    }
458    pub fn min_size(mut self, w: f32, h: f32) -> Self {
459        self.min_width = Some(w);
460        self.min_height = Some(h);
461        self
462    }
463    pub fn max_size(mut self, w: f32, h: f32) -> Self {
464        self.max_width = Some(w);
465        self.max_height = Some(h);
466        self
467    }
468    pub fn min_width(mut self, w: f32) -> Self {
469        self.min_width = Some(w);
470        self
471    }
472    pub fn min_height(mut self, h: f32) -> Self {
473        self.min_height = Some(h);
474        self
475    }
476    pub fn max_width(mut self, w: f32) -> Self {
477        self.max_width = Some(w);
478        self
479    }
480    pub fn max_height(mut self, h: f32) -> Self {
481        self.max_height = Some(h);
482        self
483    }
484    /// Set a solid color background.
485    pub fn background(mut self, color: Color) -> Self {
486        self.background = Some(Brush::Solid(color));
487        self
488    }
489    /// Set a brush (solid, gradient, etc.) background.
490    pub fn background_brush(mut self, brush: Brush) -> Self {
491        self.background = Some(brush);
492        self
493    }
494    pub fn border(mut self, width: f32, color: Color, radius: f32) -> Self {
495        self.border = Some(Border {
496            width,
497            color,
498            radius,
499        });
500        self
501    }
502    pub fn flex_grow(mut self, v: f32) -> Self {
503        self.flex_grow = Some(v);
504        self
505    }
506    pub fn flex_shrink(mut self, v: f32) -> Self {
507        self.flex_shrink = Some(v);
508        self
509    }
510    pub fn flex_basis(mut self, v: f32) -> Self {
511        self.flex_basis = Some(v);
512        self
513    }
514    pub fn flex_wrap(mut self, w: FlexWrap) -> Self {
515        self.flex_wrap = Some(w);
516        self
517    }
518    pub fn flex_dir(mut self, d: FlexDirection) -> Self {
519        self.flex_dir = Some(d);
520        self
521    }
522    pub fn gap(mut self, v: f32) -> Self {
523        let v = v.max(0.0);
524        self.gap = Some(v);
525        self.row_gap = Some(v);
526        self.column_gap = Some(v);
527        self
528    }
529    pub fn row_gap(mut self, v: f32) -> Self {
530        self.row_gap = Some(v.max(0.0));
531        self
532    }
533    pub fn column_gap(mut self, v: f32) -> Self {
534        self.column_gap = Some(v.max(0.0));
535        self
536    }
537    pub fn align_self(mut self, a: AlignSelf) -> Self {
538        self.align_self = Some(a);
539        self
540    }
541    pub fn align_self_center(mut self) -> Self {
542        self.align_self = Some(AlignSelf::Center);
543        self
544    }
545    pub fn justify_content(mut self, j: JustifyContent) -> Self {
546        self.justify_content = Some(j);
547        self
548    }
549    pub fn align_items(mut self, a: AlignItems) -> Self {
550        self.align_items_container = Some(a);
551        self
552    }
553    pub fn align_content(mut self, a: AlignContent) -> Self {
554        self.align_content = Some(a);
555        self
556    }
557    pub fn clip_rounded(mut self, radius: f32) -> Self {
558        self.clip_rounded = Some(radius);
559        self
560    }
561    pub fn z_index(mut self, z: f32) -> Self {
562        self.z_index = z;
563        self
564    }
565
566    /// Sets the render z-index for this view. Higher values are painted on top.
567    /// Unlike `z_index` (which only affects hit-testing), this affects visual layering.
568    pub fn render_z_index(mut self, z: f32) -> Self {
569        self.render_z_index = Some(z);
570        self
571    }
572
573    /// Prevent pointer/touch from reaching lower layers.
574    pub fn input_blocker(mut self) -> Self {
575        self.input_blocker = true;
576        self
577    }
578
579    pub fn hit_passthrough(mut self) -> Self {
580        self.hit_passthrough = true;
581        self
582    }
583    pub fn clickable(mut self) -> Self {
584        self.click = true;
585        self
586    }
587    /// Set state-driven background colors for hover, press, disabled states.
588    /// The layout engine automatically selects and animates between these based on interaction.
589    pub fn state_colors(mut self, colors: StateColors) -> Self {
590        self.state_colors = Some(colors);
591        self
592    }
593    /// Set state-driven elevation values for hover, press, disabled states.
594    pub fn state_elevation(mut self, elev: StateElevation) -> Self {
595        self.state_elevation = Some(elev);
596        self
597    }
598    /// Mark this component as disabled - it won't respond to pointer events.
599    pub fn disabled(mut self) -> Self {
600        self.disabled = true;
601        self
602    }
603    /// Mark this component as enabled or disabled.
604    pub fn enabled(mut self, enabled: bool) -> Self {
605        self.disabled = !enabled;
606        self
607    }
608    pub fn on_scroll(mut self, f: impl Fn(Vec2) -> Vec2 + 'static) -> Self {
609        self.on_scroll = Some(Rc::new(f));
610        self
611    }
612    pub fn on_pointer_down(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
613        self.on_pointer_down = Some(Rc::new(f));
614        self
615    }
616    pub fn on_pointer_move(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
617        self.on_pointer_move = Some(Rc::new(f));
618        self
619    }
620    pub fn on_pointer_up(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
621        self.on_pointer_up = Some(Rc::new(f));
622        self
623    }
624    pub fn on_pointer_enter(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
625        self.on_pointer_enter = Some(Rc::new(f));
626        self
627    }
628    pub fn on_pointer_leave(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
629        self.on_pointer_leave = Some(Rc::new(f));
630        self
631    }
632    pub fn semantics(mut self, s: crate::Semantics) -> Self {
633        self.semantics = Some(s);
634        self
635    }
636    pub fn alpha(mut self, a: f32) -> Self {
637        self.alpha = Some(a);
638        self
639    }
640    /// Render this subtree into an offscreen texture, then composite it
641    /// back into the parent with the given group `alpha` (0.0..=1.0).
642    /// Allows correct blending when children overlap inside the layer, and
643    /// sets up the architecture for future layer effects (shadow, blur, clip).
644    pub fn graphics_layer(mut self, alpha: f32) -> Self {
645        self.graphics_layer = Some(alpha.clamp(0.0, 1.0));
646        self
647    }
648    /// Drop shadow with the given `blur_radius` (dp) and vertical `offset_y` (dp).
649    /// The shadow color defaults to black with alpha 64 (~25%). Combines with
650    /// [`Modifier::graphics_layer`] to draw a shadow underneath the layer.
651    pub fn shadow(mut self, blur_radius: f32, offset_y: f32) -> Self {
652        self.shadow = Some(ShadowSpec {
653            blur_radius: blur_radius.max(0.0),
654            offset_y,
655            color: Color(0, 0, 0, 64),
656        });
657        self
658    }
659    /// Drop shadow with a custom color. Alpha 0..=255.
660    pub fn shadow_with_color(mut self, blur_radius: f32, offset_y: f32, color: Color) -> Self {
661        self.shadow = Some(ShadowSpec {
662            blur_radius: blur_radius.max(0.0),
663            offset_y,
664            color,
665        });
666        self
667    }
668    /// Material-style elevation. Auto-scales blur and offset by `level` (dp)
669    /// and uses a default shadow color. Level 0 = no shadow; 4 = subtle;
670    /// 16 = strong. Requires [`Modifier::graphics_layer`] to take effect.
671    pub fn elevation(mut self, level: f32) -> Self {
672        if level <= 0.0 {
673            self.shadow = None;
674            return self;
675        }
676        self.shadow = Some(ShadowSpec {
677            blur_radius: level * 2.0,
678            offset_y: level * 0.5,
679            color: Color(0, 0, 0, (level * 8.0).clamp(8.0, 80.0) as u8),
680        });
681        self
682    }
683    pub fn transform(mut self, t: Transform) -> Self {
684        self.transform = Some(t);
685        self
686    }
687    pub fn grid(mut self, columns: usize, row_gap: f32, column_gap: f32) -> Self {
688        self.grid = Some(GridConfig {
689            columns,
690            row_gap,
691            column_gap,
692        });
693        self
694    }
695    pub fn grid_span(mut self, col_span: u16, row_span: u16) -> Self {
696        self.grid_col_span = Some(col_span);
697        self.grid_row_span = Some(row_span);
698        self
699    }
700    pub fn absolute(mut self) -> Self {
701        self.position_type = Some(PositionType::Absolute);
702        self
703    }
704    pub fn offset(
705        mut self,
706        left: Option<f32>,
707        top: Option<f32>,
708        right: Option<f32>,
709        bottom: Option<f32>,
710    ) -> Self {
711        self.offset_left = left;
712        self.offset_top = top;
713        self.offset_right = right;
714        self.offset_bottom = bottom;
715        self
716    }
717    pub fn offset_left(mut self, v: f32) -> Self {
718        self.offset_left = Some(v);
719        self
720    }
721    pub fn offset_right(mut self, v: f32) -> Self {
722        self.offset_right = Some(v);
723        self
724    }
725    pub fn offset_top(mut self, v: f32) -> Self {
726        self.offset_top = Some(v);
727        self
728    }
729    pub fn offset_bottom(mut self, v: f32) -> Self {
730        self.offset_bottom = Some(v);
731        self
732    }
733
734    pub fn margin(mut self, v: f32) -> Self {
735        self.margin_left = Some(v);
736        self.margin_right = Some(v);
737        self.margin_top = Some(v);
738        self.margin_bottom = Some(v);
739        self
740    }
741
742    pub fn margin_horizontal(mut self, v: f32) -> Self {
743        self.margin_left = Some(v);
744        self.margin_right = Some(v);
745        self
746    }
747
748    pub fn margin_vertical(mut self, v: f32) -> Self {
749        self.margin_top = Some(v);
750        self.margin_bottom = Some(v);
751        self
752    }
753    pub fn aspect_ratio(mut self, ratio: f32) -> Self {
754        self.aspect_ratio = Some(ratio);
755        self
756    }
757    pub fn painter(mut self, f: impl Fn(&mut crate::Scene, crate::Rect, f32) + 'static) -> Self {
758        self.painter = Some(Rc::new(f));
759        self
760    }
761    pub fn scale(self, s: f32) -> Self {
762        self.scale2(s, s)
763    }
764    pub fn scale2(mut self, sx: f32, sy: f32) -> Self {
765        let mut t = self.transform.unwrap_or_else(Transform::identity);
766        t.scale_x *= sx;
767        t.scale_y *= sy;
768        self.transform = Some(t);
769        self
770    }
771    pub fn translate(mut self, x: f32, y: f32) -> Self {
772        let t = self.transform.unwrap_or_else(Transform::identity);
773        self.transform = Some(t.combine(&Transform::translate(x, y)));
774        self
775    }
776    pub fn translate_vec2(self, v: Vec2) -> Self {
777        self.translate(v.x, v.y)
778    }
779    pub fn rotate(mut self, radians: f32) -> Self {
780        let mut t = self.transform.unwrap_or_else(Transform::identity);
781        t.rotate += radians;
782        self.transform = Some(t);
783        self
784    }
785    pub fn weight(mut self, w: f32) -> Self {
786        let w = w.max(0.0);
787        self.flex_grow = Some(w);
788        self.flex_shrink = Some(1.0);
789        // dp units; 0 is fine.
790        self.flex_basis = Some(0.0);
791        self
792    }
793    /// Marks this view as a repaint boundary candidate.
794    ///
795    /// The engine may cache its painted output.
796    pub fn repaint_boundary(mut self) -> Self {
797        self.repaint_boundary = true;
798        self
799    }
800    pub fn on_action(mut self, f: impl Fn(crate::shortcuts::Action) -> bool + 'static) -> Self {
801        self.on_action = Some(Rc::new(f));
802        self
803    }
804
805    /// Mark this node as a drag source. Return `Some(payload)` to start dragging.
806    pub fn on_drag_start(
807        mut self,
808        f: impl Fn(crate::dnd::DragStart) -> Option<crate::dnd::DragPayload> + 'static,
809    ) -> Self {
810        self.on_drag_start = Some(Rc::new(f));
811        self
812    }
813
814    /// Called when a drag ends (drop accepted or canceled/ignored).
815    pub fn on_drag_end(mut self, f: impl Fn(crate::dnd::DragEnd) + 'static) -> Self {
816        self.on_drag_end = Some(Rc::new(f));
817        self
818    }
819
820    /// Called when a drag first enters this target.
821    pub fn on_drag_enter(mut self, f: impl Fn(crate::dnd::DragOver) + 'static) -> Self {
822        self.on_drag_enter = Some(Rc::new(f));
823        self
824    }
825
826    /// Called on every pointer move while a drag is over this target.
827    pub fn on_drag_over(mut self, f: impl Fn(crate::dnd::DragOver) + 'static) -> Self {
828        self.on_drag_over = Some(Rc::new(f));
829        self
830    }
831
832    /// Called when a drag leaves this target.
833    pub fn on_drag_leave(mut self, f: impl Fn(crate::dnd::DragOver) + 'static) -> Self {
834        self.on_drag_leave = Some(Rc::new(f));
835        self
836    }
837
838    /// Called on pointer release while a drag is over this target.
839    /// Return `true` to accept the drop.
840    pub fn on_drop(mut self, f: impl Fn(crate::dnd::DropEvent) -> bool + 'static) -> Self {
841        self.on_drop = Some(Rc::new(f));
842        self
843    }
844
845    /// Set the cursor icon hint for desktop/web runners.
846    pub fn cursor(mut self, c: crate::CursorIcon) -> Self {
847        self.cursor = Some(c);
848        self
849    }
850
851    /// Animate size changes smoothly when the content's natural size changes.
852    /// Uses the provided `AnimationSpec` for the transition.
853    /// The content will be clipped to the animated size during transitions.
854    pub fn animate_content_size(mut self, spec: AnimationSpec) -> Self {
855        self.animate_content_size = Some(spec);
856        self
857    }
858
859    /// Attach a `FocusRequester` to this view. The requester will be associated
860    /// with the view's focusable element, allowing programmatic focus requests.
861    pub fn focus_requester(mut self, fr: crate::runtime::FocusRequester) -> Self {
862        self.focus_requester = Some(fr);
863        self
864    }
865
866    /// Register a callback that fires when this view gains or loses keyboard focus.
867    /// The argument is `true` when the view receives focus, `false` when it loses it.
868    pub fn on_focus_changed(mut self, f: impl Fn(bool) + 'static) -> Self {
869        self.on_focus_changed = Some(Rc::new(f));
870        self
871    }
872
873    /// Mark this Box as a text input field with the given configuration.
874    pub fn text_input(mut self, config: TextInputConfig) -> Self {
875        self.text_input = Some(config);
876        self
877    }
878
879}