summaryrefslogtreecommitdiffstats
path: root/widget/src/pane_grid/state.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2023-05-19 17:16:22 +0200
committerLibravatar GitHub <noreply@github.com>2023-05-19 17:16:22 +0200
commit640e13943c04754ef74e11db470b6c9470640623 (patch)
treef2163bb2cbe7e26ecdd78035203181e42d8a19cb /widget/src/pane_grid/state.rs
parentcc5d11f1a6fca90ea57e3fb3a69587c65281b6b9 (diff)
parent9b5f32ee403c8b0730e3bac2b48aab6b87d7b653 (diff)
downloadiced-640e13943c04754ef74e11db470b6c9470640623.tar.gz
iced-640e13943c04754ef74e11db470b6c9470640623.tar.bz2
iced-640e13943c04754ef74e11db470b6c9470640623.zip
Merge pull request #1856 from jhff/pane_grid_split_with_dragged_pane
[Feature] Enhance PaneGrid to split panes by drag & drop
Diffstat (limited to 'widget/src/pane_grid/state.rs')
-rw-r--r--widget/src/pane_grid/state.rs41
1 files changed, 40 insertions, 1 deletions
diff --git a/widget/src/pane_grid/state.rs b/widget/src/pane_grid/state.rs
index a6e2ec7f..1f034ca3 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,43 @@ 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: &Pane, region: Region) {
+ match region {
+ Region::Center => 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: &Pane,
+ swap: bool,
+ ) {
+ if let Some((state, _)) = self.close(pane) {
+ if let Some((new_pane, _)) = self.split(axis, target, state) {
+ if swap {
+ self.swap(target, &new_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