simplify window reconciliation logic
refs: https://github.com/wez/wezterm/issues/1531
This commit is contained in:
1 parent
12f32aeda8
commit
e575bc9ff9
8 files changed
+99
-89
No files matched your search
+7
-4
@@ -715,10 +715,13 @@ impl Mux {
|
||||
}
|
||||
|
||||
pub fn add_tab_to_window(&self, tab: &Rc<Tab>, window_id: WindowId) -> anyhow::Result<()> {
|
||||
let mut window = self
|
||||
.get_window_mut(window_id)
|
||||
.ok_or_else(|| anyhow!("add_tab_to_window: no such window_id {}", window_id))?;
|
||||
window.push(tab);
|
||||
{
|
||||
let mut window = self
|
||||
.get_window_mut(window_id)
|
||||
.ok_or_else(|| anyhow!("add_tab_to_window: no such window_id {}", window_id))?;
|
||||
window.push(tab);
|
||||
}
|
||||
self.recompute_pane_count();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
+83
-73
@@ -3,16 +3,21 @@ use crate::TermWindow;
|
||||
use ::window::*;
|
||||
use anyhow::Error;
|
||||
pub use config::FrontEndSelection;
|
||||
use mux::client::ClientId;
|
||||
use mux::window::WindowId as MuxWindowId;
|
||||
use mux::{Mux, MuxNotification};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::BTreeMap;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use wezterm_term::Alert;
|
||||
use wezterm_toast_notification::*;
|
||||
|
||||
pub struct GuiFrontEnd {
|
||||
connection: Rc<Connection>,
|
||||
switching_workspaces: RefCell<bool>,
|
||||
known_windows: RefCell<Vec<Window>>,
|
||||
known_windows: RefCell<BTreeMap<Window, MuxWindowId>>,
|
||||
client_id: Arc<ClientId>,
|
||||
}
|
||||
|
||||
impl Drop for GuiFrontEnd {
|
||||
@@ -31,57 +36,23 @@ impl GuiFrontEnd {
|
||||
let front_end = Rc::new(GuiFrontEnd {
|
||||
connection,
|
||||
switching_workspaces: RefCell::new(false),
|
||||
known_windows: RefCell::new(vec![]),
|
||||
known_windows: RefCell::new(BTreeMap::new()),
|
||||
client_id: client_id.clone(),
|
||||
});
|
||||
let fe = Rc::downgrade(&front_end);
|
||||
mux.subscribe(move |n| {
|
||||
if let Some(fe) = fe.upgrade() {
|
||||
match n {
|
||||
MuxNotification::WindowCreated(mux_window_id) => {
|
||||
if !fe.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
MuxNotification::WindowWorkspaceChanged(_) | MuxNotification::ActiveWorkspaceChanged(_) => {}
|
||||
MuxNotification::WindowRemoved(_) => {}
|
||||
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) {
|
||||
// We don't want to silently kill off things that might
|
||||
// be running in other workspaces, so let's pick one
|
||||
// and activate it
|
||||
let workspaces = mux.iter_workspaces();
|
||||
if let Some(workspace) = workspaces.get(0).map(|s| s.to_string()) {
|
||||
let activity = mux::activity::Activity::new();
|
||||
let client_id = client_id.clone();
|
||||
promise::spawn::spawn(async move {
|
||||
let mux = Mux::get().expect("mux started and running on main thread");
|
||||
mux.set_active_workspace_for_client(&client_id, &workspace);
|
||||
crate::frontend::front_end().switch_workspace(&workspace);
|
||||
drop(activity);
|
||||
}).detach();
|
||||
} else {
|
||||
log::trace!("Mux workspace is empty, terminate gui");
|
||||
Connection::get().unwrap().terminate_message_loop();
|
||||
}
|
||||
MuxNotification::WindowCreated(_) | MuxNotification::WindowRemoved(_) => {
|
||||
promise::spawn::spawn(async move {
|
||||
let fe = crate::frontend::front_end();
|
||||
if !fe.is_switching_workspace() {
|
||||
fe.reconcile_workspace();
|
||||
}
|
||||
}
|
||||
}).detach();
|
||||
}
|
||||
MuxNotification::PaneRemoved(_) => {}
|
||||
MuxNotification::WindowInvalidated(_) => {}
|
||||
MuxNotification::PaneOutput(_) => {}
|
||||
MuxNotification::PaneAdded(_) => {}
|
||||
@@ -130,32 +101,64 @@ impl GuiFrontEnd {
|
||||
self.connection.run_message_loop()
|
||||
}
|
||||
|
||||
pub fn switch_workspace(&self, workspace: &str) {
|
||||
pub fn reconcile_workspace(&self) {
|
||||
let mux = Mux::get().expect("mux started and running on main thread");
|
||||
let mut mux_windows = mux.iter_windows_in_workspace(&workspace).into_iter();
|
||||
let workspace = mux.active_workspace_for_client(&self.client_id);
|
||||
|
||||
if mux.is_workspace_empty(&workspace) {
|
||||
// We don't want to silently kill off things that might
|
||||
// be running in other workspaces, so let's pick one
|
||||
// and activate it
|
||||
if self.is_switching_workspace() {
|
||||
return;
|
||||
}
|
||||
for workspace in mux.iter_workspaces() {
|
||||
if !mux.is_workspace_empty(&workspace) {
|
||||
mux.set_active_workspace_for_client(&self.client_id, &workspace);
|
||||
log::debug!("using {} instead, as it is not empty", workspace);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let workspace = mux.active_workspace_for_client(&self.client_id);
|
||||
log::debug!("workspace is {}, fixup windows", workspace);
|
||||
|
||||
let mut mux_windows = mux.iter_windows_in_workspace(&workspace);
|
||||
|
||||
// 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![];
|
||||
let known_windows = std::mem::take(&mut *self.known_windows.borrow_mut());
|
||||
let mut windows = BTreeMap::new();
|
||||
let mut unused = BTreeMap::new();
|
||||
|
||||
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();
|
||||
}
|
||||
for (window, window_id) in known_windows.into_iter() {
|
||||
if let Some(idx) = mux_windows.iter().position(|&id| id == window_id) {
|
||||
// it already points to the desired mux window
|
||||
windows.insert(window, window_id);
|
||||
mux_windows.remove(idx);
|
||||
} else {
|
||||
unused.insert(window, window_id);
|
||||
}
|
||||
|
||||
*known_windows = windows;
|
||||
}
|
||||
|
||||
let mut mux_windows = mux_windows.into_iter();
|
||||
|
||||
for (window, _old_id) in unused.into_iter() {
|
||||
if let Some(mux_window_id) = mux_windows.next() {
|
||||
window.notify(TermWindowNotif::SwitchToMuxWindow(mux_window_id));
|
||||
windows.insert(window, mux_window_id);
|
||||
} else {
|
||||
// We have more windows than are in the new workspace;
|
||||
// we no longer need this one!
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
|
||||
*self.known_windows.borrow_mut() = windows;
|
||||
|
||||
// then spawn any new windows that are needed
|
||||
promise::spawn::spawn(async move {
|
||||
while let Some(mux_window_id) = mux_windows.next() {
|
||||
@@ -165,21 +168,32 @@ impl GuiFrontEnd {
|
||||
mux.kill_window(mux_window_id);
|
||||
}
|
||||
}
|
||||
FRONT_END.with(|f| {
|
||||
f.borrow_mut()
|
||||
.as_mut()
|
||||
.map(|f| *f.switching_workspaces.borrow_mut() = false)
|
||||
});
|
||||
*front_end().switching_workspaces.borrow_mut() = false;
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
pub fn record_known_window(&self, window: Window) {
|
||||
self.known_windows.borrow_mut().push(window);
|
||||
pub fn switch_workspace(&self, workspace: &str) {
|
||||
let mux = Mux::get().expect("mux started and running on main thread");
|
||||
mux.set_active_workspace_for_client(&self.client_id, workspace);
|
||||
*self.switching_workspaces.borrow_mut() = false;
|
||||
self.reconcile_workspace();
|
||||
}
|
||||
|
||||
pub fn record_known_window(&self, window: Window, mux_window_id: MuxWindowId) {
|
||||
self.known_windows
|
||||
.borrow_mut()
|
||||
.insert(window, mux_window_id);
|
||||
if !self.is_switching_workspace() {
|
||||
self.reconcile_workspace();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn forget_known_window(&self, window: &Window) {
|
||||
self.known_windows.borrow_mut().retain(|w| w != window);
|
||||
self.known_windows.borrow_mut().remove(window);
|
||||
if !self.is_switching_workspace() {
|
||||
self.reconcile_workspace();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_switching_workspace(&self) -> bool {
|
||||
@@ -203,11 +217,7 @@ pub struct WorkspaceSwitcher {
|
||||
|
||||
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)
|
||||
});
|
||||
*front_end().switching_workspaces.borrow_mut() = true;
|
||||
Self {
|
||||
new_name: new_name.to_string(),
|
||||
}
|
||||
|
||||
@@ -777,7 +777,7 @@ impl TermWindow {
|
||||
}
|
||||
|
||||
crate::update::start_update_checker();
|
||||
front_end().record_known_window(window);
|
||||
front_end().record_known_window(window, mux_window_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -963,11 +963,8 @@ impl TermWindow {
|
||||
MuxNotification::WindowInvalidated(_) => {
|
||||
window.invalidate();
|
||||
}
|
||||
MuxNotification::WindowRemoved(window_id) => {
|
||||
if window_id == self.mux_window_id {
|
||||
window.close();
|
||||
front_end().forget_known_window(window);
|
||||
}
|
||||
MuxNotification::WindowRemoved(_window_id) => {
|
||||
// Handled by frontend
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
|
||||
@@ -367,7 +367,7 @@ fn function_key_to_keycode(function_key: char) -> KeyCode {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
||||
pub struct Window {
|
||||
id: usize,
|
||||
ns_window: *mut Object,
|
||||
|
||||
@@ -223,7 +223,7 @@ impl PendingEvent {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
||||
pub struct WaylandWindow(usize);
|
||||
|
||||
impl WaylandWindow {
|
||||
|
||||
@@ -43,7 +43,7 @@ extern "system" {
|
||||
pub fn ImmGetCompositionStringW(himc: HIMC, index: DWORD, buf: LPVOID, buflen: DWORD) -> LONG;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
||||
pub(crate) struct HWindow(HWND);
|
||||
unsafe impl Send for HWindow {}
|
||||
unsafe impl Sync for HWindow {}
|
||||
@@ -68,7 +68,7 @@ pub(crate) struct WindowInner {
|
||||
config: ConfigHandle,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
||||
pub struct Window(HWindow);
|
||||
|
||||
fn rect_width(r: &RECT) -> i32 {
|
||||
|
||||
@@ -740,7 +740,7 @@ impl XWindowInner {
|
||||
}
|
||||
|
||||
/// A Window!
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
||||
pub struct XWindow(xcb::xproto::Window);
|
||||
|
||||
impl XWindow {
|
||||
|
||||
@@ -22,7 +22,7 @@ pub enum Connection {
|
||||
Wayland(Rc<WaylandConnection>),
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
||||
pub enum Window {
|
||||
X11(XWindow),
|
||||
#[cfg(feature = "wayland")]
|
||||
|
||||
Reference in new issue
Block a user