From 11dc59b6130357bdb2f83377ea8c5d77b53fc6f2 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 4 Apr 2023 17:22:21 -0700 Subject: [PATCH] mux: explicitly update tab/pane active status on focus change Previously, we'd record the focused pane only in the per-client view. That status didn't propagate through the model fully, leading to inconsistencies when using activate-pane-direction. This commit does the full model update to force that through. I may regret this later: the focus state was intended to be a per-client attribute and this effectively prevents that from ever being useful. Maybe the per-client state should just be removed? Will ponder that later. refs: https://github.com/wez/wezterm/issues/3448 refs: https://github.com/wez/wezterm/issues/3387 Also sneaking in here is not updating the last input time for a client unless the input was taken by a user. --- codec/src/lib.rs | 17 +++++++ docs/changelog.md | 1 + wezterm-mux-server-impl/src/sessionhandler.rs | 46 ++++++++++++++++--- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/codec/src/lib.rs b/codec/src/lib.rs index 68dc16daa..41490c712 100644 --- a/codec/src/lib.rs +++ b/codec/src/lib.rs @@ -492,6 +492,23 @@ pdu! { } impl Pdu { + /// Returns true if this type of Pdu represents action taken + /// directly by a user, rather than background traffic on + /// a live connection + pub fn is_user_input(&self) -> bool { + match self { + Self::WriteToPane(_) + | Self::SendKeyDown(_) + | Self::SendMouseEvent(_) + | Self::SendPaste(_) + | Self::Resize(_) + | Self::SetClipboard(_) + | Self::SetPaneZoomed(_) + | Self::SpawnV2(_) => true, + _ => false, + } + } + pub fn stream_decode(buffer: &mut Vec) -> anyhow::Result> { let mut cursor = Cursor::new(buffer.as_slice()); match Self::decode(&mut cursor) { diff --git a/docs/changelog.md b/docs/changelog.md index 2be7fc473..17bcbf0d6 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -91,6 +91,7 @@ As features stabilize some brief notes about them will accumulate here. produce a warning about the remote home directory not being readable to the local machine or vice versa. #3442 * `wezterm.config_builder` now works with `table.insert(config.keys, {..})` +* mux: `wezterm cli activate-pane-direction` inconsistent behavior with remote mux. #3387 #3448 ### 20230326-111934-3666303c diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rs index 96c14754d..5e14c9ee0 100644 --- a/wezterm-mux-server-impl/src/sessionhandler.rs +++ b/wezterm-mux-server-impl/src/sessionhandler.rs @@ -247,7 +247,9 @@ impl SessionHandler { let serial = decoded.serial; if let Some(client_id) = &self.client_id { - Mux::get().client_had_input(client_id); + if decoded.pdu.is_user_input() { + Mux::get().client_had_input(client_id); + } } let send_response = move |result: anyhow::Result| { @@ -303,13 +305,45 @@ impl SessionHandler { Pdu::SetFocusedPane(SetFocusedPane { pane_id }) => { let client_id = self.client_id.clone(); spawn_into_main_thread(async move { - 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)); + catch( + move || { + let mux = Mux::get(); + let _identity = mux.with_identity(client_id); + log::info!("doing SetFocusedPane {pane_id}"); + + let pane = mux + .get_pane(pane_id) + .ok_or_else(|| anyhow::anyhow!("pane {pane_id} not found"))?; + + let (_domain_id, window_id, tab_id) = mux + .resolve_pane_id(pane_id) + .ok_or_else(|| anyhow::anyhow!("pane {pane_id} not found"))?; + { + let mut window = + mux.get_window_mut(window_id).ok_or_else(|| { + anyhow::anyhow!("window {window_id} not found") + })?; + let tab_idx = window.idx_by_id(tab_id).ok_or_else(|| { + anyhow::anyhow!( + "tab {tab_id} isn't really in window {window_id}!?" + ) + })?; + window.save_and_then_set_active(tab_idx); + } + let tab = mux + .get_tab(tab_id) + .ok_or_else(|| anyhow::anyhow!("tab {tab_id} not found"))?; + tab.set_active_pane(&pane); + + mux.record_focus_for_current_identity(pane_id); + mux.notify(mux::MuxNotification::PaneFocused(pane_id)); + + Ok(Pdu::UnitResponse(UnitResponse {})) + }, + send_response, + ) }) .detach(); - send_response(Ok(Pdu::UnitResponse(UnitResponse {}))) } Pdu::GetClientList(GetClientList) => { spawn_into_main_thread(async move {