Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Merge PR #666

Claude44acde3a41d9
diff --cc apps/lightcraft/src/main.rs
index 51fffa840,75f3e5c61..288cb095f
--- a/apps/lightcraft/src/main.rs
+++ b/apps/lightcraft/src/main.rs
@@@ -817,83 -754,66 +817,80 @@@ fn main() -> eframe::Result
};
// the app's copy: `log_file` is still named in the message of a start that fails
let app_log_file = log_file.clone();
- let started = eframe::run_native(
- "LightCraft",
- options,
- Box::new(move |cc| {
- let (mut session, problem) = open_session(in_memory, library_dir, seed_demo && files.is_empty());
- // AI masks: the SAM 3 checkpoint (facebook/sam3) in <config>/models/sam3, or LIGHTCRAFT_SAM3_DIR
- // (never required: without it, AI masks offer to download it; see docs/ai-masks.md)
- // and the user's own download locations, one base URL per line (LIGHTCRAFT_SAM3_MIRRORS too)
- session = session.with_default_sam3_model();
- let mut app = LightcraftApp::new(session, services(cc.egui_ctx.clone(), app_log_file.as_deref()));
- if let Some(ui) = prefs {
- app.ui = ui;
- }
- lightcraft_ui_egui::i18n::set_language(app.ui.language);
- app.integrated_titlebar = cfg!(target_os = "macos");
- app.notices.extend(prefs_warning);
- app.notices.extend(launch_notices);
- // what's on disk now: only changes are written
- let writer = PrefsWriter {
- path: if in_memory { None } else { prefs_path() },
- written: serde_json::to_vec_pretty(&app.ui).unwrap_or_default(),
- library: app.ui.settings.library_path.clone(),
- keep_file: keep_prefs_file,
- ..Default::default()
- };
- // after the snapshot above, so the writer saves the switched-off preference
- if let Some(notice) = gpu_crash {
- app.ui.settings.gpu = false;
- app.notices.push(notice);
- }
- if let Some(port) = control_port {
- let rx = control_server::start(port, cc.egui_ctx.clone());
- app = app.with_control(rx);
- }
- if let Some(mut p) = problem {
- // imported once the user has chosen where (into the library, or the temporary session)
- p.pending_import = files;
- app.library_problem = Some(p);
- } else if !files.is_empty() {
- // listed and read on the import worker once the window shows (issue #374: never
- // here, before the first frame, where a large folder kept the window from
- // appearing)
- if let Err(e) = lightcraft_ui_egui::import::start_paths(&mut app, files) {
- log::warn!("import at launch: {e}");
+ let started = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ eframe::run_native(
+ "LightCraft",
+ options,
+ Box::new(move |cc| {
+ // the window's renderer is up (eframe calls this after creating it)
+ win_files_app.started(window_renderer);
+ let (mut session, problem) = open_session(in_memory, library_dir, seed_demo && files.is_empty());
+ // AI masks: the SAM 3 checkpoint (facebook/sam3) in <config>/models/sam3, or LIGHTCRAFT_SAM3_DIR
+ // (never required: without it, AI masks offer to download it; see docs/ai-masks.md)
- session.segmenter.dir = std::env::var_os("LIGHTCRAFT_SAM3_DIR")
- .map(std::path::PathBuf::from)
- .or_else(|| config_dir().map(|d| d.join("models").join("sam3")));
- // the user's own download locations, one base URL per line (LIGHTCRAFT_SAM3_MIRRORS too)
- session.segmenter.mirrors_file = config_dir().map(|d| d.join("models").join("sam3-mirrors.txt"));
++ // and the user's own download locations, one base URL per line (LIGHTCRAFT_SAM3_MIRRORS too)
++ session = session.with_default_sam3_model();
+ let mut app = LightcraftApp::new(session, services(cc, app_log_file.as_deref()));
+ if let Some(ui) = prefs {
+ app.ui = ui;
}
- app.ui.view = lightcraft_ui_egui::state::ViewMode::PhotoGrid;
- }
- // native menu bar generated from the command registry (macOS; elsewhere the menus are
- // drawn in the window's top bar). LIGHTCRAFT_NO_NATIVE_MENU=1 keeps the in-window menus.
- #[cfg(target_os = "macos")]
- let menu = (std::env::var_os("LIGHTCRAFT_NO_NATIVE_MENU").is_none()).then(|| native_menu::NativeMenu::install(&mut app, &cc.egui_ctx));
- Ok(Box::new(App(
- app,
- writer,
+ lightcraft_ui_egui::i18n::set_language(app.ui.language);
+ app.integrated_titlebar = cfg!(target_os = "macos");
+ app.notices.extend(prefs_warning);
+ app.notices.extend(launch_notices);
+ app.notices.extend(window_notice);
+ // what's on disk now: only changes are written
+ let writer = PrefsWriter {
+ path: if in_memory { None } else { prefs_path() },
+ written: serde_json::to_vec_pretty(&app.ui).unwrap_or_default(),
+ library: app.ui.settings.library_path.clone(),
+ keep_file: keep_prefs_file,
+ ..Default::default()
+ };
+ // after the snapshot above, so the writer saves the switched-off preference
+ if let Some(notice) = gpu_crash {
+ app.ui.settings.gpu = false;
+ app.notices.push(notice);
+ }
+ if let Some(port) = control_port {
+ let rx = control_server::start(port, cc.egui_ctx.clone());
+ app = app.with_control(rx);
+ }
+ if let Some(mut p) = problem {
+ // imported once the user has chosen where (into the library, or the temporary session)
+ p.pending_import = files;
+ app.library_problem = Some(p);
+ } else if !files.is_empty() {
+ // listed and read on the import worker once the window shows (issue #374: never
+ // here, before the first frame, where a large folder kept the window from
+ // appearing)
+ if let Err(e) = lightcraft_ui_egui::import::start_paths(&mut app, files) {
+ log::warn!("import at launch: {e}");
+ }
+ app.ui.view = lightcraft_ui_egui::state::ViewMode::PhotoGrid;
+ }
+ // native menu bar generated from the command registry (macOS; elsewhere the menus are
+ // drawn in the window's top bar). LIGHTCRAFT_NO_NATIVE_MENU=1 keeps the in-window menus.
#[cfg(target_os = "macos")]
- menu,
- )))
- }),
- );
+ let menu =
+ (std::env::var_os("LIGHTCRAFT_NO_NATIVE_MENU").is_none()).then(|| native_menu::NativeMenu::install(&mut app, &cc.egui_ctx));
+ Ok(Box::new(App(
+ app,
+ writer,
+ #[cfg(target_os = "macos")]
+ menu,
+ )))
+ }),
+ )
+ }));
+ // a panic that still escaped the renderer's start (the probe and error handler should
+ // prevent it): an error like any other failed start
+ let started = started.unwrap_or_else(|_| Err(eframe::Error::AppCreation("the window's renderer panicked while starting".into())));
if let Err(e) = &started {
+ // the renderer never came up: start again, which tries the next one in the chain
+ if window_gpu::relaunch_after_failure(&win_files) {
+ log::error!("LightCraft could not open its window with {}: {e}; trying the next renderer", window_renderer.name());
+ return Ok(());
+ }
startup_failed(&e.to_string(), log_file.as_deref());
}
started
diff --cc crates/engine/src/config.rs
index 2a4ccb6b4,5931e01c7..091bf5d00
--- a/crates/engine/src/config.rs
+++ b/crates/engine/src/config.rs
@@@ -3,10 -3,9 +3,10 @@@
//!
//! Hosts that have a file system (the desktop app, the CLI, the MCP server) share these defaults so a model
//! installed from one is there for the others. Nothing here is applied automatically: a [`Session`] has no
- //! face- or denoise-models folder until a host asks for one ([`Session::with_default_face_models`],
- //! [`Session::with_default_denoise_models`]), so tests stay hermetic.
+ //! face-, denoise- or SAM 3 model folder until a host asks for one ([`Session::with_default_face_models`],
+ //! [`Session::with_default_denoise_models`], [`Session::with_default_sam3_model`]), so tests stay hermetic.
+use std::ffi::OsString;
use std::path::PathBuf;
use crate::Session;
@@@ -78,55 -75,18 +96,71 @@@ impl Session
}
}
+#[cfg(test)]
+mod config_tests {
+ use super::*;
+
+ fn resolve(platform: Platform, vars: &[(&str, &str)]) -> Option<PathBuf> {
+ config_dir_with(platform, |name| vars.iter().find_map(|(key, value)| (*key == name).then(|| OsString::from(value))))
+ }
+
+ #[test]
+ fn config_override_wins_without_reading_user_profile() {
+ for platform in [Platform::Mac, Platform::Windows, Platform::Other] {
+ let actual = config_dir_with(platform, |name| {
+ assert_eq!(name, "LIGHTCRAFT_CONFIG_DIR");
+ Some(OsString::from("isolated/settings"))
+ });
+ assert_eq!(actual, Some(PathBuf::from("isolated/settings")));
+ }
+ }
+
+ #[test]
+ fn config_empty_override_disables_profile_without_reading_defaults() {
+ for platform in [Platform::Mac, Platform::Windows, Platform::Other] {
+ assert_eq!(
+ config_dir_with(platform, |name| {
+ assert_eq!(name, "LIGHTCRAFT_CONFIG_DIR");
+ Some(OsString::new())
+ }),
+ None
+ );
+ }
+ }
+
+ #[test]
+ fn config_unset_override_keeps_platform_defaults() {
+ let vars = [("HOME", "/user"), ("APPDATA", "/roaming"), ("XDG_CONFIG_HOME", "/xdg")];
+ assert_eq!(resolve(Platform::Mac, &vars), Some(PathBuf::from("/user/Library/Application Support/LightCraft")));
+ assert_eq!(resolve(Platform::Windows, &vars), Some(PathBuf::from("/roaming/LightCraft")));
+ assert_eq!(resolve(Platform::Other, &vars), Some(PathBuf::from("/xdg/lightcraft")));
+ assert_eq!(resolve(Platform::Other, &[("HOME", "/user")]), Some(PathBuf::from("/user/.config/lightcraft")));
+ for platform in [Platform::Mac, Platform::Windows, Platform::Other] {
+ assert_eq!(resolve(platform, &[]), None);
+ }
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn config_override_preserves_non_unicode_paths() {
+ use std::os::unix::ffi::OsStringExt as _;
+ let value = OsString::from_vec(b"isolated/\xff".to_vec());
+ assert_eq!(config_dir_with(Platform::Mac, |_| Some(value.clone())), Some(PathBuf::from(value)));
+ }
+}
++
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+
+ /// A non-empty `LIGHTCRAFT_SAM3_DIR` wins; unset or empty falls back to `<config>/models/sam3` (issue #619).
+ #[test]
+ fn sam3_dir_prefers_the_override() {
+ let config = PathBuf::from("cfg");
+ let fallback = Some(config.join("models").join("sam3"));
+ assert_eq!(sam3_dir(Some("elsewhere".into()), Some(config.clone())), Some(PathBuf::from("elsewhere")));
+ assert_eq!(sam3_dir(None, Some(config.clone())), fallback);
+ assert_eq!(sam3_dir(Some(std::ffi::OsString::new()), Some(config)), fallback);
+ assert_eq!(sam3_dir(None, None), None);
+ }
+ }