1use crate::element::{DivInteractivity, DivNode, Element, ElementNode};
4use crate::pod::{ActionScope, CursorShape};
5use crate::style::{AlignItems, Color, Edges, FlexDir, Justify, Length};
6
7#[repr(C)]
8#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
9pub struct Region {
10 pub x: i16,
11 pub y: i16,
12 pub w: i16,
13 pub h: i16,
14}
15
16impl Region {
17 pub const EMPTY: Self = Self {
19 x: 0,
20 y: 0,
21 w: 0,
22 h: 0,
23 };
24
25 #[inline(always)]
27 pub const fn new(x: i16, y: i16, w: i16, h: i16) -> Self {
28 Self { x, y, w, h }
29 }
30
31 #[inline(always)]
33 pub const fn is_empty(self) -> bool {
34 self.w <= 0 || self.h <= 0
35 }
36
37 #[inline(always)]
39 pub const fn right(self) -> i16 {
40 self.x.saturating_add(self.w)
41 }
42
43 #[inline(always)]
45 pub const fn bottom(self) -> i16 {
46 self.y.saturating_add(self.h)
47 }
48
49 #[inline(always)]
51 pub const fn area(self) -> i32 {
52 if self.w <= 0 || self.h <= 0 {
53 0
54 } else {
55 (self.w as i32) * (self.h as i32)
56 }
57 }
58
59 #[inline(always)]
61 pub const fn contains(self, px: i16, py: i16) -> bool {
62 px >= self.x && px < self.right() && py >= self.y && py < self.bottom()
63 }
64
65 #[inline(always)]
67 pub const fn overlaps_or_touches(self, other: Self) -> bool {
68 if self.is_empty() || other.is_empty() {
69 return false;
70 }
71 self.x <= other.right()
72 && self.right() >= other.x
73 && self.y <= other.bottom()
74 && self.bottom() >= other.y
75 }
76
77 #[inline]
79 pub fn intersect(self, other: Self) -> Self {
80 let x1 = self.x.max(other.x);
81 let y1 = self.y.max(other.y);
82 let x2 = self.right().min(other.right());
83 let y2 = self.bottom().min(other.bottom());
84 if x2 <= x1 || y2 <= y1 {
85 Self::EMPTY
86 } else {
87 Self {
88 x: x1,
89 y: y1,
90 w: x2 - x1,
91 h: y2 - y1,
92 }
93 }
94 }
95
96 #[inline]
98 pub fn union(self, other: Self) -> Self {
99 if self.is_empty() {
100 return other;
101 }
102 if other.is_empty() {
103 return self;
104 }
105 let x1 = self.x.min(other.x);
106 let y1 = self.y.min(other.y);
107 let x2 = self.right().max(other.right());
108 let y2 = self.bottom().max(other.bottom());
109 Self {
110 x: x1,
111 y: y1,
112 w: x2.saturating_sub(x1),
113 h: y2.saturating_sub(y1),
114 }
115 }
116}
117
118#[derive(Copy, Clone, Debug)]
120pub struct InteractionState {
121 pub pointer_pos: Option<(i16, i16)>,
122 pub is_pressed: bool,
123 pub focused_index: Option<u16>,
124 pub focus_visible: bool,
126 pub focus_ring_color: Color,
128}
129
130impl InteractionState {
131 #[inline(always)]
133 pub const fn const_default() -> Self {
134 Self {
135 pointer_pos: None,
136 is_pressed: false,
137 focused_index: None,
138 focus_visible: false,
139 focus_ring_color: Color::TRANSPARENT,
140 }
141 }
142}
143
144impl Default for InteractionState {
145 #[inline(always)]
146 fn default() -> Self {
147 Self::const_default()
148 }
149}
150
151#[derive(Copy, Clone)]
153pub struct HitRegion<'a> {
154 pub id: Option<u32>,
155 pub bounds: Region,
156 pub clip: Region,
157 pub focus_index: Option<u16>,
158 pub owner_pod: u16,
159 pub scroll_target: Option<(u32, i16)>,
160 pub interact: Option<&'a DivInteractivity<'a>>,
161 pub occlude: bool,
162 pub auto_repeat: bool,
163 pub has_hover_bg: bool,
164 pub has_active_bg: bool,
165 pub scroll_horizontal: bool,
166 pub cursor: Option<CursorShape>,
167}
168
169impl<'a> HitRegion<'a> {
170 #[inline(always)]
171 pub fn on_click(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
172 self.interact.and_then(|i| i.on_click)
173 }
174
175 #[inline(always)]
176 pub fn on_long_press(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
177 self.interact.and_then(|i| i.rare).and_then(|r| r.on_long_press)
178 }
179
180 #[inline(always)]
181 pub fn on_drag(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
182 self.interact.and_then(|i| i.rare).and_then(|r| r.on_drag)
183 }
184
185 #[inline(always)]
186 pub fn on_hover(&self) -> Option<&'a dyn Fn(bool, &mut ActionScope)> {
187 self.interact.and_then(|i| i.rare).and_then(|r| r.on_hover)
188 }
189
190 #[inline(always)]
191 pub fn on_pointer_move(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
192 self.interact
193 .and_then(|i| i.rare)
194 .and_then(|r| r.on_pointer_move)
195 }
196
197 #[inline(always)]
198 pub fn on_wheel(&self) -> Option<&'a dyn Fn(crate::pod::WheelEvent, &mut ActionScope)> {
199 self.interact.and_then(|i| i.rare).and_then(|r| r.on_wheel)
200 }
201
202 #[inline(always)]
203 pub fn on_pinch(&self) -> Option<&'a dyn Fn(crate::pod::PinchEvent, &mut ActionScope)> {
204 self.interact.and_then(|i| i.rare).and_then(|r| r.on_pinch)
205 }
206
207 #[inline(always)]
208 pub fn on_swipe(&self) -> Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)> {
209 self.interact.and_then(|i| i.rare).and_then(|r| r.on_swipe)
210 }
211
212 #[inline(always)]
213 pub fn on_key_input(&self) -> Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)> {
214 self.interact.and_then(|i| i.rare).and_then(|r| r.on_key_input)
215 }
216
217 #[inline(always)]
218 pub fn on_key_nav(&self) -> Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)> {
219 self.interact.and_then(|i| i.rare).and_then(|r| r.on_key_nav)
220 }
221
222 #[inline(always)]
223 pub fn on_navigate_back(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
224 self.interact.and_then(|i| i.rare).and_then(|r| r.on_navigate_back)
225 }
226
227 #[inline(always)]
228 pub fn on_secondary_click(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
229 self.interact.and_then(|i| i.rare).and_then(|r| r.on_secondary_click)
230 }
231}
232
233pub(super) const SUBVIEW_FLAG_ROOT: u8 = 1 << 0;
234pub(super) const SUBVIEW_FLAG_CACHED_PASS1: u8 = 1 << 1;
235pub(super) const SUBVIEW_FLAG_SKIP_PASS3: u8 = 1 << 2;
236pub(super) const SUBVIEW_FLAG_OWNER: u8 = 1 << 3;
237
238pub(crate) struct LayoutNode<'a, 'b> {
240 pub(super) element: &'a Element<'a>,
241 pub(super) min_size: i16,
242 pub(super) pref_size: i16,
243 pub(super) flex_grow: u8,
244 pub(super) subview_flags: u8,
245 pub(super) x: i16,
246 pub(super) y: i16,
247 pub(super) w: i16,
248 pub(super) h: i16,
249 pub(super) subview_pod: u16,
250 pub(super) children: &'b mut [LayoutNode<'a, 'b>],
251}
252
253#[derive(Copy, Clone, PartialEq, Eq)]
254pub(super) struct OuterBoxSpec {
255 pub(super) kind: u8,
256 pub(super) width: Length,
257 pub(super) height: Length,
258 pub(super) min_w: i16,
259 pub(super) max_w: i16,
260 pub(super) min_h: i16,
261 pub(super) max_h: i16,
262 pub(super) margin: Edges<i16>,
263 pub(super) pos: Option<(i16, i16)>,
264 pub(super) flex_grow: u8,
265 pub(super) align_self: Option<AlignItems>,
266 pub(super) flex_dir: FlexDir,
267 pub(super) justify: Justify,
268}
269
270#[inline]
271pub(super) fn outer_box_spec(elem: &Element<'_>) -> OuterBoxSpec {
272 match elem {
273 Element::Div(d) => OuterBoxSpec {
274 kind: 1,
275 width: d.style.width,
276 height: d.style.height,
277 min_w: d.min_w,
278 max_w: d.max_w,
279 min_h: d.min_h,
280 max_h: d.max_h,
281 margin: child_margin(elem),
282 pos: d.pos(),
283 flex_grow: d.style.flex_grow(),
284 align_self: d.align_self(),
285 flex_dir: d.style.flex_dir(),
286 justify: d.style.justify(),
287 },
288 Element::Text(_) => OuterBoxSpec {
289 kind: 2,
290 width: Length::Auto,
291 height: Length::Auto,
292 min_w: 0,
293 max_w: 0,
294 min_h: 0,
295 max_h: 0,
296 margin: Edges::all(0),
297 pos: None,
298 flex_grow: 0,
299 align_self: None,
300 flex_dir: FlexDir::Col,
301 justify: Justify::Start,
302 },
303 Element::Img(img) => OuterBoxSpec {
304 kind: 3,
305 width: Length::Px(img.width),
306 height: Length::Px(img.height),
307 min_w: img.width,
308 max_w: img.width,
309 min_h: img.height,
310 max_h: img.height,
311 margin: Edges::all(0),
312 pos: None,
313 flex_grow: 0,
314 align_self: None,
315 flex_dir: FlexDir::Col,
316 justify: Justify::Start,
317 },
318 Element::RichText(_) => OuterBoxSpec {
319 kind: 2,
320 width: Length::Auto,
321 height: Length::Auto,
322 min_w: 0,
323 max_w: 0,
324 min_h: 0,
325 max_h: 0,
326 margin: Edges::all(0),
327 pos: None,
328 flex_grow: 0,
329 align_self: None,
330 flex_dir: FlexDir::Col,
331 justify: Justify::Start,
332 },
333 Element::DecodedImage(img) => OuterBoxSpec {
334 kind: 3,
335 width: Length::Px(img.width),
336 height: Length::Px(img.height),
337 min_w: img.width,
338 max_w: img.width,
339 min_h: img.height,
340 max_h: img.height,
341 margin: Edges::all(0),
342 pos: None,
343 flex_grow: 0,
344 align_self: None,
345 flex_dir: FlexDir::Col,
346 justify: Justify::Start,
347 },
348 Element::Vector(vec_spec) => OuterBoxSpec {
349 kind: 3,
350 width: Length::Px(vec_spec.width),
351 height: Length::Px(vec_spec.height),
352 min_w: vec_spec.width,
353 max_w: vec_spec.width,
354 min_h: vec_spec.height,
355 max_h: vec_spec.height,
356 margin: Edges::all(0),
357 pos: None,
358 flex_grow: 0,
359 align_self: None,
360 flex_dir: FlexDir::Col,
361 justify: Justify::Start,
362 },
363 Element::SceneCanvas(spec) => OuterBoxSpec {
364 kind: 3,
365 width: Length::Px(spec.width),
366 height: Length::Px(spec.height),
367 min_w: spec.width,
368 max_w: spec.width,
369 min_h: spec.height,
370 max_h: spec.height,
371 margin: Edges::all(0),
372 pos: None,
373 flex_grow: 0,
374 align_self: None,
375 flex_dir: FlexDir::Col,
376 justify: Justify::Start,
377 },
378 Element::SubView { .. } => OuterBoxSpec {
379 kind: 4,
380 width: Length::Auto,
381 height: Length::Auto,
382 min_w: 0,
383 max_w: 0,
384 min_h: 0,
385 max_h: 0,
386 margin: Edges::all(0),
387 pos: None,
388 flex_grow: 0,
389 align_self: None,
390 flex_dir: FlexDir::Col,
391 justify: Justify::Start,
392 },
393 }
394}
395
396
397pub(super) fn count_children(mut cur: Option<&ElementNode<'_>>) -> usize {
398 let mut n = 0;
399 while let Some(node) = cur {
400 n += 1;
401 cur = node.next_sibling.get();
402 }
403 n
404}
405
406#[inline(always)]
407pub(super) fn child_pos(elem: &Element<'_>) -> Option<(i16, i16)> {
408 match elem {
409 Element::Div(d) => d.pos(),
410 _ => None,
411 }
412}
413
414#[inline(always)]
415pub(super) fn child_is_overlay(elem: &Element<'_>) -> bool {
416 matches!(elem, Element::Div(d) if d.is_overlay())
417}
418
419#[inline(always)]
420pub(super) fn div_padding(div: &DivNode<'_>) -> Edges<i16> {
421 Edges {
422 top: (div.style.padding.top as i16).saturating_add(div.extra_padding.top),
423 right: (div.style.padding.right as i16).saturating_add(div.extra_padding.right),
424 bottom: (div.style.padding.bottom as i16).saturating_add(div.extra_padding.bottom),
425 left: (div.style.padding.left as i16).saturating_add(div.extra_padding.left),
426 }
427}
428
429#[inline(always)]
430pub(super) fn child_margin(elem: &Element<'_>) -> Edges<i16> {
431 match elem {
432 Element::Div(d) => {
433 let base = d.style.margin_px() as i16;
434 Edges {
435 top: d.margin.top.saturating_add(base),
436 right: d.margin.right.saturating_add(base),
437 bottom: d.margin.bottom.saturating_add(base),
438 left: d.margin.left.saturating_add(base),
439 }
440 }
441 _ => Edges::all(0),
442 }
443}
444
445#[inline(always)]
446pub(super) fn clamp_w(elem: &Element<'_>, w: i16) -> i16 {
447 if let Element::Div(d) = elem {
448 let mut res = w.max(d.min_w);
449 if d.max_w > 0 {
450 res = res.min(d.max_w);
451 }
452 res
453 } else {
454 w
455 }
456}
457
458#[inline(always)]
459pub(super) fn clamp_h(elem: &Element<'_>, h: i16) -> i16 {
460 if let Element::Div(d) = elem {
461 let mut res = h.max(d.min_h);
462 if d.max_h > 0 {
463 res = res.min(d.max_h);
464 }
465 res
466 } else {
467 h
468 }
469}
470