repose_core/
modifier.rs

1use std::rc::Rc;
2
3use taffy::{AlignContent, AlignItems, AlignSelf, FlexDirection, FlexWrap, JustifyContent};
4
5use crate::{Brush, Color, PointerEvent, Size, Transform, Vec2};
6
7#[derive(Clone, Debug)]
8pub struct Border {
9    pub width: f32,
10    pub color: Color,
11    pub radius: f32,
12}
13
14#[derive(Clone, Copy, Debug, Default)]
15pub struct PaddingValues {
16    pub left: f32,
17    pub right: f32,
18    pub top: f32,
19    pub bottom: f32,
20}
21
22#[derive(Clone, Debug)]
23pub struct GridConfig {
24    pub columns: usize,
25    pub row_gap: f32,
26    pub column_gap: f32,
27}
28
29#[derive(Clone, Copy, Debug)]
30pub enum PositionType {
31    Relative,
32    Absolute,
33}
34
35#[derive(Clone, Default)]
36pub struct Modifier {
37    /// Optional stable identity key for this view node.
38    ///
39    /// If set, `layout_and_paint` will prefer this over child index when assigning stable ViewIds.
40    /// This is the “escape hatch” for dynamic lists / conditional UI where index-based identity
41    /// would otherwise shift.
42    pub key: Option<u64>,
43
44    pub size: Option<Size>,
45    pub width: Option<f32>,
46    pub height: Option<f32>,
47    pub fill_max: bool,
48    pub fill_max_w: bool,
49    pub fill_max_h: bool,
50    pub padding: Option<f32>,
51    pub padding_values: Option<PaddingValues>,
52    pub min_width: Option<f32>,
53    pub min_height: Option<f32>,
54    pub max_width: Option<f32>,
55    pub max_height: Option<f32>,
56    pub background: Option<Brush>,
57    pub border: Option<Border>,
58    pub flex_grow: Option<f32>,
59    pub flex_shrink: Option<f32>,
60    pub flex_basis: Option<f32>,
61    pub flex_wrap: Option<FlexWrap>,
62    pub flex_dir: Option<FlexDirection>,
63    pub align_self: Option<AlignSelf>,
64    pub justify_content: Option<JustifyContent>,
65    pub align_items_container: Option<AlignItems>,
66    pub align_content: Option<AlignContent>,
67    pub clip_rounded: Option<f32>,
68    /// Works for hit-testing only, draw order is not changed.
69    pub z_index: f32,
70    pub repaint_boundary: bool,
71    pub click: bool,
72    pub on_scroll: Option<Rc<dyn Fn(Vec2) -> Vec2>>,
73    pub on_pointer_down: Option<Rc<dyn Fn(PointerEvent)>>,
74    pub on_pointer_move: Option<Rc<dyn Fn(PointerEvent)>>,
75    pub on_pointer_up: Option<Rc<dyn Fn(PointerEvent)>>,
76    pub on_pointer_enter: Option<Rc<dyn Fn(PointerEvent)>>,
77    pub on_pointer_leave: Option<Rc<dyn Fn(PointerEvent)>>,
78    pub semantics: Option<crate::Semantics>,
79    pub alpha: Option<f32>,
80    pub transform: Option<Transform>,
81    pub grid: Option<GridConfig>,
82    pub grid_col_span: Option<u16>,
83    pub grid_row_span: Option<u16>,
84    pub position_type: Option<PositionType>,
85    pub offset_left: Option<f32>,
86    pub offset_right: Option<f32>,
87    pub offset_top: Option<f32>,
88    pub offset_bottom: Option<f32>,
89    pub margin_left: Option<f32>,
90    pub margin_right: Option<f32>,
91    pub margin_top: Option<f32>,
92    pub margin_bottom: Option<f32>,
93    pub aspect_ratio: Option<f32>,
94    pub painter: Option<Rc<dyn Fn(&mut crate::Scene, crate::Rect)>>,
95
96    // Drag-drop (internal)
97    pub on_drag_start: Option<Rc<dyn Fn(crate::dnd::DragStart) -> Option<crate::dnd::DragPayload>>>,
98    pub on_drag_end: Option<Rc<dyn Fn(crate::dnd::DragEnd)>>,
99    pub on_drag_enter: Option<Rc<dyn Fn(crate::dnd::DragOver)>>,
100    pub on_drag_over: Option<Rc<dyn Fn(crate::dnd::DragOver)>>,
101    pub on_drag_leave: Option<Rc<dyn Fn(crate::dnd::DragOver)>>,
102    pub on_drop: Option<Rc<dyn Fn(crate::dnd::DropEvent) -> bool>>,
103
104    pub on_action: Option<Rc<dyn Fn(crate::shortcuts::Action) -> bool>>,
105
106    /// Cursor icon hint for desktop/web runners.
107    pub cursor: Option<crate::CursorIcon>,
108}
109
110impl std::fmt::Debug for Modifier {
111    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112        f.debug_struct("Modifier")
113            .field("key", &self.key)
114            .field("size", &self.size)
115            .field("width", &self.width)
116            .field("height", &self.height)
117            .field("fill_max", &self.fill_max)
118            .field("fill_max_w", &self.fill_max_w)
119            .field("fill_max_h", &self.fill_max_h)
120            .field("padding", &self.padding)
121            .field("padding_values", &self.padding_values)
122            .field("min_width", &self.min_width)
123            .field("min_height", &self.min_height)
124            .field("max_width", &self.max_width)
125            .field("max_height", &self.max_height)
126            .field("background", &self.background)
127            .field("border", &self.border)
128            .field("flex_grow", &self.flex_grow)
129            .field("flex_shrink", &self.flex_shrink)
130            .field("flex_basis", &self.flex_basis)
131            .field("align_self", &self.align_self)
132            .field("justify_content", &self.justify_content)
133            .field("align_items_container", &self.align_items_container)
134            .field("align_content", &self.align_content)
135            .field("clip_rounded", &self.clip_rounded)
136            .field("z_index", &self.z_index)
137            .field("repaint_boundary", &self.repaint_boundary)
138            .field("click", &self.click)
139            .field("on_scroll", &self.on_scroll.as_ref().map(|_| "..."))
140            .field(
141                "on_pointer_down",
142                &self.on_pointer_down.as_ref().map(|_| "..."),
143            )
144            .field(
145                "on_pointer_move",
146                &self.on_pointer_move.as_ref().map(|_| "..."),
147            )
148            .field("on_pointer_up", &self.on_pointer_up.as_ref().map(|_| "..."))
149            .field(
150                "on_pointer_enter",
151                &self.on_pointer_enter.as_ref().map(|_| "..."),
152            )
153            .field(
154                "on_pointer_leave",
155                &self.on_pointer_leave.as_ref().map(|_| "..."),
156            )
157            .field("semantics", &self.semantics)
158            .field("alpha", &self.alpha)
159            .field("transform", &self.transform)
160            .field("grid", &self.grid)
161            .field("grid_col_span", &self.grid_col_span)
162            .field("grid_row_span", &self.grid_row_span)
163            .field("position_type", &self.position_type)
164            .field("offset_left", &self.offset_left)
165            .field("offset_right", &self.offset_right)
166            .field("offset_top", &self.offset_top)
167            .field("offset_bottom", &self.offset_bottom)
168            .field("aspect_ratio", &self.aspect_ratio)
169            .field("painter", &self.painter.as_ref().map(|_| "..."))
170            .field("on_drag_start", &self.on_drag_start.as_ref().map(|_| "..."))
171            .field("on_drag_end", &self.on_drag_end.as_ref().map(|_| "..."))
172            .field("on_drag_enter", &self.on_drag_enter.as_ref().map(|_| "..."))
173            .field("on_drag_over", &self.on_drag_over.as_ref().map(|_| "..."))
174            .field("on_drag_leave", &self.on_drag_leave.as_ref().map(|_| "..."))
175            .field("on_drop", &self.on_drop.as_ref().map(|_| "..."))
176            .field("on_action", &self.on_action.as_ref().map(|_| "..."))
177            .finish()
178    }
179}
180
181impl Modifier {
182    pub fn new() -> Self {
183        Self::default()
184    }
185
186    /// Attaches a stable identity key to this view node.
187    /// Use for dynamic lists / conditional UI where index-based identity can shift.
188    pub fn key(mut self, key: u64) -> Self {
189        self.key = Some(key);
190        self
191    }
192
193    pub fn size(mut self, w: f32, h: f32) -> Self {
194        self.size = Some(Size {
195            width: w,
196            height: h,
197        });
198        self
199    }
200    pub fn width(mut self, w: f32) -> Self {
201        self.width = Some(w);
202        self
203    }
204    pub fn height(mut self, h: f32) -> Self {
205        self.height = Some(h);
206        self
207    }
208    pub fn fill_max_size(mut self) -> Self {
209        self.fill_max = true;
210        self
211    }
212    pub fn fill_max_width(mut self) -> Self {
213        self.fill_max_w = true;
214        self
215    }
216    pub fn fill_max_height(mut self) -> Self {
217        self.fill_max_h = true;
218        self
219    }
220    pub fn padding(mut self, v: f32) -> Self {
221        self.padding = Some(v);
222        self
223    }
224    pub fn padding_values(mut self, padding: PaddingValues) -> Self {
225        self.padding_values = Some(padding);
226        self
227    }
228    pub fn min_size(mut self, w: f32, h: f32) -> Self {
229        self.min_width = Some(w);
230        self.min_height = Some(h);
231        self
232    }
233    pub fn max_size(mut self, w: f32, h: f32) -> Self {
234        self.max_width = Some(w);
235        self.max_height = Some(h);
236        self
237    }
238    pub fn min_width(mut self, w: f32) -> Self {
239        self.min_width = Some(w);
240        self
241    }
242    pub fn min_height(mut self, h: f32) -> Self {
243        self.min_height = Some(h);
244        self
245    }
246    pub fn max_width(mut self, w: f32) -> Self {
247        self.max_width = Some(w);
248        self
249    }
250    pub fn max_height(mut self, h: f32) -> Self {
251        self.max_height = Some(h);
252        self
253    }
254    /// Set a solid color background.
255    pub fn background(mut self, color: Color) -> Self {
256        self.background = Some(Brush::Solid(color));
257        self
258    }
259    /// Set a brush (solid, gradient, etc.) background.
260    pub fn background_brush(mut self, brush: Brush) -> Self {
261        self.background = Some(brush);
262        self
263    }
264    pub fn border(mut self, width: f32, color: Color, radius: f32) -> Self {
265        self.border = Some(Border {
266            width,
267            color,
268            radius,
269        });
270        self
271    }
272    pub fn flex_grow(mut self, v: f32) -> Self {
273        self.flex_grow = Some(v);
274        self
275    }
276    pub fn flex_shrink(mut self, v: f32) -> Self {
277        self.flex_shrink = Some(v);
278        self
279    }
280    pub fn flex_basis(mut self, v: f32) -> Self {
281        self.flex_basis = Some(v);
282        self
283    }
284    pub fn flex_wrap(mut self, w: FlexWrap) -> Self {
285        self.flex_wrap = Some(w);
286        self
287    }
288    pub fn flex_dir(mut self, d: FlexDirection) -> Self {
289        self.flex_dir = Some(d);
290        self
291    }
292    pub fn align_self(mut self, a: AlignSelf) -> Self {
293        self.align_self = Some(a);
294        self
295    }
296    pub fn align_self_center(mut self) -> Self {
297        self.align_self = Some(AlignSelf::Center);
298        self
299    }
300    pub fn justify_content(mut self, j: JustifyContent) -> Self {
301        self.justify_content = Some(j);
302        self
303    }
304    pub fn align_items(mut self, a: AlignItems) -> Self {
305        self.align_items_container = Some(a);
306        self
307    }
308    pub fn align_content(mut self, a: AlignContent) -> Self {
309        self.align_content = Some(a);
310        self
311    }
312    pub fn clip_rounded(mut self, radius: f32) -> Self {
313        self.clip_rounded = Some(radius);
314        self
315    }
316    pub fn z_index(mut self, z: f32) -> Self {
317        self.z_index = z;
318        self
319    }
320    pub fn clickable(mut self) -> Self {
321        self.click = true;
322        self
323    }
324    pub fn on_scroll(mut self, f: impl Fn(Vec2) -> Vec2 + 'static) -> Self {
325        self.on_scroll = Some(Rc::new(f));
326        self
327    }
328    pub fn on_pointer_down(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
329        self.on_pointer_down = Some(Rc::new(f));
330        self
331    }
332    pub fn on_pointer_move(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
333        self.on_pointer_move = Some(Rc::new(f));
334        self
335    }
336    pub fn on_pointer_up(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
337        self.on_pointer_up = Some(Rc::new(f));
338        self
339    }
340    pub fn on_pointer_enter(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
341        self.on_pointer_enter = Some(Rc::new(f));
342        self
343    }
344    pub fn on_pointer_leave(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
345        self.on_pointer_leave = Some(Rc::new(f));
346        self
347    }
348    pub fn semantics(mut self, s: crate::Semantics) -> Self {
349        self.semantics = Some(s);
350        self
351    }
352    pub fn alpha(mut self, a: f32) -> Self {
353        self.alpha = Some(a);
354        self
355    }
356    pub fn transform(mut self, t: Transform) -> Self {
357        self.transform = Some(t);
358        self
359    }
360    pub fn grid(mut self, columns: usize, row_gap: f32, column_gap: f32) -> Self {
361        self.grid = Some(GridConfig {
362            columns,
363            row_gap,
364            column_gap,
365        });
366        self
367    }
368    pub fn grid_span(mut self, col_span: u16, row_span: u16) -> Self {
369        self.grid_col_span = Some(col_span);
370        self.grid_row_span = Some(row_span);
371        self
372    }
373    pub fn absolute(mut self) -> Self {
374        self.position_type = Some(PositionType::Absolute);
375        self
376    }
377    pub fn offset(
378        mut self,
379        left: Option<f32>,
380        top: Option<f32>,
381        right: Option<f32>,
382        bottom: Option<f32>,
383    ) -> Self {
384        self.offset_left = left;
385        self.offset_top = top;
386        self.offset_right = right;
387        self.offset_bottom = bottom;
388        self
389    }
390    pub fn offset_left(mut self, v: f32) -> Self {
391        self.offset_left = Some(v);
392        self
393    }
394    pub fn offset_right(mut self, v: f32) -> Self {
395        self.offset_right = Some(v);
396        self
397    }
398    pub fn offset_top(mut self, v: f32) -> Self {
399        self.offset_top = Some(v);
400        self
401    }
402    pub fn offset_bottom(mut self, v: f32) -> Self {
403        self.offset_bottom = Some(v);
404        self
405    }
406
407    pub fn margin(mut self, v: f32) -> Self {
408        self.margin_left = Some(v);
409        self.margin_right = Some(v);
410        self.margin_top = Some(v);
411        self.margin_bottom = Some(v);
412        self
413    }
414
415    pub fn margin_horizontal(mut self, v: f32) -> Self {
416        self.margin_left = Some(v);
417        self.margin_right = Some(v);
418        self
419    }
420
421    pub fn margin_vertical(mut self, v: f32) -> Self {
422        self.margin_top = Some(v);
423        self.margin_bottom = Some(v);
424        self
425    }
426    pub fn aspect_ratio(mut self, ratio: f32) -> Self {
427        self.aspect_ratio = Some(ratio);
428        self
429    }
430    pub fn painter(mut self, f: impl Fn(&mut crate::Scene, crate::Rect) + 'static) -> Self {
431        self.painter = Some(Rc::new(f));
432        self
433    }
434    pub fn scale(self, s: f32) -> Self {
435        self.scale2(s, s)
436    }
437    pub fn scale2(mut self, sx: f32, sy: f32) -> Self {
438        let mut t = self.transform.unwrap_or_else(Transform::identity);
439        t.scale_x *= sx;
440        t.scale_y *= sy;
441        self.transform = Some(t);
442        self
443    }
444    pub fn translate(mut self, x: f32, y: f32) -> Self {
445        let t = self.transform.unwrap_or_else(Transform::identity);
446        self.transform = Some(t.combine(&Transform::translate(x, y)));
447        self
448    }
449    pub fn rotate(mut self, radians: f32) -> Self {
450        let mut t = self.transform.unwrap_or_else(Transform::identity);
451        t.rotate += radians;
452        self.transform = Some(t);
453        self
454    }
455    pub fn weight(mut self, w: f32) -> Self {
456        let w = w.max(0.0);
457        self.flex_grow = Some(w);
458        self.flex_shrink = Some(1.0);
459        // dp units; 0 is fine.
460        self.flex_basis = Some(0.0);
461        self
462    }
463    /// Marks this view as a repaint boundary candidate.
464    ///
465    /// The engine may cache its painted output.
466    pub fn repaint_boundary(mut self) -> Self {
467        self.repaint_boundary = true;
468        self
469    }
470    pub fn on_action(mut self, f: impl Fn(crate::shortcuts::Action) -> bool + 'static) -> Self {
471        self.on_action = Some(Rc::new(f));
472        self
473    }
474
475    /// Mark this node as a drag source. Return `Some(payload)` to start dragging.
476    pub fn on_drag_start(
477        mut self,
478        f: impl Fn(crate::dnd::DragStart) -> Option<crate::dnd::DragPayload> + 'static,
479    ) -> Self {
480        self.on_drag_start = Some(Rc::new(f));
481        self
482    }
483
484    /// Called when a drag ends (drop accepted or canceled/ignored).
485    pub fn on_drag_end(mut self, f: impl Fn(crate::dnd::DragEnd) + 'static) -> Self {
486        self.on_drag_end = Some(Rc::new(f));
487        self
488    }
489
490    /// Called when a drag first enters this target.
491    pub fn on_drag_enter(mut self, f: impl Fn(crate::dnd::DragOver) + 'static) -> Self {
492        self.on_drag_enter = Some(Rc::new(f));
493        self
494    }
495
496    /// Called on every pointer move while a drag is over this target.
497    pub fn on_drag_over(mut self, f: impl Fn(crate::dnd::DragOver) + 'static) -> Self {
498        self.on_drag_over = Some(Rc::new(f));
499        self
500    }
501
502    /// Called when a drag leaves this target.
503    pub fn on_drag_leave(mut self, f: impl Fn(crate::dnd::DragOver) + 'static) -> Self {
504        self.on_drag_leave = Some(Rc::new(f));
505        self
506    }
507
508    /// Called on pointer release while a drag is over this target.
509    /// Return `true` to accept the drop.
510    pub fn on_drop(mut self, f: impl Fn(crate::dnd::DropEvent) -> bool + 'static) -> Self {
511        self.on_drop = Some(Rc::new(f));
512        self
513    }
514
515    /// Set the cursor icon hint for desktop/web runners.
516    pub fn cursor(mut self, c: crate::CursorIcon) -> Self {
517        self.cursor = Some(c);
518        self
519    }
520}