From 6d288696c83a2a8f0ff2c6683f5e557ae2adb994 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 15 Jan 2022 13:48:57 -0700 Subject: [PATCH] Add SwitchToWorkspace keyassignment action This action causes the active workspace for the gui to change. If the name is omitted a random name will be generated. If the workspace doesn't exist, it will be be created. The optional spawn parameter can be used to launch a specific program into the new workspace; if omitted, the default prog will be used. The gui only supports a single active workspace. Switching workspaces will repurpose existing gui windows and re-assign them to windows in the new workspace, adjusting their size to fit those windows, spawning new windows or closing unused windows as required. The gui now exits when there are no panes in the active workspace, rather than no panes at all. refs: #1531 --- codec/src/lib.rs | 3 +- config/src/keyassignment.rs | 5 + mux/src/lib.rs | 64 ++++++- mux/src/tab.rs | 10 +- mux/src/termwiztermtab.rs | 2 +- mux/src/window.rs | 10 +- wezterm-client/src/domain.rs | 11 +- wezterm-gui/src/frontend.rs | 161 ++++++++++++++++-- wezterm-gui/src/main.rs | 7 +- wezterm-gui/src/termwindow/mod.rs | 61 +++++++ wezterm-gui/src/termwindow/resize.rs | 8 +- wezterm-gui/src/termwindow/spawn.rs | 8 +- wezterm-mux-server-impl/src/dispatch.rs | 1 + wezterm-mux-server-impl/src/sessionhandler.rs | 1 + wezterm-mux-server/src/main.rs | 2 +- wezterm/src/main.rs | 7 + window/src/os/macos/window.rs | 2 +- window/src/os/wayland/window.rs | 2 +- window/src/os/windows/window.rs | 2 +- window/src/os/x11/window.rs | 2 +- window/src/os/x_and_wayland.rs | 2 +- 21 files changed, 328 insertions(+), 43 deletions(-) diff --git a/codec/src/lib.rs b/codec/src/lib.rs index 6b5f07ae4..085aea170 100644 --- a/codec/src/lib.rs +++ b/codec/src/lib.rs @@ -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 = 16; +pub const CODEC_VERSION: usize = 17; // Defines the Pdu enum. // Each struct has an explicit identifying number. @@ -592,6 +592,7 @@ pub struct SpawnV2 { pub command: Option, pub command_dir: Option, pub size: PtySize, + pub workspace: String, } #[derive(Deserialize, Serialize, PartialEq, Debug)] diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index d0d21f296..540ff0149 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -264,6 +264,11 @@ pub enum KeyAssignment { QuickSelectArgs(QuickSelectArguments), Multiple(Vec), + + SwitchToWorkspace { + name: Option, + spawn: Option, + }, } impl_lua_conversion!(KeyAssignment); diff --git a/mux/src/lib.rs b/mux/src/lib.rs index b74896f73..339526865 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -53,6 +53,7 @@ pub enum MuxNotification { WindowRemoved(WindowId), WindowInvalidated(WindowId), WindowWorkspaceChanged(WindowId), + ActiveWorkspaceChanged(Arc), Alert { pane_id: PaneId, alert: wezterm_term::Alert, @@ -73,6 +74,7 @@ pub struct Mux { banner: RefCell>, clients: RefCell>, identity: RefCell>>, + num_panes_by_workspace: RefCell>, } const BUFSIZE: usize = 1024 * 1024; @@ -328,9 +330,21 @@ impl Mux { banner: RefCell::new(None), clients: RefCell::new(HashMap::new()), identity: RefCell::new(None), + num_panes_by_workspace: RefCell::new(HashMap::new()), } } + fn recompute_pane_count(&self) { + let mut count = HashMap::new(); + for window in self.windows.borrow().values() { + let workspace = window.get_workspace(); + for tab in window.iter() { + *count.entry(workspace.to_string()).or_insert(0) += tab.count_panes(); + } + } + *self.num_panes_by_workspace.borrow_mut() = count; + } + pub fn client_had_input(&self, client_id: &ClientId) { if let Some(info) = self.clients.borrow_mut().get_mut(client_id) { info.update_last_input(); @@ -396,12 +410,22 @@ impl Mux { .unwrap_or_else(|| DEFAULT_WORKSPACE.to_string()) } + /// Returns the effective active workspace name for a given client + pub fn active_workspace_for_client(&self, ident: &Arc) -> String { + self.clients + .borrow() + .get(&ident) + .and_then(|info| info.active_workspace.clone()) + .unwrap_or_else(|| DEFAULT_WORKSPACE.to_string()) + } + /// Assigns the active workspace name for the current identity pub fn set_active_workspace(&self, workspace: &str) { if let Some(ident) = self.identity.borrow().clone() { let mut clients = self.clients.borrow_mut(); if let Some(info) = clients.get_mut(&ident) { info.active_workspace.replace(workspace.to_string()); + self.notify(MuxNotification::ActiveWorkspaceChanged(ident)); } } } @@ -420,6 +444,11 @@ impl Mux { std::mem::replace(&mut *self.identity.borrow_mut(), id) } + /// Returns the active identity + pub fn active_identity(&self) -> Option> { + self.identity.borrow().clone() + } + pub fn unregister_client(&self, client_id: &ClientId) { self.clients.borrow_mut().remove(client_id); } @@ -512,12 +541,14 @@ impl Mux { let banner = self.banner.borrow().clone(); thread::spawn(move || read_from_pane_pty(pane_id, banner, reader)); } + self.recompute_pane_count(); self.notify(MuxNotification::PaneAdded(pane_id)); Ok(()) } pub fn add_tab_no_panes(&self, tab: &Rc) { self.tabs.borrow_mut().insert(tab.tab_id(), Rc::clone(tab)); + self.recompute_pane_count(); } pub fn add_tab_and_active_pane(&self, tab: &Rc) -> Result<(), Error> { @@ -530,9 +561,10 @@ impl Mux { fn remove_pane_internal(&self, pane_id: PaneId) { log::debug!("removing pane {}", pane_id); - if let Some(pane) = self.panes.borrow_mut().remove(&pane_id) { + if let Some(pane) = self.panes.borrow_mut().remove(&pane_id).clone() { log::debug!("killing pane {}", pane_id); pane.kill(); + self.recompute_pane_count(); self.notify(MuxNotification::PaneRemoved(pane_id)); } } @@ -555,6 +587,7 @@ impl Mux { for pane_id in pane_ids { self.remove_pane_internal(pane_id); } + self.recompute_pane_count(); Some(tab) } @@ -572,6 +605,7 @@ impl Mux { } self.notify(MuxNotification::WindowRemoved(window_id)); } + self.recompute_pane_count(); } pub fn remove_pane(&self, pane_id: PaneId) { @@ -665,8 +699,8 @@ impl Mux { window.get_active().map(Rc::clone) } - pub fn new_empty_window(&self) -> MuxWindowBuilder { - let window = Window::new(); + pub fn new_empty_window(&self, workspace: Option) -> MuxWindowBuilder { + let window = Window::new(workspace); let window_id = window.window_id(); self.windows.borrow_mut().insert(window_id, window); MuxWindowBuilder { @@ -699,6 +733,20 @@ impl Mux { self.panes.borrow().is_empty() } + pub fn is_workspace_empty(&self, workspace: &str) -> bool { + *self + .num_panes_by_workspace + .borrow() + .get(workspace) + .unwrap_or(&0) + == 0 + } + + pub fn is_active_workspace_empty(&self) -> bool { + let workspace = self.active_workspace(); + self.is_workspace_empty(&workspace) + } + pub fn iter_panes(&self) -> Vec> { self.panes .borrow() @@ -708,7 +756,8 @@ impl Mux { } pub fn iter_windows_in_workspace(&self, workspace: &str) -> Vec { - self.windows + let mut windows: Vec = self + .windows .borrow() .iter() .filter_map(|(k, w)| { @@ -719,7 +768,9 @@ impl Mux { } }) .cloned() - .collect() + .collect(); + windows.sort(); + windows } pub fn iter_windows(&self) -> Vec { @@ -888,6 +939,7 @@ impl Mux { command_dir: Option, size: PtySize, current_pane_id: Option, + workspace_for_new_window: String, ) -> anyhow::Result<(Rc, Rc, WindowId)> { let domain = self .resolve_spawn_tab_domain(current_pane_id, &domain) @@ -913,7 +965,7 @@ impl Mux { (window_id, size) } else { term_config = None; - window_builder = self.new_empty_window(); + window_builder = self.new_empty_window(Some(workspace_for_new_window)); (*window_builder, size) }; diff --git a/mux/src/tab.rs b/mux/src/tab.rs index 8e347513b..1019f00f6 100644 --- a/mux/src/tab.rs +++ b/mux/src/tab.rs @@ -197,10 +197,10 @@ fn build_from_pane_tree( tree: bintree::Tree, active: &mut Option>, zoomed: &mut Option>, - make_pane: &F, + make_pane: &mut F, ) -> Tree where - F: Fn(PaneEntry) -> Rc, + F: FnMut(PaneEntry) -> Rc, { match tree { bintree::Tree::Empty => Tree::Empty, @@ -429,16 +429,16 @@ impl Tab { /// PaneEntry, or to create a new Pane from that entry. /// make_pane is expected to add the pane to the mux if it creates /// a new pane, otherwise the pane won't poll/update in the GUI. - pub fn sync_with_pane_tree(&self, size: PtySize, root: PaneNode, make_pane: F) + pub fn sync_with_pane_tree(&self, size: PtySize, root: PaneNode, mut make_pane: F) where - F: Fn(PaneEntry) -> Rc, + F: FnMut(PaneEntry) -> Rc, { let mut active = None; let mut zoomed = None; log::debug!("sync_with_pane_tree with size {:?}", size); - let t = build_from_pane_tree(root.into_tree(), &mut active, &mut zoomed, &make_pane); + let t = build_from_pane_tree(root.into_tree(), &mut active, &mut zoomed, &mut make_pane); let mut cursor = t.cursor(); *self.active.borrow_mut() = 0; diff --git a/mux/src/termwiztermtab.rs b/mux/src/termwiztermtab.rs index 77db09a2c..f46d012b8 100644 --- a/mux/src/termwiztermtab.rs +++ b/mux/src/termwiztermtab.rs @@ -481,7 +481,7 @@ pub async fn run< let domain: Arc = Arc::new(TermWizTerminalDomain::new()); mux.add_domain(&domain); - let window_id = mux.new_empty_window(); + let window_id = mux.new_empty_window(None); let pane = TermWizTerminalPane::new(domain.domain_id(), size, input_tx, render_rx); let pane: Rc = Rc::new(pane); diff --git a/mux/src/window.rs b/mux/src/window.rs index 2d3849983..4c0b9022d 100644 --- a/mux/src/window.rs +++ b/mux/src/window.rs @@ -17,16 +17,18 @@ pub struct Window { } impl Window { - pub fn new() -> Self { + pub fn new(workspace: Option) -> Self { Self { id: WIN_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed), tabs: vec![], active: 0, last_active: None, clipboard: None, - workspace: Mux::get() - .expect("Window::new to be called on mux thread") - .active_workspace(), + workspace: workspace.unwrap_or_else(|| { + Mux::get() + .expect("Window::new to be called on mux thread") + .active_workspace() + }), } } diff --git a/wezterm-client/src/domain.rs b/wezterm-client/src/domain.rs index 1ac9954fb..acd3c4f22 100644 --- a/wezterm-client/src/domain.rs +++ b/wezterm-client/src/domain.rs @@ -196,6 +196,9 @@ fn mux_notify_client_domain(local_domain_id: DomainId, notif: MuxNotification) - None => return false, }; match notif { + MuxNotification::ActiveWorkspaceChanged(_client_id) => { + // TODO: advice remote host of interesting workspaces + } MuxNotification::WindowWorkspaceChanged(window_id) => { if let Some(remote_window_id) = domain.local_to_remote_window_id(window_id) { if let Some(workspace) = mux @@ -334,7 +337,9 @@ impl ClientDomain { } log::debug!("tree: {:#?}", tabroot); + let mut workspace = None; tab.sync_with_pane_tree(root_size, tabroot, |entry| { + workspace.replace(entry.workspace.clone()); if let Some(pane_id) = inner.remote_to_local_pane_id(entry.pane_id) { match mux.get_pane(pane_id) { Some(pane) => pane, @@ -380,7 +385,7 @@ impl ClientDomain { window.push(&tab); } } else { - let local_window_id = mux.new_empty_window(); + let local_window_id = mux.new_empty_window(workspace.take()); inner.record_remote_to_local_window_mapping(remote_window_id, *local_window_id); mux.add_tab_to_window(&tab, *local_window_id)?; } @@ -445,6 +450,9 @@ impl Domain for ClientDomain { let inner = self .inner() .ok_or_else(|| anyhow!("domain is not attached"))?; + + let workspace = Mux::get().unwrap().active_workspace(); + let result = inner .client .spawn_v2(SpawnV2 { @@ -453,6 +461,7 @@ impl Domain for ClientDomain { size, command, command_dir, + workspace, }) .await?; diff --git a/wezterm-gui/src/frontend.rs b/wezterm-gui/src/frontend.rs index b01a66856..1d3bb11e6 100644 --- a/wezterm-gui/src/frontend.rs +++ b/wezterm-gui/src/frontend.rs @@ -1,3 +1,4 @@ +use crate::termwindow::TermWindowNotif; use crate::TermWindow; use ::window::*; use anyhow::Error; @@ -10,6 +11,8 @@ use wezterm_toast_notification::*; pub struct GuiFrontEnd { connection: Rc, + switching_workspaces: RefCell, + known_windows: RefCell>, } impl Drop for GuiFrontEnd { @@ -21,26 +24,49 @@ impl Drop for GuiFrontEnd { impl GuiFrontEnd { pub fn try_new() -> anyhow::Result> { let connection = Connection::init()?; - let front_end = Rc::new(GuiFrontEnd { connection }); + let mux = Mux::get().expect("mux started and running on main thread"); + let client_id = mux.active_identity().expect("to have set my own id"); + + let front_end = Rc::new(GuiFrontEnd { + connection, + switching_workspaces: RefCell::new(false), + known_windows: RefCell::new(vec![]), + }); let fe = Rc::downgrade(&front_end); mux.subscribe(move |n| { if let Some(_fe) = fe.upgrade() { match n { MuxNotification::WindowCreated(mux_window_id) => { - promise::spawn::spawn(async move { - if let Err(err) = TermWindow::new_window(mux_window_id).await { - log::error!("Failed to create window: {:#}", err); - let mux = Mux::get().expect("subscribe to trigger on main thread"); - mux.kill_window(mux_window_id); + if !is_switching_workspace() { + let mux = Mux::get().expect("mux started and running on main thread"); + let window = mux.get_window(mux_window_id).expect("new window notif to have valid window"); + if window.get_workspace() == mux.active_workspace_for_client(&client_id) { + + promise::spawn::spawn(async move { + if let Err(err) = TermWindow::new_window(mux_window_id).await { + log::error!("Failed to create window: {:#}", err); + let mux = Mux::get().expect("subscribe to trigger on main thread"); + mux.kill_window(mux_window_id); + } + anyhow::Result::<()>::Ok(()) + }) + .detach(); } - anyhow::Result::<()>::Ok(()) - }) - .detach(); + } } - MuxNotification::WindowWorkspaceChanged(_) => {} + MuxNotification::WindowWorkspaceChanged(_) | MuxNotification::ActiveWorkspaceChanged(_) => {} MuxNotification::WindowRemoved(_) => {} - MuxNotification::PaneRemoved(_) => {} + MuxNotification::PaneRemoved(_) => { + if mux::activity::Activity::count() == 0 { + let mux = Mux::get().expect("mux started and running on main thread"); + let workspace = mux.active_workspace_for_client(&client_id); + if mux.is_workspace_empty(&workspace) { + log::trace!("Mux workspace is empty, terminate gui"); + Connection::get().unwrap().terminate_message_loop(); + } + } + } MuxNotification::WindowInvalidated(_) => {} MuxNotification::PaneOutput(_) => {} MuxNotification::PaneAdded(_) => {} @@ -88,12 +114,125 @@ impl GuiFrontEnd { pub fn run_forever(&self) -> anyhow::Result<()> { self.connection.run_message_loop() } + + pub fn switch_workspace(&self, workspace: &str) { + let mux = Mux::get().expect("mux started and running on main thread"); + let mut mux_windows = mux.iter_windows_in_workspace(&workspace).into_iter(); + + // First, repurpose existing windows. + // Note that both iter_windows_in_workspace and self.known_windows have a + // deterministic iteration order, so switching back and forth should result + // in a consistent mux <-> gui window mapping. + { + let mut known_windows = self.known_windows.borrow_mut(); + let mut windows = vec![]; + + for window in known_windows.drain(..) { + if let Some(mux_window_id) = mux_windows.next() { + window.notify(TermWindowNotif::SwitchToMuxWindow(mux_window_id)); + windows.push(window); + } else { + // We have more windows than are in the new workspace; + // we no longer need this one! + window.close(); + } + } + + *known_windows = windows; + } + + // then spawn any new windows that are needed + promise::spawn::spawn(async move { + while let Some(mux_window_id) = mux_windows.next() { + if let Err(err) = TermWindow::new_window(mux_window_id).await { + log::error!("Failed to create window: {:#}", err); + let mux = Mux::get().expect("switching_workspaces to trigger on main thread"); + mux.kill_window(mux_window_id); + } + } + FRONT_END.with(|f| { + f.borrow_mut() + .as_mut() + .map(|f| *f.switching_workspaces.borrow_mut() = false) + }); + }) + .detach(); + } + + pub fn record_known_window(&self, window: Window) { + self.known_windows.borrow_mut().push(window); + } + + pub fn forget_known_window(&self, window: Window) { + self.known_windows.borrow_mut().retain(|w| *w != window); + } } thread_local! { static FRONT_END: RefCell>> = RefCell::new(None); } +pub fn record_known_window(window: Window) { + FRONT_END.with(|f| { + f.borrow_mut() + .as_mut() + .map(|f| f.record_known_window(window)) + }); +} + +pub fn forget_known_window(window: Window) { + FRONT_END.with(|f| { + f.borrow_mut() + .as_mut() + .map(|f| f.forget_known_window(window)) + }); +} + +pub fn is_switching_workspace() -> bool { + FRONT_END + .with(|f| { + f.borrow() + .as_ref() + .map(|f| *f.switching_workspaces.borrow()) + }) + .unwrap_or(false) +} + +pub fn switch_workspace(new_name: &str) { + FRONT_END.with(|f| { + f.borrow_mut() + .as_mut() + .map(|f| f.switch_workspace(new_name)) + }); +} + +pub struct WorkspaceSwitcher { + new_name: String, +} + +impl WorkspaceSwitcher { + pub fn new(new_name: &str) -> Self { + FRONT_END.with(|f| { + f.borrow_mut() + .as_mut() + .map(|f| *f.switching_workspaces.borrow_mut() = true) + }); + Self { + new_name: new_name.to_string(), + } + } + + pub fn do_switch(self) { + // Drop is invoked, which will complete the switch + } +} + +impl Drop for WorkspaceSwitcher { + fn drop(&mut self) { + switch_workspace(&self.new_name); + } +} + pub fn shutdown() { FRONT_END.with(|f| drop(f.borrow_mut().take())); } diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index 26fec0df1..176ae5709 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -173,7 +173,7 @@ fn run_serial(config: config::ConfigHandle, opts: &SerialCommand) -> anyhow::Res block_on(domain.attach())?; // FIXME: blocking { - let window_id = mux.new_empty_window(); + let window_id = mux.new_empty_window(None); // FIXME: blocking let _tab = block_on(domain.spawn(config.initial_size(), None, None, *window_id))?; } @@ -289,7 +289,7 @@ async fn spawn_tab_in_default_domain_if_mux_is_empty( true }); - let window_id = mux.new_empty_window(); + let window_id = mux.new_empty_window(None); let _tab = domain .spawn(config.initial_size(), cmd, None, *window_id) .await?; @@ -451,6 +451,7 @@ impl Publish { command, command_dir: None, size: config.initial_size(), + workspace: mux::DEFAULT_WORKSPACE.to_string(), // FIXME: command line option }) .await })); @@ -522,7 +523,7 @@ fn setup_mux( let client_id = Arc::new(mux::client::ClientId::new()); mux.register_client(client_id.clone()); mux.replace_identity(Some(client_id)); - mux.set_active_workspace(mux::DEFAULT_WORKSPACE); + mux.set_active_workspace(mux::DEFAULT_WORKSPACE); // FIXME: command line option crate::update::load_last_release_info_and_set_banner(); update_mux_domains(config)?; diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 83eb44e0e..81973b9ba 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -108,6 +108,7 @@ pub enum TermWindowNotif { MuxNotification(MuxNotification), EmitStatusUpdate, Apply(Box), + SwitchToMuxWindow(MuxWindowId), } #[derive(Clone, Debug, PartialEq, Eq)] @@ -772,6 +773,7 @@ impl TermWindow { } crate::update::start_update_checker(); + crate::frontend::record_known_window(window); Ok(()) } @@ -979,6 +981,19 @@ impl TermWindow { TermWindowNotif::Apply(func) => { func(self); } + TermWindowNotif::SwitchToMuxWindow(mux_window_id) => { + self.mux_window_id = mux_window_id; + self.pane_state.borrow_mut().clear(); + self.tab_state.borrow_mut().clear(); + self.current_highlight.take(); + self.invalidate_fancy_tab_bar(); + + let mux = Mux::get().expect("to be main thread with mux running"); + if let Some(tab) = mux.get_active_tab_for_window(mux_window_id) { + self.set_window_size(tab.get_size(), window)?; + } + window.invalidate(); + } } Ok(()) @@ -2075,6 +2090,44 @@ impl TermWindow { }; tab.toggle_zoom(); } + SwitchToWorkspace { name, spawn } => { + let mux = Mux::get().unwrap(); + let name = name + .as_ref() + .map(|name| name.to_string()) + .unwrap_or_else(|| mux.generate_workspace_name()); + let switcher = crate::frontend::WorkspaceSwitcher::new(&name); + mux.set_active_workspace(&name); + + if mux.iter_windows_in_workspace(&name).is_empty() { + let spawn = spawn.as_ref().map(|s| s.clone()).unwrap_or_default(); + let size = self.config.initial_size(); + let term_config = Arc::new(TermConfig::with_config(self.config.clone())); + let src_window_id = self.mux_window_id; + let clipboard = ClipboardHelper { + window: self.window.as_ref().unwrap().clone(), + }; + + promise::spawn::spawn(async move { + if let Err(err) = Self::spawn_command_internal( + spawn, + SpawnWhere::NewWindow, + size, + src_window_id, + clipboard, + term_config, + ) + .await + { + log::error!("Failed to spawn: {:#}", err); + } + switcher.do_switch(); + }) + .detach(); + } else { + switcher.do_switch(); + } + } }; Ok(()) } @@ -2479,3 +2532,11 @@ impl TermWindow { self.update_title(); } } + +impl Drop for TermWindow { + fn drop(&mut self) { + if let Some(window) = self.window.take() { + crate::frontend::forget_known_window(window); + } + } +} diff --git a/wezterm-gui/src/termwindow/resize.rs b/wezterm-gui/src/termwindow/resize.rs index 5b6d220cb..58e40b403 100644 --- a/wezterm-gui/src/termwindow/resize.rs +++ b/wezterm-gui/src/termwindow/resize.rs @@ -374,9 +374,8 @@ impl super::TermWindow { self.adjust_font_scale(1.0, window); } - pub fn reset_font_and_window_size(&mut self, window: &Window) -> anyhow::Result<()> { + pub fn set_window_size(&mut self, size: PtySize, window: &Window) -> anyhow::Result<()> { let config = &self.config; - let size = config.initial_size(); let fontconfig = Rc::new(FontConfiguration::new( Some(config.clone()), self.dimensions.dpi, @@ -434,6 +433,11 @@ impl super::TermWindow { Ok(()) } + pub fn reset_font_and_window_size(&mut self, window: &Window) -> anyhow::Result<()> { + let size = self.config.initial_size(); + self.set_window_size(size, window) + } + pub fn effective_right_padding(&self, config: &ConfigHandle) -> u16 { effective_right_padding( config, diff --git a/wezterm-gui/src/termwindow/spawn.rs b/wezterm-gui/src/termwindow/spawn.rs index 5ba36a1df..ffaf78cd4 100644 --- a/wezterm-gui/src/termwindow/spawn.rs +++ b/wezterm-gui/src/termwindow/spawn.rs @@ -16,7 +16,7 @@ pub enum SpawnWhere { } impl super::TermWindow { - pub fn spawn_command(&mut self, spawn: &SpawnCommand, spawn_where: SpawnWhere) { + pub fn spawn_command(&self, spawn: &SpawnCommand, spawn_where: SpawnWhere) { let size = if spawn_where == SpawnWhere::NewWindow { self.config.initial_size() } else { @@ -36,7 +36,7 @@ impl super::TermWindow { ) } - pub fn spawn_command_impl( + fn spawn_command_impl( spawn: &SpawnCommand, spawn_where: SpawnWhere, size: PtySize, @@ -63,7 +63,7 @@ impl super::TermWindow { .detach(); } - async fn spawn_command_internal( + pub async fn spawn_command_internal( spawn: SpawnCommand, spawn_where: SpawnWhere, size: PtySize, @@ -107,6 +107,7 @@ impl super::TermWindow { let clipboard: Arc = Arc::new(clipboard); let downloader: Arc = Arc::new(crate::download::Downloader::new()); + let workspace = mux.active_workspace().clone(); match spawn_where { SpawnWhere::SplitPane(direction) => { @@ -146,6 +147,7 @@ impl super::TermWindow { cwd, size, current_pane_id, + workspace, ) .await .context("spawn_tab_or_window")?; diff --git a/wezterm-mux-server-impl/src/dispatch.rs b/wezterm-mux-server-impl/src/dispatch.rs index 5eabdff37..3c859b649 100644 --- a/wezterm-mux-server-impl/src/dispatch.rs +++ b/wezterm-mux-server-impl/src/dispatch.rs @@ -115,6 +115,7 @@ where stream.flush().await.context("flushing PDU to client")?; } } + Ok(Item::Notif(MuxNotification::ActiveWorkspaceChanged(_))) => {} Ok(Item::Notif(MuxNotification::Empty)) => {} Err(err) => { log::error!("process_async Err {}", err); diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rs index bc49c1d53..d282132e8 100644 --- a/wezterm-mux-server-impl/src/sessionhandler.rs +++ b/wezterm-mux-server-impl/src/sessionhandler.rs @@ -759,6 +759,7 @@ async fn domain_spawn_v2( spawn.command_dir, spawn.size, None, // optional current pane_id + spawn.workspace, ) .await?; diff --git a/wezterm-mux-server/src/main.rs b/wezterm-mux-server/src/main.rs index 2d32abb8d..c9c91cc24 100644 --- a/wezterm-mux-server/src/main.rs +++ b/wezterm-mux-server/src/main.rs @@ -198,7 +198,7 @@ async fn async_run(cmd: Option) -> anyhow::Result<()> { domain.attach().await?; let config = config::configuration(); - let window_id = mux.new_empty_window(); + let window_id = mux.new_empty_window(None); let _tab = mux .default_domain() .spawn(config.initial_size(), cmd, None, *window_id) diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index ded9685ca..e9cd9daa9 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -182,6 +182,9 @@ Outputs the pane-id for the newly created pane on success" #[structopt(long = "cwd", parse(from_os_str))] cwd: Option, + #[structopt(long = "workspace")] + workspace: Option, + /// Instead of executing your shell, run PROG. /// For example: `wezterm start -- bash -l` will spawn bash /// as if it were a login shell. @@ -568,6 +571,7 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow: domain_name, window_id, new_window, + workspace, } => { let window_id = if new_window { None @@ -611,6 +615,8 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow: } }; + let workspace = workspace.unwrap_or_else(|| Mux::get().unwrap().active_workspace()); + let spawned = client .spawn_v2(codec::SpawnV2 { domain: domain_name.map_or(SpawnTabDomain::DefaultDomain, |name| { @@ -625,6 +631,7 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow: }, command_dir: canon_cwd(cwd)?, size: config::configuration().initial_size(), + workspace, }) .await?; diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index dc9607d8d..81fba927e 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -367,7 +367,7 @@ fn function_key_to_keycode(function_key: char) -> KeyCode { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Window { id: usize, ns_window: *mut Object, diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs index 47b239b55..19338f2b2 100644 --- a/window/src/os/wayland/window.rs +++ b/window/src/os/wayland/window.rs @@ -223,7 +223,7 @@ impl PendingEvent { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub struct WaylandWindow(usize); impl WaylandWindow { diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 5115d6ccc..3b3c7c14f 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -68,7 +68,7 @@ pub(crate) struct WindowInner { config: ConfigHandle, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Window(HWindow); fn rect_width(r: &RECT) -> i32 { diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index 284c79f34..05871b5a5 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -740,7 +740,7 @@ impl XWindowInner { } /// A Window! -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct XWindow(xcb::xproto::Window); impl XWindow { diff --git a/window/src/os/x_and_wayland.rs b/window/src/os/x_and_wayland.rs index d75156e25..dafb8f5be 100644 --- a/window/src/os/x_and_wayland.rs +++ b/window/src/os/x_and_wayland.rs @@ -22,7 +22,7 @@ pub enum Connection { Wayland(Rc), } -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub enum Window { X11(XWindow), #[cfg(feature = "wayland")]