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_swipe(&self) -> Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)> {
199 self.interact.and_then(|i| i.rare).and_then(|r| r.on_swipe)
200 }
201
202 #[inline(always)]
203 pub fn on_key_input(&self) -> Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)> {
204 self.interact.and_then(|i| i.rare).and_then(|r| r.on_key_input)
205 }
206
207 #[inline(always)]
208 pub fn on_key_nav(&self) -> Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)> {
209 self.interact.and_then(|i| i.rare).and_then(|r| r.on_key_nav)
210 }
211
212 #[inline(always)]
213 pub fn on_navigate_back(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
214 self.interact.and_then(|i| i.rare).and_then(|r| r.on_navigate_back)
215 }
216
217 #[inline(always)]
218 pub fn on_secondary_click(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
219 self.interact.and_then(|i| i.rare).and_then(|r| r.on_secondary_click)
220 }
221}
222
223pub(super) const SUBVIEW_FLAG_ROOT: u8 = 1 << 0;
224pub(super) const SUBVIEW_FLAG_CACHED_PASS1: u8 = 1 << 1;
225pub(super) const SUBVIEW_FLAG_SKIP_PASS3: u8 = 1 << 2;
226pub(super) const SUBVIEW_FLAG_OWNER: u8 = 1 << 3;
227
228pub(crate) struct LayoutNode<'a, 'b> {
230 pub(super) element: &'a Element<'a>,
231 pub(super) min_size: i16,
232 pub(super) pref_size: i16,
233 pub(super) flex_grow: u8,
234 pub(super) subview_flags: u8,
235 pub(super) x: i16,
236 pub(super) y: i16,
237 pub(super) w: i16,
238 pub(super) h: i16,
239 pub(super) subview_pod: u16,
240 pub(super) children: &'b mut [LayoutNode<'a, 'b>],
241}
242
243#[derive(Copy, Clone, PartialEq, Eq)]
244pub(super) struct OuterBoxSpec {
245 pub(super) kind: u8,
246 pub(super) width: Length,
247 pub(super) height: Length,
248 pub(super) min_w: i16,
249 pub(super) max_w: i16,
250 pub(super) min_h: i16,
251 pub(super) max_h: i16,
252 pub(super) margin: Edges<i16>,
253 pub(super) pos: Option<(i16, i16)>,
254 pub(super) flex_grow: u8,
255 pub(super) align_self: Option<AlignItems>,
256 pub(super) flex_dir: FlexDir,
257 pub(super) justify: Justify,
258}
259
260#[inline]
261pub(super) fn outer_box_spec(elem: &Element<'_>) -> OuterBoxSpec {
262 match elem {
263 Element::Div(d) => OuterBoxSpec {
264 kind: 1,
265 width: d.style.width,
266 height: d.style.height,
267 min_w: d.min_w,
268 max_w: d.max_w,
269 min_h: d.min_h,
270 max_h: d.max_h,
271 margin: child_margin(elem),
272 pos: d.pos(),
273 flex_grow: d.style.flex_grow(),
274 align_self: d.align_self(),
275 flex_dir: d.style.flex_dir(),
276 justify: d.style.justify(),
277 },
278 Element::Text(_) => OuterBoxSpec {
279 kind: 2,
280 width: Length::Auto,
281 height: Length::Auto,
282 min_w: 0,
283 max_w: 0,
284 min_h: 0,
285 max_h: 0,
286 margin: Edges::all(0),
287 pos: None,
288 flex_grow: 0,
289 align_self: None,
290 flex_dir: FlexDir::Col,
291 justify: Justify::Start,
292 },
293 Element::Img(img) => OuterBoxSpec {
294 kind: 3,
295 width: Length::Px(img.width),
296 height: Length::Px(img.height),
297 min_w: img.width,
298 max_w: img.width,
299 min_h: img.height,
300 max_h: img.height,
301 margin: Edges::all(0),
302 pos: None,
303 flex_grow: 0,
304 align_self: None,
305 flex_dir: FlexDir::Col,
306 justify: Justify::Start,
307 },
308 Element::RichText(_) => OuterBoxSpec {
309 kind: 2,
310 width: Length::Auto,
311 height: Length::Auto,
312 min_w: 0,
313 max_w: 0,
314 min_h: 0,
315 max_h: 0,
316 margin: Edges::all(0),
317 pos: None,
318 flex_grow: 0,
319 align_self: None,
320 flex_dir: FlexDir::Col,
321 justify: Justify::Start,
322 },
323 Element::DecodedImage(img) => OuterBoxSpec {
324 kind: 3,
325 width: Length::Px(img.width),
326 height: Length::Px(img.height),
327 min_w: img.width,
328 max_w: img.width,
329 min_h: img.height,
330 max_h: img.height,
331 margin: Edges::all(0),
332 pos: None,
333 flex_grow: 0,
334 align_self: None,
335 flex_dir: FlexDir::Col,
336 justify: Justify::Start,
337 },
338 Element::Vector(vec_spec) => OuterBoxSpec {
339 kind: 3,
340 width: Length::Px(vec_spec.width),
341 height: Length::Px(vec_spec.height),
342 min_w: vec_spec.width,
343 max_w: vec_spec.width,
344 min_h: vec_spec.height,
345 max_h: vec_spec.height,
346 margin: Edges::all(0),
347 pos: None,
348 flex_grow: 0,
349 align_self: None,
350 flex_dir: FlexDir::Col,
351 justify: Justify::Start,
352 },
353 Element::SubView { .. } => OuterBoxSpec {
354 kind: 4,
355 width: Length::Auto,
356 height: Length::Auto,
357 min_w: 0,
358 max_w: 0,
359 min_h: 0,
360 max_h: 0,
361 margin: Edges::all(0),
362 pos: None,
363 flex_grow: 0,
364 align_self: None,
365 flex_dir: FlexDir::Col,
366 justify: Justify::Start,
367 },
368 }
369}
370
371
372pub(super) fn count_children(mut cur: Option<&ElementNode<'_>>) -> usize {
373 let mut n = 0;
374 while let Some(node) = cur {
375 n += 1;
376 cur = node.next_sibling.get();
377 }
378 n
379}
380
381#[inline(always)]
382pub(super) fn child_pos(elem: &Element<'_>) -> Option<(i16, i16)> {
383 match elem {
384 Element::Div(d) => d.pos(),
385 _ => None,
386 }
387}
388
389#[inline(always)]
390pub(super) fn child_is_overlay(elem: &Element<'_>) -> bool {
391 matches!(elem, Element::Div(d) if d.is_overlay())
392}
393
394#[inline(always)]
395pub(super) fn div_padding(div: &DivNode<'_>) -> Edges<i16> {
396 Edges {
397 top: (div.style.padding.top as i16).saturating_add(div.extra_padding.top),
398 right: (div.style.padding.right as i16).saturating_add(div.extra_padding.right),
399 bottom: (div.style.padding.bottom as i16).saturating_add(div.extra_padding.bottom),
400 left: (div.style.padding.left as i16).saturating_add(div.extra_padding.left),
401 }
402}
403
404#[inline(always)]
405pub(super) fn child_margin(elem: &Element<'_>) -> Edges<i16> {
406 match elem {
407 Element::Div(d) => {
408 let base = d.style.margin_px() as i16;
409 Edges {
410 top: d.margin.top.saturating_add(base),
411 right: d.margin.right.saturating_add(base),
412 bottom: d.margin.bottom.saturating_add(base),
413 left: d.margin.left.saturating_add(base),
414 }
415 }
416 _ => Edges::all(0),
417 }
418}
419
420#[inline(always)]
421pub(super) fn clamp_w(elem: &Element<'_>, w: i16) -> i16 {
422 if let Element::Div(d) = elem {
423 let mut res = w.max(d.min_w);
424 if d.max_w > 0 {
425 res = res.min(d.max_w);
426 }
427 res
428 } else {
429 w
430 }
431}
432
433#[inline(always)]
434pub(super) fn clamp_h(elem: &Element<'_>, h: i16) -> i16 {
435 if let Element::Div(d) = elem {
436 let mut res = h.max(d.min_h);
437 if d.max_h > 0 {
438 res = res.min(d.max_h);
439 }
440 res
441 } else {
442 h
443 }
444}
445