envision 0.16.0

A ratatui framework for collaborative TUI development with headless testing support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
//! An N-pane layout manager with proportional sizing and focus cycling.
//!
//! [`PaneLayout`] divides a screen area into multiple panes with configurable
//! proportions, min/max size constraints, and keyboard-driven focus management.
//! The parent controls what to render in each pane — this component only manages
//! the layout and focus. State is stored in [`PaneLayoutState`], updated via
//! [`PaneLayoutMessage`], and produces [`PaneLayoutOutput`]. Panes are configured
//! with [`PaneConfig`].
//!
//!
//! See also [`SplitPanel`](super::SplitPanel) for a simpler two-pane layout.
//!
//! # Example
//!
//! ```rust
//! use envision::component::{
//!     PaneLayout, PaneLayoutState, PaneLayoutMessage, Component,
//!     pane_layout::{PaneConfig, PaneDirection},
//! };
//! use ratatui::prelude::Rect;
//!
//! let panes = vec![
//!     PaneConfig::new("left").with_proportion(0.3),
//!     PaneConfig::new("center").with_proportion(0.5),
//!     PaneConfig::new("right").with_proportion(0.2),
//! ];
//! let mut state = PaneLayoutState::new(PaneDirection::Horizontal, panes);
//!
//! // Get layout rects for a given area
//! let area = Rect::new(0, 0, 100, 40);
//! let rects = state.layout(area);
//! assert_eq!(rects.len(), 3);
//!
//! // Cycle focus
//! PaneLayout::update(&mut state, PaneLayoutMessage::FocusNext);
//! assert_eq!(state.focused_pane_id(), Some("center"));
//! ```

use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders};

use super::{Component, EventContext, RenderContext};
use crate::input::{Event, Key};

/// The direction in which panes are arranged.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum PaneDirection {
    /// Panes are arranged left to right.
    Horizontal,
    /// Panes are arranged top to bottom.
    Vertical,
}

/// Configuration for a single pane.
///
/// # Example
///
/// ```rust
/// use envision::component::pane_layout::PaneConfig;
///
/// let pane = PaneConfig::new("sidebar")
///     .with_title("Files")
///     .with_proportion(0.25)
///     .with_min_size(10)
///     .with_max_size(50);
///
/// assert_eq!(pane.id(), "sidebar");
/// assert_eq!(pane.title(), Some("Files"));
/// ```
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct PaneConfig {
    id: String,
    title: Option<String>,
    proportion: f32,
    min_size: u16,
    max_size: u16,
}

impl PaneConfig {
    /// Creates a new pane with default proportion (equal share).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("sidebar");
    /// assert_eq!(pane.id(), "sidebar");
    /// assert_eq!(pane.title(), None);
    /// ```
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            title: None,
            proportion: 1.0,
            min_size: 1,
            max_size: 0,
        }
    }

    /// Sets the title (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("sidebar").with_title("Files");
    /// assert_eq!(pane.title(), Some("Files"));
    /// ```
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Sets the proportion (builder pattern).
    ///
    /// Proportions are normalized relative to other panes' proportions.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("main").with_proportion(0.7);
    /// assert!((pane.proportion() - 0.7).abs() < f32::EPSILON);
    /// ```
    pub fn with_proportion(mut self, proportion: f32) -> Self {
        self.proportion = proportion.max(0.0);
        self
    }

    /// Sets the minimum size in cells (builder pattern).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("sidebar").with_min_size(20);
    /// assert_eq!(pane.min_size(), 20);
    /// ```
    pub fn with_min_size(mut self, min_size: u16) -> Self {
        self.min_size = min_size.max(1);
        self
    }

    /// Sets the maximum size in cells (builder pattern).
    ///
    /// A value of 0 means no maximum.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("sidebar").with_max_size(60);
    /// assert_eq!(pane.max_size(), 60);
    /// ```
    pub fn with_max_size(mut self, max_size: u16) -> Self {
        self.max_size = max_size;
        self
    }

    /// Returns the pane ID.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("main");
    /// assert_eq!(pane.id(), "main");
    /// ```
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Returns the pane title.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("sidebar").with_title("Files");
    /// assert_eq!(pane.title(), Some("Files"));
    /// ```
    pub fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    /// Sets the pane title.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let mut pane = PaneConfig::new("sidebar");
    /// pane.set_title("Files");
    /// assert_eq!(pane.title(), Some("Files"));
    /// ```
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.title = Some(title.into());
    }

    /// Returns the pane proportion.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("main").with_proportion(0.6);
    /// assert!((pane.proportion() - 0.6).abs() < f32::EPSILON);
    /// ```
    pub fn proportion(&self) -> f32 {
        self.proportion
    }

    /// Returns the minimum size.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("sidebar").with_min_size(15);
    /// assert_eq!(pane.min_size(), 15);
    /// ```
    pub fn min_size(&self) -> u16 {
        self.min_size
    }

    /// Sets the minimum size.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let mut pane = PaneConfig::new("sidebar");
    /// pane.set_min_size(20);
    /// assert_eq!(pane.min_size(), 20);
    /// ```
    pub fn set_min_size(&mut self, min_size: u16) {
        self.min_size = min_size;
    }

    /// Returns the maximum size.
    ///
    /// A value of 0 means no maximum.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let pane = PaneConfig::new("sidebar").with_max_size(80);
    /// assert_eq!(pane.max_size(), 80);
    /// ```
    pub fn max_size(&self) -> u16 {
        self.max_size
    }

    /// Sets the maximum size.
    ///
    /// A value of 0 means no maximum.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let mut pane = PaneConfig::new("sidebar");
    /// pane.set_max_size(60);
    /// assert_eq!(pane.max_size(), 60);
    /// ```
    pub fn set_max_size(&mut self, max_size: u16) {
        self.max_size = max_size;
    }

    /// Sets the pane proportion.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::PaneConfig;
    ///
    /// let mut pane = PaneConfig::new("sidebar");
    /// pane.set_proportion(0.3);
    /// assert!((pane.proportion() - 0.3).abs() < f32::EPSILON);
    /// ```
    pub fn set_proportion(&mut self, proportion: f32) {
        self.proportion = proportion;
    }
}

/// Messages that can be sent to a PaneLayout.
#[derive(Clone, Debug, PartialEq)]
pub enum PaneLayoutMessage {
    /// Focus the next pane (wrapping).
    FocusNext,
    /// Focus the previous pane (wrapping).
    FocusPrev,
    /// Focus a pane by ID.
    FocusPane(String),
    /// Focus a pane by index.
    FocusPaneIndex(usize),
    /// Grow the focused pane.
    GrowFocused,
    /// Shrink the focused pane.
    ShrinkFocused,
    /// Grow a specific pane by ID.
    GrowPane(String),
    /// Shrink a specific pane by ID.
    ShrinkPane(String),
    /// Set a specific pane's proportion.
    SetProportion {
        /// The pane ID.
        id: String,
        /// The new proportion.
        proportion: f32,
    },
    /// Reset all panes to equal proportions.
    ResetProportions,
}

/// Output messages from a PaneLayout.
#[derive(Clone, Debug, PartialEq)]
pub enum PaneLayoutOutput {
    /// Focus changed to a different pane.
    FocusChanged {
        /// The pane ID.
        pane_id: String,
        /// The pane index.
        index: usize,
    },
    /// A pane's proportion changed.
    ProportionChanged {
        /// The pane ID.
        pane_id: String,
        /// The new proportion.
        proportion: f32,
    },
    /// All proportions were reset.
    ProportionsReset,
}

/// State for a PaneLayout component.
///
/// Manages the direction, pane configurations, focus, and proportions.
/// The parent is responsible for rendering content into each pane's area.
///
/// # Example
///
/// ```rust
/// use envision::component::pane_layout::{PaneConfig, PaneDirection};
/// use envision::component::PaneLayoutState;
/// use ratatui::prelude::Rect;
///
/// let panes = vec![
///     PaneConfig::new("a").with_proportion(0.5),
///     PaneConfig::new("b").with_proportion(0.5),
/// ];
/// let state = PaneLayoutState::new(PaneDirection::Horizontal, panes);
///
/// let area = Rect::new(0, 0, 80, 24);
/// let rects = state.layout(area);
/// assert_eq!(rects.len(), 2);
/// assert_eq!(rects[0].width, 40);
/// assert_eq!(rects[1].width, 40);
/// ```
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct PaneLayoutState {
    direction: PaneDirection,
    panes: Vec<PaneConfig>,
    focused_pane: usize,
    resize_step: f32,
}

impl Default for PaneLayoutState {
    fn default() -> Self {
        Self {
            direction: PaneDirection::Horizontal,
            panes: Vec::new(),
            focused_pane: 0,
            resize_step: 0.05,
        }
    }
}

impl PaneLayoutState {
    /// Creates a new pane layout with the given direction and panes.
    ///
    /// Pane proportions are automatically normalized to sum to 1.0.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("left"),
    ///     PaneConfig::new("right"),
    /// ]);
    /// assert_eq!(state.pane_count(), 2);
    /// assert_eq!(state.focused_pane_id(), Some("left"));
    /// ```
    pub fn new(direction: PaneDirection, panes: Vec<PaneConfig>) -> Self {
        let mut state = Self {
            direction,
            panes,
            ..Self::default()
        };
        state.normalize_proportions();
        state
    }

    /// Sets the resize step (builder pattern).
    ///
    /// The resize step controls how much each grow/shrink operation changes
    /// proportions. Defaults to 0.05 (5%).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("a"),
    ///     PaneConfig::new("b"),
    /// ]).with_resize_step(0.1);
    /// assert!((state.resize_step() - 0.1).abs() < f32::EPSILON);
    /// ```
    pub fn with_resize_step(mut self, step: f32) -> Self {
        self.resize_step = step.clamp(0.01, 0.5);
        self
    }

    // ---- Layout computation ----

    /// Computes the layout rectangles for each pane within the given area.
    ///
    /// Respects min/max size constraints. Returns one `Rect` per pane.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    /// use ratatui::prelude::Rect;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Vertical, vec![
    ///     PaneConfig::new("top").with_proportion(0.5),
    ///     PaneConfig::new("bottom").with_proportion(0.5),
    /// ]);
    /// let rects = state.layout(Rect::new(0, 0, 80, 40));
    /// assert_eq!(rects.len(), 2);
    /// assert_eq!(rects[0].height, 20);
    /// assert_eq!(rects[1].height, 20);
    /// ```
    pub fn layout(&self, area: Rect) -> Vec<Rect> {
        if self.panes.is_empty() {
            return vec![];
        }

        let total = match self.direction {
            PaneDirection::Horizontal => area.width,
            PaneDirection::Vertical => area.height,
        };

        let sizes = self.compute_sizes(total);

        let mut rects = Vec::with_capacity(self.panes.len());
        let mut offset = 0u16;

        for (i, &size) in sizes.iter().enumerate() {
            let rect = match self.direction {
                PaneDirection::Horizontal => Rect::new(
                    area.x + offset,
                    area.y,
                    if i == sizes.len() - 1 {
                        total.saturating_sub(offset)
                    } else {
                        size
                    },
                    area.height,
                ),
                PaneDirection::Vertical => Rect::new(
                    area.x,
                    area.y + offset,
                    area.width,
                    if i == sizes.len() - 1 {
                        total.saturating_sub(offset)
                    } else {
                        size
                    },
                ),
            };
            rects.push(rect);
            offset += size;
        }

        rects
    }

    /// Returns the area for a specific pane by ID.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    /// use ratatui::prelude::Rect;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("left").with_proportion(0.5),
    ///     PaneConfig::new("right").with_proportion(0.5),
    /// ]);
    /// let area = Rect::new(0, 0, 80, 24);
    /// let left_area = state.pane_area(area, "left").unwrap();
    /// assert_eq!(left_area.width, 40);
    /// ```
    pub fn pane_area(&self, area: Rect, pane_id: &str) -> Option<Rect> {
        let index = self.panes.iter().position(|p| p.id == pane_id)?;
        let rects = self.layout(area);
        rects.into_iter().nth(index)
    }

    // ---- Accessors ----

    /// Returns the direction.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Vertical, vec![
    ///     PaneConfig::new("top"),
    ///     PaneConfig::new("bottom"),
    /// ]);
    /// assert_eq!(state.direction(), &PaneDirection::Vertical);
    /// ```
    pub fn direction(&self) -> &PaneDirection {
        &self.direction
    }

    /// Returns the pane configurations.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("left").with_title("Left"),
    ///     PaneConfig::new("right").with_title("Right"),
    /// ]);
    /// assert_eq!(state.panes().len(), 2);
    /// assert_eq!(state.panes()[0].title(), Some("Left"));
    /// ```
    pub fn panes(&self) -> &[PaneConfig] {
        &self.panes
    }

    /// Returns the number of panes.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("left"),
    ///     PaneConfig::new("right"),
    /// ]);
    /// assert_eq!(state.pane_count(), 2);
    /// ```
    pub fn pane_count(&self) -> usize {
        self.panes.len()
    }

    /// Returns the focused pane index.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("left"),
    ///     PaneConfig::new("right"),
    /// ]);
    /// assert_eq!(state.focused_pane_index(), 0);
    /// ```
    pub fn focused_pane_index(&self) -> usize {
        self.focused_pane
    }

    /// Returns the focused pane ID, if panes exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("left"),
    ///     PaneConfig::new("right"),
    /// ]);
    /// assert_eq!(state.focused_pane_id(), Some("left"));
    /// ```
    pub fn focused_pane_id(&self) -> Option<&str> {
        self.panes.get(self.focused_pane).map(|p| p.id.as_str())
    }

    /// Returns a pane configuration by ID.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("sidebar").with_title("Files"),
    /// ]);
    /// let pane = state.pane("sidebar").unwrap();
    /// assert_eq!(pane.title(), Some("Files"));
    /// ```
    pub fn pane(&self, id: &str) -> Option<&PaneConfig> {
        self.panes.iter().find(|p| p.id == id)
    }

    /// Returns the resize step.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::PaneLayoutState;
    ///
    /// let state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("a"),
    ///     PaneConfig::new("b"),
    /// ]);
    /// assert!((state.resize_step() - 0.05).abs() < f32::EPSILON); // default
    /// ```
    pub fn resize_step(&self) -> f32 {
        self.resize_step
    }

    // ---- Instance methods ----

    /// Updates the state with a message, returning any output.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::pane_layout::{PaneConfig, PaneDirection};
    /// use envision::component::{PaneLayoutState, PaneLayoutMessage, PaneLayoutOutput};
    ///
    /// let mut state = PaneLayoutState::new(PaneDirection::Horizontal, vec![
    ///     PaneConfig::new("left"),
    ///     PaneConfig::new("right"),
    /// ]);
    /// let output = state.update(PaneLayoutMessage::FocusNext);
    /// assert_eq!(state.focused_pane_id(), Some("right"));
    /// ```
    pub fn update(&mut self, msg: PaneLayoutMessage) -> Option<PaneLayoutOutput> {
        PaneLayout::update(self, msg)
    }

    // ---- Internal ----

    fn normalize_proportions(&mut self) {
        let total: f32 = self.panes.iter().map(|p| p.proportion).sum();
        if total > 0.0 {
            for pane in &mut self.panes {
                pane.proportion /= total;
            }
        }
    }

    fn compute_sizes(&self, total: u16) -> Vec<u16> {
        let n = self.panes.len();
        if n == 0 {
            return vec![];
        }

        let total_f = total as f32;
        let mut sizes: Vec<u16> = self
            .panes
            .iter()
            .map(|p| {
                let raw = (p.proportion * total_f).round() as u16;
                let clamped_min = raw.max(p.min_size);
                if p.max_size > 0 {
                    clamped_min.min(p.max_size)
                } else {
                    clamped_min
                }
            })
            .collect();

        // Adjust to exactly fill the total space
        let computed_total: u16 = sizes.iter().sum();
        if computed_total != total && !sizes.is_empty() {
            let diff = total as i32 - computed_total as i32;
            // Distribute the difference to the last pane
            let last = sizes.len() - 1;
            sizes[last] = (sizes[last] as i32 + diff).max(1) as u16;
        }

        sizes
    }

    fn grow_pane(&mut self, index: usize) -> Option<PaneLayoutOutput> {
        if self.panes.len() < 2 || index >= self.panes.len() {
            return None;
        }

        let step = self.resize_step;
        let min_proportion = 0.05;

        // Find a neighbor to take from
        let neighbor = if index + 1 < self.panes.len() {
            index + 1
        } else {
            index - 1
        };

        if self.panes[neighbor].proportion - step < min_proportion {
            return None;
        }

        self.panes[index].proportion += step;
        self.panes[neighbor].proportion -= step;
        self.normalize_proportions();

        Some(PaneLayoutOutput::ProportionChanged {
            pane_id: self.panes[index].id.clone(),
            proportion: self.panes[index].proportion,
        })
    }

    fn shrink_pane(&mut self, index: usize) -> Option<PaneLayoutOutput> {
        if self.panes.len() < 2 || index >= self.panes.len() {
            return None;
        }

        let step = self.resize_step;
        let min_proportion = 0.05;

        if self.panes[index].proportion - step < min_proportion {
            return None;
        }

        // Find a neighbor to give to
        let neighbor = if index + 1 < self.panes.len() {
            index + 1
        } else {
            index - 1
        };

        self.panes[index].proportion -= step;
        self.panes[neighbor].proportion += step;
        self.normalize_proportions();

        Some(PaneLayoutOutput::ProportionChanged {
            pane_id: self.panes[index].id.clone(),
            proportion: self.panes[index].proportion,
        })
    }
}

/// An N-pane layout manager component.
///
/// `PaneLayout` manages the layout of multiple panes with proportional
/// sizing and focus cycling. The parent renders content into each pane's
/// computed area using [`PaneLayoutState::layout`].
///
/// # Key Bindings
///
/// - `Tab` — Focus next pane
/// - `BackTab` — Focus previous pane
/// - `Ctrl+Right` / `Ctrl+Down` — Grow focused pane
/// - `Ctrl+Left` / `Ctrl+Up` — Shrink focused pane
/// - `Ctrl+0` — Reset proportions
///
/// # Example
///
/// ```rust
/// use envision::component::{
///     PaneLayout, PaneLayoutState, PaneLayoutMessage, Component,
///     pane_layout::{PaneConfig, PaneDirection},
/// };
/// use ratatui::prelude::Rect;
///
/// let panes = vec![
///     PaneConfig::new("left"),
///     PaneConfig::new("right"),
/// ];
/// let mut state = PaneLayoutState::new(PaneDirection::Horizontal, panes);
///
/// let area = Rect::new(0, 0, 80, 24);
/// let rects = state.layout(area);
/// assert_eq!(rects.len(), 2);
/// ```
pub struct PaneLayout;

impl Component for PaneLayout {
    type State = PaneLayoutState;
    type Message = PaneLayoutMessage;
    type Output = PaneLayoutOutput;

    fn init() -> Self::State {
        PaneLayoutState::default()
    }

    fn handle_event(
        _state: &Self::State,
        event: &Event,
        ctx: &EventContext,
    ) -> Option<Self::Message> {
        if !ctx.focused || ctx.disabled {
            return None;
        }

        let key = event.as_key()?;
        let ctrl = key.modifiers.ctrl();

        match key.code {
            Key::Tab if key.modifiers.shift() => Some(PaneLayoutMessage::FocusPrev),
            Key::Tab if !ctrl => Some(PaneLayoutMessage::FocusNext),
            Key::Right | Key::Down if ctrl => Some(PaneLayoutMessage::GrowFocused),
            Key::Left | Key::Up if ctrl => Some(PaneLayoutMessage::ShrinkFocused),
            Key::Char('0') if ctrl => Some(PaneLayoutMessage::ResetProportions),
            _ => None,
        }
    }

    fn update(state: &mut Self::State, msg: Self::Message) -> Option<Self::Output> {
        match msg {
            PaneLayoutMessage::FocusNext => {
                if state.panes.is_empty() {
                    return None;
                }
                state.focused_pane = (state.focused_pane + 1) % state.panes.len();
                Some(PaneLayoutOutput::FocusChanged {
                    pane_id: state.panes[state.focused_pane].id.clone(),
                    index: state.focused_pane,
                })
            }
            PaneLayoutMessage::FocusPrev => {
                if state.panes.is_empty() {
                    return None;
                }
                state.focused_pane = state
                    .focused_pane
                    .checked_sub(1)
                    .unwrap_or(state.panes.len() - 1);
                Some(PaneLayoutOutput::FocusChanged {
                    pane_id: state.panes[state.focused_pane].id.clone(),
                    index: state.focused_pane,
                })
            }
            PaneLayoutMessage::FocusPane(id) => {
                if let Some(index) = state.panes.iter().position(|p| p.id == id) {
                    state.focused_pane = index;
                    Some(PaneLayoutOutput::FocusChanged { pane_id: id, index })
                } else {
                    None
                }
            }
            PaneLayoutMessage::FocusPaneIndex(index) => {
                if index >= state.panes.len() {
                    return None;
                }
                state.focused_pane = index;
                Some(PaneLayoutOutput::FocusChanged {
                    pane_id: state.panes[index].id.clone(),
                    index,
                })
            }
            PaneLayoutMessage::GrowFocused => {
                let index = state.focused_pane;
                state.grow_pane(index)
            }
            PaneLayoutMessage::ShrinkFocused => {
                let index = state.focused_pane;
                state.shrink_pane(index)
            }
            PaneLayoutMessage::GrowPane(id) => {
                if let Some(index) = state.panes.iter().position(|p| p.id == id) {
                    state.grow_pane(index)
                } else {
                    None
                }
            }
            PaneLayoutMessage::ShrinkPane(id) => {
                if let Some(index) = state.panes.iter().position(|p| p.id == id) {
                    state.shrink_pane(index)
                } else {
                    None
                }
            }
            PaneLayoutMessage::SetProportion { id, proportion } => {
                if let Some(index) = state.panes.iter().position(|p| p.id == id) {
                    state.panes[index].proportion = proportion.max(0.0);
                    state.normalize_proportions();
                    Some(PaneLayoutOutput::ProportionChanged {
                        pane_id: id,
                        proportion: state.panes[index].proportion,
                    })
                } else {
                    None
                }
            }
            PaneLayoutMessage::ResetProportions => {
                if state.panes.is_empty() {
                    return None;
                }
                let equal = 1.0 / state.panes.len() as f32;
                for pane in &mut state.panes {
                    pane.proportion = equal;
                }
                Some(PaneLayoutOutput::ProportionsReset)
            }
        }
    }

    fn view(state: &Self::State, ctx: &mut RenderContext<'_, '_>) {
        crate::annotation::with_registry(|reg| {
            reg.register(
                ctx.area,
                crate::annotation::Annotation::new(crate::annotation::WidgetType::PaneLayout)
                    .with_id("pane_layout")
                    .with_focus(ctx.focused)
                    .with_disabled(ctx.disabled),
            );
        });

        let rects = state.layout(ctx.area);

        for (i, (pane, rect)) in state.panes.iter().zip(rects.iter()).enumerate() {
            let is_focused_pane = ctx.focused && i == state.focused_pane;
            let border_style = if ctx.disabled {
                ctx.theme.disabled_style()
            } else if is_focused_pane {
                ctx.theme.focused_border_style()
            } else {
                ctx.theme.border_style()
            };

            let mut block = Block::default()
                .borders(Borders::ALL)
                .border_style(border_style);

            if let Some(title) = &pane.title {
                block = block.title(format!(" {} ", title));
            }

            ctx.frame.render_widget(block, *rect);
        }
    }
}

#[cfg(test)]
mod tests;