mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Save: ask again for a name the Flatpak file chooser did not grant
In the Flatpak build the save panel answers with a path in the document portal (/run/user/<uid>/doc/<id>/<name>), and the portal puts only that name into the folder picked. When the app changed the answer before writing, for example `poster` to `poster.pdf` or `Untitled-1.vectorcraft` to `Untitled-1.pdf` with PDF picked, the write succeeded inside the portal, the status bar said the file was saved, and no file appeared in the folder. Export, Save As, Save a Copy and the read-only Save retry now open the save panel again with the changed name suggested, and the status bar says why. The answer to that panel is written. A read-only retry that changes format still asks for the new format's options. Fixes #10868
This commit is contained in:
1 parent
3368ac519c
commit
4e6018cece
3 files changed
+150
-2
No files matched your search
@@ -235,9 +235,47 @@ fn write_step(app: &mut VectorcraftApp, uid: u64, path: &str, target: SaveTarget
|
||||
let path = with_picked_type(&answered, &suggested, picked_ext.as_deref(), first.format, mode);
|
||||
// The flow ends here: the questions and options dialogs that follow run on the path alone.
|
||||
app.saves.release(uid);
|
||||
if portal_renamed(&answered, &path) {
|
||||
let name = confirm_portal_name(app, &answered, &path);
|
||||
return ask_again(app, mode, ¶ms, ask, target, retrying, name);
|
||||
}
|
||||
replaces_unasked(app, &answered, &path, move |app, path| finish_picked(app, mode, ¶ms, ask, &path, retrying))
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
/// The save panel again for the active document's save (`mode`, `params`), suggesting `name` in the
|
||||
/// format its extension names, as a new flow with the same options question (`ask_options`) and
|
||||
/// `target`. A read-only retry (`retrying`) that changes format drops the options it carries and
|
||||
/// asks for the new format's, as [`finish_picked`] does.
|
||||
fn ask_again(
|
||||
app: &mut VectorcraftApp,
|
||||
mode: SaveMode,
|
||||
params: &Value,
|
||||
mut ask_options: bool,
|
||||
target: SaveTarget,
|
||||
retrying: bool,
|
||||
name: String,
|
||||
) -> Result<Value, String> {
|
||||
let original = plan(app, mode, &acknowledged(params))?.format.id;
|
||||
let mut q = if params.is_object() { params.clone() } else { json!({}) };
|
||||
if let Some(f) = fileio::format_for_name(&name) {
|
||||
if retrying && f.id != original {
|
||||
if let Some(o) = q.as_object_mut() {
|
||||
o.remove("options");
|
||||
}
|
||||
ask_options = true;
|
||||
}
|
||||
q["format"] = json!(f.id);
|
||||
}
|
||||
let first = plan(app, mode, &acknowledged(&q))?;
|
||||
let directory = if mode == SaveMode::Template {
|
||||
fileio::templates_dialog_folder(&app.session.prefs).map(PathBuf::from)
|
||||
} else {
|
||||
first.folder.clone().map(PathBuf::from)
|
||||
};
|
||||
start_flow(app, &first, Start { mode, params: &q, ask_options, target, retry: None, name, directory, retrying })
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
/// What [`start_flow`] needs besides the plan.
|
||||
struct Start<'a> {
|
||||
@@ -456,18 +494,51 @@ fn ask_path(
|
||||
return then(app, with_written_extension(&name, &ext, &keeps));
|
||||
}
|
||||
let mut request = PickRequest::save("export").file_name(name);
|
||||
request.filters = filters;
|
||||
if let Some(folder) = folder {
|
||||
request.filters = filters.clone();
|
||||
if let Some(folder) = folder.clone() {
|
||||
request = request.directory(folder);
|
||||
}
|
||||
Ok(file_asks::ask(app, request, Owner::Document, move |app, picked| {
|
||||
let Picked::Saved { path: picked, .. } = picked else { return Err("cancelled".into()) };
|
||||
let picked = picked.to_string_lossy().into_owned();
|
||||
let path = with_written_extension(&picked, &ext, &keeps);
|
||||
if portal_renamed(&picked, &path) {
|
||||
let name = confirm_portal_name(app, &picked, &path);
|
||||
return ask_path(app, None, name, folder, filters, ext, keeps, then);
|
||||
}
|
||||
replaces_unasked(app, &picked, &path, then)
|
||||
}))
|
||||
}
|
||||
|
||||
/// Whether `path` is a file in a folder of the document portal (`/run/user/<uid>/doc/<id>/` or
|
||||
/// `/run/flatpak/doc/<id>/`), where the save panel's answers point in a Flatpak sandbox. The portal
|
||||
/// puts only the name the panel answered into the folder picked in it: a file written there under
|
||||
/// another name is written without an error, and the folder gets no file.
|
||||
fn in_document_portal(path: &str) -> bool {
|
||||
let rest = path.strip_prefix("/run/flatpak/doc/").or_else(|| {
|
||||
let (uid, rest) = path.strip_prefix("/run/user/")?.split_once('/')?;
|
||||
if uid.is_empty() || !uid.bytes().all(|b| b.is_ascii_digit()) {
|
||||
return None;
|
||||
}
|
||||
rest.strip_prefix("doc/")
|
||||
});
|
||||
rest.and_then(|r| r.split_once('/')).is_some_and(|(id, name)| !id.is_empty() && !name.is_empty() && !name.contains('/'))
|
||||
}
|
||||
|
||||
/// Whether the save panel's answer `picked` was made into another name, `path`, in a folder of the
|
||||
/// document portal ([`in_document_portal`]).
|
||||
fn portal_renamed(picked: &str, path: &str) -> bool {
|
||||
path != picked && in_document_portal(picked)
|
||||
}
|
||||
|
||||
/// The file name of `path`, for the save panel to ask with again in place of the answer `picked`;
|
||||
/// the status bar says why the panel opens again.
|
||||
fn confirm_portal_name(app: &mut VectorcraftApp, picked: &str, path: &str) -> String {
|
||||
let name = fileio::file_name(path);
|
||||
app.status(format!("The file chooser can only save “{}”: confirm “{name}” to save it", fileio::file_name(picked)));
|
||||
name
|
||||
}
|
||||
|
||||
/// `path` as typed in a save panel for a file with extension `ext`: kept when its extension is
|
||||
/// `ext` in any letter case, or names the format `ext` names (`photo.jpeg` for a `.jpg` file) or
|
||||
/// a format `keeps` takes. An extension that names another format (`card.svg` for a `.png` file)
|
||||
|
||||
@@ -310,6 +310,31 @@ fn readonly_changed_format_asks_for_its_options_before_writing() {
|
||||
assert_eq!(files.lock().unwrap().attempts, ["/locked/Poster.svg"]);
|
||||
}
|
||||
|
||||
/// The retry's save panel in the document portal, with PDF picked and `Poster.svg` kept: the panel
|
||||
/// opens again for `Poster.pdf`, and its answer asks for the PDF options before writing.
|
||||
#[test]
|
||||
fn readonly_changed_format_in_the_document_portal_asks_for_its_options_before_writing() {
|
||||
let portal = "/run/user/1000/doc/a1b2c3d4";
|
||||
let (mut app, files) = app("svg", ErrorKind::PermissionDenied);
|
||||
let before = state(&app);
|
||||
let picker = scripted(&mut app);
|
||||
assert_eq!(app.run("file.save", json!({})).unwrap()["pending"], "fileDialog");
|
||||
let pdf = picker.requests()[0].filters.iter().position(|f| f.extensions.iter().any(|e| e == "pdf"));
|
||||
assert!(pdf.is_some(), "the retry's save panel offers PDF");
|
||||
picker.answer_saved(&format!("{portal}/Poster.svg"), pdf);
|
||||
poll(&mut app);
|
||||
assert!(fields_of(&app, "pdf-export").is_none(), "no options before the name is granted");
|
||||
assert_eq!(picker.requests().len(), 2, "the save panel opens again");
|
||||
assert_eq!(picker.requests()[1].file_name.as_deref(), Some("Poster.pdf"));
|
||||
picker.answer_saved(&format!("{portal}/Poster.pdf"), None);
|
||||
poll(&mut app);
|
||||
assert!(fields_of(&app, "pdf-export").is_some(), "the picked format's Save dialog");
|
||||
let job = app.save_pdf.as_ref().and_then(|open| open.job.as_ref());
|
||||
assert_eq!(job.and_then(|job| job.path.clone()), Some(format!("{portal}/Poster.pdf")));
|
||||
assert_eq!(state(&app), before);
|
||||
assert_eq!(files.lock().unwrap().attempts, ["/locked/Poster.svg"]);
|
||||
}
|
||||
|
||||
/// Typed saves keep the sidecar preflight both before the refused write and at the retry's
|
||||
/// destination. Confirming the ordinary Save still permits its one read-only fallback.
|
||||
#[test]
|
||||
|
||||
@@ -552,3 +552,55 @@ fn an_export_keeps_a_typed_extension_of_the_format_it_writes() {
|
||||
answer_top(&mut app, &[], true);
|
||||
assert_eq!(last(&w), "/tmp/sel.pdf", "Export Selection takes its format from the typed name");
|
||||
}
|
||||
|
||||
/// The file name the last save panel suggested.
|
||||
fn suggested_name(picker: &Picker) -> Option<String> {
|
||||
picker.requests().last()?.file_name.clone()
|
||||
}
|
||||
|
||||
/// In a Flatpak sandbox the save panel answers with a path in the document portal, which puts only
|
||||
/// that name into the folder picked. A name the export has to change (`poster` for a PDF) is not
|
||||
/// written: the save panel opens again suggesting `poster.pdf`, and its answer is written. A
|
||||
/// look-alike path outside the portal gets the extension at once.
|
||||
#[test]
|
||||
fn an_export_asks_again_for_a_name_the_document_portal_did_not_grant() {
|
||||
for portal in ["/run/user/1000/doc/a1b2c3d4", "/run/flatpak/doc/a1b2c3d4"] {
|
||||
let (mut app, picker, w) = saving_app();
|
||||
save_via(&mut app, &picker, |a| io::export_pdf(a, json!({})), (&format!("{portal}/poster"), None));
|
||||
assert!(w.borrow().is_empty(), "nothing is written under a name the portal did not grant: {:?}", w.borrow());
|
||||
assert_eq!(picker.requests().len(), 2, "the save panel opens again");
|
||||
assert_eq!(suggested_name(&picker).as_deref(), Some("poster.pdf"));
|
||||
assert!(app.ui.status.contains("poster.pdf"), "{}", app.ui.status);
|
||||
picker.answer_saved(&format!("{portal}/poster.pdf"), None);
|
||||
poll(&mut app);
|
||||
assert_eq!(w.borrow().as_slice(), [format!("{portal}/poster.pdf")]);
|
||||
assert_eq!(picker.requests().len(), 2, "a granted name is written without asking again");
|
||||
}
|
||||
let (mut app, picker, w) = saving_app();
|
||||
save_via(&mut app, &picker, |a| io::export_pdf(a, json!({})), ("/home/me/run/user/1000/doc/a1b2/poster", None));
|
||||
assert_eq!(w.borrow().as_slice(), ["/home/me/run/user/1000/doc/a1b2/poster.pdf"]);
|
||||
assert_eq!(picker.requests().len(), 1);
|
||||
}
|
||||
|
||||
/// Save As in the document portal with PDF picked and the suggested `Untitled-1.vectorcraft` kept:
|
||||
/// the save panel opens again suggesting `Untitled-1.pdf` with PDF as the file type, and the PDF
|
||||
/// options and the write follow its answer.
|
||||
#[test]
|
||||
fn save_as_asks_again_for_a_name_the_document_portal_did_not_grant() {
|
||||
let portal = "/run/user/1000/doc/a1b2c3d4";
|
||||
let (mut app, picker, w) = saving_app();
|
||||
let _ = app.run("file.saveAs", json!({}));
|
||||
let pdf = type_index(&picker, "pdf").expect("Save As offers PDF");
|
||||
picker.answer_saved(&format!("{portal}/Untitled-1.vectorcraft"), Some(pdf));
|
||||
poll(&mut app);
|
||||
assert!(w.borrow().is_empty(), "{:?}", w.borrow());
|
||||
assert!(dialog_path(&app).is_none(), "no PDF options before the name is granted");
|
||||
assert_eq!(picker.requests().len(), 2, "the save panel opens again");
|
||||
assert_eq!(suggested_name(&picker).as_deref(), Some("Untitled-1.pdf"));
|
||||
assert_eq!(type_index(&picker, "pdf"), Some(0), "PDF is the first file type offered");
|
||||
picker.answer_saved(&format!("{portal}/Untitled-1.pdf"), Some(0));
|
||||
poll(&mut app);
|
||||
assert_eq!(dialog_path(&app), Some(format!("{portal}/Untitled-1.pdf")));
|
||||
answer_top(&mut app, &[], true);
|
||||
assert_eq!(last(&w), format!("{portal}/Untitled-1.pdf"));
|
||||
}
|
||||
Reference in new issue
Block a user