From 025732d00f63cd9fb37cbfae98cdc9603c337573 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 2 Oct 2020 20:08:13 -0700 Subject: [PATCH] Add MuxNotification::WindowCreated, use it to spawn gui window Rather than having each call site add a window to the mux and then call the front end to spawn a window, make the mux emit a signal advising of a window spawn, and have the front end subscribe to that signal. --- mux/src/lib.rs | 29 +++++++++++++++++++-- src/frontend/gui/mod.rs | 29 +++++++++++---------- src/frontend/gui/termwindow.rs | 37 +++++++++++---------------- src/frontend/mod.rs | 10 -------- src/frontend/muxserver/mod.rs | 12 --------- src/main.rs | 27 ++++++++----------- src/server/domain.rs | 12 ++------- src/server/listener/clientsession.rs | 5 +++- src/server/listener/sessionhandler.rs | 4 ++- src/termwiztermtab.rs | 11 ++------ 10 files changed, 79 insertions(+), 97 deletions(-) diff --git a/mux/src/lib.rs b/mux/src/lib.rs index d49283c12..d2e0073b1 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -25,6 +25,7 @@ pub mod window; #[derive(Clone, Debug)] pub enum MuxNotification { PaneOutput(PaneId), + WindowCreated(WindowId), } static SUB_ID: AtomicUsize = AtomicUsize::new(0); @@ -104,6 +105,30 @@ thread_local! { static MUX: RefCell>> = RefCell::new(None); } +pub struct MuxWindowBuilder { + window_id: WindowId, + // FIXME: Activity here +} + +impl Drop for MuxWindowBuilder { + fn drop(&mut self) { + let window_id = self.window_id; + promise::spawn::spawn_into_main_thread(async move { + if let Some(mux) = Mux::get() { + mux.notify(MuxNotification::WindowCreated(window_id)); + } + }); + } +} + +impl std::ops::Deref for MuxWindowBuilder { + type Target = WindowId; + + fn deref(&self) -> &WindowId { + &self.window_id + } +} + impl Mux { pub fn new(default_domain: Option>) -> Self { let mut domains = HashMap::new(); @@ -330,11 +355,11 @@ impl Mux { window.get_active().map(Rc::clone) } - pub fn new_empty_window(&self) -> WindowId { + pub fn new_empty_window(&self) -> MuxWindowBuilder { let window = Window::new(); let window_id = window.window_id(); self.windows.borrow_mut().insert(window_id, window); - window_id + MuxWindowBuilder { window_id } } pub fn add_tab_to_window(&self, tab: &Rc, window_id: WindowId) -> anyhow::Result<()> { diff --git a/src/frontend/gui/mod.rs b/src/frontend/gui/mod.rs index 227020ddc..2f982af0c 100644 --- a/src/frontend/gui/mod.rs +++ b/src/frontend/gui/mod.rs @@ -1,10 +1,7 @@ -use crate::font::FontConfiguration; use crate::frontend::FrontEnd; use ::window::*; use config::configuration; -use mux::tab::Tab; -use mux::window::WindowId as MuxWindowId; -use mux::Mux; +use mux::{Mux, MuxNotification}; use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; @@ -57,6 +54,21 @@ impl GuiFrontEnd { let connection = Connection::init()?; let front_end = Rc::new(GuiFrontEnd { connection }); + let mux = Mux::get().unwrap(); + let fe = Rc::downgrade(&front_end); + mux.subscribe(move |n| { + if let Some(_fe) = fe.upgrade() { + match n { + MuxNotification::WindowCreated(mux_window_id) => { + termwindow::TermWindow::new_window(mux_window_id).ok(); + } + MuxNotification::PaneOutput(_) => {} + } + true + } else { + false + } + }); Ok(front_end) } } @@ -76,13 +88,4 @@ impl FrontEnd for GuiFrontEnd { self.connection.run_message_loop() } - - fn spawn_new_window( - &self, - fontconfig: &Rc, - tab: &Rc, - window_id: MuxWindowId, - ) -> anyhow::Result<()> { - termwindow::TermWindow::new_window(&configuration(), fontconfig, tab, window_id) - } } diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index 6b3a888aa..baab19159 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -6,7 +6,6 @@ use crate::font::shaper::GlyphInfo; use crate::font::units::*; use crate::font::FontConfiguration; use crate::frontend::activity::Activity; -use crate::frontend::front_end; use crate::frontend::gui::overlay::{ confirm_close_pane, confirm_close_tab, launcher, start_overlay, start_overlay_pane, tab_navigator, CopyOverlay, SearchOverlay, @@ -34,7 +33,7 @@ use lru::LruCache; use mux::domain::{DomainId, DomainState}; use mux::pane::{Pane, PaneId}; use mux::renderable::{RenderableDimensions, StableCursorPosition}; -use mux::tab::{PositionedPane, PositionedSplit, SplitDirection, Tab, TabId}; +use mux::tab::{PositionedPane, PositionedSplit, SplitDirection, TabId}; use mux::window::WindowId as MuxWindowId; use mux::Mux; use portable_pty::{CommandBuilder, PtySize}; @@ -858,17 +857,16 @@ pub fn effective_right_padding(config: &ConfigHandle, render_metrics: &RenderMet } impl TermWindow { - pub fn new_window( - config: &ConfigHandle, - fontconfig: &Rc, - tab: &Rc, - mux_window_id: MuxWindowId, - ) -> anyhow::Result<()> { + pub fn new_window(mux_window_id: MuxWindowId) -> anyhow::Result<()> { + let config = configuration(); + let fontconfig = Rc::new(FontConfiguration::new()); + let mux = Mux::get().unwrap(); + let tab = mux.get_active_tab_for_window(mux_window_id).unwrap(); let size = tab.get_size(); let physical_rows = size.rows as usize; let physical_cols = size.cols as usize; - let render_metrics = RenderMetrics::new(fontconfig); + let render_metrics = RenderMetrics::new(&fontconfig); log::trace!("using render_metrics {:#?}", render_metrics); let terminal_size = PtySize { @@ -903,7 +901,7 @@ impl TermWindow { ); let render_state = RenderState::Software(SoftwareRenderState::new( - fontconfig, + &fontconfig, &render_metrics, ATLAS_SIZE, )?); @@ -919,7 +917,7 @@ impl TermWindow { window: None, focused: None, mux_window_id, - fonts: Rc::clone(fontconfig), + fonts: fontconfig, render_metrics, dimensions, terminal_size, @@ -1590,9 +1588,11 @@ impl TermWindow { promise::spawn::spawn(async move { let mux = Mux::get().unwrap(); let activity = Activity::new(); + let mux_builder; let mux_window_id = if spawn_where == SpawnWhere::NewWindow { - mux.new_empty_window() + mux_builder = mux.new_empty_window(); + *mux_builder } else { mux_window_id }; @@ -1704,11 +1704,7 @@ impl TermWindow { .get_active_pane() .ok_or_else(|| anyhow!("newly spawned tab to have a pane"))?; - if spawn_where == SpawnWhere::NewWindow { - let front_end = front_end().expect("to be called on gui thread"); - let fonts = Rc::new(FontConfiguration::new()); - front_end.spawn_new_window(&fonts, &tab, mux_window_id)?; - } else { + if spawn_where != SpawnWhere::NewWindow { let clipboard: Arc = Arc::new(clipboard); pane.set_clipboard(&clipboard); let mut window = mux @@ -1964,14 +1960,11 @@ impl TermWindow { async fn new_window() -> anyhow::Result<()> { let mux = Mux::get().unwrap(); let config = config::configuration(); - let fonts = Rc::new(FontConfiguration::new()); let window_id = mux.new_empty_window(); - let tab = mux + let _tab = mux .default_domain() - .spawn(config.initial_size(), None, None, window_id) + .spawn(config.initial_size(), None, None, *window_id) .await?; - let front_end = front_end().expect("to be called on gui thread"); - front_end.spawn_new_window(&fonts, &tab, window_id)?; Ok::<(), anyhow::Error>(()) } promise::spawn::spawn(async move { diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 250c79c09..483a931a2 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -1,8 +1,5 @@ -use crate::font::FontConfiguration; use anyhow::Error; use downcast_rs::{impl_downcast, Downcast}; -use mux::tab::Tab; -use mux::window::WindowId; use std::cell::RefCell; use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; @@ -61,12 +58,5 @@ pub trait FrontEnd: Downcast { /// Run the event loop. Does not return until there is either a fatal /// error, or until there are no more windows left to manage. fn run_forever(&self) -> anyhow::Result<()>; - - fn spawn_new_window( - &self, - fontconfig: &Rc, - tab: &Rc, - window_id: WindowId, - ) -> anyhow::Result<()>; } impl_downcast!(FrontEnd); diff --git a/src/frontend/muxserver/mod.rs b/src/frontend/muxserver/mod.rs index 8fa422951..1b55b1d78 100644 --- a/src/frontend/muxserver/mod.rs +++ b/src/frontend/muxserver/mod.rs @@ -1,12 +1,9 @@ //! Implements the multiplexer server frontend -use crate::font::FontConfiguration; use crate::frontend::FrontEnd; use crate::server::listener::spawn_listener; use anyhow::{bail, Error}; use crossbeam::channel::{unbounded as channel, Receiver}; use log::info; -use mux::tab::Tab; -use mux::window::WindowId; use mux::Mux; use promise::*; use std::rc::Rc; @@ -62,13 +59,4 @@ impl FrontEnd for MuxServerFrontEnd { } } } - - fn spawn_new_window( - &self, - _fontconfig: &Rc, - _tab: &Rc, - _window_id: WindowId, - ) -> anyhow::Result<()> { - Ok(()) - } } diff --git a/src/main.rs b/src/main.rs index 69f9256eb..8c01e910d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,6 @@ mod font; use crate::font::locator::FontLocatorSelection; use crate::font::rasterizer::FontRasterizerSelection; use crate::font::shaper::FontShaperSelection; -use crate::font::FontConfiguration; // let message = "; ❤ 😍🤢\n\x1b[91;mw00t\n\x1b[37;104;m bleet\x1b[0;m."; // terminal.advance_bytes(message); @@ -318,7 +317,7 @@ async fn async_run_ssh(opts: SshCommand) -> anyhow::Result<()> { let sess = ssh::async_ssh_connect(¶ms.host_and_port, ¶ms.username).await?; // Now we have a connected session, set up the ssh domain and make it // the default domain - let gui = front_end().unwrap(); + let _gui = front_end().unwrap(); let cmd = if !opts.prog.is_empty() { let builder = CommandBuilder::from_argv(opts.prog); @@ -340,11 +339,9 @@ async fn async_run_ssh(opts: SshCommand) -> anyhow::Result<()> { domain.attach().await?; let window_id = mux.new_empty_window(); - let tab = domain - .spawn(config.initial_size(), cmd, None, window_id) + let _tab = domain + .spawn(config.initial_size(), cmd, None, *window_id) .await?; - let fontconfig = Rc::new(FontConfiguration::new()); - gui.spawn_new_window(&fontconfig, &tab, window_id)?; Ok(()) } @@ -380,8 +377,6 @@ fn run_ssh(config: config::ConfigHandle, opts: SshCommand) -> anyhow::Result<()> } fn run_serial(config: config::ConfigHandle, opts: &SerialCommand) -> anyhow::Result<()> { - let fontconfig = Rc::new(FontConfiguration::new()); - let mut serial = portable_pty::serial::SerialTty::new(&opts.port); if let Some(baud) = opts.baud { serial.set_baud_rate(serial::BaudRate::from_speed(baud)); @@ -396,9 +391,11 @@ fn run_serial(config: config::ConfigHandle, opts: &SerialCommand) -> anyhow::Res let gui = crate::frontend::try_new(front_end)?; block_on(domain.attach())?; // FIXME: blocking - let window_id = mux.new_empty_window(); - let tab = block_on(domain.spawn(config.initial_size(), None, None, window_id))?; // FIXME: blocking - gui.spawn_new_window(&fontconfig, &tab, window_id)?; + { + let window_id = mux.new_empty_window(); + // FIXME: blocking + let _tab = block_on(domain.spawn(config.initial_size(), None, None, *window_id))?; + } maybe_show_configuration_error_window(); gui.run_forever() @@ -474,14 +471,10 @@ async fn spawn_tab_in_default_domain_if_mux_is_empty( let config = config::configuration(); let window_id = mux.new_empty_window(); - let tab = mux + let _tab = mux .default_domain() - .spawn(config.initial_size(), cmd, None, window_id) + .spawn(config.initial_size(), cmd, None, *window_id) .await?; - let fontconfig = Rc::new(FontConfiguration::new()); - front_end() - .unwrap() - .spawn_new_window(&fontconfig, &tab, window_id)?; Ok(()) } diff --git a/src/server/domain.rs b/src/server/domain.rs index 07f80e3f0..54e37cf0f 100644 --- a/src/server/domain.rs +++ b/src/server/domain.rs @@ -1,6 +1,4 @@ use crate::connui::ConnectionUI; -use crate::font::FontConfiguration; -use crate::frontend::front_end; use crate::server::client::Client; use crate::server::codec::{ListPanesResponse, Spawn, SplitPane}; use crate::server::tab::ClientPane; @@ -315,15 +313,9 @@ impl ClientDomain { window.push(&tab); } } else { - let fonts = Rc::new(FontConfiguration::new()); let local_window_id = mux.new_empty_window(); - inner.record_remote_to_local_window_mapping(remote_window_id, local_window_id); - mux.add_tab_to_window(&tab, local_window_id)?; - - front_end() - .unwrap() - .spawn_new_window(&fonts, &tab, local_window_id) - .unwrap(); + inner.record_remote_to_local_window_mapping(remote_window_id, *local_window_id); + mux.add_tab_to_window(&tab, *local_window_id)?; } } } diff --git a/src/server/listener/clientsession.rs b/src/server/listener/clientsession.rs index 123330d12..27664616f 100644 --- a/src/server/listener/clientsession.rs +++ b/src/server/listener/clientsession.rs @@ -57,7 +57,10 @@ impl ClientSession { match self.mux_rx.try_recv() { Ok(notif) => match notif { // Coalesce multiple TabOutputs for the same tab - MuxNotification::PaneOutput(pane_id) => panes_to_output.insert(pane_id), + MuxNotification::PaneOutput(pane_id) => { + panes_to_output.insert(pane_id); + } + MuxNotification::WindowCreated(_window_id) => {} }, Err(TryRecvError::Empty) => break, Err(TryRecvError::Disconnected) => bail!("mux_rx is Disconnected"), diff --git a/src/server/listener/sessionhandler.rs b/src/server/listener/sessionhandler.rs index c824e2eeb..0b69078b9 100644 --- a/src/server/listener/sessionhandler.rs +++ b/src/server/listener/sessionhandler.rs @@ -579,13 +579,15 @@ async fn domain_spawn(spawn: Spawn, sender: PollableSender) -> anyho let domain = mux .get_domain(spawn.domain_id) .ok_or_else(|| anyhow!("domain {} not found on this server", spawn.domain_id))?; + let window_builder; let window_id = if let Some(window_id) = spawn.window_id { mux.get_window_mut(window_id) .ok_or_else(|| anyhow!("window_id {} not found on this server", window_id))?; window_id } else { - mux.new_empty_window() + window_builder = mux.new_empty_window(); + *window_builder }; let tab = domain diff --git a/src/termwiztermtab.rs b/src/termwiztermtab.rs index c36682a86..9df99e5d2 100644 --- a/src/termwiztermtab.rs +++ b/src/termwiztermtab.rs @@ -3,8 +3,6 @@ //! input from the user as part of eg: setting up an ssh //! session. -use crate::font::FontConfiguration; -use crate::frontend::front_end; use anyhow::{bail, Error}; use async_trait::async_trait; use crossbeam::channel::{unbounded as channel, Receiver, Sender}; @@ -469,14 +467,9 @@ pub async fn run< tab.assign_pane(&pane); mux.add_tab_and_active_pane(&tab)?; - mux.add_tab_to_window(&tab, window_id)?; + mux.add_tab_to_window(&tab, *window_id)?; - let fontconfig = Rc::new(FontConfiguration::new()); - - let gui = front_end().unwrap(); - gui.spawn_new_window(&fontconfig, &tab, window_id)?; - - Ok(window_id) + Ok(*window_id) } let window_id: WindowId = promise::spawn::spawn_into_main_thread(async move {