2022-01-15 23:48:57 +03:00
|
|
|
use crate::termwindow::TermWindowNotif;
|
2021-02-28 11:11:17 +03:00
|
|
|
use crate::TermWindow;
|
2021-02-28 11:04:10 +03:00
|
|
|
use ::window::*;
|
|
|
|
use anyhow::Error;
|
|
|
|
pub use config::FrontEndSelection;
|
|
|
|
use mux::{Mux, MuxNotification};
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use wezterm_term::Alert;
|
|
|
|
use wezterm_toast_notification::*;
|
|
|
|
|
|
|
|
pub struct GuiFrontEnd {
|
|
|
|
connection: Rc<Connection>,
|
2022-01-15 23:48:57 +03:00
|
|
|
switching_workspaces: RefCell<bool>,
|
|
|
|
known_windows: RefCell<Vec<Window>>,
|
2021-02-28 11:04:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for GuiFrontEnd {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
::window::shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GuiFrontEnd {
|
|
|
|
pub fn try_new() -> anyhow::Result<Rc<GuiFrontEnd>> {
|
|
|
|
let connection = Connection::init()?;
|
2022-01-15 23:48:57 +03:00
|
|
|
|
2021-02-28 11:04:10 +03:00
|
|
|
let mux = Mux::get().expect("mux started and running on main thread");
|
2022-01-15 23:48:57 +03:00
|
|
|
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![]),
|
|
|
|
});
|
2021-02-28 11:04:10 +03:00
|
|
|
let fe = Rc::downgrade(&front_end);
|
|
|
|
mux.subscribe(move |n| {
|
2022-01-16 06:33:05 +03:00
|
|
|
if let Some(fe) = fe.upgrade() {
|
2021-02-28 11:04:10 +03:00
|
|
|
match n {
|
|
|
|
MuxNotification::WindowCreated(mux_window_id) => {
|
2022-01-16 06:33:05 +03:00
|
|
|
if !fe.is_switching_workspace() {
|
2022-01-15 23:48:57 +03:00
|
|
|
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();
|
2021-05-02 22:05:28 +03:00
|
|
|
}
|
2022-01-15 23:48:57 +03:00
|
|
|
}
|
2021-02-28 11:04:10 +03:00
|
|
|
}
|
2022-01-15 23:48:57 +03:00
|
|
|
MuxNotification::WindowWorkspaceChanged(_) | MuxNotification::ActiveWorkspaceChanged(_) => {}
|
2021-05-31 10:11:47 +03:00
|
|
|
MuxNotification::WindowRemoved(_) => {}
|
2022-01-15 23:48:57 +03:00
|
|
|
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) {
|
2022-01-16 00:41:19 +03:00
|
|
|
// 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);
|
2022-01-16 06:33:05 +03:00
|
|
|
crate::frontend::front_end().switch_workspace(&workspace);
|
2022-01-16 00:41:19 +03:00
|
|
|
drop(activity);
|
|
|
|
}).detach();
|
|
|
|
} else {
|
|
|
|
log::trace!("Mux workspace is empty, terminate gui");
|
|
|
|
Connection::get().unwrap().terminate_message_loop();
|
|
|
|
}
|
2022-01-15 23:48:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 21:25:03 +03:00
|
|
|
MuxNotification::WindowInvalidated(_) => {}
|
2021-02-28 11:04:10 +03:00
|
|
|
MuxNotification::PaneOutput(_) => {}
|
2021-05-31 18:21:19 +03:00
|
|
|
MuxNotification::PaneAdded(_) => {}
|
2021-02-28 11:04:10 +03:00
|
|
|
MuxNotification::Alert {
|
|
|
|
pane_id: _,
|
|
|
|
alert:
|
|
|
|
Alert::ToastNotification {
|
|
|
|
title,
|
|
|
|
body,
|
|
|
|
focus: _,
|
|
|
|
},
|
|
|
|
} => {
|
|
|
|
let message = if title.is_none() { "" } else { &body };
|
|
|
|
let title = title.as_ref().unwrap_or(&body);
|
|
|
|
// FIXME: if notification.focus is true, we should do
|
|
|
|
// something here to arrange to focus pane_id when the
|
|
|
|
// notification is clicked
|
|
|
|
persistent_toast_notification(title, message);
|
|
|
|
}
|
|
|
|
MuxNotification::Alert {
|
|
|
|
pane_id: _,
|
|
|
|
alert: Alert::Bell,
|
|
|
|
} => {
|
2021-09-23 18:21:15 +03:00
|
|
|
// Handled via TermWindowNotif; NOP it here.
|
2021-02-28 11:04:10 +03:00
|
|
|
}
|
2021-05-30 19:29:04 +03:00
|
|
|
| MuxNotification::Alert {
|
2021-04-30 17:26:46 +03:00
|
|
|
pane_id: _,
|
2022-01-11 04:33:32 +03:00
|
|
|
alert: Alert::PaletteChanged | Alert::TitleMaybeChanged | Alert::SetUserVar{..},
|
2021-04-30 17:26:46 +03:00
|
|
|
} => {}
|
2021-05-08 20:25:49 +03:00
|
|
|
MuxNotification::Empty => {
|
|
|
|
if mux::activity::Activity::count() == 0 {
|
|
|
|
log::trace!("Mux is now empty, terminate gui");
|
|
|
|
Connection::get().unwrap().terminate_message_loop();
|
|
|
|
}
|
|
|
|
}
|
2021-02-28 11:04:10 +03:00
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(front_end)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_forever(&self) -> anyhow::Result<()> {
|
|
|
|
self.connection.run_message_loop()
|
|
|
|
}
|
2022-01-15 23:48:57 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-01-16 06:02:14 +03:00
|
|
|
pub fn forget_known_window(&self, window: &Window) {
|
|
|
|
self.known_windows.borrow_mut().retain(|w| w != window);
|
2022-01-15 23:48:57 +03:00
|
|
|
}
|
2022-01-16 06:33:05 +03:00
|
|
|
|
|
|
|
pub fn is_switching_workspace(&self) -> bool {
|
|
|
|
*self.switching_workspaces.borrow()
|
|
|
|
}
|
2021-02-28 11:04:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
thread_local! {
|
|
|
|
static FRONT_END: RefCell<Option<Rc<GuiFrontEnd>>> = RefCell::new(None);
|
|
|
|
}
|
|
|
|
|
2022-01-16 06:33:05 +03:00
|
|
|
pub fn front_end() -> Rc<GuiFrontEnd> {
|
2022-01-15 23:48:57 +03:00
|
|
|
FRONT_END
|
2022-01-16 06:33:05 +03:00
|
|
|
.with(|f| f.borrow().as_ref().map(Rc::clone))
|
|
|
|
.expect("to be called on gui thread")
|
2022-01-15 23:48:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-01-16 06:33:05 +03:00
|
|
|
front_end().switch_workspace(&self.new_name);
|
2022-01-15 23:48:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:04:10 +03:00
|
|
|
pub fn shutdown() {
|
|
|
|
FRONT_END.with(|f| drop(f.borrow_mut().take()));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_new() -> Result<Rc<GuiFrontEnd>, Error> {
|
|
|
|
let front_end = GuiFrontEnd::try_new()?;
|
|
|
|
FRONT_END.with(|f| *f.borrow_mut() = Some(Rc::clone(&front_end)));
|
|
|
|
Ok(front_end)
|
|
|
|
}
|