Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

fix(gui): stop sidebar toggles from reloading the config

RoversXe91b0a146a9d
diff --git a/wezterm-gui/src/frontend.rs b/wezterm-gui/src/frontend.rs
index 8326e738..6e6fc961 100644
--- a/wezterm-gui/src/frontend.rs
+++ b/wezterm-gui/src/frontend.rs
@@ -225,6 +225,7 @@ impl GuiFrontEnd {
pub fn try_new() -> anyhow::Result<Rc<GuiFrontEnd>> {
let connection = Connection::init()?;
connection.set_event_handler(Self::app_event_handler);
+ config::set_reload_filter(crate::native_settings::watched_change_needs_reload);
crate::native_settings::apply_to_app(&crate::native_settings::load());
spawn_malloc_pressure_relief_thread();
diff --git a/wezterm-gui/src/native_settings.rs b/wezterm-gui/src/native_settings.rs
index d46b6b96..21e5ded2 100644
--- a/wezterm-gui/src/native_settings.rs
+++ b/wezterm-gui/src/native_settings.rs
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs;
use std::io;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock};
use window::{Appearance, Connection, ConnectionOps, WindowOps};
@@ -839,11 +839,12 @@ pub(crate) fn effective_remote_download_directory() -> Option<PathBuf> {
}
pub(crate) fn set_remote_download_directory(path: Option<PathBuf>) -> anyhow::Result<()> {
- let mut settings = load();
- settings.workspaces.remote_download_directory = path
- .map(|path| path.to_string_lossy().to_string())
- .unwrap_or_default();
- save(&settings)
+ update(|settings| {
+ settings.workspaces.remote_download_directory = path
+ .map(|path| path.to_string_lossy().to_string())
+ .unwrap_or_default();
+ })
+ .map(drop)
}
/// Default landing folder for files dropped onto a remote terminal. Visible
@@ -868,52 +869,74 @@ pub(crate) fn remote_drop_destination() -> String {
}
pub(crate) fn set_remote_drop_destination(value: &str) -> anyhow::Result<()> {
- let mut settings = load();
let trimmed = value.trim();
// Storing the default as emptiness keeps the file clean and lets a future
// default change reach everyone who never made a choice.
- settings.workspaces.remote_drop_destination =
- if trimmed.is_empty() || trimmed == DEFAULT_REMOTE_DROP_DESTINATION {
- String::new()
- } else {
- trimmed.to_string()
- };
- save(&settings)
+ let destination = if trimmed.is_empty() || trimmed == DEFAULT_REMOTE_DROP_DESTINATION {
+ String::new()
+ } else {
+ trimmed.to_string()
+ };
+ update(|settings| settings.workspaces.remote_drop_destination = destination).map(drop)
}
pub(crate) fn settings_path() -> PathBuf {
- config::HOME_DIR
- .join(".config")
- .join("thinkterm")
- .join("settings.json")
+ config::native_settings_path()
+}
+
+/// The settings in use, and the ledger that keeps them in step with the
+/// file. Made together, from one read of the file.
+struct SettingsStore {
+ settings: Mutex<Arc<ThinkTermNativeSettings>>,
+ ledger: Mutex<WriteLedger>,
}
-static SETTINGS_CACHE: OnceLock<Mutex<Arc<ThinkTermNativeSettings>>> = OnceLock::new();
+static SETTINGS_STORE: OnceLock<SettingsStore> = OnceLock::new();
+
+fn settings_store() -> &'static SettingsStore {
+ SETTINGS_STORE.get_or_init(|| {
+ let (settings, known) = load_from_disk();
+ SettingsStore {
+ settings: Mutex::new(Arc::new(settings)),
+ ledger: Mutex::new(WriteLedger {
+ known,
+ reload_owed: false,
+ }),
+ }
+ })
+}
fn settings_cache() -> &'static Mutex<Arc<ThinkTermNativeSettings>> {
- SETTINGS_CACHE.get_or_init(|| Mutex::new(Arc::new(load_from_disk())))
+ &settings_store().settings
}
-fn load_from_disk() -> ThinkTermNativeSettings {
+/// The settings on disk, and the fingerprint of the file they were read
+/// from.
+fn load_from_disk() -> (ThinkTermNativeSettings, Option<u64>) {
let path = settings_path();
- match fs::read_to_string(&path) {
- Ok(data) => match serde_json::from_str(&data) {
- Ok(settings) => settings,
- Err(err) => {
- log::warn!(
- "Unable to parse ThinkTerm native settings {}: {err:#}",
- path.display()
- );
- ThinkTermNativeSettings::default()
- }
- },
- Err(err) if err.kind() == io::ErrorKind::NotFound => ThinkTermNativeSettings::for_new_install(),
+ match fs::read(&path) {
+ Ok(data) => {
+ let settings = match serde_json::from_slice(&data) {
+ Ok(settings) => settings,
+ Err(err) => {
+ log::warn!(
+ "Unable to parse ThinkTerm native settings {}: {err:#}",
+ path.display()
+ );
+ ThinkTermNativeSettings::default()
+ }
+ };
+ (settings, Some(fingerprint(&data)))
+ }
+ Err(err) if err.kind() == io::ErrorKind::NotFound => {
+ (ThinkTermNativeSettings::for_new_install(), None)
+ }
Err(err) => {
log::warn!(
"Unable to read ThinkTerm native settings {}: {err:#}",
path.display()
);
- ThinkTermNativeSettings::default()
+ (ThinkTermNativeSettings::default(), None)
}
}
}
@@ -936,23 +959,289 @@ pub(crate) fn should_show_onboarding(settings: &ThinkTermNativeSettings) -> bool
settings.onboarding.seen_version < ONBOARDING_VERSION
}
+/// The settings in use, after taking in settings.json if it was changed
+/// outside ThinkTerm. For the Settings window as it opens.
pub(crate) fn reload_from_disk() -> ThinkTermNativeSettings {
- let settings = load_from_disk();
- *settings_cache().lock() = Arc::new(settings.clone());
- settings
+ take_in_disk_changes(&mut write_ledger().lock(), &settings_path());
+ load()
}
+/// Save a whole copy of the settings, such as the one the Settings window
+/// keeps. The config watcher sees the file change and reloads the
+/// configuration, as it always has for a setting. One setting is better
+/// changed with `update`, which cannot write an older copy over an edit
+/// made outside ThinkTerm.
pub(crate) fn save(settings: &ThinkTermNativeSettings) -> anyhow::Result<()> {
+ let mut ledger = write_ledger().lock();
+ if write_locked(&mut ledger, settings.clone())?.is_some() {
+ ledger.reload_owed = true;
+ }
+ Ok(())
+}
+
+/// Change a setting and save it. The change is made to the latest settings:
+/// an edit made outside ThinkTerm that the config watcher has not got to
+/// yet is taken in first, not written over. Returns the settings saved.
+pub(crate) fn update(
+ edit: impl FnOnce(&mut ThinkTermNativeSettings),
+) -> anyhow::Result<Arc<ThinkTermNativeSettings>> {
+ update_with(edit, true)
+}
+
+/// Change and save state the app keeps for itself rather than a setting:
+/// where a sidebar edge was dropped, whether a panel is open, where the
+/// window is, what the plugin host reported. Made to the latest settings,
+/// as `update` is.
+/// The window that changed it has applied it already and nothing a
+/// configuration reload does reads it, so the config watcher does not reload
+/// for it. A reload drops every loaded font, and CJK text drew as
+/// missing-glyph boxes until the fallback fonts were found again; it also
+/// closed the command palette and reset key tables. That was every sidebar
+/// toggle.
+pub(crate) fn update_ui_state(edit: impl FnOnce(&mut ThinkTermNativeSettings)) -> anyhow::Result<()> {
+ update_with(edit, false).map(drop)
+}
+
+fn update_with(
+ edit: impl FnOnce(&mut ThinkTermNativeSettings),
+ setting: bool,
+) -> anyhow::Result<Arc<ThinkTermNativeSettings>> {
+ let mut ledger = write_ledger().lock();
+ take_in_disk_changes(&mut ledger, &settings_path());
+ let mut settings = load();
+ edit(&mut settings);
+ match write_locked(&mut ledger, settings)? {
+ Some(saved) => {
+ ledger.reload_owed |= setting;
+ Ok(saved)
+ }
+ None => Ok(load_shared()),
+ }
+}
+
+/// Write `settings` and make them the settings in use, with the ledger held
+/// so the watcher never compares the file with a write it has no record of.
+/// `None` when the file holds them already and nothing was written.
+fn write_locked(
+ ledger: &mut WriteLedger,
+ settings: ThinkTermNativeSettings,
+) -> anyhow::Result<Option<Arc<ThinkTermNativeSettings>>> {
let path = settings_path();
+ let data = serde_json::to_vec_pretty(&settings)?;
+ let written = Some(fingerprint(&data));
+ if written == ledger.known {
+ return Ok(None);
+ }
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
- let data = serde_json::to_vec_pretty(settings)?;
let tmp = path.with_extension("json.tmp");
- fs::write(&tmp, data)?;
+ fs::write(&tmp, &data)?;
fs::rename(tmp, path)?;
- *settings_cache().lock() = Arc::new(settings.clone());
- Ok(())
+ ledger.known = written;
+ let settings = Arc::new(settings);
+ *settings_cache().lock() = Arc::clone(&settings);
+ Ok(Some(settings))
+}
+
+/// Where the settings in use stand against settings.json, so a change to
+/// the file by this process can be told from anybody else's. Every read and
+/// write of the file holds it.
+struct WriteLedger {
+ /// The fingerprint of the file as this process last wrote or read it.
+ known: Option<u64>,
+ /// A setting was saved, or taken in from the file, since the watcher
+ /// last looked, and is owed the configuration reload a setting has
+ /// always had.
+ reload_owed: bool,
+}
+
+fn write_ledger() -> &'static Mutex<WriteLedger> {
+ &settings_store().ledger
+}
+
+fn fingerprint(data: &[u8]) -> u64 {
+ use std::hash::{Hash, Hasher};
+ let mut hasher = std::collections::hash_map::DefaultHasher::new();
+ data.hash(&mut hasher);
+ hasher.finish()
+}
+
+/// The config watcher's reload filter (`config::set_reload_filter`).
+///
+/// A change to anything but settings.json reloads, as before. A write of
+/// settings.json by this process reloads only if it saved a setting rather
+/// than state of its own. A write by anybody else (an editor, another
+/// process) is taken in first: without that the settings in use stayed the
+/// old ones until the Settings window was opened, and the next save here
+/// wrote them back over the edit.
+pub(crate) fn watched_change_needs_reload(paths: &[PathBuf]) -> bool {
+ let settings = settings_path();
+ let ours = paths
+ .iter()
+ .filter(|path| config::is_native_settings_file(path, &settings))
+ .count();
+ let mut ledger = write_ledger().lock();
+ let (reload, external) = watched_change(ours > 0, ours < paths.len(), &settings, &mut ledger);
+ if let Some(data) = external {
+ take_in_external_settings(&data);
+ }
+ reload
+}
+
+/// Whether a watched change calls for a reload: `settings` was among the
+/// changed files or not, and other files were or not. Also the new contents
+/// of `settings` when somebody other than this process wrote it.
+fn watched_change(
+ settings_changed: bool,
+ others_changed: bool,
+ settings: &Path,
+ ledger: &mut WriteLedger,
+) -> (bool, Option<Vec<u8>>) {
+ if !settings_changed {
+ return (others_changed, None);
+ }
+ let external = external_change(ledger, settings);
+ (
+ others_changed || std::mem::take(&mut ledger.reload_owed),
+ external,
+ )
+}
+
+/// The contents of `settings` if somebody other than this process changed
+/// it since this process last wrote or read it. The change becomes known,
+/// and is owed the configuration reload a setting has; a file that is gone
+/// has nothing to take in but is owed the reload too.
+fn external_change(ledger: &mut WriteLedger, settings: &Path) -> Option<Vec<u8>> {
+ let data = fs::read(settings).ok();
+ let known = data.as_deref().map(fingerprint);
+ if known == ledger.known {
+ return None;
+ }
+ ledger.known = known;
+ ledger.reload_owed = true;
+ data
+}
+
+fn take_in_disk_changes(ledger: &mut WriteLedger, settings: &Path) {
+ if let Some(data) = external_change(ledger, settings) {
+ take_in_external_settings(&data);
+ }
+}
+
+/// Settings written by somebody else become the ones in use, and the app is
+/// brought in line with them on the GUI thread. A file that does not parse
+/// leaves the settings in use alone. Called with the ledger held.
+fn take_in_external_settings(data: &[u8]) {
+ let after: ThinkTermNativeSettings = match serde_json::from_slice(data) {
+ Ok(settings) => settings,
+ Err(err) => {
+ log::warn!(
+ "{} changed but cannot be read; keeping the settings in use: {err:#}",
+ settings_path().display()
+ );
+ return;
+ }
+ };
+ let before = std::mem::replace(&mut *settings_cache().lock(), Arc::new(after));
+ log::info!("{} changed outside ThinkTerm; applying it", settings_path().display());
+ promise::spawn::spawn_into_main_thread(async move {
+ // Against the settings in use by the time this runs: a save made in
+ // between was applied by whoever made it, and is what stays.
+ apply_external_change(&before, &load_shared());
+ })
+ .detach();
+}
+
+/// Bring the running app in line with settings that changed on disk, the
+/// way the Settings window does for each change it makes; the configuration
+/// reload a setting is owed follows, as it does for a save there. Most
+/// settings are read where they are used and only need a repaint.
+/// `local_sessions_via_mux` and `main_renderer` take effect at the next
+/// start, as they do from the Settings window. Window state (sidebar widths
+/// and the like) is picked up by the next window, as it is when another
+/// window changes it.
+fn apply_external_change(before: &ThinkTermNativeSettings, after: &ThinkTermNativeSettings) {
+ let language = before.localization.language != after.localization.language
+ || before.onboarding.language != after.onboarding.language;
+ if language
+ || before.appearance.theme_mode != after.appearance.theme_mode
+ || before.appearance.app_icon != after.appearance.app_icon
+ || before.appearance.color_scheme != after.appearance.color_scheme
+ {
+ // Language, theme, colour scheme, chrome and app icon.
+ apply_to_app(after);
+ }
+ crate::settings_window::follow_open_settings_window(before, after);
+
+ let font_size = (before.terminal.font_size != after.terminal.font_size)
+ .then_some(after.terminal.font_size);
+ let contrast = before.terminal.text_contrast != after.terminal.text_contrast;
+ let before_chrome = &before.chrome;
+ let after_chrome = &after.chrome;
+ let panels = before_chrome.right_sidebar_files_enabled != after_chrome.right_sidebar_files_enabled
+ || before_chrome.right_sidebar_notes_enabled != after_chrome.right_sidebar_notes_enabled
+ || before_chrome.right_sidebar_snippets_enabled
+ != after_chrome.right_sidebar_snippets_enabled
+ || before_chrome.snippets_plugin_enabled != after_chrome.snippets_plugin_enabled
+ || before_chrome.agent_panel_enabled != after_chrome.agent_panel_enabled
+ || before_chrome.plugin_panels != after_chrome.plugin_panels;
+ if before_chrome.agent_panel_enabled != after_chrome.agent_panel_enabled {
+ mux::agent_status::refresh_enabled();
+ }
+ if let Some(front_end) = crate::frontend::try_front_end() {
+ for gui_window in front_end.gui_windows() {
+ gui_window
+ .window
+ .notify(crate::termwindow::TermWindowNotif::Apply(Box::new(
+ move |term_window| {
+ if let Some(font_size) = font_size {
+ // As the Settings window applies it; no size is
+ // the configured one.
+ let base = term_window.config.font_size;
+ let font_size = font_size.unwrap_or(base);
+ if font_size.is_finite() && font_size > 0.0 && base > 0.0 {
+ let font_scale = (font_size / base).clamp(0.25, 4.0);
+ if let Some(window) = term_window.window.as_ref().cloned() {
+ term_window.adjust_font_scale(font_scale, &window);
+ }
+ }
+ }
+ if contrast {
+ term_window.refresh_text_min_contrast();
+ }
+ if panels {
+ term_window.right_sidebar_panels_changed();
+ }
+ if language {
+ term_window.dismiss_fallback_context_menu();
+ }
+ },
+ )));
+ }
+ front_end.invalidate_all_windows();
+ }
+
+ if before.web.reachable != after.web.reachable {
+ crate::web_settings::follow_reachable();
+ }
+ if before_chrome.snippets_plugin_enabled != after_chrome.snippets_plugin_enabled {
+ if let Some(enabled) = after_chrome.snippets_plugin_enabled {
+ crate::plugins::set_enabled(thinkterm_snippets::wire::PLUGIN, enabled);
+ }
+ }
+ if before.workspaces.remote_sftp_idle_minutes != after.workspaces.remote_sftp_idle_minutes {
+ crate::termwindow::remote_files::update_remote_connection_idle_timeout(
+ after.workspaces.remote_sftp_idle_minutes,
+ );
+ }
+ if before.workspaces.remote_update_keeps_sessions
+ != after.workspaces.remote_update_keeps_sessions
+ {
+ wezterm_client::remote_update::set_keep_sessions_on_update(
+ after.workspaces.remote_update_keeps_sessions,
+ );
+ }
}
/// Where the main window should reopen, or `None` if nothing has been
@@ -977,12 +1266,12 @@ pub(crate) fn restore_main_window_frame_enabled() -> bool {
/// into one call, and a drag that ends where it started should not rewrite
/// the file.
pub(crate) fn save_main_window_placement(placement: NativeMainWindowPlacement) {
- let mut settings = load();
- if settings.window.main_window_placement == Some(placement) {
+ if load_shared().window.main_window_placement == Some(placement) {
return;
}
- settings.window.main_window_placement = Some(placement);
- if let Err(err) = save(&settings) {
+ if let Err(err) =
+ update_ui_state(|settings| settings.window.main_window_placement = Some(placement))
+ {
log::warn!("failed to save main window placement: {err:#}");
}
}
@@ -990,12 +1279,10 @@ pub(crate) fn save_main_window_placement(placement: NativeMainWindowPlacement) {
/// Persist the palette-picked color scheme so new windows and the next
/// launch start with it. A no-op when the stored value already matches.
pub(crate) fn save_color_scheme(name: Option<String>) {
- let mut settings = load();
- if settings.appearance.color_scheme == name {
+ if load_shared().appearance.color_scheme == name {
return;
}
- settings.appearance.color_scheme = name;
- if let Err(err) = save(&settings) {
+ if let Err(err) = update(|settings| settings.appearance.color_scheme = name) {
log::error!("failed to save color scheme choice: {err:#}");
}
}
@@ -1401,9 +1688,7 @@ pub(crate) fn workspace_sidebar_width() -> Option<usize> {
}
pub(crate) fn save_workspace_sidebar_width(width: usize) -> anyhow::Result<()> {
- let mut settings = load();
- settings.chrome.workspace_sidebar_width = Some(width);
- save(&settings)
+ update_ui_state(|settings| settings.chrome.workspace_sidebar_width = Some(width))
}
/// Remember whether the workspace sidebar is open, so a new window starts the
@@ -1414,12 +1699,12 @@ pub(crate) fn save_workspace_sidebar_width(width: usize) -> anyhow::Result<()> {
/// sidebar was forgotten on the next window, and the setting had no home once
/// the wizard stopped asking about it.
pub(crate) fn save_workspace_sidebar_shown(shown: bool) {
- let mut settings = load();
- if settings.onboarding.show_left_sidebar_by_default == shown {
+ if load_shared().onboarding.show_left_sidebar_by_default == shown {
return;
}
- settings.onboarding.show_left_sidebar_by_default = shown;
- if let Err(err) = save(&settings) {
+ if let Err(err) =
+ update_ui_state(|settings| settings.onboarding.show_left_sidebar_by_default = shown)
+ {
log::warn!("failed to save workspace sidebar visibility: {err:#}");
}
}
@@ -1501,9 +1786,7 @@ pub(crate) fn workspace_sidebar_hidden_statuses() -> Vec<String> {
}
pub(crate) fn save_workspace_sidebar_hidden_statuses(hidden: Vec<String>) -> anyhow::Result<()> {
- let mut settings = load();
- settings.chrome.workspace_sidebar_hidden_statuses = hidden;
- save(&settings)
+ update_ui_state(|settings| settings.chrome.workspace_sidebar_hidden_statuses = hidden)
}
pub(crate) fn force_fallback_context_menu() -> bool {
@@ -1512,15 +1795,11 @@ pub(crate) fn force_fallback_context_menu() -> bool {
}
pub(crate) fn save_right_sidebar_width(width: usize) -> anyhow::Result<()> {
- let mut settings = load();
- settings.chrome.right_sidebar_width = Some(width);
- save(&settings)
+ update_ui_state(|settings| settings.chrome.right_sidebar_width = Some(width))
}
pub(crate) fn save_right_sidebar_file_preview_width(width: usize) -> anyhow::Result<()> {
- let mut settings = load();
- settings.chrome.right_sidebar_file_preview_width = Some(width);
- save(&settings)
+ update_ui_state(|settings| settings.chrome.right_sidebar_file_preview_width = Some(width))
}
pub(crate) fn right_sidebar_markdown_preview_rendered() -> bool {
@@ -1531,9 +1810,9 @@ pub(crate) fn right_sidebar_markdown_preview_rendered() -> bool {
}
pub(crate) fn save_right_sidebar_markdown_preview_rendered(rendered: bool) -> anyhow::Result<()> {
- let mut settings = load();
- settings.chrome.right_sidebar_markdown_preview_rendered = Some(rendered);
- save(&settings)
+ update_ui_state(|settings| {
+ settings.chrome.right_sidebar_markdown_preview_rendered = Some(rendered)
+ })
}
/// How wide the user made plugin `plugin`'s extended view.
@@ -1546,30 +1825,24 @@ pub(crate) fn plugin_extended_width(plugin: &str) -> Option<usize> {
}
pub(crate) fn save_plugin_extended_width(plugin: &str, width: usize) -> anyhow::Result<()> {
- let mut settings = load();
- settings
- .chrome
- .plugin_extended_widths
- .insert(plugin.to_string(), width);
- save(&settings)
+ update_ui_state(|settings| {
+ settings
+ .chrome
+ .plugin_extended_widths
+ .insert(plugin.to_string(), width);
+ })
}
pub(crate) fn save_right_sidebar_note_pane_width(width: usize) -> anyhow::Result<()> {
- let mut settings = load();
- settings.chrome.right_sidebar_note_pane_width = Some(width);
- save(&settings)
+ update_ui_state(|settings| settings.chrome.right_sidebar_note_pane_width = Some(width))
}
pub(crate) fn save_right_sidebar_note_pane_expanded(expanded: bool) -> anyhow::Result<()> {
- let mut settings = load();
- settings.chrome.right_sidebar_note_pane_expanded = Some(expanded);
- save(&settings)
+ update_ui_state(|settings| settings.chrome.right_sidebar_note_pane_expanded = Some(expanded))
}
pub(crate) fn save_right_sidebar_open_with_app(app: NativeOpenWithApp) -> anyhow::Result<()> {
- let mut settings = load();
- settings.chrome.right_sidebar_open_with_app = Some(app);
- save(&settings)
+ update_ui_state(|settings| settings.chrome.right_sidebar_open_with_app = Some(app))
}
pub(crate) fn right_sidebar_custom_open_with_apps() -> Vec<NativeOpenWithApp> {
@@ -1579,14 +1852,14 @@ pub(crate) fn right_sidebar_custom_open_with_apps() -> Vec<NativeOpenWithApp> {
const MAX_CUSTOM_OPEN_WITH_APPS: usize = 20;
pub(crate) fn add_right_sidebar_custom_open_with_app(app: NativeOpenWithApp) -> anyhow::Result<()> {
- let mut settings = load();
- let apps = &mut settings.chrome.right_sidebar_custom_open_with_apps;
- apps.retain(|existing| existing.id != app.id);
- apps.push(app);
- while apps.len() > MAX_CUSTOM_OPEN_WITH_APPS {
- apps.remove(0);
- }
- save(&settings)
+ update_ui_state(|settings| {
+ let apps = &mut settings.chrome.right_sidebar_custom_open_with_apps;
+ apps.retain(|existing| existing.id != app.id);
+ apps.push(app);
+ while apps.len() > MAX_CUSTOM_OPEN_WITH_APPS {
+ apps.remove(0);
+ }
+ })
}
pub(crate) fn mark_onboarding_seen(settings: &mut ThinkTermNativeSettings) {
@@ -2055,4 +2328,90 @@ mod tests {
NativeLanguagePreference::Japanese
);
}
+
+ fn ledger(known: &[u8], reload_owed: bool) -> WriteLedger {
+ WriteLedger {
+ known: Some(fingerprint(known)),
+ reload_owed,
+ }
+ }
+
+ /// A sidebar toggle rewrites settings.json: the watcher must not reload
+ /// for that, must reload once for a setting, and must hand over an edit
+ /// made by anybody else.
+ #[test]
+ fn the_watcher_reloads_for_settings_and_edits_but_not_for_window_state() {
+ let dir = tempfile::tempdir().unwrap();
+ let settings = dir.path().join("settings.json");
+ fs::write(&settings, b"ours").unwrap();
+
+ let mut state = ledger(b"ours", false);
+ assert_eq!(
+ watched_change(true, false, &settings, &mut state),
+ (false, None),
+ "window state written here"
+ );
+
+ let mut state = ledger(b"ours", true);
+ assert_eq!(
+ watched_change(true, false, &settings, &mut state),
+ (true, None),
+ "a setting saved here"
+ );
+ assert_eq!(
+ watched_change(true, false, &settings, &mut state),
+ (false, None),
+ "reloaded for once"
+ );
+ assert_eq!(
+ watched_change(true, true, &settings, &mut state),
+ (true, None),
+ "the config file changing alongside"
+ );
+ assert_eq!(watched_change(false, true, &settings, &mut state), (true, None));
+
+ fs::write(&settings, b"theirs").unwrap();
+ assert_eq!(
+ watched_change(true, false, &settings, &mut state),
+ (true, Some(b"theirs".to_vec())),
+ "an edit made elsewhere"
+ );
+ assert_eq!(
+ watched_change(true, false, &settings, &mut state),
+ (false, None),
+ "and known once taken in"
+ );
+
+ fs::remove_file(&settings).unwrap();
+ assert_eq!(
+ watched_change(true, false, &settings, &mut state),
+ (true, None),
+ "a file that is gone reloads and leaves the settings in use"
+ );
+ }
+
+ /// A setting saved here and window state saved right after it land in
+ /// one watcher batch; the setting's reload must not be lost.
+ #[test]
+ fn a_setting_and_window_state_in_one_batch_still_reload() {
+ let dir = tempfile::tempdir().unwrap();
+ let settings = dir.path().join("settings.json");
+ fs::write(&settings, b"window state on top of a setting").unwrap();
+ let mut state = ledger(b"window state on top of a setting", true);
+ assert_eq!(watched_change(true, false, &settings, &mut state), (true, None));
+ }
+
+ /// An edit made elsewhere can be taken in before the watcher gets to it,
+ /// by a save here or by the Settings window opening. It is taken in
+ /// once, and the reload it is owed still comes when the watcher looks.
+ #[test]
+ fn an_edit_taken_in_before_the_watcher_looks_still_reloads() {
+ let dir = tempfile::tempdir().unwrap();
+ let settings = dir.path().join("settings.json");
+ fs::write(&settings, b"theirs").unwrap();
+ let mut state = ledger(b"ours", false);
+ assert_eq!(external_change(&mut state, &settings), Some(b"theirs".to_vec()));
+ assert_eq!(external_change(&mut state, &settings), None, "taken in once");
+ assert_eq!(watched_change(true, false, &settings, &mut state), (true, None));
+ }
}
diff --git a/wezterm-gui/src/plugins.rs b/wezterm-gui/src/plugins.rs
index 79d07ee8..df49d9e3 100644
--- a/wezterm-gui/src/plugins.rs
+++ b/wezterm-gui/src/plugins.rs
@@ -662,14 +662,15 @@ fn follow_snippets_switch(plugins: &[Info]) {
return;
};
let enabled = snippets.enabled;
- let mut settings = crate::native_settings::load();
- let chrome = &mut settings.chrome;
+ let settings = crate::native_settings::load_shared();
+ let chrome = &settings.chrome;
// The panel was turned off in Settings › Sidebar before it was a
// plugin: that choice becomes the plugin's switch, once.
if chrome.right_sidebar_snippets_enabled == Some(false) {
- chrome.right_sidebar_snippets_enabled = None;
- chrome.snippets_plugin_enabled = Some(false);
- if let Err(err) = crate::native_settings::save(&settings) {
+ if let Err(err) = crate::native_settings::update_ui_state(|settings| {
+ settings.chrome.right_sidebar_snippets_enabled = None;
+ settings.chrome.snippets_plugin_enabled = Some(false);
+ }) {
log::warn!("saving the Snippets switch: {err:#}");
}
crate::settings_window::refresh_open_settings_window_chrome();
@@ -685,8 +686,9 @@ fn follow_snippets_switch(plugins: &[Info]) {
if chrome.snippets_plugin_enabled == Some(enabled) {
return;
}
- chrome.snippets_plugin_enabled = Some(enabled);
- if let Err(err) = crate::native_settings::save(&settings) {
+ if let Err(err) = crate::native_settings::update_ui_state(|settings| {
+ settings.chrome.snippets_plugin_enabled = Some(enabled)
+ }) {
log::warn!("saving the Snippets switch: {err:#}");
}
// An open settings window holds the settings as they were, and would
@@ -719,19 +721,20 @@ fn follow_panels(plugins: &[Info]) {
})
})
.collect();
- let mut settings = crate::native_settings::load();
// The width kept for a plugin's extended view goes with the plugin.
- let widths = settings.chrome.plugin_extended_widths.len();
- settings
- .chrome
- .plugin_extended_widths
- .retain(|id, _| plugins.iter().any(|plugin| plugin.id == *id));
- let forgotten = settings.chrome.plugin_extended_widths.len() != widths;
+ let listed = |id: &String| plugins.iter().any(|plugin| plugin.id == *id);
+ let settings = crate::native_settings::load_shared();
+ let forgotten = !settings.chrome.plugin_extended_widths.keys().all(listed);
if settings.chrome.plugin_panels == panels && !forgotten {
return;
}
- settings.chrome.plugin_panels = panels;
- if let Err(err) = crate::native_settings::save(&settings) {
+ if let Err(err) = crate::native_settings::update_ui_state(|settings| {
+ settings
+ .chrome
+ .plugin_extended_widths
+ .retain(|id, _| listed(id));
+ settings.chrome.plugin_panels = panels;
+ }) {
log::warn!("saving the plugins' panels: {err:#}");
}
crate::settings_window::refresh_open_settings_window_chrome();
diff --git a/wezterm-gui/src/settings_window.rs b/wezterm-gui/src/settings_window.rs
index b1008b4a..c68b0745 100644
--- a/wezterm-gui/src/settings_window.rs
+++ b/wezterm-gui/src/settings_window.rs
@@ -341,6 +341,24 @@ pub(crate) fn refresh_open_settings_window_chrome() {
});
}
+/// Bring the open settings window up to settings that changed outside it
+/// (settings.json edited by hand): its copy, as
+/// `refresh_open_settings_window_chrome` does, and what it holds beside
+/// that copy -- the fields it filled when it opened, the fonts it paints
+/// with and its title.
+pub(crate) fn follow_open_settings_window(
+ before: &ThinkTermNativeSettings,
+ after: &ThinkTermNativeSettings,
+) {
+ SETTINGS_WINDOW.with(|slot| {
+ if let SettingsWindowSlot::Open { settings, .. } = &*slot.borrow() {
+ if let Ok(mut settings) = settings.try_borrow_mut() {
+ settings.follow_settings_changed_elsewhere(before, after);
+ }
+ }
+ });
+}
+
fn settings_window_for_instance(instance_id: u64) -> Option<Rc<RefCell<SettingsWindow>>> {
SETTINGS_WINDOW.with(|slot| match &*slot.borrow() {
SettingsWindowSlot::Open {
@@ -4682,6 +4700,73 @@ impl SettingsWindow {
self.refresh_chrome();
}
+ /// See `follow_open_settings_window`. A field the user is typing into
+ /// is left alone.
+ fn follow_settings_changed_elsewhere(
+ &mut self,
+ before: &ThinkTermNativeSettings,
+ after: &ThinkTermNativeSettings,
+ ) {
+ self.set_native_settings(crate::native_settings::load());
+ let terminal = (&before.terminal, &after.terminal);
+ if terminal.0.font_size != terminal.1.font_size {
+ // The size steps from this field, so a stale one would put the
+ // old size back on the next click.
+ let size = terminal.1.font_size.unwrap_or_else(|| configuration().font_size);
+ self.ui.font_size_input.set_text_end(format!("{size:.1}"));
+ }
+ if terminal.0.font_family != terminal.1.font_family && !self.ui.font_family_input_dirty {
+ self.ui.font_family_input.set_text_end(
+ terminal
+ .1
+ .font_family
+ .clone()
+ .unwrap_or_else(|| Self::effective_font_family(&configuration())),
+ );
+ }
+ if before.workspaces.remote_drop_destination != after.workspaces.remote_drop_destination
+ && !self.ui.remote_drop_input_dirty
+ {
+ self.ui
+ .remote_drop_input
+ .set_text_end(crate::native_settings::remote_drop_destination());
+ }
+ if terminal.0.default_shell != terminal.1.default_shell {
+ self.refresh_shell_catalog();
+ }
+ if terminal.0.bottom_quote_enabled != terminal.1.bottom_quote_enabled
+ || terminal.0.bottom_quote_mode != terminal.1.bottom_quote_mode
+ || terminal.0.bottom_quote_interval_minutes != terminal.1.bottom_quote_interval_minutes
+ || terminal.0.bottom_quote_font_size != terminal.1.bottom_quote_font_size
+ {
+ self.ui.quote_preview = quote_preview_text(&self.native_settings);
+ }
+ if before.tab_icons != after.tab_icons {
+ self.sync_tab_icon_inputs();
+ }
+ if before.chrome.settings_font_size != after.chrome.settings_font_size
+ || before.chrome.settings_font_weight != after.chrome.settings_font_weight
+ {
+ if let Err(err) = self.reload_settings_fonts() {
+ log::warn!("settings window fonts after an outside change: {err:#}");
+ }
+ }
+ if before.localization.language != after.localization.language
+ || before.onboarding.language != after.onboarding.language
+ {
+ // As choosing a language here does; `apply_to_app` has switched
+ // it already.
+ self.ui.sidebar_scroll.reset();
+ self.sync_selected_section_with_search();
+ if let Some(window) = self.window.as_ref() {
+ window.set_title(&crate::i18n::tr("settings-window-title"));
+ }
+ }
+ if let Some(window) = self.window.as_ref() {
+ window.invalidate();
+ }
+ }
+
/// Every field reads a `UiPalette` token, so Settings tracks the main
/// window by construction. It stopped doing that once: two arms that
/// differed only in a hand-written `card_bg`, which was the token's value
@@ -4819,25 +4904,10 @@ impl SettingsWindow {
/// Where the listener binds: every address when the page is to be
/// reachable from other devices, else the configured or default one.
fn web_bind_address(&self) -> String {
- // The address the listener is up on comes first: one started by
- // the CLI on another port is what the switch must keep.
- let state = crate::web_settings::state();
- let configured = state
- .status
- .as_ref()
- .and_then(|status| status.listening.first().or(status.configured.first()).cloned())
- .unwrap_or_else(|| config::WebServer::default().bind_address);
- let port = configured.rsplit_once(':').and_then(|(_, p)| p.parse::<u16>().ok()).unwrap_or(8088);
- let wildcard = configured.starts_with("0.0.0.0:") || configured.starts_with("[::]:");
- if self.native_settings.web.reachable {
- format!("0.0.0.0:{port}")
- } else if wildcard {
- // Off means off the network: a listener found on the wildcard
- // goes back to loopback on the same port.
- format!("127.0.0.1:{port}")
- } else {
- configured
- }
+ crate::web_settings::bind_address(
+ crate::web_settings::state().status.as_ref(),
+ self.native_settings.web.reachable,
+ )
}
fn enter_section(&mut self, section: SettingsSection) {
diff --git a/wezterm-gui/src/tab_icons.rs b/wezterm-gui/src/tab_icons.rs
index 67a0bb50..52cf3bf5 100644
--- a/wezterm-gui/src/tab_icons.rs
+++ b/wezterm-gui/src/tab_icons.rs
@@ -1267,20 +1267,21 @@ pub(crate) fn draw_glyph(
// up through `catalog`, which notices the settings were replaced.
fn edit(change: impl FnOnce(&mut NativeTabIconSettings)) -> Result<()> {
- let mut settings = native_settings::load();
- change(&mut settings.tab_icons);
- // A built-in card whose every field went back to the default no longer
- // needs an entry, and the terminal card never has one that counts.
- settings.tab_icons.cards.retain(|card| {
- is_custom_id(&card.id)
- || (card.id != TERMINAL_CARD
- && *card
- != NativeTabIconCard {
- id: card.id.clone(),
- ..Default::default()
- })
- });
- native_settings::save(&settings)?;
+ let settings = native_settings::update(|settings| {
+ change(&mut settings.tab_icons);
+ // A built-in card whose every field went back to the default no
+ // longer needs an entry, and the terminal card never has one that
+ // counts.
+ settings.tab_icons.cards.retain(|card| {
+ is_custom_id(&card.id)
+ || (card.id != TERMINAL_CARD
+ && *card
+ != NativeTabIconCard {
+ id: card.id.clone(),
+ ..Default::default()
+ })
+ });
+ })?;
remove_unreferenced_svgs(&settings.tab_icons);
Ok(())
}
diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs
index 1ff799fa..776aa139 100644
--- a/wezterm-gui/src/termwindow/mouseevent.rs
+++ b/wezterm-gui/src/termwindow/mouseevent.rs
@@ -6156,7 +6156,11 @@ impl super::TermWindow {
if let Err(err) = crate::native_settings::save_workspace_sidebar_hidden_statuses(hidden) {
log::warn!("failed to save sidebar status filter: {err:#}");
}
- self.invalidate_window();
+ // Every window draws and hit-tests its sidebar through the filter as
+ // it is now, so every window repaints, not only this one.
+ if let Some(front_end) = crate::frontend::try_front_end() {
+ front_end.invalidate_all_windows();
+ }
}
/// The notification bell: pending finished/attention threads across every
diff --git a/wezterm-gui/src/termwindow/ui/right_sidebar.rs b/wezterm-gui/src/termwindow/ui/right_sidebar.rs
index 984a189b..ee87c331 100644
--- a/wezterm-gui/src/termwindow/ui/right_sidebar.rs
+++ b/wezterm-gui/src/termwindow/ui/right_sidebar.rs
@@ -2148,6 +2148,11 @@ impl crate::TermWindow {
// Source keeps nothing of the rendering.
self.right_sidebar_markdown_preview = None;
}
+ // The choice is every window's: one showing a Markdown file switches
+ // when it repaints.
+ if let Some(front_end) = crate::frontend::try_front_end() {
+ front_end.invalidate_all_windows();
+ }
}
/// Build the rendered surface for the previewed text, unless the one
diff --git a/wezterm-gui/src/web_settings.rs b/wezterm-gui/src/web_settings.rs
index 35397a54..446299de 100644
--- a/wezterm-gui/src/web_settings.rs
+++ b/wezterm-gui/src/web_settings.rs
@@ -331,12 +331,6 @@ pub fn restart(window: Window, bind_address: String) {
return;
};
promise::spawn::spawn_into_main_thread(async move {
- // Off and on are two requests, and the second can be refused after
- // the first has already succeeded: the server will not open a
- // non-loopback listener for a client that reached it over TLS, and
- // any address can fail to bind. Either would leave the listener off
- // with nothing to say so. What was listening is asked for first, so
- // a refused "on" puts it back before the error is shown.
let before = match client.get_web_server_status().await {
Ok(status) => status.listening,
Err(err) => {
@@ -344,16 +338,7 @@ pub fn restart(window: Window, bind_address: String) {
return;
}
};
- let off = client
- .set_web_server(codec::SetWebServer { enabled: false, bind_address: None })
- .await;
- let outcome = match off {
- Ok(_) => client
- .set_web_server(codec::SetWebServer { enabled: true, bind_address: Some(bind_address) })
- .await,
- Err(err) => Err(err),
- };
- match outcome {
+ match move_listener(&client, before, bind_address).await {
Ok(status) => {
with_state(|s| {
s.status = Some(status);
@@ -363,20 +348,7 @@ pub fn restart(window: Window, bind_address: String) {
finish(&window, None, Gate::Action);
refresh(window);
}
- Err(err) => {
- let mut restored = Ok(());
- for address in before {
- let put_back = client
- .set_web_server(codec::SetWebServer { enabled: true, bind_address: Some(address) })
- .await;
- if let Err(err) = put_back {
- restored = Err(err);
- }
- }
- let message = match restored {
- Ok(()) => format!("{err:#}"),
- Err(restore_err) => format!("{err:#}; and putting the previous listener back failed: {restore_err:#}"),
- };
+ Err(message) => {
finish(&window, Some(message), Gate::Action);
refresh(window);
}
@@ -385,6 +357,132 @@ pub fn restart(window: Window, bind_address: String) {
.detach();
}
+/// Off, then on at `bind_address`; the new status, or what went wrong.
+///
+/// Off and on are two requests, and the second can be refused after the
+/// first has already succeeded: the server will not open a non-loopback
+/// listener for a client that reached it over TLS, and any address can fail
+/// to bind. Either would leave the listener off with nothing to say so. So
+/// a refused "on" puts back what was listening (`before`) before the error
+/// is shown.
+async fn move_listener(
+ client: &Client,
+ before: Vec<String>,
+ bind_address: String,
+) -> Result<WebServerStatus, String> {
+ let off = client
+ .set_web_server(codec::SetWebServer { enabled: false, bind_address: None })
+ .await;
+ let outcome = match off {
+ Ok(_) => client
+ .set_web_server(codec::SetWebServer { enabled: true, bind_address: Some(bind_address) })
+ .await,
+ Err(err) => Err(err),
+ };
+ let err = match outcome {
+ Ok(status) => return Ok(status),
+ Err(err) => err,
+ };
+ let mut restored = Ok(());
+ for address in before {
+ let put_back = client
+ .set_web_server(codec::SetWebServer { enabled: true, bind_address: Some(address) })
+ .await;
+ if let Err(err) = put_back {
+ restored = Err(err);
+ }
+ }
+ Err(match restored {
+ Ok(()) => format!("{err:#}"),
+ Err(restore_err) => format!("{err:#}; and putting the previous listener back failed: {restore_err:#}"),
+ })
+}
+
+/// Where the listener binds by the "reachable from other devices" switch:
+/// every address when it is on, else the configured or default one. The
+/// address the listener is up on comes first: one started by the CLI on
+/// another port is what the switch must keep.
+pub fn bind_address(status: Option<&WebServerStatus>, reachable: bool) -> String {
+ let configured = status
+ .and_then(|status| status.listening.first().or(status.configured.first()).cloned())
+ .unwrap_or_else(|| config::WebServer::default().bind_address);
+ let port = configured.rsplit_once(':').and_then(|(_, p)| p.parse::<u16>().ok()).unwrap_or(8088);
+ let wildcard = configured.starts_with("0.0.0.0:") || configured.starts_with("[::]:");
+ if reachable {
+ format!("0.0.0.0:{port}")
+ } else if wildcard {
+ // Off means off the network: a listener found on the wildcard
+ // goes back to loopback on the same port.
+ format!("127.0.0.1:{port}")
+ } else {
+ configured
+ }
+}
+
+/// Move a listener that is up to where the "reachable from other devices"
+/// switch now says, after settings.json changed it outside the settings
+/// window (whose own switch goes through `restart`). A listener that is off
+/// stays off: the switch only says where it listens.
+pub fn follow_reachable() {
+ follow_reachable_within(10);
+}
+
+fn follow_reachable_within(tries: u32) {
+ if with_state(|s| std::mem::replace(&mut s.busy, true)) {
+ // A request from the settings window is out; once it is answered,
+ // the switch as it stands then is what counts.
+ if tries == 0 {
+ log::warn!("web settings: still busy; the listener keeps its address");
+ return;
+ }
+ promise::spawn::spawn_into_main_thread(async move {
+ smol::Timer::after(Duration::from_millis(500)).await;
+ follow_reachable_within(tries - 1);
+ })
+ .detach();
+ return;
+ }
+ let Some(client) = client() else {
+ with_state(|s| s.busy = false);
+ return;
+ };
+ let reachable = crate::native_settings::load_shared().web.reachable;
+ promise::spawn::spawn_into_main_thread(async move {
+ let outcome = async {
+ let status = client
+ .get_web_server_status()
+ .await
+ .map_err(|err| format!("{err:#}"))?;
+ let address = bind_address(Some(&status), reachable);
+ if status.listening.is_empty() || status.listening.contains(&address) {
+ return Ok(None);
+ }
+ log::info!("web settings: moving the listener to {address}, as settings.json now says");
+ move_listener(&client, status.listening, address).await.map(Some)
+ }
+ .await;
+ with_state(|s| {
+ s.busy = false;
+ s.generation += 1;
+ match outcome {
+ Ok(Some(status)) => {
+ s.status = Some(status);
+ s.qr = None;
+ s.qr_url = None;
+ s.error = None;
+ }
+ Ok(None) => {}
+ Err(message) => {
+ log::warn!("web settings: following settings.json: {message}");
+ s.error = Some(message);
+ }
+ }
+ });
+ crate::settings_window::invalidate_open_settings_window();
+ })
+ .detach();
+}
+
/// Mint a link and show it as a QR code, for a phone to scan. Prefers a
/// URL another device can use; a loopback-only listener gets a remark.
pub fn show_qr(window: Window, ttl_secs: Option<u64>) {
@@ -589,3 +687,31 @@ mod certificate_display_tests {
assert!(state.displayed_certificates().is_empty());
}
}
+
+#[cfg(test)]
+mod bind_address_tests {
+ use super::*;
+
+ fn listening(addresses: &[&str]) -> WebServerStatus {
+ WebServerStatus {
+ listening: addresses.iter().map(|address| address.to_string()).collect(),
+ urls: vec![],
+ configured: vec![],
+ certificates: vec![],
+ }
+ }
+
+ /// The switch moves a listener between every address and loopback on
+ /// the port it is on, and leaves one on a named address where it is.
+ #[test]
+ fn the_reachable_switch_keeps_the_port_and_picks_the_address() {
+ let wildcard = listening(&["0.0.0.0:9001"]);
+ assert_eq!(bind_address(Some(&wildcard), false), "127.0.0.1:9001");
+ assert_eq!(bind_address(Some(&wildcard), true), "0.0.0.0:9001");
+ let loopback = listening(&["127.0.0.1:9001"]);
+ assert_eq!(bind_address(Some(&loopback), true), "0.0.0.0:9001");
+ assert_eq!(bind_address(Some(&loopback), false), "127.0.0.1:9001");
+ let named = listening(&["192.0.2.7:9001"]);
+ assert_eq!(bind_address(Some(&named), false), "192.0.2.7:9001");
+ }
+}