feat: add in-TUI settings editors

This commit is contained in:
objz committed 2026-09-03 20:44:44 +02:00
1 parent 8c94e126c3
commit c95f2d17ff
47 files changed
+2574 -741

No files matched your search

+70 -13
View File
@@ -7,7 +7,7 @@
use config::{Config as ConfigLoader, ConfigError, File};
use std::fs;
use std::path::PathBuf;
use std::sync::LazyLock;
use std::sync::{LazyLock, RwLock, RwLockReadGuard};
pub mod settings;
pub mod theme;
@@ -56,18 +56,75 @@ pub fn load_config(config_path: &std::path::Path) -> Result<Config, ConfigError>
.try_deserialize()
}
pub static SETTINGS: LazyLock<Config> = LazyLock::new(|| {
let path = ensure_config_exists();
load_config(&path).unwrap_or_else(|e| {
tracing::error!("Config load failed, using defaults: {}", e);
Config {
general: settings::General::default(),
paths: settings::Paths::default(),
defaults: settings::Defaults::default(),
ui: settings::Ui::default(),
content: settings::Content::default(),
}
})
pub struct ConfigStore(RwLock<Config>);
impl ConfigStore {
pub fn read(&self) -> RwLockReadGuard<'_, Config> {
self.0
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub fn save_launcher_settings(&self, edited: Config) -> std::io::Result<()> {
let path = get_config_path().join("config.toml");
let current = self.read().clone();
let mut persisted = load_config(&path).unwrap_or_else(|error| {
tracing::warn!("Failed to merge config.toml while saving settings: {error}");
current.clone()
});
persisted.defaults = edited.defaults.clone();
persisted.paths.java_path = edited.paths.java_path.clone();
let serialized = toml::to_string_pretty(&persisted).map_err(std::io::Error::other)?;
crate::storage::write_atomic(&path, serialized.as_bytes())?;
let mut runtime = current;
runtime.defaults = edited.defaults;
runtime.paths.java_path = edited.paths.java_path;
*self
.0
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = runtime;
Ok(())
}
pub fn reload(&self) -> Result<bool, ConfigError> {
let mut config = load_config(&get_config_path().join("config.toml"))?;
let restart_required = {
let current = self.read();
let changed = current.paths.instances_dir != config.paths.instances_dir
|| current.paths.meta_dir != config.paths.meta_dir
|| current.ui.image_protocol != config.ui.image_protocol;
// App owns a manager and several watchers rooted at these paths.
// Keep them stable for this process; persisted path edits apply on restart.
config
.paths
.instances_dir
.clone_from(&current.paths.instances_dir);
config.paths.meta_dir.clone_from(&current.paths.meta_dir);
config.ui.image_protocol = current.ui.image_protocol;
changed
};
*self
.0
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = config;
Ok(restart_required)
}
}
pub static SETTINGS: LazyLock<ConfigStore> = LazyLock::new(|| {
ConfigStore(RwLock::new({
let path = ensure_config_exists();
load_config(&path).unwrap_or_else(|e| {
tracing::error!("Config load failed, using defaults: {}", e);
Config {
general: settings::General::default(),
paths: settings::Paths::default(),
defaults: settings::Defaults::default(),
ui: settings::Ui::default(),
content: settings::Content::default(),
}
})
}))
});
#[cfg(test)]
+8 -8
View File
@@ -6,9 +6,9 @@
use std::path::PathBuf;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Default, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ImageProtocol {
Halfblocks,
@@ -18,10 +18,10 @@ pub enum ImageProtocol {
Iterm2,
}
#[derive(Debug, Deserialize, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct General {}
#[derive(Debug, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Content {
#[serde(default = "default_true")]
pub ask_on_provider_conflict: bool,
@@ -119,7 +119,7 @@ impl Content {
}
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Paths {
#[serde(default = "default_instances_dir")]
pub instances_dir: String,
@@ -185,7 +185,7 @@ impl Paths {
}
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Defaults {
#[serde(default = "default_memory_min")]
pub memory_min: String,
@@ -209,7 +209,7 @@ impl Default for Defaults {
}
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
// timing knobs for the error toast animation: show for 5s, start sliding at 3.5s,
// fly off screen over 300ms. tweak these if the toasts feel too fast or slow.
pub struct Ui {
@@ -250,7 +250,7 @@ impl Default for Ui {
}
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub general: General,
+127 -7
View File
@@ -6,7 +6,7 @@
// config/theme/ directory or by absolute path.
use std::path::Path;
use std::sync::LazyLock;
use std::sync::{Arc, LazyLock, RwLock};
use ratatui::style::Color;
use ratatui::widgets::BorderType;
@@ -34,7 +34,7 @@ impl BorderStyle {
}
}
#[derive(Debug, Deserialize, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ThemeOverrides {
pub accent: Option<Color>,
pub accent_dim: Option<Color>,
@@ -53,7 +53,7 @@ pub struct ThemeOverrides {
pub background: Option<Color>,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThemeConfig {
#[serde(default)]
pub border_style: BorderStyle,
@@ -167,12 +167,132 @@ fn load_base_theme(name: &str) -> Box<dyn Theme> {
resolve_theme(name)
}
static THEME_CONFIG: LazyLock<ThemeConfig> = LazyLock::new(load_theme_config);
pub struct ThemeStore(RwLock<Arc<dyn Theme>>);
pub static THEME: LazyLock<Box<dyn Theme>> = LazyLock::new(|| resolve_app_theme(&THEME_CONFIG));
impl ThemeStore {
pub fn as_ref(&self) -> Arc<dyn Theme> {
self.0
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
}
pub static BORDER_STYLE: LazyLock<BorderStyle> =
LazyLock::new(|| THEME_CONFIG.border_style.clone());
fn set(&self, theme: Box<dyn Theme>) {
*self
.0
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = Arc::from(theme);
}
}
pub struct BorderStyleStore(RwLock<BorderStyle>);
impl BorderStyleStore {
pub fn to_border_type(&self) -> BorderType {
self.current().to_border_type()
}
pub fn current(&self) -> BorderStyle {
self.0
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
}
fn set(&self, style: BorderStyle) {
*self
.0
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = style;
}
}
static THEME_CONFIG: LazyLock<RwLock<ThemeConfig>> =
LazyLock::new(|| RwLock::new(load_theme_config()));
pub static THEME: LazyLock<ThemeStore> = LazyLock::new(|| {
let config = THEME_CONFIG
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
ThemeStore(RwLock::new(Arc::from(resolve_app_theme(&config))))
});
pub static BORDER_STYLE: LazyLock<BorderStyleStore> = LazyLock::new(|| {
let config = THEME_CONFIG
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
BorderStyleStore(RwLock::new(config.border_style.clone()))
});
pub fn current_theme_config() -> ThemeConfig {
THEME_CONFIG
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
}
pub fn apply_theme(theme: String, border_style: BorderStyle) -> std::io::Result<()> {
validate_theme_name(&theme)?;
let mut config = current_theme_config();
config.theme = theme;
config.border_style = border_style.clone();
let serialized = toml::to_string_pretty(&config).map_err(std::io::Error::other)?;
crate::storage::write_atomic(
&super::get_config_path().join("theme.toml"),
serialized.as_bytes(),
)?;
THEME.set(resolve_app_theme(&config));
BORDER_STYLE.set(border_style);
*THEME_CONFIG
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = config;
crate::feedback::request_redraw();
Ok(())
}
fn validate_theme_name(name: &str) -> std::io::Result<()> {
if ratatui_themekit::available_theme_ids().contains(&name) {
return Ok(());
}
let path = if Path::new(name).is_absolute() {
std::path::PathBuf::from(name)
} else {
let directory = super::get_config_path().join("theme");
let direct = directory.join(name);
if direct.exists() {
direct
} else {
directory.join(format!("{name}.toml"))
}
};
let content = std::fs::read_to_string(&path).map_err(|error| {
std::io::Error::new(
error.kind(),
format!("failed to load theme {}: {error}", path.display()),
)
})?;
toml::from_str::<CustomTheme>(&content).map_err(|error| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("invalid theme {}: {error}", path.display()),
)
})?;
Ok(())
}
pub fn reload_theme() -> std::io::Result<()> {
let path = super::get_config_path().join("theme.toml");
let content = std::fs::read_to_string(path)?;
let config: ThemeConfig = toml::from_str(&content)
.map_err(|error| std::io::Error::new(std::io::ErrorKind::InvalidData, error))?;
THEME.set(resolve_app_theme(&config));
BORDER_STYLE.set(config.border_style.clone());
*THEME_CONFIG
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = config;
crate::feedback::request_redraw();
Ok(())
}
#[cfg(test)]
#[path = "tests/theme.rs"]