BREP_app 0.8.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
//! Where the PLM panels live in the shell (plm-cad-integration-todo ยง3 S3 part 2).
//!
//! # The shape every PLM panel uses
//!
//! - **One dock pane**, [`crate::panels::dock::PaneKind::Plm`], titled "PLM". It
//!   is in the tree like every side pane and shown only while the session is on
//!   a PLM ([`in_plm_session`], a [`crate::workbench::PanelCondition`]). A
//!   file-based session never sees it.
//! - **One host**, [`PlmHost`], owned by the shell. It holds every PLM panel as
//!   a field and draws each as a section of that pane. A slice that adds a panel
//!   adds a field, a section in [`PlmHost::ui`], and the routing of its events
//!   in [`PlmHost::route`] โ€” nothing in `dock.rs` or `app.rs`.
//! - **The client** is the store's ([`crate::store::ModelStore::plm_client`]),
//!   handed in each frame by [`PlmHost::sync`]. Panels never build one.
//! - **Events between panels** go through the host: the lifecycle panel's
//!   `OpenReview` is the review section's to act on; an `Open` of another
//!   revision is the shell's, returned in [`HostActions`].
//!
//! # What the lifecycle section does to a document
//!
//! PLM permissions govern saving to the revision. The session copy stays
//! editable while permissions load, without checkout, and after release.
//! Checkout refreshes a clean copy from the store; unsaved session edits
//! are preserved through every permission change.

use std::collections::HashMap;
use std::rc::Rc;

use eframe::egui;

use crate::document::{Access, Documents};
use crate::panels::plm::{access_for, PanelEvent, PlmLifecycle, PlmPanel, Rights};
use crate::plm::client::PlmClient;
use crate::plm::PlmFuture;
use crate::store::ModelStore;

mod save;

/// A link to the configured PLM web app, for management outside CAD.
pub fn web_url(store: &dyn ModelStore, route: &str) -> Option<String> {
    let session = store.plm_session()?;
    let url = match session.status {
        crate::plm::connection::Status::Connected { url, .. }
        | crate::plm::connection::Status::NotConnected { url, .. } => url,
    };
    Some(format!("{}/{route}", url.trim_end_matches('/')))
}

pub fn web_link(ui: &mut egui::Ui, label: &str, url: String) -> egui::Response {
    ui.add(egui::Hyperlink::from_label_and_url(label, url).open_in_new_tab(true))
}

thread_local! {
    /// Whether the session is on a PLM, as of the last [`PlmHost::sync`] โ€” what
    /// the PLM pane's [`crate::workbench::PanelCondition`] reads, since a
    /// panel condition sees only the engine.
    static IN_SESSION: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// The PLM pane's panel condition: shown only while the session is on a PLM.
pub fn in_plm_session(_: &crate::workbench::ButtonState) -> bool {
    IN_SESSION.with(|flag| flag.get())
}

/// The access a PLM document has before the server has said anything.
pub fn pending_access() -> Access {
    Access::read_only("the revision is being read from the PLM")
}

/// `part/<part>/rev/<revision>` read out of a document name as the explorer
/// spells it (`/models/part/<p>/rev/<r>.nbrep`, or the bare key). Only a PLM
/// session may read names this way: the shape alone is not proof.
pub fn revision_of(name: &str) -> Option<(String, String)> {
    let start = name.find("part/")?;
    let key = &name[start..];
    let key = key.strip_suffix(".nbrep").or_else(|| key.strip_suffix(".fbrep"))
        .or_else(|| key.strip_suffix(".tbrep")).unwrap_or(key);
    let crate::store::DocumentIdentity::Revision { part: part_id, revision: revision_id } =
        crate::store::DocumentIdentity::parse_revision_key(key)? else { return None };
    Some((part_id, revision_id))
}

/// The name to open another revision of the same part under: `name` with its
/// revision swapped, so the explorer's prefix and the class's extension stay.
pub fn sibling_revision(name: &str, key: &str) -> Option<String> {
    let start = name.find("part/")?;
    let (_, new_revision) = revision_of(key)?;
    let (_, old_revision) = revision_of(name)?;
    let old_revision = crate::plm::identity::segment(&old_revision);
    let new_revision = crate::plm::identity::segment(&new_revision);
    let tail = &name[start..];
    let at = tail.rfind(&old_revision)?;
    Some(format!("{}{}{}{}", &name[..start], &tail[..at], new_revision, &tail[at + old_revision.len()..]))
}

/// The PLM pane's sections, in the order they draw. Each id is the section's
/// hit-key prefix (`plm/<id>:โ€ฆ`) and its header's id, so both stay stable as
/// sections are added. A slice adding a section adds its id here.
/// The PLM pane's published keys (`__brepPlmHit`, `plm/<key>`): each section's own keys,
/// under the section's id, and every section's header. One family per section, so a
/// section that grows a widget stays documented.
pub static HIT_KEYS: &[crate::automation::hit_keys::HitKeyDoc] = &[
    crate::automation::hit_keys::HitKeyDoc { panel: "plm", prefix: "panel:clip", meaning: "the visible PLM pane region", command: None },
    crate::automation::hit_keys::HitKeyDoc { panel: "plm", prefix: "section:", meaning: "section:lifecycle, the revision browser and checkout", command: None },
    crate::automation::hit_keys::HitKeyDoc { panel: "plm", prefix: "lifecycle:", meaning: "lifecycle:revision:<label> opens a revision; lifecycle:verb:check-out acquires its lock", command: None },
    crate::automation::hit_keys::HitKeyDoc { panel: "plm", prefix: "save:", meaning: "checkout-on-save consent: save:yes, save:no, save:remember", command: None },
    crate::automation::hit_keys::HitKeyDoc { panel: "plm", prefix: "web:", meaning: "management links opening the PLM web app in another tab: summary, revisions, attachments, structure, history, parts, workspace, reviews", command: None },
];

pub const SECTIONS: &[(&str, &str)] = &[(LIFECYCLE, "Revision")];

/// The uses-list section (S5): shown only while the active assembly's parts
/// list on the PLM disagrees with its document (Replace everywhere), with the
/// offer to re-point.
pub const USES: &str = "uses";
/// The parts section (S7): the catalog browser (Insert) and New part.
pub const PARTS: &str = "parts";
/// The import section (S13): adopt a folder of documents.
pub const IMPORT: &str = "import";

/// The where-used section: every assembly that uses the part, every level up
/// (S6's `panels::bom_plm::WhereUsedSection`). Its lines are also drawn in the
/// lifecycle section's New revision confirmation.
pub const WHERE_USED: &str = "where-used";

/// The review section: a revision's review round (S4).
pub const REVIEW: &str = "review";

/// The lifecycle section: the revision picker and the checkout verbs (S3).
pub const LIFECYCLE: &str = "lifecycle";

/// The files section: the part's and revision's attachments, and this
/// document's drawing PDF or STEP attached to the revision (S8).
pub const ATTACHMENTS: &str = "attachments";
/// The workspace section: the user's folders of links and files, and open by number (S14).
/// The same browser is File > Open in a PLM session.
pub const WORKSPACE: &str = "workspace";

/// The pane's widget rects, published as `__brepPlmHit` (`plm/<key>`).
pub type Hits = HashMap<String, egui::Rect>;

/// One section's view of [`Hits`]: every key it puts is prefixed with the
/// section's id, so two sections can never collide.
pub struct SectionHits<'a> {
    prefix: &'static str,
    map: &'a mut Hits,
}

impl<'a> SectionHits<'a> {
    pub fn new(prefix: &'static str, map: &'a mut Hits) -> Self {
        Self { prefix, map }
    }

    pub fn put(&mut self, key: &str, rect: egui::Rect) {
        self.map.insert(format!("{}:{key}", self.prefix), rect);
    }
}

/// A collapsible section of the PLM pane with a stable id: open by default,
/// its header's rect published as `plm/section:<id>`.
pub fn section<R>(ui: &mut egui::Ui, hits: &mut Hits, id: &'static str, add: impl FnOnce(&mut egui::Ui, &mut SectionHits<'_>) -> R) -> Option<R> {
    let title = SECTIONS.iter().find(|(key, _)| *key == id).map_or(id, |(_, title)| *title);
    let mut inner = Hits::new();
    let shown = egui::CollapsingHeader::new(title)
        .id_salt(("plm-section", id))
        .default_open(true)
        .show(ui, |ui| add(ui, &mut SectionHits::new(id, &mut inner)));
    hits.insert(format!("section:{id}"), shown.header_response.rect);
    hits.extend(inner);
    shown.body_returned
}

/// What the shell must do after [`PlmHost::sync`].
#[derive(Debug, Default, PartialEq)]
pub struct HostActions {
    /// Open these document names (another revision the user picked, or the
    /// one a New revision started).
    pub open: Vec<String>,
    /// A section asked for a file: open the file chooser for (tag, title),
    /// and hand what it delivers to [`PlmHost::deliver_picked`].
    pub pick_file: Option<(String, String)>,
    /// The user decided, submitted, commented or released: ask the review
    /// inbox again (`ToolbarPanel::refresh_inbox`).
    pub refresh_inbox: bool,
}

/// The file-chooser tag of the workspace section.
pub const PICK_WORKSPACE: &str = "plm:workspace";
/// The file-chooser tag prefix of the attachments section: `plm:attachments:<document id>`.
pub const PICK_ATTACHMENTS: &str = "plm:attachments:";

/// Every PLM panel, and the one client they share.
#[derive(Default)]
pub struct PlmHost {
    save: save::SaveCheckout,
    web_base: String,
    client: Option<Rc<PlmClient>>,
    rights: Option<Rights>,
    rights_pending: Option<PlmFuture<Rights>>,
    /// The lifecycle panel of each open PLM document, by document id.
    lifecycle: HashMap<u64, PlmPanel>,
    /// Events a panel raised that another section acts on. S4's review section
    /// drains `OpenReview` here.
    pub routed: Vec<PanelEvent>,
    /// What the user picked while the pane drew (another revision to open),
    /// applied by the next [`Self::sync`], which knows the store.
    picked: Vec<PanelEvent>,
    /// This frame's widget rects ([`Self::hits_json`]).
    hits: Hits,
    /// The file lane's save count last seen ([`Self::sync`]'s `saves`).
    saves_seen: Option<u64>,
    /// Documents just saved whose revision is being re-read, to warn (D8) from
    /// the server's current word rather than a view another client may have
    /// moved on.
    saved_pending: Vec<u64>,
    /// Where the active document's part is used (S6).
    where_used: crate::panels::bom_plm::WhereUsedSection,
    /// The files section (S8): one attachments panel per open PLM document.
    attachments: crate::panels::plm_attachments::AttachmentsSection,
    /// The workspace section (S14).
    workspace: crate::panels::plm_workspace::WorkspaceBrowser,
    /// What the workspace asked for while the pane drew โ€” document keys to open, a
    /// file to save โ€” applied by the next [`Self::sync`], which knows the store.
    workspace_asked: Vec<crate::panels::plm_workspace::WorkspaceOutcome>,
    /// Following other clients live: the change feed, asked once per
    /// interval (`panels::plm_follow`).
    follower: crate::panels::plm_follow::Follower,
    /// When this host first synced, the follower's clock.
    started: Option<web_time::Instant>,
    /// The feed moved since the shell last asked ([`Self::take_followed`]).
    followed: bool,
    /// The re-point check on open (S5) and the answers given while the pane
    /// drew, applied by the next [`Self::sync`].
    repoint: crate::panels::plm_import::RepointCheck,
    repoint_asked: Vec<(u64, crate::panels::plm_import::RepointChoice)>,
    /// The parts section (S7), and what it asked for while it drew.
    parts: crate::panels::plm_parts::PlmPartsPanel,
    parts_asked: Vec<crate::panels::plm_parts::PlmPartsOutcome>,
    /// Parts picked for Insert, by store name and number, waiting for their
    /// document to be readable (an index-hydrated store loads it on demand).
    inserting: Vec<(String, String)>,
    /// A New part's first revision being written with the active document;
    /// its store name is opened when it lands.
    uploading: Option<(crate::panels::plm_parts::Pending<()>, String)>,
    /// The import section (S13).
    import: crate::panels::plm_import::PlmImportPanel,
    /// The review section (S4).
    review: crate::panels::plm_review::ReviewSection,
    /// Reviews asked for โ€” by the lifecycle section, the
    /// toolbar's inbox โ€” opened by the next [`Self::sync`], which has the client.
    review_asked: Vec<crate::panels::plm_review::PlmReviewEvent>,
}

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

    /// Once a frame, before the dock draws: take the store's client, give each
    /// PLM document its lifecycle panel, drive every panel's request, and apply
    /// what they answer to the documents.
    ///
    /// `saves` is the file lane's save count (`FileDialog::save_generation`):
    /// when it moves, the active document was just saved, and a save of a
    /// revision in review warns (D8, S4's `plm_review::save_warning`) โ€” it is
    /// never refused.
    pub fn sync(&mut self, store: &dyn ModelStore, docs: &mut Documents, saves: u64) -> HostActions {
        self.web_base = web_url(store, "").unwrap_or_default();
        let client = store.plm_client();
        IN_SESSION.with(|flag| flag.set(client.is_some()));
        let Some(client) = client else {
            self.client = None;
            self.lifecycle.clear();
            return HostActions::default();
        };
        if !self.client.as_ref().is_some_and(|c| Rc::ptr_eq(c, &client)) {
            self.client = Some(client.clone());
            self.rights = None;
            self.rights_pending = Some(client.rights());
            self.lifecycle.clear();
            self.workspace = crate::panels::plm_workspace::WorkspaceBrowser::new();
            self.repoint = crate::panels::plm_import::RepointCheck::new();
            self.parts = crate::panels::plm_parts::PlmPartsPanel::new();
            self.import = crate::panels::plm_import::PlmImportPanel::new();
            self.review = crate::panels::plm_review::ReviewSection::new();
        }
        self.sync_sections(&client, store, docs);
        let mut actions = self.sync_with(&client, store, docs, saves);
        // The review section (S4): what the lifecycle section and the
        // toolbar's inbox asked to open, and the open pane's answers.
        let reviews = crate::panels::plm_review::PlmReviewClient { client: client.clone() };
        for event in std::mem::take(&mut self.review_asked) {
            self.review.open(&event, &reviews);
        }
        self.review.poll(&reviews);
        actions.refresh_inbox = self.review.take_acted();
        // Follow other clients: when anything moved on the server, re-read
        // every open PLM document's part (a panel already reading is skipped).
        let now = self.started.get_or_insert_with(web_time::Instant::now).elapsed().as_secs_f64();
        if let Some(moved) = self.follower.tick(now, &client) {
            self.parts.refresh_catalog(&crate::panels::plm_parts::PlmPartCatalog::new(client.clone()));
            if self.workspace.entries.is_some() {
                self.workspace.reload(&crate::panels::plm_workspace::PlmWorkspaces { client: client.clone() });
            }
            for panel in self.lifecycle.values_mut() {
                panel.refresh(&client);
            }
            // The store's held index rows (the BOM's PLM columns): exactly the
            // revisions the feed named, the whole index only when stale.
            store.refresh_plm_index(&moved.keys, moved.stale);
            self.followed = true;
        }
        actions
    }

    /// Open a review in the review section (the toolbar's
    /// inbox rows). Opened by the next [`Self::sync`].
    pub fn open_review(&mut self, event: crate::panels::plm_review::PlmReviewEvent) {
        self.review_asked.push(event);
    }

    /// The review section's state, for tests (nothing draws the section since
    /// the pane split: review management is the web app's; no state blob).
    pub fn review_json(&self) -> String {
        self.review.state_json().to_string()
    }

    /// The feed moved since the last call: the shell asks the inbox again.
    pub fn take_followed(&mut self) -> bool {
        std::mem::take(&mut self.followed)
    }

    /// Seconds until the pane asks the feed again (the shell repaints then);
    /// `None` outside a PLM session.
    pub fn next_follow_in(&self) -> Option<f64> {
        let now = self.started?.elapsed().as_secs_f64();
        self.client.as_ref().map(|_| self.follower.next_in(now))
    }

    /// Ask the feed at the next sync instead of at the interval (scripts).
    pub fn follow_now(&mut self) {
        self.follower.ask_now();
    }

    /// The follower, for the state blob and tests.
    pub fn follower(&self) -> &crate::panels::plm_follow::Follower {
        &self.follower
    }

    /// [`Self::sync`] over any server โ€” the tests' fake, or the real client.
    pub fn sync_with(&mut self, server: &dyn PlmLifecycle, store: &dyn ModelStore, docs: &mut Documents, saves: u64) -> HostActions {
        let mut actions = HostActions::default();
        // The file chooser exists natively (this machine's files) and in the
        // browser (the page's file input): every section may ask for a file.
        self.attachments.can_pick_files = true;
        self.workspace.can_pick_files = true;
        if let Some(id) = self.attachments.take_pick_request() {
            actions.pick_file = Some((format!("{PICK_ATTACHMENTS}{id}"), "Attach a file".into()));
        }
        // What the workspace section asked for while the pane drew: a link's document to
        // open (under the name the store spells it), a file to save.
        if self.workspace.pins.is_none() {
            self.workspace.pins = Some(crate::panels::plm_workspace::load_pins(store));
        }
        for asked in std::mem::take(&mut self.workspace_asked) {
            if let Some(pins) = &asked.pins {
                if let Err(e) = crate::panels::plm_workspace::save_pins(store, pins) {
                    self.workspace.notice = Some(format!("could not save the pinned folders: {e}"));
                }
            }
            if asked.pick_file {
                actions.pick_file = Some((PICK_WORKSPACE.into(), "Add a file to this folder".into()));
            }
            if let Some(key) = asked.open {
                actions.open.push(crate::panels::plm_workspace::document_name(store, &key));
            }
            if let Some((name, bytes)) = asked.downloaded {
                self.workspace.notice = Some(match store.export_file_named_bytes(&name, &bytes) {
                    Ok(()) => format!("saved {name}"),
                    Err(e) => format!("could not save {name}: {e}"),
                });
            }
        }
        let saved = self.saves_seen.is_some_and(|seen| seen != saves);
        self.saves_seen = Some(saves);
        if saved {
            self.saved_pending.push(docs.active().id());
        }
        for id in &self.saved_pending {
            if let Some(panel) = self.lifecycle.get_mut(id) {
                panel.refresh(server);
            }
        }
        if let Some(pending) = self.rights_pending.as_mut() {
            let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
            if let std::task::Poll::Ready(answer) = pending.as_mut().poll(&mut cx) {
                self.rights_pending = None;
                // A refusal leaves the rights empty: the panel offers nothing,
                // and the server would refuse every verb anyway.
                self.rights = Some(answer.unwrap_or_default());
            }
        }
        let open: Vec<(u64, String)> = docs.iter().filter_map(|d| Some((d.id(), d.name()?.to_string()))).collect();
        // Picks from the pane last frame belong to the document that was active.
        let active = docs.active().id();
        if let Some((_, name)) = open.iter().find(|(id, _)| *id == active).cloned() {
            for event in std::mem::take(&mut self.picked) {
                self.apply(event, active, &name, store, docs, &mut actions);
            }
        }
        self.lifecycle.retain(|id, panel| open.iter().any(|(open_id, name)| {
            open_id == id && revision_of(name).is_some_and(|(part, revision)| panel.matches_document(&part, &revision))
        }));
        for (id, name) in &open {
            let Some((part, revision)) = revision_of(name) else { continue };
            if !self.lifecycle.contains_key(id) {
                let Some(rights) = self.rights else {
                    // Pending rights restrict saving, never session editing.
                    set_access(docs, *id, pending_access());
                    continue;
                };
                // A successful Save As already knows its destination is checked out.
                // Keep that permission while fetching metadata, so stale cached bytes
                // cannot replace the session copy as though this were a new checkout.
                if !docs.iter().find(|d| d.id() == *id).is_some_and(crate::document::Document::has_plm_save_access) {
                    set_access(docs, *id, pending_access());
                }
                self.lifecycle.insert(*id, PlmPanel::new(server, &part, &revision, rights));
            }
            let Some(panel) = self.lifecycle.get_mut(id) else { continue };
            for event in panel.poll(server) {
                self.apply(event, *id, name, store, docs, &mut actions);
            }
        }
        let settled: Vec<u64> = self
            .saved_pending
            .iter()
            .copied()
            .filter(|id| self.lifecycle.get(id).is_none_or(|p| !p.busy()))
            .collect();
        self.saved_pending.retain(|id| !settled.contains(id));
        for id in settled {
            self.warn_saved_in_review(docs, id);
        }
        // The files section's exports and downloads need the documents and the
        // store mutably, which drawing does not have.
        self.attachments.sync(store, docs);
        self.route();
        actions
    }

    /// The S5 / S7 / S13 sections' store-and-document work for this frame:
    /// the re-point check and its answers, Insert and New part, the import.
    fn sync_sections(&mut self, client: &Rc<PlmClient>, store: &dyn ModelStore, docs: &mut Documents) {
        let waker = std::task::Waker::noop();
        self.parts.browser.poll(waker);
        // The workspace's requests are driven here too, not only while its section is
        // drawn: a request another section started (a New part linked into the folder)
        // must land with the pane hidden.
        {
            let plm = crate::panels::plm_workspace::PlmWorkspaces { client: client.clone() };
            let mut outcome = crate::panels::plm_workspace::WorkspaceOutcome::default();
            self.workspace.poll(waker, &plm, &mut outcome);
            if outcome != crate::panels::plm_workspace::WorkspaceOutcome::default() {
                self.workspace_asked.push(outcome);
            }
        }
        self.repoint.sync(client, docs, waker);
        for (id, choice) in std::mem::take(&mut self.repoint_asked) {
            if let Err(problem) = self.repoint.answer(docs, id, choice) {
                docs.engine_mut().push_notice(format!("Re-point: {problem}"));
            }
        }
        for outcome in std::mem::take(&mut self.parts_asked) {
            // "Link <number> to workspace" from the catalog: into the folder the
            // workspace section shows, following the newest revision.
            if let Some(row) = &outcome.link {
                let plm = crate::panels::plm_workspace::PlmWorkspaces { client: client.clone() };
                self.workspace.link(&plm, &row.id, "");
            }
            if let Some(row) = outcome.insert {
                let key = crate::plm::identity::document_key(&row.id, &row.latest_revision_id);
                self.inserting.push((crate::panels::plm_workspace::document_name(store, &key), row.number));
            }
            if let Some(created) = outcome.created {
                // D9: the SERVER linked the new part into the workspace folder the
                // user is in (the request's `workspace_folder`), following its newest
                // revision; show it there.
                let plm = crate::panels::plm_workspace::PlmWorkspaces { client: client.clone() };
                self.workspace.reload(&plm);
                if let Some(key) = created.first_revision_key() {
                    let (client, document) = (client.clone(), docs.engine().history_request_json());
                    let at = key.clone();
                    let write: crate::plm::PlmFuture<()> =
                        Box::pin(async move { crate::plm::adoption::load_into_revision(&client, &at, &document).await });
                    self.uploading = Some((crate::panels::plm_parts::Pending::new(write), crate::panels::plm_workspace::document_name(store, &key)));
                }
            }
        }
        self.inserting.retain(|(name, number)| match crate::store::read_now(store, name) {
            crate::store::ReadNow::Ready(contents) => {
                let doc = docs.active_mut();
                if !doc.access().is_editable() {
                    doc.engine.push_notice(format!("Insert {number}: check this document out first"));
                    return false;
                }
                let inserted = doc.engine.insert_component(brep_render::engine_state::ComponentInsert::New {
                    name: number,
                    source_key: name,
                    source_signature: &crate::panels::parts_library::document_signature(&contents),
                    document_json: &contents,
                });
                if let Err(problem) = inserted {
                    doc.engine.push_notice(format!("Insert {number}: {problem}"));
                }
                false
            }
            // Listed but not loaded: the read just asked for it.
            crate::store::ReadNow::Loading => true,
            crate::store::ReadNow::Absent => {
                docs.engine_mut().push_notice(format!("Insert {number}: its newest revision has no document yet"));
                false
            }
        });
        if let Some(answer) = self.uploading.as_mut().and_then(|(pending, _)| pending.poll(waker)) {
            let (_, name) = self.uploading.take().unwrap();
            match answer {
                Ok(()) => docs.engine_mut().push_notice_as(
                    brep_render::engine_state::NoticeSeverity::Info,
                    format!("This document is now the new part's first revision: {name}"),
                ),
                Err(problem) => docs.engine_mut().push_notice(format!("The part was created, but this document was not written into it: {problem}")),
            }
        }
        let server = crate::plm::connection::parse_label(&store.backend_label()).map(|(url, _)| url).unwrap_or_default();
        // The folder is read from this machine's files, never the PLM's.
        self.import.sync(store, store.local_files(), Some(client.clone()), &server, waker);
    }

    fn apply(
        &mut self,
        event: PanelEvent,
        id: u64,
        name: &str,
        store: &dyn ModelStore,
        docs: &mut Documents,
        actions: &mut HostActions,
    ) {
        match event {
            PanelEvent::Access(access) => {
                let was_editable = docs.iter().find(|d| d.id() == id).is_some_and(|d| d.save_access().is_editable());
                set_access(docs, id, access.clone());
                let unsaved_edits = docs.iter().find(|d| d.id() == id).is_some_and(|doc| doc.is_dirty());
                if access.is_editable() && !was_editable && !unsaved_edits {
                    reload(docs, id, name, store);
                }
            }
            PanelEvent::Open(key) => {
                if let Some(sibling) = sibling_revision(name, &key) {
                    actions.open.push(sibling);
                }
            }
            other => self.routed.push(other),
        }
    }

    /// D8: the active document was saved; if its revision is in review, say
    /// what that save means for the reviewers.
    fn warn_saved_in_review(&self, docs: &mut Documents, id: u64) {
        let Some(revision) = self.lifecycle.get(&id).and_then(PlmPanel::revision) else { return };
        let Some(warning) = crate::panels::plm_review::save_warning(&revision.label, &revision.lifecycle) else { return };
        if let Some(doc) = docs.iter_mut().find(|d| d.id() == id) {
            doc.engine.push_notice_as(brep_render::engine_state::NoticeSeverity::Warning, warning);
        }
    }

    /// Hand the routed events to whichever section acts on them: the
    /// lifecycle section's Submit for review, Reviewโ€ฆ
    /// open the review section. Run at the end of every [`Self::sync_with`].
    pub fn route(&mut self) {
        for event in std::mem::take(&mut self.routed) {
            if let Some(event) = crate::panels::plm_review::PlmReviewEvent::from_lifecycle(&event) {
                self.review_asked.push(event);
            }
        }
    }

    /// Whether a PLM request any section made is still unanswered: the idle
    /// contract waits for it (`automation::cmd_frame`), and the shell keeps
    /// repainting until it lands.
    pub fn busy(&self) -> bool {
        self.rights_pending.is_some()
            || self.lifecycle.values().any(PlmPanel::busy)
            || self.attachments.busy()
            || self.workspace.busy()
            || self.follower.busy()
            || self.repoint.busy()
            || self.parts.busy()
            || !self.inserting.is_empty()
            || self.uploading.is_some()
            || self.import.running()
            || self.where_used.busy()
            || self.review.busy()
            || !self.review_asked.is_empty()
    }

    /// Open the parts section on its New part form: Save As โ†’ "A new part" (D9). The
    /// form writes the active document into the new part's first revision, and the
    /// server links the part into the workspace folder shown.
    pub fn open_new_part(&mut self) {
        self.parts.tab = crate::panels::plm_parts::Tab::NewPart;
    }

    /// The lifecycle panel of document `id`, if it is a PLM document.
    pub fn lifecycle(&self, id: u64) -> Option<&PlmPanel> {
        self.lifecycle.get(&id)
    }

    /// The same, to press a verb on (the automation layer, tests).
    pub fn lifecycle_mut(&mut self, id: u64) -> Option<&mut PlmPanel> {
        self.lifecycle.get_mut(&id)
    }

    /// Draw the PLM pane for the active document.
    pub fn ui(&mut self, ui: &mut egui::Ui, docs: &Documents) {
        self.hits.clear();
        // The pane's visible region: `click_widget` wheels the pane until a
        // widget is inside it, so a section below the fold is reachable.
        self.hits.insert("panel:clip".into(), ui.clip_rect());
        let Some(client) = self.client.clone() else {
            ui.label("Not connected to a PLM.");
            return;
        };
        let active = docs.active_id();
        let mut target = None;
        section(ui, &mut self.hits, LIFECYCLE, |ui, hits| {
            let Some(panel) = self.lifecycle.get_mut(&active) else {
                ui.label("Open a PLM part using File โ†’ Open.");
                return;
            };
            if let Some(part) = panel.part().cloned() {
                ui.heading(format!("{} ยท {}", part.number, part.name));
                if let Some(revision) = panel.revision() {
                    target = Some((part.id.clone(), revision.id.clone()));
                    match access_for(revision) {
                        Access::Editable => { ui.label("Checked out by you"); }
                        Access::ReadOnly { reason } => { ui.colored_label(ui.visuals().warn_fg_color, format!("Session edits allowed; saving unavailable: {reason}")); }
                    }
                }
                ui.label("Open revision");
                for revision in &part.revision_views {
                    let selected = panel.revision().is_some_and(|r| r.id == revision.id);
                    let button = ui.selectable_label(selected, &revision.label);
                    hits.put(&format!("revision:{}", revision.label), button.rect);
                    if button.clicked() && !selected {
                        self.picked.push(PanelEvent::Open(revision.document_key.clone()));
                    }
                }
                if panel.offered().contains(&crate::panels::plm::Verb::CheckOut) {
                    let button = ui.button("Check out");
                    hits.put("verb:check-out", button.rect);
                    if button.clicked() { panel.press(&client, crate::panels::plm::Verb::CheckOut); }
                }
            }
            if panel.busy() { ui.spinner(); ui.ctx().request_repaint(); }
            if let Some(message) = panel.message() { ui.label(message); }
        });
        ui.separator();
        ui.label("Manage in PLM");
        if let Some((part, revision)) = target {
            let route = format!("#/part/{}/{}", crate::plm::identity::segment(&part), crate::plm::identity::segment(&revision));
            for (label, tab) in [("Part details, check-in and release", "summary"), ("Revisions", "revisions"), ("Attachments", "attachments"), ("BOM and where used", "structure"), ("History", "history")] {
                let link = web_link(ui, label, format!("{}{route}/{tab}", self.web_base));
                self.hits.insert(format!("web:{tab}"), link.rect);
            }
        }
        for (label, route) in [("Parts and new parts", "parts"), ("Workspace", "workspace"), ("Reviews and approvals", "reviews")] {
            let link = web_link(ui, label, format!("{}#/{route}", self.web_base));
            self.hits.insert(format!("web:{route}"), link.rect);
        }
    }

    pub fn pick_tags(&self) -> Vec<String> {
        let mut tags = vec![PICK_WORKSPACE.to_string()];
        tags.extend(self.attachments.panel_ids().into_iter().map(|id| format!("{PICK_ATTACHMENTS}{id}")));
        tags
    }

    /// The file the chooser delivered for `tag`: staged on the attachments
    /// panel that asked, or added to the workspace folder shown.
    pub fn deliver_picked(&mut self, tag: &str, file: crate::panels::file::PickedFile) {
        let Some(client) = self.client.clone() else { return };
        if tag == PICK_WORKSPACE {
            let plm = crate::panels::plm_workspace::PlmWorkspaces { client };
            self.workspace.add_file(&plm, &file.name, file.bytes);
        } else if let Some(id) = tag.strip_prefix(PICK_ATTACHMENTS).and_then(|id| id.parse::<u64>().ok()) {
            self.attachments.stage_picked(id, &file.name, file.bytes);
        }
    }

    /// `__brepPlmHit`: the pane's widget rects.
    pub fn hits_json(&self) -> String {
        crate::automation::hit_rects::hits_json(&self.hits)
    }

    /// `__brepPlmDoc`: the active document's standing, for scripts. Null outside
    /// a PLM session.
    pub fn state_json(&self, docs: &Documents) -> String {
        if self.client.is_none() {
            return "null".into();
        }
        let doc = docs.active();
        let access = match doc.save_access() {
            Access::Editable => serde_json::json!({ "editable": doc.access().is_editable(), "canSave": true }),
            Access::ReadOnly { reason } => serde_json::json!({ "editable": doc.access().is_editable(), "canSave": false, "reason": reason }),
        };
        let panel = self.lifecycle.get(&doc.id());
        let revision = panel.and_then(|p| p.revision()).map(|r| {
            serde_json::json!({
                "id": r.id, "label": r.label, "lifecycle": r.lifecycle,
                "lockedBy": r.locked_by, "lockedByMe": r.locked_by_me,
            })
        });
        serde_json::json!({
            "access": access,
            "plmDocument": panel.is_some(),
            "revision": revision,
            "busy": panel.is_some_and(|p| p.busy()),
            "message": panel.and_then(|p| p.message()),
            "offered": panel.map(|p| p.offered().iter().filter(|v| **v == crate::panels::plm::Verb::CheckOut).map(|v| v.key()).collect::<Vec<_>>()).unwrap_or_default(),
            // The Files section (S8): what is staged, listed, refused.
            "files": self.attachments.state_json(doc.id()),
            // The Parts list (S5): whether this document disagrees with its
            // revision's uses list, and how many re-points are offered.
            "uses": {
                "checking": self.repoint.busy(),
                "disagrees": self.repoint.disagreement(doc.id()).is_some(),
                "repoints": self.repoint.disagreement(doc.id()).map_or(0, |d| d.repoints.len()),
            },
            // The Parts section (S7).
            "parts": self.parts.state_json(),
            "workspaceThumbnails": self.workspace.thumbnail_entries(),
            // The Import section (S13).
            "import": self.import.state_json(),
            // The Where used section (S6): the part's assemblies, every level.
            "whereUsed": self.where_used.state_json(),
        })
        .to_string()
    }
}

fn set_access(docs: &mut Documents, id: u64, access: Access) {
    if let Some(doc) = docs.iter_mut().find(|d| d.id() == id) {
        if doc.save_access() != access || !doc.access().is_editable() {
            doc.set_plm_save_access(access);
        }
    }
}

/// Re-read a document that just became editable, if the store holds other
/// bytes than it shows: someone saved it since it was opened.
fn reload(docs: &mut Documents, id: u64, name: &str, store: &dyn ModelStore) {
    let Some(text) = store.read(name) else { return };
    let Some(doc) = docs.iter_mut().find(|d| d.id() == id) else { return };
    let same = serde_json::from_str::<serde_json::Value>(&text).ok()
        == serde_json::from_str::<serde_json::Value>(&doc.engine.history.request_json()).ok();
    if same {
        return;
    }
    if doc.engine.set_history_json(&text).is_ok() {
        doc.mark_clean();
    }
}