From 99aa54cd88d7eb99149699d539ee4d59e08047b1 Mon Sep 17 00:00:00 2001 From: Joao Freitas <51237625+jhff@users.noreply.github.com> Date: Tue, 16 May 2023 16:12:29 +0100 Subject: Add pane_grid functionality to split a pane with another pane --- widget/src/pane_grid/state.rs | 51 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'widget/src/pane_grid/state.rs') 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 State { 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 -- cgit From bc590e2d6f3a39b09b11928c829f05d7bc9f9211 Mon Sep 17 00:00:00 2001 From: Joao Freitas <51237625+jhff@users.noreply.github.com> Date: Fri, 19 May 2023 12:12:08 +0100 Subject: Take pane state internally --- widget/src/pane_grid/state.rs | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) (limited to 'widget/src/pane_grid/state.rs') diff --git a/widget/src/pane_grid/state.rs b/widget/src/pane_grid/state.rs index b57cdd92..6ab34884 100644 --- a/widget/src/pane_grid/state.rs +++ b/widget/src/pane_grid/state.rs @@ -170,17 +170,9 @@ impl State { /// 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, - ) { + pub fn split_with(&mut self, target: &Pane, pane: &Pane, region: Region) { match region { - Region::Center => { - let (_, pane) = pane; - self.swap(pane, target); - } + Region::Center => self.swap(pane, target), Region::Top => { self.split_and_swap(Axis::Horizontal, target, pane, true) } @@ -200,17 +192,15 @@ impl State { &mut self, axis: Axis, target: &Pane, - pane: (T, &Pane), + pane: &Pane, invert: bool, ) { - let (state, pane) = pane; - - if let Some((new_pane, _)) = self.split(axis, target, state) { - if invert { - self.swap(target, &new_pane); + if let Some((state, _)) = self.close(pane) { + if let Some((new_pane, _)) = self.split(axis, target, state) { + if invert { + self.swap(target, &new_pane); + } } - - let _ = self.close(pane); } } -- cgit From 9b5f32ee403c8b0730e3bac2b48aab6b87d7b653 Mon Sep 17 00:00:00 2001 From: Joao Freitas <51237625+jhff@users.noreply.github.com> Date: Fri, 19 May 2023 12:15:44 +0100 Subject: Rename invert -> swap --- widget/src/pane_grid/state.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'widget/src/pane_grid/state.rs') diff --git a/widget/src/pane_grid/state.rs b/widget/src/pane_grid/state.rs index 6ab34884..1f034ca3 100644 --- a/widget/src/pane_grid/state.rs +++ b/widget/src/pane_grid/state.rs @@ -193,11 +193,11 @@ impl State { axis: Axis, target: &Pane, pane: &Pane, - invert: bool, + swap: bool, ) { if let Some((state, _)) = self.close(pane) { if let Some((new_pane, _)) = self.split(axis, target, state) { - if invert { + if swap { self.swap(target, &new_pane); } } -- cgit