lingxia-surface 0.16.0

Core surface presentation primitives (roles, kinds, positions) for the LingXia framework
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
//! `SurfaceManager` — the stateful per-window driver platforms bind to.
//!
//! Wraps a [`SurfaceGraph`] with the current size band and arbitration policy:
//! open/close requests go through the pure arbiter, width changes resolve the
//! `SizeClass` with hysteresis, and `derive()` produces the `DerivedLayout` the
//! skin renders. All layout decisions stay in the shared core; the platform
//! only maps legacy primitives in and binds the output.

use std::collections::HashMap;

use crate::arbitrate::{OpenOutcome, Policy, arbitrate};
use crate::graph::SurfaceGraph;
use crate::layout::{DEFAULT_HYSTERESIS, DerivedLayout, LayoutPresentationPlan, SizeClass};
use crate::model::{Surface, SurfaceId};
use crate::{CloseOutcome, ReplaceMainsError, Role, SurfacePresentation, SurfaceSwitcherSnapshot};

/// One window's stateful surface driver.
#[derive(Debug, Clone)]
pub struct SurfaceManager {
    graph: SurfaceGraph,
    presentations: HashMap<SurfaceId, SurfacePresentation>,
    revision: u64,
    policy: Policy,
    width: f64,
    sidebar_width: f64,
    hysteresis: f64,
    size_class: SizeClass,
    /// Most recently explicitly shown aside that could not be admitted as a
    /// dock. It remains live and is projected over the main until hidden.
    overlay_fallback_surface_id: Option<SurfaceId>,
}

impl SurfaceManager {
    /// New manager for a container of `width` logical px, default policy.
    pub fn new(width: f64) -> Self {
        Self::with_policy(width, Policy::default())
    }

    pub fn with_policy(width: f64, policy: Policy) -> Self {
        Self {
            graph: SurfaceGraph::new(),
            presentations: HashMap::new(),
            revision: 0,
            policy,
            width,
            sidebar_width: 0.0,
            hysteresis: DEFAULT_HYSTERESIS,
            size_class: SizeClass::from_width(width),
            overlay_fallback_surface_id: None,
        }
    }

    pub fn graph(&self) -> &SurfaceGraph {
        &self.graph
    }
    pub fn size_class(&self) -> SizeClass {
        self.size_class
    }
    pub fn width(&self) -> f64 {
        self.width
    }

    fn workspace_width(&self) -> f64 {
        (self.width - self.sidebar_width).max(0.0)
    }

    fn needs_overlay(&self, id: &str) -> bool {
        self.graph.role_of(id) == Some(crate::model::Role::Aside)
            && (self.size_class == SizeClass::Compact
                || !self
                    .graph
                    .aside_slots_admitted(self.size_class, self.workspace_width(), &self.policy)
                    .into_iter()
                    .find(|slot| slot.children.iter().any(|child| child == id))
                    .is_some_and(|slot| slot.visible))
    }

    fn reconcile_overlay_fallback(&mut self) {
        if self
            .overlay_fallback_surface_id
            .as_deref()
            .is_some_and(|id| !self.needs_overlay(id))
        {
            self.overlay_fallback_surface_id = None;
        }
    }

    /// Update the container width. Returns `true` if the `SizeClass` changed
    /// after hysteresis, i.e. when the skin must re-derive its layout.
    pub fn set_width(&mut self, width: f64) -> bool {
        self.width = width;
        let next = SizeClass::resolve(Some(self.size_class), width, self.hysteresis);
        let changed = next != self.size_class;
        self.size_class = next;
        self.reconcile_overlay_fallback();
        changed
    }

    /// Report the platform's live sidebar allocation. Mobile/custom hosts use
    /// zero; desktop shells update this during resize and collapse.
    pub fn set_sidebar_width(&mut self, width: f64) -> bool {
        let width = if width.is_finite() {
            width.max(0.0).min(self.width)
        } else {
            0.0
        };
        let changed = (self.sidebar_width - width).abs() > f64::EPSILON;
        self.sidebar_width = width;
        self.reconcile_overlay_fallback();
        changed
    }

    /// Open (or replace by id) a surface through the arbiter at the current size.
    /// Always leaves the graph valid; returns the structured decision.
    pub fn open(&mut self, request: Surface) -> OpenOutcome {
        let requested_id = request.id.clone();
        let default_presentation = SurfacePresentation::for_content(&request.content);
        let content_changed = self
            .graph
            .get(&requested_id)
            .is_some_and(|surface| surface.content != request.content);
        let (next, mut outcome) = arbitrate(&self.graph, request, &self.policy, self.size_class);
        self.graph = next;
        if outcome.resolved_surface_id == requested_id {
            if content_changed {
                self.presentations
                    .insert(requested_id, default_presentation);
            } else {
                self.presentations
                    .entry(requested_id)
                    .or_insert(default_presentation);
            }
        }
        self.presentations
            .retain(|id, _| self.graph.get(id).is_some());
        self.bump_revision();
        if outcome.resolved_role == crate::model::Role::Aside {
            let admitted = self
                .graph
                .aside_slots_admitted(self.size_class, self.workspace_width(), &self.policy)
                .into_iter()
                .find(|slot| slot.children.contains(&outcome.resolved_surface_id))
                .is_some_and(|slot| slot.visible);
            outcome.overlay |= self.size_class == SizeClass::Compact || !admitted;
            if outcome.overlay {
                self.overlay_fallback_surface_id = Some(outcome.resolved_surface_id.clone());
            } else {
                self.overlay_fallback_surface_id = None;
            }
        }
        outcome
    }

    pub fn close(&mut self, id: &str) -> CloseOutcome {
        let outcome = self.graph.close(id);
        let removed = outcome.removed();
        if self
            .overlay_fallback_surface_id
            .as_ref()
            .is_some_and(|fallback| removed.contains(fallback))
        {
            let focused = self
                .graph
                .focused_surface_id
                .as_deref()
                .filter(|focused| self.graph.role_of(focused) == Some(crate::model::Role::Aside))
                .map(str::to_string);
            self.overlay_fallback_surface_id =
                focused.filter(|focused| self.needs_overlay(focused));
        }
        for id in removed {
            self.presentations.remove(id);
        }
        if matches!(outcome, CloseOutcome::Closed { .. }) {
            self.bump_revision();
        }
        outcome
    }

    pub fn replace_mains(
        &mut self,
        mains: Vec<(Surface, SurfacePresentation)>,
    ) -> Result<SurfaceSwitcherSnapshot, ReplaceMainsError> {
        let mut ids = std::collections::HashSet::with_capacity(mains.len());
        for (surface, _) in &mains {
            if surface.role != Role::Main {
                return Err(ReplaceMainsError::InvalidRole {
                    surface_id: surface.id.clone(),
                });
            }
            if !ids.insert(surface.id.clone()) {
                return Err(ReplaceMainsError::DuplicateId {
                    surface_id: surface.id.clone(),
                });
            }
        }
        let old_main_ids: Vec<_> = self
            .graph
            .mains()
            .into_iter()
            .map(|surface| surface.id.clone())
            .collect();
        self.remove_presentations(&old_main_ids);

        let (surfaces, presentations): (Vec<_>, Vec<_>) = mains.into_iter().unzip();
        self.graph.replace_mains(surfaces);
        for (surface, presentation) in self.graph.mains().into_iter().zip(presentations) {
            self.presentations.insert(surface.id.clone(), presentation);
        }
        self.presentations
            .retain(|id, _| self.graph.get(id).is_some());
        self.bump_revision();
        Ok(self.switcher_snapshot())
    }

    pub fn open_main(
        &mut self,
        surface: Surface,
        presentation: SurfacePresentation,
    ) -> Result<SurfaceSwitcherSnapshot, ReplaceMainsError> {
        if surface.role != Role::Main {
            return Err(ReplaceMainsError::InvalidRole {
                surface_id: surface.id,
            });
        }
        let surface_id = surface.id.clone();
        // Main registration has a stricter contract than a general open: the
        // requested identity must become that exact main. Bypass aside reuse
        // and role arbitration so a future policy change cannot redirect the
        // request while we publish presentation metadata under the old id.
        self.graph.insert(surface);
        self.presentations.insert(surface_id.clone(), presentation);
        self.presentations
            .retain(|id, _| self.graph.get(id).is_some());
        self.bump_revision();
        self.set_active_main(&surface_id);
        Ok(self.switcher_snapshot())
    }

    pub fn close_other_mains(&mut self, keeping: &str) -> Vec<SurfaceId> {
        if self.graph.role_of(keeping) != Some(Role::Main) {
            return Vec::new();
        }
        let targets: Vec<_> = self
            .switcher_snapshot()
            .items
            .into_iter()
            .filter(|item| item.surface_id != keeping && item.closable)
            .map(|item| item.surface_id)
            .collect();
        let removed = targets
            .into_iter()
            .flat_map(|id| self.graph.close(&id).into_removed())
            .collect::<Vec<_>>();
        self.remove_presentations(&removed);
        if !removed.is_empty() {
            self.bump_revision();
        }
        removed
    }

    pub fn close_mains_after(&mut self, id: &str) -> Vec<SurfaceId> {
        let snapshot = self.switcher_snapshot();
        let Some(index) = snapshot.items.iter().position(|item| item.surface_id == id) else {
            return Vec::new();
        };
        let removed = snapshot
            .items
            .into_iter()
            .skip(index + 1)
            .filter(|item| item.closable)
            .flat_map(|item| self.graph.close(&item.surface_id).into_removed())
            .collect::<Vec<_>>();
        self.remove_presentations(&removed);
        if !removed.is_empty() {
            self.bump_revision();
        }
        removed
    }

    fn remove_presentations(&mut self, ids: &[SurfaceId]) {
        for id in ids {
            self.presentations.remove(id);
        }
    }

    pub fn set_presentation(&mut self, id: &str, presentation: SurfacePresentation) -> bool {
        if self.graph.get(id).is_none() {
            return false;
        }
        self.presentations.insert(id.to_string(), presentation);
        self.bump_revision();
        true
    }

    pub fn update_automatic_title(&mut self, id: &str, title: Option<&str>) -> bool {
        let Some(presentation) = self.presentations.get_mut(id) else {
            return false;
        };
        let title = title
            .map(str::trim)
            .filter(|title| !title.is_empty())
            .map(str::to_string);
        if presentation.automatic_title == title {
            return false;
        }
        presentation.automatic_title = title;
        self.bump_revision();
        true
    }

    pub fn rename(&mut self, id: &str, title: Option<&str>) -> bool {
        let Some(presentation) = self.presentations.get_mut(id) else {
            return false;
        };
        if !presentation.capabilities.rename {
            return false;
        }
        presentation.set_custom_title(title);
        self.bump_revision();
        true
    }

    pub fn switcher_snapshot(&self) -> SurfaceSwitcherSnapshot {
        let mut snapshot = SurfaceSwitcherSnapshot::derive(&self.graph, &self.presentations);
        snapshot.revision = self.revision;
        snapshot
    }

    fn bump_revision(&mut self) {
        self.revision = self.revision.wrapping_add(1);
    }

    pub fn set_active_main(&mut self, id: &str) -> bool {
        let changed = self.graph.active_main_id.as_deref() != Some(id);
        let active = self.graph.set_active_main(id);
        if active {
            self.overlay_fallback_surface_id = None;
            if changed {
                self.bump_revision();
            }
        }
        active
    }
    pub fn set_focus(&mut self, id: &str) -> bool {
        let focused = self.graph.set_focus(id);
        if focused {
            self.overlay_fallback_surface_id = self.needs_overlay(id).then(|| id.to_string());
        }
        focused
    }

    pub fn show(&mut self, id: &str) -> bool {
        let role = self.graph.role_of(id);
        let activates_main =
            role == Some(Role::Main) && self.graph.active_main_id.as_deref() != Some(id);
        let shown = self.graph.show(id);
        if shown && role == Some(Role::Main) {
            self.overlay_fallback_surface_id = None;
            if activates_main {
                // `SurfaceGraph::show` performs the selection. Mirror
                // `set_active_main`'s observable switcher revision here.
                self.bump_revision();
            }
        }
        if shown && role == Some(Role::Aside) {
            let admitted = self
                .graph
                .aside_slots_admitted(self.size_class, self.workspace_width(), &self.policy)
                .into_iter()
                .find(|slot| slot.children.iter().any(|child| child == id))
                .is_some_and(|slot| slot.visible);
            if self.size_class == SizeClass::Compact || !admitted {
                self.overlay_fallback_surface_id = Some(id.to_string());
            } else {
                self.overlay_fallback_surface_id = None;
            }
        }
        shown
    }

    /// Collapse or restore a whole aside slot. Collapsing is the shell's
    /// "put this region away" gesture: nothing closes, so the overlay
    /// fallback for a child of the slot is dropped along with the dock.
    pub fn set_slot_collapsed(&mut self, kind: crate::SlotKind, collapsed: bool) -> bool {
        if !self.graph.set_slot_collapsed(kind, collapsed) {
            return false;
        }
        if collapsed
            && let Some(id) = self.overlay_fallback_surface_id.clone()
            && self
                .graph
                .get(&id)
                .is_some_and(|surface| surface.content.slot_kind() == kind)
        {
            self.overlay_fallback_surface_id = None;
        }
        true
    }

    pub fn hide(&mut self, id: &str) -> bool {
        let hidden = self.graph.hide(id);
        if hidden && self.overlay_fallback_surface_id.as_deref() == Some(id) {
            let focused = self
                .graph
                .focused_surface_id
                .as_deref()
                .filter(|focused| self.graph.role_of(focused) == Some(crate::model::Role::Aside))
                .map(str::to_string);
            self.overlay_fallback_surface_id =
                focused.filter(|focused| self.needs_overlay(focused));
        }
        hidden
    }

    /// Derive the platform-agnostic layout output at the current size.
    pub fn derive(&self) -> DerivedLayout {
        self.graph.derive_layout(self.size_class)
    }

    /// Build the stable, skin-bindable [`LayoutPresentationPlan`] at the current
    /// size — the renderable contract platforms reconcile against. Slot
    /// admission respects both the size-class ceiling and the physical fit at
    /// the current width (§3.3).
    pub fn presentation_plan(&self) -> LayoutPresentationPlan {
        let mut plan =
            self.graph
                .presentation_plan(self.size_class, self.workspace_width(), &self.policy);
        plan.main_switcher = self.switcher_snapshot();
        if self.size_class == SizeClass::Compact {
            for slot in plan.aside_slots.iter_mut().filter(|slot| slot.visible) {
                slot.overlay = true;
            }
        }
        if let Some(id) = self.overlay_fallback_surface_id.as_deref()
            && let Some(slot) = plan
                .aside_slots
                .iter_mut()
                .find(|slot| slot.children.iter().any(|child| child == id))
            && !slot.collapsed
            && (self.size_class == SizeClass::Compact || !slot.visible)
        {
            slot.visible = true;
            slot.active_child = Some(id.to_string());
            slot.overlay = true;
        }
        plan
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Decision;
    use crate::layout::{SplitForm, SwitcherForm};
    use crate::{Edge, Role, Surface, SurfaceContent};

    fn main_s(id: &str) -> Surface {
        Surface::lxapp(id, Role::Main, id)
    }
    fn aside_s(id: &str, edge: Edge) -> Surface {
        let mut s = Surface::lxapp(id, Role::Aside, id);
        s.placement.edge = Some(edge);
        s
    }

    #[test]
    fn open_then_derive_on_expanded() {
        let mut m = SurfaceManager::new(1200.0);
        assert_eq!(m.size_class(), SizeClass::Expanded);
        assert_eq!(m.open(main_s("home")), Decision::Accepted);
        assert_eq!(
            m.open(aside_s("assistant", Edge::Right)),
            Decision::Accepted
        );
        let d = m.derive();
        assert_eq!(d.split_form, SplitForm::Split);
        assert!(m.graph().is_valid());
    }

    #[test]
    fn aside_on_compact_overlays_without_host_switcher() {
        let mut m = SurfaceManager::new(390.0); // phone width
        assert_eq!(m.size_class(), SizeClass::Compact);
        m.open(main_s("home"));
        // Arbitration preserves the aside role and marks it as a full-screen
        // overlay; compact still has no sidebar switcher.
        assert_eq!(
            m.open(aside_s("assistant", Edge::Right)),
            Decision::FullScreenFallback
        );
        let d = m.derive();
        assert_eq!(d.switcher_form, SwitcherForm::None);
        assert_eq!(d.bottom_owner, crate::BottomOwner::App);
        let slot = &m.presentation_plan().aside_slots[0];
        assert!(slot.visible);
        assert!(slot.overlay);
        assert!(m.graph().is_valid());
    }

    #[test]
    fn collapsing_a_slot_keeps_its_children_alive() {
        let mut m = SurfaceManager::new(1200.0);
        m.open(main_s("home"));
        m.open(aside_s("assistant", Edge::Right));
        m.open(aside_s("notes", Edge::Right));

        assert!(m.set_slot_collapsed(crate::SlotKind::Lxapp, true));
        let slot = &m.presentation_plan().aside_slots[0];
        assert!(!slot.visible);
        assert!(slot.collapsed);
        assert_eq!(slot.children.len(), 2);

        assert!(m.set_slot_collapsed(crate::SlotKind::Lxapp, false));
        let slot = &m.presentation_plan().aside_slots[0];
        assert!(slot.visible);
        assert_eq!(slot.children.len(), 2);
    }

    #[test]
    fn a_collapsed_slot_reopens_when_content_arrives_or_is_focused() {
        let mut m = SurfaceManager::new(1200.0);
        m.open(main_s("home"));
        m.open(aside_s("assistant", Edge::Right));

        m.set_slot_collapsed(crate::SlotKind::Lxapp, true);
        m.open(aside_s("notes", Edge::Right));
        assert!(m.presentation_plan().aside_slots[0].visible);

        m.set_slot_collapsed(crate::SlotKind::Lxapp, true);
        assert!(m.set_focus("assistant"));
        assert!(m.presentation_plan().aside_slots[0].visible);
    }

    #[test]
    fn a_collapsed_slot_that_loses_its_last_child_starts_open_again() {
        let mut m = SurfaceManager::new(1200.0);
        m.open(main_s("home"));
        m.open(aside_s("assistant", Edge::Right));
        m.set_slot_collapsed(crate::SlotKind::Lxapp, true);
        m.close("assistant");

        m.open(aside_s("notes", Edge::Right));
        assert!(m.presentation_plan().aside_slots[0].visible);
    }

    #[test]
    fn a_collapsed_compact_slot_does_not_come_back_as_an_overlay() {
        let mut m = SurfaceManager::new(390.0);
        m.open(main_s("home"));
        m.open(aside_s("assistant", Edge::Right));
        assert!(m.presentation_plan().aside_slots[0].overlay);

        m.set_slot_collapsed(crate::SlotKind::Lxapp, true);
        let slot = &m.presentation_plan().aside_slots[0];
        assert!(!slot.visible);
        assert!(!slot.overlay);
    }

    #[test]
    fn width_changes_recompute_physical_admission_within_a_size_class() {
        let mut manager = SurfaceManager::new(1400.0);
        manager.set_sidebar_width(184.0);
        manager.open(main_s("home"));
        manager.open(aside_s("lxapp", Edge::Right));
        let mut browser = Surface::lxapp("browser", Role::Aside, "browser");
        browser.content = SurfaceContent::Browser {
            initial_url: "https://example.com".to_string(),
            reuse_by_url: true,
        };
        browser.placement.edge = Some(Edge::Right);
        manager.open(browser);
        let mut native = Surface::native("terminal", Role::Aside, "terminal");
        native.placement.edge = Some(Edge::Right);
        manager.open(native);
        assert_eq!(
            manager
                .presentation_plan()
                .aside_slots
                .iter()
                .filter(|slot| slot.visible)
                .count(),
            3
        );

        // Both widths are Expanded. After the full sidebar is allocated, only
        // one horizontal slot fits at a 900-wide client area.
        assert!(!manager.set_width(900.0));
        assert_eq!(manager.size_class(), SizeClass::Expanded);
        assert_eq!(
            manager
                .presentation_plan()
                .aside_slots
                .iter()
                .filter(|slot| slot.visible)
                .count(),
            1
        );
    }

    #[test]
    fn explicitly_opened_non_fitting_aside_overlays_until_it_can_dock() {
        let mut manager = SurfaceManager::new(500.0);
        manager.open(main_s("home"));
        let outcome = manager.open(aside_s("assistant", Edge::Right));
        assert!(outcome.overlay);
        let slot = &manager.presentation_plan().aside_slots[0];
        assert!(slot.visible);
        assert!(slot.overlay);

        manager.set_width(700.0);
        let slot = &manager.presentation_plan().aside_slots[0];
        assert!(slot.visible);
        assert!(!slot.overlay);
    }

    #[test]
    fn compact_focus_updates_the_overlay_tab() {
        let mut manager = SurfaceManager::new(500.0);
        manager.open(main_s("home"));
        manager.open(aside_s("first", Edge::Right));
        manager.open(aside_s("second", Edge::Right));

        assert!(manager.set_focus("first"));
        let slot = &manager.presentation_plan().aside_slots[0];
        assert_eq!(slot.active_child.as_deref(), Some("first"));
    }

    #[test]
    fn showing_an_existing_main_advances_the_switcher_revision() {
        let mut manager = SurfaceManager::new(1200.0);
        manager.open(main_s("home"));
        manager.open(main_s("workspace"));
        manager.set_active_main("workspace");
        let before = manager.switcher_snapshot();

        assert!(manager.show("home"));
        let after = manager.switcher_snapshot();

        assert_eq!(after.active_surface_id.as_deref(), Some("home"));
        assert!(after.revision > before.revision);
    }

    #[test]
    fn docked_fallback_does_not_reappear_after_later_resize() {
        let policy = Policy {
            main_min_width: 400.0,
            aside_min_width: 240.0,
            ..Policy::default()
        };
        let mut manager = SurfaceManager::with_policy(620.0, policy);
        manager.open(main_s("home"));
        let mut browser = Surface::lxapp("browser", Role::Aside, "browser");
        browser.content = SurfaceContent::Browser {
            initial_url: "https://example.com".to_string(),
            reuse_by_url: true,
        };
        browser.placement.edge = Some(Edge::Right);
        assert!(manager.open(browser).overlay);

        manager.set_width(1000.0);
        manager.open(aside_s("chat", Edge::Right));
        assert!(manager.set_focus("chat"));
        manager.set_width(700.0);

        let visible: Vec<_> = manager
            .presentation_plan()
            .aside_slots
            .into_iter()
            .filter(|slot| slot.visible)
            .map(|slot| slot.kind)
            .collect();
        assert_eq!(visible, vec![crate::SlotKind::Lxapp]);
    }

    #[test]
    fn live_sidebar_width_controls_physical_admission() {
        let mut manager = SurfaceManager::new(900.0);
        manager.open(main_s("home"));
        manager.open(aside_s("chat", Edge::Right));
        let mut browser = Surface::lxapp("browser", Role::Aside, "browser");
        browser.content = SurfaceContent::Browser {
            initial_url: "https://example.com".to_string(),
            reuse_by_url: true,
        };
        browser.placement.edge = Some(Edge::Right);
        manager.open(browser);

        assert_eq!(
            manager
                .presentation_plan()
                .aside_slots
                .iter()
                .filter(|slot| slot.visible)
                .count(),
            2
        );
        manager.set_sidebar_width(300.0);
        assert_eq!(
            manager
                .presentation_plan()
                .aside_slots
                .iter()
                .filter(|slot| slot.visible)
                .count(),
            1
        );
        manager.set_sidebar_width(0.0);
        assert_eq!(
            manager
                .presentation_plan()
                .aside_slots
                .iter()
                .filter(|slot| slot.visible)
                .count(),
            2
        );
    }

    #[test]
    fn width_change_reports_sizeclass_flip_with_hysteresis() {
        let mut m = SurfaceManager::new(1200.0);
        // small nudge that stays expanded → no change reported.
        assert!(!m.set_width(900.0));
        assert_eq!(m.size_class(), SizeClass::Expanded);
        // drop to phone width → flips to compact.
        assert!(m.set_width(390.0));
        assert_eq!(m.size_class(), SizeClass::Compact);
        // hovering just under the 600 boundary keeps compact (hysteresis).
        assert!(!m.set_width(590.0));
        assert_eq!(m.size_class(), SizeClass::Compact);
    }

    #[test]
    fn resize_reflows_existing_aside_without_mutating_roles() {
        let mut m = SurfaceManager::new(1200.0);
        m.open(main_s("home"));
        m.open(aside_s("assistant", Edge::Right));
        // expanded: real split, aside stays an aside.
        assert_eq!(m.derive().split_form, SplitForm::Split);
        assert_eq!(m.graph().role_of("assistant"), Some(Role::Aside));
        // shrink to compact: same graph, layout re-flows to full-screen.
        m.set_width(390.0);
        let d = m.derive();
        assert_eq!(d.split_form, SplitForm::FullScreen);
        assert_eq!(d.switcher_form, SwitcherForm::None);
        let slot = &m.presentation_plan().aside_slots[0];
        assert!(slot.visible);
        assert!(slot.overlay);
        // role unchanged → widening back restores the split (reversible).
        assert_eq!(m.graph().role_of("assistant"), Some(Role::Aside));
        m.set_width(1200.0);
        assert_eq!(m.derive().split_form, SplitForm::Split);
    }
}