Merge pull request #9516 from dotCooCoo/fix/print-original-document

Keep Print attached to the original drawing
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 07:27:00 +07:00
commit 2cc51bb89f
2 files changed
+75 -16

No files matched your search

+30 -16
View File
@@ -100,6 +100,7 @@ pub(crate) struct PrintDialogs {
struct Open {
id: DialogId,
mode: PrintMode,
document: Option<u64>,
/// The preset the settings were loaded from last (the Print Preset row's selection).
preset: String,
/// The saved preset the editor edits (empty: a new one).
@@ -157,7 +158,8 @@ pub fn open(app: &mut VectorcraftApp) -> Result<Value, String> {
// The saved preset these settings are, if any (else [Default], shown as [Custom] if they differ).
let preset = app.session.prefs.print_presets.iter().find(|p| p.settings == saved).map_or(DEFAULT_PRESET, |p| p.name.as_str()).to_string();
let fields = fields_of(&saved)?;
start(app, PrintMode::Print, fields, preset, Default::default())
let document = st.uid;
start(app, PrintMode::Print, Some(document), fields, preset, Default::default())
}
/// Open the preset editor on the saved preset `name`, or on a new preset starting from `preset`
@@ -173,7 +175,7 @@ pub fn open_preset(app: &mut VectorcraftApp, params: &Value) -> Result<Value, St
None => (s("preset").unwrap_or(DEFAULT_PRESET).to_string(), app.session.new_print_preset_name(), String::new()),
};
let set = app.session.print_preset_settings(&base).ok_or_else(|| format!("no print preset named `{base}`"))?;
start(app, PrintMode::PresetEditor, fields_of(&set)?, base, (editing, name))
start(app, PrintMode::PresetEditor, None, fields_of(&set)?, base, (editing, name))
}
/// Host the dialog over the settings `fields`; the preset editor edits the saved preset `editing` (empty: a new one)
@@ -181,6 +183,7 @@ pub fn open_preset(app: &mut VectorcraftApp, params: &Value) -> Result<Value, St
fn start(
app: &mut VectorcraftApp,
mode: PrintMode,
document: Option<u64>,
fields: DialogFields,
preset: String,
(editing, name): (String, String),
@@ -217,7 +220,7 @@ fn start(
hosted.set_json_field(&ctx, "name", json!(name))?;
}
let id = dialogs.with(|h| h.open(hosted));
app.print_dialog.open = Some(Open { id, mode, preset, editing, art: None, preview: None, driver });
app.print_dialog.open = Some(Open { id, mode, document, preset, editing, art: None, preview: None, driver });
Ok(Value::Null)
}
@@ -366,6 +369,30 @@ fn serve(app: &mut VectorcraftApp, ctx: &egui::Context, dialogs: &WindowDialogs,
let _ = dialogs.with(|h| h.take_by_id::<HostedPrint>(open.id));
return false;
};
for event in std::mem::take(&mut d.events) {
apply_preset_event(app, dialogs, open, &mut d.settings, event);
}
if open.mode == PrintMode::Print {
let same = serde_json::from_value::<PrintSettings>(d.settings.clone())
.ok()
.is_some_and(|s| app.session.print_preset_settings(&open.preset) == Some(s));
let held = same.then(|| open.preset.clone());
let rows: Vec<PresetRow<String>> = std::iter::once(PresetRow::item(DEFAULT_PRESET.to_string(), tl!(DEFAULT_PRESET), 0))
.chain(app.session.prefs.print_presets.iter().map(|p| PresetRow::item(p.name.clone(), p.name.clone(), 0)))
.collect();
on(dialogs, |p| p.set_presets(rows, held, vec![DEFAULT_PRESET.to_string()]));
}
if open.document.is_some_and(|document| app.session.active().is_none_or(|st| st.uid != document)) {
let message = tl!("Return to the original drawing to print it");
on(dialogs, |p| {
p.set_sheet(None);
p.set_message(Some(message.into()));
if d.ok.is_some() {
p.answer_ok(Err(message.into()));
}
});
return true;
}
// The row was drawn this frame with the controls of the printer before: the next frame shows the new ones.
if d.driver != open.driver {
open.driver = d.driver;
@@ -383,9 +410,6 @@ fn serve(app: &mut VectorcraftApp, ctx: &egui::Context, dialogs: &WindowDialogs,
{
app.status(e);
}
for event in std::mem::take(&mut d.events) {
apply_preset_event(app, dialogs, open, &mut d.settings, event);
}
let doc = app.session.active().map_or((0, 0), |s| (s.uid, s.revision));
let stamp = (d.settings.to_string(), doc);
let changed = open.preview.as_ref().is_none_or(|(s, _)| *s != stamp);
@@ -425,16 +449,6 @@ fn serve(app: &mut VectorcraftApp, ctx: &egui::Context, dialogs: &WindowDialogs,
p.set_field("__inks", FieldValue::Json(inks));
});
}
if open.mode == PrintMode::Print {
let same = serde_json::from_value::<PrintSettings>(d.settings.clone())
.ok()
.is_some_and(|s| app.session.print_preset_settings(&open.preset) == Some(s));
let held = same.then(|| open.preset.clone());
let rows: Vec<PresetRow<String>> = std::iter::once(PresetRow::item(DEFAULT_PRESET.to_string(), tl!(DEFAULT_PRESET), 0))
.chain(app.session.prefs.print_presets.iter().map(|p| PresetRow::item(p.name.clone(), p.name.clone(), 0)))
.collect();
on(dialogs, |p| p.set_presets(rows, held, vec![DEFAULT_PRESET.to_string()]));
}
if let Some(answer) = d.ok {
let result = accept(app, open, &answer, &d.name);
on(dialogs, |p| p.answer_ok(result.map_err(Into::into)));
+45
View File
@@ -206,6 +206,34 @@ fn done_keeps_the_settings_without_printing_and_bad_settings_keep_the_dialog() {
assert!(!crate::menus::enabled(&app, "print.printerSetup"), "the mock has no settings to open");
}
#[test]
fn print_does_not_send_or_update_the_other_drawing() {
let (mut app, jobs) = app();
app.run("file.new", json!({"width": 180, "height": 100, "name": "Other drawing"})).unwrap();
app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 50, "height": 30})).unwrap();
let activate = |app: &mut VectorcraftApp, index| app.run("document.activate", json!({"index": index})).unwrap();
let undo = app.session.active().unwrap().history.undo.len();
activate(&mut app, 0);
app.run("file.print", json!({})).unwrap();
pump(&mut app);
set(&mut app, "copies", json!(2));
activate(&mut app, 1);
confirm(&mut app);
assert!(jobs.borrow().is_empty());
let other = app.session.active().unwrap();
assert!(other.doc.print_setup.is_none());
assert_eq!(other.history.undo.len(), undo);
assert!(is_open(&app));
assert_eq!(fields(&app)["copies"], 2);
activate(&mut app, 0);
confirm(&mut app);
assert_eq!(jobs.borrow().len(), 1);
assert_eq!(pages(&jobs.borrow()[0].2), 6);
assert!(!is_open(&app));
}
/// A print queue that puts each page on the paper as it is, as `lp` does.
struct UprightQueue(Jobs);
@@ -482,6 +510,23 @@ fn a_print_preset_keeps_the_scale_link() {
assert_eq!(fields(&app)["scale"], json!({"width": 30, "height": 70, "linked": false}));
}
#[test]
fn print_preset_changes_survive_a_paused_preview() {
let (mut app, jobs) = app();
app.run("print.presets.save", json!({"name": "Apart", "settings": {"scaling": "custom", "scale": {"width": 30, "height": 90}}})).unwrap();
app.run("file.print", json!({})).unwrap();
pump(&mut app);
app.run("file.new", json!({"width": 90, "height": 90})).unwrap();
set(&mut app, "preset", json!("Apart"));
let ctx = ctx(&app);
super::print::pump(&mut app, &ctx);
assert_eq!(scale(&app), (30.0, 90.0, false));
assert!(jobs.borrow().is_empty());
app.run("document.activate", json!({"index": 0})).unwrap();
super::print::pump(&mut app, &ctx);
assert_eq!(scale(&app), (30.0, 90.0, false));
}
#[test]
fn a_scale_set_through_the_dialog_keeps_the_percentage_it_changes() {
let (mut app, _) = app();