//! The Lutra desktop app (layer L6): a Tauri 2 shell around the engine. //! //! Builds the main window with the theme tokens injected before first paint, //! bridges frontend IPC to engine commands, and optionally serves the JSON //! control channel (`--control PORT`). #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] mod args; mod host; mod screenshot; use std::process::ExitCode; use std::sync::Arc; use lutra_engine::{CommandInfo, ErrorPayload, theme}; use serde_json::Value; use tauri::window::Color; use tauri::{WebviewUrl, WebviewWindowBuilder}; use crate::host::{Host, MAIN_WINDOW, WINDOW_TITLE}; /// Parses the arguments, starts the control channel if asked, and runs the /// app until it quits. fn main() -> ExitCode { let args = match args::parse(std::env::args().skip(1)) { Ok(args) => args, Err(message) => { eprintln!("lutra: {message}\n\n{}", args::USAGE); return ExitCode::from(2); } }; if args.help { println!("{}", args::USAGE); return ExitCode::SUCCESS; } if args.version { println!("lutra-desktop {}", env!("CARGO_PKG_VERSION")); return ExitCode::SUCCESS; } match run(&args) { Ok(()) => ExitCode::SUCCESS, Err(message) => { eprintln!("lutra: {message}"); ExitCode::FAILURE } } } /// Starts the host, the control channel and the Tauri event loop. /// /// # Errors /// A message if the engine cannot start, the control port cannot be bound, /// or Tauri fails. fn run(args: &args::Args) -> Result<(), String> { let host = Arc::new(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()); } if let Some(path) = &args.path { eprintln!( "lutra: opening {} arrives with milestone M1; starting without a project", path.display() ); } let theme = theme::DARK; let setup_host = host.clone(); tauri::Builder::default() .manage(host) .invoke_handler(tauri::generate_handler![ engine_execute, engine_list_commands ]) .setup(move |app| { setup_host.attach(app.handle().clone()); let mut window = WebviewWindowBuilder::new(app, MAIN_WINDOW, WebviewUrl::default()) .title(WINDOW_TITLE) .inner_size(1200.0, 800.0) .min_inner_size(640.0, 400.0) .initialization_script(theme::init_script(&theme)); if let Some(color) = token_color(&theme, "bg") { window = window.background_color(color); } window.build()?; Ok(()) }) .run(tauri::generate_context!()) .map_err(|e| e.to_string()) } /// Reads a `#rrggbb` theme token as an opaque colour for the window /// background, so that no white frame shows before the page paints. fn token_color(theme: &theme::Theme, name: &str) -> Option { let (_, value) = theme.tokens.iter().find(|(n, _)| *n == name)?; let hex = value.strip_prefix('#')?; let byte = |at: usize| u8::from_str_radix(hex.get(at..at + 2)?, 16).ok(); Some(Color(byte(0)?, byte(2)?, byte(4)?, 255)) } /// IPC: runs an engine command for the frontend. #[tauri::command] fn engine_execute( host: tauri::State<'_, Arc>, command: String, params: Value, ) -> Result { host.run(&command, params).map_err(|e| e.payload()) } /// IPC: lists the engine's commands for the frontend. #[tauri::command] fn engine_list_commands(host: tauri::State<'_, Arc>) -> Vec { host.commands() } #[cfg(test)] mod tests { use super::*; #[test] fn reads_theme_colours() { assert_eq!( token_color(&theme::DARK, "bg"), Some(Color(0x1f, 0x1d, 0x1b, 255)) ); assert_eq!(token_color(&theme::DARK, "missing"), None); } }