diff options
| author | Aram Drevekenin <aram@poor.dev> | 2025-06-26 13:51:17 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-26 13:51:17 +0200 |
| commit | 11015c8fe4db1a8ffe77e0c569f0e026c53d9776 (patch) | |
| tree | 82c8513128e4be58cf6ac991b9872c9b44be5b46 /zellij-server/src | |
| parent | docs(changelog): terminal performance (diff) | |
| download | sl0ppij-11015c8fe4db1a8ffe77e0c569f0e026c53d9776.tar.gz sl0ppij-11015c8fe4db1a8ffe77e0c569f0e026c53d9776.tar.xz sl0ppij-11015c8fe4db1a8ffe77e0c569f0e026c53d9776.zip | |
feat(plugin-api): allow replacing pane with existing pane (#4246)
* work
* make the api work
* some cleanups
* close pane
* style(fmt): rustfmt
Diffstat (limited to 'zellij-server/src')
| -rw-r--r-- | zellij-server/src/plugins/zellij_exports.rs | 22 | ||||
| -rw-r--r-- | zellij-server/src/screen.rs | 51 | ||||
| -rw-r--r-- | zellij-server/src/tab/mod.rs | 24 |
3 files changed, 97 insertions, 0 deletions
diff --git a/zellij-server/src/plugins/zellij_exports.rs b/zellij-server/src/plugins/zellij_exports.rs index b928b224..7b682f6d 100644 --- a/zellij-server/src/plugins/zellij_exports.rs +++ b/zellij-server/src/plugins/zellij_exports.rs @@ -495,6 +495,14 @@ fn host_run_plugin_command(mut caller: Caller<'_, PluginEnv>) { PluginCommand::ClearKeyPressesIntercepts => { clear_key_presses_intercepts(&mut env) }, + PluginCommand::ReplacePaneWithExistingPane( + pane_id_to_replace, + existing_pane_id, + ) => replace_pane_with_existing_pane( + &mut env, + pane_id_to_replace.into(), + existing_pane_id.into(), + ), }, (PermissionStatus::Denied, permission) => { log::error!( @@ -2455,6 +2463,19 @@ fn clear_key_presses_intercepts(env: &mut PluginEnv) { .send_to_screen(ScreenInstruction::ClearKeyPressesIntercepts(env.client_id)); } +fn replace_pane_with_existing_pane( + env: &mut PluginEnv, + pane_to_replace: PaneId, + existing_pane: PaneId, +) { + let _ = env + .senders + .send_to_screen(ScreenInstruction::ReplacePaneWithExistingPane( + pane_to_replace, + existing_pane, + )); +} + // Custom panic handler for plugins. // // This is called when a panic occurs in a plugin. Since most panics will likely originate in the @@ -2621,6 +2642,7 @@ fn check_command_permission( | PluginCommand::CloseMultiplePanes(..) | PluginCommand::FloatMultiplePanes(..) | PluginCommand::EmbedMultiplePanes(..) + | PluginCommand::ReplacePaneWithExistingPane(..) | PluginCommand::KillSessions(..) => PermissionType::ChangeApplicationState, PluginCommand::UnblockCliPipeInput(..) | PluginCommand::BlockCliPipeInput(..) diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs index d1b2139c..376c726e 100644 --- a/zellij-server/src/screen.rs +++ b/zellij-server/src/screen.rs @@ -422,6 +422,7 @@ pub enum ScreenInstruction { SetMouseSelectionSupport(PaneId, bool), InterceptKeyPresses(PluginId, ClientId), ClearKeyPressesIntercepts(ClientId), + ReplacePaneWithExistingPane(PaneId, PaneId), } impl From<&ScreenInstruction> for ScreenContext { @@ -654,6 +655,9 @@ impl From<&ScreenInstruction> for ScreenContext { ScreenInstruction::ClearKeyPressesIntercepts(..) => { ScreenContext::ClearKeyPressesIntercepts }, + ScreenInstruction::ReplacePaneWithExistingPane(..) => { + ScreenContext::ReplacePaneWithExistingPane + }, } } } @@ -2608,6 +2612,50 @@ impl Screen { } Ok(()) } + pub fn replace_pane_with_existing_pane( + &mut self, + pane_id_to_replace: PaneId, + pane_id_of_existing_pane: PaneId, + ) { + let Some(tab_index_of_pane_id_to_replace) = self + .tabs + .iter() + .find(|(_tab_index, tab)| tab.has_pane_with_pid(&pane_id_to_replace)) + .map(|(_tab_index, tab)| tab.position) + else { + log::error!("Could not find tab"); + return; + }; + let Some(tab_index_of_existing_pane) = self + .tabs + .iter() + .find(|(_tab_index, tab)| tab.has_pane_with_pid(&pane_id_of_existing_pane)) + .map(|(_tab_index, tab)| tab.position) + else { + log::error!("Could not find tab"); + return; + }; + let Some(extracted_pane_from_other_tab) = self + .tabs + .iter_mut() + .find(|(_, t)| t.position == tab_index_of_existing_pane) + .and_then(|(_, t)| t.extract_pane(pane_id_of_existing_pane, false)) + else { + log::error!("Failed to find pane"); + return; + }; + if let Some(tab) = self + .tabs + .iter_mut() + .find(|(_, t)| t.position == tab_index_of_pane_id_to_replace) + { + tab.1.close_pane_and_replace_with_other_pane( + pane_id_to_replace, + extracted_pane_from_other_tab, + ); + } + let _ = self.log_and_report_session_state(); + } pub fn reconfigure( &mut self, new_keybinds: Keybinds, @@ -5488,6 +5536,9 @@ pub(crate) fn screen_thread_main( ScreenInstruction::ClearKeyPressesIntercepts(client_id) => { keybind_intercepts.remove(&client_id); }, + ScreenInstruction::ReplacePaneWithExistingPane(old_pane_id, new_pane_id) => { + screen.replace_pane_with_existing_pane(old_pane_id, new_pane_id) + }, } } Ok(()) diff --git a/zellij-server/src/tab/mod.rs b/zellij-server/src/tab/mod.rs index 0e4ee7fe..683a3d4a 100644 --- a/zellij-server/src/tab/mod.rs +++ b/zellij-server/src/tab/mod.rs @@ -1617,6 +1617,30 @@ impl Tab { } Ok(()) } + pub fn close_pane_and_replace_with_other_pane( + &mut self, + pane_id_to_replace: PaneId, + pane_to_replace_with: Box<dyn Pane>, + ) { + let mut replaced_pane = if self.floating_panes.panes_contain(&pane_id_to_replace) { + self.floating_panes + .replace_pane(pane_id_to_replace, pane_to_replace_with) + .ok() + } else { + self.tiled_panes + .replace_pane(pane_id_to_replace, pane_to_replace_with) + }; + if let Some(replaced_pane) = replaced_pane.take() { + let pane_id = replaced_pane.pid(); + let _ = self.senders.send_to_pty(PtyInstruction::ClosePane(pane_id)); + let _ = self.senders.send_to_plugin(PluginInstruction::Update(vec![( + None, + None, + Event::PaneClosed(pane_id.into()), + )])); + drop(replaced_pane); + } + } pub fn horizontal_split( &mut self, pid: PaneId, |
