mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Script File Access: keep chooser answers with their opening
This commit is contained in:
1 parent
5c7bbeecaa
commit
dfff52639f
2 files changed
+36
-2
No files matched your search
@@ -29,6 +29,7 @@
|
||||
//! are not the dialog's default. `selectDlg` opens the host's folder picker in its own folder.
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft::host::{PickRequest, Picked};
|
||||
use craft::theme::variant::Tone;
|
||||
@@ -276,6 +277,7 @@ impl StackDialog for ScriptNotice {
|
||||
/// The dialog of a script's file dialog call: what the run asked for, read from the run's own
|
||||
/// request when the dialog opens, so no field of the dialog changes what the person reads.
|
||||
pub(crate) struct ScriptFiles {
|
||||
opening: Arc<()>,
|
||||
kind: PickerKind,
|
||||
prompt: String,
|
||||
can_always: bool,
|
||||
@@ -290,6 +292,7 @@ pub(crate) struct ScriptFiles {
|
||||
impl ScriptFiles {
|
||||
fn new(p: ScriptPicker) -> Self {
|
||||
Self {
|
||||
opening: Arc::new(()),
|
||||
kind: p.kind,
|
||||
prompt: p.prompt,
|
||||
can_always: p.can_always,
|
||||
@@ -719,8 +722,11 @@ pub(crate) fn settle_files(app: &mut VectorcraftApp, result: DialogResult<FilesA
|
||||
}
|
||||
|
||||
/// Asks for the files of the kind the running script asked for, starting at the script's file or
|
||||
/// folder, with the script's file types; the answer goes to the open file dialog.
|
||||
/// folder, with the script's file types; the answer goes to the dialog that opened the picker.
|
||||
fn choose(app: &mut VectorcraftApp) {
|
||||
let Some(opening) = with_host(app, |host, _| host.get::<ScriptFiles>().map(|dialog| Arc::clone(&dialog.opening))).flatten() else {
|
||||
return;
|
||||
};
|
||||
let Some(p) = app.session.script_picker() else { return };
|
||||
let several = p.multiple && p.kind == PickerKind::Open;
|
||||
let mut request = match p.kind {
|
||||
@@ -750,7 +756,11 @@ fn choose(app: &mut VectorcraftApp) {
|
||||
file_asks::ask(app, request, Owner::Free, move |app, picked| {
|
||||
let chosen = picked.paths().iter().map(|path| path.to_string_lossy().into_owned()).collect();
|
||||
let automated = app.file_asks.automated();
|
||||
with_host(app, |host, _| host.get_mut::<ScriptFiles>().map(|dialog| dialog.add(chosen, several, automated)));
|
||||
with_host(app, |host, _| {
|
||||
if let Some(dialog) = host.get_mut::<ScriptFiles>().filter(|dialog| Arc::ptr_eq(&dialog.opening, &opening)) {
|
||||
dialog.add(chosen, several, automated);
|
||||
}
|
||||
});
|
||||
Ok(Value::Null)
|
||||
});
|
||||
}
|
||||
@@ -70,6 +70,30 @@ fn allow_once(app: &mut VectorcraftApp) {
|
||||
script_run::settle_files(app, DialogResult::Ok(FilesAnswer { answer: "once", paths, left_out: false, automated: false }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_script_file_reply_does_not_populate_a_reopened_dialog() {
|
||||
let mut app = app();
|
||||
let picker = scripted(&mut app);
|
||||
app.session.prefs.show_external_script_warning = false;
|
||||
menus::run_ui_command(&mut app, "ui.runScript", &json!({"source": "File.openDialog('Choose');"})).unwrap().unwrap();
|
||||
script_runs::poll(&mut app);
|
||||
script_run::with_host(&app, |host, _| host.get_mut::<ScriptFiles>().unwrap().choose = true).unwrap();
|
||||
pump(&mut app);
|
||||
assert_eq!(picker.requests().len(), 1);
|
||||
|
||||
script_run::forget::<ScriptFiles>(&app);
|
||||
script_run::open_picker(&app, app.session.script_picker().unwrap());
|
||||
let old_path = std::env::temp_dir().join("original-script-picker.txt");
|
||||
picker.answer(&[&old_path.to_string_lossy()]);
|
||||
let ctx = app.ui_ctx.clone().unwrap();
|
||||
crate::tests::picks::poll(&mut app, &ctx);
|
||||
|
||||
assert!(picked(&app).is_empty(), "the old file chooser must not populate the new open dialog");
|
||||
set(&app, PICKER_KIND, "answer", json!("deny")).unwrap();
|
||||
assert!(confirm(&mut app, PICKER_KIND));
|
||||
finish(&mut app);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_warning_comes_before_the_run_and_cancel_runs_nothing() {
|
||||
let mut app = app();
|
||||
|
||||
Reference in new issue
Block a user