track focused pane in the client info

wezterm cli spawn, and wezterm cli split-pane can use this information
to pick a default for the pane id when invoked from outside of wezterm.

refs: https://github.com/wez/wezterm/issues/1531
This commit is contained in:
Wez Furlong committed 2022-03-18 19:24:54 -07:00
1 parent 0b7cb6550b
commit 4007ccbcd2
10 files changed
+105 -35

No files matched your search

+7 -1
View File
@@ -405,7 +405,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 = 18;
pub const CODEC_VERSION: usize = 19;
// Defines the Pdu enum.
// Each struct has an explicit identifying number.
@@ -448,6 +448,7 @@ pdu! {
GetClientListResponse: 42,
SetWindowWorkspace: 43,
WindowWorkspaceChanged: 44,
SetFocusedPane: 45,
}
impl Pdu {
@@ -708,6 +709,11 @@ pub struct SetClientId {
pub client_id: ClientId,
}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct SetFocusedPane {
pub pane_id: PaneId,
}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct GetClientList;
+8
View File
@@ -1,3 +1,4 @@
use crate::PaneId;
use chrono::serde::ts_seconds;
use chrono::{DateTime, Utc};
use serde::*;
@@ -47,6 +48,8 @@ pub struct ClientInfo {
/// The last time we received input from this client
#[serde(with = "ts_seconds")]
pub last_input: DateTime<Utc>,
/// The currently-focused pane
pub focused_pane_id: Option<PaneId>,
}
impl ClientInfo {
@@ -56,10 +59,15 @@ impl ClientInfo {
connected_at: Utc::now(),
active_workspace: None,
last_input: Utc::now(),
focused_pane_id: None,
}
}
pub fn update_last_input(&mut self) {
self.last_input = Utc::now();
}
pub fn update_focused_pane(&mut self, pane_id: PaneId) {
self.focused_pane_id.replace(pane_id);
}
}
+12
View File
@@ -359,6 +359,18 @@ impl Mux {
}
}
pub fn record_focus_for_current_identity(&self, pane_id: PaneId) {
if let Some(ident) = self.identity.borrow().as_ref() {
self.record_focus_for_client(ident, pane_id);
}
}
pub fn record_focus_for_client(&self, client_id: &ClientId, pane_id: PaneId) {
if let Some(info) = self.clients.borrow_mut().get_mut(client_id) {
info.update_focused_pane(pane_id);
}
}
pub fn register_client(&self, client_id: Arc<ClientId>) {
self.clients
.borrow_mut()
+5
View File
@@ -343,6 +343,11 @@ pub trait Pane: Downcast {
/// Called to advise on whether this tab has focus
fn focus_changed(&self, _focused: bool) {}
/// Called to advise remote mux that this is the active tab
/// for the current identity
fn advise_focus(&self) {}
fn has_unseen_output(&self) -> bool {
false
}
+1
View File
@@ -1173,4 +1173,5 @@ impl Client {
rpc!(set_client_id, SetClientId, UnitResponse);
rpc!(list_clients, GetClientList, GetClientListResponse);
rpc!(set_window_workspace, SetWindowWorkspace, UnitResponse);
rpc!(set_focused_pane_id, SetFocusedPane, UnitResponse);
}
+2
View File
@@ -26,6 +26,7 @@ pub struct ClientInner {
remote_to_local_window: Mutex<HashMap<WindowId, WindowId>>,
remote_to_local_tab: Mutex<HashMap<TabId, TabId>>,
remote_to_local_pane: Mutex<HashMap<PaneId, PaneId>>,
pub focused_remote_pane_id: Mutex<Option<PaneId>>,
}
impl ClientInner {
@@ -179,6 +180,7 @@ impl ClientInner {
remote_to_local_window: Mutex::new(HashMap::new()),
remote_to_local_tab: Mutex::new(HashMap::new()),
remote_to_local_pane: Mutex::new(HashMap::new()),
focused_remote_pane_id: Mutex::new(None),
}
}
}
+18
View File
@@ -415,6 +415,24 @@ impl Pane for ClientPane {
self.renderable.borrow().inner.borrow().working_dir.clone()
}
fn advise_focus(&self) {
let mut focused_pane = self.client.focused_remote_pane_id.lock().unwrap();
if *focused_pane != Some(self.remote_pane_id) {
focused_pane.replace(self.remote_pane_id);
let client = Arc::clone(&self.client);
let remote_pane_id = self.remote_pane_id;
promise::spawn::spawn(async move {
client
.client
.set_focused_pane_id(SetFocusedPane {
pane_id: remote_pane_id,
})
.await
})
.detach();
}
}
fn can_close_without_prompting(&self, reason: CloseReason) -> bool {
match reason {
CloseReason::Window => true,
+4
View File
@@ -1538,6 +1538,10 @@ impl super::TermWindow {
for pos in panes {
if pos.is_active {
self.update_text_cursor(&pos.pane);
pos.pane.advise_focus();
mux::Mux::get()
.expect("called on mux thread")
.record_focus_for_current_identity(pos.pane.pane_id());
}
self.paint_pane_opengl(&pos, num_panes)?;
}
@@ -303,6 +303,16 @@ impl SessionHandler {
.detach();
send_response(Ok(Pdu::UnitResponse(UnitResponse {})))
}
Pdu::SetFocusedPane(SetFocusedPane { pane_id }) => {
let client_id = self.client_id.clone();
spawn_into_main_thread(async move {
let mux = Mux::get().unwrap();
let _identity = mux.with_identity(client_id);
mux.record_focus_for_current_identity(pane_id);
})
.detach();
send_response(Ok(Pdu::UnitResponse(UnitResponse {})))
}
Pdu::GetClientList(GetClientList) => {
spawn_into_main_thread(async move {
catch(
+38 -34
View File
@@ -409,6 +409,33 @@ fn delegate_to_gui(saver: UmaskSaver) -> anyhow::Result<()> {
}
}
async fn resolve_pane_id(client: &Client, pane_id: Option<PaneId>) -> anyhow::Result<PaneId> {
let pane_id: PaneId = match pane_id {
Some(p) => p,
None => {
if let Ok(pane) = std::env::var("WEZTERM_PANE") {
pane.parse()?
} else {
let mut clients = client.list_clients(codec::GetClientList).await?.clients;
clients.retain(|client| client.focused_pane_id.is_some());
clients.sort_by(|a, b| b.last_input.cmp(&a.last_input));
if clients.is_empty() {
anyhow::bail!(
"--pane-id was not specified and $WEZTERM_PANE
is not set in the environment, and I couldn't
determine which pane was currently focused"
);
}
clients[0]
.focused_pane_id
.expect("to have filtered out above")
}
}
};
Ok(pane_id)
}
async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow::Result<()> {
let mut ui = mux::connui::ConnectionUI::new_headless();
let initial = true;
@@ -450,6 +477,10 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow:
name: "WORKSPACE".to_string(),
alignment: Alignment::Left,
},
Column {
name: "FOCUS".to_string(),
alignment: Alignment::Right,
},
];
let mut data = vec![];
let clients = client.list_clients(codec::GetClientList).await?;
@@ -473,6 +504,9 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow:
duration_string(connected),
duration_string(idle),
info.active_workspace.as_deref().unwrap_or("").to_string(),
info.focused_pane_id
.map(|id| id.to_string())
.unwrap_or_else(String::new),
]);
}
@@ -547,17 +581,7 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow:
prog,
horizontal,
} => {
let pane_id: PaneId = match pane_id {
Some(p) => p,
None => std::env::var("WEZTERM_PANE")
.map_err(|_| {
anyhow!(
"--pane-id was not specified and $WEZTERM_PANE
is not set in the environment"
)
})?
.parse()?,
};
let pane_id = resolve_pane_id(&client, pane_id).await?;
let spawned = client
.split_pane(codec::SplitPane {
@@ -582,17 +606,8 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow:
println!("{}", spawned.pane_id);
}
CliSubCommand::SendText { pane_id, text } => {
let pane_id: PaneId = match pane_id {
Some(p) => p,
None => std::env::var("WEZTERM_PANE")
.map_err(|_| {
anyhow!(
"--pane-id was not specified and $WEZTERM_PANE \
is not set in the environment."
)
})?
.parse()?,
};
let pane_id = resolve_pane_id(&client, pane_id).await?;
let data = match text {
Some(text) => text,
None => {
@@ -623,18 +638,7 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow:
match window_id {
Some(w) => Some(w),
None => {
let pane_id: PaneId = match pane_id {
Some(p) => p,
None => std::env::var("WEZTERM_PANE")
.map_err(|_| {
anyhow!(
"--pane-id was not specified and $WEZTERM_PANE \
is not set in the environment. \
Consider using --new-window?"
)
})?
.parse()?,
};
let pane_id = resolve_pane_id(&client, pane_id).await?;
let panes = client.list_panes().await?;
let mut window_id = None;