Add relabel helper to pane_grid::State

This commit is contained in:
Héctor Ramón Jiménez 2024-10-24 14:17:38 +02:00
parent 55504ffcd4
commit d08bc6e45d
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -220,12 +220,8 @@ impl<T> State<T> {
) { ) {
if let Some((state, _)) = self.close(pane) { if let Some((state, _)) = self.close(pane) {
if let Some((new_pane, _)) = self.split(axis, target, state) { if let Some((new_pane, _)) = self.split(axis, target, state) {
// Ensure new node corresponds to original `Pane` for state continuity // Ensure new node corresponds to original closed `Pane` for state continuity
self.swap(pane, new_pane); self.relabel(new_pane, pane);
let _ = self
.panes
.remove(&new_pane)
.and_then(|state| self.panes.insert(pane, state));
if swap { if swap {
self.swap(target, pane); self.swap(target, pane);
@ -258,22 +254,27 @@ impl<T> State<T> {
&mut self, &mut self,
axis: Axis, axis: Axis,
pane: Pane, pane: Pane,
swap: bool, inverse: bool,
) { ) {
if let Some((state, _)) = self.close(pane) { if let Some((state, _)) = self.close(pane) {
if let Some((new_pane, _)) = if let Some((new_pane, _)) =
self.split_node(axis, None, state, swap) self.split_node(axis, None, state, inverse)
{ {
// Ensure new node corresponds to original `Pane` for state continuity // Ensure new node corresponds to original closed `Pane` for state continuity
self.swap(pane, new_pane); self.relabel(new_pane, pane);
let _ = self
.panes
.remove(&new_pane)
.and_then(|state| self.panes.insert(pane, state));
} }
} }
} }
fn relabel(&mut self, target: Pane, label: Pane) {
self.swap(target, label);
let _ = self
.panes
.remove(&target)
.and_then(|state| self.panes.insert(label, state));
}
/// Swaps the position of the provided panes in the [`State`]. /// Swaps the position of the provided panes in the [`State`].
/// ///
/// If you want to swap panes on drag and drop in your [`PaneGrid`], you /// If you want to swap panes on drag and drop in your [`PaneGrid`], you