Skip to content

Commit

Permalink
Introduce drop helper to pane_grid::State
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jul 6, 2023
1 parent ecce8bb commit c5a623f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
11 changes: 3 additions & 8 deletions examples/pane_grid/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,9 @@ impl Application for Example {
Message::Dragged(pane_grid::DragEvent::Dropped {
pane,
target,
}) => match target {
pane_grid::Target::Edge(edge) => {
self.panes.move_to_edge(&pane, edge)
}
pane_grid::Target::Pane(target, region) => {
self.panes.split_with(&target, &pane, region)
}
},
}) => {
self.panes.drop(&pane, target);
}
Message::Dragged(_) => {}
Message::TogglePin(pane) => {
if let Some(Pane { is_pinned, .. }) = self.panes.get_mut(&pane)
Expand Down
58 changes: 34 additions & 24 deletions widget/src/pane_grid/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c5a623f

Please sign in to comment.