Introduce drop helper to pane_grid::State

This commit is contained in:
Héctor Ramón Jiménez 2023-07-06 07:45:47 +02:00
parent ecce8bbcee
commit c5a623f32b
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 37 additions and 32 deletions

View file

@ -3,7 +3,7 @@
//! [`PaneGrid`]: crate::widget::PaneGrid
use crate::core::{Point, Size};
use crate::pane_grid::{
Axis, Configuration, Direction, Edge, Node, Pane, Region, Split,
Axis, Configuration, Direction, Edge, Node, Pane, Region, Split, Target,
};
use std::collections::HashMap;
@ -148,6 +148,39 @@ impl<T> State<T> {
self.split_node(axis, Some(pane), state, false)
}
/// 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::Edge(edge) => match edge {
Edge::Top => {
self.split_and_swap(Axis::Horizontal, target, pane, true)
}
Edge::Bottom => {
self.split_and_swap(Axis::Horizontal, target, pane, false)
}
Edge::Left => {
self.split_and_swap(Axis::Vertical, target, pane, true)
}
Edge::Right => {
self.split_and_swap(Axis::Vertical, target, pane, false)
}
},
}
}
/// Drops the given [`Pane`] into the provided [`Target`].
pub fn drop(&mut self, pane: &Pane, target: Target) {
match target {
Target::Edge(edge) => self.move_to_edge(pane, edge),
Target::Pane(target, region) => {
self.split_with(&target, pane, region)
}
}
}
fn split_node(
&mut self,
axis: Axis,
@ -186,29 +219,6 @@ 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::Edge(edge) => match edge {
Edge::Top => {
self.split_and_swap(Axis::Horizontal, target, pane, true)
}
Edge::Bottom => {
self.split_and_swap(Axis::Horizontal, target, pane, false)
}
Edge::Left => {
self.split_and_swap(Axis::Vertical, target, pane, true)
}
Edge::Right => {
self.split_and_swap(Axis::Vertical, target, pane, false)
}
},
}
}
fn split_and_swap(
&mut self,
axis: Axis,