Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

feat(panes): swap two panes by dropping one on the other's middle

RoversX9376880d79cc
diff --git a/codec/src/lib.rs b/codec/src/lib.rs
index 85f00cae..c260ae87 100644
--- a/codec/src/lib.rs
+++ b/codec/src/lib.rs
@@ -827,7 +827,8 @@ macro_rules! pdu {
/// 81: Pane labels (PaneLabelChanged, ListPanesResponse::pane_labels), the
/// threads behind workspaces (GetWorkspaceNames), and
/// ClientJoined/ClientLeft.
-pub const CODEC_VERSION: usize = 81;
+/// 82: SwapPanes swaps two panes of a tab, each into the other's place.
+pub const CODEC_VERSION: usize = 82;
// Defines the Pdu enum.
// Each struct has an explicit identifying number.
@@ -947,6 +948,7 @@ pdu! {
GetWorkspaceNamesResponse: 119,
ClientJoined: 120,
ClientLeft: 121,
+ SwapPanes: 122,
}
impl Pdu {
@@ -987,6 +989,7 @@ impl Pdu {
| Self::SpawnPaneInStack(_)
| Self::ActivatePaneInStack(_)
| Self::MovePaneToStack(_)
+ | Self::SwapPanes(_)
| Self::MoveTab(_) => true,
_ => false,
}
@@ -1282,6 +1285,15 @@ pub struct MovePaneToStack {
pub target_pane_id: PaneId,
}
+/// Swap two panes of a tab, each into the other's place in its stack; the
+/// first is left the active pane. Both ids are in the mux server's pane-id
+/// space. Responds with UnitResponse.
+#[derive(Deserialize, Serialize, PartialEq, Debug)]
+pub struct SwapPanes {
+ pub pane_id: PaneId,
+ pub other_pane_id: PaneId,
+}
+
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct MovePaneToNewTab {
pub pane_id: PaneId,
@@ -3076,6 +3088,27 @@ mod test {
);
}
+ #[test]
+ fn swap_panes_round_trip() {
+ let mut encoded = Vec::new();
+ Pdu::SwapPanes(SwapPanes {
+ pane_id: 17,
+ other_pane_id: 29,
+ })
+ .encode(&mut encoded, 0x42)
+ .unwrap();
+ assert_eq!(
+ Pdu::decode(encoded.as_slice()).unwrap(),
+ DecodedPdu {
+ serial: 0x42,
+ pdu: Pdu::SwapPanes(SwapPanes {
+ pane_id: 17,
+ other_pane_id: 29,
+ }),
+ }
+ );
+ }
+
#[test]
fn thinkterm_tree_round_trip() {
let mut tree = ThinkTermTree::default();
@@ -3165,7 +3198,7 @@ mod test {
// tripwire: whoever bumps the codec must come here, confirm the
// round-trips still cover the new version, and advance it
// deliberately.
- assert_eq!(CODEC_VERSION, 81);
+ assert_eq!(CODEC_VERSION, 82);
use thinkterm_proto::{
AgentEvidence, AgentState, AgentStatus, ProgramBlockedKind, ProgramReport,
ProgramReportChild, ProgramReportState,
@@ -3235,7 +3268,7 @@ mod test {
#[test]
fn foreground_program_protocol_round_trip_at_version_74() {
// Same tripwire as the agent-status test above.
- assert_eq!(CODEC_VERSION, 81);
+ assert_eq!(CODEC_VERSION, 82);
use thinkterm_proto::ForegroundProgram;
fn round_trip(pdu: &Pdu) -> Pdu {
@@ -3336,7 +3369,7 @@ mod test {
#[test]
fn run_state_protocol_round_trip_at_version_80() {
// Same tripwire as the agent-status test above.
- assert_eq!(CODEC_VERSION, 81);
+ assert_eq!(CODEC_VERSION, 82);
use thinkterm_proto::{PaneExit, PaneRunState, ProgramState, ShellCommands};
let exit = PaneExit {
@@ -3435,7 +3468,7 @@ mod test {
fn frame_control_extension_round_trips_without_changing_legacy_version() {
// The extension is asked for, so it needed no bump of its own; 74
// is the foreground program's, which a server sends unasked.
- assert_eq!(CODEC_VERSION, 81);
+ assert_eq!(CODEC_VERSION, 82);
for pdu in [
Pdu::GetKittyImage(GetKittyImage { pane_id: 3, image_id: 7, data_hash: [9; 32], have_frames: 2, image_epoch: Some(4) }),
Pdu::GetKittyFrameSelections(GetKittyFrameSelections { pane_id: 3, subscribe: true }),
diff --git a/mux/src/domain.rs b/mux/src/domain.rs
index 4cfc89e7..a9a08e90 100644
--- a/mux/src/domain.rs
+++ b/mux/src/domain.rs
@@ -155,6 +155,21 @@ pub trait Domain: Downcast + Send + Sync {
Ok(pane)
}
+ /// Swap two panes of a tab, each into the other's place. The default
+ /// implementation mutates the local mux tree. Proxy domains override
+ /// this so the authoritative remote tree is changed as well.
+ async fn swap_panes(
+ &self,
+ tab_id: TabId,
+ pane_id: PaneId,
+ other_pane_id: PaneId,
+ ) -> anyhow::Result<()> {
+ let tab = Mux::get()
+ .get_tab(tab_id)
+ .ok_or_else(|| anyhow::anyhow!("Invalid tab id {tab_id}"))?;
+ tab.swap_panes(pane_id, other_pane_id)
+ }
+
/// The mux will call this method on the domain of the pane that
/// is being moved to give the domain a chance to handle the movement.
/// If this method returns Ok(None), then the mux will handle the
diff --git a/mux/src/lib.rs b/mux/src/lib.rs
index 4ece226c..a1ba2687 100644
--- a/mux/src/lib.rs
+++ b/mux/src/lib.rs
@@ -3771,6 +3771,31 @@ impl Mux {
.await
}
+ /// Swap two panes of the same tab, each into the other's place; the
+ /// first is left active. The domain owns the mutation, as for
+ /// `move_pane_to_stack`.
+ pub async fn swap_panes(&self, pane_id: PaneId, other_pane_id: PaneId) -> anyhow::Result<()> {
+ if pane_id == other_pane_id {
+ anyhow::bail!("cannot swap pane {pane_id} with itself");
+ }
+ let (domain_id, _window_id, tab_id) = self
+ .resolve_pane_id(pane_id)
+ .ok_or_else(|| anyhow::anyhow!("pane {pane_id} not found"))?;
+ let (other_domain_id, _other_window_id, other_tab_id) = self
+ .resolve_pane_id(other_pane_id)
+ .ok_or_else(|| anyhow::anyhow!("pane {other_pane_id} not found"))?;
+ if domain_id != other_domain_id || tab_id != other_tab_id {
+ anyhow::bail!(
+ "cannot swap pane {pane_id} with pane {other_pane_id}: \
+ they are not in the same tab of one domain"
+ );
+ }
+ let domain = self
+ .get_domain(domain_id)
+ .ok_or_else(|| anyhow::anyhow!("domain {domain_id} not found"))?;
+ domain.swap_panes(tab_id, pane_id, other_pane_id).await
+ }
+
pub async fn move_pane_to_new_tab(
&self,
pane_id: PaneId,
diff --git a/mux/src/tab.rs b/mux/src/tab.rs
index 6386ac18..13153b3d 100644
--- a/mux/src/tab.rs
+++ b/mux/src/tab.rs
@@ -1281,6 +1281,13 @@ impl Tab {
.move_pane_to_stack(src_pane_id, target_pane_id)
}
+ /// Swap two panes, each into the other's place in its stack and to that
+ /// stack's size; the stacks and the panes around them stay as they are.
+ /// The first is left in front of its new stack and active.
+ pub fn swap_panes(&self, pane_id: PaneId, other_pane_id: PaneId) -> anyhow::Result<()> {
+ self.inner.lock().swap_panes(pane_id, other_pane_id)
+ }
+
/// Re-attach a live pane that was detached mid-operation (e.g. a
/// MovePane split that failed after removal): push it into the first
/// leaf stack so it never ends up outside every tab.
@@ -3494,6 +3501,100 @@ impl TabInner {
Ok(())
}
+ fn swap_panes(&mut self, pane_id: PaneId, other_pane_id: PaneId) -> anyhow::Result<()> {
+ if pane_id == other_pane_id {
+ anyhow::bail!("cannot swap pane {pane_id} with itself");
+ }
+ if self.zoomed.is_some() {
+ anyhow::bail!("cannot swap panes {pane_id} and {other_pane_id} while one is zoomed");
+ }
+
+ // Validation pass: each pane, the size of the stack it is in, as
+ // its shown pane has it, and the stack's place among the leaves.
+ let mut found: [Option<(Arc<dyn Pane>, Option<TerminalSize>, usize)>; 2] = [None, None];
+ {
+ let mut cursor = self.pane.take().unwrap().cursor();
+ let mut leaf = 0;
+ loop {
+ if cursor.is_leaf() {
+ let stack = cursor.leaf_mut().unwrap();
+ for (slot, id) in [pane_id, other_pane_id].iter().copied().enumerate() {
+ if let Some(pane) = stack.pane_by_id(id) {
+ let size = stack
+ .active_pane()
+ .map(|shown| shown.summary_without_waiting().terminal_size());
+ found[slot] = Some((pane, size, leaf));
+ }
+ }
+ leaf += 1;
+ }
+ match cursor.preorder_next() {
+ Ok(c) => cursor = c,
+ Err(c) => {
+ self.pane.replace(c.tree());
+ break;
+ }
+ }
+ }
+ }
+ let [Some((pane, size, leaf)), Some((other, other_size, other_leaf))] = found else {
+ anyhow::bail!("panes {pane_id} and {other_pane_id} are not both in tab {}", self.id);
+ };
+ if leaf == other_leaf {
+ anyhow::bail!("panes {pane_id} and {other_pane_id} are already in the same stack");
+ }
+
+ // The only fallible steps come before the tree is touched: each
+ // takes the size of the other's stack, or both keep their own.
+ let own = pane.summary_without_waiting().terminal_size();
+ if let Some(to) = other_size.filter(|_| !pane.is_remote_mirror()) {
+ pane.resize(to)?;
+ }
+ if let Some(to) = size.filter(|_| !other.is_remote_mirror()) {
+ if let Err(err) = other.resize(to) {
+ if !pane.is_remote_mirror() {
+ if let Err(err) = pane.resize(own) {
+ log::error!("swap_panes: resize back failed: {err:#}");
+ }
+ }
+ return Err(err);
+ }
+ }
+
+ let prior = self.get_active_pane();
+ {
+ let mut cursor = self.pane.take().unwrap().cursor();
+ loop {
+ if cursor.is_leaf() {
+ let stack = cursor.leaf_mut().unwrap();
+ for index in 0..stack.panes.len() {
+ let id = stack.panes[index].pane_id();
+ if id == pane_id {
+ stack.panes[index] = Arc::clone(&other);
+ } else if id == other_pane_id {
+ stack.panes[index] = Arc::clone(&pane);
+ stack.active = index;
+ }
+ }
+ }
+ match cursor.preorder_next() {
+ Ok(c) => cursor = c,
+ Err(c) => {
+ self.pane.replace(c.tree());
+ break;
+ }
+ }
+ }
+ }
+
+ self.active = other_leaf;
+ self.recency.tag(other_leaf);
+ self.advise_focus_change(prior);
+ Mux::try_get().map(|mux| mux.notify(MuxNotification::TabResized(self.id)));
+
+ Ok(())
+ }
+
/// Last-resort re-homing used when the attach phase of a pane move
/// cannot find its target: push the pane into the first leaf stack so
/// it never ends up detached from every tab.
@@ -5509,6 +5610,78 @@ mod test {
assert_eq!(tab.get_active_pane().unwrap().pane_id(), 3);
}
+ #[test]
+ fn swap_panes_trades_places_and_sizes() {
+ let _mux = install_mux();
+ let size = test_size();
+ let tab = Tab::new(&size);
+ tab.assign_pane(&FakePane::new(1, size));
+ let horz_size = tab.compute_split_size(0, SplitRequest::default()).unwrap();
+ tab.split_and_insert(
+ 0,
+ SplitRequest::default(),
+ FakePane::new(2, horz_size.second),
+ )
+ .unwrap();
+ // A third, out of sight behind the second in its stack.
+ tab.add_pane_to_stack(2, FakePane::new(3, horz_size.second))
+ .unwrap();
+ tab.activate_pane_in_stack(2).unwrap();
+ let before = tab.iter_panes();
+
+ tab.swap_panes(1, 2).unwrap();
+
+ let after = tab.iter_panes();
+ let shown: Vec<PaneId> = after.iter().map(|pos| pos.pane.pane_id()).collect();
+ assert_eq!(shown, vec![2, 1]);
+ for (was, now) in before.iter().zip(&after) {
+ // The stacks stay where they were, the other pane in each.
+ assert_eq!(
+ (now.left, now.width, now.pane_stack_id),
+ (was.left, was.width, was.pane_stack_id)
+ );
+ // And each pane is its new stack's size.
+ assert_eq!(now.pane.get_dimensions().cols, now.width);
+ }
+ // 1 in front of 3, where 2 was, and active.
+ let tabs = tab.pane_stack_tabs(1);
+ assert_eq!(
+ tabs.iter().map(|tab| tab.pane_id).collect::<Vec<_>>(),
+ vec![1, 3]
+ );
+ assert!(tabs[0].is_active);
+ assert_eq!(tab.get_active_pane().unwrap().pane_id(), 1);
+ }
+
+ #[test]
+ fn swap_panes_refuses_what_it_cannot_do_leaving_the_tree() {
+ let _mux = install_mux();
+ let size = test_size();
+ let tab = Tab::new(&size);
+ tab.assign_pane(&FakePane::new(1, size));
+ tab.add_pane_to_stack(1, FakePane::new(3, size)).unwrap();
+
+ assert!(tab.swap_panes(1, 1).is_err());
+ assert!(tab.swap_panes(1, 3).is_err());
+ assert!(tab.swap_panes(1, 99).is_err());
+ let tabs = tab.pane_stack_tabs(1);
+ assert_eq!(
+ tabs.iter().map(|tab| tab.pane_id).collect::<Vec<_>>(),
+ vec![1, 3]
+ );
+ assert_eq!(tab.get_active_pane().unwrap().pane_id(), 3);
+
+ let horz_size = tab.compute_split_size(0, SplitRequest::default()).unwrap();
+ tab.split_and_insert(
+ 0,
+ SplitRequest::default(),
+ FakePane::new(2, horz_size.second),
+ )
+ .unwrap();
+ tab.set_zoomed(true);
+ assert!(tab.swap_panes(2, 3).is_err());
+ }
+
/// A split that fails after the source pane was detached (MovePane
/// style) must be able to re-home the pane instead of orphaning it.
#[test]
diff --git a/mux/src/tmux.rs b/mux/src/tmux.rs
index 863817b8..951c8fb1 100644
--- a/mux/src/tmux.rs
+++ b/mux/src/tmux.rs
@@ -413,6 +413,15 @@ impl Domain for TmuxDomain {
anyhow::bail!("moving an existing pane is not supported by TmuxDomain")
}
+ async fn swap_panes(
+ &self,
+ _tab_id: TabId,
+ _pane_id: PaneId,
+ _other_pane_id: PaneId,
+ ) -> anyhow::Result<()> {
+ anyhow::bail!("swapping panes is not supported by TmuxDomain")
+ }
+
async fn spawn_pane(
&self,
_size: TerminalSize,
diff --git a/wezterm-client/src/client.rs b/wezterm-client/src/client.rs
index 78ee0341..017bae84 100644
--- a/wezterm-client/src/client.rs
+++ b/wezterm-client/src/client.rs
@@ -2905,6 +2905,7 @@ impl Client {
rpc!(spawn_pane_in_stack, SpawnPaneInStack, SpawnResponse);
rpc!(activate_pane_in_stack, ActivatePaneInStack, UnitResponse);
rpc!(move_pane_to_stack, MovePaneToStack, UnitResponse);
+ rpc!(swap_panes, SwapPanes, UnitResponse);
rpc!(
move_pane_to_new_tab,
MovePaneToNewTab,
diff --git a/wezterm-client/src/domain.rs b/wezterm-client/src/domain.rs
index cedbf662..9f9b741f 100644
--- a/wezterm-client/src/domain.rs
+++ b/wezterm-client/src/domain.rs
@@ -5718,6 +5718,87 @@ impl Domain for ClientDomain {
Ok(source_pane)
}
+ async fn swap_panes(
+ &self,
+ tab_id: TabId,
+ pane_id: PaneId,
+ other_pane_id: PaneId,
+ ) -> anyhow::Result<()> {
+ let inner = self
+ .inner()
+ .ok_or_else(|| anyhow!("domain is not attached"))?;
+ let _mutation = inner.begin_structure_mutation();
+ let mux = Mux::get();
+
+ let tab = mux
+ .get_tab(tab_id)
+ .ok_or_else(|| anyhow!("tab_id {tab_id} is invalid"))?;
+ if tab.pane_index_for_pane(pane_id).is_none()
+ || tab.pane_index_for_pane(other_pane_id).is_none()
+ {
+ bail!("remote swap requires panes {pane_id} and {other_pane_id} to be in tab {tab_id}");
+ }
+
+ let pane = mux
+ .get_pane(pane_id)
+ .ok_or_else(|| anyhow!("pane_id {pane_id} is invalid"))?;
+ let other = mux
+ .get_pane(other_pane_id)
+ .ok_or_else(|| anyhow!("pane_id {other_pane_id} is invalid"))?;
+ let client_pane = pane
+ .downcast_ref::<ClientPane>()
+ .ok_or_else(|| anyhow!("pane_id {pane_id} is not a ClientPane"))?;
+ let other_client_pane = other
+ .downcast_ref::<ClientPane>()
+ .ok_or_else(|| anyhow!("pane_id {other_pane_id} is not a ClientPane"))?;
+ if !client_pane.belongs_to_client(&inner) || !other_client_pane.belongs_to_client(&inner) {
+ bail!("remote swap panes belong to different client connections");
+ }
+
+ let remote_pane_id = remote_move_pane_id(
+ other_pane_id,
+ other_client_pane.remote_pane_id(),
+ pane_id,
+ client_pane.remote_pane_id(),
+ inner.local_domain_id,
+ pane.domain_id(),
+ )?;
+ let remote_other_pane_id = other_client_pane.remote_pane_id();
+ let remote_tab_id = other_client_pane.remote_tab_id();
+ if client_pane.remote_tab_id() != remote_tab_id {
+ inner.defer_resync();
+ bail!(
+ "remote swap panes do not belong to the same remote tab: \
+ {}, {remote_tab_id}",
+ client_pane.remote_tab_id()
+ );
+ }
+
+ if !inner.prepare_remote_tab_input(remote_tab_id).await? {
+ bail!("terminal is being operated on another device");
+ }
+
+ // As for a stack move: in place here before the round-trip, so
+ // dropping the drag preview does not show the old layout for one
+ // RTT. A failed RPC schedules a resync, which restores the tree.
+ tab.swap_panes(pane_id, other_pane_id)?;
+
+ if let Err(err) = inner
+ .client
+ .swap_panes(codec::SwapPanes {
+ pane_id: remote_pane_id,
+ other_pane_id: remote_other_pane_id,
+ })
+ .await
+ {
+ inner.defer_resync();
+ return Err(err).context("swapping remote panes");
+ }
+
+ inner.defer_resync();
+ Ok(())
+ }
+
/// Forward the request to the remote; we need to translate the local ids
/// to those that match the remote for the request, resync the changed
/// structure, and then translate the results back to local
diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs
index cce0892e..5b7a248f 100644
--- a/wezterm-gui/src/termwindow/mod.rs
+++ b/wezterm-gui/src/termwindow/mod.rs
@@ -1449,6 +1449,10 @@ pub(crate) enum PendingRemoteConfirm {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum PaneDropZone {
+ /// The target's own level-2 tab bar: join its stack, like a browser
+ /// tab dropped onto another window's tab strip.
+ Bar,
+ /// The middle of the target: trade places with it.
Center,
Left,
Right,
@@ -1459,6 +1463,7 @@ pub(crate) enum PaneDropZone {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum PaneDropKind {
MoveIntoStack,
+ Swap,
Split,
}
@@ -1534,8 +1539,8 @@ pub(crate) struct PaneTabDragState {
pub target: Option<PaneTabDropTarget>,
}
-/// Split the target pane into a hit grid: an inner box maps to "move into
-/// this pane's stack", the border bands map to the nearest edge.
+/// Split the target pane into a hit grid: an inner box maps to "trade
+/// places with this pane", the border bands map to the nearest edge.
pub(crate) fn pane_drop_zone(fx: f32, fy: f32) -> PaneDropZone {
if fx > 0.28 && fx < 0.72 && fy > 0.28 && fy < 0.72 {
return PaneDropZone::Center;
@@ -1558,7 +1563,9 @@ pub(crate) fn zone_split_request(zone: PaneDropZone) -> SplitRequest {
direction: match zone {
PaneDropZone::Left | PaneDropZone::Right => SplitDirection::Horizontal,
PaneDropZone::Top | PaneDropZone::Bottom => SplitDirection::Vertical,
- PaneDropZone::Center => unreachable!("center zone does not split"),
+ PaneDropZone::Bar | PaneDropZone::Center => {
+ unreachable!("joining or swapping does not split")
+ }
},
target_is_second: matches!(zone, PaneDropZone::Right | PaneDropZone::Bottom),
top_level: false,
@@ -1567,21 +1574,28 @@ pub(crate) fn zone_split_request(zone: PaneDropZone) -> SplitRequest {
}
/// Decide what dropping onto `zone` would do, or None when the drop is a
-/// no-op (moving a pane onto the stack it is already in, or splitting a
-/// single-pane stack against itself).
+/// no-op (moving a pane onto the stack it is already in or swapping it with
+/// that stack, or splitting a single-pane stack against itself).
pub(crate) fn pane_drop_action(
zone: PaneDropZone,
src_in_target_stack: bool,
target_stack_len: usize,
) -> Option<PaneDropKind> {
match zone {
- PaneDropZone::Center => {
+ PaneDropZone::Bar => {
if src_in_target_stack {
None
} else {
Some(PaneDropKind::MoveIntoStack)
}
}
+ PaneDropZone::Center => {
+ if src_in_target_stack {
+ None
+ } else {
+ Some(PaneDropKind::Swap)
+ }
+ }
_ => {
if src_in_target_stack && target_stack_len <= 1 {
None
@@ -1830,10 +1844,16 @@ mod pane_drop_tests {
#[test]
fn drop_action_table() {
// Moving into a stack the pane already lives in is a no-op
+ assert_eq!(pane_drop_action(PaneDropZone::Bar, true, 2), None);
+ assert_eq!(
+ pane_drop_action(PaneDropZone::Bar, false, 1),
+ Some(PaneDropKind::MoveIntoStack)
+ );
+ // So is trading places with it; any other pane trades places
assert_eq!(pane_drop_action(PaneDropZone::Center, true, 2), None);
assert_eq!(
pane_drop_action(PaneDropZone::Center, false, 1),
- Some(PaneDropKind::MoveIntoStack)
+ Some(PaneDropKind::Swap)
);
// Splitting a single-pane stack against itself is a no-op
assert_eq!(pane_drop_action(PaneDropZone::Left, true, 1), None);
diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs
index 25d40cb8..6838dfe1 100644
--- a/wezterm-gui/src/termwindow/mouseevent.rs
+++ b/wezterm-gui/src/termwindow/mouseevent.rs
@@ -3529,8 +3529,9 @@ impl super::TermWindow {
}
/// Hit-test the pointer against the rendered panes and record which
- /// drop (move-into-stack or directional split) releasing here would
- /// perform, along with the preview rectangle to paint.
+ /// drop (move into its stack, swap with it, or directional split)
+ /// releasing here would perform, along with the preview rectangle to
+ /// paint.
fn update_pane_tab_drag_target(&mut self, event: &MouseEvent) {
let Some(src_pane_id) = self.pane_tab_drag.as_ref().map(|state| state.pane_id) else {
return;
@@ -3565,7 +3566,7 @@ impl super::TermWindow {
// another window's tab strip.
let nav_bar_h = self.pane_nav_bar_height_for_pane(&pos) as f32;
let zone = if y < rect.min_y() + nav_bar_h {
- PaneDropZone::Center
+ PaneDropZone::Bar
} else {
pane_drop_zone(fx, fy)
};
@@ -3576,12 +3577,14 @@ impl super::TermWindow {
let preview = match zone {
// Joining the stack highlights the target's level-2 tab
// bar, like dropping a browser tab onto a tab strip.
- PaneDropZone::Center => euclid::rect(
+ PaneDropZone::Bar => euclid::rect(
rect.origin.x,
rect.origin.y,
rect.size.width,
nav_bar_h.max(1.0),
),
+ // Trading places takes the whole of it.
+ PaneDropZone::Center => rect,
PaneDropZone::Left => euclid::rect(
rect.origin.x,
rect.origin.y,
@@ -3684,6 +3687,36 @@ impl super::TermWindow {
})
.detach();
}
+ PaneDropKind::Swap => {
+ let target_pane_id = target.target_pane_id;
+ let dest_tab_id = tab.tab_id();
+ let workspace = self.current_mux_workspace();
+ let window = GuiWin::new(self);
+ promise::spawn::spawn(async move {
+ match Mux::get().swap_panes(src_pane_id, target_pane_id).await {
+ Ok(()) => {
+ window.window.notify(TermWindowNotif::Apply(Box::new(
+ move |term_window| {
+ if term_window.active_tab_is(dest_tab_id) {
+ term_window.sync_active_tab_geometry_now();
+ }
+ if term_window.current_mux_workspace() == workspace {
+ term_window.persist_workspace_layout_after_mutation(
+ "pane tab drop swap",
+ );
+ }
+ term_window.update_title();
+ if let Some(window) = term_window.window.as_ref() {
+ window.invalidate();
+ }
+ },
+ )));
+ }
+ Err(err) => log::error!("pane tab drop swap failed: {err:#}"),
+ }
+ })
+ .detach();
+ }
PaneDropKind::Split => {
let stack_tabs = tab.pane_stack_tabs(target.target_pane_id);
let src_in_target_stack =
diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rs
index 0669f7d1..8c1a6250 100644
--- a/wezterm-mux-server-impl/src/sessionhandler.rs
+++ b/wezterm-mux-server-impl/src/sessionhandler.rs
@@ -1028,6 +1028,7 @@ fn requires_existing_frontend_access(pdu: &Pdu) -> bool {
| Pdu::SpawnPaneInStack(_)
| Pdu::ActivatePaneInStack(_)
| Pdu::MovePaneToStack(_)
+ | Pdu::SwapPanes(_)
| Pdu::MovePaneToNewTab(_)
| Pdu::MoveTab(_)
| Pdu::AdjustPaneSize(_)
@@ -1157,6 +1158,7 @@ fn web_peer_may_send(pdu: &Pdu) -> bool {
| Pdu::SpawnPaneInStack(_)
| Pdu::ActivatePaneInStack(_)
| Pdu::MovePaneToStack(_)
+ | Pdu::SwapPanes(_)
| Pdu::GetThinkTermTree(_)
| Pdu::MutateThinkTermTree(_)
| Pdu::GetThinkTermSessionState(_)
@@ -2105,6 +2107,13 @@ impl SessionHandler {
.detach();
}
+ Pdu::SwapPanes(request) => {
+ spawn_into_main_thread(async move {
+ schedule_swap_panes(request, send_response);
+ })
+ .detach();
+ }
+
Pdu::GetThinkTermTree(_) => {
send_response(Ok(Pdu::ThinkTermTreeState(ThinkTermTreeState {
tree: crate::thinkterm_tree::snapshot(),
@@ -3189,6 +3198,22 @@ where
.detach();
}
+fn schedule_swap_panes<SND>(request: SwapPanes, send_response: SND)
+where
+ SND: Fn(anyhow::Result<Pdu>) + 'static,
+{
+ promise::spawn::spawn(async move {
+ // No identity, as for `schedule_move_pane_to_stack`.
+ send_response(
+ Mux::get()
+ .swap_panes(request.pane_id, request.other_pane_id)
+ .await
+ .map(|()| Pdu::UnitResponse(UnitResponse {})),
+ );
+ })
+ .detach();
+}
+
async fn spawn_pane_in_stack(request: SpawnPaneInStack) -> anyhow::Result<Pdu> {
let mux = Mux::get();
// No identity: `spawn_pane_in_stack` reads none, and a guard held across