teksilo-widgets 0.9.0

Widget library for Teksilo — over a hundred widgets and layout primitives, from Button to TreeTableView.
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! [`SplitterModel`] — the shared, cloneable, serializable state behind a
//! [`Splitter`](crate::splitter::Splitter).
//!
//! Mirrors the `SceneModel = Rc<RefCell<…>>` handle pattern: cloning a
//! `SplitterModel` produces a **second handle to the same data**, so the
//! app keeps a clone to read/mutate/persist while the widget renders it,
//! and a future `DockingLayout` composes a tree of them. Every mutator
//! takes `&self`, borrows the inner `RefCell` mutably, mutates, drops the
//! borrow, then bumps a `version: Signal<u64>` — the widget binds that
//! signal at `BindingLevel::Relayout`, so any external change reflows the
//! panes with no rebuild.
//!
//! ## Source of truth: pixel sizes
//!
//! Each pane stores an absolute `stored_size` (logical px along the main
//! axis). This is the user's intent. The widget projects it onto the
//! current bounds every layout pass via the pure
//! [`distribute`](super::distribute::distribute) function; a container
//! resize never writes back, so drag positions survive resizes. Stored
//! sizes change **only** on drag, programmatic mutation, or structural
//! insert/remove.
//!
//! ## Borrow / observer contract
//!
//! `version.set` snapshots its observers and releases the signal's cell
//! before invoking them, and every mutator drops its `RefCell` borrow
//! before bumping. So the one rule (same as `SceneModel`) is: **an
//! observer on [`version`](SplitterModel::version) must not mutate the
//! model re-entrantly from inside its own callback.** The widget's
//! observers only read the model and set their own signals, so they are
//! safe.

use std::cell::RefCell;
use std::rc::Rc;

use serde::{Deserialize, Serialize};
use teksilo_core::signal::Signal;
use teksilo_settings::Versioned;
use teksilo_tokens::Orientation;

/// Default gutter (handle) thickness in logical px. Resolved into the
/// model at construction; override with [`SplitterModel::set_gutter_thickness`].
pub const SPLITTER_GUTTER_THICKNESS: f32 = 6.0;
/// Default minimum pane size in logical px.
pub const SPLITTER_MIN_PANE_SIZE: f32 = 96.0;
/// Default keyboard resize step in logical px (per arrow press).
pub const SPLITTER_KEYBOARD_STEP: f32 = 24.0;
/// Default drag-past-min snap-to-collapse threshold in logical px.
pub const SPLITTER_SNAP_OFFSET: f32 = 30.0;

// ---------------------------------------------------------------------
// Pane descriptor (construction-time per-pane config)
// ---------------------------------------------------------------------

/// Per-pane configuration passed to [`SplitterModel::from_panes`] /
/// [`SplitterModel::insert_pane`]. Public fields + [`Default`] so it can
/// be built with struct-literal `..Default::default()` syntax, or via the
/// fluent setters.
#[derive(Debug, Clone)]
pub struct PaneDescriptor {
    /// Initial main-axis size in px. `None` ⇒ take an equal share (the
    /// first layout equalizes via the stretch path).
    pub initial_size: Option<f32>,
    /// Hard compression floor in px.
    pub min_size: f32,
    /// Optional growth ceiling in px.
    pub max_size: Option<f32>,
    /// Container-resize slack weight (Qt `setStretchFactor`). `0.0` ⇒
    /// rigid (keeps its size on resize); `>0` ⇒ absorbs slack ∝ weight.
    pub stretch: f32,
    /// Whether the user may collapse this pane (drag-snap / double-click /
    /// keyboard). Programmatic [`set_collapsed`](SplitterModel::set_collapsed)
    /// ignores this flag (it governs *interactive* collapse only, like Qt's
    /// `childrenCollapsible`).
    pub collapsible: bool,
    /// Initial collapsed state.
    pub collapsed: bool,
    /// The main-axis size a collapsed pane folds down to (default `0` ⇒ fully
    /// gone). Set this to keep a sliver visible while collapsed — e.g. an
    /// accordion's header height, so the pane shrinks to just its header and
    /// can be re-expanded from there. The pane restores to its prior size on
    /// expand regardless.
    pub collapsed_size: f32,
    /// Whether the pane is present at all. Unlike `collapsed` (which folds
    /// the pane but keeps its grabbable gutter), a hidden pane removes both
    /// the pane *and* an adjacent gutter from the layout — it reads as
    /// absent. Toggled reactively via
    /// [`set_pane_visible`](SplitterModel::set_pane_visible).
    pub visible: bool,
}

impl Default for PaneDescriptor {
    fn default() -> Self {
        Self {
            initial_size: None,
            min_size: SPLITTER_MIN_PANE_SIZE,
            max_size: None,
            stretch: 1.0,
            collapsible: false,
            collapsed: false,
            collapsed_size: 0.0,
            visible: true,
        }
    }
}

impl PaneDescriptor {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn size(mut self, size: f32) -> Self {
        self.initial_size = Some(size);
        self
    }
    pub fn min_size(mut self, min: f32) -> Self {
        self.min_size = min;
        self
    }
    pub fn max_size(mut self, max: f32) -> Self {
        self.max_size = Some(max);
        self
    }
    pub fn stretch(mut self, stretch: f32) -> Self {
        self.stretch = stretch;
        self
    }
    pub fn collapsible(mut self, collapsible: bool) -> Self {
        self.collapsible = collapsible;
        self
    }
    pub fn collapsed(mut self, collapsed: bool) -> Self {
        self.collapsed = collapsed;
        self
    }
    /// Size a collapsed pane folds down to (default `0`). See
    /// [`collapsed_size`](Self::collapsed_size).
    pub fn collapsed_size(mut self, px: f32) -> Self {
        self.collapsed_size = px.max(0.0);
        self
    }
    pub fn visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }
}

// ---------------------------------------------------------------------
// Internal pane entry + immutable snapshot for the sizing engine
// ---------------------------------------------------------------------

#[derive(Debug, Clone)]
struct PaneEntry {
    stored_size: f32,
    min_size: f32,
    max_size: Option<f32>,
    stretch: f32,
    collapsible: bool,
    collapsed: bool,
    collapsed_size: f32,
    visible: bool,
}

impl PaneEntry {
    fn from_descriptor(d: &PaneDescriptor, fallback_size: f32) -> Self {
        let min = d.min_size.max(0.0);
        // Enforce max ≥ min on the way in so `distribute` never has to
        // resolve an impossible [min,max].
        let max = d.max_size.map(|m| m.max(min));
        let stored = d.initial_size.unwrap_or(fallback_size).max(0.0);
        Self {
            stored_size: stored,
            min_size: min,
            max_size: max,
            stretch: d.stretch.max(0.0),
            collapsible: d.collapsible,
            collapsed: d.collapsed,
            collapsed_size: d.collapsed_size.max(0.0),
            visible: d.visible,
        }
    }
}

/// Immutable per-pane view handed to the pure `distribute` sizing
/// function (the internal `splitter::distribute` engine).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PaneSnapshot {
    pub stored_size: f32,
    pub min_size: f32,
    pub max_size: Option<f32>,
    pub stretch: f32,
    pub collapsed: bool,
    pub collapsed_size: f32,
    pub visible: bool,
}

// ---------------------------------------------------------------------
// Serde DTO (export / import — the persistence surface)
// ---------------------------------------------------------------------

/// Persistable per-pane layout state. Captures the user-controllable
/// values (size + collapsed); structural config (min/max/stretch/
/// collapsible) is app-declared and not serialized — Qt `saveState`
/// parity.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct PaneState {
    pub stored_size: f32,
    pub collapsed: bool,
}

/// Full serializable snapshot of a [`SplitterModel`]'s sizes + collapsed
/// flags. Round-trips through [`SplitterModel::export_state`] /
/// [`import_state`](SplitterModel::import_state) and implements
/// [`Versioned`] so apps persist it through
/// `SettingsFile<SplitterState>` + `Migrator` (TOML).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SplitterState {
    #[serde(default = "default_version")]
    pub version: u32,
    #[serde(default)]
    pub panes: Vec<PaneState>,
}

fn default_version() -> u32 {
    SplitterState::CURRENT_VERSION
}

impl Default for SplitterState {
    fn default() -> Self {
        Self {
            version: SplitterState::CURRENT_VERSION,
            panes: Vec::new(),
        }
    }
}

impl Versioned for SplitterState {
    const CURRENT_VERSION: u32 = 1;
    fn version(&self) -> u32 {
        self.version
    }
    fn set_version(&mut self, v: u32) {
        self.version = v;
    }
}

// ---------------------------------------------------------------------
// The model handle
// ---------------------------------------------------------------------

struct SplitterModelInner {
    panes: Vec<PaneEntry>,
    orientation: Orientation,
    gutter_thickness: f32,
    keyboard_step_px: f32,
    snap_offset: f32,
    version: Signal<u64>,
    /// `true` ⇒ the next collapse-flag change should *animate*; `false`
    /// ⇒ snap instantly (drag-driven). Read-and-reset by the widget's
    /// collapse effect via [`consume_animate_flag`](SplitterModel::consume_animate_flag).
    animate_next_collapse: bool,
}

/// A shared, cloneable handle to a splitter's layout state. `Clone` =
/// share-by-handle (cheap `Rc` bump).
pub struct SplitterModel(Rc<RefCell<SplitterModelInner>>);

impl Clone for SplitterModel {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl std::fmt::Debug for SplitterModel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.0.try_borrow() {
            Ok(inner) => f
                .debug_struct("SplitterModel")
                .field("handles", &Rc::strong_count(&self.0))
                .field("panes", &inner.panes.len())
                .field("orientation", &inner.orientation)
                .finish(),
            Err(_) => f
                .debug_struct("SplitterModel")
                .field("handles", &Rc::strong_count(&self.0))
                .field("panes", &"<borrowed>")
                .finish(),
        }
    }
}

impl SplitterModel {
    // ---- Construction -------------------------------------------------

    /// `n` equal-share panes (each `stretch = 1`, `min = SPLITTER_MIN_PANE_SIZE`).
    pub fn new(n: usize, orientation: Orientation) -> Self {
        let panes = (0..n)
            .map(|_| PaneEntry::from_descriptor(&PaneDescriptor::default(), 0.0))
            .collect();
        Self::from_inner(panes, orientation)
    }

    /// Build from explicit per-pane descriptors.
    pub fn from_panes(panes: Vec<PaneDescriptor>, orientation: Orientation) -> Self {
        let entries = panes
            .iter()
            .map(|d| PaneEntry::from_descriptor(d, d.initial_size.unwrap_or(0.0)))
            .collect();
        Self::from_inner(entries, orientation)
    }

    fn from_inner(panes: Vec<PaneEntry>, orientation: Orientation) -> Self {
        Self(Rc::new(RefCell::new(SplitterModelInner {
            panes,
            orientation,
            gutter_thickness: SPLITTER_GUTTER_THICKNESS,
            keyboard_step_px: SPLITTER_KEYBOARD_STEP,
            snap_offset: SPLITTER_SNAP_OFFSET,
            version: Signal::new(0),
            animate_next_collapse: true,
        })))
    }

    /// Number of distinct handles to this model (1 = unshared).
    pub fn handle_count(&self) -> usize {
        Rc::strong_count(&self.0)
    }

    // ---- Version bump -------------------------------------------------

    fn bump_version(&self) {
        // Clone the signal out and drop the borrow before `set`, so an
        // observer may safely read the model from its callback.
        let version = self.0.borrow().version.clone();
        version.set(version.get().wrapping_add(1));
    }

    // ---- Per-pane size mutators --------------------------------------

    pub fn set_stored_size(&self, index: usize, size: f32) {
        {
            let mut inner = self.0.borrow_mut();
            let Some(p) = inner.panes.get_mut(index) else {
                return;
            };
            p.stored_size = size.max(0.0);
        }
        self.bump_version();
    }

    /// Like [`set_stored_size`](Self::set_stored_size) but **without** a version
    /// bump — for writes made from inside a layout/effect pass that is already
    /// relaying out (e.g. capturing the displayed size as the collapse
    /// reference), where a bump would re-enter the effect.
    pub fn set_stored_size_silent(&self, index: usize, size: f32) {
        let mut inner = self.0.borrow_mut();
        if let Some(p) = inner.panes.get_mut(index) {
            p.stored_size = size.max(0.0);
        }
    }

    /// Set both sides of handle `index` (panes `index` and `index+1`) in
    /// one mutation — a single version bump, so a drag produces exactly
    /// one relayout per move.
    pub fn set_pair_sizes(&self, index: usize, size_a: f32, size_b: f32) {
        {
            let mut inner = self.0.borrow_mut();
            if index + 1 >= inner.panes.len() {
                return;
            }
            inner.panes[index].stored_size = size_a.max(0.0);
            inner.panes[index + 1].stored_size = size_b.max(0.0);
        }
        self.bump_version();
    }

    pub fn set_min_size(&self, index: usize, min: f32) {
        {
            let mut inner = self.0.borrow_mut();
            let Some(p) = inner.panes.get_mut(index) else {
                return;
            };
            p.min_size = min.max(0.0);
            // Keep max ≥ min.
            if let Some(m) = p.max_size {
                p.max_size = Some(m.max(p.min_size));
            }
        }
        self.bump_version();
    }

    pub fn set_max_size(&self, index: usize, max: Option<f32>) {
        {
            let mut inner = self.0.borrow_mut();
            let Some(p) = inner.panes.get_mut(index) else {
                return;
            };
            p.max_size = max.map(|m| m.max(p.min_size));
        }
        self.bump_version();
    }

    pub fn set_stretch(&self, index: usize, stretch: f32) {
        {
            let mut inner = self.0.borrow_mut();
            let Some(p) = inner.panes.get_mut(index) else {
                return;
            };
            p.stretch = stretch.max(0.0);
        }
        self.bump_version();
    }

    pub fn set_collapsible(&self, index: usize, collapsible: bool) {
        {
            let mut inner = self.0.borrow_mut();
            let Some(p) = inner.panes.get_mut(index) else {
                return;
            };
            p.collapsible = collapsible;
        }
        self.bump_version();
    }

    // ---- Collapse mutators -------------------------------------------

    /// Programmatically collapse/expand pane `index`, *animated*. Ignores
    /// the `collapsible` flag (that flag only gates interactive triggers).
    pub fn set_collapsed(&self, index: usize, collapsed: bool) {
        self.set_collapsed_inner(index, collapsed, true);
    }

    /// Collapse/expand pane `index` *instantly* (no tween). Used by the
    /// drag handlers — the pointer is already the motion.
    pub fn set_collapsed_immediate(&self, index: usize, collapsed: bool) {
        self.set_collapsed_inner(index, collapsed, false);
    }

    /// Toggle pane `index`'s collapsed state, animated.
    pub fn toggle_collapsed(&self, index: usize) {
        let current = self.is_collapsed(index);
        self.set_collapsed(index, !current);
    }

    /// Set the size pane `index` folds down to when collapsed (default `0`).
    /// See [`PaneDescriptor::collapsed_size`]. No version bump on its own — it
    /// only affects the next collapse.
    pub fn set_collapsed_size(&self, index: usize, px: f32) {
        let mut inner = self.0.borrow_mut();
        if let Some(p) = inner.panes.get_mut(index) {
            p.collapsed_size = px.max(0.0);
        }
    }

    fn set_collapsed_inner(&self, index: usize, collapsed: bool, animate: bool) {
        {
            let mut inner = self.0.borrow_mut();
            let Some(p) = inner.panes.get_mut(index) else {
                return;
            };
            if p.collapsed == collapsed {
                return; // no-op — avoid a spurious version bump
            }
            p.collapsed = collapsed;
            inner.animate_next_collapse = animate;
        }
        self.bump_version();
    }

    /// Show or hide pane `index` (animated). A hidden pane removes both the
    /// pane and an adjacent gutter from the layout — it reads as absent,
    /// unlike a collapsed pane (which keeps its grabbable gutter). The pane
    /// must be pre-mounted in the `Splitter`; this is the reactive "add /
    /// remove a pane from a fixed set" trick (no rebuild).
    pub fn set_pane_visible(&self, index: usize, visible: bool) {
        {
            let mut inner = self.0.borrow_mut();
            let Some(p) = inner.panes.get_mut(index) else {
                return;
            };
            if p.visible == visible {
                return;
            }
            p.visible = visible;
            inner.animate_next_collapse = true;
        }
        self.bump_version();
    }

    pub fn is_pane_visible(&self, index: usize) -> bool {
        self.0
            .borrow()
            .panes
            .get(index)
            .map(|p| p.visible)
            .unwrap_or(false)
    }

    /// Read-and-reset the "animate the next collapse change?" latch. The
    /// widget's collapse effect calls this once per version bump; it
    /// resets to `true` so the default (programmatic) path animates.
    pub fn consume_animate_flag(&self) -> bool {
        let mut inner = self.0.borrow_mut();
        let f = inner.animate_next_collapse;
        inner.animate_next_collapse = true;
        f
    }

    // ---- Structural mutators -----------------------------------------

    /// Insert a pane at `index` (clamped to `[0, len]`). A `None`
    /// `initial_size` takes the average of the existing panes' sizes; the
    /// next layout rebalances. The app must rebuild the `Splitter` widget
    /// to supply the new pane's content (retained-mode: changing a
    /// container's child *set* is a rebuild; the model keeps the
    /// persistent size/collapse state across it).
    pub fn insert_pane(&self, index: usize, desc: PaneDescriptor) {
        {
            let mut inner = self.0.borrow_mut();
            let idx = index.min(inner.panes.len());
            let fallback = if inner.panes.is_empty() {
                SPLITTER_MIN_PANE_SIZE
            } else {
                inner.panes.iter().map(|p| p.stored_size).sum::<f32>() / inner.panes.len() as f32
            };
            inner
                .panes
                .insert(idx, PaneEntry::from_descriptor(&desc, fallback));
        }
        self.bump_version();
    }

    /// Remove the pane at `index` (no-op if out of range). The app must
    /// rebuild the `Splitter` widget to drop the corresponding content.
    pub fn remove_pane(&self, index: usize) {
        {
            let mut inner = self.0.borrow_mut();
            if index >= inner.panes.len() {
                return;
            }
            inner.panes.remove(index);
        }
        self.bump_version();
    }

    /// Replace the metadata of pane `index` (keeps its current size unless
    /// the descriptor specifies one).
    pub fn replace_pane_desc(&self, index: usize, desc: PaneDescriptor) {
        {
            let mut inner = self.0.borrow_mut();
            let Some(p) = inner.panes.get_mut(index) else {
                return;
            };
            let fallback = p.stored_size;
            *p = PaneEntry::from_descriptor(&desc, fallback);
        }
        self.bump_version();
    }

    // ---- Global mutators ---------------------------------------------

    pub fn set_gutter_thickness(&self, thickness: f32) {
        {
            self.0.borrow_mut().gutter_thickness = thickness.max(1.0);
        }
        self.bump_version();
    }

    pub fn set_snap_offset(&self, offset: f32) {
        {
            self.0.borrow_mut().snap_offset = offset.max(0.0);
        }
        self.bump_version();
    }

    pub fn set_keyboard_step_px(&self, step: f32) {
        {
            self.0.borrow_mut().keyboard_step_px = step.max(1.0);
        }
        self.bump_version();
    }

    pub fn set_orientation(&self, orientation: Orientation) {
        {
            self.0.borrow_mut().orientation = orientation;
        }
        self.bump_version();
    }

    // ---- Queries ------------------------------------------------------

    pub fn pane_count(&self) -> usize {
        self.0.borrow().panes.len()
    }
    pub fn stored_size(&self, index: usize) -> f32 {
        self.0
            .borrow()
            .panes
            .get(index)
            .map(|p| p.stored_size)
            .unwrap_or(0.0)
    }
    pub fn min_size(&self, index: usize) -> f32 {
        self.0
            .borrow()
            .panes
            .get(index)
            .map(|p| p.min_size)
            .unwrap_or(0.0)
    }
    pub fn max_size(&self, index: usize) -> Option<f32> {
        self.0.borrow().panes.get(index).and_then(|p| p.max_size)
    }
    pub fn stretch(&self, index: usize) -> f32 {
        self.0
            .borrow()
            .panes
            .get(index)
            .map(|p| p.stretch)
            .unwrap_or(0.0)
    }
    pub fn is_collapsible(&self, index: usize) -> bool {
        self.0
            .borrow()
            .panes
            .get(index)
            .map(|p| p.collapsible)
            .unwrap_or(false)
    }
    /// The size pane `index` folds to when collapsed (default `0`). See
    /// [`PaneDescriptor::collapsed_size`].
    pub fn collapsed_size(&self, index: usize) -> f32 {
        self.0
            .borrow()
            .panes
            .get(index)
            .map(|p| p.collapsed_size)
            .unwrap_or(0.0)
    }
    pub fn is_collapsed(&self, index: usize) -> bool {
        self.0
            .borrow()
            .panes
            .get(index)
            .map(|p| p.collapsed)
            .unwrap_or(false)
    }
    pub fn orientation(&self) -> Orientation {
        self.0.borrow().orientation
    }
    pub fn gutter_thickness(&self) -> f32 {
        self.0.borrow().gutter_thickness
    }
    pub fn snap_offset(&self) -> f32 {
        self.0.borrow().snap_offset
    }
    pub fn keyboard_step_px(&self) -> f32 {
        self.0.borrow().keyboard_step_px
    }

    /// The reactive version signal. The `Splitter` widget binds this at
    /// `BindingLevel::Relayout`.
    pub fn version(&self) -> Signal<u64> {
        self.0.borrow().version.clone()
    }

    /// Immutable per-pane snapshot for the pure sizing engine.
    pub fn pane_snapshots(&self) -> Vec<PaneSnapshot> {
        self.0
            .borrow()
            .panes
            .iter()
            .map(|p| PaneSnapshot {
                stored_size: p.stored_size,
                min_size: p.min_size,
                max_size: p.max_size,
                stretch: p.stretch,
                collapsed: p.collapsed,
                collapsed_size: p.collapsed_size,
                visible: p.visible,
            })
            .collect()
    }

    // ---- Import / export ---------------------------------------------

    /// Snapshot the per-pane sizes + collapsed flags into a serializable
    /// [`SplitterState`].
    pub fn export_state(&self) -> SplitterState {
        let inner = self.0.borrow();
        SplitterState {
            version: SplitterState::CURRENT_VERSION,
            panes: inner
                .panes
                .iter()
                .map(|p| PaneState {
                    stored_size: p.stored_size,
                    collapsed: p.collapsed,
                })
                .collect(),
        }
    }

    /// Restore sizes + collapsed flags from a [`SplitterState`]. Returns
    /// `false` (and changes nothing) if the pane count doesn't match — the
    /// structural config must be reconstructed first. Restoration is
    /// instant (collapsed panes don't animate open on load).
    pub fn import_state(&self, state: &SplitterState) -> bool {
        let ok = {
            let mut inner = self.0.borrow_mut();
            if state.panes.len() != inner.panes.len() {
                false
            } else {
                for (p, s) in inner.panes.iter_mut().zip(&state.panes) {
                    p.stored_size = s.stored_size.max(0.0);
                    p.collapsed = s.collapsed;
                }
                inner.animate_next_collapse = false;
                true
            }
        };
        if ok {
            self.bump_version();
        }
        ok
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn clone_shares_state() {
        let a = SplitterModel::new(3, Orientation::Horizontal);
        let b = a.clone();
        assert_eq!(b.pane_count(), 3);
        a.set_stored_size(0, 200.0);
        assert_eq!(b.stored_size(0), 200.0);
        assert_eq!(a.handle_count(), 2);
    }

    #[test]
    fn version_bumps_on_mutation() {
        let m = SplitterModel::new(2, Orientation::Horizontal);
        let v = m.version();
        let v0 = v.get();
        m.set_stored_size(0, 150.0);
        assert_ne!(v.get(), v0);
        // No-op collapse change must NOT bump.
        let v1 = v.get();
        m.set_collapsed(0, false); // already false
        assert_eq!(v.get(), v1);
    }

    #[test]
    fn export_import_round_trips() {
        let m = SplitterModel::new(3, Orientation::Horizontal);
        m.set_stored_size(0, 120.0);
        m.set_stored_size(1, 340.0);
        m.set_collapsed(2, true);
        let state = m.export_state();

        let restored = SplitterModel::new(3, Orientation::Horizontal);
        assert!(restored.import_state(&state));
        assert_eq!(restored.stored_size(0), 120.0);
        assert_eq!(restored.stored_size(1), 340.0);
        assert!(restored.is_collapsed(2));
    }

    #[test]
    fn import_rejects_pane_count_mismatch() {
        let m = SplitterModel::new(3, Orientation::Horizontal);
        let state = m.export_state();
        let two = SplitterModel::new(2, Orientation::Horizontal);
        assert!(!two.import_state(&state));
    }

    #[test]
    fn insert_remove_change_count() {
        let m = SplitterModel::new(2, Orientation::Horizontal);
        m.insert_pane(1, PaneDescriptor::new().size(100.0));
        assert_eq!(m.pane_count(), 3);
        assert_eq!(m.stored_size(1), 100.0);
        m.remove_pane(0);
        assert_eq!(m.pane_count(), 2);
    }

    #[test]
    fn max_size_enforced_ge_min() {
        let m = SplitterModel::from_panes(
            vec![PaneDescriptor::new().min_size(200.0).max_size(100.0)],
            Orientation::Horizontal,
        );
        // max was clamped up to min.
        assert_eq!(m.max_size(0), Some(200.0));
    }

    #[test]
    fn animate_flag_consumed_and_resets() {
        let m = SplitterModel::new(2, Orientation::Horizontal);
        m.set_collapsed_immediate(0, true);
        assert!(!m.consume_animate_flag()); // immediate path
        // After consuming, it defaults back to animated.
        assert!(m.consume_animate_flag());
    }
}