M1.9a: engine dirs and settings backend

EngineConfig and Engine::with_config; the desktop resolves Tauri's app
config and data dirs. Settings model (appearance.theme, editor.fontSize,
ui.language) with typed validation, persistence in settings.json via
write_atomic (1 MiB cap, unknown keys kept, corrupt file backed up with a
warning notification), settings.list/get/set/reset, settingsChanged and
notification events. Property test over random settings.json content,
control-channel test for persistence and corrupt-file recovery.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-16 15:23:43 +02:00
parent 851d438e2a
commit f9357ee4b5
17 changed files with 1406 additions and 41 deletions

View file

@ -49,19 +49,15 @@ pub struct Host {
}
impl Host {
/// A host with a fresh engine.
///
/// # Errors
/// The engine's error if the built-in commands cannot be registered.
pub fn new() -> Result<Arc<Self>, EngineError> {
let engine = Engine::new()?;
Ok(Arc::new_cyclic(|me| Self {
/// A host around `engine`.
pub fn new(engine: Engine) -> Arc<Self> {
Arc::new_cyclic(|me| Self {
engine: Mutex::new(engine),
app: OnceLock::new(),
subscribers: Subscribers::default(),
me: me.clone(),
picking: AtomicBool::new(false),
}))
})
}
/// Records the app handle once Tauri has started. Later calls are

View file

@ -12,12 +12,12 @@ mod host;
mod screenshot;
use std::process::ExitCode;
use std::sync::Arc;
use std::sync::{Arc, OnceLock};
use lutra_engine::{CommandInfo, ErrorPayload, theme};
use lutra_engine::{CommandInfo, Engine, EngineConfig, ErrorPayload, Event, theme};
use serde_json::Value;
use tauri::window::Color;
use tauri::{WebviewUrl, WebviewWindowBuilder};
use tauri::{Manager, WebviewUrl, WebviewWindowBuilder};
use crate::host::{Host, MAIN_WINDOW, WINDOW_TITLE};
@ -48,32 +48,28 @@ fn main() -> ExitCode {
}
}
/// Starts the host, the control channel and the Tauri event loop.
/// Builds the Tauri app, starts the engine with the app's config and data
/// directories, the host, the control channel, and runs the event loop.
/// The window is created in the setup hook, which runs once the event loop
/// has started.
///
/// # Errors
/// A message if the engine cannot start, the control port cannot be bound,
/// or Tauri fails.
/// A message if Tauri cannot build the app or resolve its directories, the
/// engine cannot start, or the control port cannot be bound.
fn run(args: &args::Args) -> Result<(), String> {
let host = Host::new().map_err(|e| e.to_string())?;
if let Some(port) = args.control {
let server = lutra_control::start(port, host.clone())
.map_err(|e| format!("cannot serve the control channel on port {port}: {e}"))?;
eprintln!("lutra: control channel on {}", server.local_addr());
}
host.start_pump()
.map_err(|e| format!("cannot start watching for changes: {e}"))?;
let slot: Arc<OnceLock<Arc<Host>>> = Arc::new(OnceLock::new());
let setup_slot = slot.clone();
let theme = theme::DARK;
let setup_host = host.clone();
let initial_path = args.path.clone();
tauri::Builder::default()
let app = tauri::Builder::default()
.plugin(tauri_plugin_dialog::init())
.manage(host)
.invoke_handler(tauri::generate_handler![
engine_execute,
engine_list_commands,
engine_ready
])
.setup(move |app| {
let setup_host = setup_slot.get().ok_or("the host is not ready")?;
setup_host.attach(app.handle().clone());
let mut window = WebviewWindowBuilder::new(app, MAIN_WINDOW, WebviewUrl::default())
.title(WINDOW_TITLE)
@ -91,8 +87,56 @@ fn run(args: &args::Args) -> Result<(), String> {
}
Ok(())
})
.run(tauri::generate_context!())
.map_err(|e| e.to_string())
.build(tauri::generate_context!())
.map_err(|e| e.to_string())?;
let mut engine = Engine::with_config(engine_config(&app)?).map_err(|e| e.to_string())?;
log_startup_events(&engine.take_events());
let host = Host::new(engine);
app.manage(host.clone());
if slot.set(host.clone()).is_err() {
return Err("the host was set twice".into());
}
if let Some(port) = args.control {
let server = lutra_control::start(port, host.clone())
.map_err(|e| format!("cannot serve the control channel on port {port}: {e}"))?;
eprintln!("lutra: control channel on {}", server.local_addr());
}
host.start_pump()
.map_err(|e| format!("cannot start watching for changes: {e}"))?;
app.run(|_, _| {});
Ok(())
}
/// The engine directories from Tauri's path resolver: the app config dir
/// for settings, the app data dir for the recovery journal.
///
/// # Errors
/// A message if the platform has no such directories.
fn engine_config(app: &tauri::App) -> Result<EngineConfig, String> {
let paths = app.path();
Ok(EngineConfig {
config_dir: paths
.app_config_dir()
.map_err(|e| format!("no config directory: {e}"))?,
data_dir: paths
.app_data_dir()
.map_err(|e| format!("no data directory: {e}"))?,
})
}
/// Logs the notifications the engine queued while starting, such as the
/// fallback to default settings, to stderr.
fn log_startup_events(events: &[Event]) {
for event in events {
if let Event::Notification {
level,
message: Some(message),
..
} = event
{
eprintln!("lutra: {level:?}: {message}");
}
}
}
/// Reads a `#rrggbb` theme token as an opaque colour for the window