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