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.
This commit is contained in:
Wez Furlong committed 2020-10-03 11:15:57 -07:00
1 parent a511abb1c2
commit 025732d00f
10 files changed
+79 -97

No files matched your search

+27 -2
View File
@@ -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<Option<Rc<Mux>>> = 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<Arc<dyn Domain>>) -> 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<Tab>, window_id: WindowId) -> anyhow::Result<()> {
+16 -13
View File
@@ -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<FontConfiguration>,
tab: &Rc<Tab>,
window_id: MuxWindowId,
) -> anyhow::Result<()> {
termwindow::TermWindow::new_window(&configuration(), fontconfig, tab, window_id)
}
}
+15 -22
View File
@@ -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<FontConfiguration>,
tab: &Rc<Tab>,
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<dyn wezterm_term::Clipboard> = 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 {
-10
View File
@@ -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<FontConfiguration>,
tab: &Rc<Tab>,
window_id: WindowId,
) -> anyhow::Result<()>;
}
impl_downcast!(FrontEnd);
-12
View File
@@ -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<FontConfiguration>,
_tab: &Rc<Tab>,
_window_id: WindowId,
) -> anyhow::Result<()> {
Ok(())
}
}
+10 -17
View File
@@ -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(&params.host_and_port, &params.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(())
}
+2 -10
View File
@@ -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)?;
}
}
}
+4 -1
View File
@@ -57,7 +57,10 @@ impl<S: ReadAndWrite> ClientSession<S> {
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"),
+3 -1
View File
@@ -579,13 +579,15 @@ async fn domain_spawn(spawn: Spawn, sender: PollableSender<DecodedPdu>) -> 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
+2 -9
View File
@@ -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 {