BREP_app 0.4.0

The BREP CAD application: an eframe (egui + wgpu) host that draws the brep-render 3D engine into an egui frame — native + wasm from one codebase.
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
//! History panel — a feature **tree** that switches to a full-panel **form**.
//!
//! The panel has exactly two modes ([`PanelMode`]):
//!
//! * **Tree** — one row per feature: `[+/-] id LongName   N ms  [✎] [✕]`. The
//!   collapse box is a PURE ROLL control (roll the model to that step, nothing
//!   opens); the `✎` edit button and a plain LABEL CLICK both open that
//!   feature's form (and roll to it); rows drag-reorder; `✕` deletes. Built on
//!   the reusable [`tree`] node helper (connector lines + `[+]`/`[-]` boxes) the
//!   rest of the sidebar reuses.
//! * **Form** — the whole panel is replaced by ONE feature's dialog, drawn by
//!   the shared [`crate::form_view`], with a single `Return to tree` button at
//!   the top right, beside the title. There is no OK/Cancel and no buffer:
//!   editing is LIVE and UNDO
//!   is the revert mechanism (`History::set_feature_params` checkpoints and
//!   coalesces per feature).
//!
//! Both entry points roll the model to the feature, exactly as expanding did;
//! returning to the tree rolls to the TIP so downstream features rebuild and the
//! edit becomes visible.
//!
//! This panel OWNS NO model state — it calls the ENGINE's history methods
//! (`state.*`) and reads the history + last-run report back to draw. The engine
//! core (`EngineState.history`) is the single source of truth. The panel holds
//! only transient UI state: the mode, an in-flight drag, the add-menu toggle,
//! the run-spinner clock, and the per-frame `hits` map (widget screen rects) the
//! headed verifier reads to drive real clicks.
//!
//! The tree also shows HOW FAR the model is built: the rolled-to feature is the
//! last one EXECUTED, so a rollback BAR is drawn under its block, every feature
//! below that bar — not yet executed — is dimmed, and its `[+]`/`[-]` box reads
//! `[-]` down to the rolled-to step and `[+]` below it.

use crate::automation::hit_keys::HitKeyDoc;
use crate::form_view::{form_view, FormViewSpec};
use crate::panels::spline_anchors;
use crate::palette::{Palette, PaletteItem};
use crate::panels::tree::{self, TreeRow};
use brep_render::engine_state::EngineState;
use brep_render::features;
use eframe::egui;
use serde_json::Value;
use std::collections::HashMap;

/// Who this panel is when it drives the viewport's dialog-row hover
/// (`EngineState::hover_entity_by_name`) — the owner tag that keeps its
/// highlight independent of the Scene tree's and the other form panes'.
const DIALOG_HOVER_OWNER: &str = "history";

/// The red of the per-feature delete affordance (theme-independent — it must read
/// as "destructive" in both light and dark).
const DELETE_RED: egui::Color32 = egui::Color32::from_rgb(0xd8, 0x54, 0x4f);

/// The red of a feature's error message node — a brighter, clearly-legible red for
/// wrapped body text (the delete red is tuned for a small glyph). Matches the
/// hardcoded-chrome-red convention of `DELETE_RED`.
const ERROR_RED: egui::Color32 = egui::Color32::from_rgb(0xff, 0x6b, 0x6b);

/// The height of the ROLLBACK BAR row — the horizontal rule painted after the
/// rolled-to feature ("the model is executed up to HERE"). Tall enough to read as
/// a break between the executed block above and the dimmed, not-yet-executed rows
/// below.
const ROLLBACK_BAR_H: f32 = 8.0;

/// How long a run must be in flight before the tree header's spinner appears, in
/// seconds. A single param edit lands in a frame or two, and a spinner that blinks
/// for one frame reads as a glitch — only a run the user actually WAITS on spins.
const RUN_SPINNER_DELAY: f64 = 0.2;

/// The error message for feature `id` from the run report's `featureErrors` array,
/// or `None` when that feature ran clean. The kernel records each hard failure as
/// `"<feature id>: <message>"` (see `pipeline::SceneBuildReport`); this matches the
/// `"<id>: "` prefix (the delimiter after the exact id stops a shorter id from
/// matching a longer one) and returns just the message.
fn feature_error_message(report: &serde_json::Value, id: &str) -> Option<String> {
    let prefix = format!("{id}: ");
    report
        .get("featureErrors")?
        .as_array()?
        .iter()
        .filter_map(serde_json::Value::as_str)
        .find(|entry| entry.starts_with(&prefix))
        .map(|entry| entry[prefix.len()..].to_string())
}

/// What the panel is showing: the feature TREE, or ONE feature's FORM filling
/// the whole panel. Exclusive by construction — there is no "expanded feature"
/// inside the tree any more, so there is nothing to reconcile between them.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub enum PanelMode {
    /// The feature tree (the resting state).
    #[default]
    Tree,
    /// `feature_id`'s dialog, replacing the whole panel. Falls back to
    /// [`PanelMode::Tree`] the moment that id stops resolving (undo, delete,
    /// document load) — see the validity guard at the top of
    /// [`HistoryPanel::show`].
    Form { feature_id: String },
}

impl PanelMode {
    /// The open form's feature id, or `None` in tree mode.
    fn feature_id(&self) -> Option<&str> {
        match self {
            PanelMode::Tree => None,
            PanelMode::Form { feature_id } => Some(feature_id.as_str()),
        }
    }
}

/// The history panel's transient UI state (the model lives in the engine).
#[derive(Default)]
pub struct HistoryPanel {
    /// The per-frame map of egui widget screen rects, published to JS for the
    /// headed verifier. Rebuilt every frame.
    hits: HashMap<String, egui::Rect>,
    /// Tree, or one feature's full-panel form.
    mode: PanelMode,
    /// The feature id the panel last AUTO-ARMED a dimension gizmo for (gizmo-on-
    /// open). Compared to the OPEN FORM's feature id each frame: on a CHANGE
    /// (open a different feature's form, or return to the tree) the panel
    /// disarms the old gizmo and arms the dimension gizmo for the newly-opened
    /// feature IF it has dimensions — once per transition, so the in-viewport
    /// sphere/center toggle (transform↔dimension) isn't clobbered back to
    /// dimension each frame.
    gizmo_armed_for: Option<String>,
    /// The feature index currently being drag-reordered (None = not dragging).
    drag_src: Option<usize>,
    /// The reusable searchable command palette that `Add new feature` opens,
    /// populated from the kernel feature catalogue. Generic + engine-agnostic —
    /// this panel drives it and acts on the returned type code.
    palette: Palette,
    palette_display_loaded: bool,
    /// A schema `button` field click staged this frame — `(feature id, button
    /// key)` — applied AFTER the draw loop (so no engine mutation runs mid-render).
    /// E.g. `editSketch` on a SKETCH feature → `enter_sketch_mode`.
    pending_button: Option<(String, String)>,
    /// The egui clock time (`Input::time`, seconds) at which the CURRENT in-flight
    /// run was FIRST seen pending, or `None` when nothing is running. Drives the
    /// header spinner's anti-flicker delay ([`RUN_SPINNER_DELAY`]); egui's own
    /// clock, not `Instant` (which panics on wasm32).
    run_started: Option<f64>,
    /// Set when the palette picked the ACOMP type: the insert flow must NOT
    /// open a bare feature dialog — the shell polls this
    /// ([`Self::take_insert_component_request`]) and opens the COMPONENT
    /// SELECTOR (the file dialog's insert mode) instead.
    pending_insert_component: bool,
    /// The SELECTED anchor of the open Spline form's anchor editor (the row
    /// the gizmo is armed on and the cage highlights). Reset when the open
    /// feature changes. See [`super::spline_anchors`].
    anchor_selected: Option<usize>,
}

impl HistoryPanel {
    /// Load once per panel (including document switches), save only user changes.
    pub fn sync_palette_display(&mut self, store: &dyn crate::store::ModelStore) {
        use crate::store::FEATURE_PALETTE_DISPLAY_KEY;
        if !self.palette_display_loaded {
            self.palette.display = store.read(FEATURE_PALETTE_DISPLAY_KEY)
                .and_then(|json| serde_json::from_str(&json).ok()).unwrap_or_default();
            self.palette_display_loaded = true;
        }
        if self.palette.take_display_change() {
            if let Ok(json) = serde_json::to_string(&self.palette.display) {
                let _ = store.write(FEATURE_PALETTE_DISPLAY_KEY, &json);
            }
        }
    }

    pub fn new() -> Self {
        Self::default()
    }

    /// Draw the feature tree. While a reference-selection picker is active the
    /// shell HIDES this whole panel (the design doc's "hide the rest of the UI")
    /// and shows the picker in the top-right mode card ([`super::mode_bar`]), so
    /// this method is not called in that mode.
    pub fn show(&mut self, ui: &mut egui::Ui, state: &mut EngineState) {
        self.hits.clear();

        // The panel's VISIBLE region (the enclosing dock pane's scroll viewport).
        // Every other rect below is a raw LAYOUT rect: a long feature list — or a
        // long form — runs past the pane's bottom, where egui clips it and it
        // stops being clickable even though the rect is still published. The
        // headed verifier intersects against this to
        // know when it must scroll a row into view first — without it a script
        // clicks dead space outside the panel and silently no-ops.
        self.hits.insert("panel:clip".into(), ui.clip_rect());

        // --- validity guard: a form whose SUBJECT is gone falls back, silently.
        // One check covers every hazard — undo that removed the feature, a delete
        // from another surface, a document load that changed the ids wholesale.
        // With no Cancel there is no dirty state to protect, so there is nothing
        // to ask the user about (params edits are already committed and undoable).
        if let Some(id) = self.mode.feature_id() {
            if feature_index_of(state, id).is_none() {
                self.mode = PanelMode::Tree;
            }
        }

        // Publish the armed transform gizmo's origin in VIEWPORT-LOCAL px (the
        // center handle sits there) so the headed verifier can locate + drag it.
        // Map to page px with `window.__brepView`'s origin (viewport rect).
        if let Some((ax, ay)) = state.transform_gizmo_anchor() {
            self.hits.insert(
                "gizmo-anchor".into(),
                egui::Rect::from_min_size(egui::pos2(ax as f32, ay as f32), egui::Vec2::ZERO),
            );
        }

        // Last-run report → per-feature timing + output solid names (parsed once).
        let report: Value = serde_json::from_str(&state.history_report_json()).unwrap_or(Value::Null);

        // Tight, tree-like row spacing so connector verticals read continuously.
        ui.spacing_mut().item_spacing.y = 2.0;

        // --- run-indicator bookkeeping (both modes) ---------------------------
        // Tracked in EVERY mode so a run that starts in the form and finishes
        // while the tree is up can't strand a stale start time (which would flash
        // the spinner on the next tree frame).
        let now = ui.input(|i| i.time);
        match (state.run_pending(), self.run_started) {
            (true, None) => self.run_started = Some(now),
            (false, Some(_)) => self.run_started = None,
            _ => {}
        }

        // --- THE MODE SWITCH: the tree, or ONE feature's form -----------------
        match self.mode.clone() {
            PanelMode::Tree => {
                // No spline form is open: no anchor cage in the viewport.
                state.refresh_spline_edit_overlay(None, None);
                self.show_tree(ui, state, &report);
            }
            PanelMode::Form { feature_id } => self.show_form(ui, state, &report, &feature_id),
        }

        // --- apply a staged schema-button click (after the draw loop) ----------
        if let Some((fid, key)) = self.pending_button.take() {
            self.handle_feature_button(state, &fid, &key);
        }

        // --- gizmo-on-open (Phase 1) ------------------------------------------
        // Arm the DIMENSION gizmo for the feature whose FORM is open so its
        // draggable arrows appear on open (the reported bug), and DISARM on
        // return to the tree so gizmos don't leak. Runs only on an open/close
        // TRANSITION (the open feature changed since last frame) so it never
        // thrashes per frame — that lets the in-viewport sphere/center toggle flip
        // a feature to transform mode and STAY there (a per-frame re-arm would
        // snap it back to dimension). Guarded on the feature actually having
        // dimension annotations or a schema-declared Transform group.
        let open_feature = self.mode.feature_id().map(str::to_string);
        if self.gizmo_armed_for != open_feature {
            self.anchor_selected = None;
            state.disarm_transform();
            if let Some(id) = open_feature.clone() {
                if state.feature_dimension_annotations_json(&id) != "[]" {
                    // Has dimensions → dimension arrows (sphere-toggle to transform).
                    state.arm_dimension(&id);
                } else if state.feature_has_transform(&id) {
                    // No dimensions but transformable (Transform/datum/helix/…) →
                    // arm the TRANSFORM gizmo directly, so it isn't stranded without
                    // a gizmo now that the ◎ arm button is gone.
                    state.arm_transform(&id);
                } else if state.is_spline_feature(&id) {
                    // A spline's editing surface is its anchors: arm the gizmo on
                    // the first FREE one, so the form opens with a handle to drag
                    // (an attached anchor is placed by its port and has none).
                    let first_free = state
                        .spline_anchors(&id)
                        .iter()
                        .find(|row| row.attached.is_none())
                        .map(|row| row.index);
                    if let Some(index) = first_free {
                        if state.arm_spline_anchor(&id, index) {
                            self.anchor_selected = Some(index);
                        }
                    }
                }
            }
            self.gizmo_armed_for = open_feature;
        }
    }

    /// Draw ONE feature's dialog filling the whole panel, through the SHARED
    /// [`form_view`]. The panel supplies the schema + the live params and acts on
    /// the intents that come back — it is the engine side of a form view that
    /// holds no engine itself. Editing is LIVE (every change commits and
    /// re-runs); the single exit button returns to the tree and rolls to the
    /// TIP, so downstream features rebuild and the edit becomes visible — the
    /// same reason collapsing an inline dialog used to roll to the tip.
    fn show_form(
        &mut self,
        ui: &mut egui::Ui,
        state: &mut EngineState,
        report: &Value,
        id: &str,
    ) {
        // The guard in `show` already proved the id resolves.
        let Some(index) = feature_index_of(state, id) else {
            return;
        };
        let ty = state.feature_type_at(index).unwrap_or_else(|| "?".into());
        // NO glyph: an egui window title is plain text and cannot hold a widget,
        // so it is the one place an icon cannot be drawn as artwork — and with
        // no icon font there is nothing else to draw a private-use character
        // with. The id and name identify the form; the tree row behind it shows
        // the icon.
        let title = format!("{id}  {}", features::feature_plain_name(&ty));
        let fields = features::feature_form_fields(&ty);
        let mut params: Value =
            serde_json::from_str(&state.feature_params_json(index)).unwrap_or(Value::Null);
        // The feature's error shows HERE, as a banner above the fields — and the
        // tree row keeps its own error leaf, so a failure is still visible while
        // scanning the tree.
        let error = feature_error_message(report, id);
        let outputs: Vec<String> = report
            .get("featureOutputs")
            .and_then(|m| m.get(id))
            .and_then(Value::as_array)
            .map(|a| a.iter().filter_map(|v| v.as_str().map(String::from)).collect())
            .unwrap_or_default();
        let trailing = [("Outputs", outputs)];

        // A SPLINE's editing surface is its anchor list — the schema cannot
        // express it, so it is the form's consumer section. Intent-out: the
        // drawer only records what was asked; the engine is driven below.
        let is_spline = ty == "SP";
        if is_spline {
            // A viewport click on an anchor dot selected it (and armed the
            // gizmo there): the row selection follows before the list draws.
            if let Some(index) = state.take_spline_anchor_pick() {
                self.anchor_selected = Some(index);
            }
        }
        let anchor_rows = if is_spline { state.spline_anchors(id) } else { Vec::new() };
        let anchor_intents: std::cell::RefCell<Vec<spline_anchors::AnchorIntent>> =
            std::cell::RefCell::new(Vec::new());
        let anchor_hits: std::cell::RefCell<Vec<(String, egui::Rect)>> =
            std::cell::RefCell::new(Vec::new());
        let anchor_selected = self.anchor_selected;
        let draw_anchors = |ui: &mut egui::Ui| {
            spline_anchors::draw(ui, &anchor_rows, anchor_selected, &anchor_intents, &anchor_hits);
        };

        let spec = FormViewSpec {
            title: &title,
            subtitle: None,
            fields: &fields,
            banner: error.as_deref().map(|m| (m, ERROR_RED)),
            trailing: Some(&trailing),
            exit_label: "Return to tree",
            extra: is_spline.then_some(("Anchors", &draw_anchors as &dyn Fn(&mut egui::Ui))),
            // A feature LIVES in the rolled history, so leaving its form rolls to
            // the tip (Q2). Declared here, acted on below via `out.roll_to_tip`.
            rollback: true,
            // History shows ONE form at a time, so its field keys stay exactly
            // the tree's — `field:sizeX`, `field:boolean.operation` — and every
            // verifier field flow keeps working unchanged.
            hits_prefix: "",
        };
        let out = form_view(ui, &spec, &mut params, Some(&mut self.hits));
        for (key, rect) in anchor_hits.into_inner() {
            self.hits.insert(key, rect);
        }
        if is_spline {
            // ONE anchor intent per frame (each is a document edit + re-run).
            if let Some(intent) = anchor_intents.into_inner().into_iter().next() {
                self.anchor_selected = spline_anchors::apply(state, id, intent, self.anchor_selected);
            }
            state.refresh_spline_edit_overlay(Some(id), self.anchor_selected);
        } else {
            state.refresh_spline_edit_overlay(None, None);
        }

        // WHICH feature the form is showing, as a presence-only zero-size rect
        // beside the header's real rect (`form:feature`) — the same convention
        // `run:spinner` uses for "this is showing right now".
        let anchor = self
            .hits
            .get("form:feature")
            .map(|r| r.min)
            .unwrap_or(egui::Pos2::ZERO);
        self.hits.insert(
            format!("form:feature:{id}"),
            egui::Rect::from_min_size(anchor, egui::Vec2::ZERO),
        );

        if out.changed {
            let _ = state.update_feature_params(id, &params.to_string());
        }
        // A button click (e.g. `editSketch`) binds to no param — stage it as a
        // deferred action keyed by (feature id, button key); `show` acts after the
        // draw so no engine mutation happens mid-render.
        if let Some(key) = out.button_clicked {
            self.pending_button = Some((id.to_string(), key));
        }
        if let Some(activate) = out.ref_activate {
            state.begin_ref_select(
                id,
                activate.path,
                activate.label,
                activate.filter,
                activate.multiple,
                activate.seed,
            );
        }
        // A hovered reference / `Outputs` line lights the entity it names in the
        // 3D view. Applied EVERY frame, independent of everything above (hovering
        // a line and editing a field legitimately land on the same frame): the
        // engine dedupes a held hover, and `dialog_hover_end` only ends a hover
        // THIS panel set, so the Scene tree beside it in a split dock keeps its
        // own.
        let hover_changed = match &out.hovered_entity {
            Some(name) => state.hover_entity_by_name(DIALOG_HOVER_OWNER, name),
            None => state.dialog_hover_end(DIALOG_HOVER_OWNER),
        };
        if hover_changed {
            // The viewport tile may have drawn (and consumed `state.dirty`) BEFORE
            // this pane in the dock, so without this the new highlight would wait
            // for the next pointer event to reach the screen.
            ui.ctx().request_repaint();
        }
        if out.exit_clicked {
            self.mode = PanelMode::Tree;
        }
        if out.roll_to_tip {
            // Finished editing → return the model to the TIP so the WHOLE history
            // runs and every downstream feature (e.g. a boolean that consumes this
            // one) reappears and reflects the edit. Without this the view stays
            // rolled at the just-edited feature and the result never updates — half
            // of the reported "edit the cylinder, close it, nothing changes" bug
            // (the other half was the stale cache, fixed in the kernel). The cache
            // makes this cheap: unchanged features replay instantly.
            //
            // The ROLL half is gated by `spec.rollback` (the form view's one-place
            // decision), not by an `if self is the history panel` here — a consumer
            // with no rollback simply never receives this intent.
            state.roll_to(state.history_len().saturating_sub(1));
        }
    }

    /// Draw the feature TREE — one row per feature, the rollback bar, and the
    /// add-feature palette.
    fn show_tree(&mut self, ui: &mut egui::Ui, state: &mut EngineState, report: &Value) {
        // --- run indicator (INTERIM) ------------------------------------------
        // A history run is ATOMIC from the app's side — the runner (browser worker /
        // native thread) computes the WHOLE history and replies once, so the engine
        // can only say "something is running" ([`EngineState::run_pending`]), never
        // WHICH feature is executing. So the spinner rides the tree HEADER, not a
        // feature row: it is honest about what is actually known. (A true
        // per-feature spinner needs a progress channel out of the kernel's execute
        // loop — planned, not built.) The shell already repaints every frame while a
        // run is pending, so the spinner animates; under the synchronous Inline
        // runner `run_pending()` is never true, so nothing ever spins there.
        let now = ui.input(|i| i.time);
        let spinning = self
            .run_started
            .is_some_and(|started| now - started >= RUN_SPINNER_DELAY);
        // What the runner says it is executing (posted before each feature it
        // runs), and the feature a cancelled run was stuck on until the next
        // rebuild.
        let progress = state.run_progress().cloned();
        let cancelled = state.cancelled_run().map(str::to_string);

        // --- ROOT: `[-] Features` (always open) -------------------------------
        let mut spinner_rect = egui::Rect::NOTHING;
        let mut cancel_rect = egui::Rect::NOTHING;
        let mut cancel_clicked = false;
        tree::node(
            ui,
            TreeRow {
                guides: &[],
                is_last: true,
                expandable: true,
                expanded: true,
                root: true,
                glyph: None,
                label: "Features",
                selected: false,
                draggable: false,
                tint: None,
            },
            |ui| {
                // The row's right-hand slot lays out RIGHT TO LEFT, so the
                // first widget sits at the pane's edge: the button goes first,
                // where a narrow pane can never push it under the row label.
                if spinning {
                    let cancel = ui.small_button("Cancel").on_hover_text(
                        "Stop the run. The model keeps the last completed result; \
                         edit or delete the slow feature to rebuild.",
                    );
                    cancel_rect = cancel.rect;
                    cancel_clicked = cancel.clicked();
                    spinner_rect = ui.add(egui::Spinner::new().size(12.0)).rect;
                    if let Some(progress) = &progress {
                        ui.weak(format!(
                            "{} · {}/{}",
                            progress.feature_id,
                            progress.index + 1,
                            progress.total
                        ));
                    }
                } else if let Some(cancelled) = &cancelled {
                    let note = if cancelled.is_empty() {
                        "run cancelled — the next edit rebuilds".to_string()
                    } else {
                        format!("run cancelled at {cancelled} — edit or delete it to rebuild")
                    };
                    ui.colored_label(ERROR_RED, note);
                }
            },
        );
        // Published only while it SHOWS, so the verifier reads presence, not a rect.
        if spinning {
            self.hits.insert("run:spinner".into(), spinner_rect);
            self.hits.insert("run:cancel".into(), cancel_rect);
        }
        if cancel_clicked {
            state.cancel_run();
        }

        let len = state.history_len();
        if len == 0 {
            let g = tree::child_guides(&[], true);
            tree::node(ui, TreeRow::leaf(&g, true, "(empty — add a feature)"), |_| {});
        }

        // Deferred engine mutations (applied after the draw loop so no borrow of
        // `self`/`state` is held across them).
        let mut roll: Option<usize> = None;
        let mut delete: Option<String> = None;
        let mut drag_move: Option<(usize, usize)> = None;
        // The feature whose FORM to open — `(index, id)`. Applied after the draw
        // loop, with the roll, so the mode flip and the roll are one step.
        let mut open_form: Option<(usize, String)> = None;
        let mut feature_rects: Vec<(usize, egui::Rect)> = Vec::with_capacity(len);

        let current = state.history_rollback();
        for i in 0..len {
            let ty = state.feature_type_at(i).unwrap_or_else(|| "?".into());
            let id = state.feature_id_at(i).unwrap_or_else(|| "(no id)".into());
            let is_last_feature = i + 1 == len;
            let ms = report
                .get("featureTimings")
                .and_then(|m| m.get(&id))
                .and_then(Value::as_f64)
                .unwrap_or(0.0);
            // The feature's glyph goes in the tree's OWN glyph column rather
            // than inline in the label, so the icons line up down the tree and
            // a catalogued COLOUR icon is drawn as its artwork instead of as a
            // one-colour font character (see `tree::node`). `feature_plain_name`
            // is `feature_long_name` without the glyph it would prepend.
            let glyph = features::feature_icon(&ty).map(String::from);
            let label = format!("{id}  {}", features::feature_plain_name(&ty));

            // Features AFTER the rollback point have NOT been executed: dim the
            // WHOLE row — header, timing, edit + delete buttons and the connector
            // lines — with egui's own disabled dimming, the app's existing
            // "not active" language (see `panels::toolbar_button`). Dimmed, NOT
            // disabled: a click on one still rolls the model FORWARD to it.
            let pending = i > current;
            ui.scope(|ui| {
                if pending {
                    ui.set_opacity(ui.visuals().disabled_alpha());
                }
                // --- feature header row: [+/-] {glyph} id LongName  N ms [✎] [X] --
                // The per-type glyph sits in the tree's own glyph column, so it
                // is drawn as real COLOUR artwork from the icon catalog rather
                // than as a one-colour font character, and the icons line up in
                // a column down the tree. See `tree::node`.
                //
                // The `[+]`/`[-]` box is the BUILT-UP-TO marker: `[-]` down to the
                // rolled-to feature, `[+]` on the not-yet-executed ones below it —
                // the same boundary the rollback bar and the dimming draw. Clicking
                // one MOVES that boundary (a pure roll); nothing expands, because a
                // feature's fields no longer live in the tree.
                let mut del_rect = egui::Rect::NOTHING;
                let mut del_clicked = false;
                let mut edit_rect = egui::Rect::NOTHING;
                let mut edit_clicked = false;
                let resp = tree::node(
                    ui,
                    TreeRow::branch(&[], is_last_feature, !pending, &label)
                        .glyph(glyph.as_deref())
                        .selected(i == current)
                        .draggable(true),
                    |ui| {
                        // right-to-left: X first (rightmost), then the edit pencil,
                        // then the timing. Both buttons are `small()` so a third
                        // control costs the label as little width as possible.
                        let del = ui.add(
                            crate::icon_text::icon_button_colored(ui, "", Some(DELETE_RED))
                                .stroke(egui::Stroke::new(1.0, DELETE_RED))
                                .small(),
                        );
                        del_rect = del.rect;
                        del_clicked = del.clicked();
                        ui.add_space(4.0);
                        let edit = ui
                            .add(crate::icon_text::icon_button(ui, "").small())
                            .on_hover_text("Edit this feature");
                        edit_rect = edit.rect;
                        edit_clicked = edit.clicked();
                        ui.add_space(6.0);
                        ui.label(egui::RichText::new(format!("{} ms", ms.round() as i64)).weak());
                    },
                );
                self.hits.insert(format!("step:{i}"), resp.label.rect);
                self.hits.insert(format!("box:{i}"), resp.box_rect);
                self.hits.insert(format!("del:{i}"), del_rect);
                self.hits.insert(format!("edit:{i}"), edit_rect);
                feature_rects.push((i, resp.row_rect));

                if del_clicked {
                    delete = Some(id.clone());
                }
                // Collapse box → a PURE ROLL to that step (no dialog), so the model
                // can be rolled around without opening anything. Edit button OR a
                // plain label click → open that feature's form AND roll to it (one
                // gesture, two affordances: the button is the discoverable one, the
                // label click is the one the hand already does). Drag to ANOTHER row
                // → reorder. A drag that ends back on its OWN row is a click that
                // egui timed out of the click window — the drag-resolution block
                // below routes it here-equivalently (open + roll).
                if resp.toggled {
                    roll = Some(i);
                }
                if edit_clicked || resp.label.clicked() {
                    open_form = Some((i, id.clone()));
                }
                if resp.label.drag_started() {
                    self.drag_src = Some(i);
                }

                // --- error node: shown under a FAILING feature, ALWAYS, so a
                // failure is visible while SCANNING the tree (the form's banner
                // shows the same message to whoever opens the feature), and gone
                // the moment the feature runs clean.
                if let Some(message) = feature_error_message(report, &id) {
                    let g = tree::child_guides(&[], is_last_feature);
                    tree::message_leaf(ui, &g, true, &message, ERROR_RED);
                }
            });

            // --- the EXECUTED-UP-TO boundary --------------------------------
            // The rolled-to feature IS executed (a run builds `features[0..=rollback]`
            // — see `brep_render::history`; the request's `stopAtId` stops AFTER that
            // feature), so the bar goes BELOW its whole block: everything above the
            // bar is live, everything below it is dimmed and not yet built. Drawn
            // OUTSIDE the dim scope (and at the tip too, where it simply reports that
            // the model is built to the end).
            if i == current {
                let bar = rollback_bar(ui);
                self.hits.insert("rollback:bar".into(), bar);
            }
        }

        // --- resolve an in-flight drag ----------------------------------------
        if let Some(src) = self.drag_src {
            let released = ui.input(|i| i.pointer.any_released());
            let ptr = ui.input(|i| i.pointer.interact_pos());
            match (ptr, released) {
                (Some(p), released) => {
                    let target = feature_rects
                        .iter()
                        .min_by(|a, b| {
                            let da = (a.1.center().y - p.y).abs();
                            let db = (b.1.center().y - p.y).abs();
                            da.partial_cmp(&db).unwrap_or(std::cmp::Ordering::Equal)
                        })
                        .map(|(idx, _)| *idx)
                        .unwrap_or(src);
                    if released {
                        if target == src {
                            // NOT a reorder — a press that STARTED and ENDED on the
                            // same row. egui reclassifies a press as a DRAG once it
                            // outlives `max_click_duration` (0.8 s) or drifts past
                            // `max_click_dist` (6 pt), so an ordinary human click on
                            // a label — which routinely lingers or wobbles a few
                            // pixels — fires `drag_started` and NEVER `clicked`.
                            // Routing that through the reorder arm below opened the
                            // feature's dialog but silently dropped the roll (the
                            // reported "clicking a feature's label doesn't move the
                            // model" bug), because `drag_move` shadows `roll` in the
                            // apply chain. A same-slot release IS the label click:
                            // open that feature's form and roll, exactly like
                            // `resp.label.clicked()` does. egui never fires both a
                            // click and a drag for one press, so this can't
                            // double-roll.
                            match state.feature_id_at(src) {
                                Some(id) => open_form = Some((src, id)),
                                // No id to open a form for — still roll, which is
                                // the half of the gesture that must never be lost.
                                None => roll = Some(src),
                            }
                        } else {
                            drag_move = Some((src, target));
                        }
                        self.drag_src = None;
                    } else if target != src {
                        // Draw an insertion indicator at the target row edge.
                        if let Some((_, rect)) = feature_rects.iter().find(|(idx, _)| *idx == target)
                        {
                            let y = if target >= src { rect.bottom() } else { rect.top() };
                            ui.painter().hline(
                                rect.x_range(),
                                y,
                                egui::Stroke::new(2.0, ui.visuals().selection.bg_fill),
                            );
                        }
                    }
                }
                (None, true) => self.drag_src = None,
                _ => {}
            }
        }

        // --- Add new feature (full-width) → open the searchable palette -------
        ui.add_space(6.0);
        let add = ui.add_sized(
            [ui.available_width(), 26.0],
            egui::Button::new("Add new feature"),
        );
        self.hits.insert("add:menu".into(), add.rect);
        if add.clicked() {
            // The active workbench TRIMS the creation palette (a UI filter only —
            // the history/execution surface is untouched).
            let items = feature_palette_items(&state.settings.workbench);
            self.palette.open(items, "Add feature", "Search features…");
        }

        // --- apply deferred engine mutations (one per frame) ------------------
        // A reorder does NOT open the moved feature's form: a drag is a
        // restructuring gesture, and replacing the whole panel with a dialog
        // after one would hide the tree the user was just arranging.
        if let Some((src, to)) = drag_move {
            self.move_feature(state, src, to);
        } else if let Some(id) = delete {
            // A deleted feature can't be the open form's subject (the form has no
            // delete affordance), and if another surface deletes it the validity
            // guard in `show` falls back to the tree next frame.
            state.delete_feature(&id);
        } else if let Some((i, id)) = open_form {
            // Opening ROLLS to that feature, exactly as expanding it used to:
            // the dialog and the model it describes must agree.
            self.mode = PanelMode::Form { feature_id: id };
            state.roll_to(i);
        } else if let Some(i) = roll {
            state.roll_to(i);
        }

        // --- the command palette (a ctx-level modal; drawn last) --------------
        // A pick returns the chosen feature TYPE CODE; add that feature to the
        // engine-owned history with schema-derived defaults + a unique id.
        let ctx = ui.ctx().clone();
        if let Some(type_code) = self.palette.show(&ctx) {
            self.add_feature_of_type(state, &type_code);
        }
        // Republish the palette's widget rects (prefixed) so the headed verifier
        // can locate + drive the modal without the app shell knowing about it.
        let palette_hits: Vec<(String, egui::Rect)> = self
            .palette
            .hits()
            .iter()
            .map(|(k, r)| (format!("palette:{k}"), *r))
            .collect();
        self.hits.extend(palette_hits);
    }

    /// Move feature `from` to slot `to` via the engine's adjacent-swap reorder
    /// (each swap re-runs the truncated history — small N, and the ONE reorder
    /// primitive the engine exposes).
    fn move_feature(&mut self, state: &mut EngineState, from: usize, to: usize) {
        if from == to {
            return;
        }
        let mut cur = from;
        if to > from {
            while cur < to {
                state.reorder_feature(cur, false);
                cur += 1;
            }
        } else {
            while cur > to {
                state.reorder_feature(cur, true);
                cur -= 1;
            }
        }
    }

    /// Open the feature `id`'s FORM — the panel shows one form at a time, so
    /// this replaces whatever was open. The context action bar calls this via the
    /// shell after creating a feature from the selection or opening a selection's
    /// owning feature (both of which also rolled the model to that step), so the
    /// target feature's dialog is up for tweaking on the next frame. If the id
    /// does not resolve, the validity guard in [`Self::show`] drops straight back
    /// to the tree.
    pub fn focus_feature(&mut self, id: String) {
        self.mode = PanelMode::Form { feature_id: id };
    }

    /// Act on a schema `button` field click on a feature. `editSketch` on a SKETCH
    /// feature opens the engine-native sketcher on THAT feature (roll-to-before +
    /// plane orient) and returns the panel to the tree (the sketch-mode bar takes
    /// over the UI). `enter_sketch_mode` guards the feature is a sketch, so a
    /// stray click on a non-sketch is a harmless no-op.
    fn handle_feature_button(&mut self, state: &mut EngineState, feature_id: &str, key: &str) {
        match key {
            "editSketch" => match state.enter_sketch_mode(feature_id) {
                Ok(_) => self.mode = PanelMode::Tree,
                Err(_err) => {
                    #[cfg(not(target_arch = "wasm32"))]
                    eprintln!("Edit Sketch failed for '{feature_id}': {_err}");
                }
            },
            _ => {}
        }
    }

    /// The published widget hit-rects (egui points) for the headed verifier.
    pub fn hits_json(&self) -> String {
        crate::automation::hit_rects::hits_json(&self.hits)
    }

    /// Whether the palette requested a COMPONENT INSERT this frame (the ACOMP
    /// palette entry routes to the component selector, never a bare feature
    /// dialog). Consumed by the shell, which opens the file dialog's insert
    /// mode.
    pub fn take_insert_component_request(&mut self) -> bool {
        std::mem::take(&mut self.pending_insert_component)
    }

    /// Append a feature of type `type_code` to the engine-owned history: build a
    /// fresh descriptor whose `inputParams` are the schema DEFAULTS
    /// ([`features::feature_default_params`]) with an engine-unique `id` assigned,
    /// hand it to `EngineState::add_feature` (which appends + rolls to it), and
    /// open the new feature's FORM. Works for ANY registered feature type — the
    /// catalogue drives both the palette and the defaults.
    ///
    /// EXCEPTION — `ACOMP` (assembly component): inserting an instance needs a
    /// parts-library payload first, so the palette pick surfaces an
    /// insert-component REQUEST to the shell (which opens the component
    /// selector) instead of appending an empty feature that could only fail.
    pub(crate) fn add_feature_of_type(&mut self, state: &mut EngineState, type_code: &str) {
        if type_code == "ACOMP" {
            self.pending_insert_component = true;
            return;
        }
        let id = state.next_feature_id(&features::feature_short_name(type_code));
        let mut params = features::feature_default_params(type_code);
        if let Value::Object(map) = &mut params {
            map.insert("id".into(), Value::String(id.clone()));
        }
        let feature = serde_json::json!({
            "type": type_code, "inputParams": params, "persistentData": {}
        });
        if state.add_feature(&feature.to_string()).is_ok() {
            self.mode = PanelMode::Form { feature_id: id };
        }
    }
}

/// The history index of feature `id`, by linear scan over the engine's history.
/// `EngineState` exposes `feature_id_at` but no public `index_of`, and the panel
/// needs one for the form's per-frame validity guard (does the open form's
/// subject still exist?). Small N, once per frame.
fn feature_index_of(state: &EngineState, id: &str) -> Option<usize> {
    (0..state.history_len()).find(|i| state.feature_id_at(*i).as_deref() == Some(id))
}

/// Paint the ROLLBACK BAR: a full-width horizontal rule marking the step the model
/// is EXECUTED UP TO. It reuses the drag-reorder insertion indicator's look (a 2 px
/// line in the theme's selection accent — see the drag branch of
/// [`HistoryPanel::show`]) because it says the same thing: "the boundary is HERE".
/// Returns its row rect, which the panel publishes for the headed verifier.
fn rollback_bar(ui: &mut egui::Ui) -> egui::Rect {
    let (rect, _) = ui.allocate_exact_size(
        egui::vec2(ui.available_width(), ROLLBACK_BAR_H),
        egui::Sense::hover(),
    );
    ui.painter().hline(
        rect.x_range(),
        rect.center().y,
        egui::Stroke::new(2.0, ui.visuals().selection.bg_fill),
    );
    rect
}

/// Build one [`PaletteItem`] per registered feature from the kernel catalogue:
/// `id` = the feature TYPE CODE (e.g. `P.CU`), `label` = its long name (e.g.
/// `Primitive Cube`), `keywords` = the type code + short name (aliases the user
/// might type). The palette sorts them alphabetically by label on open.
///
/// `workbench` is the active workbench id: only entries that workbench INCLUDES
/// (each workbench classifies off the feature TYPE CODE) are offered. This is a
/// pure UI filter over CREATION — it does not touch the existing history, so a
/// document with sheet-metal features still shows and edits them in Modeling; only
/// the "Add new feature" list is trimmed.
fn feature_palette_items(workbench: &str) -> Vec<PaletteItem> {
    let catalogue = features::feature_catalogue();
    let mut items = Vec::new();
    if let Some(list) = catalogue.get("features").and_then(Value::as_array) {
        for feature in list {
            let ty = feature.get("type").and_then(Value::as_str).unwrap_or("");
            if ty.is_empty() {
                continue;
            }
            if !crate::workbench::includes_feature(workbench, ty) {
                continue;
            }
            // `feature_long_name` prepends the glyph; the palette sorts/searches
            // on a glyph-stripped key so it stays alphabetical.
            let long = features::feature_long_name(ty);
            let short = feature.get("shortName").and_then(Value::as_str).unwrap_or(ty);
            let mut keywords = vec![ty.to_string()];
            if short != ty {
                keywords.push(short.to_string());
            }
            items.push(PaletteItem::new(ty, long, keywords));
        }
    }
    items
}

// BREP private tests: 5a290dd8d45ead6c

/// The hit keys this panel publishes (see `automation::hit_keys`).
pub static HIT_KEYS: &[HitKeyDoc] = &[
    HitKeyDoc { panel: "history", prefix: "step:", meaning: "roll the history to feature i (step:i)", command: None },
    HitKeyDoc { panel: "history", prefix: "edit:", meaning: "open feature i's form (edit:i)", command: None },
    HitKeyDoc { panel: "history", prefix: "del:", meaning: "delete feature i (del:i)", command: None },
    HitKeyDoc { panel: "history", prefix: "box:", meaning: "feature i's visibility box (box:i)", command: None },
    HitKeyDoc { panel: "history", prefix: "add:menu", meaning: "open the add-feature palette", command: None },
    HitKeyDoc { panel: "history", prefix: "palette:", meaning: "add the feature of that type from the open palette", command: None },
    HitKeyDoc { panel: "history", prefix: "form:return", meaning: "close the open form and roll to the tip", command: None },
    HitKeyDoc { panel: "history", prefix: "form:feature", meaning: "the open form's title row (form:feature) and its feature chip (form:feature:id)", command: None },
    HitKeyDoc { panel: "history", prefix: "form:feature:", meaning: "the open form's feature chip (form:feature:id)", command: None },
    HitKeyDoc { panel: "history", prefix: "gizmo-anchor", meaning: "the open form's gizmo anchor control", command: None },
    HitKeyDoc { panel: "history", prefix: "form:section:", meaning: "toggle an accordion section of the open form", command: None },
    HitKeyDoc { panel: "history", prefix: "field:", meaning: "a field of the open form, by param path", command: None },
    HitKeyDoc { panel: "history", prefix: "form:trailing:", meaning: "a read-only line of the open form (form:trailing:Outputs:i) — hovering it lights that entity", command: None },
    HitKeyDoc { panel: "history", prefix: "run:cancel", meaning: "cancel the in-flight run", command: None },
    HitKeyDoc { panel: "history", prefix: "run:spinner", meaning: "the in-flight run indicator", command: None },
    HitKeyDoc { panel: "history", prefix: "rollback:bar", meaning: "the executed-up-to indicator", command: None },
    HitKeyDoc { panel: "history", prefix: "panel:clip", meaning: "the visible region of the pane", command: None },
];