diff options
author | 2023-05-16 16:12:29 +0100 | |
---|---|---|
committer | 2023-05-16 16:12:29 +0100 | |
commit | 99aa54cd88d7eb99149699d539ee4d59e08047b1 (patch) | |
tree | acf92d0abab84919f0dc660aa234c824a2f96632 /widget/src/pane_grid | |
parent | 8e8b1e1eacc4e2c19c9878625f423c8e09e2d3b9 (diff) | |
download | iced-99aa54cd88d7eb99149699d539ee4d59e08047b1.tar.gz iced-99aa54cd88d7eb99149699d539ee4d59e08047b1.tar.bz2 iced-99aa54cd88d7eb99149699d539ee4d59e08047b1.zip |
Add pane_grid functionality to split a pane with another pane
Diffstat (limited to 'widget/src/pane_grid')
-rw-r--r-- | widget/src/pane_grid/state.rs | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/widget/src/pane_grid/state.rs b/widget/src/pane_grid/state.rs index a6e2ec7f..b57cdd92 100644 --- a/widget/src/pane_grid/state.rs +++ b/widget/src/pane_grid/state.rs @@ -2,7 +2,9 @@ //! //! [`PaneGrid`]: crate::widget::PaneGrid use crate::core::{Point, Size}; -use crate::pane_grid::{Axis, Configuration, Direction, Node, Pane, Split}; +use crate::pane_grid::{ + Axis, Configuration, Direction, Node, Pane, Region, Split, +}; use std::collections::HashMap; @@ -165,6 +167,53 @@ impl<T> State<T> { Some((new_pane, new_split)) } + /// Split a target [`Pane`] with a given [`Pane`] on a given [`Region`]. + /// + /// Panes will be swapped by default for [`Region::Center`]. + pub fn split_with( + &mut self, + target: &Pane, + pane: (T, &Pane), + region: Region, + ) { + match region { + Region::Center => { + let (_, pane) = pane; + self.swap(pane, target); + } + Region::Top => { + self.split_and_swap(Axis::Horizontal, target, pane, true) + } + Region::Bottom => { + self.split_and_swap(Axis::Horizontal, target, pane, false) + } + Region::Left => { + self.split_and_swap(Axis::Vertical, target, pane, true) + } + Region::Right => { + self.split_and_swap(Axis::Vertical, target, pane, false) + } + } + } + + fn split_and_swap( + &mut self, + axis: Axis, + target: &Pane, + pane: (T, &Pane), + invert: bool, + ) { + let (state, pane) = pane; + + if let Some((new_pane, _)) = self.split(axis, target, state) { + if invert { + self.swap(target, &new_pane); + } + + let _ = self.close(pane); + } + } + /// Swaps the position of the provided panes in the [`State`]. /// /// If you want to swap panes on drag and drop in your [`PaneGrid`], you |