Skip to content

Commit

Permalink
plumb appearance change event -> reload on macos
Browse files Browse the repository at this point in the history
This commit causes a window-config-reloaded event to trigger
when the appearance (dark/light mode) is changed on macos.

It also arranges to propagate the window level config to newly
spawned panes and tabs, created both via the gui and via the
CLI/mux interface.

refs: #894
refs: #806
  • Loading branch information
wez committed Jul 18, 2021
1 parent daaa329 commit 1f5b900
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 22 deletions.
6 changes: 6 additions & 0 deletions config/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ impl TermConfig {
}
}

pub fn with_config(config: ConfigHandle) -> Self {
Self {
config: Mutex::new(Some(config)),
}
}

pub fn set_config(&self, config: ConfigHandle) {
self.config.lock().unwrap().replace(config);
}
Expand Down
10 changes: 7 additions & 3 deletions mux/src/localpane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{Domain, Mux, MuxNotification};
use anyhow::Error;
use async_trait::async_trait;
use config::keyassignment::ScrollbackEraseMode;
use config::{configuration, ExitBehavior, TermConfig};
use config::{configuration, ExitBehavior};
#[cfg(windows)]
use filedescriptor::OwnedHandle;
use portable_pty::{Child, ExitStatus, MasterPty, PtySize};
Expand All @@ -25,7 +25,7 @@ use url::Url;
use wezterm_term::color::ColorPalette;
use wezterm_term::{
Alert, AlertHandler, CellAttributes, Clipboard, KeyCode, KeyModifiers, MouseEvent,
SemanticZone, StableRowIndex, Terminal,
SemanticZone, StableRowIndex, Terminal, TerminalConfiguration,
};

#[derive(Debug)]
Expand Down Expand Up @@ -202,10 +202,14 @@ impl Pane for LocalPane {
self.terminal.borrow_mut().set_clipboard(clipboard);
}

fn set_config(&self, config: Arc<TermConfig>) {
fn set_config(&self, config: Arc<dyn TerminalConfiguration>) {
self.terminal.borrow_mut().set_config(config);
}

fn get_config(&self) -> Option<Arc<dyn TerminalConfiguration>> {
Some(self.terminal.borrow().get_config())
}

fn perform_actions(&self, actions: Vec<termwiz::escape::Action>) {
self.terminal.borrow_mut().perform_actions(actions)
}
Expand Down
11 changes: 8 additions & 3 deletions mux/src/pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::renderable::*;
use crate::Mux;
use async_trait::async_trait;
use config::keyassignment::ScrollbackEraseMode;
use config::TermConfig;
use downcast_rs::{impl_downcast, Downcast};
use portable_pty::PtySize;
use rangeset::RangeSet;
Expand All @@ -16,7 +15,10 @@ use termwiz::hyperlink::Rule;
use termwiz::surface::Line;
use url::Url;
use wezterm_term::color::ColorPalette;
use wezterm_term::{Clipboard, KeyCode, KeyModifiers, MouseEvent, SemanticZone, StableRowIndex};
use wezterm_term::{
Clipboard, KeyCode, KeyModifiers, MouseEvent, SemanticZone, StableRowIndex,
TerminalConfiguration,
};

static PANE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
pub type PaneId = usize;
Expand Down Expand Up @@ -341,7 +343,10 @@ pub trait Pane: Downcast {
fn is_alt_screen_active(&self) -> bool;

fn set_clipboard(&self, _clipboard: &Arc<dyn Clipboard>) {}
fn set_config(&self, _config: Arc<TermConfig>) {}
fn set_config(&self, _config: Arc<dyn TerminalConfiguration>) {}
fn get_config(&self) -> Option<Arc<dyn TerminalConfiguration>> {
None
}

fn get_current_working_dir(&self) -> Option<Url>;

Expand Down
4 changes: 4 additions & 0 deletions term/src/terminalstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ impl TerminalState {
self.config = config;
}

pub fn get_config(&self) -> Arc<dyn TerminalConfiguration> {
Arc::clone(&self.config)
}

pub fn set_clipboard(&mut self, clipboard: &Arc<dyn Clipboard>) {
self.clipboard.replace(Arc::clone(clipboard));
}
Expand Down
24 changes: 21 additions & 3 deletions wezterm-gui/src/overlay/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use crate::termwindow::clipboard::ClipboardHelper;
use crate::termwindow::spawn::SpawnWhere;
use crate::termwindow::TermWindow;
use anyhow::anyhow;
use config::configuration;
use config::keyassignment::{SpawnCommand, SpawnTabDomain};
use config::{configuration, TermConfig};
use mux::domain::{DomainId, DomainState};
use mux::tab::TabId;
use mux::termwiztermtab::TermWizTerminal;
use mux::window::WindowId;
use mux::Mux;
use portable_pty::PtySize;
use std::sync::Arc;
use termwiz::cell::{AttributeChange, CellAttributes};
use termwiz::color::ColorAttribute;
use termwiz::input::{InputEvent, KeyCode, KeyEvent, MouseButtons, MouseEvent};
Expand Down Expand Up @@ -106,6 +107,7 @@ pub fn launcher(
domains: Vec<(DomainId, String, DomainState, String)>,
clipboard: ClipboardHelper,
size: PtySize,
term_config: Arc<TermConfig>,
) -> anyhow::Result<()> {
let mut active_idx = 0;
let mut entries = vec![];
Expand Down Expand Up @@ -205,6 +207,7 @@ pub fn launcher(
size: PtySize,
mux_window_id: WindowId,
clipboard: ClipboardHelper,
term_config: Arc<TermConfig>,
) {
match entries[active_idx].clone() {
Entry::Spawn {
Expand All @@ -219,6 +222,7 @@ pub fn launcher(
size,
mux_window_id,
clipboard,
term_config,
);
})
.detach();
Expand Down Expand Up @@ -270,7 +274,14 @@ pub fn launcher(
active_idx = y as usize - 1;

if mouse_buttons == MouseButtons::LEFT {
launch(active_idx, &entries, size, mux_window_id, clipboard);
launch(
active_idx,
&entries,
size,
mux_window_id,
clipboard,
term_config,
);
break;
}
}
Expand All @@ -283,7 +294,14 @@ pub fn launcher(
key: KeyCode::Enter,
..
}) => {
launch(active_idx, &entries, size, mux_window_id, clipboard);
launch(
active_idx,
&entries,
size,
mux_window_id,
clipboard,
term_config,
);
break;
}
_ => {}
Expand Down
5 changes: 4 additions & 1 deletion wezterm-gui/src/scripting/guiwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use mlua::{UserData, UserDataMethods};
use mux::window::WindowId as MuxWindowId;
use serde::*;
use wezterm_toast_notification::ToastNotification;
use window::WindowOps;
use window::{Connection, ConnectionOps, WindowOps};

#[derive(Clone)]
pub struct GuiWin {
Expand Down Expand Up @@ -43,6 +43,9 @@ impl UserData for GuiWin {
Ok(())
},
);
methods.add_method("get_appearance", |_, _, _: ()| {
Ok(Connection::get().unwrap().get_appearance().to_string())
});
methods.add_method("set_right_status", |_, this, status: String| {
this.window.notify(TermWindowNotif::SetRightStatus(status));
Ok(())
Expand Down
12 changes: 9 additions & 3 deletions wezterm-gui/src/termwindow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ impl TermWindow {
myself.gl.replace(Rc::clone(&gl));
myself.created(&window, Rc::clone(&gl))?;
myself.subscribe_to_pane_updates();
myself.emit_window_event("window-config-reloaded");
myself.emit_status_event();
}

Expand All @@ -545,6 +546,10 @@ impl TermWindow {
self.close_requested(window);
Ok(true)
}
WindowEvent::AppearanceChanged(_appearance) => {
self.config_was_reloaded();
Ok(true)
}
WindowEvent::FocusChanged(focused) => {
self.focus_changed(focused, window);
Ok(true)
Expand Down Expand Up @@ -1012,9 +1017,8 @@ impl TermWindow {
}

if let Some(window) = mux.get_window(self.mux_window_id) {
let term_config = TermConfig::new();
term_config.set_config(config.clone());
let term_config = Arc::new(term_config);
let term_config: Arc<dyn TerminalConfiguration> =
Arc::new(TermConfig::with_config(config.clone()));
for tab in window.iter() {
for pane in tab.iter_panes() {
pane.pane.set_config(Arc::clone(&term_config));
Expand Down Expand Up @@ -1413,6 +1417,7 @@ impl TermWindow {
.expect("tab has no panes!")
.domain_id();
let size = self.terminal_size;
let term_config = Arc::new(TermConfig::with_config(self.config.clone()));

let (overlay, future) = start_overlay(self, &tab, move |tab_id, term| {
launcher(
Expand All @@ -1423,6 +1428,7 @@ impl TermWindow {
domains,
clipboard,
size,
term_config,
)
});
self.assign_overlay(tab.tab_id(), overlay);
Expand Down
22 changes: 18 additions & 4 deletions wezterm-gui/src/termwindow/spawn.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::termwindow::{ClipboardHelper, MuxWindowId};
use anyhow::{anyhow, bail};
use config::keyassignment::{SpawnCommand, SpawnTabDomain};
use config::TermConfig;
use mux::activity::Activity;
use mux::domain::DomainState;
use mux::tab::SplitDirection;
Expand All @@ -23,6 +24,8 @@ impl super::TermWindow {
} else {
self.terminal_size
};
let term_config = Arc::new(TermConfig::with_config(self.config.clone()));

Self::spawn_command_impl(
spawn,
spawn_where,
Expand All @@ -31,6 +34,7 @@ impl super::TermWindow {
ClipboardHelper {
window: self.window.as_ref().unwrap().clone(),
},
term_config,
)
}

Expand All @@ -40,13 +44,20 @@ impl super::TermWindow {
size: PtySize,
src_window_id: MuxWindowId,
clipboard: ClipboardHelper,
term_config: Arc<TermConfig>,
) {
let spawn = spawn.clone();

promise::spawn::spawn(async move {
if let Err(err) =
Self::spawn_command_internal(spawn, spawn_where, size, src_window_id, clipboard)
.await
if let Err(err) = Self::spawn_command_internal(
spawn,
spawn_where,
size,
src_window_id,
clipboard,
term_config,
)
.await
{
log::error!("Failed to spawn: {:#}", err);
}
Expand All @@ -60,6 +71,7 @@ impl super::TermWindow {
size: PtySize,
src_window_id: MuxWindowId,
clipboard: ClipboardHelper,
term_config: Arc<TermConfig>,
) -> anyhow::Result<()> {
let mux = Mux::get().unwrap();
let activity = Activity::new();
Expand Down Expand Up @@ -173,9 +185,10 @@ impl super::TermWindow {
let pane = domain
.split_pane(cmd_builder, cwd, tab.tab_id(), pane.pane_id(), direction)
.await?;
pane.set_config(term_config);
pane.set_clipboard(&clipboard);
} else {
log::error!("there is no active tab while splitting pane!?");
bail!("there is no active tab while splitting pane!?");
}
}
_ => {
Expand All @@ -186,6 +199,7 @@ impl super::TermWindow {
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("newly spawned tab to have a pane"))?;
pane.set_config(term_config);

if spawn_where != SpawnWhere::NewWindow {
pane.set_clipboard(&clipboard);
Expand Down
25 changes: 24 additions & 1 deletion wezterm-mux-server-impl/src/sessionhandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,11 @@ async fn split_pane(split: SplitPane, sender: PduSender) -> anyhow::Result<Pdu>
};

let pane_id = split.pane_id;
let current_pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("pane_id {} is invalid", pane_id))?;
let term_config = current_pane.get_config();

let cwd = split.command_dir.or_else(|| {
mux.get_pane(pane_id)
.and_then(|pane| pane.get_current_working_dir())
Expand Down Expand Up @@ -686,6 +691,9 @@ async fn split_pane(split: SplitPane, sender: PduSender) -> anyhow::Result<Pdu>
sender,
});
pane.set_clipboard(&clip);
if let Some(config) = term_config {
pane.set_config(config);
}

Ok::<Pdu, anyhow::Error>(Pdu::SpawnResponse(SpawnResponse {
pane_id: pane.pane_id(),
Expand Down Expand Up @@ -745,12 +753,23 @@ async fn domain_spawn_v2(spawn: SpawnV2, sender: PduSender) -> anyhow::Result<Pd
};

let window_builder;
let term_config;

let window_id = if let Some(window_id) = spawn.window_id {
mux.get_window_mut(window_id)
let window = mux
.get_window_mut(window_id)
.ok_or_else(|| anyhow!("window_id {} not found on this server", window_id))?;
let tab = window
.get_active()
.ok_or_else(|| anyhow!("window {} has no tabs", window_id))?;
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("active tab in window {} has no panes", window_id))?;
term_config = pane.get_config();

window_id
} else {
term_config = None;
window_builder = mux.new_empty_window();
*window_builder
};
Expand All @@ -763,6 +782,10 @@ async fn domain_spawn_v2(spawn: SpawnV2, sender: PduSender) -> anyhow::Result<Pd
.get_active_pane()
.ok_or_else(|| anyhow!("missing active pane on tab!?"))?;

if let Some(config) = term_config {
pane.set_config(config);
}

let clip: Arc<dyn Clipboard> = Arc::new(RemoteClipboard {
pane_id: pane.pane_id(),
sender,
Expand Down
4 changes: 3 additions & 1 deletion window/examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ impl MyWindow {
win.finish_frame(frame).unwrap();
}
}
WindowEvent::Notification(_) | WindowEvent::FocusChanged(_) => {}
WindowEvent::AppearanceChanged(_)
| WindowEvent::Notification(_)
| WindowEvent::FocusChanged(_) => {}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion window/examples/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ impl MyWindow {
WindowEvent::NeedRepaint => {
self.paint(win).unwrap();
}
WindowEvent::Notification(_) | WindowEvent::FocusChanged(_) => {}
WindowEvent::AppearanceChanged(_)
| WindowEvent::Notification(_)
| WindowEvent::FocusChanged(_) => {}
}
}
}
Expand Down
Loading

0 comments on commit 1f5b900

Please sign in to comment.