diff --git a/crates/ui-egui/src/dialogs/tracing_presets.rs b/crates/ui-egui/src/dialogs/tracing_presets.rs index d8ff9b4d49..2db0261f2b 100644 --- a/crates/ui-egui/src/dialogs/tracing_presets.rs +++ b/crates/ui-egui/src/dialogs/tracing_presets.rs @@ -89,6 +89,8 @@ pub(crate) struct Manager { data: RowsData, /// What a button asked for, taken by [`pump`]. action: Option, + /// Identifies this opening when an Import picker finishes later. + import_owner: Arc<()>, } impl StackDialog for Manager { @@ -180,8 +182,13 @@ impl StackDialog for Manager { /// Open the presets manager on `selected` (default: the first preset, Default). pub(crate) fn open(app: &mut VectorcraftApp, selected: Option<&str>) { - let manager = - Manager { list: image_trace::presets(app), selected: selected.unwrap_or("Default").to_string(), data: RowsData::of(app), action: None }; + let manager = Manager { + list: image_trace::presets(app), + selected: selected.unwrap_or("Default").to_string(), + data: RowsData::of(app), + action: None, + import_owner: Arc::new(()), + }; match app.dialogs() { Some(dialogs) => { dialogs.with(|host| host.open(manager)); @@ -251,10 +258,11 @@ fn run(app: &mut VectorcraftApp, act: Action, current: &str) -> Result<(), Strin select(app, next.as_deref().unwrap_or("Default")); } Action::Import => { + let Some(owner) = with_manager(app, |m| Arc::clone(&m.import_owner)) else { return Ok(()) }; let name = file_asks::filter_name(crate::i18n::current(), "Tracing presets"); let request = PickRequest::open("tracingPresets").filter(name, TRACE_PRESET_EXTS); // The answer, or its failure, reaches the status bar from the continuation; nothing is pending here. - let _ = file_asks::ask(app, request, Owner::Free, picked); + let _ = file_asks::ask(app, request, Owner::Free, move |app, answer| picked(app, answer, ImportSource::Picker(owner))); } Action::Export => { crate::io::save_command_output(app, "imageTrace.presets.export", TRACE_PRESET_FORMAT, json!({}), |_, _| Ok(Value::Null))?; @@ -263,20 +271,37 @@ fn run(app: &mut VectorcraftApp, act: Action, current: &str) -> Result<(), Strin Ok(()) } -/// The file Import… picked: a path on the desktop, the file's bytes on the web. -fn picked(app: &mut VectorcraftApp, picked: Picked) -> Result { - match picked { - Picked::Paths(paths) => { - if let Some(path) = paths.first() { - open_path(app, &path.to_string_lossy())?; - } +enum ImportSource { + FileOpen, + Picker(Arc<()>), +} + +impl ImportSource { + fn matches(&self, manager: &Manager) -> bool { + match self { + Self::FileOpen => true, + Self::Picker(owner) => Arc::ptr_eq(owner, &manager.import_owner), } - Picked::Contents(files) => { - if let Some((_, bytes)) = files.first() { - open_file(app, bytes)?; + } + + fn select(&self, app: &VectorcraftApp, name: &str) { + let _ = with_manager(app, |manager| { + if self.matches(manager) { + manager.selected = name.to_string(); } - } - _ => {} + }); + } +} + +/// The file Import… picked, a path on the desktop or the file's bytes on the web. +fn picked(app: &mut VectorcraftApp, picked: Picked, source: ImportSource) -> Result { + let file = match picked { + Picked::Paths(paths) => paths.first().map(|path| file_contents(app, json!({ "path": path.to_string_lossy() }))).transpose()?, + Picked::Contents(files) => files.first().map(|(_, bytes)| checked_contents(bytes)).transpose()?, + _ => None, + }; + if let Some(file) = file { + open_import(app, file, source)?; } Ok(Value::Null) } @@ -294,6 +319,7 @@ fn import_file(app: &mut VectorcraftApp, file: &Value) -> Result<(Value, Vec, + source: ImportSource, } /// The Replace question. Its own type, so the host keeps its answer apart from every other prompt's. @@ -301,22 +327,23 @@ pub(crate) struct ReplacePresets(pub(crate) StandardPrompt); crate::dialogs::confirm::wrapped_dialog!(ReplacePresets, StandardPrompt, REPLACE_KIND); -/// Asks whether the import of `file` replaces the saved presets `skipped` kept, over the manager -/// when it is open. -fn ask_replace(app: &VectorcraftApp, skipped: &[Value], file: Value) { +/// Asks whether the import of `file` replaces the saved presets `skipped` kept. It opens over the +/// manager if the import targets that opening. +fn ask_replace(app: &VectorcraftApp, skipped: &[Value], file: Value, source: ImportSource) { let names: Vec