From 76efa2715bcf5c3f3efe4bb3281feb7f38cad485 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Fri, 28 Feb 2025 18:53:25 +0800 Subject: [PATCH] tiles: Fix panels get item out of bound error when removed some panel. (#675) --- crates/ui/src/dock/tiles.rs | 50 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/crates/ui/src/dock/tiles.rs b/crates/ui/src/dock/tiles.rs index 686a4975..36e68db5 100644 --- a/crates/ui/src/dock/tiles.rs +++ b/crates/ui/src/dock/tiles.rs @@ -847,30 +847,14 @@ impl Tiles { // Handle dragging if let Some(index) = self.dragging_index { - let initial_bounds = self.dragging_initial_bounds; - let current_bounds = self.panels[index].bounds; - if initial_bounds.origin != current_bounds.origin - || initial_bounds.size != current_bounds.size - { - changes_to_push.push(TileChange { - tile_id: self.panels[index].panel.view().entity_id(), - old_bounds: Some(initial_bounds), - new_bounds: Some(current_bounds), - old_order: None, - new_order: None, - version: 0, - }); - } - } - - // Handle resizing - if let Some(index) = self.resizing_index { - if let Some(drag_data) = &self.resizing_drag_data { - let initial_bounds = drag_data.last_bounds; - let current_bounds = self.panels[index].bounds; - if initial_bounds.size != current_bounds.size { + if let Some(item) = self.panels.get(index) { + let initial_bounds = self.dragging_initial_bounds; + let current_bounds = item.bounds; + if initial_bounds.origin != current_bounds.origin + || initial_bounds.size != current_bounds.size + { changes_to_push.push(TileChange { - tile_id: self.panels[index].panel.view().entity_id(), + tile_id: item.panel.view().entity_id(), old_bounds: Some(initial_bounds), new_bounds: Some(current_bounds), old_order: None, @@ -881,6 +865,26 @@ impl Tiles { } } + // Handle resizing + if let Some(index) = self.resizing_index { + if let Some(drag_data) = &self.resizing_drag_data { + if let Some(item) = self.panels.get(index) { + let initial_bounds = drag_data.last_bounds; + let current_bounds = item.bounds; + if initial_bounds.size != current_bounds.size { + changes_to_push.push(TileChange { + tile_id: item.panel.view().entity_id(), + old_bounds: Some(initial_bounds), + new_bounds: Some(current_bounds), + old_order: None, + new_order: None, + version: 0, + }); + } + } + } + } + // Push changes to history if any if !changes_to_push.is_empty() { for change in changes_to_push {