repose_core/
modifier.rs

1use std::rc::Rc;
2
3use taffy::{AlignContent, AlignItems, AlignSelf, 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    pub size: Option<Size>,
38    pub width: Option<f32>,
39    pub height: Option<f32>,
40    pub fill_max: bool,
41    pub fill_max_w: bool,
42    pub fill_max_h: bool,
43    pub padding: Option<f32>,
44    pub padding_values: Option<PaddingValues>,
45    pub min_width: Option<f32>,
46    pub min_height: Option<f32>,
47    pub max_width: Option<f32>,
48    pub max_height: Option<f32>,
49    pub background: Option<Brush>,
50    pub border: Option<Border>,
51    pub flex_grow: Option<f32>,
52    pub flex_shrink: Option<f32>,
53    pub flex_basis: Option<f32>,
54    pub align_self: Option<AlignSelf>,
55    pub justify_content: Option<JustifyContent>,
56    pub align_items_container: Option<AlignItems>,
57    pub align_content: Option<AlignContent>,
58    pub clip_rounded: Option<f32>,
59    /// Works for hit-testing only, draw order is not changed.
60    pub z_index: f32,
61    pub click: bool,
62    pub on_scroll: Option<Rc<dyn Fn(Vec2) -> Vec2>>,
63    pub on_pointer_down: Option<Rc<dyn Fn(PointerEvent)>>,
64    pub on_pointer_move: Option<Rc<dyn Fn(PointerEvent)>>,
65    pub on_pointer_up: Option<Rc<dyn Fn(PointerEvent)>>,
66    pub on_pointer_enter: Option<Rc<dyn Fn(PointerEvent)>>,
67    pub on_pointer_leave: Option<Rc<dyn Fn(PointerEvent)>>,
68    pub semantics: Option<crate::Semantics>,
69    pub alpha: Option<f32>,
70    pub transform: Option<Transform>,
71    pub grid: Option<GridConfig>,
72    pub grid_col_span: Option<u16>,
73    pub grid_row_span: Option<u16>,
74    pub position_type: Option<PositionType>,
75    pub offset_left: Option<f32>,
76    pub offset_right: Option<f32>,
77    pub offset_top: Option<f32>,
78    pub offset_bottom: Option<f32>,
79    pub aspect_ratio: Option<f32>,
80    pub painter: Option<Rc<dyn Fn(&mut crate::Scene, crate::Rect)>>,
81}
82
83impl std::fmt::Debug for Modifier {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        f.debug_struct("Modifier")
86            .field("size", &self.size)
87            .field("width", &self.width)
88            .field("height", &self.height)
89            .field("fill_max", &self.fill_max)
90            .field("fill_max_w", &self.fill_max_w)
91            .field("fill_max_h", &self.fill_max_h)
92            .field("padding", &self.padding)
93            .field("padding_values", &self.padding_values)
94            .field("min_width", &self.min_width)
95            .field("min_height", &self.min_height)
96            .field("max_width", &self.max_width)
97            .field("max_height", &self.max_height)
98            .field("background", &self.background)
99            .field("border", &self.border)
100            .field("flex_grow", &self.flex_grow)
101            .field("flex_shrink", &self.flex_shrink)
102            .field("flex_basis", &self.flex_basis)
103            .field("align_self", &self.align_self)
104            .field("justify_content", &self.justify_content)
105            .field("align_items_container", &self.align_items_container)
106            .field("align_content", &self.align_content)
107            .field("clip_rounded", &self.clip_rounded)
108            .field("z_index", &self.z_index)
109            .field("click", &self.click)
110            .field("on_scroll", &self.on_scroll.as_ref().map(|_| "..."))
111            .field(
112                "on_pointer_down",
113                &self.on_pointer_down.as_ref().map(|_| "..."),
114            )
115            .field(
116                "on_pointer_move",
117                &self.on_pointer_move.as_ref().map(|_| "..."),
118            )
119            .field("on_pointer_up", &self.on_pointer_up.as_ref().map(|_| "..."))
120            .field(
121                "on_pointer_enter",
122                &self.on_pointer_enter.as_ref().map(|_| "..."),
123            )
124            .field(
125                "on_pointer_leave",
126                &self.on_pointer_leave.as_ref().map(|_| "..."),
127            )
128            .field("semantics", &self.semantics)
129            .field("alpha", &self.alpha)
130            .field("transform", &self.transform)
131            .field("grid", &self.grid)
132            .field("grid_col_span", &self.grid_col_span)
133            .field("grid_row_span", &self.grid_row_span)
134            .field("position_type", &self.position_type)
135            .field("offset_left", &self.offset_left)
136            .field("offset_right", &self.offset_right)
137            .field("offset_top", &self.offset_top)
138            .field("offset_bottom", &self.offset_bottom)
139            .field("aspect_ratio", &self.aspect_ratio)
140            .field("painter", &self.painter.as_ref().map(|_| "..."))
141            .finish()
142    }
143}
144
145impl Modifier {
146    pub fn new() -> Self {
147        Self::default()
148    }
149    pub fn size(mut self, w: f32, h: f32) -> Self {
150        self.size = Some(Size {
151            width: w,
152            height: h,
153        });
154        self
155    }
156    pub fn width(mut self, w: f32) -> Self {
157        self.width = Some(w);
158        self
159    }
160    pub fn height(mut self, h: f32) -> Self {
161        self.height = Some(h);
162        self
163    }
164    pub fn fill_max_size(mut self) -> Self {
165        self.fill_max = true;
166        self
167    }
168    pub fn fill_max_width(mut self) -> Self {
169        self.fill_max_w = true;
170        self
171    }
172    pub fn fill_max_height(mut self) -> Self {
173        self.fill_max_h = true;
174        self
175    }
176    pub fn padding(mut self, v: f32) -> Self {
177        self.padding = Some(v);
178        self
179    }
180    pub fn padding_values(mut self, padding: PaddingValues) -> Self {
181        self.padding_values = Some(padding);
182        self
183    }
184    pub fn min_size(mut self, w: f32, h: f32) -> Self {
185        self.min_width = Some(w);
186        self.min_height = Some(h);
187        self
188    }
189    pub fn max_size(mut self, w: f32, h: f32) -> Self {
190        self.max_width = Some(w);
191        self.max_height = Some(h);
192        self
193    }
194    pub fn min_width(mut self, w: f32) -> Self {
195        self.min_width = Some(w);
196        self
197    }
198    pub fn min_height(mut self, h: f32) -> Self {
199        self.min_height = Some(h);
200        self
201    }
202    pub fn max_width(mut self, w: f32) -> Self {
203        self.max_width = Some(w);
204        self
205    }
206    pub fn max_height(mut self, h: f32) -> Self {
207        self.max_height = Some(h);
208        self
209    }
210    /// Set a solid color background.
211    pub fn background(mut self, color: Color) -> Self {
212        self.background = Some(Brush::Solid(color));
213        self
214    }
215    /// Set a brush (solid, gradient, etc.) background.
216    pub fn background_brush(mut self, brush: Brush) -> Self {
217        self.background = Some(brush);
218        self
219    }
220    pub fn border(mut self, width: f32, color: Color, radius: f32) -> Self {
221        self.border = Some(Border {
222            width,
223            color,
224            radius,
225        });
226        self
227    }
228    pub fn flex_grow(mut self, v: f32) -> Self {
229        self.flex_grow = Some(v);
230        self
231    }
232    pub fn flex_shrink(mut self, v: f32) -> Self {
233        self.flex_shrink = Some(v);
234        self
235    }
236    pub fn flex_basis(mut self, v: f32) -> Self {
237        self.flex_basis = Some(v);
238        self
239    }
240    pub fn align_self(mut self, a: AlignSelf) -> Self {
241        self.align_self = Some(a);
242        self
243    }
244    pub fn align_self_center(mut self) -> Self {
245        self.align_self = Some(AlignSelf::Center);
246        self
247    }
248    pub fn justify_content(mut self, j: JustifyContent) -> Self {
249        self.justify_content = Some(j);
250        self
251    }
252    pub fn align_items(mut self, a: AlignItems) -> Self {
253        self.align_items_container = Some(a);
254        self
255    }
256    pub fn align_content(mut self, a: AlignContent) -> Self {
257        self.align_content = Some(a);
258        self
259    }
260    pub fn clip_rounded(mut self, radius: f32) -> Self {
261        self.clip_rounded = Some(radius);
262        self
263    }
264    pub fn z_index(mut self, z: f32) -> Self {
265        self.z_index = z;
266        self
267    }
268    pub fn clickable(mut self) -> Self {
269        self.click = true;
270        self
271    }
272    pub fn on_scroll(mut self, f: impl Fn(Vec2) -> Vec2 + 'static) -> Self {
273        self.on_scroll = Some(Rc::new(f));
274        self
275    }
276    pub fn on_pointer_down(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
277        self.on_pointer_down = Some(Rc::new(f));
278        self
279    }
280    pub fn on_pointer_move(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
281        self.on_pointer_move = Some(Rc::new(f));
282        self
283    }
284    pub fn on_pointer_up(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
285        self.on_pointer_up = Some(Rc::new(f));
286        self
287    }
288    pub fn on_pointer_enter(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
289        self.on_pointer_enter = Some(Rc::new(f));
290        self
291    }
292    pub fn on_pointer_leave(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
293        self.on_pointer_leave = Some(Rc::new(f));
294        self
295    }
296    pub fn semantics(mut self, s: crate::Semantics) -> Self {
297        self.semantics = Some(s);
298        self
299    }
300    pub fn alpha(mut self, a: f32) -> Self {
301        self.alpha = Some(a);
302        self
303    }
304    pub fn transform(mut self, t: Transform) -> Self {
305        self.transform = Some(t);
306        self
307    }
308    pub fn grid(mut self, columns: usize, row_gap: f32, column_gap: f32) -> Self {
309        self.grid = Some(GridConfig {
310            columns,
311            row_gap,
312            column_gap,
313        });
314        self
315    }
316    pub fn grid_span(mut self, col_span: u16, row_span: u16) -> Self {
317        self.grid_col_span = Some(col_span);
318        self.grid_row_span = Some(row_span);
319        self
320    }
321    pub fn absolute(mut self) -> Self {
322        self.position_type = Some(PositionType::Absolute);
323        self
324    }
325    pub fn offset(
326        mut self,
327        left: Option<f32>,
328        top: Option<f32>,
329        right: Option<f32>,
330        bottom: Option<f32>,
331    ) -> Self {
332        self.offset_left = left;
333        self.offset_top = top;
334        self.offset_right = right;
335        self.offset_bottom = bottom;
336        self
337    }
338    pub fn offset_left(mut self, v: f32) -> Self {
339        self.offset_left = Some(v);
340        self
341    }
342    pub fn offset_right(mut self, v: f32) -> Self {
343        self.offset_right = Some(v);
344        self
345    }
346    pub fn offset_top(mut self, v: f32) -> Self {
347        self.offset_top = Some(v);
348        self
349    }
350    pub fn offset_bottom(mut self, v: f32) -> Self {
351        self.offset_bottom = Some(v);
352        self
353    }
354    pub fn aspect_ratio(mut self, ratio: f32) -> Self {
355        self.aspect_ratio = Some(ratio);
356        self
357    }
358    pub fn painter(mut self, f: impl Fn(&mut crate::Scene, crate::Rect) + 'static) -> Self {
359        self.painter = Some(Rc::new(f));
360        self
361    }
362    pub fn scale(mut self, s: f32) -> Self {
363        self.scale2(s, s)
364    }
365    pub fn scale2(mut self, sx: f32, sy: f32) -> Self {
366        let mut t = self.transform.unwrap_or_else(Transform::identity);
367        t.scale_x *= sx;
368        t.scale_y *= sy;
369        self.transform = Some(t);
370        self
371    }
372    pub fn translate(mut self, x: f32, y: f32) -> Self {
373        let t = self.transform.unwrap_or_else(Transform::identity);
374        self.transform = Some(t.combine(&Transform::translate(x, y)));
375        self
376    }
377    pub fn rotate(mut self, radians: f32) -> Self {
378        let mut t = self.transform.unwrap_or_else(Transform::identity);
379        t.rotate += radians;
380        self.transform = Some(t);
381        self
382    }
383}