eguidev 0.0.2

AI-assisted development tooling and in-process instrumentation for egui apps
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//! Instrumentation and geometry recording module.
#[cfg(test)]
use std::cell::Cell;
use std::{
    cell::RefCell,
    panic::{self, AssertUnwindSafe},
    sync::Arc,
};

use egui::scroll_area::{ScrollAreaOutput, State};

use crate::{
    registry::Inner,
    types::{RoleState, WidgetLayout, WidgetRole, WidgetValue},
    ui_ext::DevScrollAreaExt,
    widget_registry::{WidgetMeta, record_widget},
};

thread_local! {
    pub(crate) static ACTIVE: RefCell<Option<Arc<Inner>>> = const { RefCell::new(None) };
}

#[cfg(test)]
thread_local! {
    static TEST_LAYOUT_CAPTURE_COUNT: Cell<usize> = const { Cell::new(0) };
}

pub fn swallow_panic(label: &'static str, f: impl FnOnce()) {
    if panic::catch_unwind(AssertUnwindSafe(f)).is_err() {
        eprintln!("eguidev: recovered panic in {label}");
    }
}

pub fn active_inner() -> Option<Arc<Inner>> {
    ACTIVE.with(|active| {
        active
            .try_borrow()
            .ok()
            .and_then(|guard| guard.as_ref().cloned())
    })
}

/// Record a widget with an explicit id and geometry only.
///
/// Prefer `DevUiExt` for standard widgets. Use `id` for custom widgets where metadata is
/// unnecessary.
pub fn id(
    ui: &mut egui::Ui,
    id: impl Into<String>,
    add: impl FnOnce(&mut egui::Ui) -> egui::Response,
) -> egui::Response {
    let response = add(ui);
    let visible = ui.is_visible() && ui.is_rect_visible(response.rect);
    track_response_full(
        id,
        &response,
        WidgetMeta {
            visible,
            ..Default::default()
        },
    );
    response
}

/// Record a widget with an explicit id and explicit metadata.
///
/// Use this when you need control over role/type/value/label metadata for custom widgets.
pub fn id_with_meta(
    ui: &mut egui::Ui,
    id: impl Into<String>,
    role: WidgetRole,
    label: Option<String>,
    value: Option<WidgetValue>,
    add: impl FnOnce(&mut egui::Ui) -> egui::Response,
) -> egui::Response {
    let response = add(ui);
    let Some(inner) = active_inner() else {
        return response;
    };
    let visible = ui.is_visible() && ui.is_rect_visible(response.rect);
    let layout = Some(capture_layout(ui, &response));
    let id = id.into();
    swallow_panic("id_with_meta", || {
        record_widget(
            &inner.widgets,
            id,
            &response,
            WidgetMeta {
                role,
                label,
                value,
                layout,
                visible,
                ..Default::default()
            },
        );
    });
    response
}

/// Track an already-created widget response with explicit metadata.
///
/// Use this when you have an `egui::Response` from a custom widget and cannot
/// wrap it with [`id`] or [`id_with_meta`]. Prefer the `DevUiExt` helpers or
/// the wrapping functions for standard widgets.
pub fn track_response_full(id: impl Into<String>, response: &egui::Response, meta: WidgetMeta) {
    let Some(inner) = active_inner() else {
        return;
    };
    let id = id.into();
    swallow_panic("track_response_full", || {
        record_widget(&inner.widgets, id, response, meta);
    });
}

/// RAII guard that pops a container scope when dropped.
pub struct ContainerGuard {
    inner: Option<Arc<Inner>>,
    viewport_id: egui::ViewportId,
}

impl Drop for ContainerGuard {
    fn drop(&mut self) {
        if let Some(inner) = &self.inner {
            inner.widgets.pop_container(self.viewport_id);
        }
    }
}

/// Begin a container scope with an explicit id.
pub fn begin_container(ui: &egui::Ui, id: impl Into<String>) -> ContainerGuard {
    let inner = active_inner();
    begin_container_with_inner(ui, inner, id)
}

fn begin_container_with_inner(
    ui: &egui::Ui,
    inner: Option<Arc<Inner>>,
    id: impl Into<String>,
) -> ContainerGuard {
    let viewport_id = ui.ctx().viewport_id();
    if let Some(inner) = inner.as_ref() {
        inner.widgets.push_container(viewport_id, id.into());
    }
    ContainerGuard { inner, viewport_id }
}

/// Run a closure within a container scope with an explicit id.
///
/// The container is registered as a widget so it is discoverable via `parent()` and
/// `children()`.
pub fn container<R>(
    ui: &mut egui::Ui,
    id: impl Into<String>,
    add_contents: impl FnOnce(&mut egui::Ui) -> R,
) -> R {
    let id = id.into();
    let inner = active_inner();
    // Run contents inside a scope to get a response, with the container on the stack
    // so children get the right parent_id.
    let output = ui.scope(|ui| {
        let _guard = begin_container_with_inner(ui, inner.clone(), id.clone());
        add_contents(ui)
    });
    let Some(inner) = inner else {
        return output.inner;
    };
    // Register the container widget after content, so its rect covers all children.
    // At this point the container has been popped, so the container's own parent_id
    // is correctly set to the enclosing scope.
    swallow_panic("container", || {
        record_widget(
            &inner.widgets,
            id,
            &output.response,
            WidgetMeta {
                visible: output.response.interact_rect.is_positive(),
                ..Default::default()
            },
        );
    });
    output.inner
}

/// Scroll area state tracker with one-shot offset jumps.
#[derive(Debug, Clone)]
pub struct ScrollAreaState {
    offset: egui::Vec2,
    pending_offset: Option<egui::Vec2>,
}

impl Default for ScrollAreaState {
    fn default() -> Self {
        Self {
            offset: egui::Vec2::ZERO,
            pending_offset: None,
        }
    }
}

impl ScrollAreaState {
    /// Create a new scroll area state tracker.
    pub fn new() -> Self {
        Self::default()
    }

    /// The most recent scroll offset after egui applied input.
    pub fn offset(&self) -> egui::Vec2 {
        self.offset
    }

    /// Request a one-shot jump to the given offset on the next frame.
    pub fn jump_to(&mut self, offset: egui::Vec2) {
        self.pending_offset = Some(offset);
    }

    /// Reset tracked state and request that the next frame jump back to the origin.
    pub fn reset(&mut self) {
        self.offset = egui::Vec2::ZERO;
        self.pending_offset = Some(egui::Vec2::ZERO);
    }

    /// Show a scroll area, record DevMCP metadata, and update the tracked offset.
    pub fn show<R>(
        &mut self,
        scroll_area: egui::ScrollArea,
        ui: &mut egui::Ui,
        id: impl Into<String>,
        add_contents: impl FnOnce(&mut egui::Ui) -> R,
    ) -> ScrollAreaOutput<R> {
        self.show_viewport(scroll_area, ui, id, |ui, _| add_contents(ui))
    }

    /// Show a scroll area with viewport access, record DevMCP metadata, and update the offset.
    pub fn show_viewport<R>(
        &mut self,
        scroll_area: egui::ScrollArea,
        ui: &mut egui::Ui,
        id: impl Into<String>,
        add_contents: impl FnOnce(&mut egui::Ui, egui::Rect) -> R,
    ) -> ScrollAreaOutput<R> {
        let requested_offset = self.pending_offset;
        let scroll_area = if let Some(offset) = requested_offset {
            scroll_area.scroll_offset(offset)
        } else {
            scroll_area
        };
        // Use our extension trait method
        let output = scroll_area.dev_show_viewport(ui, id, add_contents);
        let effective_offset = State::load(ui.ctx(), output.id)
            .map(|state| state.offset)
            .unwrap_or(output.state.offset);
        self.offset = effective_offset;
        self.pending_offset = requested_offset.filter(|offset| {
            let delta = effective_offset - *offset;
            delta.length_sq() > 0.25
        });
        output
    }
}

fn sanitize_f32(value: f32) -> f32 {
    if value.is_finite() { value } else { 0.0 }
}

pub fn capture_layout(ui: &egui::Ui, response: &egui::Response) -> WidgetLayout {
    #[cfg(test)]
    TEST_LAYOUT_CAPTURE_COUNT.with(|count| count.set(count.get() + 1));
    let desired_size = response.intrinsic_size().unwrap_or(response.rect.size());
    let actual_size = response.rect.size();
    let rect = response.rect;
    let clip_rect = ui.clip_rect();
    let available_rect = ui.available_rect_before_wrap();
    let clipped = !clip_rect.contains_rect(rect);
    let overflow = !available_rect.contains_rect(rect);
    WidgetLayout {
        desired_size: desired_size.into(),
        actual_size: actual_size.into(),
        clip_rect: clip_rect.into(),
        clipped,
        overflow,
        available_rect: available_rect.into(),
        visible_fraction: sanitize_f32(visible_fraction(response.rect, clip_rect)),
    }
}

#[cfg(test)]
pub fn reset_test_counters() {
    TEST_LAYOUT_CAPTURE_COUNT.with(|count| count.set(0));
}

#[cfg(test)]
pub fn test_layout_capture_count() -> usize {
    TEST_LAYOUT_CAPTURE_COUNT.with(Cell::get)
}

pub fn record_scroll_area<R>(ui: &egui::Ui, id: impl Into<String>, output: &ScrollAreaOutput<R>) {
    let response = ui.interact(output.inner_rect, output.id, egui::Sense::hover());
    let Some(inner) = active_inner() else {
        return;
    };
    let visible = ui.is_visible() && ui.is_rect_visible(response.rect);
    let layout = Some(capture_layout(ui, &response));
    let id = id.into();
    swallow_panic("record_scroll_area", || {
        let viewport_id = response.ctx.viewport_id();
        let widget_id = response.id.value();
        let override_offset = inner.take_scroll_override(viewport_id, widget_id);
        let recorded_offset = override_offset.unwrap_or(output.state.offset);
        if let Some(offset) = override_offset {
            let mut state = State::load(ui.ctx(), output.id).unwrap_or_default();
            state.offset = offset;
            state.store(ui.ctx(), output.id);
            inner.request_repaint();
        }
        record_widget(
            &inner.widgets,
            id,
            &response,
            WidgetMeta {
                role: WidgetRole::ScrollArea,
                layout,
                role_state: Some(RoleState::ScrollArea {
                    offset: recorded_offset.into(),
                    viewport_size: output.inner_rect.size().into(),
                    content_size: output.content_size.into(),
                }),
                visible,
                ..Default::default()
            },
        );
    });
}

fn visible_fraction(rect: egui::Rect, clip_rect: egui::Rect) -> f32 {
    let area = rect.area();
    if area <= 0.0 {
        return 0.0;
    }
    let intersection = rect.intersect(clip_rect);
    (intersection.area() / area).clamp(0.0, 1.0)
}

#[cfg(test)]
#[allow(deprecated)]
#[allow(clippy::tests_outside_test_module)]
mod tests {
    use std::{any::Any, sync::Arc};

    use egui::{Color32, ColorImage, Context, TextureOptions};

    use super::*;
    use crate::{
        DevMcp, WidgetState,
        devmcp::RuntimeHooks,
        registry::Inner,
        types::WidgetRegistryEntry,
        ui_ext::{ButtonOptions, CheckboxOptions, DevUiExt, ProgressBarOptions, TextEditOptions},
    };

    #[derive(Debug)]
    struct NoopRuntimeHooks;

    impl RuntimeHooks for NoopRuntimeHooks {
        fn as_any(&self) -> &(dyn Any + Send + Sync) {
            self
        }
    }

    fn devmcp_enabled() -> DevMcp {
        DevMcp::new().activate_runtime(Arc::new(Inner::new()), Arc::new(NoopRuntimeHooks))
    }

    fn widget_by_id<'a>(widgets: &'a [WidgetRegistryEntry], id: &str) -> &'a WidgetRegistryEntry {
        widgets
            .iter()
            .find(|entry| entry.id == id)
            .unwrap_or_else(|| panic!("missing widget: {id}"))
    }

    fn run_panel(
        ctx: &Context,
        raw_input: egui::RawInput,
        mut add: impl FnMut(&Context, &mut egui::Ui),
    ) {
        let _output = ctx.run_ui(raw_input, |ui| {
            let ctx = ui.ctx().clone();
            egui::Frame::central_panel(ui.style()).show(ui, |ui| add(&ctx, ui));
        });
    }

    fn assert_combo_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::ComboBox);
        assert!(matches!(widget.value, Some(WidgetValue::Int(1))));
    }

    fn assert_drag_float_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::DragValue);
        assert!(matches!(
            widget.value,
            Some(WidgetValue::Float(value)) if (value - 1.5).abs() < f64::EPSILON
        ));
    }

    fn assert_drag_int_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::DragValue);
        assert!(matches!(widget.value, Some(WidgetValue::Int(7))));
    }

    fn assert_label_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::Label);
        assert!(matches!(
            widget.value,
            Some(WidgetValue::Text(ref value)) if value == "Ready"
        ));
    }

    fn assert_text_edit_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::TextEdit);
        assert!(matches!(
            widget.value,
            Some(WidgetValue::Text(ref value)) if value == "Hello"
        ));
    }

    fn assert_toggle_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::Toggle);
        assert!(matches!(widget.value, Some(WidgetValue::Bool(true))));
    }

    fn assert_radio_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::Radio);
        assert!(matches!(widget.value, Some(WidgetValue::Bool(true))));
    }

    fn assert_selectable_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::Selectable);
        assert!(matches!(widget.value, Some(WidgetValue::Bool(true))));
    }

    fn assert_link_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::Link);
        assert_eq!(widget.label.as_deref(), Some("Docs"));
        assert!(widget.value.is_none());
    }

    fn assert_hyperlink_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::Link);
        assert_eq!(widget.label.as_deref(), Some("Reference"));
        assert!(matches!(
            widget.value,
            Some(WidgetValue::Text(ref value)) if value == "https://example.invalid/reference"
        ));
    }

    fn assert_image_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::Image);
        assert_eq!(widget.label.as_deref(), Some("Preview"));
        assert!(matches!(
            widget.value,
            Some(WidgetValue::Text(ref value)) if value == "Preview"
        ));
    }

    fn assert_separator_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::Separator);
    }

    fn assert_spinner_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::Spinner);
    }

    fn assert_menu_button_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::MenuButton);
        assert_eq!(widget.label.as_deref(), Some("Actions"));
        assert!(matches!(widget.value, Some(WidgetValue::Bool(false))));
    }

    fn assert_collapsing_widget(widget: &WidgetRegistryEntry) {
        assert_eq!(widget.role, WidgetRole::CollapsingHeader);
        assert_eq!(widget.label.as_deref(), Some("Advanced"));
        assert!(matches!(widget.value, Some(WidgetValue::Bool(true))));
    }

    #[test]
    fn registry_records_identified_widget() {
        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let raw_input = egui::RawInput::default();
        run_panel(&ctx, raw_input, |ctx, ui| {
            devmcp.begin_frame(ctx);
            let response = ui.button("Save");
            track_response_full(
                "save",
                &response,
                WidgetMeta {
                    visible: response.interact_rect.is_positive(),
                    ..Default::default()
                },
            );
            devmcp.end_frame(ctx);
        });
        let widgets = devmcp
            .inner()
            .expect("attached inner")
            .widgets
            .widget_list(egui::ViewportId::ROOT)
            .into_iter()
            .map(|entry| entry.id)
            .collect::<Vec<_>>();
        assert!(widgets.contains(&"save".to_string()));
    }

    #[test]
    fn dev_ui_ext_records_extended_widgets() {
        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let preview_texture = ctx.load_texture(
            "instrument.preview",
            ColorImage::filled([4, 4], Color32::from_rgb(64, 156, 255)),
            TextureOptions::LINEAR,
        );
        let raw_input = egui::RawInput::default();
        let mut selected = 1;
        let options = ["One", "Two", "Three"];
        let mut drag_f = 1.5_f32;
        let mut drag_i = 7_i32;
        let mut text = "Hello".to_string();
        let mut toggle = true;
        let mut radio_current = "a".to_string();
        let mut selectable_current = 2_i32;
        let mut advanced_open = true;
        run_panel(&ctx, raw_input, |ctx, ui| {
            devmcp.begin_frame(ctx);
            ui.dev_link("docs", "Docs");
            ui.dev_hyperlink_to(
                "reference",
                "Reference",
                "https://example.invalid/reference",
            );
            ui.dev_image("preview", "Preview", &preview_texture);
            ui.dev_combo_box("combo", "Choice", &mut selected, &options);
            ui.dev_drag_value("drag_f", &mut drag_f);
            ui.dev_drag_value_i32("drag_i", &mut drag_i);
            ui.dev_label("status", "Ready");
            ui.dev_text_edit_multiline("multi", &mut text);
            ui.dev_toggle_value("toggle", &mut toggle, "Toggle");
            ui.dev_radio_value("radio", &mut radio_current, "a".to_string(), "Radio");
            ui.dev_selectable_value("select", &mut selectable_current, 2, "Select");
            ui.dev_separator("separator");
            ui.dev_spinner("spinner");
            let _menu = ui.dev_menu_button("menu", "Actions", |ui| {
                ui.dev_button("menu.item", "Reset");
            });
            let _advanced = ui.dev_collapsing("advanced", &mut advanced_open, "Advanced", |ui| {
                ui.dev_label("advanced.body", "Shown");
            });
            devmcp.end_frame(ctx);
        });

        let widgets = devmcp
            .inner()
            .expect("attached inner")
            .widgets
            .widget_list(egui::ViewportId::ROOT);
        assert_link_widget(widget_by_id(&widgets, "docs"));
        assert_hyperlink_widget(widget_by_id(&widgets, "reference"));
        assert_image_widget(widget_by_id(&widgets, "preview"));
        assert_combo_widget(widget_by_id(&widgets, "combo"));
        assert_drag_float_widget(widget_by_id(&widgets, "drag_f"));
        assert_drag_int_widget(widget_by_id(&widgets, "drag_i"));
        assert_label_widget(widget_by_id(&widgets, "status"));
        assert_text_edit_widget(widget_by_id(&widgets, "multi"));
        assert_toggle_widget(widget_by_id(&widgets, "toggle"));
        assert_radio_widget(widget_by_id(&widgets, "radio"));
        assert_selectable_widget(widget_by_id(&widgets, "select"));
        assert_separator_widget(widget_by_id(&widgets, "separator"));
        assert_spinner_widget(widget_by_id(&widgets, "spinner"));
        assert_menu_button_widget(widget_by_id(&widgets, "menu"));
        assert_collapsing_widget(widget_by_id(&widgets, "advanced"));
        assert_eq!(
            widget_by_id(&widgets, "advanced.body").parent_id.as_deref(),
            Some("advanced.body")
        );
    }

    #[test]
    fn dev_ui_ext_projects_role_state_and_new_roles() {
        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let raw_input = egui::RawInput::default();
        let mut toolbar_selected = true;
        let mut mixed = true;
        let mut password = "opensesame".to_string();
        let mut slider = 4.0_f32;
        let mut drag = 2_i32;
        let mut combo = 1_usize;
        let mut color = Color32::from_rgba_unmultiplied(64, 156, 255, 255);
        run_panel(&ctx, raw_input, |ctx, ui| {
            devmcp.begin_frame(ctx);
            if ui
                .dev_button_with(
                    "toolbar",
                    "Toolbar",
                    ButtonOptions {
                        selected: toolbar_selected,
                    },
                )
                .clicked()
            {
                toolbar_selected = !toolbar_selected;
            }
            ui.dev_checkbox_with(
                "mixed",
                &mut mixed,
                "Mixed",
                CheckboxOptions {
                    indeterminate: true,
                },
            );
            ui.dev_text_edit_with(
                "password",
                &mut password,
                TextEditOptions {
                    multiline: false,
                    password: true,
                },
            );
            ui.dev_slider("slider", &mut slider, 0.0..=10.0);
            ui.dev_drag_value_i32_range("drag", &mut drag, -5..=20);
            ui.dev_combo_box("combo", "Choice", &mut combo, &["Alpha", "Beta", "Gamma"]);
            ui.dev_progress_bar_with(
                "progress",
                0.42,
                ProgressBarOptions {
                    text: None,
                    show_percentage: true,
                },
            );
            ui.dev_color_edit("accent", &mut color);
            devmcp.end_frame(ctx);
        });

        let widgets = devmcp
            .inner()
            .expect("attached inner")
            .widgets
            .widget_list(egui::ViewportId::ROOT);
        let toolbar = WidgetState::from(widget_by_id(&widgets, "toolbar"));
        assert_eq!(toolbar.selected, Some(true));

        let mixed = WidgetState::from(widget_by_id(&widgets, "mixed"));
        assert_eq!(mixed.indeterminate, Some(true));

        let password = WidgetState::from(widget_by_id(&widgets, "password"));
        assert_eq!(password.multiline, Some(false));
        assert_eq!(password.password, Some(true));

        let slider = WidgetState::from(widget_by_id(&widgets, "slider"));
        assert_eq!(slider.range.map(|range| range.min), Some(0.0));
        assert_eq!(slider.range.map(|range| range.max), Some(10.0));

        let drag = WidgetState::from(widget_by_id(&widgets, "drag"));
        assert_eq!(drag.range.map(|range| range.min), Some(-5.0));
        assert_eq!(drag.range.map(|range| range.max), Some(20.0));

        let combo = WidgetState::from(widget_by_id(&widgets, "combo"));
        assert_eq!(
            combo.options,
            Some(vec![
                "Alpha".to_string(),
                "Beta".to_string(),
                "Gamma".to_string()
            ])
        );

        let progress = WidgetState::from(widget_by_id(&widgets, "progress"));
        assert_eq!(progress.role, WidgetRole::ProgressBar);
        assert_eq!(progress.label.as_deref(), Some("42%"));

        let accent = WidgetState::from(widget_by_id(&widgets, "accent"));
        assert_eq!(accent.role, WidgetRole::ColorPicker);
        assert_eq!(accent.value_text, "#409CFFFF");
    }

    #[test]
    fn layout_metadata_captures_clipping() {
        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let raw_input = egui::RawInput::default();
        run_panel(&ctx, raw_input, |ctx, ui| {
            devmcp.begin_frame(ctx);
            egui::ScrollArea::vertical()
                .max_height(40.0)
                .show(ui, |ui| {
                    ui.add_space(200.0);
                    ui.dev_button("clipped", "Clipped");
                });
            devmcp.end_frame(ctx);
        });

        let widgets = devmcp
            .inner()
            .expect("attached inner")
            .widgets
            .widget_list(egui::ViewportId::ROOT);
        let clipped = widgets
            .iter()
            .find(|entry| entry.id == "clipped")
            .expect("clipped widget");
        let layout = clipped.layout.as_ref().expect("layout");
        assert!(layout.desired_size.x >= 0.0);
        assert!(layout.actual_size.x >= 0.0);
        assert!(layout.clipped);
        assert!(layout.overflow);
        assert!(layout.visible_fraction < 1.0);
    }

    #[test]
    fn registry_records_scroll_area() {
        use crate::ui_ext::DevScrollAreaExt;
        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let raw_input = egui::RawInput::default();
        run_panel(&ctx, raw_input, |ctx, ui| {
            devmcp.begin_frame(ctx);
            egui::ScrollArea::vertical()
                .id_salt("scroll_test")
                .max_height(24.0)
                .dev_show(ui, "scroll", |ui| {
                    ui.add_space(200.0);
                });
            devmcp.end_frame(ctx);
        });

        let widgets = devmcp
            .inner()
            .expect("attached inner")
            .widgets
            .widget_list(egui::ViewportId::ROOT);
        let scroll = widgets
            .iter()
            .find(|entry| entry.id == "scroll")
            .expect("scroll widget");
        assert_eq!(scroll.role, WidgetRole::ScrollArea);
        let scroll_meta = scroll
            .role_state
            .as_ref()
            .and_then(RoleState::scroll_state)
            .expect("scroll metadata");
        assert!(scroll_meta.viewport_size.y > 0.0);
        assert!(scroll_meta.content_size.y >= scroll_meta.viewport_size.y);
    }

    #[test]
    fn scroll_area_metadata_does_not_block_child_clicks() {
        use crate::ui_ext::DevScrollAreaExt;

        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let mut clicked = false;

        run_panel(&ctx, egui::RawInput::default(), |ctx, ui| {
            devmcp.begin_frame(ctx);
            egui::ScrollArea::vertical()
                .id_salt("scroll_click_test")
                .max_height(48.0)
                .dev_show(ui, "scroll", |ui| {
                    if ui.dev_button("child", "Child").clicked() {
                        clicked = true;
                    }
                    ui.add_space(200.0);
                });
            devmcp.end_frame(ctx);
        });

        let widgets = devmcp
            .inner()
            .expect("attached inner")
            .widgets
            .widget_list(egui::ViewportId::ROOT);
        let pos: egui::Pos2 = widget_by_id(&widgets, "child")
            .interact_rect
            .center()
            .into();
        let click_input = egui::RawInput {
            events: vec![
                egui::Event::PointerMoved(pos),
                egui::Event::PointerButton {
                    pos,
                    button: egui::PointerButton::Primary,
                    pressed: true,
                    modifiers: egui::Modifiers::default(),
                },
                egui::Event::PointerButton {
                    pos,
                    button: egui::PointerButton::Primary,
                    pressed: false,
                    modifiers: egui::Modifiers::default(),
                },
            ],
            ..Default::default()
        };

        run_panel(&ctx, click_input, |ctx, ui| {
            devmcp.begin_frame(ctx);
            egui::ScrollArea::vertical()
                .id_salt("scroll_click_test")
                .max_height(48.0)
                .dev_show(ui, "scroll", |ui| {
                    if ui.dev_button("child", "Child").clicked() {
                        clicked = true;
                    }
                    ui.add_space(200.0);
                });
            devmcp.end_frame(ctx);
        });

        assert!(
            clicked,
            "expected child button click to survive scroll-area metadata"
        );
    }

    #[test]
    fn scroll_area_state_reset_requests_origin_jump() {
        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let mut state = ScrollAreaState::new();

        let render = |ctx: &Context, state: &mut ScrollAreaState| {
            run_panel(ctx, egui::RawInput::default(), |ctx, ui| {
                devmcp.begin_frame(ctx);
                state.show(
                    egui::ScrollArea::vertical()
                        .id_salt("scroll_reset_test")
                        .max_height(24.0),
                    ui,
                    "scroll_reset",
                    |ui| {
                        ui.add_space(200.0);
                    },
                );
                devmcp.end_frame(ctx);
            });
        };

        state.jump_to(egui::vec2(0.0, 100.0));
        render(&ctx, &mut state);
        assert!(
            state.offset().y > 0.0,
            "expected jump_to to move scroll state"
        );

        state.reset();
        render(&ctx, &mut state);
        assert_eq!(
            state.offset().y,
            0.0,
            "expected reset to jump back to origin"
        );
    }

    #[test]
    fn scroll_area_state_reflects_pending_tool_override() {
        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let mut state = ScrollAreaState::new();

        let render = |ctx: &Context, state: &mut ScrollAreaState| {
            run_panel(ctx, egui::RawInput::default(), |ctx, ui| {
                devmcp.begin_frame(ctx);
                state.show(
                    egui::ScrollArea::vertical()
                        .id_salt("scroll_override_test")
                        .max_height(24.0),
                    ui,
                    "scroll_override",
                    |ui| {
                        ui.add_space(200.0);
                    },
                );
                devmcp.end_frame(ctx);
            });
        };

        render(&ctx, &mut state);
        let widgets = devmcp
            .inner()
            .expect("attached inner")
            .widgets
            .widget_list(egui::ViewportId::ROOT);
        let scroll = widget_by_id(&widgets, "scroll_override");
        let widget_id = scroll.native_id;

        devmcp.inner().expect("attached inner").set_scroll_override(
            egui::ViewportId::ROOT,
            widget_id,
            egui::vec2(0.0, 80.0),
        );
        render(&ctx, &mut state);

        assert_eq!(
            state.offset().y,
            80.0,
            "expected tool-driven override to update tracked offset immediately"
        );
    }

    #[test]
    fn container_scopes_track_parent_id() {
        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let raw_input = egui::RawInput::default();
        run_panel(&ctx, raw_input, |ctx, ui| {
            devmcp.begin_frame(ctx);
            container(ui, "outer", |ui| {
                container(ui, "inner", |ui| {
                    ui.dev_button("leaf", "Leaf");
                });
            });
            devmcp.end_frame(ctx);
        });

        let widgets = devmcp
            .inner()
            .expect("attached inner")
            .widgets
            .widget_list(egui::ViewportId::ROOT);
        let leaf = widgets
            .iter()
            .find(|entry| entry.id == "leaf")
            .expect("leaf widget");
        assert_eq!(leaf.parent_id.as_deref(), Some("inner"));
    }

    #[test]
    fn metadata_captures_enabled_and_visible() {
        let devmcp = devmcp_enabled();
        let ctx = Context::default();
        let raw_input = egui::RawInput::default();
        run_panel(&ctx, raw_input, |ctx, ui| {
            devmcp.begin_frame(ctx);
            ui.dev_button("enabled_btn", "Enabled");
            ui.add_enabled_ui(false, |ui| {
                ui.dev_button("disabled_btn", "Disabled");
            });
            ui.dev_label("visible_lbl", "Visible");
            egui::ScrollArea::vertical()
                .max_height(10.0)
                .show(ui, |ui| {
                    ui.add_space(100.0);
                    ui.dev_label("clipped_lbl", "Clipped");
                });
            devmcp.end_frame(ctx);
        });

        let widgets = devmcp
            .inner()
            .expect("attached inner")
            .widgets
            .widget_list(egui::ViewportId::ROOT);

        let enabled_btn = widget_by_id(&widgets, "enabled_btn");
        assert!(enabled_btn.enabled, "enabled_btn should be enabled");
        assert!(enabled_btn.visible, "enabled_btn should be visible");

        let disabled_btn = widget_by_id(&widgets, "disabled_btn");
        assert!(!disabled_btn.enabled, "disabled_btn should be disabled");

        let visible_lbl = widget_by_id(&widgets, "visible_lbl");
        assert!(visible_lbl.visible, "visible_lbl should be visible");

        let clipped_lbl = widget_by_id(&widgets, "clipped_lbl");
        assert!(
            !clipped_lbl.visible,
            "clipped_lbl should be invisible (clipped)"
        );
    }
}