Skip to content

Commit

Permalink
Add move item actions support to the terminal panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Igonato committed Dec 10, 2024
1 parent 3edee86 commit a9252ae
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
30 changes: 26 additions & 4 deletions crates/terminal_view/src/terminal_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ use workspace::{
move_item, pane,
ui::IconName,
ActivateNextPane, ActivatePane, ActivatePaneInDirection, ActivatePreviousPane, DraggedTab,
ItemId, NewTerminal, Pane, PaneGroup, SplitDirection, SplitDown, SplitLeft, SplitRight,
SplitUp, SwapPaneInDirection, ToggleZoom, Workspace,
ItemId, MovableActiveItem, MoveItemToPane, MoveItemToPaneInDirection, NewTerminal, Pane,
PaneGroup, SplitDirection, SplitDown, SplitLeft, SplitRight, SplitUp, SwapPaneInDirection,
ToggleZoom, Workspace,
};

use anyhow::Result;
Expand Down Expand Up @@ -1124,8 +1125,8 @@ impl Render for TerminalPanel {
.detach();
}
}))
.on_action(cx.listener(
|terminal_panel, action: &SwapPaneInDirection, cx| {
.on_action(
cx.listener(|terminal_panel, action: &SwapPaneInDirection, cx| {
if let Some(to) = terminal_panel
.center
.find_pane_in_direction(&terminal_panel.active_pane, action.0, cx)
Expand All @@ -1136,6 +1137,27 @@ impl Render for TerminalPanel {
.swap(&terminal_panel.active_pane.clone(), &to);
cx.notify();
}
}),
)
.on_action(cx.listener(|terminal_panel, action: &MoveItemToPane, cx| {
let panes = terminal_panel.center.panes();
let Some(target_pane) = panes.get(action.0).map(|p| (*p).clone()) else {
return;
};
let source_pane = terminal_panel.active_pane.clone();
source_pane.move_active_item_to(target_pane, true, true, cx);
}))
.on_action(cx.listener(
|terminal_panel, action: &MoveItemToPaneInDirection, cx| {
let source_pane = terminal_panel.active_pane.clone();
let Some(target_pane) = terminal_panel
.center
.find_pane_in_direction(&source_pane, action.0, cx)
.cloned()
else {
return;
};
source_pane.move_active_item_to(target_pane, true, true, cx);
},
))
})
Expand Down
74 changes: 42 additions & 32 deletions crates/workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2846,19 +2846,7 @@ impl Workspace {
let Some(target_pane) = panes.get(action.0).map(|p| (*p).clone()) else {
return;
};
if target_pane == source_pane {
return;
}
let Some(active_item) = source_pane.read(cx).active_item() else {
return;
};
source_pane.update(cx, |pane, cx| {
let item_id = active_item.item_id();
pane.remove_item(item_id, false, true, cx);
target_pane.update(cx, |target_pane, cx| {
target_pane.add_item(active_item, true, true, Some(target_pane.items_len()), cx);
});
});
source_pane.move_active_item_to(target_pane, true, true, cx);
}

pub fn activate_next_pane(&mut self, cx: &mut WindowContext) {
Expand Down Expand Up @@ -2986,25 +2974,7 @@ impl Workspace {
) {
if let Some(target_pane) = self.find_pane_in_direction(direction, cx) {
let source_pane = self.active_pane.clone();
if target_pane == source_pane {
return;
}
let Some(active_item) = source_pane.read(cx).active_item() else {
return;
};
source_pane.update(cx, |pane, cx| {
let item_id = active_item.item_id();
pane.remove_item(item_id, false, true, cx);
target_pane.update(cx, |target_pane, cx| {
target_pane.add_item(
active_item,
true,
true,
Some(target_pane.items_len()),
cx,
);
});
});
source_pane.move_active_item_to(target_pane, true, true, cx);
}
}

Expand Down Expand Up @@ -5201,6 +5171,46 @@ 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 a9252ae

Please sign in to comment.