ratatui_toolkit/primitives/split_layout/methods/resize_divider.rs
1use crate::primitives::split_layout::constants::{MAX_SPLIT_PERCENT, MIN_SPLIT_PERCENT};
2use crate::primitives::split_layout::enums::layout_node::LayoutNode;
3use crate::primitives::split_layout::PaneId;
4use crate::primitives::split_layout::SplitLayout;
5
6impl SplitLayout {
7 /// Resizes the split containing a pane.
8 ///
9 /// # Arguments
10 /// - `pane_id`: The pane whose split divider should be resized.
11 /// - `percent`: The percentage to allocate to the pane within its split.
12 ///
13 /// # Returns
14 /// `true` when the pane is part of a split and the divider was updated.
15 ///
16 /// # Errors
17 /// - None.
18 ///
19 /// # Panics
20 /// - Does not panic.
21 ///
22 /// # Safety
23 /// - No safety requirements.
24 ///
25 /// # Performance
26 /// - O(n) to locate the pane.
27 ///
28 /// # Example
29 /// ```rust
30 /// use ratatui_toolkit::primitives::split_layout::SplitLayout;
31 ///
32 /// let mut layout = SplitLayout::new(0);
33 /// let pane_id = layout.split_pane_vertically(0).unwrap();
34 /// let _ = layout.resize_divider(pane_id, 30);
35 /// ```
36 pub fn resize_divider(&mut self, pane_id: PaneId, percent: u16) -> bool {
37 let Some((parent_index, is_first)) = self.find_parent_split(pane_id) else {
38 return false;
39 };
40 let Some(LayoutNode::Split { ratio, .. }) = self.nodes.get_mut(parent_index) else {
41 return false;
42 };
43
44 let new_ratio = if is_first {
45 percent
46 } else {
47 100_u16.saturating_sub(percent)
48 };
49
50 *ratio = new_ratio.clamp(MIN_SPLIT_PERCENT, MAX_SPLIT_PERCENT);
51 true
52 }
53}