sk-tree 0.5.0

Skathi's retained node tree and the renderer's host functions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
//! The DOM surface the JS mirror nodes answer from.

use crate::live_state::{LiveRef, LiveStates};
use crate::tree::{self, Kind, Tree};

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum QueryMode {
    Descendants,
    Document,
    SelfOnly,
    Closest,
}

pub fn query(selector: &str, root: u32, mode: QueryMode) -> Result<Vec<u32>, String> {
    tree::read_tree(|t| {
        if t.node(root).is_none() {
            return Ok(Vec::new());
        }
        let ids: Vec<u32> = match mode {
            QueryMode::Descendants => descendants(t, root),
            QueryMode::Document => std::iter::once(root).chain(descendants(t, root)).collect(),
            QueryMode::SelfOnly => vec![root],
            QueryMode::Closest => ancestors_from(t, root),
        };
        // Live state answers `:hover` / `:active` / `:focus*` as a browser would; the cascade's matcher
        // says "would match" (`crate::live_state`).
        let live = LiveStates::take(t);
        let candidates: Vec<LiveRef<'_>> =
            ids.iter().map(|&id| LiveRef::new(t, id, &live)).collect();
        let result =
            sk_css::query_all_in(selector, &candidates, Some(LiveRef::new(t, root, &live)))?;
        for w in &result.warnings {
            crate::log::record("css", format!("[query] {w}"));
        }
        let mut matched: Vec<u32> = result.matched.iter().map(|&i| ids[i]).collect();
        if mode == QueryMode::Closest {
            matched.truncate(1);
        }
        Ok(matched)
    })
}

fn descendants(t: &Tree, id: u32) -> Vec<u32> {
    let mut out = Vec::new();
    fn walk(t: &Tree, id: u32, out: &mut Vec<u32>) {
        let Some(node) = t.node(id) else { return };
        for &c in &node.children {
            if t.node(c).is_some_and(|n| n.kind == Kind::Element) {
                out.push(c);
            }
            walk(t, c, out);
        }
    }
    walk(t, id, &mut out);
    out
}

fn ancestors_from(t: &Tree, id: u32) -> Vec<u32> {
    let mut out = Vec::new();
    let mut cursor = id;
    // A corrupt parent chain must not hang the call.
    for _ in 0..4096 {
        match t.node(cursor) {
            Some(n) => {
                if n.kind == Kind::Element {
                    out.push(cursor);
                }
                if n.parent == 0 {
                    break;
                }
                cursor = n.parent;
            }
            None => break,
        }
    }
    out
}

/// A browser lays out on read; so does this: only for a node with no rectangle while the tree holds
/// changes no frame has taken (`onMounted` + `nextTick` measuring). Returns `true` when the answer
/// stays STALE (the host cannot draw inside a gpui input listener).
fn force_layout_if_unpainted(id: u32) -> bool {
    if id == 0 {
        return false;
    }
    if crate::render::bounds_of(id).is_some() {
        return false;
    }
    if !tree::read_tree(|t| t.node(id).is_some()) || !crate::layout_owed() {
        return false;
    }
    let before = crate::render::build_count();
    crate::sync_layout_by(crate::perf::SyncCaller::Bounds);
    crate::render::build_count() == before && crate::render::bounds_of(id).is_none()
}

/// The same forced layout for reads that depend on the node's content (`scrollWidth`/`scrollHeight`
/// and the writes clamped against them), also for a node that has a rectangle
/// (`list.append(row); list.scrollTop = list.scrollHeight`). Returns `true` when stale.
#[must_use]
pub(crate) fn layout_if_stale(id: u32) -> bool {
    if id == 0 || !tree::read_tree(|t| t.node(id).is_some()) || !crate::layout_owed() {
        return false;
    }
    let before = crate::render::build_count();
    crate::sync_layout_by(crate::perf::SyncCaller::Scroll);
    crate::render::build_count() == before
}

/// `getBoundingClientRect()` in CSS px relative to the APP ROOT (what `position: fixed` resolves
/// against); window-client coordinates would make floating-ui measure and clip in different spaces.
/// `None` = not painted; the `bool` says a missing answer is STALE.
pub fn bounds(id: u32) -> (Option<(f32, f32, f32, f32)>, bool) {
    let stale = force_layout_if_unpainted(id);
    (bounds_now(id), stale)
}

/// [`bounds`] for a script's geometry read: lays out first when changes are pending, as a
/// browser's forced reflow does.
pub(crate) fn current_bounds(id: u32) -> (Option<(f32, f32, f32, f32)>, bool) {
    if id == 0 || crate::render::bounds_of(id).is_none() {
        return bounds(id);
    }
    if crate::layout_owed() && tree::read_tree(|t| t.node(id).is_some()) {
        crate::sync_layout_by(crate::perf::SyncCaller::Bounds);
    }
    (bounds_now(id), false)
}

pub fn transformed(id: u32) -> Option<(f32, f32, f32, f32)> {
    let (x, y, w, h) = crate::render::transformed_of(id)?;
    let (ox, oy) = root_origin();
    let (sx, sy) = crate::scroll::unpainted_shift(id);
    Some((x - ox + sx, y - oy + sy, w, h))
}

fn root_origin() -> (f32, f32) {
    crate::render::bounds_of(tree::read_tree(|t| t.root))
        .map(|(rx, ry, _, _)| (rx, ry))
        .unwrap_or((0.0, 0.0))
}

/// How many element descendants `id` has and how many of them the last frame painted inside its
/// nearest scroll container (`id` itself counts). `None` outside a scroll container or before a paint.
/// Never lays out: a dev advisory must not change the frame it measures.
pub fn visible_share(id: u32) -> Option<(usize, usize)> {
    let (chain, ids) = tree::read_tree(|t| (ancestors_from(t, id), descendants(t, id)));
    let scroller = chain
        .into_iter()
        .find(|&a| crate::render::scroll_handle_of(a).is_some())?;
    let window = crate::windows::window_of_node(scroller);
    crate::windows::peek(window, |w| {
        let &(vx, vy, vw, vh) = w.bounds.get(&scroller)?;
        let seen = ids
            .iter()
            .filter(|&id| {
                w.bounds.get(id).is_some_and(|&(x, y, width, height)| {
                    x < vx + vw && x + width > vx && y < vy + vh && y + height > vy
                })
            })
            .count();
        Some((ids.len(), seen))
    })
    .flatten()
}

fn bounds_now(id: u32) -> Option<(f32, f32, f32, f32)> {
    let (x, y, w, h) = crate::render::bounds_of(id)?;
    let (ox, oy) = root_origin();
    let (sx, sy) = crate::scroll::unpainted_shift(id);
    Some((x - ox + sx, y - oy + sy, w, h))
}

/// The properties `getComputedStyle` answers, besides every custom property. Each ALWAYS has a value: a
/// browser's computed style has no "absent", and a component that reads `display` expects a keyword, not "".
pub const COMPUTED_PROPERTIES: &[&str] = &[
    "display",
    "position",
    "visibility",
    "overflow-x",
    "overflow-y",
    "overflow",
    "scrollbar-gutter",
    "opacity",
    "z-index",
    "pointer-events",
    "transform",
    "transform-origin",
    "zoom",
    "translate",
    "rotate",
    "scale",
    "perspective",
    "filter",
    "will-change",
    "contain",
    "container-type",
    "container-name",
    "backdrop-filter",
    "animation-name",
    "animation-duration",
    "animation-delay",
    "animation-fill-mode",
    "transition-property",
    "transition-duration",
    "transition-delay",
    "width",
    "height",
    "padding-top",
    "padding-right",
    "padding-bottom",
    "padding-left",
    "margin-top",
    "margin-right",
    "margin-bottom",
    "margin-left",
    "border-top-width",
    "border-right-width",
    "border-bottom-width",
    "border-left-width",
    "direction",
    "interpolate-size",
    "dynamic-range-limit",
    "font-size",
    "color",
    "text-shadow",
    "color-scheme",
    "aspect-ratio",
    "corner-top-left-shape",
    "corner-top-right-shape",
    "corner-bottom-right-shape",
    "corner-bottom-left-shape",
    "flex-direction",
    "flex-wrap",
    "cursor",
];

/// One computed property of `id` (a [`COMPUTED_PROPERTIES`] name or a custom property); `None` for a node
/// not in the tree, or a name outside both.
pub fn computed_value(id: u32, name: &str) -> Option<String> {
    tree::flush_pending_style();
    tree::read_tree(|t| computed_in(t, id, name))
}

/// `(name, value)` for every [`COMPUTED_PROPERTIES`] entry, then each custom property.
pub fn computed_entries(id: u32) -> Option<Vec<(String, String)>> {
    tree::flush_pending_style();
    tree::read_tree(|t| {
        let n = t.node(id)?;
        let mut out: Vec<(String, String)> = COMPUTED_PROPERTIES
            .iter()
            .filter_map(|&k| computed_in(t, id, k).map(|v| (k.to_string(), v)))
            .collect();
        if let Some(cs) = n.computed.as_ref() {
            out.extend(
                cs.vars
                    .iter()
                    .filter(|(k, _)| k.starts_with("--"))
                    .map(|(k, v)| (k.to_string(), v.to_string())),
            );
        }
        Some(out)
    })
}

#[cfg(test)]
/// [`computed_entries`] as `name=value` lines.
pub fn computed_style(id: u32) -> Option<String> {
    let mut out = String::new();
    for (k, v) in computed_entries(id)? {
        out.push_str(&k);
        out.push('=');
        out.push_str(&v);
        out.push('\n');
    }
    Some(out)
}

fn computed_in(t: &Tree, id: u32, name: &str) -> Option<String> {
    let default = sk_css::ComputedStates::default();
    let cs = t.node(id)?.computed.as_ref().unwrap_or(&default);
    if name.starts_with("--") {
        return cs.vars.get(name).map(str::to_string);
    }
    let animations = |f: &dyn Fn(&sk_css::AnimationSpec) -> String| {
        cs.animations.iter().map(f).collect::<Vec<_>>().join(", ")
    };
    let transitions = |f: &dyn Fn(&sk_css::TransitionSpec) -> String| {
        cs.transitions.iter().map(f).collect::<Vec<_>>().join(", ")
    };
    Some(match name {
        "display" => match cs.base.display.unwrap_or(gpui::Display::Block) {
            gpui::Display::Block => "block",
            gpui::Display::Flex => "flex",
            gpui::Display::Grid => "grid",
            gpui::Display::None => "none",
        }
        .into(),
        // `fixed` is laid out as absolute (see the mapper) but the AUTHORED value is what a
        // component reads back, and @floating-ui branches on exactly this string.
        "position" => match (cs.extras.fixed, cs.base.position) {
            (Some(true), _) => "fixed",
            _ if cs.extras.sticky == Some(true) => "sticky",
            (_, Some(gpui::Position::Absolute)) => "absolute",
            (_, Some(gpui::Position::Relative)) => "relative",
            (_, None) => "static",
        }
        .into(),
        // The CSS value, inherited, not gpui's paint switch: a screen-reader-only box paints nothing
        // but is `visible` (render::css_visible).
        "visibility" => if crate::render::css_visible(t, id) {
            "visible"
        } else {
            "hidden"
        }
        .into(),
        // `auto` and `clip` as computed (sk_css::Extras::overflow_names), not the `scroll` and
        // `hidden` gpui folds them into.
        "overflow-x" | "overflow-y" | "overflow" => {
            let [x, y] = cs.extras.overflow_names(&cs.base);
            match name {
                "overflow-x" => x.to_string(),
                "overflow-y" => y.to_string(),
                _ if x == y => x.to_string(),
                _ => format!("{x} {y}"),
            }
        }
        // `auto` is the CSS initial value AND an exact description of what we do: gpui's scrollbars
        // are overlays, so no gutter is ever reserved and none can be. (Reka's scroll lock reads it.)
        "scrollbar-gutter" => "auto".into(),
        "opacity" => format!("{}", cs.base.opacity.unwrap_or(1.0)),
        "z-index" => cs
            .extras
            .z()
            .map(|z| z.to_string())
            .unwrap_or_else(|| "auto".into()),
        "pointer-events" => if cs.extras.pointer_events_none == Some(true) {
            "none"
        } else {
            "auto"
        }
        .into(),
        // The function list, not the `matrix()` a browser resolves to: it is what the cascade holds,
        // and floating-ui only tests it against `"none"`.
        "transform" => transform_text(cs.base.vn_transform),
        // Resolved against the border box, as a browser's computed value is: Svelte's `flip` divides it
        // by the size.
        "transform-origin" => {
            let (w, h) = crate::render::bounds_of(id).map_or((0.0, 0.0), |(_, _, w, h)| (w, h));
            let at = |len: gpui::TransformLength, size: f32| match len {
                gpui::TransformLength::Pixels(px) => px,
                gpui::TransformLength::Fraction(f) => f * size,
            };
            let (ox, oy) = cs.base.vn_transform.map_or(
                (
                    gpui::TransformLength::Fraction(0.5),
                    gpui::TransformLength::Fraction(0.5),
                ),
                |t| t.origin,
            );
            format!("{}px {}px", at(ox, w), at(oy, h))
        }
        "zoom" => "1".into(),
        // The individual transform properties are not mapped (`transform` is the only one that
        // applies), plus three not implemented.
        "translate" | "rotate" | "scale" | "perspective" | "filter" => "none".into(),
        "will-change" => "auto".into(),
        "contain" => "none".into(),
        "container-type" => match cs.extras.container_type.unwrap_or(0) {
            0 => "normal",
            sk_css::container::INLINE => "inline-size",
            _ => "size",
        }
        .into(),
        "container-name" => match cs.extras.container_name.clone().flatten() {
            Some(names) => names.join(" "),
            None => "none".into(),
        },
        "backdrop-filter" => match cs.base.backdrop_blur {
            Some(r) => format!("blur({}px)", f32::from(r)),
            None => "none".into(),
        },
        // reka-ui's `Presence` reads `animationName` to decide whether to wait for `animationend`, so
        // this must be the truth or an exit animation never plays or never finishes.
        "animation-name" if cs.animations.is_empty() => "none".into(),
        "animation-duration" | "animation-delay" if cs.animations.is_empty() => "0s".into(),
        "animation-fill-mode" if cs.animations.is_empty() => "none".into(),
        "animation-name" => animations(&|s| {
            if s.name.is_empty() {
                "none".into()
            } else {
                s.name.to_string()
            }
        }),
        "animation-duration" => animations(&|s| secs(s.duration)),
        "animation-delay" => animations(&|s| signed_secs(s.delay)),
        "animation-fill-mode" => animations(&|s| {
            match s.fill {
                sk_css::FillMode::None => "none",
                sk_css::FillMode::Forwards => "forwards",
                sk_css::FillMode::Backwards => "backwards",
                sk_css::FillMode::Both => "both",
            }
            .to_string()
        }),
        "transition-property" if cs.transitions.is_empty() => "all".into(),
        "transition-duration" | "transition-delay" if cs.transitions.is_empty() => "0s".into(),
        "transition-property" => transitions(&|s| s.property.name().to_string()),
        "transition-duration" => transitions(&|s| secs(s.duration)),
        "transition-delay" => transitions(&|s| signed_secs(s.delay)),
        "width" | "height" => {
            let (w, h) = crate::render::bounds_of(id)
                .map(|(_, _, w, h)| (w, h))
                .unwrap_or((0.0, 0.0));
            format!("{}px", if name == "width" { w } else { h })
        }
        _ if edge_of(name).is_some() => edge_px(t, id, cs, name)?,
        "direction" => "ltr".into(),
        "interpolate-size" => {
            if crate::inherit::interpolate_keywords(t, id, cs.extras.interpolate_size) {
                "allow-keywords"
            } else {
                "numeric-only"
            }
        }
        .into(),
        "dynamic-range-limit" => crate::inherit::dynamic_range_limit_in(t, id).to_css(),
        "font-size" => {
            let mut px = None;
            let mut at = id;
            while let Some(n) = t.node(at) {
                if let Some(c) = n.computed.as_ref() {
                    px = Some(c.font_size);
                    break;
                }
                if n.parent == 0 || n.parent == at {
                    break;
                }
                at = n.parent;
            }
            format!("{}px", px.unwrap_or_else(sk_css::rem_basis))
        }
        "color" => {
            let mut color = None;
            let mut at = id;
            while let Some(n) = t.node(at) {
                if let Some(c) = n.computed.as_ref() {
                    color = color.or(c.base.text.color);
                }
                if color.is_some() || n.parent == 0 || n.parent == at {
                    break;
                }
                at = n.parent;
            }
            css_rgb(color.unwrap_or_else(gpui::black))
        }
        "color-scheme" => cs
            .vars
            .get(sk_css::COLOR_SCHEME)
            .unwrap_or("normal")
            .to_string(),
        "aspect-ratio" => match cs.base.aspect_ratio {
            Some(r) if r > 0.0 => format!("{r} / 1"),
            Some(r) if r < 0.0 => format!("auto {} / 1", -r),
            _ => "auto".into(),
        },
        "corner-top-left-shape"
        | "corner-top-right-shape"
        | "corner-bottom-right-shape"
        | "corner-bottom-left-shape" => {
            let s = &cs.base.vn_corner_shapes;
            let shape = match name {
                "corner-top-left-shape" => s.top_left,
                "corner-top-right-shape" => s.top_right,
                "corner-bottom-right-shape" => s.bottom_right,
                _ => s.bottom_left,
            };
            sk_css::corner_shape_css(shape.unwrap_or_default())
        }
        "flex-direction" => match cs.base.flex_direction.unwrap_or_default() {
            gpui::FlexDirection::Row => "row",
            gpui::FlexDirection::RowReverse => "row-reverse",
            gpui::FlexDirection::Column => "column",
            gpui::FlexDirection::ColumnReverse => "column-reverse",
        }
        .into(),
        "flex-wrap" => match cs.base.flex_wrap.unwrap_or_default() {
            gpui::FlexWrap::NoWrap => "nowrap",
            gpui::FlexWrap::Wrap => "wrap",
            gpui::FlexWrap::WrapReverse => "wrap-reverse",
        }
        .into(),
        "cursor" => cursor_keyword(t, id).into(),
        "text-shadow" => {
            let (mut shadows, mut color) = (None, None);
            let mut at = id;
            while let Some(n) = t.node(at) {
                if let Some(c) = n.computed.as_ref() {
                    shadows = shadows.or_else(|| c.base.text.text_shadow.clone());
                    color = color.or(c.base.text.color);
                }
                if (shadows.is_some() && color.is_some()) || n.parent == 0 || n.parent == at {
                    break;
                }
                at = n.parent;
            }
            sk_css::text_shadow_css(
                shadows.as_deref().unwrap_or_default(),
                color.unwrap_or_else(gpui::black),
            )
        }
        _ => return None,
    })
}

/// `cursor` inherits; the keyword the nearest declaration mapped to.
fn cursor_keyword(t: &Tree, id: u32) -> &'static str {
    use gpui::CursorStyle as C;
    let mut at = id;
    let mut found = None;
    while let Some(n) = t.node(at) {
        found = n.computed.as_ref().and_then(|c| c.base.mouse_cursor);
        if found.is_some() || n.parent == 0 || n.parent == at {
            break;
        }
        at = n.parent;
    }
    match found {
        None => "auto",
        Some(C::Arrow) => "default",
        Some(C::IBeam) => "text",
        Some(C::Crosshair) => "crosshair",
        Some(C::ClosedHand) => "grabbing",
        Some(C::OpenHand) => "grab",
        Some(C::PointingHand) => "pointer",
        Some(C::ResizeLeft) => "w-resize",
        Some(C::ResizeRight) => "e-resize",
        Some(C::ResizeLeftRight) => "ew-resize",
        Some(C::ResizeUp) => "n-resize",
        Some(C::ResizeDown) => "s-resize",
        Some(C::ResizeUpDown) => "ns-resize",
        Some(C::ResizeUpLeftDownRight) => "nwse-resize",
        Some(C::ResizeUpRightDownLeft) => "nesw-resize",
        Some(C::ResizeColumn) => "col-resize",
        Some(C::ResizeRow) => "row-resize",
        Some(C::IBeamCursorForVerticalLayout) => "vertical-text",
        Some(C::OperationNotAllowed) => "not-allowed",
        Some(C::DragLink) => "alias",
        Some(C::DragCopy) => "copy",
        Some(C::ContextualMenu) => "context-menu",
        Some(C::Help) => "help",
        Some(C::Progress) => "progress",
        Some(C::Wait) => "wait",
        Some(C::Move) => "move",
        Some(C::AllScroll) => "all-scroll",
        Some(C::Cell) => "cell",
        Some(C::ZoomIn) => "zoom-in",
        Some(C::ZoomOut) => "zoom-out",
        Some(C::NoDrop) => "no-drop",
        Some(C::Hidden) => "none",
    }
}

#[derive(Clone, Copy)]
enum EdgeBox {
    Padding,
    Margin,
    Border,
}

fn edge_of(name: &str) -> Option<(EdgeBox, &str)> {
    if let Some(side) = name.strip_prefix("padding-") {
        return Some((EdgeBox::Padding, side));
    }
    if let Some(side) = name.strip_prefix("margin-") {
        return Some((EdgeBox::Margin, side));
    }
    let side = name.strip_prefix("border-")?.strip_suffix("-width")?;
    Some((EdgeBox::Border, side))
}

fn side_of<T: Clone + std::fmt::Debug + Default + PartialEq>(
    edges: &gpui::EdgesRefinement<T>,
    side: &str,
) -> Option<T> {
    match side {
        "top" => edges.top.clone(),
        "right" => edges.right.clone(),
        "bottom" => edges.bottom.clone(),
        "left" => edges.left.clone(),
        _ => None,
    }
}

/// A padding, margin or border-width longhand as a browser resolves it: px, a percentage of the parent's width
/// (every side, as CSS has it), `auto` margins as 0.
fn edge_px(t: &Tree, id: u32, cs: &sk_css::ComputedStates, name: &str) -> Option<String> {
    let (which, side) = edge_of(name)?;
    let rem = gpui::px(sk_css::rem_basis());
    let parent_width = || {
        let parent = t.node(id).map(|n| n.parent).unwrap_or(0);
        let w = crate::render::bounds_of(parent).map_or(0.0, |(_, _, w, _)| w);
        gpui::AbsoluteLength::Pixels(gpui::px(w))
    };
    let px = match which {
        EdgeBox::Padding => side_of(&cs.base.padding, side)
            .map_or(gpui::px(0.0), |l| l.to_pixels(parent_width(), rem)),
        EdgeBox::Margin => match side_of(&cs.base.margin, side) {
            Some(gpui::Length::Definite(l)) => l.to_pixels(parent_width(), rem),
            Some(gpui::Length::Auto | gpui::Length::VnCalcSize(_)) | None => gpui::px(0.0),
        },
        EdgeBox::Border => {
            side_of(&cs.base.border_widths, side).map_or(gpui::px(0.0), |l| l.to_pixels(rem))
        }
    };
    Some(format!("{}px", f32::from(px)))
}

fn secs(d: std::time::Duration) -> String {
    format!("{}s", d.as_secs_f32())
}

fn signed_secs(d: sk_css::Delay) -> String {
    format!("{}s", d.as_secs_f64() as f32)
}

fn css_rgb(c: gpui::Hsla) -> String {
    let c = gpui::Rgba::from(c);
    let ch = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
    let (r, g, b) = (ch(c.r), ch(c.g), ch(c.b));
    if c.a >= 1.0 {
        format!("rgb({r}, {g}, {b})")
    } else {
        let a = (c.a.clamp(0.0, 1.0) * 100.0).round() / 100.0;
        format!("rgba({r}, {g}, {b}, {a})")
    }
}

fn transform_text(t: Option<gpui::TransformSpec>) -> String {
    t.map(sk_css::transform_css)
        .unwrap_or_else(|| "none".into())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn prop(id: u32, name: &str) -> String {
        let head = format!("{name}=");
        computed_style(id)
            .unwrap_or_default()
            .lines()
            .find_map(|l| l.strip_prefix(head.as_str()).map(str::to_string))
            .unwrap_or_default()
    }

    #[test]
    fn document_queries_include_the_document_element_but_element_queries_exclude_self() {
        let (root, child) = tree::with_tree(|tree| {
            let root = tree.create_root();
            tree.set_class(root, "match");
            let tag = tree.intern("div");
            let child = tree.create_element(tag);
            tree.set_class(child, "match");
            tree.insert(child, root, 0);
            (root, child)
        });
        assert_eq!(
            query(".match", root, QueryMode::Document).unwrap(),
            vec![root, child]
        );
        assert_eq!(
            query(".match", root, QueryMode::Descendants).unwrap(),
            vec![child]
        );
        assert_eq!(
            query(":root", root, QueryMode::Document).unwrap(),
            vec![root]
        );
        assert!(query(":root", root, QueryMode::Descendants)
            .unwrap()
            .is_empty());
    }

    #[test]
    fn computed_style_reports_the_inherited_font_size_and_color() {
        let (outer, inner, bare) = tree::with_tree(|t| {
            let root = t.create_root();
            let div = t.intern("div");
            let outer = t.create_element(div);
            t.insert(outer, root, 0);
            t.set_class(outer, "outer");
            let inner = t.create_element(div);
            t.insert(inner, outer, 0);
            let bare = t.create_element(div);
            t.insert(bare, root, 1);
            (outer, inner, bare)
        });
        tree::set_engine(sk_css::Engine::compile(
            ".outer { font-size: 20px; color: rgba(255, 0, 0, 0.5) }",
        ));
        tree::restyle_all();
        assert_eq!(prop(outer, "font-size"), "20px");
        assert_eq!(prop(inner, "font-size"), "20px", "inherited from .outer");
        assert_eq!(prop(inner, "color"), "rgba(255, 0, 0, 0.5)");
        assert_eq!(
            prop(bare, "font-size"),
            format!("{}px", sk_css::rem_basis())
        );
        assert_eq!(prop(bare, "color"), "rgb(0, 0, 0)");
        for name in [
            "width",
            "height",
            "position",
            "direction",
            "animation-name",
            "transition-delay",
            "transition-duration",
        ] {
            assert!(!prop(inner, name).is_empty(), "{name}");
        }
    }

    #[test]
    fn computed_animation_none_cancels_presence_and_preserves_mixed_list_slots() {
        let (single, mixed) = tree::with_tree(|t| {
            let root = t.create_root();
            let div = t.intern("div");
            let single = t.create_element(div);
            t.set_class(single, "single");
            t.insert(single, root, 0);
            let mixed = t.create_element(div);
            t.set_class(mixed, "mixed");
            t.insert(mixed, root, 1);
            (single, mixed)
        });
        tree::set_engine(sk_css::Engine::compile(
            "@keyframes spin { from { opacity: 0 } to { opacity: 1 } } \
             @keyframes fade { from { opacity: 1 } to { opacity: 0 } } \
             .single { animation-name: spin; animation-duration: 250ms; animation-fill-mode: forwards } \
             .mixed { animation-name: none, spin, none, fade; animation-duration: 100ms, 300ms; animation-delay: 20ms }",
        ));
        tree::restyle_all();
        assert_eq!(prop(single, "animation-name"), "spin");
        assert_eq!(prop(mixed, "animation-name"), "none, spin, none, fade");
        assert_eq!(prop(mixed, "animation-duration"), "0.1s, 0.3s, 0.1s, 0.3s");
        assert_eq!(prop(mixed, "animation-delay"), "0.02s, 0.02s, 0.02s, 0.02s");

        // The none slot keeps the other components, but must serialize the literal keyword so it
        // does not wait for animationend from an inert animation.
        tree::with_tree(|t| {
            let name = t.intern("animation-name");
            t.set_style(single, name, "none".into());
        });
        tree::restyle_all();
        assert_eq!(prop(single, "animation-name"), "none");
        assert_eq!(prop(single, "animation-duration"), "0.25s");
        assert_eq!(prop(single, "animation-fill-mode"), "forwards");
    }

    #[test]
    fn computed_box_edges_resolve_to_px() {
        let (boxed, bare) = tree::with_tree(|t| {
            let root = t.create_root();
            let div = t.intern("div");
            let boxed = t.create_element(div);
            t.set_class(boxed, "boxed");
            t.insert(boxed, root, 0);
            let bare = t.create_element(div);
            t.insert(bare, root, 1);
            (boxed, bare)
        });
        tree::set_engine(sk_css::Engine::compile(
            ".boxed { padding: 4px 1rem; margin: 3px auto 0; border-top: 2px solid red }",
        ));
        tree::restyle_all();
        let rem = format!("{}px", sk_css::rem_basis());
        assert_eq!(prop(boxed, "padding-top"), "4px");
        assert_eq!(prop(boxed, "padding-left"), rem);
        assert_eq!(prop(boxed, "margin-top"), "3px");
        assert_eq!(prop(boxed, "margin-right"), "0px", "auto");
        assert_eq!(prop(boxed, "border-top-width"), "2px");
        assert_eq!(prop(boxed, "border-bottom-width"), "0px");
        for name in ["padding-bottom", "margin-left", "border-left-width"] {
            assert_eq!(prop(bare, name), "0px", "{name}");
        }
    }

    #[test]
    fn computed_overflow_pairs_the_axes_and_reports_auto_and_clip() {
        let (list, wide, cut, plain) = tree::with_tree(|t| {
            let root = t.create_root();
            let div = t.intern("div");
            let mut make = |class: &str, at: u32| {
                let n = t.create_element(div);
                t.insert(n, root, at);
                if !class.is_empty() {
                    t.set_class(n, class);
                }
                n
            };
            (
                make("list", 0),
                make("wide", 1),
                make("cut", 2),
                make("", 3),
            )
        });
        tree::set_engine(sk_css::Engine::compile(
            ".list { overflow-y: auto } .wide { overflow-x: hidden } .cut { overflow-x: clip }",
        ));
        tree::restyle_all();
        let axes = |id| {
            [
                prop(id, "overflow-x"),
                prop(id, "overflow-y"),
                prop(id, "overflow"),
            ]
        };
        assert_eq!(axes(list), ["auto", "auto", "auto"]);
        assert_eq!(axes(wide), ["hidden", "auto", "hidden auto"]);
        assert_eq!(axes(cut), ["clip", "visible", "clip visible"]);
        assert_eq!(axes(plain), ["visible", "visible", "visible"]);
        let x = tree::with_tree(|t| {
            t.node(list)
                .and_then(|n| n.computed.as_ref())
                .map(|cs| cs.base.overflow.x)
        });
        assert_eq!(x, Some(Some(gpui::Overflow::Scroll)));
    }
}