Skip to content

Commit

Permalink
Transfer move item implementation to PaneGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
Igonato committed Dec 11, 2024
1 parent f6e85f3 commit 63a60f9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 47 deletions.
22 changes: 17 additions & 5 deletions crates/terminal_view/src/terminal_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ use workspace::{
move_item, pane,
ui::IconName,
ActivateNextPane, ActivatePane, ActivatePaneInDirection, ActivatePreviousPane, DraggedTab,
ItemId, MovableActiveItem, MoveItemToPane, MoveItemToPaneInDirection, NewTerminal, Pane,
PaneGroup, SplitDirection, SplitDown, SplitLeft, SplitRight, SplitUp, SwapPaneInDirection,
ToggleZoom, Workspace,
ItemId, MoveItemToPane, MoveItemToPaneInDirection, NewTerminal, Pane, PaneGroup,
SplitDirection, SplitDown, SplitLeft, SplitRight, SplitUp, SwapPaneInDirection, ToggleZoom,
Workspace,
};

use anyhow::Result;
Expand Down Expand Up @@ -1145,7 +1145,13 @@ impl Render for TerminalPanel {
return;
};
let source_pane = terminal_panel.active_pane.clone();
source_pane.move_active_item_to(target_pane, true, true, cx);
terminal_panel.center.move_active_item(
&source_pane,
&target_pane,
true,
true,
cx,
);
}))
.on_action(cx.listener(
|terminal_panel, action: &MoveItemToPaneInDirection, cx| {
Expand All @@ -1157,7 +1163,13 @@ impl Render for TerminalPanel {
else {
return;
};
source_pane.move_active_item_to(target_pane, true, true, cx);
terminal_panel.center.move_active_item(
&source_pane,
&target_pane,
true,
true,
cx,
);
},
))
})
Expand Down
29 changes: 29 additions & 0 deletions crates/workspace/src/pane_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ impl PaneGroup {
};
}

pub fn move_active_item(
&self,
from: &View<Pane>,
to: &View<Pane>,
focus_target: bool,
close_if_empty: bool,
cx: &mut WindowContext,
) {
if from == to {
return;
}
let Some(active_item) = from.read(cx).active_item() else {
return;
};
from.update(cx, |source_pane, cx| {
let item_id = active_item.item_id();
source_pane.remove_item(item_id, false, close_if_empty, cx);
to.update(cx, |target_pane, cx| {
target_pane.add_item(
active_item,
focus_target,
focus_target,
Some(target_pane.items_len()),
cx,
);
});
});
}

#[allow(clippy::too_many_arguments)]
pub fn render(
&self,
Expand Down
46 changes: 4 additions & 42 deletions crates/workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2846,7 +2846,8 @@ impl Workspace {
let Some(target_pane) = panes.get(action.0).map(|p| (*p).clone()) else {
return;
};
source_pane.move_active_item_to(target_pane, true, true, cx);
self.center
.move_active_item(&source_pane, &target_pane, true, true, cx);
}

pub fn activate_next_pane(&mut self, cx: &mut WindowContext) {
Expand Down Expand Up @@ -2974,7 +2975,8 @@ impl Workspace {
) {
if let Some(target_pane) = self.find_pane_in_direction(direction, cx) {
let source_pane = self.active_pane.clone();
source_pane.move_active_item_to(target_pane, true, true, cx);
self.center
.move_active_item(&source_pane, &target_pane, true, true, cx);
}
}

Expand Down Expand Up @@ -5171,46 +5173,6 @@ impl WorkspaceHandle for View<Workspace> {
}
}

pub trait MovableActiveItem {
fn move_active_item_to(
&self,
target_pane: View<Pane>,
focus_target: bool,
close_if_empty: bool,
cx: &mut WindowContext,
);
}

impl MovableActiveItem for View<Pane> {
fn move_active_item_to(
&self,
target_pane: View<Pane>,
focus_target: bool,
close_if_empty: bool,
cx: &mut WindowContext,
) {
if &target_pane == self {
return;
}
let Some(active_item) = self.read(cx).active_item() else {
return;
};
self.update(cx, |pane, cx| {
let item_id = active_item.item_id();
pane.remove_item(item_id, false, close_if_empty, cx);
target_pane.update(cx, |target_pane, cx| {
target_pane.add_item(
active_item,
focus_target,
focus_target,
Some(target_pane.items_len()),
cx,
);
});
});
}
}

impl std::fmt::Debug for OpenPaths {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OpenPaths")
Expand Down

0 comments on commit 63a60f9

Please sign in to comment.