From 3cb9547e6de4433d619f7d579182a709ab5a0dc5 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler Date: Thu, 25 Jan 2024 16:46:39 +0100 Subject: [PATCH] Add an API to move an existing tile to an give container and position index --- src/tree.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/tree.rs b/src/tree.rs index fde7ef2..4354570 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -1,6 +1,6 @@ use egui::{NumExt as _, Rect, Ui}; -use crate::{ContainerKind, UiResponse}; +use crate::{ContainerInsertion, ContainerKind, UiResponse}; use super::{ is_possible_drag, Behavior, Container, DropContext, InsertionPoint, SimplificationOptions, @@ -427,6 +427,41 @@ impl Tree { self.tiles.gc_root(behavior, self.root); } + /// Move a tile to a new container, at the specified insertion index. + /// + /// If the insertion index is greater than the current number of children, the tile is appended at the end. + pub fn move_tile_to_container( + &mut self, + moved_tile_id: TileId, + destination_container: TileId, + mut insertion_index: usize, + ) { + // find target container + if let Some(Tile::Container(target_container)) = self.tiles.get(destination_container) { + let num_children = target_container.num_children(); + if insertion_index > num_children { + insertion_index = num_children; + } + + let container_insertion = match target_container.kind() { + ContainerKind::Tabs => ContainerInsertion::Tabs(insertion_index), + ContainerKind::Horizontal => ContainerInsertion::Horizontal(insertion_index), + ContainerKind::Vertical => ContainerInsertion::Vertical(insertion_index), + ContainerKind::Grid => ContainerInsertion::Grid(insertion_index), + }; + + self.move_tile( + moved_tile_id, + InsertionPoint { + parent_id: destination_container, + insertion: container_insertion, + }, + ); + } else { + log::warn!("Failed to find destination container {destination_container:?} during `move_tile_to_container()`"); + } + } + /// Move the given tile to the given insertion point. pub(super) fn move_tile(&mut self, moved_tile_id: TileId, insertion_point: InsertionPoint) { log::trace!(