From f71bce1727bbf1c384ca63a650a2aadec5afe841 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 24 Mar 2023 13:13:31 -0700 Subject: [PATCH] mux: propagate pane focus change events to clients This commit causes the mux to generate a PaneFocused notification when the active pane is changed. The mux server will forward that as a unilateral PDU to connected clients. The clientpane implementation will handle that by applying the same state to the local mux. refs: #2863 --- codec/src/lib.rs | 9 ++++- docs/changelog.md | 2 + mux/src/lib.rs | 1 + mux/src/tab.rs | 3 ++ wezterm-client/src/pane/clientpane.rs | 39 +++++++++++++++++++ wezterm-gui/src/frontend.rs | 1 + wezterm-gui/src/termwindow/mod.rs | 4 ++ wezterm-mux-server-impl/src/dispatch.rs | 6 +++ wezterm-mux-server-impl/src/sessionhandler.rs | 2 + 9 files changed, 66 insertions(+), 1 deletion(-) diff --git a/codec/src/lib.rs b/codec/src/lib.rs index 6e5d072d9..ae4f9ffce 100644 --- a/codec/src/lib.rs +++ b/codec/src/lib.rs @@ -417,7 +417,7 @@ macro_rules! pdu { /// The overall version of the codec. /// This must be bumped when backwards incompatible changes /// are made to the types and protocol. -pub const CODEC_VERSION: usize = 33; +pub const CODEC_VERSION: usize = 34; // Defines the Pdu enum. // Each struct has an explicit identifying number. @@ -468,6 +468,7 @@ pdu! { ActivatePaneDirection: 50, GetPaneRenderableDimensions: 51, GetPaneRenderableDimensionsResponse: 52, + PaneFocused: 53, } impl Pdu { @@ -543,6 +544,7 @@ impl Pdu { | Pdu::SetPalette(SetPalette { pane_id, .. }) | Pdu::NotifyAlert(NotifyAlert { pane_id, .. }) | Pdu::SetClipboard(SetClipboard { pane_id, .. }) + | Pdu::PaneFocused(PaneFocused { pane_id }) | Pdu::PaneRemoved(PaneRemoved { pane_id }) => Some(*pane_id), _ => None, } @@ -733,6 +735,11 @@ pub struct NotifyAlert { pub alert: Alert, } +#[derive(Deserialize, Serialize, PartialEq, Debug)] +pub struct PaneFocused { + pub pane_id: PaneId, +} + #[derive(Deserialize, Serialize, PartialEq, Debug)] pub struct WindowWorkspaceChanged { pub window_id: WindowId, diff --git a/docs/changelog.md b/docs/changelog.md index 2df466850..f2f857ddb 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -53,6 +53,8 @@ As features stabilize some brief notes about them will accumulate here. * macos: command line parameters beyond the first were treated as terminal command scripts and opened spurious windows. #3340 * imgcat broken with multiplexer protocol #3343 +* `wezterm cli activate-pane-direction` would not cause the focus to change + in the GUI when explicitly using multiplexing domains. #2863 ### 20230320-124340-559cb7b0 diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 11f31d749..1c3184ffe 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -77,6 +77,7 @@ pub enum MuxNotification { tab_id: TabId, window_id: WindowId, }, + PaneFocused(PaneId), } static SUB_ID: AtomicUsize = AtomicUsize::new(0); diff --git a/mux/src/tab.rs b/mux/src/tab.rs index 8c0b056ef..14944d832 100644 --- a/mux/src/tab.rs +++ b/mux/src/tab.rs @@ -1715,14 +1715,17 @@ impl TabInner { } fn advise_focus_change(&mut self, prior: Option>) { + let mux = Mux::get(); let current = self.get_active_pane(); match (prior, current) { (Some(prior), Some(current)) if prior.pane_id() != current.pane_id() => { prior.focus_changed(false); current.focus_changed(true); + mux.notify(MuxNotification::PaneFocused(current.pane_id())); } (None, Some(current)) => { current.focus_changed(true); + mux.notify(MuxNotification::PaneFocused(current.pane_id())); } (Some(prior), None) => { prior.focus_changed(false); diff --git a/wezterm-client/src/pane/clientpane.rs b/wezterm-client/src/pane/clientpane.rs index 7dbfe4c35..6ea82ba07 100644 --- a/wezterm-client/src/pane/clientpane.rs +++ b/wezterm-client/src/pane/clientpane.rs @@ -169,6 +169,45 @@ impl ClientPane { self.client.expire_stale_mappings(); } + Pdu::PaneFocused(PaneFocused { pane_id }) => { + // We get here whenever the pane focus is changed on the + // server. That might be due to the user here in the GUI + // doing things, or it may be due to a "remote" + // `wezterm cli activate-pane-direction` or similar call + // from some other actor. + // The latter case is the important one: it is desirable + // for the focus change to be reflected locally after it + // has been changed on the server, so we work to apply + // it here. + log::trace!("advised of remote pane focus: {pane_id}"); + + let mux = Mux::get(); + let (_domain, window_id, tab_id) = mux + .resolve_pane_id(self.local_pane_id) + .ok_or_else(|| anyhow::anyhow!("can't find myself in the mux"))?; + + // Focus/activate the containing tab within its window + { + let mut win = mux + .get_window_mut(window_id) + .ok_or_else(|| anyhow::anyhow!("window_id {window_id} not found"))?; + let tab_idx = win + .idx_by_id(tab_id) + .ok_or_else(|| anyhow::anyhow!("tab {tab_id} not in {window_id}"))?; + win.save_and_then_set_active(tab_idx); + } + + // Focus/activate the pane locally + let tab = mux + .get_tab(tab_id) + .ok_or_else(|| anyhow::anyhow!("tab {tab_id} not found"))?; + + let pane = mux.get_pane(self.local_pane_id).ok_or_else(|| { + anyhow::anyhow!("pane {} not found. That's me?", self.local_pane_id) + })?; + + tab.set_active_pane(&pane); + } _ => bail!("unhandled unilateral pdu: {:?}", pdu), }; Ok(()) diff --git a/wezterm-gui/src/frontend.rs b/wezterm-gui/src/frontend.rs index 9a58e5d92..9e197166c 100644 --- a/wezterm-gui/src/frontend.rs +++ b/wezterm-gui/src/frontend.rs @@ -64,6 +64,7 @@ impl GuiFrontEnd { }) .detach(); } + MuxNotification::PaneFocused(_) => {} MuxNotification::TabAddedToWindow { .. } => {} MuxNotification::PaneRemoved(_) => {} MuxNotification::WindowInvalidated(_) => {} diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 7c5c33dcd..6a5ff2dad 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -1161,6 +1161,9 @@ impl TermWindow { MuxNotification::SaveToDownloads { .. } => { // Handled by frontend } + MuxNotification::PaneFocused(_) => { + // Handled by frontend + } MuxNotification::PaneAdded(_) | MuxNotification::PaneRemoved(_) | MuxNotification::WindowWorkspaceChanged(_) @@ -1345,6 +1348,7 @@ impl TermWindow { } | MuxNotification::AssignClipboard { .. } | MuxNotification::SaveToDownloads { .. } + | MuxNotification::PaneFocused(_) | MuxNotification::PaneRemoved(_) | MuxNotification::WindowCreated(_) | MuxNotification::ActiveWorkspaceChanged(_) diff --git a/wezterm-mux-server-impl/src/dispatch.rs b/wezterm-mux-server-impl/src/dispatch.rs index 01749cf0a..ad4a5fe35 100644 --- a/wezterm-mux-server-impl/src/dispatch.rs +++ b/wezterm-mux-server-impl/src/dispatch.rs @@ -159,6 +159,12 @@ where stream.flush().await.context("flushing PDU to client")?; } } + Ok(Item::Notif(MuxNotification::PaneFocused(pane_id))) => { + Pdu::PaneFocused(codec::PaneFocused { pane_id }) + .encode_async(&mut stream, 0) + .await?; + stream.flush().await.context("flushing PDU to client")?; + } Ok(Item::Notif(MuxNotification::ActiveWorkspaceChanged(_))) => {} Ok(Item::Notif(MuxNotification::Empty)) => {} Err(err) => { diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rs index 20228e103..d00ddc5ff 100644 --- a/wezterm-mux-server-impl/src/sessionhandler.rs +++ b/wezterm-mux-server-impl/src/sessionhandler.rs @@ -301,6 +301,7 @@ impl SessionHandler { let mux = Mux::get(); let _identity = mux.with_identity(client_id); mux.record_focus_for_current_identity(pane_id); + mux.notify(mux::MuxNotification::PaneFocused(pane_id)); }) .detach(); send_response(Ok(Pdu::UnitResponse(UnitResponse {}))) @@ -746,6 +747,7 @@ impl SessionHandler { | Pdu::GetTlsCredsResponse { .. } | Pdu::GetClientListResponse { .. } | Pdu::PaneRemoved { .. } + | Pdu::PaneFocused { .. } | Pdu::GetImageCellResponse { .. } | Pdu::MovePaneToNewTabResponse { .. } | Pdu::GetPaneRenderableDimensionsResponse { .. }