Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an API to move an existing tile to an give container and position index #44

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/tree.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -427,6 +427,41 @@ impl<Pane> Tree<Pane> {
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!(
Expand Down
Loading