1
//! Chip / tag widget — a compact rounded "pill" holding a short label plus an
2
//! optional removable "x" affordance. A blend of
3
//! [`crate::widgets::badge::Badge`] (the coloured pill visual + [`ChipKind`]
4
//! colour variants) and [`crate::widgets::alert::Alert`] (the dismiss pattern:
5
//! a stateful close affordance that hides the widget on click).
6
//!
7
//! When made removable (`with_removable(true)` or `set_on_remove`), the chip
8
//! mirrors the stateful pattern of [`crate::widgets::alert::Alert`]: it carries a
9
//! [`ChipStateWrapper`] (`{ visible } + on_remove`) in a [`RefAny`] attached to
10
//! the "x" node. Clicking "x" flips `visible` to `false`, invokes the optional
11
//! user `on_remove`, and hides the whole chip by setting `display: none` on the
12
//! container via `set_css_property` (mirroring alert's live restyle). A
13
//! non-removable chip renders no "x" and carries no live callback — it is then
14
//! just a stateless styled pill (a near-clone of [`Badge`]).
15
//!
16
//! Key types: [`Chip`], [`ChipKind`], [`ChipState`], [`ChipOnRemove`],
17
//! [`ChipOnClick`].
18

            
19
use azul_core::{
20
    callbacks::{CoreCallbackData, Update},
21
    dom::{Dom, IdOrClass, IdOrClass::Class, IdOrClassVec, TabIndex},
22
    refany::RefAny,
23
};
24
use azul_css::{
25
    dynamic_selector::{
26
        CssPropertyWithConditions, CssPropertyWithConditionsVec, OptionCssPropertyWithConditionsVec,
27
    },
28
    impl_option_inner,
29
    props::{
30
        basic::{
31
            color::ColorU,
32
            font::{StyleFontFamily, StyleFontFamilyVec},
33
            StyleFontSize,
34
        },
35
        layout::{
36
            LayoutAlignItems, LayoutAlignSelf, LayoutDisplay, LayoutFlexDirection, LayoutFlexGrow,
37
            LayoutMarginLeft, LayoutPaddingBottom, LayoutPaddingLeft, LayoutPaddingRight,
38
            LayoutPaddingTop,
39
        },
40
        property::{CssProperty, *},
41
        style::{
42
            StyleBackgroundContent, StyleBackgroundContentVec, StyleBorderBottomLeftRadius,
43
            StyleBorderBottomRightRadius, StyleBorderTopLeftRadius, StyleBorderTopRightRadius,
44
            StyleCursor, StyleTextAlign, StyleTextColor, StyleUserSelect,
45
        },
46
    },
47
    AzString,
48
};
49

            
50
use crate::callbacks::{Callback, CallbackInfo};
51

            
52
static CHIP_CONTAINER_CLASS: &[IdOrClass] =
53
    &[Class(AzString::from_const_str("__azul-native-chip"))];
54
static CHIP_LABEL_CLASS: &[IdOrClass] =
55
    &[Class(AzString::from_const_str("__azul-native-chip-label"))];
56
static CHIP_REMOVE_CLASS: &[IdOrClass] =
57
    &[Class(AzString::from_const_str("__azul-native-chip-remove"))];
58

            
59
const SYSTEM_UI_STR: AzString = AzString::from_const_str("system:ui");
60
const SYSTEM_UI_FAMILIES: &[StyleFontFamily] = &[StyleFontFamily::System(SYSTEM_UI_STR)];
61
const SYSTEM_UI_FAMILY: StyleFontFamilyVec =
62
    StyleFontFamilyVec::from_const_slice(SYSTEM_UI_FAMILIES);
63

            
64
/// Callback function type invoked when a removable chip's "x" is clicked.
65
pub type ChipOnRemoveCallbackType = extern "C" fn(RefAny, CallbackInfo, ChipState) -> Update;
66
impl_widget_callback!(
67
    ChipOnRemove,
68
    OptionChipOnRemove,
69
    ChipOnRemoveCallback,
70
    ChipOnRemoveCallbackType
71
);
72

            
73
azul_core::impl_managed_callback! {
74
    wrapper:        ChipOnRemoveCallback,
75
    info_ty:        CallbackInfo,
76
    return_ty:      Update,
77
    default_ret:    Update::DoNothing,
78
    invoker_static: CHIP_ON_REMOVE_INVOKER,
79
    invoker_ty:     AzChipOnRemoveCallbackInvoker,
80
    thunk_fn:       az_chip_on_remove_callback_thunk,
81
    setter_fn:      AzApp_setChipOnRemoveCallbackInvoker,
82
    from_handle_fn: AzChipOnRemoveCallback_createFromHostHandle,
83
    extra_args:     [ state: ChipState ],
84
}
85

            
86
/// Callback function type invoked when the chip's label area is clicked.
87
pub type ChipOnClickCallbackType = extern "C" fn(RefAny, CallbackInfo, ChipState) -> Update;
88
impl_widget_callback!(
89
    ChipOnClick,
90
    OptionChipOnClick,
91
    ChipOnClickCallback,
92
    ChipOnClickCallbackType
93
);
94

            
95
azul_core::impl_managed_callback! {
96
    wrapper:        ChipOnClickCallback,
97
    info_ty:        CallbackInfo,
98
    return_ty:      Update,
99
    default_ret:    Update::DoNothing,
100
    invoker_static: CHIP_ON_CLICK_INVOKER,
101
    invoker_ty:     AzChipOnClickCallbackInvoker,
102
    thunk_fn:       az_chip_on_click_callback_thunk,
103
    setter_fn:      AzApp_setChipOnClickCallbackInvoker,
104
    from_handle_fn: AzChipOnClickCallback_createFromHostHandle,
105
    extra_args:     [ state: ChipState ],
106
}
107

            
108
/// The semantic colour variant of a [`Chip`] (mirrors `badge::BadgeKind`).
109
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
110
#[repr(C)]
111
pub enum ChipKind {
112
    /// Neutral light-grey chip — the default.
113
    #[default]
114
    Default,
115
    /// Blue "primary" chip.
116
    Primary,
117
    /// Green "success" chip.
118
    Success,
119
    /// Red "danger" chip.
120
    Danger,
121
    /// Yellow "warning" chip (uses dark text).
122
    Warning,
123
    /// Cyan "info" chip (uses dark text).
124
    Info,
125
}
126

            
127
impl ChipKind {
128
    /// Returns the `(background, text)` colours for this chip kind.
129
    #[allow(clippy::trivially_copy_pass_by_ref)] // <=8B Copy param kept by-ref intentionally (hot
130
                                                 // pixel/coord path or to avoid churning call sites
131
                                                 // for a perf-neutral change)
132
928
    const fn colors(&self) -> (ColorU, ColorU) {
133
        const WHITE: ColorU = ColorU {
134
            r: 255,
135
            g: 255,
136
            b: 255,
137
            a: 255,
138
        };
139
        const DARK: ColorU = ColorU {
140
            r: 33,
141
            g: 37,
142
            b: 41,
143
            a: 255,
144
        };
145
928
        match self {
146
            // The default chip is a light neutral pill with dark text (the
147
            // common "tag" look), unlike Badge's solid grey.
148
226
            Self::Default => (
149
226
                ColorU {
150
226
                    r: 233,
151
226
                    g: 236,
152
226
                    b: 239,
153
226
                    a: 255,
154
226
                },
155
226
                DARK,
156
226
            ),
157
144
            Self::Primary => (
158
144
                ColorU {
159
144
                    r: 13,
160
144
                    g: 110,
161
144
                    b: 253,
162
144
                    a: 255,
163
144
                },
164
144
                WHITE,
165
144
            ),
166
137
            Self::Success => (
167
137
                ColorU {
168
137
                    r: 25,
169
137
                    g: 135,
170
137
                    b: 84,
171
137
                    a: 255,
172
137
                },
173
137
                WHITE,
174
137
            ),
175
141
            Self::Danger => (
176
141
                ColorU {
177
141
                    r: 220,
178
141
                    g: 53,
179
141
                    b: 69,
180
141
                    a: 255,
181
141
                },
182
141
                WHITE,
183
141
            ),
184
139
            Self::Warning => (
185
139
                ColorU {
186
139
                    r: 255,
187
139
                    g: 193,
188
139
                    b: 7,
189
139
                    a: 255,
190
139
                },
191
139
                DARK,
192
139
            ),
193
141
            Self::Info => (
194
141
                ColorU {
195
141
                    r: 13,
196
141
                    g: 202,
197
141
                    b: 240,
198
141
                    a: 255,
199
141
                },
200
141
                DARK,
201
141
            ),
202
        }
203
928
    }
204

            
205
    /// CSS class name for this chip kind (mirrors `BadgeKind::class_name`).
206
    #[must_use]
207
31
    pub const fn class_name(&self) -> &'static str {
208
31
        match self {
209
6
            Self::Default => "__azul-chip-default",
210
5
            Self::Primary => "__azul-chip-primary",
211
5
            Self::Success => "__azul-chip-success",
212
5
            Self::Danger => "__azul-chip-danger",
213
5
            Self::Warning => "__azul-chip-warning",
214
5
            Self::Info => "__azul-chip-info",
215
        }
216
31
    }
217
}
218

            
219
/// A compact rounded pill holding a label plus an optional removable "x".
220
#[derive(Debug, Clone, PartialEq, Eq)]
221
#[repr(C)]
222
pub struct Chip {
223
    /// Runtime state (`visible`) plus the optional remove callback.
224
    pub chip_state: ChipStateWrapper,
225
    /// The text shown inside the pill.
226
    pub label: AzString,
227
    /// The colour variant.
228
    pub kind: ChipKind,
229
    /// Whether to render the "x" remove affordance (hides the chip on click).
230
    pub removable: bool,
231
    /// The computed inline style for the pill container.
232
    /// The container's CSS, or `None` for "no opinion" — in which case the style
233
    /// is derived from `kind` at render time.
234
    ///
235
    /// `None` and `Some(empty)` are different answers: the first means the
236
    /// widget picks, the second means the caller asked for no properties at all
237
    /// and gets none.
238
    pub container_style: OptionCssPropertyWithConditionsVec,
239
}
240

            
241
#[derive(Debug, Default, Clone, PartialEq, Eq)]
242
#[repr(C)]
243
pub struct ChipStateWrapper {
244
    /// Whether the chip is currently visible.
245
    pub inner: ChipState,
246
    /// Optional: function to call when the chip is removed.
247
    pub on_remove: OptionChipOnRemove,
248
    /// Optional: function to call when the chip's label area is clicked.
249
    pub on_click: OptionChipOnClick,
250
}
251

            
252
/// The visible/hidden state of a [`Chip`].
253
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
254
#[repr(C)]
255
pub struct ChipState {
256
    /// `true` (default) = shown, `false` = removed/hidden.
257
    pub visible: bool,
258
}
259

            
260
impl Default for ChipState {
261
257
    fn default() -> Self {
262
257
        Self { visible: true }
263
257
    }
264
}
265

            
266
/// Builds the pill container style for a given [`ChipKind`]. The colours are the
267
/// only kind-dependent properties, so the style is built at runtime per the
268
/// recipe's "runtime vec when param-dependent" path (see `badge::build_badge_style`).
269
700
fn build_chip_style(kind: ChipKind) -> CssPropertyWithConditionsVec {
270
700
    let (bg, text) = kind.colors();
271
700
    let bg_vec =
272
700
        StyleBackgroundContentVec::from_vec(alloc::vec![StyleBackgroundContent::Color(bg)]);
273
700
    CssPropertyWithConditionsVec::from_vec(alloc::vec![
274
700
        CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::Flex)),
275
700
        CssPropertyWithConditions::simple(CssProperty::const_flex_direction(
276
700
            LayoutFlexDirection::Row,
277
        )),
278
700
        CssPropertyWithConditions::simple(CssProperty::const_align_items(LayoutAlignItems::Center)),
279
        // Hug the content rather than stretch across a flex parent's cross axis.
280
700
        CssPropertyWithConditions::simple(CssProperty::align_self(LayoutAlignSelf::Start)),
281
700
        CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(
282
            0,
283
        ))),
284
        // padding: 4px 10px
285
700
        CssPropertyWithConditions::simple(CssProperty::const_padding_top(
286
700
            LayoutPaddingTop::const_px(4,)
287
        )),
288
700
        CssPropertyWithConditions::simple(CssProperty::const_padding_bottom(
289
700
            LayoutPaddingBottom::const_px(4),
290
        )),
291
700
        CssPropertyWithConditions::simple(CssProperty::const_padding_left(
292
700
            LayoutPaddingLeft::const_px(10),
293
        )),
294
700
        CssPropertyWithConditions::simple(CssProperty::const_padding_right(
295
700
            LayoutPaddingRight::const_px(10),
296
        )),
297
        // border-radius: 12px (pill)
298
700
        CssPropertyWithConditions::simple(CssProperty::const_border_top_left_radius(
299
700
            StyleBorderTopLeftRadius::const_px(12),
300
        )),
301
700
        CssPropertyWithConditions::simple(CssProperty::const_border_top_right_radius(
302
700
            StyleBorderTopRightRadius::const_px(12),
303
        )),
304
700
        CssPropertyWithConditions::simple(CssProperty::const_border_bottom_left_radius(
305
700
            StyleBorderBottomLeftRadius::const_px(12),
306
        )),
307
700
        CssPropertyWithConditions::simple(CssProperty::const_border_bottom_right_radius(
308
700
            StyleBorderBottomRightRadius::const_px(12),
309
        )),
310
700
        CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(
311
            13
312
        ))),
313
700
        CssPropertyWithConditions::simple(CssProperty::const_font_family(SYSTEM_UI_FAMILY)),
314
        // Text colour is inherited by the label + "x" children.
315
700
        CssPropertyWithConditions::simple(CssProperty::const_text_color(StyleTextColor {
316
700
            inner: text,
317
700
        })),
318
700
        CssPropertyWithConditions::simple(CssProperty::const_background_content(bg_vec)),
319
    ])
320
700
}
321

            
322
/// Label style: left-aligned, hugs its content.
323
static CHIP_LABEL_STYLE: &[CssPropertyWithConditions] = &[
324
    CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(0))),
325
    CssPropertyWithConditions::simple(CssProperty::const_text_align(StyleTextAlign::Left)),
326
    CssPropertyWithConditions::simple(CssProperty::user_select(StyleUserSelect::None)),
327
];
328

            
329
/// "x" remove-affordance style: a small pointer-cursor box on the right.
330
static CHIP_REMOVE_STYLE: &[CssPropertyWithConditions] = &[
331
    CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(0))),
332
    CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(14))),
333
    CssPropertyWithConditions::simple(CssProperty::const_cursor(StyleCursor::Pointer)),
334
    CssPropertyWithConditions::simple(CssProperty::user_select(StyleUserSelect::None)),
335
    CssPropertyWithConditions::simple(CssProperty::const_margin_left(LayoutMarginLeft::const_px(
336
        6,
337
    ))),
338
];
339

            
340
impl Chip {
341
    /// Creates a new chip with the given label and the default (light-grey) kind.
342
    #[inline]
343
    #[must_use]
344
138
    pub fn create(label: AzString) -> Self {
345
138
        Self::with_kind(label, ChipKind::Default)
346
138
    }
347

            
348
    /// Creates a new chip with the given label and colour variant.
349
    #[inline]
350
    #[must_use]
351
251
    pub fn with_kind(label: AzString, kind: ChipKind) -> Self {
352
251
        Self {
353
251
            chip_state: ChipStateWrapper::default(),
354
251
            label,
355
251
            kind,
356
251
            removable: false,
357
251
            container_style: OptionCssPropertyWithConditionsVec::None,
358
251
        }
359
251
    }
360

            
361
    /// The container CSS this chip renders with.
362
    ///
363
    /// `None` means no opinion, so the kind's default applies — the same answer
364
    /// both themes give, asked in one place so they cannot drift.
365
    #[must_use]
366
415
    pub fn resolved_container_style(&self) -> CssPropertyWithConditionsVec {
367
415
        self.container_style
368
415
            .clone()
369
415
            .into_option()
370
415
            .unwrap_or_else(|| build_chip_style(self.kind))
371
415
    }
372

            
373
    /// Sets the colour variant.
374
    ///
375
    /// Does not touch `container_style`: the style is resolved from `kind` when
376
    /// the DOM is built, so changing the kind is enough. Writing it here would
377
    /// also overwrite a caller's explicit style.
378
    #[inline]
379
82
    pub const fn set_kind(&mut self, kind: ChipKind) {
380
82
        self.kind = kind;
381
82
    }
382

            
383
    /// Builder-style setter for the colour variant.
384
    #[inline]
385
    #[must_use]
386
9
    pub const fn with_chip_kind(mut self, kind: ChipKind) -> Self {
387
9
        self.set_kind(kind);
388
9
        self
389
9
    }
390

            
391
    /// Sets whether the chip shows a "x" remove affordance.
392
    #[inline]
393
73
    pub const fn set_removable(&mut self, removable: bool) {
394
73
        self.removable = removable;
395
73
    }
396

            
397
    /// Builder-style setter for the removable flag.
398
    #[inline]
399
    #[must_use]
400
65
    pub const fn with_removable(mut self, removable: bool) -> Self {
401
65
        self.set_removable(removable);
402
65
        self
403
65
    }
404

            
405
    /// Sets the remove callback. Implies `removable = true` so the "x" is rendered.
406
    #[inline]
407
9
    pub fn set_on_remove<C: Into<ChipOnRemoveCallback>>(&mut self, data: RefAny, on_remove: C) {
408
9
        self.removable = true;
409
9
        self.chip_state.on_remove = Some(ChipOnRemove {
410
9
            callback: on_remove.into(),
411
9
            refany: data,
412
9
        })
413
9
        .into();
414
9
    }
415

            
416
    /// Builder-style setter for the remove callback (implies removable).
417
    #[inline]
418
    #[must_use]
419
4
    pub fn with_on_remove<C: Into<ChipOnRemoveCallback>>(
420
4
        mut self,
421
4
        data: RefAny,
422
4
        on_remove: C,
423
4
    ) -> Self {
424
4
        self.set_on_remove(data, on_remove);
425
4
        self
426
4
    }
427

            
428
    /// Sets the click callback, invoked when the chip's label area is clicked.
429
    #[inline]
430
15
    pub fn set_on_click<C: Into<ChipOnClickCallback>>(&mut self, data: RefAny, on_click: C) {
431
15
        self.chip_state.on_click = Some(ChipOnClick {
432
15
            callback: on_click.into(),
433
15
            refany: data,
434
15
        })
435
15
        .into();
436
15
    }
437

            
438
    /// Builder-style setter for the click callback.
439
    #[inline]
440
    #[must_use]
441
11
    pub fn with_on_click<C: Into<ChipOnClickCallback>>(
442
11
        mut self,
443
11
        data: RefAny,
444
11
        on_click: C,
445
11
    ) -> Self {
446
11
        self.set_on_click(data, on_click);
447
11
        self
448
11
    }
449

            
450
    /// Replaces `self` with an empty default chip and returns the original.
451
    #[inline]
452
    #[must_use]
453
16
    pub fn swap_with_default(&mut self) -> Self {
454
16
        let mut s = Self::create(AzString::from_const_str(""));
455
16
        core::mem::swap(&mut s, self);
456
16
        s
457
16
    }
458

            
459
    /// Converts this chip into a DOM subtree with the `__azul-native-chip` class.
460
    #[inline]
461
    #[must_use]
462
80
    pub fn dom(self) -> Dom {
463
        use azul_core::{
464
            callbacks::CoreCallback,
465
            dom::{EventFilter, HoverEventFilter},
466
            refany::OptionRefAny,
467
        };
468

            
469
        // Resolved before `self.chip_state` is moved out below.
470
80
        let container_style = self.resolved_container_style();
471

            
472
80
        let has_on_click = matches!(self.chip_state.on_click, OptionChipOnClick::Some(_));
473

            
474
        // The remove ("x") and the label-click callbacks share the same state
475
        // RefAny so both handlers observe the same ChipState.
476
80
        let state_ref = RefAny::new(self.chip_state);
477

            
478
80
        let mut label = crate::widgets::widget_p_with_text(self.label)
479
80
            .with_ids_and_classes(IdOrClassVec::from_const_slice(CHIP_LABEL_CLASS))
480
80
            .with_css_props(CssPropertyWithConditionsVec::from_const_slice(
481
80
                CHIP_LABEL_STYLE,
482
            ));
483

            
484
        // The click callback is attached to the LABEL node rather than the
485
        // pill container: a container-level MouseUp would also fire when the
486
        // remove "x" (a child of the container) is clicked, double-firing
487
        // alongside on_remove. Attaching per-child sidesteps that (same
488
        // wiring as list_view's row/column callbacks); clicks on the pill's
489
        // padding therefore do not trigger on_click.
490
80
        if has_on_click {
491
6
            // A clickable chip is a compact button.
492
6
            label = label
493
6
                .with_tab_index(TabIndex::Auto)
494
6
                .with_accessibility_info(azul_core::a11y::AccessibilityInfo {
495
6
                    role: azul_core::a11y::AccessibilityRole::PushButton,
496
6
                    ..Default::default()
497
6
                })
498
6
                .with_callbacks(
499
6
                    alloc::vec![CoreCallbackData {
500
6
                        event: EventFilter::Hover(HoverEventFilter::Click),
501
6
                        callback: CoreCallback {
502
6
                            cb: default_on_chip_click as usize,
503
6
                            ctx: OptionRefAny::None,
504
6
                        },
505
6
                        refany: state_ref.clone(),
506
6
                    }]
507
6
                    .into(),
508
6
                );
509
74
        }
510

            
511
80
        let mut children = alloc::vec![label];
512

            
513
80
        if self.removable {
514
63
            let remove = crate::widgets::widget_p_with_text(AzString::from_const_str("\u{00D7}"))
515
63
                .with_ids_and_classes(IdOrClassVec::from_const_slice(CHIP_REMOVE_CLASS))
516
63
                .with_css_props(CssPropertyWithConditionsVec::from_const_slice(CHIP_REMOVE_STYLE))
517
63
                .with_tab_index(TabIndex::Auto)
518
63
                // The remove affordance is its own button, not part of the chip's label.
519
63
                .with_accessibility_info(azul_core::a11y::AccessibilityInfo {
520
63
                    role: azul_core::a11y::AccessibilityRole::PushButton,
521
63
                    ..Default::default()
522
63
                })
523
63
                .with_callbacks(
524
63
                    alloc::vec![CoreCallbackData {
525
63
                        event: EventFilter::Hover(HoverEventFilter::Click),
526
63
                        callback: CoreCallback {
527
63
                            cb: default_on_chip_remove as usize,
528
63
                            ctx: OptionRefAny::None,
529
63
                        },
530
63
                        refany: state_ref,
531
63
                    }]
532
63
                    .into(),
533
63
                );
534
63
            children.push(remove);
535
63
        }
536

            
537
80
        Dom::create_div()
538
80
            .with_ids_and_classes(IdOrClassVec::from_const_slice(CHIP_CONTAINER_CLASS))
539
80
            .with_css_props(container_style)
540
80
            .with_children(children.into())
541
80
    }
542
}
543

            
544
impl Default for Chip {
545
36
    fn default() -> Self {
546
36
        Self::create(AzString::from_const_str(""))
547
36
    }
548
}
549

            
550
/// "x" click handler. The hit node is the "x" (the callback-bearing node, per
551
/// `currentTarget` semantics — see `radio_group`); its parent is the chip
552
/// container. Flips `visible` to `false`, invokes the optional user callback,
553
/// then hides the whole chip via `display: none`.
554
12
extern "C" fn default_on_chip_remove(mut data: RefAny, mut info: CallbackInfo) -> Update {
555
12
    let remove_node = info.get_hit_node();
556
12
    let Some(container) = info.get_parent(remove_node) else {
557
3
        return Update::DoNothing;
558
    };
559

            
560
8
    let result = {
561
9
        let Some(mut chip) = data.downcast_mut::<ChipStateWrapper>() else {
562
1
            return Update::DoNothing;
563
        };
564
8
        chip.inner.visible = false;
565
8
        let inner = chip.inner;
566
8
        let chip = &mut *chip;
567
8
        match chip.on_remove.as_mut() {
568
4
            Some(ChipOnRemove { callback, refany }) => (callback.cb)(refany.clone(), info, inner),
569
4
            None => Update::DoNothing,
570
        }
571
    };
572

            
573
    // TODO2: hides the chip by toggling `display: none` via set_css_property.
574
    // This follows the proven live-restyle pattern of alert/check_box/radio_group
575
    // (which toggle display/opacity/background); the display:none relayout itself
576
    // is not GUI-verified in this build.
577
8
    info.set_css_property(container, CssProperty::const_display(LayoutDisplay::None));
578

            
579
8
    result
580
12
}
581

            
582
/// Label click handler. Invokes the optional user `on_click` with the current
583
/// [`ChipState`] (mirrors `default_on_chip_remove`, minus the state flip/hide).
584
10
extern "C" fn default_on_chip_click(mut data: RefAny, info: CallbackInfo) -> Update {
585
10
    let Some(mut chip) = data.downcast_mut::<ChipStateWrapper>() else {
586
1
        return Update::DoNothing;
587
    };
588
9
    let inner = chip.inner;
589
9
    let chip = &mut *chip;
590
9
    match chip.on_click.as_mut() {
591
8
        Some(ChipOnClick { callback, refany }) => (callback.cb)(refany.clone(), info, inner),
592
1
        None => Update::DoNothing,
593
    }
594
10
}
595

            
596
impl From<Chip> for Dom {
597
6
    fn from(c: Chip) -> Self {
598
6
        c.dom()
599
6
    }
600
}
601

            
602
#[cfg(test)]
603
mod autotest_generated {
604
    use std::{
605
        collections::{BTreeMap, HashMap, HashSet},
606
        sync::{Arc, Mutex},
607
    };
608

            
609
    use azul_core::{
610
        dom::{DomId, DomNodeId, EventFilter, HoverEventFilter, NodeId, NodeType},
611
        geom::{LogicalRect, OptionLogicalPosition},
612
        gl::OptionGlContextPtr,
613
        hit_test::ScrollPosition,
614
        refany::OptionRefAny,
615
        resources::RendererResources,
616
        styled_dom::{NodeHierarchyItemId, StyledDom},
617
        window::{MonitorVec, RawWindowHandle},
618
    };
619
    use azul_css::{
620
        props::basic::{length::SizeMetric, pixel::PixelValue},
621
        system::SystemStyle,
622
    };
623
    use rust_fontconfig::FcFontCache;
624

            
625
    use super::*;
626
    #[cfg(feature = "icu")]
627
    use crate::icu::IcuLocalizerHandle;
628
    use crate::{
629
        callbacks::{CallbackChange, CallbackInfoRefData, ExternalSystemCallbacks},
630
        solver3::{display_list::DisplayList, layout_tree::LayoutTree},
631
        window::{DomLayoutResult, LayoutWindow},
632
        window_state::FullWindowState,
633
    };
634

            
635
    // ------------------------------------------------------------------
636
    // Helpers
637
    // ------------------------------------------------------------------
638

            
639
    /// Every variant of `ChipKind` — the complete input domain of `colors`,
640
    /// `class_name` and `build_chip_style`.
641
    const ALL_KINDS: [ChipKind; 6] = [
642
        ChipKind::Default,
643
        ChipKind::Primary,
644
        ChipKind::Success,
645
        ChipKind::Danger,
646
        ChipKind::Warning,
647
        ChipKind::Info,
648
    ];
649

            
650
    const WHITE: ColorU = ColorU {
651
        r: 255,
652
        g: 255,
653
        b: 255,
654
        a: 255,
655
    };
656
    const DARK: ColorU = ColorU {
657
        r: 33,
658
        g: 37,
659
        b: 41,
660
        a: 255,
661
    };
662

            
663
    /// The declared properties of a style vec, in declaration order.
664
    fn properties(v: &CssPropertyWithConditionsVec) -> Vec<CssProperty> {
665
        v.as_ref().iter().map(|p| p.property.clone()).collect()
666
    }
667

            
668
    /// The *kind* of every declared property, in order (ignores the values).
669
    fn property_types(
670
        v: &CssPropertyWithConditionsVec,
671
    ) -> Vec<core::mem::Discriminant<CssProperty>> {
672
        v.as_ref()
673
            .iter()
674
            .map(|p| core::mem::discriminant(&p.property))
675
            .collect()
676
    }
677

            
678
    /// The `f32` of a `PixelValue`, asserting it is an absolute `px` length — an
679
    /// `em`/`%` slipping into the pill geometry would resolve against the parent
680
    /// font/box instead of the intended fixed padding or radius.
681
    fn px(pv: &PixelValue) -> f32 {
682
        assert_eq!(
683
            pv.metric,
684
            SizeMetric::Px,
685
            "chip geometry must be absolute px, got {:?}",
686
            pv.metric
687
        );
688
        pv.number.get()
689
    }
690

            
691
    /// The four paddings in `(top, bottom, left, right)` order.
692
    fn padding_px(
693
        v: &CssPropertyWithConditionsVec,
694
    ) -> (Option<f32>, Option<f32>, Option<f32>, Option<f32>) {
695
        let find = |f: &dyn Fn(&CssProperty) -> Option<f32>| {
696
            v.as_ref().iter().find_map(|p| f(&p.property))
697
        };
698
        (
699
            find(&|p| match p {
700
                CssProperty::PaddingTop(x) => x.get_property().map(|x| px(&x.inner)),
701
                _ => None,
702
            }),
703
            find(&|p| match p {
704
                CssProperty::PaddingBottom(x) => x.get_property().map(|x| px(&x.inner)),
705
                _ => None,
706
            }),
707
            find(&|p| match p {
708
                CssProperty::PaddingLeft(x) => x.get_property().map(|x| px(&x.inner)),
709
                _ => None,
710
            }),
711
            find(&|p| match p {
712
                CssProperty::PaddingRight(x) => x.get_property().map(|x| px(&x.inner)),
713
                _ => None,
714
            }),
715
        )
716
    }
717

            
718
    /// The four corner radii, in declaration order.
719
    fn radii_px(v: &CssPropertyWithConditionsVec) -> Vec<f32> {
720
        v.as_ref()
721
            .iter()
722
            .filter_map(|p| match &p.property {
723
                CssProperty::BorderTopLeftRadius(r) => r.get_property().map(|r| px(&r.inner)),
724
                CssProperty::BorderTopRightRadius(r) => r.get_property().map(|r| px(&r.inner)),
725
                CssProperty::BorderBottomLeftRadius(r) => r.get_property().map(|r| px(&r.inner)),
726
                CssProperty::BorderBottomRightRadius(r) => r.get_property().map(|r| px(&r.inner)),
727
                _ => None,
728
            })
729
            .collect()
730
    }
731

            
732
    fn font_size_px(v: &CssPropertyWithConditionsVec) -> Option<f32> {
733
        v.as_ref().iter().find_map(|p| match &p.property {
734
            CssProperty::FontSize(f) => f.get_property().map(|f| px(&f.inner)),
735
            _ => None,
736
        })
737
    }
738

            
739
    fn text_color(v: &CssPropertyWithConditionsVec) -> Option<ColorU> {
740
        v.as_ref().iter().find_map(|p| match &p.property {
741
            CssProperty::TextColor(c) => c.get_property().map(|c| c.inner),
742
            _ => None,
743
        })
744
    }
745

            
746
    /// The single background layer of a style vec, asserting there is exactly one
747
    /// and that it is a flat colour (a gradient would not be a `Color`).
748
    fn background_color(v: &CssPropertyWithConditionsVec) -> Option<ColorU> {
749
        let bg = v.as_ref().iter().find_map(|p| match &p.property {
750
            CssProperty::BackgroundContent(b) => b.get_property(),
751
            _ => None,
752
        })?;
753
        assert_eq!(
754
            bg.as_ref().len(),
755
            1,
756
            "a chip must declare exactly one background layer"
757
        );
758
        match &bg.as_ref()[0] {
759
            StyleBackgroundContent::Color(c) => Some(*c),
760
            other => panic!("chip background is not a flat colour: {other:?}"),
761
        }
762
    }
763

            
764
    /// Every `PixelValue` a style vec mentions (paddings, radii, font size).
765
    fn all_pixel_values(v: &CssPropertyWithConditionsVec) -> Vec<PixelValue> {
766
        v.as_ref()
767
            .iter()
768
            .filter_map(|p| match &p.property {
769
                CssProperty::PaddingTop(x) => x.get_property().map(|x| x.inner),
770
                CssProperty::PaddingBottom(x) => x.get_property().map(|x| x.inner),
771
                CssProperty::PaddingLeft(x) => x.get_property().map(|x| x.inner),
772
                CssProperty::PaddingRight(x) => x.get_property().map(|x| x.inner),
773
                CssProperty::BorderTopLeftRadius(r) => r.get_property().map(|r| r.inner),
774
                CssProperty::BorderTopRightRadius(r) => r.get_property().map(|r| r.inner),
775
                CssProperty::BorderBottomLeftRadius(r) => r.get_property().map(|r| r.inner),
776
                CssProperty::BorderBottomRightRadius(r) => r.get_property().map(|r| r.inner),
777
                CssProperty::FontSize(f) => f.get_property().map(|f| f.inner),
778
                _ => None,
779
            })
780
            .collect()
781
    }
782

            
783
    /// Perceived brightness (0..=255) of an sRGB colour, Rec.709 weights. Kept to
784
    /// plain `+`/`*` (no gamma expansion) so the readability assertions below stay
785
    /// exact and toolchain-independent.
786
    fn luma(c: ColorU) -> f32 {
787
        0.2126 * f32::from(c.r) + 0.7152 * f32::from(c.g) + 0.0722 * f32::from(c.b)
788
    }
789

            
790
    /// The text of a text node, looking through the `<p>` block wrapper the
791
    /// label convention mandates (`p > text`).
792
    fn text_of(node: &Dom) -> Option<&str> {
793
        match node.root.get_node_type() {
794
            NodeType::Text(s) => Some(s.as_ref().as_str()),
795
            NodeType::P => match node.children.as_ref() {
796
                [only] => match only.root.get_node_type() {
797
                    NodeType::Text(s) => Some(s.as_ref().as_str()),
798
                    _ => None,
799
                },
800
                _ => None,
801
            },
802
            _ => None,
803
        }
804
    }
805

            
806
    /// The properties of a rendered node's *inline* style, in declaration order.
807
    fn inline_properties(node: &Dom) -> Vec<CssProperty> {
808
        node.root
809
            .style
810
            .iter_inline_properties()
811
            .map(|(p, _)| p.clone())
812
            .collect()
813
    }
814

            
815
    /// Adversarial chip labels: empty, whitespace, combining marks, ZWJ emoji,
816
    /// RTL, embedded NULs (`AzString` is length-based, so a NUL must not
817
    /// truncate), the remove glyph itself, and a string far longer than any
818
    /// plausible tag.
819
    fn adversarial_strings() -> Vec<String> {
820
        let mut v: Vec<String> = [
821
            "",
822
            "tag",
823
            " ",
824
            "e\u{0301}",                                   // e + combining acute
825
            "\u{1F469}\u{200D}\u{1F469}\u{200D}\u{1F467}", // ZWJ family emoji
826
            "\u{5E9}\u{5DC}\u{5D5}\u{5DD}",                // RTL Hebrew
827
            "\0",                                          // a single NUL
828
            "a\0b",                                        // embedded NUL
829
            "\u{FFFD}\u{202E}\u{200B}",                    // replacement char, RTL override, ZWSP
830
            "\u{00D7}",                                    // the remove glyph as a label
831
            "line\nbreak\ttab",                            // control characters
832
            "-9223372036854775808",                        // i64::MIN as a "count"
833
        ]
834
        .iter()
835
        .map(|s| (*s).to_string())
836
        .collect();
837
        v.push("x".repeat(100_000));
838
        v
839
    }
840

            
841
    fn remove_cb(f: ChipOnRemoveCallbackType) -> ChipOnRemoveCallback {
842
        f.into()
843
    }
844

            
845
    fn click_cb(f: ChipOnClickCallbackType) -> ChipOnClickCallback {
846
        f.into()
847
    }
848

            
849
    /// A `RefAny` payload recording every `ChipState` a user callback observes.
850
    struct StateLog {
851
        calls: Vec<bool>,
852
    }
853

            
854
    extern "C" fn record_remove(mut data: RefAny, _: CallbackInfo, state: ChipState) -> Update {
855
        if let Some(mut log) = data.downcast_mut::<StateLog>() {
856
            log.calls.push(state.visible);
857
        }
858
        Update::RefreshDom
859
    }
860

            
861
    extern "C" fn record_click(mut data: RefAny, _: CallbackInfo, state: ChipState) -> Update {
862
        if let Some(mut log) = data.downcast_mut::<StateLog>() {
863
            log.calls.push(state.visible);
864
            log.calls.push(state.visible); // keeps this body distinct from record_remove
865
        }
866
        Update::RefreshDom
867
    }
868

            
869
    extern "C" fn remove_do_nothing(_: RefAny, _: CallbackInfo, _: ChipState) -> Update {
870
        Update::DoNothing
871
    }
872

            
873
    extern "C" fn click_do_nothing(_: RefAny, _: CallbackInfo, state: ChipState) -> Update {
874
        // `state.visible` is read (and discarded) purely so this body cannot be
875
        // identical-code-folded onto `remove_do_nothing`; the tests below compare
876
        // callback function pointers for inequality.
877
        let _ = state.visible;
878
        Update::DoNothing
879
    }
880

            
881
    /// A payload whose callback tries to read the *same* `ChipStateWrapper`
882
    /// `RefAny` that the handler is currently holding a mutable borrow on.
883
    struct ReentrantProbe {
884
        /// A clone of the state `RefAny` the handler was invoked with.
885
        state: RefAny,
886
        /// `Some(visible)` if the re-entrant read succeeded, `None` if it was
887
        /// refused. Starts as `Some(true)` so "never ran" is distinguishable.
888
        saw_state: Option<bool>,
889
        calls: usize,
890
    }
891

            
892
    extern "C" fn probe_state_reentrantly(
893
        mut data: RefAny,
894
        _: CallbackInfo,
895
        _: ChipState,
896
    ) -> Update {
897
        if let Some(mut probe) = data.downcast_mut::<ReentrantProbe>() {
898
            probe.calls += 1;
899
            let mut state = probe.state.clone();
900
            probe.saw_state = state
901
                .downcast_ref::<ChipStateWrapper>()
902
                .map(|w| w.inner.visible);
903
        }
904
        Update::DoNothing
905
    }
906

            
907
    /// `visible` of a `ChipStateWrapper` payload.
908
    fn wrapper_visible(data: &mut RefAny) -> bool {
909
        data.downcast_ref::<ChipStateWrapper>()
910
            .expect("payload must still be a ChipStateWrapper")
911
            .inner
912
            .visible
913
    }
914

            
915
    /// The `visible` flags recorded by a `StateLog` payload.
916
    fn log_calls(data: &mut RefAny) -> Vec<bool> {
917
        data.downcast_ref::<StateLog>()
918
            .expect("payload must still be a StateLog")
919
            .calls
920
            .clone()
921
    }
922

            
923
    /// A `DomLayoutResult` with an *empty* layout tree: the chip handlers only
924
    /// walk `styled_dom.node_hierarchy`, so no real layout (and no font) is needed.
925
    fn layout_result(styled_dom: StyledDom) -> DomLayoutResult {
926
        DomLayoutResult {
927
            styled_dom,
928
            layout_tree: LayoutTree {
929
                nodes: Vec::new(),
930
                warm: Vec::new(),
931
                cold: Vec::new(),
932
                root: 0,
933
                dom_to_layout: BTreeMap::new(),
934
                children_arena: Vec::new(),
935
                children_offsets: Vec::new(),
936
                subtree_needs_intrinsic: Vec::new(),
937
            },
938
            calculated_positions: Vec::new(),
939
            viewport: LogicalRect::zero(),
940
            display_list: Arc::new(DisplayList::default()),
941
            scroll_ids: HashMap::new(),
942
            scroll_id_to_node_id: HashMap::new(),
943
        }
944
    }
945

            
946
    /// Flat indices of a removable chip in depth-first pre-order:
947
    /// `0 container / 1 label <p> / 2 label text / 3 remove <p> / 4 remove text`.
948
    /// Both callbacks sit on the `<p>`s — a text node owns no rect and could
949
    /// never be hit-tested.
950
    const LABEL_NODE: usize = 1;
951
    const REMOVE_NODE: usize = 3;
952

            
953
    /// The flattened DOM of a removable chip — exactly the hierarchy
954
    /// `default_on_chip_remove` walks (hit node -> parent).
955
    fn removable_styled_dom() -> StyledDom {
956
        let chip = Chip::create(AzString::from("tag")).with_removable(true);
957
        let styled = StyledDom::create_from_dom(chip.dom());
958
        assert_eq!(
959
            styled.node_hierarchy.as_ref().len(),
960
            5,
961
            "fixture must flatten to container / label <p> + text / remove <p> + text"
962
        );
963
        styled
964
    }
965

            
966
    /// Builds a `CallbackInfo` pointing at node `hit` of `styled` (or at a window
967
    /// with no layout result at all when `styled` is `None`), hands it to `f`, and
968
    /// returns `f`'s result plus every recorded `CallbackChange`.
969
    fn with_callback_info<R>(
970
        styled: Option<StyledDom>,
971
        hit: usize,
972
        f: impl FnOnce(CallbackInfo) -> R,
973
    ) -> (R, Vec<CallbackChange>) {
974
        let mut layout_window =
975
            LayoutWindow::new(FcFontCache::default()).expect("LayoutWindow::new failed");
976
        if let Some(sd) = styled {
977
            layout_window
978
                .layout_results
979
                .insert(DomId::ROOT_ID, layout_result(sd));
980
        }
981

            
982
        let renderer_resources = RendererResources::default();
983
        let previous_window_state: Option<FullWindowState> = None;
984
        let current_window_state = FullWindowState::default();
985
        let gl_context = OptionGlContextPtr::None;
986
        let scroll_states: BTreeMap<DomId, BTreeMap<NodeHierarchyItemId, ScrollPosition>> =
987
            BTreeMap::new();
988
        let window_handle = RawWindowHandle::Unsupported;
989
        let system_callbacks = ExternalSystemCallbacks::rust_internal();
990

            
991
        let ref_data = CallbackInfoRefData {
992
            layout_window: &layout_window,
993
            renderer_resources: &renderer_resources,
994
            previous_window_state: &previous_window_state,
995
            current_window_state: &current_window_state,
996
            gl_context: &gl_context,
997
            current_scroll_manager: &scroll_states,
998
            current_window_handle: &window_handle,
999
            system_callbacks: &system_callbacks,
            system_style: Arc::new(SystemStyle::default()),
            monitors: Arc::new(Mutex::new(MonitorVec::from_const_slice(&[]))),
            #[cfg(feature = "icu")]
            icu_localizer: IcuLocalizerHandle::default(),
            ctx: OptionRefAny::None,
        };
        let changes: Arc<Mutex<Vec<CallbackChange>>> = Arc::new(Mutex::new(Vec::new()));
        let info = CallbackInfo::new(
            &ref_data,
            &changes,
            DomNodeId {
                dom: DomId::ROOT_ID,
                node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(hit))),
            },
            OptionLogicalPosition::None,
            OptionLogicalPosition::None,
        );
        let out = f(info);
        let recorded = core::mem::take(&mut *changes.lock().expect("change log poisoned"));
        (out, recorded)
    }
    fn run_remove(
        styled: Option<StyledDom>,
        hit: usize,
        data: RefAny,
    ) -> (Update, Vec<CallbackChange>) {
        with_callback_info(styled, hit, move |info| default_on_chip_remove(data, info))
    }
    fn run_click(
        styled: Option<StyledDom>,
        hit: usize,
        data: RefAny,
    ) -> (Update, Vec<CallbackChange>) {
        with_callback_info(styled, hit, move |info| default_on_chip_click(data, info))
    }
    /// Every `display` write recorded in the change log, as `(node index, display)`.
    fn display_writes(changes: &[CallbackChange]) -> Vec<(usize, LayoutDisplay)> {
        let mut out = Vec::new();
        for change in changes {
            if let CallbackChange::ChangeNodeCssProperties {
                node_id,
                properties,
                ..
            } = change
            {
                for p in properties.as_ref() {
                    if let CssProperty::Display(v) = p {
                        if let Some(d) = v.get_property() {
                            out.push((node_id.index(), *d));
                        }
                    }
                }
            }
        }
        out
    }
    /// A `ChipStateWrapper` wired to `record_remove`, plus the log it writes to.
    fn state_with_remove_log() -> (RefAny, RefAny) {
        let log = RefAny::new(StateLog { calls: Vec::new() });
        let state = RefAny::new(ChipStateWrapper {
            inner: ChipState { visible: true },
            on_remove: Some(ChipOnRemove {
                callback: remove_cb(record_remove),
                refany: log.clone(),
            })
            .into(),
            on_click: OptionChipOnClick::None,
        });
        (state, log)
    }
    /// A `ChipStateWrapper` wired to `record_click`, plus the log it writes to.
    fn state_with_click_log(visible: bool) -> (RefAny, RefAny) {
        let log = RefAny::new(StateLog { calls: Vec::new() });
        let state = RefAny::new(ChipStateWrapper {
            inner: ChipState { visible },
            on_remove: OptionChipOnRemove::None,
            on_click: Some(ChipOnClick {
                callback: click_cb(record_click),
                refany: log.clone(),
            })
            .into(),
        });
        (state, log)
    }
    // ------------------------------------------------------------------
    // ChipKind::colors  (getter)
    // ------------------------------------------------------------------
    #[test]
    fn colors_returns_the_documented_constants_for_every_kind() {
        let expected = [
            // NOTE: unlike `BadgeKind::Default` (solid grey + white text), the
            // default *chip* is the light neutral "tag" pill with dark text.
            (
                ChipKind::Default,
                ColorU {
                    r: 233,
                    g: 236,
                    b: 239,
                    a: 255,
                },
                DARK,
            ),
            (
                ChipKind::Primary,
                ColorU {
                    r: 13,
                    g: 110,
                    b: 253,
                    a: 255,
                },
                WHITE,
            ),
            (
                ChipKind::Success,
                ColorU {
                    r: 25,
                    g: 135,
                    b: 84,
                    a: 255,
                },
                WHITE,
            ),
            (
                ChipKind::Danger,
                ColorU {
                    r: 220,
                    g: 53,
                    b: 69,
                    a: 255,
                },
                WHITE,
            ),
            (
                ChipKind::Warning,
                ColorU {
                    r: 255,
                    g: 193,
                    b: 7,
                    a: 255,
                },
                DARK,
            ),
            (
                ChipKind::Info,
                ColorU {
                    r: 13,
                    g: 202,
                    b: 240,
                    a: 255,
                },
                DARK,
            ),
        ];
        for (kind, bg, text) in expected {
            assert_eq!(
                kind.colors(),
                (bg, text),
                "{kind:?}: wrong (background, text) pair"
            );
        }
        // The doc comments promise Default/Warning/Info are the dark-text kinds and
        // no others: a fourth dark-text kind sneaking in here is a regression.
        for kind in ALL_KINDS {
            let (_, text) = kind.colors();
            let dark_text = matches!(kind, ChipKind::Default | ChipKind::Warning | ChipKind::Info);
            assert_eq!(
                text == DARK,
                dark_text,
                "{kind:?}: text colour contradicts the documented variant"
            );
        }
    }
    #[test]
    fn colors_only_ever_returns_one_of_the_two_documented_text_colours() {
        for kind in ALL_KINDS {
            let (_, text) = kind.colors();
            assert!(
                text == WHITE || text == DARK,
                "{kind:?}: text colour {text:?} is neither the documented WHITE nor DARK"
            );
        }
    }
    #[test]
    fn colors_are_fully_opaque_on_every_kind() {
        // A non-opaque pill would let the page background bleed through and
        // silently destroy the contrast the kind was chosen for.
        for kind in ALL_KINDS {
            let (bg, text) = kind.colors();
            assert_eq!(bg.a, 255, "{kind:?}: translucent background {bg:?}");
            assert_eq!(text.a, 255, "{kind:?}: translucent text colour {text:?}");
            assert_ne!(bg, text, "{kind:?}: an invisible label is not a chip");
        }
    }
    #[test]
    fn colors_give_every_kind_a_distinguishable_background() {
        // Two kinds that render identically make the semantic variant useless.
        let mut seen = HashSet::new();
        for kind in ALL_KINDS {
            let (bg, _) = kind.colors();
            assert!(
                seen.insert((bg.r, bg.g, bg.b, bg.a)),
                "{kind:?}: duplicate background colour {bg:?}"
            );
        }
        assert_eq!(seen.len(), ALL_KINDS.len());
    }
    #[test]
    fn colors_pick_the_more_readable_of_the_two_text_colours() {
        // The only real invariant of `colors()`: the text must be legible on the
        // pill. For each kind the chosen text colour must be further from the
        // background (in perceived brightness) than the rejected alternative,
        // and light backgrounds must take the dark text.
        for kind in ALL_KINDS {
            let (bg, text) = kind.colors();
            let other = if text == WHITE { DARK } else { WHITE };
            let chosen = (luma(bg) - luma(text)).abs();
            let rejected = (luma(bg) - luma(other)).abs();
            assert!(
                chosen > rejected,
                "{kind:?}: text {text:?} (delta luma {chosen:.1}) is less readable on {bg:?} than \
                 {other:?} (delta luma {rejected:.1})"
            );
            assert!(
                chosen >= 60.0,
                "{kind:?}: text/background brightness gap {chosen:.1} is too low to read"
            );
            // Mid-grey split: a light pill must not carry white text.
            let light_bg = luma(bg) >= 128.0;
            assert_eq!(
                text == DARK,
                light_bg,
                "{kind:?}: bg luma {:.1} but text is {text:?}",
                luma(bg)
            );
        }
    }
    #[test]
    fn colors_is_pure_and_the_default_kind_is_the_neutral_tag() {
        assert_eq!(ChipKind::default(), ChipKind::Default);
        assert_eq!(ChipKind::default().colors(), ChipKind::Default.colors());
        // `colors()` takes `&self` on a `Copy` enum: repeated calls, and calls
        // through a copy, must be side-effect free and identical.
        for kind in ALL_KINDS {
            let copy = kind;
            assert_eq!(
                kind.colors(),
                kind.colors(),
                "{kind:?}: colors() is not pure"
            );
            assert_eq!(
                kind.colors(),
                copy.colors(),
                "{kind:?}: a copy disagrees with the original"
            );
        }
    }
    #[test]
    fn colors_is_const_evaluable() {
        const DEFAULT: (ColorU, ColorU) = ChipKind::Default.colors();
        assert_eq!(
            DEFAULT.0,
            ColorU {
                r: 233,
                g: 236,
                b: 239,
                a: 255
            }
        );
        assert_eq!(DEFAULT.1, DARK);
    }
    // ------------------------------------------------------------------
    // ChipKind::class_name  (getter)
    // ------------------------------------------------------------------
    #[test]
    fn class_name_returns_the_documented_string_for_every_kind() {
        assert_eq!(ChipKind::Default.class_name(), "__azul-chip-default");
        assert_eq!(ChipKind::Primary.class_name(), "__azul-chip-primary");
        assert_eq!(ChipKind::Success.class_name(), "__azul-chip-success");
        assert_eq!(ChipKind::Danger.class_name(), "__azul-chip-danger");
        assert_eq!(ChipKind::Warning.class_name(), "__azul-chip-warning");
        assert_eq!(ChipKind::Info.class_name(), "__azul-chip-info");
        assert_eq!(ChipKind::default().class_name(), "__azul-chip-default");
    }
    #[test]
    fn class_name_is_unique_per_kind_and_a_usable_css_identifier() {
        let mut seen = HashSet::new();
        for kind in ALL_KINDS {
            let name = kind.class_name();
            assert!(
                seen.insert(name),
                "{kind:?}: class name {name:?} collides with another kind"
            );
            assert!(!name.is_empty(), "{kind:?}: empty class name");
            assert!(
                name.starts_with("__azul-chip-"),
                "{kind:?}: unnamespaced class {name:?}"
            );
            assert!(name.is_ascii(), "{kind:?}: non-ASCII class name {name:?}");
            // A space, a dot or a `#` would silently split/re-target the selector.
            assert!(
                name.chars()
                    .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'),
                "{kind:?}: class name {name:?} contains a CSS-significant character"
            );
            // The returned `&'static str` must be stable across calls.
            assert_eq!(
                name.as_ptr(),
                kind.class_name().as_ptr(),
                "{kind:?}: class_name() is not a stable constant"
            );
        }
        assert_eq!(seen.len(), ALL_KINDS.len());
    }
    #[test]
    fn class_name_does_not_collide_with_the_widget_element_classes() {
        // `__azul-native-chip` is a *prefix* of `__azul-native-chip-label` and
        // `__azul-native-chip-remove`; the kind classes live in their own
        // `__azul-chip-` namespace and must not alias any of the three.
        let element_classes = [
            "__azul-native-chip",
            "__azul-native-chip-label",
            "__azul-native-chip-remove",
        ];
        for kind in ALL_KINDS {
            let name = kind.class_name();
            for element in element_classes {
                assert_ne!(
                    name, element,
                    "{kind:?}: kind class shadows the element class {element:?}"
                );
            }
        }
    }
    #[test]
    fn class_name_is_const_evaluable() {
        const DANGER: &str = ChipKind::Danger.class_name();
        assert_eq!(DANGER, "__azul-chip-danger");
    }
    // ------------------------------------------------------------------
    // build_chip_style
    // ------------------------------------------------------------------
    #[test]
    fn build_chip_style_emits_the_documented_pill_geometry() {
        for kind in ALL_KINDS {
            let style = build_chip_style(kind);
            assert_eq!(
                padding_px(&style),
                (Some(4.0), Some(4.0), Some(10.0), Some(10.0)),
                "{kind:?}: padding is not 4px 10px"
            );
            assert_eq!(
                radii_px(&style),
                vec![12.0, 12.0, 12.0, 12.0],
                "{kind:?}: all four corners must carry a 12px radius"
            );
            assert_eq!(
                font_size_px(&style),
                Some(13.0),
                "{kind:?}: wrong font size"
            );
        }
    }
    #[test]
    fn build_chip_style_radius_actually_rounds_the_chip_to_a_pill() {
        // The widget's premise: the corner radius must reach at least half the
        // content height (font + vertical padding), otherwise it renders as a
        // rounded rectangle rather than a pill.
        for kind in ALL_KINDS {
            let style = build_chip_style(kind);
            let (top, bottom, ..) = padding_px(&style);
            let height = font_size_px(&style).expect("a font size must be declared")
                + top.expect("padding-top")
                + bottom.expect("padding-bottom");
            for r in radii_px(&style) {
                assert!(
                    r * 2.0 >= height,
                    "{kind:?}: radius {r} does not reach half of the {height}px pill height"
                );
            }
        }
    }
    #[test]
    fn build_chip_style_is_a_row_flexbox_that_hugs_its_content() {
        for kind in ALL_KINDS {
            let props = properties(&build_chip_style(kind));
            let has = |p: &CssProperty| props.contains(p);
            assert!(
                has(&CssProperty::const_display(LayoutDisplay::Flex)),
                "{kind:?}: not a flex box"
            );
            assert!(
                has(&CssProperty::const_flex_direction(LayoutFlexDirection::Row)),
                "{kind:?}: the label and the x must sit side by side"
            );
            assert!(
                has(&CssProperty::const_align_items(LayoutAlignItems::Center)),
                "{kind:?}: label and x not vertically centred"
            );
            // align-self: start + flex-grow: 0 — without both, the pill stretches
            // across a flex parent instead of hugging its label.
            assert!(
                has(&CssProperty::align_self(LayoutAlignSelf::Start)),
                "{kind:?}: chip stretches on the cross axis"
            );
            assert!(
                has(&CssProperty::const_flex_grow(LayoutFlexGrow::const_new(0))),
                "{kind:?}: chip grows on the main axis"
            );
        }
    }
    #[test]
    fn build_chip_style_leaves_text_alignment_to_the_label() {
        // Unlike `badge`, the chip container declares neither `text-align` nor
        // `justify-content` — the label child owns `text-align: left`. A
        // container-level declaration here would fight the label's own style.
        for kind in ALL_KINDS {
            for p in build_chip_style(kind).as_ref() {
                assert!(
                    !matches!(p.property, CssProperty::TextAlign(_)),
                    "{kind:?}: the container must not declare text-align"
                );
                assert!(
                    !matches!(p.property, CssProperty::JustifyContent(_)),
                    "{kind:?}: the container must not declare justify-content"
                );
            }
        }
    }
    #[test]
    fn build_chip_style_declares_the_inheritable_text_style_once() {
        // The label and the "x" carry no colour/family of their own — both are
        // inherited from the container, so the container must declare them.
        for kind in ALL_KINDS {
            let style = build_chip_style(kind);
            let families: Vec<_> = style
                .as_ref()
                .iter()
                .filter_map(|p| match &p.property {
                    CssProperty::FontFamily(f) => f.get_property(),
                    _ => None,
                })
                .collect();
            assert_eq!(
                families.len(),
                1,
                "{kind:?}: exactly one font-family declaration"
            );
            let fams = families[0].as_ref();
            assert_eq!(
                fams.len(),
                1,
                "{kind:?}: expected a single system-ui family"
            );
            match &fams[0] {
                StyleFontFamily::System(name) => {
                    assert_eq!(
                        name.as_str(),
                        "system:ui",
                        "{kind:?}: wrong system font family"
                    )
                }
                other => panic!("{kind:?}: chip must use the system UI font, got {other:?}"),
            }
            assert!(
                text_color(&style).is_some(),
                "{kind:?}: no inheritable text colour declared"
            );
        }
    }
    #[test]
    fn build_chip_style_colours_track_the_kind() {
        for kind in ALL_KINDS {
            let style = build_chip_style(kind);
            let (bg, text) = kind.colors();
            assert_eq!(
                background_color(&style),
                Some(bg),
                "{kind:?}: emitted background != colors().0"
            );
            assert_eq!(
                text_color(&style),
                Some(text),
                "{kind:?}: emitted text colour != colors().1"
            );
        }
    }
    #[test]
    fn build_chip_style_declares_every_property_at_most_once() {
        // A duplicated declaration is a last-one-wins ambiguity: two backgrounds
        // would make one of them silently dead.
        for kind in ALL_KINDS {
            let types = property_types(&build_chip_style(kind));
            let mut seen = HashSet::new();
            for t in &types {
                assert!(
                    seen.insert(*t),
                    "{kind:?}: the container style declares the same property twice"
                );
            }
            assert_eq!(seen.len(), types.len());
        }
    }
    #[test]
    fn build_chip_style_properties_are_all_unconditional() {
        // The chip container is stateless — a declaration gated on
        // `:hover`/`:active` would simply never paint.
        for kind in ALL_KINDS {
            for p in build_chip_style(kind).as_ref() {
                assert!(
                    p.apply_if.as_ref().is_empty(),
                    "{kind:?}: {:?} is conditional on a stateless container",
                    p.property
                );
            }
        }
    }
    #[test]
    fn build_chip_style_is_deterministic_and_kind_dependent() {
        let baseline = property_types(&build_chip_style(ChipKind::Default));
        assert!(
            !baseline.is_empty(),
            "the container style must not be empty"
        );
        for kind in ALL_KINDS {
            assert_eq!(
                properties(&build_chip_style(kind)),
                properties(&build_chip_style(kind)),
                "{kind:?}: two builds of the same kind disagree"
            );
            assert_eq!(
                property_types(&build_chip_style(kind)),
                baseline,
                "{kind:?}: declares different properties, or in a different order, than Default"
            );
        }
        // No two kinds may collapse onto the same style, or the variant is a no-op.
        for (i, a) in ALL_KINDS.iter().enumerate() {
            for b in &ALL_KINDS[i + 1..] {
                assert_ne!(
                    properties(&build_chip_style(*a)),
                    properties(&build_chip_style(*b)),
                    "{a:?} and {b:?} produce an identical style"
                );
            }
        }
    }
    #[test]
    fn build_chip_style_differs_only_in_the_colours() {
        // Exactly two declarations may depend on the kind: the background and the
        // text colour. Kinds that share a text colour (Default/Warning/Info are
        // all dark-on-light) must therefore differ in the background *alone* —
        // any third differing declaration means geometry leaked into the palette.
        for (i, a) in ALL_KINDS.iter().enumerate() {
            for b in &ALL_KINDS[i + 1..] {
                let (style_a, style_b) = (build_chip_style(*a), build_chip_style(*b));
                let differing: Vec<_> = style_a
                    .as_ref()
                    .iter()
                    .zip(style_b.as_ref().iter())
                    .filter(|(x, y)| x != y)
                    .map(|(x, _)| core::mem::discriminant(&x.property))
                    .collect();
                let same_text = a.colors().1 == b.colors().1;
                let expected = if same_text { 1 } else { 2 };
                assert_eq!(
                    differing.len(),
                    expected,
                    "{a:?} vs {b:?}: only the background{} may depend on the kind",
                    if same_text {
                        ""
                    } else {
                        " and the text colour"
                    }
                );
                let bg_differs =
                    style_a
                        .as_ref()
                        .iter()
                        .zip(style_b.as_ref().iter())
                        .any(|(x, y)| {
                            x != y && matches!(x.property, CssProperty::BackgroundContent(_))
                        });
                assert!(
                    bg_differs,
                    "{a:?} vs {b:?}: the background must be one of the differing declarations"
                );
            }
        }
    }
    #[test]
    fn build_chip_style_emits_only_finite_non_negative_px_lengths() {
        // Guard the numeric conversions in this file (`isize` -> `PixelValue`):
        // a NaN/inf/negative length must never reach the layout solver.
        for kind in ALL_KINDS {
            let values = all_pixel_values(&build_chip_style(kind));
            assert_eq!(
                values.len(),
                9,
                "{kind:?}: expected 4 paddings + 4 radii + 1 font size"
            );
            for pv in values {
                let n = px(&pv); // also asserts SizeMetric::Px
                assert!(n.is_finite(), "{kind:?}: non-finite length {n}");
                assert!(n >= 0.0, "{kind:?}: negative length {n}");
                assert!(
                    n <= 128.0,
                    "{kind:?}: implausibly large length {n} for a chip"
                );
            }
        }
    }
    #[test]
    fn label_and_remove_static_styles_are_finite_unconditional_and_non_growing() {
        for (name, style) in [("label", CHIP_LABEL_STYLE), ("remove", CHIP_REMOVE_STYLE)] {
            let vec = CssPropertyWithConditionsVec::from_const_slice(style);
            for p in vec.as_ref() {
                assert!(
                    p.apply_if.as_ref().is_empty(),
                    "{name}: {:?} must be unconditional",
                    p.property
                );
            }
            // Both children must hug their content, or the "x" is pushed off the pill.
            assert!(
                properties(&vec)
                    .contains(&CssProperty::const_flex_grow(LayoutFlexGrow::const_new(0))),
                "{name}: child must not grow inside the pill"
            );
            for pv in all_pixel_values(&vec) {
                let n = px(&pv);
                assert!(n.is_finite(), "{name}: non-finite length {n}");
                assert!((0.0..=64.0).contains(&n), "{name}: implausible length {n}");
            }
            let types = property_types(&vec);
            let mut seen = HashSet::new();
            for t in &types {
                assert!(seen.insert(*t), "{name}: declares the same property twice");
            }
        }
    }
    #[test]
    fn the_remove_affordance_is_styled_as_a_clickable_target() {
        let remove = CssPropertyWithConditionsVec::from_const_slice(CHIP_REMOVE_STYLE);
        let props = properties(&remove);
        assert!(
            props.contains(&CssProperty::const_cursor(StyleCursor::Pointer)),
            "the x must advertise itself as clickable"
        );
        assert!(
            props.contains(&CssProperty::user_select(StyleUserSelect::None)),
            "dragging across the x must not select the glyph"
        );
        assert!(
            props.contains(&CssProperty::const_margin_left(LayoutMarginLeft::const_px(
                6
            ))),
            "the x needs breathing room from the label"
        );
        let label = CssPropertyWithConditionsVec::from_const_slice(CHIP_LABEL_STYLE);
        let label_props = properties(&label);
        assert!(
            label_props.contains(&CssProperty::const_text_align(StyleTextAlign::Left)),
            "the label owns text-align, not the container"
        );
        assert!(
            label_props.contains(&CssProperty::user_select(StyleUserSelect::None)),
            "the label must not be selectable (it is a click target)"
        );
        assert!(
            !label_props
                .iter()
                .any(|p| matches!(p, CssProperty::Cursor(_))),
            "only the x declares a pointer cursor"
        );
    }
    // ------------------------------------------------------------------
    // Chip::create / Chip::with_kind  (constructors)
    // ------------------------------------------------------------------
    #[test]
    fn create_defaults_to_the_neutral_tag_and_keeps_the_label_verbatim() {
        for s in adversarial_strings() {
            let c = Chip::create(AzString::from(s.clone()));
            assert_eq!(
                c.label.as_str(),
                s.as_str(),
                "the label was not preserved verbatim"
            );
            assert_eq!(
                c.label.len(),
                s.len(),
                "byte length changed (NUL truncation?)"
            );
            assert_eq!(
                c.kind,
                ChipKind::Default,
                "create() must use the neutral default kind"
            );
            assert!(!c.removable, "a fresh chip renders no x");
            assert!(c.chip_state.inner.visible, "a fresh chip is visible");
            assert!(
                c.chip_state.on_remove.is_none(),
                "a fresh chip carries no remove callback"
            );
            assert!(
                c.chip_state.on_click.is_none(),
                "a fresh chip carries no click callback"
            );
            assert_eq!(
                properties(&c.resolved_container_style()),
                properties(&build_chip_style(ChipKind::Default))
            );
        }
    }
    #[test]
    fn with_kind_stores_both_arguments_and_the_matching_style() {
        for kind in ALL_KINDS {
            for s in adversarial_strings() {
                let c = Chip::with_kind(AzString::from(s.clone()), kind);
                assert_eq!(
                    c.label.as_str(),
                    s.as_str(),
                    "{kind:?}: label not preserved"
                );
                assert_eq!(c.label.len(), s.len(), "{kind:?}: byte length changed");
                assert_eq!(
                    c.kind, kind,
                    "{kind:?}: kind field does not match the argument"
                );
                assert!(!c.removable, "{kind:?}: with_kind must not turn on the x");
                // The invariant that makes `container_style` a cache and not a lie.
                assert_eq!(
                    properties(&c.resolved_container_style()),
                    properties(&build_chip_style(kind))
                );
                assert_eq!(
                    background_color(&c.resolved_container_style()),
                    Some(kind.colors().0)
                );
            }
        }
    }
    #[test]
    fn create_is_with_kind_default() {
        for s in ["", "tag", "\u{1F600}", "\u{00D7}"] {
            assert_eq!(
                Chip::create(AzString::from_const_str(s)),
                Chip::with_kind(AzString::from_const_str(s), ChipKind::Default)
            );
        }
    }
    #[test]
    fn default_chip_is_an_empty_neutral_chip_and_equality_sees_every_field() {
        let d = Chip::default();
        assert_eq!(d, Chip::create(AzString::from_const_str("")));
        assert_eq!(d.label.as_str(), "");
        assert_eq!(d.kind, ChipKind::Default);
        assert!(!d.removable);
        assert_eq!(d.clone(), d, "Clone must preserve equality");
        assert_ne!(
            d,
            Chip::create(AzString::from_const_str("tag")),
            "the label must affect equality"
        );
        assert_ne!(
            Chip::with_kind(AzString::from_const_str("t"), ChipKind::Danger),
            Chip::with_kind(AzString::from_const_str("t"), ChipKind::Success),
            "chips of different kinds must not compare equal"
        );
        assert_ne!(
            Chip::default(),
            Chip::default().with_removable(true),
            "the removable flag must affect equality"
        );
    }
    #[test]
    fn chips_wired_to_separate_payloads_are_not_equal() {
        // `RefAny` equality is "same data", not "same value": two independently
        // allocated payloads holding the same `u8` are distinct, so the chips
        // that carry them must not compare equal either.
        let shared = RefAny::new(0u8);
        let a = Chip::create(AzString::from("t"))
            .with_on_click(shared.clone(), click_cb(click_do_nothing));
        let b = Chip::create(AzString::from("t"))
            .with_on_click(shared.clone(), click_cb(click_do_nothing));
        assert_eq!(
            a, b,
            "chips sharing one payload and one callback must be equal"
        );
        let c = Chip::create(AzString::from("t"))
            .with_on_click(RefAny::new(0u8), click_cb(click_do_nothing));
        assert_ne!(a, c, "a separately allocated payload is a different chip");
    }
    // ------------------------------------------------------------------
    // Chip::set_kind / with_chip_kind
    // ------------------------------------------------------------------
    #[test]
    fn set_kind_recomputes_the_style_without_growing_it() {
        // A push-instead-of-replace bug would grow the style vec on every call and
        // leave stale (earlier-kind) colour declarations behind, which then win or
        // lose the cascade by accident.
        let mut c = Chip::create(AzString::from_const_str("tag"));
        let expected_len = build_chip_style(ChipKind::Default).as_ref().len();
        for round in 0..50 {
            let kind = ALL_KINDS[round % ALL_KINDS.len()];
            c.set_kind(kind);
            assert_eq!(c.kind, kind, "round {round}: kind field not updated");
            assert_eq!(
                c.resolved_container_style().as_ref().len(),
                expected_len,
                "round {round}: style vec changed length — stale declarations?"
            );
            assert_eq!(
                properties(&c.resolved_container_style()),
                properties(&build_chip_style(kind)),
                "round {round}: style does not match a freshly built one"
            );
            assert_eq!(
                background_color(&c.resolved_container_style()),
                Some(kind.colors().0),
                "round {round}: stale background"
            );
            assert_eq!(
                c.label.as_str(),
                "tag",
                "round {round}: set_kind ate the label"
            );
        }
    }
    #[test]
    fn set_kind_leaves_label_removable_and_callbacks_alone() {
        let mut c = Chip::create(AzString::from("keep me"));
        c.set_on_remove(
            RefAny::new(StateLog { calls: Vec::new() }),
            remove_cb(remove_do_nothing),
        );
        c.set_on_click(RefAny::new(0u8), click_cb(click_do_nothing));
        c.chip_state.inner.visible = false;
        c.set_kind(ChipKind::Warning);
        assert_eq!(c.label.as_str(), "keep me");
        assert!(c.removable, "set_kind must not clear the x");
        assert!(
            c.chip_state.on_remove.is_some(),
            "set_kind must not drop the remove callback"
        );
        assert!(
            c.chip_state.on_click.is_some(),
            "set_kind must not drop the click callback"
        );
        assert!(
            !c.chip_state.inner.visible,
            "set_kind must not resurrect a removed chip"
        );
    }
    #[test]
    fn with_chip_kind_agrees_with_set_kind_and_is_last_call_wins() {
        let chained = Chip::create(AzString::from_const_str("t"))
            .with_chip_kind(ChipKind::Danger)
            .with_chip_kind(ChipKind::Warning)
            .with_chip_kind(ChipKind::Info);
        let mut mutated = Chip::create(AzString::from_const_str("t"));
        mutated.set_kind(ChipKind::Danger);
        mutated.set_kind(ChipKind::Warning);
        mutated.set_kind(ChipKind::Info);
        assert_eq!(chained, mutated, "the builder and the mutator must agree");
        assert_eq!(chained.kind, ChipKind::Info);
        assert_eq!(chained.label.as_str(), "t");
        assert_eq!(
            properties(&chained.resolved_container_style()),
            properties(&build_chip_style(ChipKind::Info))
        );
        // In particular the Danger red must be completely gone.
        assert_eq!(
            background_color(&chained.resolved_container_style()),
            Some(ChipKind::Info.colors().0)
        );
    }
    #[test]
    fn setting_the_same_kind_twice_is_idempotent() {
        for kind in ALL_KINDS {
            let once = Chip::with_kind(AzString::from_const_str("x"), kind);
            let twice = once.clone().with_chip_kind(kind);
            assert_eq!(
                once, twice,
                "{kind:?}: re-setting the same kind changed the chip"
            );
        }
        // A full cycle through every kind and back must not accumulate state.
        let original = Chip::create(AzString::from("t"));
        let mut cycled = original.clone();
        for kind in ALL_KINDS {
            cycled.set_kind(kind);
        }
        cycled.set_kind(ChipKind::Default);
        assert_eq!(cycled, original, "kind cycling must not accumulate state");
    }
    // ------------------------------------------------------------------
    // Chip::set_removable / with_removable
    // ------------------------------------------------------------------
    #[test]
    fn set_removable_last_write_wins_and_touches_nothing_else() {
        let mut c = Chip::with_kind(AzString::from("t"), ChipKind::Warning);
        let style_before = c.resolved_container_style();
        for flag in [true, true, false, true, false, false] {
            c.set_removable(flag);
            assert_eq!(c.removable, flag);
        }
        assert_eq!(c.kind, ChipKind::Warning);
        assert_eq!(c.label.as_str(), "t");
        assert_eq!(
            c.resolved_container_style(),
            style_before,
            "toggling must not restyle the pill"
        );
        assert!(
            c.chip_state.on_remove.is_none(),
            "toggling must not invent a callback"
        );
        assert!(
            c.chip_state.inner.visible,
            "toggling must not hide the chip"
        );
    }
    #[test]
    fn with_removable_toggle_sequence_ends_on_the_last_value() {
        assert!(Chip::default().with_removable(true).removable);
        assert!(!Chip::default().with_removable(false).removable);
        assert!(
            !Chip::default()
                .with_removable(true)
                .with_removable(false)
                .removable
        );
        assert!(
            Chip::default()
                .with_removable(false)
                .with_removable(true)
                .removable
        );
        // builder == setter
        let mut mutated = Chip::default();
        mutated.set_removable(true);
        assert_eq!(Chip::default().with_removable(true), mutated);
    }
    #[test]
    fn removable_only_changes_the_child_count_not_the_pill() {
        let plain = Chip::create(AzString::from("t"));
        let style = plain.resolved_container_style();
        let removable = plain.clone().with_removable(true);
        assert_eq!(
            removable.resolved_container_style(),
            style,
            "the x must not restyle the container"
        );
        assert_eq!(plain.dom().children.as_ref().len(), 1);
        assert_eq!(removable.dom().children.as_ref().len(), 2);
    }
    // ------------------------------------------------------------------
    // Chip::set_on_remove / with_on_remove
    // ------------------------------------------------------------------
    #[test]
    fn set_on_remove_implies_removable() {
        let mut c = Chip::create(AzString::from("t"));
        assert!(!c.removable);
        c.set_on_remove(RefAny::new(1u8), remove_cb(remove_do_nothing));
        assert!(c.removable, "a remove callback must render an x");
        assert!(c.chip_state.on_remove.is_some());
        assert!(
            c.chip_state.on_click.is_none(),
            "wiring on_remove must not invent an on_click"
        );
        assert!(
            c.chip_state.inner.visible,
            "wiring a callback must not hide the chip"
        );
        assert_eq!(
            c.dom().children.as_ref().len(),
            2,
            "the x must actually be rendered"
        );
    }
    #[test]
    fn set_on_remove_replaces_rather_than_appends() {
        let mut c = Chip::create(AzString::from("t"));
        c.set_on_remove(RefAny::new(1u8), remove_cb(remove_do_nothing));
        let first = c
            .chip_state
            .on_remove
            .as_ref()
            .expect("first callback")
            .refany
            .get_type_id();
        assert_eq!(first, RefAny::new(1u8).get_type_id());
        // a second call must *replace* the payload + function, not stack another one
        c.set_on_remove(RefAny::new(9i64), remove_cb(record_remove));
        let second = c.chip_state.on_remove.as_ref().expect("second callback");
        assert_eq!(second.refany.get_type_id(), RefAny::new(9i64).get_type_id());
        assert_eq!(second.callback, remove_cb(record_remove));
        assert_ne!(second.callback, remove_cb(remove_do_nothing));
        // still exactly one x in the DOM
        assert_eq!(c.dom().children.as_ref().len(), 2);
    }
    #[test]
    fn with_on_remove_keeps_the_label_kind_and_style() {
        let c = Chip::with_kind(AzString::from("boom"), ChipKind::Danger)
            .with_on_remove(RefAny::new(0u8), remove_cb(remove_do_nothing));
        assert_eq!(c.label.as_str(), "boom");
        assert_eq!(c.kind, ChipKind::Danger);
        assert_eq!(
            c.resolved_container_style(),
            build_chip_style(ChipKind::Danger)
        );
        assert!(c.removable);
        assert!(c.chip_state.on_remove.is_some());
    }
    #[test]
    fn set_removable_false_after_set_on_remove_silently_drops_the_x() {
        // Footgun, pinned as the *current* behaviour: `set_on_remove` implies
        // `removable = true`, but a later `set_removable(false)` wins and the
        // wired-up callback becomes unreachable (no x is rendered).
        let mut c = Chip::create(AzString::from("t"));
        c.set_on_remove(RefAny::new(0u8), remove_cb(record_remove));
        c.set_removable(false);
        assert!(
            c.chip_state.on_remove.is_some(),
            "the callback is still stored"
        );
        let dom = c.dom();
        assert_eq!(
            dom.children.as_ref().len(),
            1,
            "no x is rendered, so the remove callback can never fire"
        );
    }
    // ------------------------------------------------------------------
    // Chip::set_on_click / with_on_click
    // ------------------------------------------------------------------
    #[test]
    fn set_on_click_does_not_imply_removable() {
        // Deliberate asymmetry with `set_on_remove`: a clickable chip is not
        // automatically a removable one.
        let mut c = Chip::create(AzString::from("t"));
        c.set_on_click(RefAny::new(1u8), click_cb(click_do_nothing));
        assert!(!c.removable, "on_click must not turn on the x");
        assert!(c.chip_state.on_click.is_some());
        assert!(c.chip_state.on_remove.is_none());
        assert_eq!(c.dom().children.as_ref().len(), 1, "still just the label");
    }
    #[test]
    fn set_on_click_replaces_rather_than_appends() {
        let mut c = Chip::create(AzString::from("t"));
        c.set_on_click(RefAny::new(1u8), click_cb(click_do_nothing));
        c.set_on_click(RefAny::new(9i64), click_cb(record_click));
        let second = c.chip_state.on_click.as_ref().expect("second callback");
        assert_eq!(second.refany.get_type_id(), RefAny::new(9i64).get_type_id());
        assert_eq!(second.callback, click_cb(record_click));
        assert_ne!(second.callback, click_cb(click_do_nothing));
        // exactly one handler reaches the label
        let dom = c.dom();
        assert_eq!(
            dom.children.as_ref()[0].root.get_callbacks().as_ref().len(),
            1
        );
    }
    #[test]
    fn with_on_click_keeps_the_label_kind_and_style() {
        let c = Chip::with_kind(AzString::from("click me"), ChipKind::Primary)
            .with_on_click(RefAny::new(0u8), click_cb(click_do_nothing));
        assert_eq!(c.label.as_str(), "click me");
        assert_eq!(c.kind, ChipKind::Primary);
        assert_eq!(
            c.resolved_container_style(),
            build_chip_style(ChipKind::Primary)
        );
        assert!(c.chip_state.on_click.is_some());
    }
    #[test]
    fn both_callbacks_can_be_wired_at_once() {
        let c = Chip::create(AzString::from("t"))
            .with_on_click(RefAny::new(1u8), click_cb(record_click))
            .with_on_remove(RefAny::new(2u8), remove_cb(record_remove));
        assert!(
            c.removable,
            "on_remove still implies removable when on_click is set"
        );
        assert!(
            c.chip_state.on_click.is_some(),
            "on_remove must not clobber on_click"
        );
        assert!(c.chip_state.on_remove.is_some());
        assert_eq!(
            c.chip_state.on_click.as_ref().expect("on_click").callback,
            click_cb(record_click)
        );
        assert_eq!(
            c.chip_state.on_remove.as_ref().expect("on_remove").callback,
            remove_cb(record_remove)
        );
        // Setting them in the opposite order must produce the same wiring.
        let reversed = Chip::create(AzString::from("t"))
            .with_on_remove(RefAny::new(2u8), remove_cb(record_remove))
            .with_on_click(RefAny::new(1u8), click_cb(record_click));
        assert!(reversed.removable);
        assert!(reversed.chip_state.on_click.is_some());
        assert!(reversed.chip_state.on_remove.is_some());
    }
    // ------------------------------------------------------------------
    // Chip::swap_with_default
    // ------------------------------------------------------------------
    #[test]
    fn swap_with_default_returns_the_original_and_leaves_a_default_behind() {
        let mut c =
            Chip::with_kind(AzString::from_const_str("tag"), ChipKind::Danger).with_removable(true);
        let taken = c.swap_with_default();
        // The returned value is the *original*, intact.
        assert_eq!(taken.label.as_str(), "tag");
        assert_eq!(taken.kind, ChipKind::Danger);
        assert!(taken.removable);
        assert_eq!(
            properties(&taken.resolved_container_style()),
            properties(&build_chip_style(ChipKind::Danger))
        );
        // What is left behind is a *default* chip — in particular its style must
        // be the neutral one and not a stale Danger red.
        assert_eq!(c, Chip::default());
        assert_eq!(c.label.as_str(), "");
        assert_eq!(c.kind, ChipKind::Default);
        assert!(!c.removable, "the x must not survive the swap");
        assert_eq!(
            background_color(&c.resolved_container_style()),
            Some(ChipKind::Default.colors().0),
            "the red survived the swap"
        );
    }
    #[test]
    fn swap_with_default_is_idempotent_on_an_already_default_chip() {
        let mut c = Chip::default();
        for _ in 0..3 {
            let taken = c.swap_with_default();
            assert_eq!(taken, Chip::default());
            assert_eq!(c, Chip::default());
        }
    }
    #[test]
    fn swap_with_default_moves_the_callbacks_out_of_self() {
        let mut c = Chip::create(AzString::from("t"))
            .with_on_remove(RefAny::new(7u32), remove_cb(record_remove))
            .with_on_click(RefAny::new(8u32), click_cb(record_click));
        let taken = c.swap_with_default();
        assert!(
            taken.chip_state.on_remove.is_some(),
            "the remove callback moves out"
        );
        assert!(
            taken.chip_state.on_click.is_some(),
            "the click callback moves out"
        );
        assert!(
            c.chip_state.on_remove.is_none(),
            "the reset chip must not keep a reference to the old callback"
        );
        assert!(c.chip_state.on_click.is_none());
        assert!(!c.removable);
    }
    #[test]
    fn swap_with_default_survives_a_huge_label_and_repeated_swaps() {
        let long = "x".repeat(100_000);
        let mut c = Chip::with_kind(AzString::from(long.clone()), ChipKind::Success);
        for round in 0..10 {
            let taken = c.swap_with_default();
            if round == 0 {
                assert_eq!(
                    taken.label.len(),
                    long.len(),
                    "the long label was truncated"
                );
                assert_eq!(taken.kind, ChipKind::Success);
            } else {
                assert_eq!(
                    taken,
                    Chip::default(),
                    "round {round}: the emptied chip is not a default"
                );
            }
            assert_eq!(
                c,
                Chip::default(),
                "round {round}: what was left behind is not a default"
            );
        }
    }
    #[test]
    fn swap_with_default_preserves_a_hidden_state_on_the_returned_chip() {
        let mut c = Chip::create(AzString::from("t")).with_removable(true);
        c.chip_state.inner.visible = false;
        let taken = c.swap_with_default();
        assert!(
            !taken.chip_state.inner.visible,
            "the removed state travels with the original"
        );
        assert!(
            c.chip_state.inner.visible,
            "the fresh chip left behind must be visible"
        );
    }
    // ------------------------------------------------------------------
    // Chip::dom
    // ------------------------------------------------------------------
    #[test]
    fn dom_of_a_plain_chip_is_a_container_with_one_inert_label() {
        for kind in ALL_KINDS {
            let chip = Chip::with_kind(AzString::from("tag"), kind);
            let expected = properties(&chip.resolved_container_style());
            let dom = chip.dom();
            assert!(
                dom.root.has_class("__azul-native-chip"),
                "{kind:?}: missing the widget class"
            );
            assert!(
                dom.root.get_callbacks().as_ref().is_empty(),
                "{kind:?}: a stateless chip must carry no container callback"
            );
            assert_eq!(
                inline_properties(&dom),
                expected,
                "{kind:?}: the pill lost its computed style"
            );
            let children = dom.children.as_ref();
            assert_eq!(children.len(), 1, "{kind:?}: no x without `removable`");
            let label = &children[0];
            assert!(label.root.has_class("__azul-native-chip-label"));
            assert_eq!(
                text_of(label),
                Some("tag"),
                "{kind:?}: the label was mangled"
            );
            assert!(
                label.root.get_callbacks().as_ref().is_empty(),
                "{kind:?}: no on_click means no handler on the label"
            );
            assert!(
                label.root.get_tab_index().is_none(),
                "{kind:?}: an inert label must not be keyboard-focusable"
            );
            assert_eq!(
                label.children.as_ref().len(),
                1,
                "{kind:?}: the label is a <p> wrapping exactly one bare text node"
            );
        }
    }
    #[test]
    fn dom_of_a_removable_chip_appends_a_focusable_x() {
        let dom = Chip::create(AzString::from("tag"))
            .with_removable(true)
            .dom();
        let children = dom.children.as_ref();
        assert_eq!(children.len(), 2, "[label, remove]");
        let remove = &children[1];
        assert!(remove.root.has_class("__azul-native-chip-remove"));
        assert_eq!(
            text_of(remove),
            Some("\u{00D7}"),
            "the remove glyph is U+00D7 MULTIPLICATION SIGN, not an ASCII 'x'"
        );
        assert!(
            matches!(remove.root.get_tab_index(), Some(TabIndex::Auto)),
            "the x must be keyboard-reachable"
        );
        let callbacks = remove.root.get_callbacks();
        assert_eq!(callbacks.as_ref().len(), 1, "exactly one remove handler");
        let cb = &callbacks.as_ref()[0];
        assert!(matches!(
            &cb.event,
            EventFilter::Hover(HoverEventFilter::Click)
        ));
        assert_eq!(cb.callback.cb, default_on_chip_remove as usize);
        assert!(matches!(&cb.callback.ctx, OptionRefAny::None));
        // The container itself must stay handler-free, or a click on the x would
        // bubble and double-fire.
        assert!(dom.root.get_callbacks().as_ref().is_empty());
    }
    #[test]
    fn dom_attaches_the_click_handler_to_the_label_not_the_container() {
        // Documented rationale: a container-level MouseUp would also fire when the
        // x (a child) is clicked, double-firing alongside on_remove.
        let dom = Chip::create(AzString::from("tag"))
            .with_on_click(RefAny::new(0u8), click_cb(record_click))
            .dom();
        assert!(
            dom.root.get_callbacks().as_ref().is_empty(),
            "the pill container must never carry the click handler"
        );
        let label = &dom.children.as_ref()[0];
        assert!(
            matches!(label.root.get_tab_index(), Some(TabIndex::Auto)),
            "a clickable label must be keyboard-reachable"
        );
        let callbacks = label.root.get_callbacks();
        assert_eq!(callbacks.as_ref().len(), 1, "exactly one click handler");
        let cb = &callbacks.as_ref()[0];
        assert!(matches!(
            &cb.event,
            EventFilter::Hover(HoverEventFilter::Click)
        ));
        assert_eq!(cb.callback.cb, default_on_chip_click as usize);
        assert!(matches!(&cb.callback.ctx, OptionRefAny::None));
    }
    #[test]
    fn dom_gives_the_label_and_the_x_one_shared_state() {
        // The doc promises both handlers observe the same `ChipState`.
        let dom = Chip::create(AzString::from("tag"))
            .with_removable(true)
            .with_on_click(RefAny::new(0u8), click_cb(record_click))
            .dom();
        let children = dom.children.as_ref();
        assert_eq!(children.len(), 2);
        let mut label_state = children[0].root.get_callbacks().as_ref()[0].refany.clone();
        let mut remove_state = children[1].root.get_callbacks().as_ref()[0].refany.clone();
        assert_eq!(
            label_state, remove_state,
            "the two handlers must share one state RefAny"
        );
        // ...and prove it is genuinely shared, not merely equal.
        {
            let mut w = label_state
                .downcast_mut::<ChipStateWrapper>()
                .expect("the label payload must be a ChipStateWrapper");
            w.inner.visible = false;
        }
        assert!(
            !wrapper_visible(&mut remove_state),
            "a write through the label's handle must be visible through the x's handle"
        );
    }
    #[test]
    fn dom_of_a_removable_chip_without_on_click_leaves_the_label_inert() {
        let dom = Chip::create(AzString::from("tag"))
            .with_removable(true)
            .dom();
        let label = &dom.children.as_ref()[0];
        assert!(
            label.root.get_callbacks().as_ref().is_empty(),
            "removable alone must not make the label clickable"
        );
        assert!(label.root.get_tab_index().is_none());
    }
    #[test]
    fn dom_preserves_adversarial_labels_verbatim() {
        for s in adversarial_strings() {
            let dom = Chip::create(AzString::from(s.clone()))
                .with_removable(true)
                .dom();
            let label = &dom.children.as_ref()[0];
            let t = text_of(label).expect("the label must be a text node");
            assert_eq!(t, s.as_str(), "the label changed on its way into the DOM");
            assert_eq!(t.len(), s.len(), "byte length changed (NUL truncation?)");
            assert!(dom.root.has_class("__azul-native-chip"));
            // A label that *is* the remove glyph must not be confusable with the x:
            // the two are told apart by class, never by text.
            let remove = &dom.children.as_ref()[1];
            assert!(label.root.has_class("__azul-native-chip-label"));
            assert!(remove.root.has_class("__azul-native-chip-remove"));
            assert!(!label.root.has_class("__azul-native-chip-remove"));
        }
    }
    #[test]
    fn dom_does_not_emit_the_kind_class() {
        // Current behaviour, pinned: `ChipKind::class_name()` is never applied to
        // the DOM — the container only carries the generic container class, and
        // the kind travels as inline style instead.
        for kind in ALL_KINDS {
            let dom = Chip::with_kind(AzString::from("t"), kind)
                .with_removable(true)
                .dom();
            assert!(dom.root.has_class("__azul-native-chip"));
            assert!(
                !dom.root.has_class(kind.class_name()),
                "{kind:?}: the kind class is not emitted (kind is carried inline)"
            );
        }
    }
    #[test]
    fn dom_renders_the_kind_the_chip_was_last_set_to() {
        // `dom()` consumes the *cached* style, so a `set_kind` that forgot to
        // recompute would paint the previous colour here and nowhere else.
        for kind in ALL_KINDS {
            let mut chip = Chip::create(AzString::from_const_str("t"));
            chip.set_kind(ChipKind::Danger);
            chip.set_kind(kind);
            let expected = properties(&build_chip_style(kind));
            assert_eq!(
                inline_properties(&chip.dom()),
                expected,
                "{kind:?}: the DOM does not show the current kind"
            );
        }
    }
    #[test]
    fn from_chip_for_dom_is_exactly_dom() {
        for kind in ALL_KINDS {
            let chip = Chip::with_kind(AzString::from_const_str("ok"), kind).with_removable(true);
            let via_into: Dom = chip.clone().into();
            let via_dom = chip.dom();
            assert_eq!(
                inline_properties(&via_into),
                inline_properties(&via_dom),
                "{kind:?}: `From` diverges from `dom()`"
            );
            assert_eq!(
                via_into.root.get_node_type(),
                via_dom.root.get_node_type(),
                "{kind:?}: `From` built a different node"
            );
            assert_eq!(
                via_into.children.as_ref().len(),
                via_dom.children.as_ref().len()
            );
        }
    }
    #[test]
    fn dom_flattens_to_the_hierarchy_the_remove_handler_expects() {
        // `default_on_chip_remove` hard-codes "parent of the hit node is the
        // container". That only holds while the x is a *direct* child.
        let styled = removable_styled_dom();
        let hierarchy = styled.node_hierarchy.as_ref();
        assert_eq!(
            hierarchy.len(),
            5,
            "container(0), label <p>(1)+text(2), remove <p>(3)+text(4)"
        );
        assert_eq!(
            hierarchy[REMOVE_NODE].parent_id(),
            Some(NodeId::new(0)),
            "the x's parent must be the pill container"
        );
        assert_eq!(hierarchy[0].parent_id(), None, "the container is the root");
    }
    // ------------------------------------------------------------------
    // default_on_chip_remove
    // ------------------------------------------------------------------
    #[test]
    fn remove_hides_the_container_and_flips_visible() {
        let mut data = RefAny::new(ChipStateWrapper::default());
        // REMOVE_NODE == the x <p>, its parent (node 0) is the container
        let (update, changes) = run_remove(Some(removable_styled_dom()), REMOVE_NODE, data.clone());
        assert_eq!(update, Update::DoNothing, "no user callback -> DoNothing");
        assert_eq!(
            display_writes(&changes),
            alloc::vec![(0usize, LayoutDisplay::None)],
            "the *container* (not the x) must be hidden"
        );
        assert_eq!(changes.len(), 1, "exactly one restyle per click");
        assert!(!wrapper_visible(&mut data), "state must flip to hidden");
    }
    #[test]
    fn remove_invokes_the_user_callback_with_the_already_flipped_state() {
        let (mut data, mut log) = state_with_remove_log();
        let (update, changes) = run_remove(Some(removable_styled_dom()), REMOVE_NODE, data.clone());
        assert_eq!(
            update,
            Update::RefreshDom,
            "the user callback's Update is returned"
        );
        assert_eq!(
            log_calls(&mut log),
            alloc::vec![false],
            "the callback must see `visible == false` (already removed)"
        );
        assert!(!wrapper_visible(&mut data));
        assert_eq!(
            display_writes(&changes),
            alloc::vec![(0usize, LayoutDisplay::None)],
            "the container is hidden even after a user callback ran"
        );
    }
    #[test]
    fn remove_twice_is_idempotent() {
        let (mut data, mut log) = state_with_remove_log();
        for _ in 0..2 {
            let (update, changes) =
                run_remove(Some(removable_styled_dom()), REMOVE_NODE, data.clone());
            assert_eq!(update, Update::RefreshDom);
            assert_eq!(
                display_writes(&changes),
                alloc::vec![(0usize, LayoutDisplay::None)]
            );
        }
        assert!(
            !wrapper_visible(&mut data),
            "a second remove must not un-hide"
        );
        assert_eq!(
            log_calls(&mut log),
            alloc::vec![false, false],
            "each click fires the callback exactly once, always with visible == false"
        );
    }
    #[test]
    fn remove_on_a_root_hit_node_is_a_noop() {
        // node 0 has no parent -> there is no container to hide, and the early
        // return happens *before* the state is touched
        let mut data = RefAny::new(ChipStateWrapper::default());
        let (update, changes) = run_remove(Some(removable_styled_dom()), 0, data.clone());
        assert_eq!(update, Update::DoNothing);
        assert!(
            changes.is_empty(),
            "nothing may be restyled without a parent"
        );
        assert!(wrapper_visible(&mut data), "state must not flip");
    }
    #[test]
    fn remove_with_a_stale_hit_node_is_a_noop() {
        // node 999 does not exist in the 3-node fixture
        let mut data = RefAny::new(ChipStateWrapper::default());
        let (update, changes) = run_remove(Some(removable_styled_dom()), 999, data.clone());
        assert_eq!(update, Update::DoNothing);
        assert!(changes.is_empty());
        assert!(wrapper_visible(&mut data));
    }
    #[test]
    fn remove_without_any_layout_result_is_a_noop() {
        let mut data = RefAny::new(ChipStateWrapper::default());
        let (update, changes) = run_remove(None, REMOVE_NODE, data.clone());
        assert_eq!(update, Update::DoNothing);
        assert!(changes.is_empty());
        assert!(wrapper_visible(&mut data), "state must not flip");
    }
    #[test]
    fn remove_with_a_foreign_payload_is_a_noop() {
        // the callback-bearing node carries a RefAny of the *wrong* type
        let data = RefAny::new(0xdead_beef_u64);
        let (update, changes) = run_remove(Some(removable_styled_dom()), REMOVE_NODE, data.clone());
        assert_eq!(update, Update::DoNothing);
        assert!(
            changes.is_empty(),
            "a foreign payload must not hide the container"
        );
    }
    #[test]
    fn remove_fired_from_the_label_still_hides_the_container() {
        // Current behaviour, pinned: the handler trusts its wiring — it hides
        // whatever the hit node's parent is and never checks that the hit node is
        // actually the x. Firing it from the label <p> therefore hides the
        // container just the same.
        let mut data = RefAny::new(ChipStateWrapper::default());
        let (update, changes) = run_remove(Some(removable_styled_dom()), LABEL_NODE, data.clone());
        assert_eq!(update, Update::DoNothing);
        assert_eq!(
            display_writes(&changes),
            alloc::vec![(0usize, LayoutDisplay::None)]
        );
        assert!(!wrapper_visible(&mut data));
    }
    #[test]
    fn remove_holds_the_state_borrow_across_the_user_callback() {
        // The handler invokes the user callback while its own `downcast_mut` on
        // the state is still live. A user callback that re-enters the *same*
        // state `RefAny` is therefore refused — it must get `None` back rather
        // than a second aliasing borrow (or a panic).
        //
        // NOTE: probe <-> state form a RefAny reference cycle, so this fixture
        // leaks. That is deliberate and harmless for a single test.
        let mut probe = RefAny::new(ReentrantProbe {
            state: RefAny::new(0u8),
            saw_state: Some(true),
            calls: 0,
        });
        let state = RefAny::new(ChipStateWrapper {
            inner: ChipState { visible: true },
            on_remove: Some(ChipOnRemove {
                callback: remove_cb(probe_state_reentrantly),
                refany: probe.clone(),
            })
            .into(),
            on_click: OptionChipOnClick::None,
        });
        {
            let mut p = probe
                .downcast_mut::<ReentrantProbe>()
                .expect("ReentrantProbe");
            p.state = state.clone();
        }
        let (update, changes) =
            run_remove(Some(removable_styled_dom()), REMOVE_NODE, state.clone());
        assert_eq!(update, Update::DoNothing);
        assert_eq!(
            display_writes(&changes),
            alloc::vec![(0usize, LayoutDisplay::None)]
        );
        let p = probe
            .downcast_ref::<ReentrantProbe>()
            .expect("ReentrantProbe");
        assert_eq!(p.calls, 1, "the user callback must have run exactly once");
        assert_eq!(
            p.saw_state, None,
            "a re-entrant read of the state must be refused, not aliased"
        );
    }
    #[test]
    fn remove_end_to_end_through_the_real_dom_payload() {
        // Take the *actual* RefAny the widget wired into its x and drive the
        // *actual* handler the widget registered against it.
        let chip = Chip::create(AzString::from("bye")).with_removable(true);
        let dom = chip.dom();
        let remove = &dom.children.as_ref()[1];
        let entry = &remove.root.get_callbacks().as_ref()[0];
        assert_eq!(entry.callback.cb, default_on_chip_remove as usize);
        let mut payload = entry.refany.clone();
        let styled = StyledDom::create_from_dom(dom);
        let (update, changes) = run_remove(Some(styled), REMOVE_NODE, payload.clone());
        assert_eq!(update, Update::DoNothing);
        assert_eq!(
            display_writes(&changes),
            alloc::vec![(0usize, LayoutDisplay::None)]
        );
        assert!(
            !wrapper_visible(&mut payload),
            "the state living in the DOM must be flipped to hidden"
        );
    }
    // ------------------------------------------------------------------
    // default_on_chip_click
    // ------------------------------------------------------------------
    #[test]
    fn click_without_a_user_callback_is_a_noop() {
        let mut data = RefAny::new(ChipStateWrapper::default());
        let (update, changes) = run_click(Some(removable_styled_dom()), LABEL_NODE, data.clone());
        assert_eq!(update, Update::DoNothing);
        assert!(changes.is_empty(), "a click must never restyle anything");
        assert!(wrapper_visible(&mut data), "a click must not hide the chip");
    }
    #[test]
    fn click_invokes_the_user_callback_with_the_current_state() {
        let (mut data, mut log) = state_with_click_log(true);
        let (update, changes) = run_click(Some(removable_styled_dom()), LABEL_NODE, data.clone());
        assert_eq!(
            update,
            Update::RefreshDom,
            "the user callback's Update is returned"
        );
        assert_eq!(
            log_calls(&mut log),
            alloc::vec![true, true],
            "the callback must see the *unmodified* state (still visible)"
        );
        assert!(
            wrapper_visible(&mut data),
            "on_click must not flip `visible`"
        );
        assert!(
            changes.is_empty(),
            "on_click must not write any CSS property"
        );
    }
    #[test]
    fn click_reports_a_hidden_chip_as_hidden() {
        // The label and the x share one state, so a click after a remove must
        // observe `visible == false`.
        let (data, mut log) = state_with_click_log(false);
        let (update, _) = run_click(Some(removable_styled_dom()), LABEL_NODE, data);
        assert_eq!(update, Update::RefreshDom);
        assert_eq!(log_calls(&mut log), alloc::vec![false, false]);
    }
    #[test]
    fn click_does_not_need_the_node_hierarchy() {
        // Unlike the remove handler, `default_on_chip_click` never walks the tree:
        // it must still fire with no layout result at all, and from the root node.
        let (data, mut log) = state_with_click_log(true);
        let (update, changes) = run_click(None, 0, data);
        assert_eq!(
            update,
            Update::RefreshDom,
            "a missing layout result must not suppress on_click"
        );
        assert!(changes.is_empty());
        assert_eq!(log_calls(&mut log), alloc::vec![true, true]);
    }
    #[test]
    fn click_with_a_foreign_payload_is_a_noop() {
        let data = RefAny::new(0xdead_beef_u64);
        let (update, changes) = run_click(Some(removable_styled_dom()), LABEL_NODE, data);
        assert_eq!(update, Update::DoNothing);
        assert!(changes.is_empty());
    }
    #[test]
    fn click_twice_is_stable_and_never_mutates_the_state() {
        let (mut data, mut log) = state_with_click_log(true);
        for _ in 0..3 {
            let (update, changes) =
                run_click(Some(removable_styled_dom()), LABEL_NODE, data.clone());
            assert_eq!(update, Update::RefreshDom);
            assert!(changes.is_empty());
        }
        assert!(
            wrapper_visible(&mut data),
            "repeated clicks must not hide the chip"
        );
        assert_eq!(
            log_calls(&mut log).len(),
            6,
            "each click fires the callback exactly once"
        );
    }
    #[test]
    fn click_end_to_end_through_the_real_dom_payload() {
        let log = RefAny::new(StateLog { calls: Vec::new() });
        let mut log_handle = log.clone();
        let chip = Chip::create(AzString::from("tag"))
            .with_removable(true)
            .with_on_click(log, click_cb(record_click));
        let dom = chip.dom();
        let label = &dom.children.as_ref()[0];
        let entry = &label.root.get_callbacks().as_ref()[0];
        assert_eq!(entry.callback.cb, default_on_chip_click as usize);
        let mut payload = entry.refany.clone();
        let styled = StyledDom::create_from_dom(dom);
        let (update, changes) = run_click(Some(styled), LABEL_NODE, payload.clone());
        assert_eq!(update, Update::RefreshDom);
        assert!(changes.is_empty());
        assert_eq!(log_calls(&mut log_handle), alloc::vec![true, true]);
        assert!(
            wrapper_visible(&mut payload),
            "the shared state must be untouched by a click"
        );
    }
    #[test]
    fn remove_then_click_through_the_shared_dom_state_sees_the_hidden_chip() {
        // The end-to-end consequence of the shared `RefAny`: once the x has been
        // clicked, the label's handler observes `visible == false`.
        let log = RefAny::new(StateLog { calls: Vec::new() });
        let mut log_handle = log.clone();
        let chip = Chip::create(AzString::from("tag"))
            .with_removable(true)
            .with_on_click(log, click_cb(record_click));
        let dom = chip.dom();
        let click_payload = dom.children.as_ref()[0].root.get_callbacks().as_ref()[0]
            .refany
            .clone();
        let remove_payload = dom.children.as_ref()[1].root.get_callbacks().as_ref()[0]
            .refany
            .clone();
        let styled = StyledDom::create_from_dom(dom);
        let (_, changes) = run_remove(Some(styled), REMOVE_NODE, remove_payload);
        assert_eq!(
            display_writes(&changes),
            alloc::vec![(0usize, LayoutDisplay::None)]
        );
        let (update, _) = run_click(Some(removable_styled_dom()), LABEL_NODE, click_payload);
        assert_eq!(update, Update::RefreshDom);
        assert_eq!(
            log_calls(&mut log_handle),
            alloc::vec![false, false],
            "after the x was clicked, the label handler must see a hidden chip"
        );
    }
}