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 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 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 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
107impl std::fmt::Debug for Modifier {
108 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109 f.debug_struct("Modifier")
110 .field("key", &self.key)
111 .field("size", &self.size)
112 .field("width", &self.width)
113 .field("height", &self.height)
114 .field("fill_max", &self.fill_max)
115 .field("fill_max_w", &self.fill_max_w)
116 .field("fill_max_h", &self.fill_max_h)
117 .field("padding", &self.padding)
118 .field("padding_values", &self.padding_values)
119 .field("min_width", &self.min_width)
120 .field("min_height", &self.min_height)
121 .field("max_width", &self.max_width)
122 .field("max_height", &self.max_height)
123 .field("background", &self.background)
124 .field("border", &self.border)
125 .field("flex_grow", &self.flex_grow)
126 .field("flex_shrink", &self.flex_shrink)
127 .field("flex_basis", &self.flex_basis)
128 .field("align_self", &self.align_self)
129 .field("justify_content", &self.justify_content)
130 .field("align_items_container", &self.align_items_container)
131 .field("align_content", &self.align_content)
132 .field("clip_rounded", &self.clip_rounded)
133 .field("z_index", &self.z_index)
134 .field("repaint_boundary", &self.repaint_boundary)
135 .field("click", &self.click)
136 .field("on_scroll", &self.on_scroll.as_ref().map(|_| "..."))
137 .field(
138 "on_pointer_down",
139 &self.on_pointer_down.as_ref().map(|_| "..."),
140 )
141 .field(
142 "on_pointer_move",
143 &self.on_pointer_move.as_ref().map(|_| "..."),
144 )
145 .field("on_pointer_up", &self.on_pointer_up.as_ref().map(|_| "..."))
146 .field(
147 "on_pointer_enter",
148 &self.on_pointer_enter.as_ref().map(|_| "..."),
149 )
150 .field(
151 "on_pointer_leave",
152 &self.on_pointer_leave.as_ref().map(|_| "..."),
153 )
154 .field("semantics", &self.semantics)
155 .field("alpha", &self.alpha)
156 .field("transform", &self.transform)
157 .field("grid", &self.grid)
158 .field("grid_col_span", &self.grid_col_span)
159 .field("grid_row_span", &self.grid_row_span)
160 .field("position_type", &self.position_type)
161 .field("offset_left", &self.offset_left)
162 .field("offset_right", &self.offset_right)
163 .field("offset_top", &self.offset_top)
164 .field("offset_bottom", &self.offset_bottom)
165 .field("aspect_ratio", &self.aspect_ratio)
166 .field("painter", &self.painter.as_ref().map(|_| "..."))
167 .field("on_drag_start", &self.on_drag_start.as_ref().map(|_| "..."))
168 .field("on_drag_end", &self.on_drag_end.as_ref().map(|_| "..."))
169 .field("on_drag_enter", &self.on_drag_enter.as_ref().map(|_| "..."))
170 .field("on_drag_over", &self.on_drag_over.as_ref().map(|_| "..."))
171 .field("on_drag_leave", &self.on_drag_leave.as_ref().map(|_| "..."))
172 .field("on_drop", &self.on_drop.as_ref().map(|_| "..."))
173 .field("on_action", &self.on_action.as_ref().map(|_| "..."))
174 .finish()
175 }
176}
177
178impl Modifier {
179 pub fn new() -> Self {
180 Self::default()
181 }
182
183 pub fn key(mut self, key: u64) -> Self {
186 self.key = Some(key);
187 self
188 }
189
190 pub fn size(mut self, w: f32, h: f32) -> Self {
191 self.size = Some(Size {
192 width: w,
193 height: h,
194 });
195 self
196 }
197 pub fn width(mut self, w: f32) -> Self {
198 self.width = Some(w);
199 self
200 }
201 pub fn height(mut self, h: f32) -> Self {
202 self.height = Some(h);
203 self
204 }
205 pub fn fill_max_size(mut self) -> Self {
206 self.fill_max = true;
207 self
208 }
209 pub fn fill_max_width(mut self) -> Self {
210 self.fill_max_w = true;
211 self
212 }
213 pub fn fill_max_height(mut self) -> Self {
214 self.fill_max_h = true;
215 self
216 }
217 pub fn padding(mut self, v: f32) -> Self {
218 self.padding = Some(v);
219 self
220 }
221 pub fn padding_values(mut self, padding: PaddingValues) -> Self {
222 self.padding_values = Some(padding);
223 self
224 }
225 pub fn min_size(mut self, w: f32, h: f32) -> Self {
226 self.min_width = Some(w);
227 self.min_height = Some(h);
228 self
229 }
230 pub fn max_size(mut self, w: f32, h: f32) -> Self {
231 self.max_width = Some(w);
232 self.max_height = Some(h);
233 self
234 }
235 pub fn min_width(mut self, w: f32) -> Self {
236 self.min_width = Some(w);
237 self
238 }
239 pub fn min_height(mut self, h: f32) -> Self {
240 self.min_height = Some(h);
241 self
242 }
243 pub fn max_width(mut self, w: f32) -> Self {
244 self.max_width = Some(w);
245 self
246 }
247 pub fn max_height(mut self, h: f32) -> Self {
248 self.max_height = Some(h);
249 self
250 }
251 pub fn background(mut self, color: Color) -> Self {
253 self.background = Some(Brush::Solid(color));
254 self
255 }
256 pub fn background_brush(mut self, brush: Brush) -> Self {
258 self.background = Some(brush);
259 self
260 }
261 pub fn border(mut self, width: f32, color: Color, radius: f32) -> Self {
262 self.border = Some(Border {
263 width,
264 color,
265 radius,
266 });
267 self
268 }
269 pub fn flex_grow(mut self, v: f32) -> Self {
270 self.flex_grow = Some(v);
271 self
272 }
273 pub fn flex_shrink(mut self, v: f32) -> Self {
274 self.flex_shrink = Some(v);
275 self
276 }
277 pub fn flex_basis(mut self, v: f32) -> Self {
278 self.flex_basis = Some(v);
279 self
280 }
281 pub fn flex_wrap(mut self, w: FlexWrap) -> Self {
282 self.flex_wrap = Some(w);
283 self
284 }
285 pub fn flex_dir(mut self, d: FlexDirection) -> Self {
286 self.flex_dir = Some(d);
287 self
288 }
289 pub fn align_self(mut self, a: AlignSelf) -> Self {
290 self.align_self = Some(a);
291 self
292 }
293 pub fn align_self_center(mut self) -> Self {
294 self.align_self = Some(AlignSelf::Center);
295 self
296 }
297 pub fn justify_content(mut self, j: JustifyContent) -> Self {
298 self.justify_content = Some(j);
299 self
300 }
301 pub fn align_items(mut self, a: AlignItems) -> Self {
302 self.align_items_container = Some(a);
303 self
304 }
305 pub fn align_content(mut self, a: AlignContent) -> Self {
306 self.align_content = Some(a);
307 self
308 }
309 pub fn clip_rounded(mut self, radius: f32) -> Self {
310 self.clip_rounded = Some(radius);
311 self
312 }
313 pub fn z_index(mut self, z: f32) -> Self {
314 self.z_index = z;
315 self
316 }
317 pub fn clickable(mut self) -> Self {
318 self.click = true;
319 self
320 }
321 pub fn on_scroll(mut self, f: impl Fn(Vec2) -> Vec2 + 'static) -> Self {
322 self.on_scroll = Some(Rc::new(f));
323 self
324 }
325 pub fn on_pointer_down(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
326 self.on_pointer_down = Some(Rc::new(f));
327 self
328 }
329 pub fn on_pointer_move(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
330 self.on_pointer_move = Some(Rc::new(f));
331 self
332 }
333 pub fn on_pointer_up(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
334 self.on_pointer_up = Some(Rc::new(f));
335 self
336 }
337 pub fn on_pointer_enter(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
338 self.on_pointer_enter = Some(Rc::new(f));
339 self
340 }
341 pub fn on_pointer_leave(mut self, f: impl Fn(PointerEvent) + 'static) -> Self {
342 self.on_pointer_leave = Some(Rc::new(f));
343 self
344 }
345 pub fn semantics(mut self, s: crate::Semantics) -> Self {
346 self.semantics = Some(s);
347 self
348 }
349 pub fn alpha(mut self, a: f32) -> Self {
350 self.alpha = Some(a);
351 self
352 }
353 pub fn transform(mut self, t: Transform) -> Self {
354 self.transform = Some(t);
355 self
356 }
357 pub fn grid(mut self, columns: usize, row_gap: f32, column_gap: f32) -> Self {
358 self.grid = Some(GridConfig {
359 columns,
360 row_gap,
361 column_gap,
362 });
363 self
364 }
365 pub fn grid_span(mut self, col_span: u16, row_span: u16) -> Self {
366 self.grid_col_span = Some(col_span);
367 self.grid_row_span = Some(row_span);
368 self
369 }
370 pub fn absolute(mut self) -> Self {
371 self.position_type = Some(PositionType::Absolute);
372 self
373 }
374 pub fn offset(
375 mut self,
376 left: Option<f32>,
377 top: Option<f32>,
378 right: Option<f32>,
379 bottom: Option<f32>,
380 ) -> Self {
381 self.offset_left = left;
382 self.offset_top = top;
383 self.offset_right = right;
384 self.offset_bottom = bottom;
385 self
386 }
387 pub fn offset_left(mut self, v: f32) -> Self {
388 self.offset_left = Some(v);
389 self
390 }
391 pub fn offset_right(mut self, v: f32) -> Self {
392 self.offset_right = Some(v);
393 self
394 }
395 pub fn offset_top(mut self, v: f32) -> Self {
396 self.offset_top = Some(v);
397 self
398 }
399 pub fn offset_bottom(mut self, v: f32) -> Self {
400 self.offset_bottom = Some(v);
401 self
402 }
403
404 pub fn margin(mut self, v: f32) -> Self {
405 self.margin_left = Some(v);
406 self.margin_right = Some(v);
407 self.margin_top = Some(v);
408 self.margin_bottom = Some(v);
409 self
410 }
411
412 pub fn margin_horizontal(mut self, v: f32) -> Self {
413 self.margin_left = Some(v);
414 self.margin_right = Some(v);
415 self
416 }
417
418 pub fn margin_vertical(mut self, v: f32) -> Self {
419 self.margin_top = Some(v);
420 self.margin_bottom = Some(v);
421 self
422 }
423 pub fn aspect_ratio(mut self, ratio: f32) -> Self {
424 self.aspect_ratio = Some(ratio);
425 self
426 }
427 pub fn painter(mut self, f: impl Fn(&mut crate::Scene, crate::Rect) + 'static) -> Self {
428 self.painter = Some(Rc::new(f));
429 self
430 }
431 pub fn scale(self, s: f32) -> Self {
432 self.scale2(s, s)
433 }
434 pub fn scale2(mut self, sx: f32, sy: f32) -> Self {
435 let mut t = self.transform.unwrap_or_else(Transform::identity);
436 t.scale_x *= sx;
437 t.scale_y *= sy;
438 self.transform = Some(t);
439 self
440 }
441 pub fn translate(mut self, x: f32, y: f32) -> Self {
442 let t = self.transform.unwrap_or_else(Transform::identity);
443 self.transform = Some(t.combine(&Transform::translate(x, y)));
444 self
445 }
446 pub fn rotate(mut self, radians: f32) -> Self {
447 let mut t = self.transform.unwrap_or_else(Transform::identity);
448 t.rotate += radians;
449 self.transform = Some(t);
450 self
451 }
452 pub fn weight(mut self, w: f32) -> Self {
453 let w = w.max(0.0);
454 self.flex_grow = Some(w);
455 self.flex_shrink = Some(1.0);
456 self.flex_basis = Some(0.0);
458 self
459 }
460 pub fn repaint_boundary(mut self) -> Self {
464 self.repaint_boundary = true;
465 self
466 }
467 pub fn on_action(mut self, f: impl Fn(crate::shortcuts::Action) -> bool + 'static) -> Self {
468 self.on_action = Some(Rc::new(f));
469 self
470 }
471
472 pub fn on_drag_start(
474 mut self,
475 f: impl Fn(crate::dnd::DragStart) -> Option<crate::dnd::DragPayload> + 'static,
476 ) -> Self {
477 self.on_drag_start = Some(Rc::new(f));
478 self
479 }
480
481 pub fn on_drag_end(mut self, f: impl Fn(crate::dnd::DragEnd) + 'static) -> Self {
483 self.on_drag_end = Some(Rc::new(f));
484 self
485 }
486
487 pub fn on_drag_enter(mut self, f: impl Fn(crate::dnd::DragOver) + 'static) -> Self {
489 self.on_drag_enter = Some(Rc::new(f));
490 self
491 }
492
493 pub fn on_drag_over(mut self, f: impl Fn(crate::dnd::DragOver) + 'static) -> Self {
495 self.on_drag_over = Some(Rc::new(f));
496 self
497 }
498
499 pub fn on_drag_leave(mut self, f: impl Fn(crate::dnd::DragOver) + 'static) -> Self {
501 self.on_drag_leave = Some(Rc::new(f));
502 self
503 }
504
505 pub fn on_drop(mut self, f: impl Fn(crate::dnd::DropEvent) -> bool + 'static) -> Self {
508 self.on_drop = Some(Rc::new(f));
509 self
510 }
511}