1use core::time::Duration;
4
5use crate::paint::PaintEffects;
6use crate::pod::{ActionScope, CursorShape};
7use crate::style::{AlignItems, Color, Easing, Edges, LoopMode, Style, StyleRefinement};
8
9use super::ElementNode;
10
11const VIS_FLAG_HAS_POS: u8 = 1 << 0;
12const VIS_FLAG_HAS_SCROLL: u8 = 1 << 1;
13const VIS_FLAG_HAS_BG_GRAD: u8 = 1 << 2;
14const VIS_FLAG_BG_GRAD_HORIZ: u8 = 1 << 3;
15const VIS_FLAG_ITEMS_BASELINE: u8 = 1 << 4;
16const VIS_ALIGN_SHIFT: u8 = 5;
17pub(super) const FOCUS_BORDER_NONE: u8 = u8::MAX;
18
19#[inline(always)]
20pub(super) const fn pack_visual_flags(
21 has_pos: bool,
22 has_scroll: bool,
23 has_bg_grad: bool,
24 bg_grad_horiz: bool,
25 items_baseline: bool,
26 align_self: Option<AlignItems>,
27) -> u8 {
28 let mut f = 0u8;
29 if has_pos {
30 f |= VIS_FLAG_HAS_POS;
31 }
32 if has_scroll {
33 f |= VIS_FLAG_HAS_SCROLL;
34 }
35 if has_bg_grad {
36 f |= VIS_FLAG_HAS_BG_GRAD;
37 }
38 if bg_grad_horiz {
39 f |= VIS_FLAG_BG_GRAD_HORIZ;
40 }
41 if items_baseline {
42 f |= VIS_FLAG_ITEMS_BASELINE;
43 }
44 let align_code: u8 = match align_self {
45 None => 0,
46 Some(AlignItems::Stretch) => 1,
47 Some(AlignItems::Center) => 2,
48 Some(AlignItems::Start) => 3,
49 Some(AlignItems::End) => 4,
50 };
51 f | (align_code << VIS_ALIGN_SHIFT)
52}
53
54const STATE_FLAG_HAS_TEXT_COLOR: u8 = 1 << 0;
55
56#[derive(Copy, Clone, Debug, Default)]
58pub struct DivStateStyles<'a> {
59 pub hover_style: Option<&'a StyleRefinement>,
60 pub active_style: Option<&'a StyleRefinement>,
61 pub focus_style: Option<&'a StyleRefinement>,
62 pub text_color: Color,
63 pub text_size: u8,
64 pub flags: u8,
65}
66
67#[cfg(target_pointer_width = "64")]
68const _: () = assert!(core::mem::size_of::<DivStateStyles<'static>>() == 32);
69
70#[derive(Copy, Clone, Debug, Default)]
72pub struct DivExtended<'a> {
73 pub state_styles: Option<&'a DivStateStyles<'a>>,
74 pub effects: Option<&'a PaintEffects<'a>>,
75}
76
77#[derive(Copy, Clone)]
83pub struct DivExtPtr<'a> {
84 raw: *const u8,
85 _marker: core::marker::PhantomData<&'a ()>,
86}
87
88unsafe impl<'a> Send for DivExtPtr<'a> {}
90unsafe impl<'a> Sync for DivExtPtr<'a> {}
91
92impl<'a> core::fmt::Debug for DivExtPtr<'a> {
93 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
94 f.debug_struct("DivExtPtr")
95 .field("tag", &(self.raw.addr() & 0b11))
96 .finish()
97 }
98}
99
100const EXT_TAG_MASK: usize = 0b11;
101const EXT_TAG_STATE_ONLY: usize = 0b01;
102const EXT_TAG_EFFECTS_ONLY: usize = 0b10;
103const EXT_TAG_BOTH: usize = 0b11;
104
105const _: () = assert!(core::mem::align_of::<DivStateStyles<'static>>() >= 4);
106const _: () = assert!(core::mem::align_of::<PaintEffects<'static>>() >= 4);
107const _: () = assert!(core::mem::align_of::<DivExtended<'static>>() >= 4);
108
109impl<'a> DivExtPtr<'a> {
110 pub const NONE: Self = Self {
111 raw: core::ptr::null(),
112 _marker: core::marker::PhantomData,
113 };
114
115 #[inline(always)]
116 pub fn from_state_styles(s: &'a DivStateStyles<'a>) -> Self {
117 Self {
118 raw: (s as *const DivStateStyles<'a> as *const u8).map_addr(|a| a | EXT_TAG_STATE_ONLY),
119 _marker: core::marker::PhantomData,
120 }
121 }
122
123 #[inline(always)]
124 pub fn from_effects(e: &'a PaintEffects<'a>) -> Self {
125 Self {
126 raw: (e as *const PaintEffects<'a> as *const u8).map_addr(|a| a | EXT_TAG_EFFECTS_ONLY),
127 _marker: core::marker::PhantomData,
128 }
129 }
130
131 #[inline(always)]
132 pub fn from_both(ext: &'a DivExtended<'a>) -> Self {
133 Self {
134 raw: (ext as *const DivExtended<'a> as *const u8).map_addr(|a| a | EXT_TAG_BOTH),
135 _marker: core::marker::PhantomData,
136 }
137 }
138
139 #[inline(always)]
140 pub fn state_styles(&self) -> Option<&'a DivStateStyles<'a>> {
141 if self.raw.is_null() {
142 return None;
143 }
144 let tag = self.raw.addr() & EXT_TAG_MASK;
145 let base = self.raw.map_addr(|a| a & !EXT_TAG_MASK);
146 match tag {
147 EXT_TAG_STATE_ONLY => Some(unsafe { &*(base as *const DivStateStyles<'a>) }),
148 EXT_TAG_BOTH => unsafe { &*(base as *const DivExtended<'a>) }.state_styles,
149 _ => None,
150 }
151 }
152
153 #[inline(always)]
154 pub fn effects(&self) -> Option<&'a PaintEffects<'a>> {
155 if self.raw.is_null() {
156 return None;
157 }
158 let tag = self.raw.addr() & EXT_TAG_MASK;
159 let base = self.raw.map_addr(|a| a & !EXT_TAG_MASK);
160 match tag {
161 EXT_TAG_EFFECTS_ONLY => Some(unsafe { &*(base as *const PaintEffects<'a>) }),
162 EXT_TAG_BOTH => unsafe { &*(base as *const DivExtended<'a>) }.effects,
163 _ => None,
164 }
165 }
166}
167
168#[derive(Copy, Clone, Debug)]
170pub struct DivVisual<'a> {
171 pub extended: DivExtPtr<'a>,
172 pub transition: Option<&'a TransitionSpec>,
173 pub margin: Edges<i16>,
174 pub extra_padding: Edges<i16>,
175 pub bg_grad_to: Color,
176 pub focus_border_color: Color,
177 pub shadow_color: Color,
178 pub min_w: i16,
179 pub max_w: i16,
180 pub min_h: i16,
181 pub max_h: i16,
182 pub pos_x: i16,
183 pub pos_y: i16,
184 pub raw_scrollable_max: i16,
185 pub extra_flags: u8,
186 pub focus_border_w: u8,
187 pub shadow_blur: u8,
188 pub shadow_ox: i8,
189 pub shadow_oy: i8,
190 pub cursor: Option<CursorShape>,
191}
192
193#[cfg(target_pointer_width = "64")]
194const _: () = assert!(core::mem::size_of::<DivVisual<'static>>() == 64);
195
196impl<'a> DivVisual<'a> {
197 #[inline(always)]
198 pub const fn pos(&self) -> Option<(i16, i16)> {
199 if (self.extra_flags & VIS_FLAG_HAS_POS) != 0 {
200 Some((self.pos_x, self.pos_y))
201 } else {
202 None
203 }
204 }
205
206 #[inline(always)]
207 pub const fn scrollable_max(&self) -> Option<i16> {
208 if (self.extra_flags & VIS_FLAG_HAS_SCROLL) != 0 {
209 Some(self.raw_scrollable_max)
210 } else {
211 None
212 }
213 }
214
215 #[inline(always)]
216 pub const fn bg_grad(&self) -> Option<(Color, bool)> {
217 if (self.extra_flags & VIS_FLAG_HAS_BG_GRAD) != 0 {
218 Some((
219 self.bg_grad_to,
220 (self.extra_flags & VIS_FLAG_BG_GRAD_HORIZ) != 0,
221 ))
222 } else {
223 None
224 }
225 }
226
227 #[inline(always)]
228 pub const fn focus_border(&self) -> Option<(u8, Color)> {
229 if self.focus_border_w != FOCUS_BORDER_NONE {
230 Some((self.focus_border_w, self.focus_border_color))
231 } else {
232 None
233 }
234 }
235
236 #[inline(always)]
237 pub const fn shadow(&self) -> Option<(u8, i8, i8, Color)> {
238 if self.shadow_blur > 0 {
239 Some((
240 self.shadow_blur,
241 self.shadow_ox,
242 self.shadow_oy,
243 self.shadow_color,
244 ))
245 } else {
246 None
247 }
248 }
249
250 #[inline(always)]
251 pub const fn items_baseline(&self) -> bool {
252 (self.extra_flags & VIS_FLAG_ITEMS_BASELINE) != 0
253 }
254
255 #[inline(always)]
256 pub const fn align_self(&self) -> Option<AlignItems> {
257 match self.extra_flags >> VIS_ALIGN_SHIFT {
258 1 => Some(AlignItems::Stretch),
259 2 => Some(AlignItems::Center),
260 3 => Some(AlignItems::Start),
261 4 => Some(AlignItems::End),
262 _ => None,
263 }
264 }
265
266 #[inline(always)]
267 pub fn transition_spec(&self) -> Option<TransitionSpec> {
268 self.transition.copied()
269 }
270}
271
272static DEFAULT_DIV_VISUAL: DivVisual<'static> = DivVisual {
273 extended: DivExtPtr::NONE,
274 transition: None,
275 margin: Edges::all(0),
276 extra_padding: Edges::all(0),
277 bg_grad_to: Color::TRANSPARENT,
278 focus_border_color: Color::TRANSPARENT,
279 shadow_color: Color::TRANSPARENT,
280 min_w: 0,
281 max_w: 0,
282 min_h: 0,
283 max_h: 0,
284 pos_x: 0,
285 pos_y: 0,
286 raw_scrollable_max: 0,
287 extra_flags: 0,
288 focus_border_w: FOCUS_BORDER_NONE,
289 shadow_blur: 0,
290 shadow_ox: 0,
291 shadow_oy: 0,
292 cursor: None,
293};
294
295pub(super) static NO_FOCUS_BORDER_DIV_VISUAL: DivVisual<'static> = DivVisual {
297 extended: DivExtPtr::NONE,
298 transition: None,
299 margin: Edges::all(0),
300 extra_padding: Edges::all(0),
301 bg_grad_to: Color::TRANSPARENT,
302 focus_border_color: Color::TRANSPARENT,
303 shadow_color: Color::TRANSPARENT,
304 min_w: 0,
305 max_w: 0,
306 min_h: 0,
307 max_h: 0,
308 pos_x: 0,
309 pos_y: 0,
310 raw_scrollable_max: 0,
311 extra_flags: 0,
312 focus_border_w: 0,
313 shadow_blur: 0,
314 shadow_ox: 0,
315 shadow_oy: 0,
316 cursor: None,
317};
318
319#[derive(Copy, Clone)]
321pub struct DivRareHandlers<'a> {
322 pub on_long_press: Option<&'a dyn Fn(&mut ActionScope)>,
323 pub on_drag: Option<&'a dyn Fn(&mut ActionScope)>,
324 pub on_hover: Option<&'a dyn Fn(bool, &mut ActionScope)>,
325 pub on_pointer_move: Option<&'a dyn Fn(&mut ActionScope)>,
326 pub on_swipe: Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)>,
327 pub on_key_input: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
328 pub on_key_nav: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
329 pub on_navigate_back: Option<&'a dyn Fn(&mut ActionScope)>,
330 pub on_secondary_click: Option<&'a dyn Fn(&mut ActionScope)>,
331}
332
333#[derive(Copy, Clone)]
335pub struct DivInteractivity<'a> {
336 pub on_click: Option<&'a dyn Fn(&mut ActionScope)>,
337 pub rare: Option<&'a DivRareHandlers<'a>>,
338}
339
340pub struct DivNode<'a> {
342 pub style: Style,
343 pub first_child: Option<&'a ElementNode<'a>>,
344 pub visual: Option<&'a DivVisual<'a>>,
345 pub interact: Option<&'a DivInteractivity<'a>>,
346 pub raw_id: u32,
347 pub raw_active_bg: Color,
348 pub raw_hover_bg: Color,
349 pub opacity: u8,
350 pub z_index: i8,
351 pub grid_cols: u8,
352 pub flags: u8,
353}
354
355impl<'a> core::ops::Deref for DivNode<'a> {
356 type Target = DivVisual<'a>;
357 #[inline(always)]
358 fn deref(&self) -> &Self::Target {
359 self.visual.unwrap_or(&DEFAULT_DIV_VISUAL)
360 }
361}
362
363impl<'a> DivNode<'a> {
364 #[inline(always)]
365 pub fn state_styles(&self) -> Option<&'a DivStateStyles<'a>> {
366 self.extended.state_styles()
367 }
368
369 #[inline(always)]
370 pub fn effects(&self) -> Option<&'a PaintEffects<'a>> {
371 self.extended.effects()
372 }
373
374 #[inline(always)]
375 pub fn layout_ext(&self) -> Option<&'a crate::paint::LayoutExtras<'a>> {
376 self.extended.effects().and_then(|e| e.layout_ext)
377 }
378
379 #[inline(always)]
380 pub fn text_color(&self) -> Option<Color> {
381 self.state_styles().and_then(|s| {
382 if (s.flags & STATE_FLAG_HAS_TEXT_COLOR) != 0 {
383 Some(s.text_color)
384 } else {
385 None
386 }
387 })
388 }
389
390 #[inline(always)]
391 pub fn text_size(&self) -> Option<u8> {
392 self.state_styles()
393 .and_then(|s| if s.text_size > 0 { Some(s.text_size) } else { None })
394 }
395
396 #[inline(always)]
397 pub fn hover_style(&self) -> Option<StyleRefinement> {
398 self.state_styles().and_then(|s| s.hover_style.copied())
399 }
400
401 #[inline(always)]
402 pub fn active_style(&self) -> Option<StyleRefinement> {
403 self.state_styles().and_then(|s| s.active_style.copied())
404 }
405
406 #[inline(always)]
407 pub fn focus_style(&self) -> Option<StyleRefinement> {
408 self.state_styles().and_then(|s| s.focus_style.copied())
409 }
410
411 #[inline(always)]
412 pub const fn id(&self) -> Option<u32> {
413 if (self.flags & (1 << 5)) != 0 {
414 Some(self.raw_id)
415 } else {
416 None
417 }
418 }
419
420 #[inline(always)]
421 pub const fn active_bg(&self) -> Option<Color> {
422 if (self.flags & (1 << 6)) != 0 {
423 Some(self.raw_active_bg)
424 } else {
425 None
426 }
427 }
428
429 #[inline(always)]
430 pub const fn hover_bg(&self) -> Option<Color> {
431 if (self.flags & (1 << 7)) != 0 {
432 Some(self.raw_hover_bg)
433 } else {
434 None
435 }
436 }
437
438 #[inline(always)]
439 pub const fn focusable(&self) -> bool {
440 (self.flags & (1 << 0)) != 0
441 }
442
443 #[inline(always)]
444 pub const fn is_overlay(&self) -> bool {
445 (self.flags & (1 << 1)) != 0
446 }
447
448 #[inline(always)]
449 pub const fn occlude_hit(&self) -> bool {
450 (self.flags & (1 << 2)) != 0
451 }
452
453 #[inline(always)]
454 pub const fn auto_repeat(&self) -> bool {
455 (self.flags & (1 << 3)) != 0
456 }
457
458 #[inline(always)]
459 pub const fn clip_children(&self) -> bool {
460 (self.flags & (1 << 4)) != 0
461 }
462
463 #[inline(always)]
464 pub fn on_click(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
465 self.interact.and_then(|i| i.on_click)
466 }
467
468 #[inline(always)]
469 pub fn on_long_press(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
470 self.interact.and_then(|i| i.rare).and_then(|r| r.on_long_press)
471 }
472
473 #[inline(always)]
474 pub fn on_drag(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
475 self.interact.and_then(|i| i.rare).and_then(|r| r.on_drag)
476 }
477
478 #[inline(always)]
479 pub fn on_hover(&self) -> Option<&'a dyn Fn(bool, &mut ActionScope)> {
480 self.interact.and_then(|i| i.rare).and_then(|r| r.on_hover)
481 }
482
483 #[inline(always)]
484 pub fn on_pointer_move(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
485 self.interact
486 .and_then(|i| i.rare)
487 .and_then(|r| r.on_pointer_move)
488 }
489
490 #[inline(always)]
491 pub fn on_swipe(&self) -> Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)> {
492 self.interact.and_then(|i| i.rare).and_then(|r| r.on_swipe)
493 }
494
495 #[inline(always)]
496 pub fn on_key_input(&self) -> Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)> {
497 self.interact.and_then(|i| i.rare).and_then(|r| r.on_key_input)
498 }
499
500 #[inline(always)]
501 pub fn on_key_nav(&self) -> Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)> {
502 self.interact.and_then(|i| i.rare).and_then(|r| r.on_key_nav)
503 }
504
505 #[inline(always)]
506 pub fn on_navigate_back(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
507 self.interact.and_then(|i| i.rare).and_then(|r| r.on_navigate_back)
508 }
509
510 #[inline(always)]
511 pub fn on_secondary_click(&self) -> Option<&'a dyn Fn(&mut ActionScope)> {
512 self.interact.and_then(|i| i.rare).and_then(|r| r.on_secondary_click)
513 }
514}
515
516#[inline(always)]
518pub(super) fn duration_to_ms(d: Duration) -> u16 {
519 d.as_millis().min(u16::MAX as u128) as u16
520}
521
522#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
524pub struct EnterFrom {
525 pub opacity: u8,
527 pub dx: i16,
529 pub dy: i16,
531}
532
533#[derive(Copy, Clone, PartialEq, Eq, Debug)]
535pub struct TransitionSpec {
536 pub duration_ms: u16,
538 pub delay_ms: u16,
540 pub animate_color: bool,
542 pub animate_bounds: bool,
544 pub animate_opacity: bool,
546 pub easing: Easing,
548 pub loop_mode: LoopMode,
550 pub pulse_from_bg: Option<Color>,
552 pub enter: Option<EnterFrom>,
554}
555
556impl Default for TransitionSpec {
557 #[inline(always)]
558 fn default() -> Self {
559 Self {
560 duration_ms: 180,
561 delay_ms: 0,
562 animate_color: false,
563 animate_bounds: false,
564 animate_opacity: false,
565 easing: Easing::EaseOut,
566 loop_mode: LoopMode::Once,
567 pulse_from_bg: None,
568 enter: None,
569 }
570 }
571}
572