mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-03 06:30:00 -06:00
Print: Print, Print Preview and Print Setup take a print preset by name
file.print, print.preview and print.setup took print settings only as settings, so an agent had to read a preset with print.presets.list and copy its settings into the call. They now take preset, the name of [Default] or of a saved print preset in any case. The preset's settings are used instead of the document's, settings applies over them, and print.setup saves them with the document as one undo step. An unknown name is a parameter error that lists the presets. The app's file.print with params passes preset on to the engine. Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
//! Print presets in the UI: the Print dialog's preset list and Save Preset…, and Edit → Print
|
||||
//! Presets (New…, Edit…, Delete, Import…, Export…) with its preset editor.
|
||||
//! Presets (New…, Edit…, Delete, Import…, Export…) with its preset editor, and `file.print` with
|
||||
//! a preset's name.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_engine::Session;
|
||||
@@ -7,7 +11,7 @@ use vectorcraft_engine::cmd::printpresets::DEFAULT_PRESET;
|
||||
|
||||
use super::print::{KIND, PRESET_KIND, SECTIONS};
|
||||
use super::print_presets::{self, Action};
|
||||
use crate::{VectorcraftApp, theme};
|
||||
use crate::{Services, VectorcraftApp, theme};
|
||||
|
||||
fn frame(app: &mut VectorcraftApp) {
|
||||
let ctx = egui::Context::default();
|
||||
@@ -77,6 +81,22 @@ fn the_print_dialog_saves_and_loads_presets() {
|
||||
assert_eq!(app.session.prefs.print_presets.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_print_with_params_prints_a_preset_by_name() {
|
||||
let written: Rc<RefCell<Vec<Vec<u8>>>> = Rc::default();
|
||||
let w = written.clone();
|
||||
let write = move |_: &str, b: &[u8]| {
|
||||
w.borrow_mut().push(b.to_vec());
|
||||
Ok(())
|
||||
};
|
||||
let mut app = VectorcraftApp::new(Session::new(), Services { write: Some(Box::new(write)), ..Default::default() });
|
||||
app.run("file.new", json!({"width": 200, "height": 100, "artboards": 4})).unwrap();
|
||||
app.run("print.presets.save", json!({"name": "Sheet", "settings": {"artboards": "range", "range": "4"}})).unwrap();
|
||||
let r = app.run("file.print", json!({"path": "/tmp/sheet.pdf", "preset": "Sheet"})).unwrap();
|
||||
assert_eq!(r["pages"], 1, "{r}");
|
||||
assert_eq!(vectorcraft_pdf::info(&written.borrow()[0], None).unwrap().pages.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_manager_adds_edits_and_deletes_presets_through_the_editor() {
|
||||
// Edit → Print Presets works without a document too.
|
||||
|
||||
@@ -812,7 +812,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"file.print",
|
||||
"Print…",
|
||||
"Cmd+P",
|
||||
"{} open the Print dialog (dialog `print`: the print.setup settings, preset (setting it loads that print preset; Save Preset… saves them as one), printer, toFile; Print keeps the settings with the document and prints, Done (discard: true) only keeps them); with params {settings? (over the document's), printer? (print.printers name; \"\" the default), toFile?, path?, format?: pdf|postscript, level?, flattenerPreset? (as engine file.print)} print without it: the print-ready PDF (engine file.print) goes to the printer → {pages, printer, printed: true, warnings}; with toFile, a path or no printing here (no print service) it is saved as a PDF at path (else a picked file, the web downloads it) → {path, pages, printed: false, warnings}; a PostScript file (format postscript, or a .ps path) is always saved that way",
|
||||
"{} open the Print dialog (dialog `print`: the print.setup settings, preset (setting it loads that print preset; Save Preset… saves them as one), printer, toFile; Print keeps the settings with the document and prints, Done (discard: true) only keeps them); with params {preset? (a print preset's name: its settings instead of the document's), settings? (over the document's or the preset's), printer? (print.printers name; \"\" the default), toFile?, path?, format?: pdf|postscript, level?, flattenerPreset? (as engine file.print)} print without it: the print-ready PDF (engine file.print) goes to the printer → {pages, printer, printed: true, warnings}; with toFile, a path or no printing here (no print service) it is saved as a PDF at path (else a picked file, the web downloads it) → {path, pages, printed: false, warnings}; a PostScript file (format postscript, or a .ps path) is always saved that way",
|
||||
),
|
||||
(
|
||||
"print.printers",
|
||||
|
||||
@@ -58,9 +58,10 @@ pub fn printers(app: &mut VectorcraftApp) -> Value {
|
||||
json!({ "printers": printers, "service": app.services.print.is_some(), "setup": setup })
|
||||
}
|
||||
|
||||
/// `file.print` with settings: print the active document with `settings` (over the document's)
|
||||
/// on `printer` (default: the system's default), or save the job as a PDF at `path` (else a picked
|
||||
/// file, the web downloads it) with `toFile`, a `path` or no print service. A PostScript file
|
||||
/// `file.print` with settings: print the active document with `settings` (over the document's, or
|
||||
/// over those of the print preset `preset` names) on `printer` (default: the system's default), or
|
||||
/// save the job as a PDF at `path` (else a picked file, the web downloads it) with `toFile`, a
|
||||
/// `path` or no print service. A PostScript file
|
||||
/// (`format: "postscript"` or a `.ps` path, at `level`, flattened with `flattenerPreset`) is saved
|
||||
/// the same way.
|
||||
pub fn run(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
@@ -68,7 +69,7 @@ pub fn run(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
let postscript = s("format").as_deref() == Some("postscript") || s("path").is_some_and(|path| fileio::extension(&path) == "ps");
|
||||
let to_file = p.get("toFile").and_then(Value::as_bool).unwrap_or(false) || s("path").is_some() || postscript;
|
||||
let mut q = json!({});
|
||||
for k in ["settings", "format", "level", "flattenerPreset"] {
|
||||
for k in ["preset", "settings", "format", "level", "flattenerPreset"] {
|
||||
if let Some(v) = p.get(k).filter(|v| !v.is_null()) {
|
||||
q[k] = v.clone();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user