Avoid panicking when closing a dragged tab

Co-Authored-By: Max <max@zed.dev>
Co-Authored-By: Nathan <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2024-01-15 19:46:34 +01:00
parent a56265e607
commit e60117dc54
2 changed files with 10 additions and 12 deletions

View file

@ -219,8 +219,8 @@ pub struct NavigationEntry {
#[derive(Clone)] #[derive(Clone)]
pub struct DraggedTab { pub struct DraggedTab {
pub pane: View<Pane>, pub pane: View<Pane>,
pub item: Box<dyn ItemHandle>,
pub ix: usize, pub ix: usize,
pub item_id: EntityId,
pub detail: usize, pub detail: usize,
pub is_active: bool, pub is_active: bool,
} }
@ -1310,9 +1310,9 @@ impl Pane {
) )
.on_drag( .on_drag(
DraggedTab { DraggedTab {
item: item.boxed_clone(),
pane: cx.view().clone(), pane: cx.view().clone(),
detail, detail,
item_id,
is_active, is_active,
ix, ix,
}, },
@ -1603,7 +1603,7 @@ impl Pane {
} }
let mut to_pane = cx.view().clone(); let mut to_pane = cx.view().clone();
let split_direction = self.drag_split_direction; let split_direction = self.drag_split_direction;
let item_id = dragged_tab.item_id; let item_id = dragged_tab.item.item_id();
let from_pane = dragged_tab.pane.clone(); let from_pane = dragged_tab.pane.clone();
self.workspace self.workspace
.update(cx, |_, cx| { .update(cx, |_, cx| {
@ -2603,8 +2603,7 @@ mod tests {
impl Render for DraggedTab { impl Render for DraggedTab {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement { fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let ui_font = ThemeSettings::get_global(cx).ui_font.family.clone(); let ui_font = ThemeSettings::get_global(cx).ui_font.family.clone();
let item = &self.pane.read(cx).items[self.ix]; let label = self.item.tab_content(Some(self.detail), false, cx);
let label = item.tab_content(Some(self.detail), false, cx);
Tab::new("") Tab::new("")
.selected(self.is_active) .selected(self.is_active)
.child(label) .child(label)

View file

@ -2250,17 +2250,16 @@ impl Workspace {
destination_index: usize, destination_index: usize,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
let item_to_move = source let Some((item_ix, item_handle)) = source
.read(cx) .read(cx)
.items() .items()
.enumerate() .enumerate()
.find(|(_, item_handle)| item_handle.item_id() == item_id_to_move); .find(|(_, item_handle)| item_handle.item_id() == item_id_to_move)
else {
if item_to_move.is_none() { // Tab was closed during drag
log::warn!("Tried to move item handle which was not in `from` pane. Maybe tab was closed during drop");
return; return;
} };
let (item_ix, item_handle) = item_to_move.unwrap();
let item_handle = item_handle.clone(); let item_handle = item_handle.clone();
if source != destination { if source != destination {