1use core::fmt;
10use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, Not};
11
12use nativeview_abi::ReprEnum;
13use nativeview_engine::{LayoutRect, Viewport};
14use nativeview_store::{NodeId, NodeStore};
15use nativeview_tables::tables::{
16 Display, Isolation, Layout as NvLayout, Position, TextContent, Visual, is_translucent_opacity,
17};
18
19use crate::scene::{StackingStyleSource, ZOrder};
20use crate::MAX_TREE_DEPTH;
21
22pub const DEFAULT_FLASH_TTL: u8 = 18;
24
25pub const MAX_FLASH_RECTS: usize = 256;
27
28#[repr(transparent)]
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
33pub struct InspectorFlags(u8);
34
35impl InspectorFlags {
36 pub const NONE: Self = Self(0);
37 pub const BOX_MODEL: Self = Self(1 << 0);
38 pub const PAINT_FLASHES: Self = Self(1 << 1);
39 pub const STACKING_BADGES: Self = Self(1 << 2);
40 pub const LAYOUT_WARNINGS: Self = Self(1 << 3);
41 pub const PICKER_MODE: Self = Self(1 << 4);
42 pub const ALL: Self = Self(
43 Self::BOX_MODEL.0
44 | Self::PAINT_FLASHES.0
45 | Self::STACKING_BADGES.0
46 | Self::LAYOUT_WARNINGS.0
47 | Self::PICKER_MODE.0,
48 );
49
50 #[inline]
53 pub fn is_toggle_shortcut(key: &str, code: &str, modifiers: nativeview_abi::EventFlags) -> bool {
54 let command = modifiers.meta() || modifiers.ctrl();
55 !modifiers.alt()
56 && ((key == "F12" && !command)
57 || (command && modifiers.shift() && (key.eq_ignore_ascii_case("i") || code == "KeyI")))
58 }
59
60 #[inline]
61 pub const fn from_bits_truncate(bits: u8) -> Self {
62 Self(bits & Self::ALL.0)
63 }
64
65 #[inline]
66 pub const fn bits(self) -> u8 {
67 self.0
68 }
69
70 #[inline]
71 pub const fn is_empty(self) -> bool {
72 self.0 == 0
73 }
74
75 #[inline]
76 pub const fn is_enabled(self) -> bool {
77 self.0 != 0
78 }
79
80 #[inline]
81 pub const fn contains(self, other: Self) -> bool {
82 (self.0 & other.0) == other.0
83 }
84
85 #[inline]
86 pub const fn intersects(self, other: Self) -> bool {
87 (self.0 & other.0) != 0
88 }
89
90 #[inline]
91 pub const fn with(self, other: Self) -> Self {
92 Self(self.0 | other.0)
93 }
94
95 #[inline]
96 pub const fn without(self, other: Self) -> Self {
97 Self(self.0 & !other.0)
98 }
99
100 #[inline]
101 pub const fn toggle(self, other: Self) -> Self {
102 Self(self.0 ^ other.0)
103 }
104}
105
106impl BitOr for InspectorFlags {
107 type Output = Self;
108 #[inline]
109 fn bitor(self, rhs: Self) -> Self {
110 Self(self.0 | rhs.0)
111 }
112}
113
114impl BitOrAssign for InspectorFlags {
115 #[inline]
116 fn bitor_assign(&mut self, rhs: Self) {
117 self.0 |= rhs.0;
118 }
119}
120
121impl BitAnd for InspectorFlags {
122 type Output = Self;
123 #[inline]
124 fn bitand(self, rhs: Self) -> Self {
125 Self(self.0 & rhs.0)
126 }
127}
128
129impl BitAndAssign for InspectorFlags {
130 #[inline]
131 fn bitand_assign(&mut self, rhs: Self) {
132 self.0 &= rhs.0;
133 }
134}
135
136impl BitXor for InspectorFlags {
137 type Output = Self;
138 #[inline]
139 fn bitxor(self, rhs: Self) -> Self {
140 Self(self.0 ^ rhs.0)
141 }
142}
143
144impl Not for InspectorFlags {
145 type Output = Self;
146 #[inline]
147 fn not(self) -> Self {
148 Self((!self.0) & Self::ALL.0)
149 }
150}
151
152#[derive(Debug, Clone, Copy, Default, PartialEq)]
157pub struct ResolvedBoxEdges {
158 pub margin: [f32; 4],
159 pub border: [f32; 4],
160 pub padding: [f32; 4],
161 pub content_w: f32,
162 pub content_h: f32,
163}
164
165const _: () = assert!(core::mem::size_of::<ResolvedBoxEdges>() == 56);
166
167impl ResolvedBoxEdges {
168 #[inline]
170 pub fn margin_box(self, border_box: LayoutRect) -> LayoutRect {
171 let [mt, mr, mb, ml] = self.margin;
172 let x = border_box.x - ml;
173 let y = border_box.y - mt;
174 let width = (border_box.width + ml + mr).max(0.0);
175 let height = (border_box.height + mt + mb).max(0.0);
176 LayoutRect {
177 x,
178 y,
179 width,
180 height,
181 }
182 }
183
184 #[inline]
186 pub fn padding_box(self, border_box: LayoutRect) -> LayoutRect {
187 let [bt, br, bb, bl] = self.border;
188 let bl = bl.max(0.0).min(border_box.width);
189 let bt = bt.max(0.0).min(border_box.height);
190 let width = (border_box.width - bl - br.max(0.0)).max(0.0);
191 let height = (border_box.height - bt - bb.max(0.0)).max(0.0);
192 LayoutRect {
193 x: border_box.x + bl,
194 y: border_box.y + bt,
195 width,
196 height,
197 }
198 }
199
200 #[inline]
202 pub fn content_box(self, border_box: LayoutRect) -> LayoutRect {
203 let pb = self.padding_box(border_box);
204 let [pt, pr, pb_e, pl] = self.padding;
205 let pl = pl.max(0.0).min(pb.width);
206 let pt = pt.max(0.0).min(pb.height);
207 let width = (pb.width - pl - pr.max(0.0)).max(0.0);
208 let height = (pb.height - pt - pb_e.max(0.0)).max(0.0);
209 LayoutRect {
210 x: pb.x + pl,
211 y: pb.y + pt,
212 width,
213 height,
214 }
215 }
216
217 #[inline]
219 pub fn scaled(self, sx: f32, sy: f32) -> Self {
220 let sx = if sx.is_finite() && sx > 0.0 { sx } else { 1.0 };
221 let sy = if sy.is_finite() && sy > 0.0 { sy } else { 1.0 };
222 Self {
223 margin: [
224 self.margin[0] * sy,
225 self.margin[1] * sx,
226 self.margin[2] * sy,
227 self.margin[3] * sx,
228 ],
229 border: [
230 self.border[0] * sy,
231 self.border[1] * sx,
232 self.border[2] * sy,
233 self.border[3] * sx,
234 ],
235 padding: [
236 self.padding[0] * sy,
237 self.padding[1] * sx,
238 self.padding[2] * sy,
239 self.padding[3] * sx,
240 ],
241 content_w: self.content_w * sx,
242 content_h: self.content_h * sy,
243 }
244 }
245}
246
247#[inline]
250pub fn ring_bands(outer: LayoutRect, inner: LayoutRect) -> [LayoutRect; 4] {
251 let top_h = (inner.y - outer.y).max(0.0);
252 let bot_y = (inner.y + inner.height).max(outer.y);
253 let bot_h = (outer.y + outer.height - bot_y).max(0.0);
254 let left_w = (inner.x - outer.x).max(0.0);
255 let right_x = (inner.x + inner.width).max(outer.x);
256 let right_w = (outer.x + outer.width - right_x).max(0.0);
257 let mid_y = inner.y.max(outer.y);
258 let mid_h = ((outer.y + outer.height).min(inner.y + inner.height) - mid_y).max(0.0);
259 [
260 LayoutRect { x: outer.x, y: outer.y, width: outer.width, height: top_h },
261 LayoutRect { x: right_x, y: mid_y, width: right_w, height: mid_h },
262 LayoutRect { x: outer.x, y: bot_y, width: outer.width, height: bot_h },
263 LayoutRect { x: outer.x, y: mid_y, width: left_w, height: mid_h },
264 ]
265}
266
267#[inline]
271pub fn margin_edge_bands(border_box: LayoutRect, margin: [f32; 4]) -> [LayoutRect; 4] {
272 let mt = if margin[0].is_finite() { margin[0].max(0.0) } else { 0.0 };
273 let mr = if margin[1].is_finite() { margin[1].max(0.0) } else { 0.0 };
274 let mb = if margin[2].is_finite() { margin[2].max(0.0) } else { 0.0 };
275 let ml = if margin[3].is_finite() { margin[3].max(0.0) } else { 0.0 };
276 let full_x = border_box.x - ml;
277 let full_w = (border_box.width + ml + mr).max(0.0);
278 [
279 LayoutRect { x: full_x, y: border_box.y - mt, width: full_w, height: mt },
280 LayoutRect { x: border_box.x + border_box.width, y: border_box.y, width: mr, height: border_box.height.max(0.0) },
281 LayoutRect { x: full_x, y: border_box.y + border_box.height, width: full_w, height: mb },
282 LayoutRect { x: full_x, y: border_box.y, width: ml, height: border_box.height.max(0.0) },
283 ]
284}
285
286#[derive(Debug, Clone, Copy)]
288pub struct CompactEdges(pub [f32; 4]);
289
290impl fmt::Display for CompactEdges {
291 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
292 let [t, r, b, l] = [
293 self.0[0].round() as i32,
294 self.0[1].round() as i32,
295 self.0[2].round() as i32,
296 self.0[3].round() as i32,
297 ];
298 if t == r && r == b && b == l {
299 write!(f, "{t}")
300 } else if t == b && l == r {
301 write!(f, "{t}/{r}")
302 } else {
303 write!(f, "{t}/{r}/{b}/{l}")
304 }
305 }
306}
307
308pub fn resolve_fallback_box_edges(
311 store: &NodeStore,
312 id: NodeId,
313 border_box: LayoutRect,
314) -> ResolvedBoxEdges {
315 let mut edges = ResolvedBoxEdges::default();
316 if let Some(l) = store.try_row::<NvLayout>(id) {
317 let px = |len: nativeview_store::Length| {
318 if len.is_points() {
319 let v = len.value();
320 if v.is_finite() { v } else { 0.0 }
321 } else {
322 0.0
323 }
324 };
325 edges.margin = [
326 px(l.margin_top()),
327 px(l.margin_right()),
328 px(l.margin_bottom()),
329 px(l.margin_left()),
330 ];
331 edges.padding = [
332 px(l.padding_top()).max(0.0),
333 px(l.padding_right()).max(0.0),
334 px(l.padding_bottom()).max(0.0),
335 px(l.padding_left()).max(0.0),
336 ];
337 }
338 if let Some(v) = store.try_row::<Visual>(id) {
339 let bw = v.border_width();
340 if bw.is_finite() && bw > 0.0 {
341 edges.border = [bw, bw, bw, bw];
342 }
343 }
344 let cb = edges.content_box(border_box);
345 edges.content_w = cb.width;
346 edges.content_h = cb.height;
347 edges
348}
349
350#[repr(transparent)]
352#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
353pub struct StackingReasonFlags(u8);
354
355impl StackingReasonFlags {
356 pub const NONE: Self = Self(0);
357 pub const ROOT: Self = Self(1 << 0);
358 pub const Z_INDEX: Self = Self(1 << 1);
359 pub const OPACITY: Self = Self(1 << 2);
360 pub const TRANSFORM: Self = Self(1 << 3);
361 pub const ISOLATION: Self = Self(1 << 4);
362 pub const TOP_LAYER: Self = Self(1 << 5);
363 pub const FIXED: Self = Self(1 << 6);
364 pub const STICKY: Self = Self(1 << 7);
365
366 #[inline]
367 pub const fn bits(self) -> u8 {
368 self.0
369 }
370
371 #[inline]
372 pub const fn is_empty(self) -> bool {
373 self.0 == 0
374 }
375
376 #[inline]
377 pub const fn contains(self, other: Self) -> bool {
378 (self.0 & other.0) == other.0
379 }
380
381 #[inline]
382 pub const fn with(self, other: Self) -> Self {
383 Self(self.0 | other.0)
384 }
385}
386
387pub fn compute_stacking_reasons<S: StackingStyleSource>(
390 store: &NodeStore,
391 style: &S,
392 id: NodeId,
393 is_top_layer_root: bool,
394) -> (bool, Option<ZOrder>, StackingReasonFlags) {
395 let box_parent_display = if id == NodeId::ROOT || is_top_layer_root {
396 Display::Block
397 } else {
398 let mut cur = store.parent(id);
399 let mut parent_display = Display::Block;
400 for _ in 0..MAX_TREE_DEPTH {
401 let Some(p) = cur else {
402 break;
403 };
404 if !style.is_display_contents(store, p) {
405 parent_display = style.display_of(store, p);
406 break;
407 }
408 cur = store.parent(p).filter(|&gp| gp != p);
409 }
410 parent_display
411 };
412
413 let pos = style.position_of(store, id);
414 let parent_is_flex_or_grid = matches!(
415 box_parent_display,
416 Display::Flex | Display::InlineFlex | Display::Grid
417 );
418 let eff_z = if pos != Position::Static || parent_is_flex_or_grid {
419 style.z_index_of(store, id)
420 } else {
421 None
422 };
423
424 let mut reasons = StackingReasonFlags::NONE;
425 if id == NodeId::ROOT {
426 reasons = reasons.with(StackingReasonFlags::ROOT);
427 }
428 if is_top_layer_root {
429 reasons = reasons.with(StackingReasonFlags::TOP_LAYER);
430 }
431 if eff_z.is_some() {
432 reasons = reasons.with(StackingReasonFlags::Z_INDEX);
433 }
434 if pos == Position::Fixed {
435 reasons = reasons.with(StackingReasonFlags::FIXED);
436 } else if pos == Position::Sticky {
437 reasons = reasons.with(StackingReasonFlags::STICKY);
438 }
439 if is_translucent_opacity(style.opacity_of(store, id)) {
440 reasons = reasons.with(StackingReasonFlags::OPACITY);
441 }
442 if style.has_transform(store, id) {
443 reasons = reasons.with(StackingReasonFlags::TRANSFORM);
444 }
445 if style.isolation_of(store, id) == Isolation::Isolate {
446 reasons = reasons.with(StackingReasonFlags::ISOLATION);
447 }
448 let extra_table_sc = store
449 .node_tables(id)
450 .intersects(store.stacking_context_tables());
451 let is_real_sc = !reasons.is_empty() || extra_table_sc;
452 (is_real_sc, eff_z.map(ZOrder), reasons)
453}
454
455#[repr(transparent)]
457#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
458pub struct LayoutWarningFlags(u8);
459
460impl LayoutWarningFlags {
461 pub const NONE: Self = Self(0);
462 pub const ZERO_SIZE_COLLAPSE: Self = Self(1 << 0);
463 pub const OVERFLOW_CLIP: Self = Self(1 << 1);
464 pub const OFFSCREEN: Self = Self(1 << 2);
465
466 #[inline]
467 pub const fn bits(self) -> u8 {
468 self.0
469 }
470
471 #[inline]
472 pub const fn is_empty(self) -> bool {
473 self.0 == 0
474 }
475
476 #[inline]
477 pub const fn contains(self, other: Self) -> bool {
478 (self.0 & other.0) == other.0
479 }
480
481 #[inline]
482 pub const fn with(self, other: Self) -> Self {
483 Self(self.0 | other.0)
484 }
485}
486
487#[inline]
493#[allow(clippy::too_many_arguments)]
494pub fn compute_layout_warnings(
495 store: &NodeStore,
496 id: NodeId,
497 layout_box: LayoutRect,
498 visual_box: LayoutRect,
499 edges: ResolvedBoxEdges,
500 viewport: Viewport,
501 clips_overflow: bool,
502 is_scrollable: bool,
503) -> LayoutWarningFlags {
504 let mut flags = LayoutWarningFlags::NONE;
505 let has_content = !store.children(id).is_empty()
506 || store
507 .try_row::<TextContent>(id)
508 .is_some_and(|t| !t.text().is_empty());
509 if has_content && (layout_box.width <= 0.0 || layout_box.height <= 0.0) {
510 flags = flags.with(LayoutWarningFlags::ZERO_SIZE_COLLAPSE);
511 }
512 let client = edges.padding_box(layout_box);
513 if clips_overflow
514 && !is_scrollable
515 && (edges.content_w > client.width + 0.5 || edges.content_h > client.height + 0.5)
516 {
517 flags = flags.with(LayoutWarningFlags::OVERFLOW_CLIP);
518 }
519 if viewport.width > 0.0
520 && viewport.height > 0.0
521 && visual_box.width > 0.0
522 && visual_box.height > 0.0
523 && (visual_box.x + visual_box.width <= 0.0
524 || visual_box.y + visual_box.height <= 0.0
525 || visual_box.x >= viewport.width
526 || visual_box.y >= viewport.height)
527 {
528 flags = flags.with(LayoutWarningFlags::OFFSCREEN);
529 }
530 flags
531}
532
533#[repr(u8)]
535#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, ReprEnum)]
536#[repr_enum(fallback = None)]
537pub enum SceneDirtyKind {
538 #[default]
540 None = 0,
541 CompositeOnly = 1,
543 PaintOnly = 2,
545 LayoutOrTree = 3,
547}
548
549impl SceneDirtyKind {
550 #[inline]
551 pub const fn label(self) -> &'static str {
552 match self {
553 Self::None => "Clean",
554 Self::CompositeOnly => "CompositeOnly",
555 Self::PaintOnly => "PaintOnly",
556 Self::LayoutOrTree => "LayoutOrTree",
557 }
558 }
559}
560
561#[derive(Debug, Clone, Copy, Default, PartialEq)]
563pub struct InspectorNodeSnapshot {
564 pub node_id: NodeId,
565 pub border_box: LayoutRect,
566 pub edges: ResolvedBoxEdges,
567 pub is_real_sc: bool,
568 pub z_index: Option<ZOrder>,
569 pub sc_reason: StackingReasonFlags,
570 pub warnings: LayoutWarningFlags,
571 pub paint_count: u32,
572 pub layout_count: u32,
573 pub recompose_count: u32,
574 pub last_dirty_kind: u8,
575}
576
577impl InspectorNodeSnapshot {
578 #[inline]
579 pub fn margin_box(&self) -> LayoutRect {
580 self.edges.margin_box(self.border_box)
581 }
582
583 #[inline]
584 pub fn padding_box(&self) -> LayoutRect {
585 self.edges.padding_box(self.border_box)
586 }
587
588 #[inline]
589 pub fn content_box(&self) -> LayoutRect {
590 self.edges.content_box(self.border_box)
591 }
592
593 #[inline]
594 pub fn dirty_kind(&self) -> SceneDirtyKind {
595 SceneDirtyKind::from_u8(self.last_dirty_kind)
596 }
597}
598
599mod heat;
600pub use heat::{InspectorHeatTracker, NODE_HEAT_FLASHING, NODE_HEAT_OCCUPIED, NodeHeat};
601
602#[cfg(test)]
603mod tests;