envision 0.15.1

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
//! A resizable split panel layout component.
//!
//! [`SplitPanel`] divides an area into two panes (horizontal or vertical)
//! with a draggable split ratio. The parent controls what to render in
//! each pane — this component only manages the layout and focus. State is
//! stored in [`SplitPanelState`], updated via [`SplitPanelMessage`], and
//! produces [`SplitPanelOutput`].
//!
//!
//! See also [`PaneLayout`](super::PaneLayout) for N-pane layouts.
//!
//! # Example
//!
//! ```rust
//! use envision::component::{
//!     Component, SplitPanel, SplitPanelState,
//!     SplitPanelMessage, SplitOrientation,
//! };
//!
//! let mut state = SplitPanelState::new(SplitOrientation::Vertical);
//!
//! assert_eq!(state.ratio(), 0.5);
//! assert!(state.is_first_pane_focused());
//!
//! // Resize: shift split 10% to the right
//! SplitPanel::update(&mut state, SplitPanelMessage::GrowFirst);
//! assert!((state.ratio() - 0.6).abs() < f32::EPSILON);
//!
//! // Toggle focus to the second pane
//! SplitPanel::update(&mut state, SplitPanelMessage::FocusOther);
//! assert!(state.is_second_pane_focused());
//! ```

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

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

/// The orientation of a split panel.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub enum SplitOrientation {
    /// Panes are arranged left-right (vertical divider).
    Vertical,
    /// Panes are arranged top-bottom (horizontal divider).
    Horizontal,
}

/// Messages that can be sent to a SplitPanel.
#[derive(Clone, Debug, PartialEq)]
pub enum SplitPanelMessage {
    /// Toggle focus between the two panes.
    FocusOther,
    /// Focus the first pane.
    FocusFirst,
    /// Focus the second pane.
    FocusSecond,
    /// Increase the first pane's share by the resize step.
    GrowFirst,
    /// Decrease the first pane's share by the resize step.
    ShrinkFirst,
    /// Set the split ratio directly (0.0 to 1.0).
    SetRatio(f32),
    /// Reset the split to 50/50.
    ResetRatio,
}

/// Output messages from a SplitPanel.
#[derive(Clone, Debug, PartialEq)]
pub enum SplitPanelOutput {
    /// Focus changed to the first pane.
    FocusedFirst,
    /// Focus changed to the second pane.
    FocusedSecond,
    /// The split ratio changed.
    RatioChanged(f32),
}

/// Identifies which pane has focus.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
enum Pane {
    First,
    Second,
}

/// State for a SplitPanel component.
///
/// Manages the split ratio, orientation, and which pane has focus.
/// The parent is responsible for rendering content into each pane.
#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "serialization",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct SplitPanelState {
    /// The orientation of the split.
    orientation: SplitOrientation,
    /// Split ratio: 0.0 = all second pane, 1.0 = all first pane.
    ratio: f32,
    /// Which pane currently has focus.
    focused_pane: Pane,
    /// How much the ratio changes per resize step.
    resize_step: f32,
    /// Minimum ratio (prevents collapsing first pane).
    min_ratio: f32,
    /// Maximum ratio (prevents collapsing second pane).
    max_ratio: f32,
}

impl PartialEq for SplitPanelState {
    fn eq(&self, other: &Self) -> bool {
        self.orientation == other.orientation
            && (self.ratio - other.ratio).abs() < f32::EPSILON
            && self.focused_pane == other.focused_pane
            && (self.resize_step - other.resize_step).abs() < f32::EPSILON
            && (self.min_ratio - other.min_ratio).abs() < f32::EPSILON
            && (self.max_ratio - other.max_ratio).abs() < f32::EPSILON
    }
}

impl Default for SplitPanelState {
    fn default() -> Self {
        Self {
            orientation: SplitOrientation::Vertical,
            ratio: 0.5,
            focused_pane: Pane::First,
            resize_step: 0.1,
            min_ratio: 0.1,
            max_ratio: 0.9,
        }
    }
}

impl SplitPanelState {
    /// Creates a new split panel with the given orientation and a 50/50 split.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let state = SplitPanelState::new(SplitOrientation::Horizontal);
    /// assert_eq!(state.ratio(), 0.5);
    /// assert_eq!(state.orientation(), &SplitOrientation::Horizontal);
    /// ```
    pub fn new(orientation: SplitOrientation) -> Self {
        Self {
            orientation,
            ..Default::default()
        }
    }

    /// Sets the split ratio (builder pattern).
    ///
    /// The ratio is clamped to `[min_ratio, max_ratio]`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let state = SplitPanelState::new(SplitOrientation::Vertical).with_ratio(0.3);
    /// assert!((state.ratio() - 0.3).abs() < f32::EPSILON);
    /// ```
    pub fn with_ratio(mut self, ratio: f32) -> Self {
        self.ratio = ratio.clamp(self.min_ratio, self.max_ratio);
        self
    }

    /// Returns the current orientation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let state = SplitPanelState::new(SplitOrientation::Horizontal);
    /// assert_eq!(state.orientation(), &SplitOrientation::Horizontal);
    /// ```
    pub fn orientation(&self) -> &SplitOrientation {
        &self.orientation
    }

    /// Sets the orientation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let mut state = SplitPanelState::new(SplitOrientation::Vertical);
    /// state.set_orientation(SplitOrientation::Horizontal);
    /// assert_eq!(state.orientation(), &SplitOrientation::Horizontal);
    /// ```
    pub fn set_orientation(&mut self, orientation: SplitOrientation) {
        self.orientation = orientation;
    }

    /// Returns the current split ratio.
    ///
    /// 0.5 means equal split. Values closer to 1.0 give more space
    /// to the first pane.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let state = SplitPanelState::new(SplitOrientation::Vertical).with_ratio(0.4);
    /// assert!((state.ratio() - 0.4).abs() < f32::EPSILON);
    /// ```
    pub fn ratio(&self) -> f32 {
        self.ratio
    }

    /// Sets the split ratio, clamped to `[min_ratio, max_ratio]`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let mut state = SplitPanelState::new(SplitOrientation::Vertical);
    /// state.set_ratio(0.7);
    /// assert!((state.ratio() - 0.7).abs() < f32::EPSILON);
    /// ```
    pub fn set_ratio(&mut self, ratio: f32) {
        self.ratio = ratio.clamp(self.min_ratio, self.max_ratio);
    }

    /// Returns true if the first pane has focus.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let state = SplitPanelState::new(SplitOrientation::Vertical);
    /// assert!(state.is_first_pane_focused());
    /// assert!(!state.is_second_pane_focused());
    /// ```
    pub fn is_first_pane_focused(&self) -> bool {
        self.focused_pane == Pane::First
    }

    /// Returns true if the second pane has focus.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitPanelMessage, SplitOrientation};
    ///
    /// let mut state = SplitPanelState::new(SplitOrientation::Vertical);
    /// state.update(SplitPanelMessage::FocusSecond);
    /// assert!(state.is_second_pane_focused());
    /// ```
    pub fn is_second_pane_focused(&self) -> bool {
        self.focused_pane == Pane::Second
    }

    /// Returns the resize step size (default 0.1 = 10%).
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let state = SplitPanelState::new(SplitOrientation::Vertical).with_resize_step(0.05);
    /// assert!((state.resize_step() - 0.05).abs() < f32::EPSILON);
    /// ```
    pub fn resize_step(&self) -> f32 {
        self.resize_step
    }

    /// Sets the resize step size.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let state = SplitPanelState::new(SplitOrientation::Vertical)
    ///     .with_resize_step(0.05);
    /// assert!((state.resize_step() - 0.05).abs() < f32::EPSILON);
    /// ```
    pub fn with_resize_step(mut self, step: f32) -> Self {
        self.resize_step = step;
        self
    }

    /// Sets the minimum and maximum ratio bounds.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    ///
    /// let state = SplitPanelState::new(SplitOrientation::Vertical)
    ///     .with_bounds(0.2, 0.8);
    /// ```
    pub fn with_bounds(mut self, min: f32, max: f32) -> Self {
        self.min_ratio = min;
        self.max_ratio = max;
        self.ratio = self.ratio.clamp(min, max);
        self
    }

    /// Updates the state with a message, returning any output.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitPanelMessage, SplitPanelOutput, SplitOrientation};
    ///
    /// let mut state = SplitPanelState::new(SplitOrientation::Vertical);
    /// let output = state.update(SplitPanelMessage::FocusSecond);
    /// assert_eq!(output, Some(SplitPanelOutput::FocusedSecond));
    /// ```
    pub fn update(&mut self, msg: SplitPanelMessage) -> Option<SplitPanelOutput> {
        SplitPanel::update(self, msg)
    }

    /// Computes the layout areas for the two panes.
    ///
    /// Returns `(first_pane_area, second_pane_area)`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::component::{SplitPanelState, SplitOrientation};
    /// use ratatui::prelude::Rect;
    ///
    /// let state = SplitPanelState::new(SplitOrientation::Vertical);
    /// let area = Rect::new(0, 0, 80, 24);
    /// let (first, second) = state.layout(area);
    /// assert_eq!(first.width + second.width, 80);
    /// ```
    pub fn layout(&self, area: Rect) -> (Rect, Rect) {
        let direction = match self.orientation {
            SplitOrientation::Vertical => Direction::Horizontal,
            SplitOrientation::Horizontal => Direction::Vertical,
        };

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

        let first_size = ((total as f32) * self.ratio).round() as u16;
        let first_size = first_size.min(total);

        let chunks = Layout::default()
            .direction(direction)
            .constraints([Constraint::Length(first_size), Constraint::Min(0)])
            .split(area);

        (chunks[0], chunks[1])
    }
}

/// A resizable split panel layout component.
///
/// `SplitPanel` manages the split ratio and pane focus for a two-pane
/// layout. The parent uses [`SplitPanelState::layout()`] to get the
/// pane areas and renders content into them.
///
/// # Navigation
///
/// - `Tab` — Toggle focus between panes
/// - `Ctrl+Left/Up` — Grow first pane (shrink second)
/// - `Ctrl+Right/Down` — Shrink first pane (grow second)
/// - `Ctrl+0` — Reset to 50/50 split
///
/// # Rendering
///
/// The `view()` method renders placeholder borders for each pane.
/// For real use, call `state.layout(area)` to get pane areas and
/// render your own content.
///
/// # Example
///
/// ```rust
/// use envision::component::{
///     Component, SplitPanel, SplitPanelState,
///     SplitPanelMessage, SplitOrientation,
/// };
///
/// let mut state = SplitPanelState::new(SplitOrientation::Vertical);
///
/// // Get layout areas for rendering
/// let area = ratatui::layout::Rect::new(0, 0, 80, 24);
/// let (left, right) = state.layout(area);
/// assert!(left.width > 0);
/// assert!(right.width > 0);
/// ```
pub struct SplitPanel;

impl Component for SplitPanel {
    type State = SplitPanelState;
    type Message = SplitPanelMessage;
    type Output = SplitPanelOutput;

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

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

        if let Some(key) = event.as_key() {
            // Tab toggles pane focus
            if key.code == Key::Tab {
                return Some(SplitPanelMessage::FocusOther);
            }

            // Ctrl+arrow resizes
            if key.modifiers.ctrl() {
                match key.code {
                    Key::Left | Key::Up => return Some(SplitPanelMessage::ShrinkFirst),
                    Key::Right | Key::Down => return Some(SplitPanelMessage::GrowFirst),
                    Key::Char('0') => return Some(SplitPanelMessage::ResetRatio),
                    _ => {}
                }
            }

            None
        } else {
            None
        }
    }

    fn update(state: &mut Self::State, msg: Self::Message) -> Option<Self::Output> {
        match msg {
            SplitPanelMessage::FocusOther => {
                state.focused_pane = match state.focused_pane {
                    Pane::First => Pane::Second,
                    Pane::Second => Pane::First,
                };
                match state.focused_pane {
                    Pane::First => Some(SplitPanelOutput::FocusedFirst),
                    Pane::Second => Some(SplitPanelOutput::FocusedSecond),
                }
            }
            SplitPanelMessage::FocusFirst => {
                if state.focused_pane != Pane::First {
                    state.focused_pane = Pane::First;
                    Some(SplitPanelOutput::FocusedFirst)
                } else {
                    None
                }
            }
            SplitPanelMessage::FocusSecond => {
                if state.focused_pane != Pane::Second {
                    state.focused_pane = Pane::Second;
                    Some(SplitPanelOutput::FocusedSecond)
                } else {
                    None
                }
            }
            SplitPanelMessage::GrowFirst => {
                let new_ratio = (state.ratio + state.resize_step).min(state.max_ratio);
                if (new_ratio - state.ratio).abs() > f32::EPSILON {
                    state.ratio = new_ratio;
                    Some(SplitPanelOutput::RatioChanged(new_ratio))
                } else {
                    None
                }
            }
            SplitPanelMessage::ShrinkFirst => {
                let new_ratio = (state.ratio - state.resize_step).max(state.min_ratio);
                if (new_ratio - state.ratio).abs() > f32::EPSILON {
                    state.ratio = new_ratio;
                    Some(SplitPanelOutput::RatioChanged(new_ratio))
                } else {
                    None
                }
            }
            SplitPanelMessage::SetRatio(ratio) => {
                let clamped = ratio.clamp(state.min_ratio, state.max_ratio);
                if (clamped - state.ratio).abs() > f32::EPSILON {
                    state.ratio = clamped;
                    Some(SplitPanelOutput::RatioChanged(clamped))
                } else {
                    None
                }
            }
            SplitPanelMessage::ResetRatio => {
                let target = 0.5_f32.clamp(state.min_ratio, state.max_ratio);
                if (target - state.ratio).abs() > f32::EPSILON {
                    state.ratio = target;
                    Some(SplitPanelOutput::RatioChanged(target))
                } else {
                    None
                }
            }
        }
    }

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

        let (first_area, second_area) = state.layout(ctx.area);

        let first_focused = ctx.focused && state.focused_pane == Pane::First;
        let second_focused = ctx.focused && state.focused_pane == Pane::Second;

        let first_border = if ctx.disabled {
            ctx.theme.disabled_style()
        } else if first_focused {
            ctx.theme.focused_border_style()
        } else {
            ctx.theme.border_style()
        };

        let second_border = if ctx.disabled {
            ctx.theme.disabled_style()
        } else if second_focused {
            ctx.theme.focused_border_style()
        } else {
            ctx.theme.border_style()
        };

        let first_block = Block::default()
            .borders(Borders::ALL)
            .border_style(first_border)
            .title(" Pane 1 ");

        let second_block = Block::default()
            .borders(Borders::ALL)
            .border_style(second_border)
            .title(" Pane 2 ");

        ctx.frame.render_widget(first_block, first_area);
        ctx.frame.render_widget(second_block, second_area);

        crate::annotation::with_registry(|reg| {
            reg.close();
        });
    }
}

#[cfg(test)]
mod snapshot_tests;
#[cfg(test)]
mod tests;