diff --git a/apps/solidcraft/src/main.rs b/apps/solidcraft/src/main.rs index ea12cf4b7..74d835da5 100644 --- a/apps/solidcraft/src/main.rs +++ b/apps/solidcraft/src/main.rs @@ -92,6 +92,8 @@ struct App { native_menu: Option, /// A device's own app moving the view (Preferences › Navigation, off by default). nav_bridge: nav_bridge::NavBridge, + /// Where the next file dialog opens: it follows the open design (#7756). + dialog_folder: DialogFolder, } impl eframe::App for App { @@ -147,6 +149,7 @@ impl eframe::App for App { menu.tick(&mut self.solve, ctx); } self.solve.logic(ctx); + self.dialog_folder.design(self.solve.session.path.as_deref()); #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] if !self.solve.file_picker_busy() { self.space_mouse.tick(&mut self.solve, ctx); @@ -241,6 +244,60 @@ impl App { } } +/// Where the next file dialog opens (#7756): the folder of the file last chosen in one, or of the +/// design last opened or saved, whichever came last. Linux dialogs otherwise open in the home +/// folder every time; macOS and Windows remember a folder of their own, which this agrees with. +#[derive(Clone, Default)] +struct DialogFolder(std::sync::Arc>); + +#[derive(Default)] +struct DialogFolderState { + folder: Option, + /// The open design's path when last looked at, to notice another design opened or saved. + design: Option, +} + +impl DialogFolder { + /// A file was chosen in a dialog: the next one opens in its folder. + fn chosen(&self, file: &std::path::Path) { + if let (Some(folder), Ok(mut state)) = (file.parent().filter(|f| !f.as_os_str().is_empty()), self.0.lock()) { + state.folder = Some(folder.to_path_buf()); + } + } + + /// The open design's path, every frame: a design opened or saved somewhere else moves the + /// next dialog to its folder; the same design again leaves a folder chosen since alone. + fn design(&self, path: Option<&str>) { + let Ok(mut state) = self.0.lock() else { return }; + if state.design.as_deref() == path { + return; + } + state.design = path.map(str::to_string); + if let Some(folder) = path.and_then(|p| std::path::Path::new(p).parent()).filter(|f| !f.as_os_str().is_empty()) { + state.folder = Some(folder.to_path_buf()); + } + } + + /// A dialog's answer as a path, its folder remembered for the next dialog. + fn answer(&self, picked: Option) -> Option { + let picked = picked?; + self.chosen(picked.path()); + Some(picked.path().to_string_lossy().into_owned()) + } + + fn folder(&self) -> Option { + self.0.lock().ok().and_then(|state| state.folder.clone()) + } + + /// The dialog, opening in the remembered folder when there is one. + fn start(&self, dialog: rfd::AsyncFileDialog) -> rfd::AsyncFileDialog { + match self.folder() { + Some(folder) => dialog.set_directory(folder), + None => dialog, + } + } +} + /// Linux file dialogs match patterns by case, so each extension goes in as lower and upper /// case, plus FreeCAD's own mixed casing. fn case_variants(exts: &[&str]) -> Vec { @@ -290,15 +347,16 @@ fn fullscreen_content_insets(raw: &mut egui::RawInput, title_height: f32, zoom: raw.safe_area_insets = Some(insets); } -fn services(cc: &eframe::CreationContext<'_>) -> Services { +fn services(cc: &eframe::CreationContext<'_>, folder: &DialogFolder) -> Services { let parent = rfd::AsyncFileDialog::new().set_parent(cc); let open_parent = parent.clone(); let program_parent = parent.clone(); let file_parent = parent.clone(); + let (open_folder, file_folder, save_folder) = (folder.clone(), folder.clone(), folder.clone()); Services { pick_open: Some(Box::new(move |first: &[&str]| { // The file type the command reads comes first, so the picker opens on it (#1801). - let mut dialog = open_parent.clone(); + let mut dialog = open_folder.start(open_parent.clone()); if !first.is_empty() { let label = first.iter().map(|e| e.to_uppercase()).collect::>().join(", "); dialog = dialog.add_filter(label, &case_variants(first)); @@ -311,7 +369,8 @@ fn services(cc: &eframe::CreationContext<'_>) -> Services { dialog = dialog.add_filter(label, &case_variants(&exts)); } let future = dialog.add_filter("All files", &["*"]).pick_file(); - Box::pin(async move { future.await.map(|p| p.path().to_string_lossy().into_owned()) }) + let folder = open_folder.clone(); + Box::pin(async move { folder.answer(future.await) }) })), // One kind of file (the agent chat's attachments): only the types it names, as given and in // lower and upper case (Linux dialogs match case; macOS allows just the listed types). @@ -326,8 +385,9 @@ fn services(cc: &eframe::CreationContext<'_>) -> Services { } let all: Vec<&str> = all.iter().map(String::as_str).collect(); // Any other file attaches by reference, so the list is not the only choice. - let future = file_parent.clone().add_filter(label, &all).add_filter("All files", &["*"]).pick_file(); - Box::pin(async move { future.await.map(|p| p.path().to_string_lossy().into_owned()) }) + let future = file_folder.start(file_parent.clone()).add_filter(label, &all).add_filter("All files", &["*"]).pick_file(); + let folder = file_folder.clone(); + Box::pin(async move { folder.answer(future.await) }) })), pick_program: Some(Box::new(move || { let dialog = program_parent.clone().set_title("Choose your slicer"); @@ -336,7 +396,7 @@ fn services(cc: &eframe::CreationContext<'_>) -> Services { Box::pin(async move { future.await.map(|p| p.path().to_string_lossy().into_owned()) }) })), pick_save: Some(Box::new(move |name: &str, exts: &[&str]| { - let dialog = parent.clone().set_file_name(name); + let dialog = save_folder.start(parent.clone()).set_file_name(name); let dialog = if exts == [io::DESIGN_EXT, "json"] { if name.ends_with(".json") { dialog.add_filter("Plain JSON design", &["json"]).add_filter("SolidCraft container", &[io::DESIGN_EXT]) @@ -347,7 +407,8 @@ fn services(cc: &eframe::CreationContext<'_>) -> Services { dialog.add_filter("File", exts) }; let future = dialog.save_file(); - Box::pin(async move { future.await.map(|p| p.path().to_string_lossy().into_owned()) }) + let folder = save_folder.clone(); + Box::pin(async move { folder.answer(future.await) }) })), graphics_adapters: Vec::new(), graphics_path: graphics_path().map(|p| p.to_string_lossy().to_string()), @@ -841,7 +902,8 @@ fn main() -> eframe::Result { solidcraft_ui_egui::fonts::setup_bundled_fonts(&cc.egui_ctx); solidcraft_ui_egui::fonts::load_system_fonts_in_background(&cc.egui_ctx); mark("fonts"); - let mut app = SolveApp::new(Session::default(), services(cc)); + let dialog_folder = DialogFolder::default(); + let mut app = SolveApp::new(Session::default(), services(cc, &dialog_folder)); app.apply_off_thread = Some(solidcraft_ui_egui::dialogs::APPLY_AT_ONCE); app.custom_titlebar = custom_titlebar; #[cfg(target_os = "windows")] @@ -994,6 +1056,7 @@ fn main() -> eframe::Result { #[cfg(target_os = "macos")] native_menu, nav_bridge, + dialog_folder, })) }), ); @@ -1172,6 +1235,40 @@ mod backend_tests { } } +#[cfg(test)] +mod dialog_folder_tests { + use super::DialogFolder; + use std::path::{Path, PathBuf}; + + /// #7756: a file dialog opens where the last one left off, not in the home folder. + #[test] + fn the_next_dialog_opens_in_the_folder_of_the_file_chosen_last() { + let folder = DialogFolder::default(); + assert_eq!(folder.folder(), None, "before any choice the system decides"); + folder.chosen(Path::new("/work/prints/bracket.stl")); + assert_eq!(folder.folder(), Some(PathBuf::from("/work/prints"))); + // A bare file name has no folder to remember. + folder.chosen(Path::new("bracket.step")); + assert_eq!(folder.folder(), Some(PathBuf::from("/work/prints"))); + } + + /// Save As starts beside the open design (#7756), and a folder chosen since wins until another + /// design is opened or saved. + #[test] + fn the_open_design_moves_the_folder_only_when_it_changes() { + let folder = DialogFolder::default(); + folder.design(Some("/work/lid/lid.solidcraft")); + assert_eq!(folder.folder(), Some(PathBuf::from("/work/lid"))); + folder.chosen(Path::new("/exports/lid.3mf")); + folder.design(Some("/work/lid/lid.solidcraft")); + assert_eq!(folder.folder(), Some(PathBuf::from("/exports")), "the same design again leaves the export folder"); + folder.design(None); + assert_eq!(folder.folder(), Some(PathBuf::from("/exports")), "a new, unsaved design has no folder of its own"); + folder.design(Some("/work/base/base.solidcraft")); + assert_eq!(folder.folder(), Some(PathBuf::from("/work/base"))); + } +} + #[cfg(test)] mod viewport_tests { #[cfg(target_os = "macos")]