refactor: move frontend up a level -> own module

This commit is contained in:
Wez Furlong committed 2021-02-28 00:04:10 -08:00
1 parent 04ecd16493
commit ac376fb9c6
3 files changed
+115 -111

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
use crate::gui::TermWindow;
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>,
}
impl Drop for GuiFrontEnd {
fn drop(&mut self) {
::window::shutdown();
}
}
impl GuiFrontEnd {
pub fn try_new() -> anyhow::Result<Rc<GuiFrontEnd>> {
let connection = Connection::init()?;
let front_end = Rc::new(GuiFrontEnd { connection });
let mux = Mux::get().expect("mux started and running on main thread");
let fe = Rc::downgrade(&front_end);
mux.subscribe(move |n| {
if let Some(_fe) = fe.upgrade() {
match n {
MuxNotification::WindowCreated(mux_window_id) => {
if let Err(err) = TermWindow::new_window(mux_window_id) {
log::error!("Failed to create window: {:#}", err);
let mux = Mux::get().expect("subscribe to trigger on main thread");
mux.kill_window(mux_window_id);
}
}
MuxNotification::PaneOutput(_) => {}
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,
} => {
// persistent_toast_notification("Ding!", "This is the bell");
log::info!("Ding! (this is the bell)");
}
}
true
} else {
false
}
});
Ok(front_end)
}
pub fn run_forever(&self) -> anyhow::Result<()> {
self.connection
.schedule_timer(std::time::Duration::from_millis(200), move || {
if mux::activity::Activity::count() == 0 {
let mux = Mux::get().unwrap();
mux.prune_dead_windows();
if mux.is_empty() {
Connection::get().unwrap().terminate_message_loop();
}
}
});
self.connection.run_message_loop()
}
}
thread_local! {
static FRONT_END: RefCell<Option<Rc<GuiFrontEnd>>> = RefCell::new(None);
}
pub fn front_end() -> Option<Rc<GuiFrontEnd>> {
let mut res = None;
FRONT_END.with(|f| {
if let Some(me) = &*f.borrow() {
res = Some(Rc::clone(me));
}
});
res
}
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)
}
-105
View File
@@ -1,11 +1,5 @@
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::*;
mod glyphcache;
mod overlay;
@@ -22,102 +16,3 @@ pub use selection::SelectionMode;
pub use termwindow::set_window_class;
pub use termwindow::TermWindow;
pub use termwindow::ICON_DATA;
pub struct GuiFrontEnd {
connection: Rc<Connection>,
}
impl Drop for GuiFrontEnd {
fn drop(&mut self) {
::window::shutdown();
}
}
impl GuiFrontEnd {
pub fn try_new() -> anyhow::Result<Rc<GuiFrontEnd>> {
let connection = Connection::init()?;
let front_end = Rc::new(GuiFrontEnd { connection });
let mux = Mux::get().expect("mux started and running on main thread");
let fe = Rc::downgrade(&front_end);
mux.subscribe(move |n| {
if let Some(_fe) = fe.upgrade() {
match n {
MuxNotification::WindowCreated(mux_window_id) => {
if let Err(err) = termwindow::TermWindow::new_window(mux_window_id) {
log::error!("Failed to create window: {:#}", err);
let mux = Mux::get().expect("subscribe to trigger on main thread");
mux.kill_window(mux_window_id);
}
}
MuxNotification::PaneOutput(_) => {}
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,
} => {
// persistent_toast_notification("Ding!", "This is the bell");
log::info!("Ding! (this is the bell)");
}
}
true
} else {
false
}
});
Ok(front_end)
}
pub fn run_forever(&self) -> anyhow::Result<()> {
self.connection
.schedule_timer(std::time::Duration::from_millis(200), move || {
if mux::activity::Activity::count() == 0 {
let mux = Mux::get().unwrap();
mux.prune_dead_windows();
if mux.is_empty() {
Connection::get().unwrap().terminate_message_loop();
}
}
});
self.connection.run_message_loop()
}
}
thread_local! {
static FRONT_END: RefCell<Option<Rc<GuiFrontEnd>>> = RefCell::new(None);
}
pub fn front_end() -> Option<Rc<GuiFrontEnd>> {
let mut res = None;
FRONT_END.with(|f| {
if let Some(me) = &*f.borrow() {
res = Some(Rc::clone(me));
}
});
res
}
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)
}
+7 -6
View File
@@ -1,7 +1,7 @@
// Don't create a new standard console window when launched from the windows GUI.
#![windows_subsystem = "windows"]
use crate::gui::front_end;
use crate::frontend::front_end;
use anyhow::anyhow;
use mux::activity::Activity;
use mux::domain::{Domain, LocalDomain};
@@ -16,6 +16,7 @@ use wezterm_client::domain::{ClientDomain, ClientDomainConfig};
use wezterm_gui_subcommands::*;
use wezterm_toast_notification::*;
mod frontend;
mod gui;
mod markdown;
mod scripting;
@@ -113,7 +114,7 @@ fn run_ssh(opts: SshCommand) -> anyhow::Result<()> {
Mux::set_mux(&mux);
crate::update::load_last_release_info_and_set_banner();
let gui = crate::gui::try_new()?;
let gui = crate::frontend::try_new()?;
// Keep the frontend alive until we've run through the ssh authentication
// phase. This is passed into the thread and dropped when it is done.
@@ -148,7 +149,7 @@ fn run_serial(config: config::ConfigHandle, opts: &SerialCommand) -> anyhow::Res
Mux::set_mux(&mux);
crate::update::load_last_release_info_and_set_banner();
let gui = crate::gui::try_new()?;
let gui = crate::frontend::try_new()?;
block_on(domain.attach())?; // FIXME: blocking
{
@@ -193,7 +194,7 @@ fn run_mux_client(config: config::ConfigHandle, opts: &ConnectCommand) -> anyhow
Mux::set_mux(&mux);
crate::update::load_last_release_info_and_set_banner();
let gui = crate::gui::try_new()?;
let gui = crate::frontend::try_new()?;
let opts = opts.clone();
let cmd = if !opts.prog.is_empty() {
@@ -307,7 +308,7 @@ fn run_terminal_gui(opts: StartCommand) -> anyhow::Result<()> {
Mux::set_mux(&mux);
crate::update::load_last_release_info_and_set_banner();
let gui = crate::gui::try_new()?;
let gui = crate::frontend::try_new()?;
let activity = Activity::new();
let do_auto_connect = !opts.no_auto_connect;
@@ -366,7 +367,7 @@ fn main() {
terminate_with_error(e);
}
Mux::shutdown();
gui::shutdown();
frontend::shutdown();
}
fn maybe_show_configuration_error_window() {