use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::sync::mpsc;
use std::time::Duration;
use notify_debouncer_full::DebouncedEvent;
use notify_debouncer_full::new_debouncer;
use notify_debouncer_full::notify::RecursiveMode;
use serde::Deserialize;
use serde::Deserializer;
use serde::de::Error as DeError;
use serde::de::Unexpected;
use winit::event_loop::EventLoopProxy;
use super::font::FontSettings;
use crate::cmd_line::GeometryArgs;
use crate::cmd_line::MouseCursorIcon;
use crate::error_msg;
use crate::frame::Frame;
use crate::renderer::box_drawing::BoxDrawingSettings;
use crate::window::EventPayload;
use crate::window::UserEvent;
const CONFIG_FILE: &str = "config.toml";
#[cfg(unix)]
fn neovide_config_dir() -> PathBuf {
let xdg_dirs = xdg::BaseDirectories::with_prefix("neovide");
xdg_dirs.get_config_home().unwrap()
}
#[cfg(windows)]
fn neovide_config_dir() -> PathBuf {
let mut path = dirs::config_dir().unwrap();
path.push("neovide");
path
}
pub fn config_path() -> PathBuf {
env::var("NEOVIDE_CONFIG")
.ok()
.map(PathBuf::from)
.filter(|path| path.exists() && path.is_file())
.unwrap_or_else(|| {
let mut path = neovide_config_dir();
path.push(CONFIG_FILE);
path
})
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum HotkeyConfigValue {
String(String),
Bool(bool),
}
fn deserialize_optional_hotkey<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where D: Deserializer<'de> {
match Option::<HotkeyConfigValue>::deserialize(deserializer)? {
None => Ok(None),
Some(HotkeyConfigValue::String(value)) => Ok(Some(value)),
Some(HotkeyConfigValue::Bool(false)) => Ok(Some("false".to_string())),
Some(HotkeyConfigValue::Bool(true)) => {
Err(D::Error::invalid_value(Unexpected::Bool(true), &"a shortcut string or false"))
}
}
}
#[derive(Debug, Deserialize, Default, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
pub font: Option<FontSettings>,
pub box_drawing: Option<BoxDrawingSettings>,
pub server: Option<String>,
pub fork: Option<bool>,
pub frame: Option<Frame>,
pub size: Option<String>,
pub grid: Option<String>,
pub idle: Option<bool>,
pub maximized: Option<bool>,
pub neovim_bin: Option<PathBuf>,
pub no_multigrid: Option<bool>,
pub srgb: Option<bool>,
pub startup_message_capture: Option<bool>,
pub tabs: Option<bool>,
pub system_native_tabs: Option<bool>,
pub mouse_cursor_icon: Option<String>,
pub title_hidden: Option<bool>,
pub vsync: Option<bool>,
pub wsl: Option<bool>,
pub backtraces_path: Option<PathBuf>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_pinned_hotkey: Option<String>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_switcher_hotkey: Option<String>,
pub system_switcher_glass_style: Option<String>,
pub system_switcher_glass_dimming: Option<f64>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_new_window_hotkey: Option<String>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_hide_hotkey: Option<String>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_hide_others_hotkey: Option<String>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_quit_hotkey: Option<String>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_minimize_hotkey: Option<String>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_fullscreen_hotkey: Option<String>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_show_all_tabs_hotkey: Option<String>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_tab_prev_hotkey: Option<String>,
#[serde(default, deserialize_with = "deserialize_optional_hotkey")]
pub system_tab_next_hotkey: Option<String>,
pub icon: Option<String>,
pub chdir: Option<PathBuf>,
pub opengl: Option<bool>,
pub wayland_app_id: Option<String>,
pub x11_wm_class: Option<String>,
pub x11_wm_class_instance: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum HotReloadConfigs {
App(AppHotReloadConfigs),
Renderer(RendererHotReloadConfigs),
Window(WindowHotReloadConfigs),
}
#[derive(Debug, Clone, PartialEq)]
pub enum AppHotReloadConfigs {
Idle(bool),
}
#[derive(Debug, Clone, PartialEq)]
pub enum RendererHotReloadConfigs {
Font(Box<Option<FontSettings>>),
BoxDrawing(Option<BoxDrawingSettings>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum WindowHotReloadConfigs {
TitleHidden(Option<bool>),
MouseCursorIcon(MouseCursorIcon),
Geometry(GeometryArgs),
}
impl Config {
pub fn init() -> Config {
let config = Config::load_from_path(&config_path());
match &config {
Ok(config) => config.write_to_env(),
Err(Some(err)) => eprintln!("{err}"),
Err(None) => {}
};
config.unwrap_or_default()
}
pub fn watch_config_file(init_config: Config, event_loop_proxy: EventLoopProxy<EventPayload>) {
std::thread::spawn(move || watcher_thread(init_config, event_loop_proxy));
}
fn write_to_env(&self) {
if let Some(server) = &self.server {
unsafe { env::set_var("NEOVIDE_SERVER", server) };
}
if let Some(wsl) = self.wsl {
unsafe { env::set_var("NEOVIDE_WSL", wsl.to_string()) };
}
if let Some(no_multigrid) = self.no_multigrid {
unsafe { env::set_var("NEOVIDE_NO_MULTIGRID", no_multigrid.to_string()) };
}
if let Some(maximized) = self.maximized {
unsafe { env::set_var("NEOVIDE_MAXIMIZED", maximized.to_string()) };
}
if let Some(vsync) = self.vsync {
unsafe { env::set_var("NEOVIDE_VSYNC", vsync.to_string()) };
}
if let Some(srgb) = self.srgb {
unsafe { env::set_var("NEOVIDE_SRGB", srgb.to_string()) };
}
if let Some(startup_message_capture) = self.startup_message_capture {
unsafe {
env::set_var("NEOVIDE_STARTUP_MESSAGE_CAPTURE", startup_message_capture.to_string())
};
}
if let Some(fork) = self.fork {
unsafe { env::set_var("NEOVIDE_FORK", fork.to_string()) };
}
if let Some(opengl) = self.opengl {
unsafe { env::set_var("NEOVIDE_OPENGL", opengl.to_string()) };
}
if let Some(idle) = self.idle {
unsafe { env::set_var("NEOVIDE_IDLE", idle.to_string()) };
}
if let Some(frame) = self.frame {
unsafe { env::set_var("NEOVIDE_FRAME", frame.to_string()) };
}
if let Some(size) = &self.size {
unsafe { env::set_var("NEOVIDE_SIZE", size) };
}
if let Some(grid) = &self.grid {
unsafe { env::set_var("NEOVIDE_GRID", grid) };
}
if let Some(neovim_bin) = &self.neovim_bin {
unsafe { env::set_var("NEOVIM_BIN", neovim_bin.to_string_lossy().to_string()) };
}
if let Some(mouse_cursor_icon) = &self.mouse_cursor_icon {
unsafe { env::set_var("NEOVIDE_MOUSE_CURSOR_ICON", mouse_cursor_icon) };
}
if let Some(title_hidden) = &self.title_hidden {
unsafe { env::set_var("NEOVIDE_TITLE_HIDDEN", title_hidden.to_string()) };
}
if let Some(tabs) = &self.tabs {
unsafe { env::set_var("NEOVIDE_TABS", tabs.to_string()) };
}
if let Some(system_native_tabs) = &self.system_native_tabs {
unsafe { env::set_var("NEOVIDE_SYSTEM_NATIVE_TABS", system_native_tabs.to_string()) };
}
if let Some(pinned_hotkey) = &self.system_pinned_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_PINNED_HOTKEY", pinned_hotkey) };
}
if let Some(switcher_hotkey) = &self.system_switcher_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_SWITCHER_HOTKEY", switcher_hotkey) };
}
if let Some(glass_style) = &self.system_switcher_glass_style {
unsafe { env::set_var("NEOVIDE_SYSTEM_SWITCHER_GLASS_STYLE", glass_style) };
}
if let Some(glass_dimming) = self.system_switcher_glass_dimming {
unsafe {
env::set_var("NEOVIDE_SYSTEM_SWITCHER_GLASS_DIMMING", glass_dimming.to_string())
};
}
if let Some(new_window_hotkey) = &self.system_new_window_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_NEW_WINDOW_HOTKEY", new_window_hotkey) };
}
if let Some(hide_hotkey) = &self.system_hide_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_HIDE_HOTKEY", hide_hotkey) };
}
if let Some(hide_others_hotkey) = &self.system_hide_others_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_HIDE_OTHERS_HOTKEY", hide_others_hotkey) };
}
if let Some(quit_hotkey) = &self.system_quit_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_QUIT_HOTKEY", quit_hotkey) };
}
if let Some(minimize_hotkey) = &self.system_minimize_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_MINIMIZE_HOTKEY", minimize_hotkey) };
}
if let Some(fullscreen_hotkey) = &self.system_fullscreen_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_FULLSCREEN_HOTKEY", fullscreen_hotkey) };
}
if let Some(show_all_tabs_hotkey) = &self.system_show_all_tabs_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_SHOW_ALL_TABS_HOTKEY", show_all_tabs_hotkey) };
}
if let Some(tab_prev_hotkey) = &self.system_tab_prev_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_TAB_PREV_HOTKEY", tab_prev_hotkey) };
}
if let Some(tab_next_hotkey) = &self.system_tab_next_hotkey {
unsafe { env::set_var("NEOVIDE_SYSTEM_TAB_NEXT_HOTKEY", tab_next_hotkey) };
}
if let Some(icon) = &self.icon {
unsafe { env::set_var("NEOVIDE_ICON", icon) };
}
if let Some(wayland_app_id) = &self.wayland_app_id {
unsafe { env::set_var("NEOVIDE_APP_ID", wayland_app_id) };
}
if let Some(x11_wm_class) = &self.x11_wm_class {
unsafe { env::set_var("NEOVIDE_WM_CLASS", x11_wm_class) };
}
if let Some(x11_wm_class_instance) = &self.x11_wm_class_instance {
unsafe { env::set_var("NEOVIDE_WM_CLASS_INSTANCE", x11_wm_class_instance) };
}
if let Some(chdir) = &self.chdir {
unsafe { env::set_var("NEOVIDE_CHDIR", chdir.to_string_lossy().to_string()) };
}
}
fn load_from_path(path: &Path) -> Result<Self, Option<String>> {
if !path.exists() {
return Err(None);
}
let toml = fs::read_to_string(path).map_err(|e| {
format!(
"Error while trying to open config file {}:\n{}\nContinuing with default config.",
path.to_string_lossy(),
e
)
})?;
let config = toml::from_str(&toml).map_err(|e| {
format!(
"Error while parsing config file {}:\n{}\nContinuing with default config.",
path.to_string_lossy(),
e
)
})?;
Ok(config)
}
}
fn watcher_thread(init_config: Config, event_loop_proxy: EventLoopProxy<EventPayload>) {
let config_path = config_path();
let parent_path = match config_path.parent() {
Some(dir) => dir,
None => return,
};
let (tx, rx) = mpsc::channel();
let mut debouncer = new_debouncer(Duration::from_millis(500), None, tx).unwrap();
if let Err(e) = debouncer.watch(
parent_path,
RecursiveMode::NonRecursive,
) {
log::warn!("Error while trying to watch config file parent directory for changes: {e}");
return;
}
let mut previous_config = init_config;
loop {
let events = match rx.recv() {
Ok(Ok(events)) => events,
Ok(Err(errors)) => {
for error in errors {
log::warn!("Error while watching config file: {error}");
}
continue;
}
Err(error) => {
log::warn!("Config file watcher stopped: {error}");
return;
}
};
if !events_relevant_to_config(&events, &config_path) {
continue;
}
let config = match Config::load_from_path(&config_path) {
Ok(config) => config,
Err(maybe_err) => {
if let Some(err) = maybe_err {
error_msg!("While reloading config file: {err}");
}
continue;
}
};
if config.font != previous_config.font {
event_loop_proxy
.send_event(EventPayload::all(UserEvent::ConfigsChanged(Box::new(
HotReloadConfigs::Renderer(RendererHotReloadConfigs::Font(Box::new(
config.font.clone(),
))),
))))
.unwrap();
}
if config.box_drawing != previous_config.box_drawing {
event_loop_proxy
.send_event(EventPayload::all(UserEvent::ConfigsChanged(Box::new(
HotReloadConfigs::Renderer(RendererHotReloadConfigs::BoxDrawing(
config.box_drawing.clone(),
)),
))))
.unwrap();
}
if config.idle != previous_config.idle {
event_loop_proxy
.send_event(EventPayload::all(UserEvent::ConfigsChanged(Box::new(
HotReloadConfigs::App(AppHotReloadConfigs::Idle(config.idle.unwrap_or(true))),
))))
.unwrap();
}
if config.title_hidden != previous_config.title_hidden {
event_loop_proxy
.send_event(EventPayload::all(UserEvent::ConfigsChanged(Box::new(
HotReloadConfigs::Window(WindowHotReloadConfigs::TitleHidden(
config.title_hidden,
)),
))))
.unwrap();
}
if config.mouse_cursor_icon != previous_config.mouse_cursor_icon {
match MouseCursorIcon::from_config(config.mouse_cursor_icon.as_deref()) {
Ok(mouse_cursor_icon) => {
event_loop_proxy
.send_event(EventPayload::all(UserEvent::ConfigsChanged(Box::new(
HotReloadConfigs::Window(WindowHotReloadConfigs::MouseCursorIcon(
mouse_cursor_icon,
)),
))))
.unwrap();
}
Err(err) => {
error_msg!("While reloading config file: invalid mouse-cursor-icon: {err}");
}
}
}
if config.size != previous_config.size
|| config.grid != previous_config.grid
|| config.maximized != previous_config.maximized
{
match GeometryArgs::from_config(
config.size.as_deref(),
config.grid.as_deref(),
config.maximized,
) {
Ok(geometry) => {
event_loop_proxy
.send_event(EventPayload::all(UserEvent::ConfigsChanged(Box::new(
HotReloadConfigs::Window(WindowHotReloadConfigs::Geometry(geometry)),
))))
.unwrap();
}
Err(err) => {
error_msg!("While reloading config file: invalid geometry: {err}");
}
}
}
previous_config = config;
}
}
fn events_relevant_to_config(events: &[DebouncedEvent], config_path: &Path) -> bool {
events.iter().any(|event| {
event.event.need_rescan() || event.event.paths.iter().any(|path| path == config_path)
})
}
#[cfg(test)]
mod tests {
use std::time::Instant;
use notify_debouncer_full::DebouncedEvent;
use notify_debouncer_full::notify::Event;
use notify_debouncer_full::notify::EventKind;
use notify_debouncer_full::notify::event::Flag;
use super::*;
#[test]
fn parses_system_switcher_glass_options() {
for (style, dimming) in [("regular", "0"), ("clear", "0.3"), ("clear", "1")] {
let config: Config = toml::from_str(&format!(
"system-switcher-glass-style = \"{style}\"\nsystem-switcher-glass-dimming = {dimming}"
))
.unwrap();
assert_eq!(config.system_switcher_glass_style.as_deref(), Some(style));
assert_eq!(config.system_switcher_glass_dimming, Some(dimming.parse::<f64>().unwrap()));
}
}
#[test]
#[serial_test::serial]
fn system_switcher_glass_options_override_environment_only_when_set() {
let _style = scoped_env::ScopedEnv::set("NEOVIDE_SYSTEM_SWITCHER_GLASS_STYLE", "regular");
let _dimming = scoped_env::ScopedEnv::set("NEOVIDE_SYSTEM_SWITCHER_GLASS_DIMMING", "0.2");
let config: Config = toml::from_str("").unwrap();
config.write_to_env();
assert_eq!(env::var("NEOVIDE_SYSTEM_SWITCHER_GLASS_STYLE").unwrap(), "regular");
assert_eq!(env::var("NEOVIDE_SYSTEM_SWITCHER_GLASS_DIMMING").unwrap(), "0.2");
let config: Config = toml::from_str(
"system-switcher-glass-style = \"clear\"\nsystem-switcher-glass-dimming = 0.30",
)
.unwrap();
config.write_to_env();
assert_eq!(env::var("NEOVIDE_SYSTEM_SWITCHER_GLASS_STYLE").unwrap(), "clear");
assert_eq!(env::var("NEOVIDE_SYSTEM_SWITCHER_GLASS_DIMMING").unwrap(), "0.3");
}
#[test]
#[cfg(target_os = "macos")]
#[serial_test::serial]
fn system_switcher_glass_flags_override_config() {
use crate::cmd_line::CmdLineSettings;
use crate::cmd_line::handle_command_line_arguments;
use crate::settings::Settings;
let _style = scoped_env::ScopedEnv::set("NEOVIDE_SYSTEM_SWITCHER_GLASS_STYLE", "regular");
let _dimming = scoped_env::ScopedEnv::set("NEOVIDE_SYSTEM_SWITCHER_GLASS_DIMMING", "0.75");
let config: Config = toml::from_str(
"system-switcher-glass-style = \"clear\"\nsystem-switcher-glass-dimming = 0.30",
)
.unwrap();
config.write_to_env();
let settings = Settings::new();
handle_command_line_arguments(vec!["neovide".into()], &settings).unwrap();
let cmdline = settings.get::<CmdLineSettings>();
assert_eq!(cmdline.system_switcher_glass_style.as_deref(), Some("clear"));
assert_eq!(cmdline.system_switcher_glass_dimming, "0.3");
let args = [
"neovide",
"--system-switcher-glass-style=regular",
"--system-switcher-glass-dimming=0",
]
.map(String::from)
.to_vec();
handle_command_line_arguments(args, &settings).unwrap();
let cmdline = settings.get::<CmdLineSettings>();
assert_eq!(cmdline.system_switcher_glass_style.as_deref(), Some("regular"));
assert_eq!(cmdline.system_switcher_glass_dimming, "0");
assert_eq!(env::var("NEOVIDE_SYSTEM_SWITCHER_GLASS_STYLE").unwrap(), "clear");
assert_eq!(env::var("NEOVIDE_SYSTEM_SWITCHER_GLASS_DIMMING").unwrap(), "0.3");
}
fn event_with_paths(paths: &[&str]) -> DebouncedEvent {
let event = paths
.iter()
.fold(Event::new(EventKind::Any), |event, path| event.add_path(PathBuf::from(path)));
DebouncedEvent::new(event, Instant::now())
}
#[test]
fn ignores_events_for_other_files_in_config_directory() {
let events = [event_with_paths(&["/tmp/.config.toml.swp"])];
assert!(!events_relevant_to_config(&events, Path::new("/tmp/config.toml")));
}
#[test]
fn accepts_events_for_config_file() {
let events = [event_with_paths(&["/tmp/config.toml"])];
assert!(events_relevant_to_config(&events, Path::new("/tmp/config.toml")));
}
#[test]
fn accepts_atomic_rename_to_config_file() {
let events = [event_with_paths(&["/tmp/config.toml.tmp", "/tmp/config.toml"])];
assert!(events_relevant_to_config(&events, Path::new("/tmp/config.toml")));
}
#[test]
fn accepts_events_that_require_a_rescan() {
let event = Event::new(EventKind::Any).set_flag(Flag::Rescan);
let events = [DebouncedEvent::new(event, Instant::now())];
assert!(events_relevant_to_config(&events, Path::new("/tmp/config.toml")));
}
}