bevy_extended_ui 1.7.0

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

use crate::html::HtmlStyle;
use crate::old::registry::UiRegistry;
use crate::styles::{CssClass, CssSource, Style, TagName};
use crate::widgets::controls::choice_box::ChoiceLayoutBoxBase;
use crate::widgets::div::DivContentRoot;
use crate::widgets::widget_util::{
    apply_wheel_scroll_events_for_root, can_scroll_axis, find_owner_with_scroll_and_bar,
    max_scroll_for_extents, set_scrollbar_display_and_visibility,
};
use crate::widgets::{
    ActiveScrollTarget, BindToID, Body, Div, Scrollbar, UIGenID, UIWidgetState, WidgetId,
    WidgetKind,
};
use crate::{CurrentWidgetState, ExtendedUiConfiguration};
use bevy::camera::visibility::RenderLayers;
use bevy::input::mouse::MouseWheel;
use bevy::prelude::*;
use bevy::ui::ScrollPosition;

/// Marker component for the internal body node.
#[derive(Component)]
pub struct BodyBase;

/// Marker for the scroll content node inside a body.
#[derive(Component)]
pub struct BodyScrollContent;

/// Component storing the root content entity for a body.
#[derive(Component, Deref)]
pub struct BodyContentRoot(pub Entity);

/// Component storing the vertical scrollbar entity for a body.
#[derive(Component, Deref)]
pub struct BodyScrollbar(pub Entity);

/// Component storing the horizontal scrollbar entity for a body.
#[derive(Component, Deref)]
pub struct BodyScrollbarH(pub Entity);

/// Marks which body owns a scroll-content subtree.
#[derive(Component, Deref)]
struct BodyContentOwner(Entity);

/// Marks which body owns a scrollbar overlay.
#[derive(Component, Deref)]
struct BodyScrollbarOwner(Entity);

/// Tracks hover counts for body widgets.
#[derive(Resource, Default)]
pub struct HoveredBodyTracker {
    pub body_ref: HashMap<Entity, u32>,
    pub last_body: Option<Entity>,
}

/// Returns true for dialog overlays/panels that must not be nested into scroll-content.
fn is_dialog_overlay_node(tag_opt: Option<&TagName>, class_opt: Option<&CssClass>) -> bool {
    if let Some(tag) = tag_opt {
        let tag_name = tag.0.trim();
        if tag_name.eq_ignore_ascii_case("dialog")
            || tag_name.eq_ignore_ascii_case("dialog-overlay")
        {
            return true;
        }
    }

    class_opt.is_some_and(|classes| {
        classes.0.iter().any(|class_name| {
            class_name == "dialog-widget"
                || class_name == "dialog-renderer-bevy-app"
                || class_name == "dialog-overlay"
        })
    })
}

/// Plugin that wires up body widget behavior.
pub struct BodyWidget;

impl Plugin for BodyWidget {
    /// Registers systems for body widget setup.
    fn build(&self, app: &mut App) {
        app.init_resource::<HoveredBodyTracker>();
        app.init_resource::<ActiveScrollTarget>();
        app.add_systems(
            Update,
            (
                internal_node_creation_system,
                ensure_body_scroll_structure,
                route_hover_from_pointer_messages,
                sync_body_content_from_scrollbar,
                handle_body_scroll_wheel,
                sync_body_scrollbar_from_content,
            )
                .chain(),
        );
    }
}

/// Spawns internal Bevy UI nodes for body widgets.
fn internal_node_creation_system(
    mut commands: Commands,
    mut query: Query<
        (Entity, &Body, Option<&CssSource>, Option<&mut HtmlStyle>),
        (With<Body>, Without<BodyBase>),
    >,
    existing_bodies: Query<&ZIndex, With<BodyBase>>,
    config: Res<ExtendedUiConfiguration>,
    ui_registry: Option<Res<UiRegistry>>,
) {
    let layer = config.render_layers.first().unwrap_or(&1);

    let ui_order = ui_registry.as_ref().and_then(|reg| reg.current.as_ref());
    let max_z_index = existing_bodies
        .iter()
        .map(|z_index| z_index.0)
        .max()
        .unwrap_or(-1);
    let mut next_z_index = max_z_index + 1;

    for (entity, body, source_opt, html_style) in query.iter_mut() {
        let css_source = source_opt.cloned().unwrap_or_default();

        let mut html_id = String::new();
        if let Some(id) = body.html_key.clone() {
            html_id = id;
        }

        let z_index = if html_id.is_empty() {
            0
        } else if ui_order
            .and_then(|names| names.iter().position(|name| name == &html_id))
            .is_some()
        {
            let assigned = next_z_index;
            next_z_index += 1;
            assigned
        } else {
            0
        };

        if let Some(mut inline_style) = html_style {
            inline_style.0.z_index = Some(z_index);
        } else {
            let mut style = Style::default();
            style.z_index = Some(z_index);
            commands.entity(entity).insert(HtmlStyle(style));
        }

        commands
            .entity(entity)
            .insert((
                Name::new(format!("Body-{}-{}", html_id, body.entry)),
                Node::default(),
                WidgetId {
                    id: body.entry,
                    kind: WidgetKind::Body,
                },
                BackgroundColor::default(),
                ImageNode::default(),
                ZIndex(z_index),
                Pickable::default(),
                css_source,
                TagName("body".to_string()),
                RenderLayers::layer(*layer),
                BodyBase,
            ))
            .observe(on_internal_click)
            .observe(on_internal_cursor_entered)
            .observe(on_internal_cursor_leave);
    }
}

/// Ensures the body has scroll content and scrollbar nodes when needed.
pub fn ensure_body_scroll_structure(
    mut commands: Commands,
    mut body_q: Query<
        (
            Entity,
            &Body,
            &UIGenID,
            &mut Node,
            Option<&mut HtmlStyle>,
            Option<&Children>,
            Option<&CssSource>,
            Option<&CssClass>,
            Option<&RenderLayers>,
            Option<&BodyContentRoot>,
            Option<&BodyScrollbar>,
            Option<&BodyScrollbarH>,
        ),
        (With<Body>, With<BodyBase>, Without<BodyScrollContent>),
    >,
    parent_q: Query<&ChildOf>,
    mut content_node_q: Query<&mut Node, (With<BodyScrollContent>, Without<BodyBase>)>,
    mut content_style_q: Query<&mut HtmlStyle, (With<BodyScrollContent>, Without<Body>)>,
    content_children_q: Query<&Children, With<BodyScrollContent>>,
    child_meta_q: Query<(Option<&TagName>, Option<&CssClass>)>,
    has_scroll_pos_q: Query<(), With<ScrollPosition>>,
) {
    for (
        body_entity,
        body,
        ui_id,
        mut body_node,
        body_inline_style_opt,
        children_opt,
        source_opt,
        class_opt,
        layers_opt,
        root_opt,
        sb_y_opt,
        sb_x_opt,
    ) in body_q.iter_mut()
    {
        let mut wants_scroll_y = body_node.overflow.y == OverflowAxis::Scroll || sb_y_opt.is_some();
        let mut wants_scroll_x = body_node.overflow.x == OverflowAxis::Scroll || sb_x_opt.is_some();
        let body_layout_template = body_node.clone();

        if let Some(root) = root_opt {
            if let Ok(content_node) = content_node_q.get(**root) {
                wants_scroll_y |= content_node.overflow.y == OverflowAxis::Scroll;
                wants_scroll_x |= content_node.overflow.x == OverflowAxis::Scroll;
            }
        }

        if !(wants_scroll_y || wants_scroll_x || root_opt.is_some()) {
            continue;
        }

        // 1) Ensure scroll-content child
        let content_entity = if let Some(root) = root_opt {
            **root
        } else {
            let css_source = source_opt.cloned().unwrap_or_default();

            let mut content_node = body_layout_template.clone();
            content_node.width = Val::Percent(100.0);
            content_node.height = Val::Percent(100.0);
            content_node.position_type = PositionType::Relative;
            content_node.left = Val::Auto;
            content_node.right = Val::Auto;
            content_node.top = Val::Auto;
            content_node.bottom = Val::Auto;
            content_node.overflow.y = if wants_scroll_y {
                OverflowAxis::Scroll
            } else {
                OverflowAxis::Hidden
            };
            content_node.overflow.x = if wants_scroll_x {
                OverflowAxis::Scroll
            } else {
                OverflowAxis::Hidden
            };

            let mut classes = class_opt.map(|c| c.0.clone()).unwrap_or_default();
            classes.push("body-scroll-content".to_string());

            let content_entity = commands
                .spawn((
                    Name::new(format!("Body-ScrollContent-{}", body.entry)),
                    content_node,
                    css_source,
                    CssClass(classes),
                    TagName("div".to_string()),
                    BodyScrollContent,
                    BodyContentOwner(body_entity),
                    BindToID(ui_id.get()),
                    Visibility::Inherited,
                    InheritedVisibility::default(),
                    Transform::default(),
                    GlobalTransform::default(),
                    ScrollPosition::default(),
                    UIWidgetState::default(),
                    Pickable::default(),
                ))
                .id();

            if let Some(layers) = layers_opt {
                commands.entity(content_entity).insert(layers.clone());
            }

            commands.entity(body_entity).add_child(content_entity);
            commands
                .entity(body_entity)
                .insert(BodyContentRoot(content_entity));

            content_entity
        };
        commands
            .entity(content_entity)
            .insert(BindToID(ui_id.get()));

        let mut sb_y_entity = sb_y_opt.map(|s| **s);
        let mut sb_x_entity = sb_x_opt.map(|s| **s);

        // 2a) Ensure vertical scrollbar
        if wants_scroll_y && sb_y_opt.is_none() {
            let css_source = source_opt.cloned().unwrap_or_default();

            let mut sb_node = Node::default();
            sb_node.position_type = PositionType::Absolute;
            sb_node.left = Val::Auto;
            sb_node.right = Val::Px(0.0);
            sb_node.top = Val::Px(0.0);
            sb_node.bottom = Val::Px(0.0);
            sb_node.width = Val::Px(12.0);

            let sb_entity = commands
                .spawn((
                    Name::new(format!("Body-Scrollbar-{}", body.entry)),
                    sb_node,
                    css_source.clone(),
                    CssClass(vec![
                        "scrollbar".to_string(),
                        "scrollbar-vertical".to_string(),
                    ]),
                    TagName("scroll".to_string()),
                    BodyScrollbarOwner(body_entity),
                    Scrollbar {
                        vertical: true,
                        min: 0.0,
                        max: 0.0,
                        value: 0.0,
                        step: 1.0,
                        entity: Some(content_entity),
                        ..default()
                    },
                    UIWidgetState::default(),
                    ZIndex(10),
                    ImageNode::default(),
                    BackgroundColor::default(),
                    BorderColor::default(),
                    Pickable::default(),
                ))
                .id();

            if let Some(layers) = layers_opt {
                commands.entity(sb_entity).insert(layers.clone());
            }

            commands.entity(body_entity).add_child(sb_entity);
            commands
                .entity(body_entity)
                .insert(BodyScrollbar(sb_entity));
            sb_y_entity = Some(sb_entity);
        }

        // 2b) Ensure horizontal scrollbar
        if wants_scroll_x && sb_x_opt.is_none() {
            let css_source = source_opt.cloned().unwrap_or_default();

            let mut sb_node = Node::default();
            sb_node.position_type = PositionType::Absolute;
            sb_node.left = Val::Px(0.0);
            sb_node.right = Val::Px(0.0);
            sb_node.bottom = Val::Px(0.0);
            sb_node.top = Val::Auto;
            sb_node.height = Val::Px(12.0);

            let sb_entity = commands
                .spawn((
                    Name::new(format!("Body-Scrollbar-H-{}", body.entry)),
                    sb_node,
                    css_source,
                    CssClass(vec![
                        "scrollbar".to_string(),
                        "scrollbar-horizontal".to_string(),
                        "scroll-horizontal".to_string(),
                    ]),
                    TagName("scroll".to_string()),
                    BodyScrollbarOwner(body_entity),
                    Scrollbar {
                        vertical: false,
                        min: 0.0,
                        max: 0.0,
                        value: 0.0,
                        step: 1.0,
                        entity: Some(content_entity),
                        ..default()
                    },
                    UIWidgetState::default(),
                    ZIndex(10),
                    ImageNode::default(),
                    BackgroundColor::default(),
                    BorderColor::default(),
                    Pickable::default(),
                ))
                .id();

            if let Some(layers) = layers_opt {
                commands.entity(sb_entity).insert(layers.clone());
            }

            commands.entity(body_entity).add_child(sb_entity);
            commands
                .entity(body_entity)
                .insert(BodyScrollbarH(sb_entity));
            sb_x_entity = Some(sb_entity);
        }

        // 3a) Move dialog overlays back to body root if they were previously nested in content.
        if let Ok(content_children) = content_children_q.get(content_entity) {
            for child in content_children.iter() {
                if let Ok((tag_opt, class_opt)) = child_meta_q.get(child)
                    && is_dialog_overlay_node(tag_opt, class_opt)
                {
                    commands.entity(child).set_parent_in_place(body_entity);
                }
            }
        }

        // 3b) Reparent regular body children under content (only if needed)
        if let Some(children) = children_opt {
            let sb_y = sb_y_opt.map(|s| **s);
            let sb_x = sb_x_opt.map(|s| **s);

            let list: Vec<Entity> = children.iter().clone().collect();
            for child in list {
                if child == content_entity {
                    continue;
                }
                if Some(child) == sb_y || Some(child) == sb_x {
                    continue;
                }
                if let Ok((tag_opt, class_opt)) = child_meta_q.get(child)
                    && is_dialog_overlay_node(tag_opt, class_opt)
                {
                    continue;
                }

                if let Ok(parent) = parent_q.get(child) {
                    if parent.parent() == content_entity {
                        continue;
                    }
                }

                commands.entity(child).set_parent_in_place(content_entity);
            }
        }

        // 3c) Ensure the body's direct children order keeps scroll-content first.
        if let Some(children) = children_opt {
            let mut desired = Vec::with_capacity(children.len());
            desired.push(content_entity);
            if let Some(sb) = sb_y_entity {
                desired.push(sb);
            }
            if let Some(sb) = sb_x_entity {
                desired.push(sb);
            }
            for child in children.iter().clone() {
                if child == content_entity
                    || Some(child) == sb_y_opt.map(|s| **s)
                    || Some(child) == sb_x_opt.map(|s| **s)
                {
                    continue;
                }
                if let Ok((tag_opt, class_opt)) = child_meta_q.get(child)
                    && is_dialog_overlay_node(tag_opt, class_opt)
                {
                    desired.push(child);
                }
            }

            if !children.iter().clone().eq(desired.iter().copied()) {
                commands.entity(body_entity).add_children(&desired);
            }
        }

        // Wrapper clips but does not scroll.
        body_node.overflow.y = OverflowAxis::Clip;
        body_node.overflow.x = OverflowAxis::Clip;

        // Ensure content scroll flags stay aligned.
        if let Ok(mut node) = content_node_q.get_mut(content_entity) {
            *node = body_layout_template.clone();
            node.overflow.y = if wants_scroll_y {
                OverflowAxis::Scroll
            } else {
                OverflowAxis::Hidden
            };
            node.overflow.x = if wants_scroll_x {
                OverflowAxis::Scroll
            } else {
                OverflowAxis::Hidden
            };
            node.width = Val::Percent(100.0);
            node.height = Val::Percent(100.0);
            node.position_type = PositionType::Relative;
            node.left = Val::Auto;
            node.right = Val::Auto;
            node.top = Val::Auto;
            node.bottom = Val::Auto;
        }

        if has_scroll_pos_q.get(content_entity).is_err() {
            commands
                .entity(content_entity)
                .insert(ScrollPosition::default());
        }

        // Keep the outer body as a clipping wrapper; the inner content node is the scroll target.
        let body_clip_overflow = Overflow {
            x: OverflowAxis::Clip,
            y: OverflowAxis::Clip,
        };
        if let Some(mut inline_style) = body_inline_style_opt {
            inline_style.0.overflow = Some(body_clip_overflow);
        } else {
            let mut style = Style::default();
            style.overflow = Some(body_clip_overflow);
            commands.entity(body_entity).insert(HtmlStyle(style));
        }

        // Mirror effective scroll axes into inline style so later CSS passes cannot unset scrolling.
        let content_overflow = Overflow {
            x: if wants_scroll_x {
                OverflowAxis::Scroll
            } else {
                OverflowAxis::Hidden
            },
            y: if wants_scroll_y {
                OverflowAxis::Scroll
            } else {
                OverflowAxis::Hidden
            },
        };
        if let Ok(mut inline_style) = content_style_q.get_mut(content_entity) {
            inline_style.0.overflow = Some(content_overflow);
            inline_style.0.width = Some(Val::Percent(100.0));
            inline_style.0.height = Some(Val::Percent(100.0));
            inline_style.0.position_type = Some(PositionType::Relative);
            inline_style.0.left = Some(Val::Auto);
            inline_style.0.right = Some(Val::Auto);
            inline_style.0.top = Some(Val::Auto);
            inline_style.0.bottom = Some(Val::Auto);
        } else {
            let mut style = Style::default();
            style.overflow = Some(content_overflow);
            style.width = Some(Val::Percent(100.0));
            style.height = Some(Val::Percent(100.0));
            style.position_type = Some(PositionType::Relative);
            style.left = Some(Val::Auto);
            style.right = Some(Val::Auto);
            style.top = Some(Val::Auto);
            style.bottom = Some(Val::Auto);
            commands.entity(content_entity).insert(HtmlStyle(style));
        }

        commands.entity(content_entity).insert((
            Transform::default(),
            GlobalTransform::default(),
            Visibility::Inherited,
            InheritedVisibility::default(),
        ));
    }
}

/// Routes hover state to the owning body.
fn route_hover_from_pointer_messages(
    mut over: MessageReader<Pointer<Over>>,
    mut out: MessageReader<Pointer<Out>>,
    parents: Query<&ChildOf>,
    is_body_q: Query<(), With<Body>>,
    scroll_owner_q: Query<&BodyContentOwner, With<BodyScrollContent>>,
    sb_owner_q: Query<&BodyScrollbarOwner>,
    is_scroll_content_q: Query<(), With<BodyScrollContent>>,
    mut body_state_q: Query<&mut UIWidgetState, With<Body>>,
    mut hovered: ResMut<HoveredBodyTracker>,
) {
    for msg in over.read() {
        let Some(body) = find_owner_with_scroll_and_bar(
            msg.entity,
            &parents,
            &is_body_q,
            &is_scroll_content_q,
            &scroll_owner_q,
            &sb_owner_q,
        ) else {
            continue;
        };

        let d = hovered.body_ref.entry(body).or_insert(0);
        *d = d.saturating_add(1);
        hovered.last_body = Some(body);

        if let Ok(mut state) = body_state_q.get_mut(body) {
            state.hovered = true;
        }
    }

    for msg in out.read() {
        let Some(body) = find_owner_with_scroll_and_bar(
            msg.entity,
            &parents,
            &is_body_q,
            &is_scroll_content_q,
            &scroll_owner_q,
            &sb_owner_q,
        ) else {
            continue;
        };

        if let Some(d) = hovered.body_ref.get_mut(&body) {
            *d = d.saturating_sub(1);
            if *d == 0 {
                hovered.body_ref.remove(&body);

                if let Ok(mut state) = body_state_q.get_mut(body) {
                    state.hovered = false;
                }

                if hovered.last_body == Some(body) {
                    hovered.last_body = hovered.body_ref.keys().next().copied();
                }
            }
        }
    }
}

/// Wheel scroll uses the "last hovered body" and scrolls its BodyScrollContent on both axes.
pub fn handle_body_scroll_wheel(
    mut wheel_events: MessageReader<MouseWheel>,
    keyboard: Option<Res<ButtonInput<KeyCode>>>,
    active_scroll_target: Option<Res<ActiveScrollTarget>>,
    hovered: Res<HoveredBodyTracker>,
    body_q: Query<(Entity, &BodyContentRoot), With<Body>>,
    mut content_q: Query<(&Node, &ComputedNode, &mut ScrollPosition), With<BodyScrollContent>>,
    div_q: Query<(&UIWidgetState, &DivContentRoot), With<Div>>,
    div_content_q: Query<
        (&Node, &ComputedNode),
        (With<ScrollPosition>, Without<BodyScrollContent>),
    >,
    choice_overlay_q: Query<
        (&UIWidgetState, &Node, &ComputedNode, &Visibility),
        With<ChoiceLayoutBoxBase>,
    >,
    dialog_overlay_q: Query<(Option<&TagName>, Option<&CssClass>, &Visibility)>,
) {
    if active_scroll_target
        .as_ref()
        .is_some_and(|target| target.entity.is_some())
    {
        wheel_events.clear();
        return;
    }

    // Scrollable div under the pointer has priority over body scrolling.
    let has_hovered_scrollable_div = div_q.iter().any(|(state, root)| {
        if !state.hovered {
            return false;
        }

        let Ok((node, computed)) = div_content_q.get(**root) else {
            return false;
        };

        let inv_sf = computed.inverse_scale_factor.max(f32::EPSILON);
        let can_scroll_y = can_scroll_axis(
            node.overflow.y,
            computed.size().y,
            computed.content_size.y,
            inv_sf,
        );
        let can_scroll_x = can_scroll_axis(
            node.overflow.x,
            computed.size().x,
            computed.content_size.x,
            inv_sf,
        );

        can_scroll_x || can_scroll_y
    });
    if has_hovered_scrollable_div {
        return;
    }

    // Open ChoiceBox overlays should keep wheel focus while hovered.
    let has_hovered_scrollable_choice_overlay =
        choice_overlay_q
            .iter()
            .any(|(state, node, computed, visibility)| {
                if !state.hovered {
                    return false;
                }
                if !matches!(*visibility, Visibility::Visible | Visibility::Inherited) {
                    return false;
                }
                let inv_sf = computed.inverse_scale_factor.max(f32::EPSILON);
                let can_scroll_y = can_scroll_axis(
                    node.overflow.y,
                    computed.size().y,
                    computed.content_size.y,
                    inv_sf,
                );
                let can_scroll_x = can_scroll_axis(
                    node.overflow.x,
                    computed.size().x,
                    computed.content_size.x,
                    inv_sf,
                );

                can_scroll_x || can_scroll_y
            });
    if has_hovered_scrollable_choice_overlay {
        return;
    }

    // Block body wheel while any bevy-app dialog overlay is visible.
    let has_visible_dialog_overlay =
        dialog_overlay_q
            .iter()
            .any(|(tag_opt, class_opt, visibility)| {
                matches!(*visibility, Visibility::Visible | Visibility::Inherited)
                    && is_dialog_overlay_node(tag_opt, class_opt)
            });
    if has_visible_dialog_overlay {
        return;
    }

    let active_body = hovered
        .last_body
        .or_else(|| body_q.iter().next().map(|(entity, _)| entity));
    let Some(active_body) = active_body else {
        wheel_events.clear();
        return;
    };

    let Ok((_, root)) = body_q.get(active_body) else {
        return;
    };

    apply_wheel_scroll_events_for_root(
        **root,
        &mut wheel_events,
        keyboard.as_deref(),
        &mut content_q,
    );
}

/// Synchronizes body content scroll positions from scrollbar widget values.
fn sync_body_content_from_scrollbar(
    body_q: Query<
        (
            Option<&BodyContentRoot>,
            Option<&BodyScrollbar>,
            Option<&BodyScrollbarH>,
        ),
        With<Body>,
    >,
    content_q: Query<&ComputedNode, With<BodyScrollContent>>,
    scroll_q: Query<&Scrollbar>,
    mut target_pos_q: Query<&mut ScrollPosition, With<BodyScrollContent>>,
) {
    for (root_opt, sb_y_opt, sb_x_opt) in body_q.iter() {
        let Some(root) = root_opt else {
            continue;
        };
        let Ok(content_comp) = content_q.get(**root) else {
            continue;
        };
        let Ok(mut pos) = target_pos_q.get_mut(**root) else {
            continue;
        };

        let inv_sf = content_comp.inverse_scale_factor.max(f32::EPSILON);
        let viewport = content_comp.size() * inv_sf;
        let content = content_comp.content_size * inv_sf;

        if let Some(sb) = sb_y_opt {
            if let Ok(scrollbar) = scroll_q.get(**sb) {
                let viewport_h = viewport.y.max(1.0);
                let content_h = content.y.max(viewport_h);
                let max_scroll = max_scroll_for_extents(viewport_h, content_h);
                pos.y = scrollbar.value.clamp(0.0, max_scroll);
            }
        }

        if let Some(sb) = sb_x_opt {
            if let Ok(scrollbar) = scroll_q.get(**sb) {
                let viewport_w = viewport.x.max(1.0);
                let content_w = content.x.max(viewport_w);
                let max_scroll = max_scroll_for_extents(viewport_w, content_w);
                pos.x = scrollbar.value.clamp(0.0, max_scroll);
            }
        }
    }
}

/// Synchronizes body scrollbars from scrollable content.
fn sync_body_scrollbar_from_content(
    body_q: Query<
        (
            Option<&BodyContentRoot>,
            Option<&BodyScrollbar>,
            Option<&BodyScrollbarH>,
        ),
        With<Body>,
    >,
    content_q: Query<&ComputedNode, With<BodyScrollContent>>,
    mut scroll_q: Query<&mut Scrollbar>,
    target_pos_q: Query<&ScrollPosition, With<BodyScrollContent>>,
    mut sb_node_q: Query<&mut Node, With<Scrollbar>>,
    mut sb_vis_q: Query<&mut Visibility, With<Scrollbar>>,
) {
    for (root_opt, sb_y_opt, sb_x_opt) in body_q.iter() {
        let Some(root) = root_opt else {
            continue;
        };

        let Ok(content_comp) = content_q.get(**root) else {
            continue;
        };
        let Ok(scroll_pos) = target_pos_q.get(**root) else {
            continue;
        };

        let inv_sf = content_comp.inverse_scale_factor.max(f32::EPSILON);
        let viewport = content_comp.size() * inv_sf;
        let content = content_comp.content_size * inv_sf;

        // -------- Y --------
        if let Some(sb) = sb_y_opt {
            let viewport_h = viewport.y.max(1.0);
            let content_h = content.y.max(viewport_h);
            let max_scroll = max_scroll_for_extents(viewport_h, content_h);

            set_scrollbar_display_and_visibility(&mut sb_node_q, &mut sb_vis_q, sb, max_scroll);

            if let Ok(mut scrollbar) = scroll_q.get_mut(**sb) {
                scrollbar.entity = Some(**root);
                scrollbar.min = 0.0;
                scrollbar.max = max_scroll;
                scrollbar.viewport_extent = viewport_h;
                scrollbar.content_extent = content_h;
                scrollbar.value = scroll_pos.y.clamp(0.0, max_scroll);
            }
        }

        // -------- X --------
        if let Some(sb) = sb_x_opt {
            let viewport_w = viewport.x.max(1.0);
            let content_w = content.x.max(viewport_w);
            let max_scroll = max_scroll_for_extents(viewport_w, content_w);

            set_scrollbar_display_and_visibility(&mut sb_node_q, &mut sb_vis_q, sb, max_scroll);

            if let Ok(mut scrollbar) = scroll_q.get_mut(**sb) {
                scrollbar.entity = Some(**root);
                scrollbar.min = 0.0;
                scrollbar.max = max_scroll;
                scrollbar.viewport_extent = viewport_w;
                scrollbar.content_extent = content_w;
                scrollbar.value = scroll_pos.x.clamp(0.0, max_scroll);
            }
        }
    }
}

/// Event handler that activates when an internal body node is clicked.
fn on_internal_click(
    mut trigger: On<Pointer<Click>>,
    mut query: Query<(&mut UIWidgetState, &UIGenID), With<Body>>,
    mut current_widget_state: ResMut<CurrentWidgetState>,
) {
    if let Ok((mut state, gen_id)) = query.get_mut(trigger.event_target()) {
        state.focused = true;
        current_widget_state.widget_id = gen_id.0;
    }

    trigger.propagate(false);
}

/// Event handler for when the cursor enters an internal body node.
fn on_internal_cursor_entered(
    mut trigger: On<Pointer<Over>>,
    mut query: Query<&mut UIWidgetState, With<Body>>,
) {
    if let Ok(mut state) = query.get_mut(trigger.event_target()) {
        state.hovered = true;
    }

    trigger.propagate(false);
}

/// Event handler for when the cursor leaves an internal body node.
fn on_internal_cursor_leave(
    mut trigger: On<Pointer<Out>>,
    mut query: Query<&mut UIWidgetState, With<Body>>,
) {
    if let Ok(mut state) = query.get_mut(trigger.event_target()) {
        state.hovered = false;
    }

    trigger.propagate(false);
}