mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
feat(print): Printer menu lists the system's printers (file.printers) (#4748)
The Print dialog's Printer field was free text, so you had to type the
spooler's exact queue name (e.g. HP_OfficeJet_3830_series). It's now a
dropdown, like Photoshop's: "Default printer (<name>)" first (value "",
as before), then every printer craft_print::printers() reports (CUPS
queues and driverless destinations, or Win32_Printer on Windows).
- engine: new query command `file.printers` → {printers:[{name, default}],
default}, no document needed, not journaled (same shape as DesignCraft's).
- ui: the dialog asks once per opening and keeps the list in `__printers`,
which never reaches file.print's params. Labels show `_` as spaces; the
exact queue name is what's sent. A remembered printer the spooler no
longer lists stays selectable.
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
6ddb0e0b0d
commit
d3b2dd1aef
3 files changed
+78
-1
No files matched your search
@@ -450,6 +450,15 @@ pub fn printing_disabled() -> bool {
|
||||
PRINTING_DISABLED.load(std::sync::atomic::Ordering::Relaxed) || std::env::var_os("PHOTOCRAFT_NO_PRINT").is_some_and(|v| !v.is_empty() && v != "0")
|
||||
}
|
||||
|
||||
/// The printers the system spooler knows (Print's Printer menu): CUPS queues and driverless
|
||||
/// destinations on macOS and Linux, `Win32_Printer` on Windows, none in the browser.
|
||||
fn printers(_: &mut Session, _: &Value) -> Result<Value> {
|
||||
let list = craft_print::printers();
|
||||
let default = list.iter().find(|p| p.default).map(|p| p.name.clone());
|
||||
let printers: Vec<Value> = list.into_iter().map(|p| json!({"name": p.name, "default": p.default})).collect();
|
||||
Ok(json!({"printers": printers, "default": default}))
|
||||
}
|
||||
|
||||
fn print(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
do_print(s, p, "file.print")
|
||||
}
|
||||
@@ -621,6 +630,16 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
const PRINT_PARAMS: &str = r##"{"printer":name? (default printer),"copies":1..999=1,"paper":"letter|legal|tabloid|a3|a4|a5|4x6|5x7"|[w,h] pt="letter","orientation":"portrait|landscape"="portrait","center":bool=true,"top":in?,"left":in?,"scale":%=100,"scaleToFit":bool=false,"colorHandling":"printerManages|photocraftManages|noColorManagement"="printerManages","printerProfile":profile? (photocraftManages),"intent":"perceptual|relative|saturation|absolute"="relative","bpc":bool=true,"cornerCropMarks":bool,"centerCropMarks":bool,"registrationMarks":bool,"description":bool,"labels":bool,"emulsionDown":bool=false (mirror the page left to right, for sublimation and other transfer printing),"output":pdf path? (print to PDF; then "send" defaults to false),"send":bool?,"dryRun":bool=false (render the PDF, report the backend command, don't spool; write a PDF only at an explicit output path)} → {pdf:path|null (null without explicit output), imageRect, command:diagnostic array|null, sent}"##;
|
||||
vec![
|
||||
spec!("file.print", "Print…", &["File"], Some("Cmd+P"), PRINT_PARAMS, native_doc, print),
|
||||
CommandSpec {
|
||||
id: "file.printers",
|
||||
label: "Printers",
|
||||
menu: &[],
|
||||
shortcut: None,
|
||||
params: r##"{} → {printers:[{name, default}], default:name|null} (the system spooler's printers; a name is Print's "printer")"##,
|
||||
enabled: crate::commands::always,
|
||||
journal: false,
|
||||
run: printers,
|
||||
},
|
||||
spec!(
|
||||
"file.printOneCopy",
|
||||
"Print One Copy",
|
||||
|
||||
@@ -368,3 +368,15 @@ fn disabled_printing_never_spools() {
|
||||
assert_eq!(r["sent"], false);
|
||||
assert!(r["command"].is_array(), "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn printers_lists_the_spooler_destinations_without_a_document() {
|
||||
let mut s = Session::new();
|
||||
let r = s.execute("file.printers", json!({})).unwrap();
|
||||
let list = r["printers"].as_array().unwrap();
|
||||
assert!(list.iter().all(|p| p["name"].is_string() && p["default"].is_boolean()));
|
||||
match r["default"].as_str() {
|
||||
Some(d) => assert!(list.iter().any(|p| p["name"] == d && p["default"] == true)),
|
||||
None => assert!(list.iter().all(|p| p["default"] == false)),
|
||||
}
|
||||
}
|
||||
@@ -352,6 +352,27 @@ fn web_pane(app: &PhotocraftApp, ui: &mut egui::Ui, size: egui::Vec2, p: Option<
|
||||
});
|
||||
}
|
||||
|
||||
/// Print's Printer menu: the default printer (value ""), then every printer `file.printers`
|
||||
/// listed. A saved name the spooler no longer lists stays selectable rather than vanishing.
|
||||
/// CUPS queue names stand in for spaces with `_`; the labels show the spaces.
|
||||
fn printer_options(list: Option<&Value>, current: &str) -> Vec<(String, String)> {
|
||||
let printers: Vec<(&str, bool)> = list
|
||||
.and_then(Value::as_array)
|
||||
.map(|a| a.iter().filter_map(|p| Some((p.get("name")?.as_str()?, p.get("default").and_then(Value::as_bool).unwrap_or(false)))).collect())
|
||||
.unwrap_or_default();
|
||||
let pretty = |n: &str| n.replace('_', " ");
|
||||
let default = match printers.iter().find(|p| p.1) {
|
||||
Some((n, _)) => format!("{} ({})", tl!("Default printer"), pretty(n)),
|
||||
None => tl!("Default printer").to_string(),
|
||||
};
|
||||
let mut opts = vec![(String::new(), default)];
|
||||
opts.extend(printers.iter().map(|(n, _)| (n.to_string(), pretty(n))));
|
||||
if !current.is_empty() && !printers.iter().any(|p| p.0 == current) {
|
||||
opts.push((current.to_string(), pretty(current)));
|
||||
}
|
||||
opts
|
||||
}
|
||||
|
||||
fn dropdown_str(ui: &mut egui::Ui, id: &str, f: &mut Map<String, Value>, key: &str, opts: &[(&str, &str)], w: f32) {
|
||||
let mut v = s(f, key, opts.first().map_or("", |o| o.0));
|
||||
let o: Vec<(String, &str)> = opts.iter().map(|(k, l)| (k.to_string(), *l)).collect();
|
||||
@@ -689,6 +710,9 @@ pub fn open_print(app: &mut PhotocraftApp) -> u64 {
|
||||
let mut f = Map::new();
|
||||
f.insert("__print".into(), json!(true));
|
||||
f.insert("__label".into(), json!("PhotoCraft Print Settings"));
|
||||
// Asked once per opening: the Printer menu lists what the spooler knows now.
|
||||
let printers = app.session.execute("file.printers", json!({})).unwrap_or_default();
|
||||
f.insert("__printers".into(), printers["printers"].clone());
|
||||
if let Some(Value::Object(m)) = app.session.file_menu.last_print.clone() {
|
||||
f.extend(m);
|
||||
}
|
||||
@@ -880,7 +904,9 @@ fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<String, Va
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(egui::RichText::new(tl!("Printer")).color(t.text_dim));
|
||||
let mut pr = s(f, "printer", "");
|
||||
if ui.add(egui::TextEdit::singleline(&mut pr).hint_text(tl!("Default printer")).desired_width(180.0)).changed() {
|
||||
let opts = printer_options(f.get("__printers"), &pr);
|
||||
let o: Vec<(String, &str)> = opts.iter().map(|(k, l)| (k.clone(), l.as_str())).collect();
|
||||
if crate::widgets::dropdown(ui, "print-printer", &mut pr, &o, 240.0) {
|
||||
f.insert("printer".into(), json!(pr));
|
||||
}
|
||||
});
|
||||
@@ -1283,6 +1309,26 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn printer_menu_lists_the_spooler_printers_after_the_default() {
|
||||
let list = json!([{"name": "HP_OfficeJet_3830_series", "default": true}, {"name": "DNP_DP_DS620", "default": false}]);
|
||||
let o = printer_options(Some(&list), "");
|
||||
let keys: Vec<&str> = o.iter().map(|p| p.0.as_str()).collect();
|
||||
assert_eq!(keys, ["", "HP_OfficeJet_3830_series", "DNP_DP_DS620"]);
|
||||
assert!(o[0].1.ends_with("(HP OfficeJet 3830 series)"), "{}", o[0].1);
|
||||
assert_eq!(o[2].1, "DNP DP DS620");
|
||||
// A remembered printer that's gone stays selectable; no list still offers the default.
|
||||
let o = printer_options(Some(&list), "Old_Queue");
|
||||
assert_eq!(o.last().unwrap().0, "Old_Queue");
|
||||
assert_eq!(printer_options(None, "").len(), 1);
|
||||
// The dialog asks the engine, and the list never reaches file.print's params.
|
||||
let (mut app, ctx) = app();
|
||||
let id = crate::menus::invoke(&mut app, &ctx, "file.print", json!({})).unwrap()["dialog"].as_u64().unwrap();
|
||||
let f = &app.ui.dialog_mut(id).unwrap().fields;
|
||||
assert!(f.get("__printers").is_some_and(Value::is_array));
|
||||
assert!(params(f).get("__printers").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_dialog_does_not_inherit_scripted_send_override() {
|
||||
for send in [false, true] {
|
||||
|
||||
Reference in new issue
Block a user