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