mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Keep format-specific save and export options in their source drawing
This commit is contained in:
1 parent
fd13713f45
commit
c3576a7143
2 files changed
+52
-7
No files matched your search
@@ -64,6 +64,8 @@ pub(crate) enum Export {
|
||||
pub(crate) struct Open {
|
||||
/// The hosted dialog's id.
|
||||
pub id: DialogId,
|
||||
/// The drawing whose options and output this dialog controls.
|
||||
document: Option<u64>,
|
||||
/// Which dialog it is.
|
||||
pub kind: Export,
|
||||
/// Whether Export Selection opened it: OK then writes the selection alone.
|
||||
@@ -88,6 +90,15 @@ impl ExportDialogs {
|
||||
}
|
||||
}
|
||||
|
||||
impl Open {
|
||||
fn check_source(&self, app: &VectorcraftApp) -> Result<(), String> {
|
||||
if !matches!(self.kind, Export::Hpgl { .. }) && self.document != app.session.active().map(|doc| doc.uid) {
|
||||
return Err(tl!("Return to the original drawing to export it").into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// `ids` paired with `labels` as the options of a choice row.
|
||||
pub(super) fn options<L: From<&'static str>>(ids: &[&'static str], labels: &[&'static str]) -> Vec<(String, L)> {
|
||||
ids.iter().zip(labels).map(|(id, label)| (id.to_string(), L::from(label))).collect()
|
||||
@@ -96,13 +107,18 @@ pub(super) fn options<L: From<&'static str>>(ids: &[&'static str], labels: &[&'s
|
||||
/// Opens `dialog` as the export dialog `kind` and records it; `None` without a window. A dialog of the same key
|
||||
/// replaces the one open, and its record.
|
||||
pub(crate) fn open(app: &mut VectorcraftApp, kind: Export, selection: bool, dialog: HostedParamDialog) -> Option<DialogId> {
|
||||
let document = match &kind {
|
||||
Export::As { document } | Export::Hpgl { doc: document } => Some(*document),
|
||||
Export::EpsCustom { parent } => app.export_dialogs.find(*parent)?.document,
|
||||
_ => app.session.active().map(|doc| doc.uid),
|
||||
};
|
||||
let options = match &kind {
|
||||
Export::EpsCustom { parent } => HeldOptions { parent: Some(*parent), cancel: None },
|
||||
_ => HeldOptions::default(),
|
||||
};
|
||||
let id = host::open_held_with(app, dialog, answer, options)?;
|
||||
app.export_dialogs.0.retain(|o| o.id != id);
|
||||
app.export_dialogs.0.push(Open { id, kind, selection, after: None });
|
||||
app.export_dialogs.0.push(Open { id, document, kind, selection, after: None });
|
||||
Some(id)
|
||||
}
|
||||
|
||||
@@ -117,9 +133,10 @@ pub(super) fn set_fields(app: &VectorcraftApp, id: DialogId, fields: &[(&str, Va
|
||||
|
||||
/// OK of the dialog `result` is from: its kind's `confirm`, and the step that follows kept for [`pump`].
|
||||
fn answer(app: &mut VectorcraftApp, result: &ParamResult) -> Result<(), String> {
|
||||
let (id, kind, selection) =
|
||||
app.export_dialogs.find(result.id).map(|o| (o.id, o.kind.clone(), o.selection)).ok_or("this export dialog is no longer open")?;
|
||||
let open = Open { id, kind, selection, after: None };
|
||||
let open = app.export_dialogs.find(result.id).ok_or("this export dialog is no longer open")?;
|
||||
open.check_source(app)?;
|
||||
let id = open.id;
|
||||
let open = Open { id, document: open.document, kind: open.kind.clone(), selection: open.selection, after: None };
|
||||
let after = confirm(app, &open, &result.fields)?;
|
||||
if let Some(record) = app.export_dialogs.0.iter_mut().find(|o| o.id == id) {
|
||||
record.after = after;
|
||||
@@ -197,14 +214,20 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
let Some((footer, actions)) = taken else {
|
||||
// The dialog closed: the step after an OK runs, a cancelled one has none.
|
||||
if let Some(i) = app.export_dialogs.0.iter().position(|o| o.id == id) {
|
||||
let record = app.export_dialogs.0.remove(i);
|
||||
if let Some(after) = record.after {
|
||||
let result = after(app);
|
||||
let mut record = app.export_dialogs.0.remove(i);
|
||||
if let Some(after) = record.after.take() {
|
||||
let result = record.check_source(app).and_then(|()| after(app));
|
||||
report(app, result);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
};
|
||||
if let Err(error) = open.check_source(app) {
|
||||
if footer.is_some() || !actions.is_empty() {
|
||||
app.status(error);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Export::Svg { mode } = kind {
|
||||
if footer.as_deref() == Some(svg_options::BROWSER_ACTION)
|
||||
&& let Some(fields) = fields_of(app, id)
|
||||
|
||||
@@ -378,6 +378,28 @@ fn export_as_waits_for_the_drawing_that_opened_it() {
|
||||
assert_eq!(&written.borrow()[0].1[16..20], 60u32.to_be_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn png_options_wait_for_the_drawing_that_opened_them() {
|
||||
let (mut app, written) = app(1);
|
||||
app.run("file.export.png", json!({})).unwrap();
|
||||
answer_saves(&mut app);
|
||||
assert_eq!(kind(&app).as_deref(), Some("pngOptions"));
|
||||
let draft = fields(&app);
|
||||
app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
|
||||
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 40})).unwrap();
|
||||
let error = try_confirm(&mut app).expect_err("PNG Options must refuse the other drawing");
|
||||
assert!(error.contains("original drawing"), "{error}");
|
||||
assert_eq!(kind(&app).as_deref(), Some("pngOptions"));
|
||||
assert_eq!(fields(&app)["path"], draft["path"]);
|
||||
assert!(written.borrow().is_empty());
|
||||
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
confirm(&mut app);
|
||||
let w = written.borrow();
|
||||
assert_eq!(w.len(), 1);
|
||||
assert_eq!(&w[0].1[16..20], 60u32.to_be_bytes());
|
||||
}
|
||||
|
||||
/// Each format's Export As continues in its options dialog, writes nothing before that dialog's OK, and then
|
||||
/// writes its files: (case, artboards, Export As params, the options' key, the files, the pages of a PDF, the
|
||||
/// page size of a PDF of the art's bounds).
|
||||
|
||||
Reference in new issue
Block a user