mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 04:52:54 -05:00
VectorCraft's UI moves onto the shared craft UI framework in storytold/craft-libs (`next`; the framework side is storytold/craft-libs#2809), as PhotoCraft's did. The app keeps its engine, document model, file formats, commands and automation; its windows, panels, bars, dialogs and canvas are now built and driven by the framework: shell, docking and panels, hosted dialogs, keyboard, focus, IME and pointer routing, and theme tokens. About 264,000 lines of VectorCraft's own egui UI code are removed and about 154,000 added. craft-libs is pinned at bf62d509f (in `next`), the egui fork at f5025284. Tests that drove the old UI were rewritten against the port where only their premise changed, with their assertions kept. The UI tests still failing at merge, and their follow-ups, are listed in the pull request.
149 lines
6.2 KiB
Rust
149 lines
6.2 KiB
Rust
//! Desktop printer transport. VectorCraft composes every page and copy before submission;
|
|
//! craft-print owns discovery and spooling.
|
|
use std::process::Command;
|
|
use vectorcraft_ui_egui::print::{PrintJob, PrintService, Printer};
|
|
|
|
mod direct_pdf;
|
|
|
|
pub struct SystemPrint;
|
|
impl PrintService for SystemPrint {
|
|
fn printers(&mut self) -> Vec<Printer> {
|
|
system::printers()
|
|
}
|
|
fn print(&mut self, job: &PrintJob) -> Result<String, String> {
|
|
let note = system::print(job)?;
|
|
Ok(sent(job.title, job.printer, ¬e))
|
|
}
|
|
fn shared_print(&self) -> Option<vectorcraft_ui_egui::print::SharedPrintFn> {
|
|
Some(std::sync::Arc::new(|job| SystemPrint.print(job)))
|
|
}
|
|
fn turns_landscape(&self) -> bool {
|
|
// macOS's `lp` doesn't turn a landscape page to the paper: it cuts it off on upright
|
|
// paper. craft-print turns each sheet to its own orientation on Windows.
|
|
!cfg!(target_os = "macos")
|
|
}
|
|
fn has_setup(&self) -> bool {
|
|
true
|
|
}
|
|
fn setup(&mut self, printer: Option<&str>) -> Result<(), String> {
|
|
system::setup(printer)
|
|
}
|
|
}
|
|
|
|
/// The status after the job `title` went to `printer` (the default one without a name), with the
|
|
/// spooler's `note` when it gave one. Each message has text between its values, so a catalog can
|
|
/// match it and translate it. The note follows as its own sentence: a printer name can end in
|
|
/// parentheses ("HP LaserJet (Copy 1)"), and a note in parentheses would match that name.
|
|
fn sent(title: &str, printer: Option<&str>, note: &str) -> String {
|
|
let to = printer.unwrap_or("the default printer");
|
|
if note.is_empty() { format!("Sent “{title}” to {to}") } else { format!("Sent “{title}” to {to}. The spooler replied: {note}") }
|
|
}
|
|
|
|
mod system {
|
|
use super::{Command, PrintJob, Printer};
|
|
pub fn printers() -> Vec<Printer> {
|
|
craft::print::printers().into_iter().map(Printer::from).collect()
|
|
}
|
|
pub fn print(job: &PrintJob) -> Result<String, String> {
|
|
if job.direct_pdf {
|
|
return super::direct_pdf::print(job);
|
|
}
|
|
// Copies, layout, marks and colour management are already in the PDF. Preserve its
|
|
// physical sheet size and the queue's duplex default without printing copies twice.
|
|
let settings = craft::print::Job { printer: job.printer.map(str::to_string), title: job.title.into(), ..Default::default() };
|
|
craft::print::submit(job.pdf, &settings).map_err(|e| e.to_string())
|
|
}
|
|
#[cfg(windows)]
|
|
pub(crate) fn setup_command(printer: Option<&str>, default_printer: Option<&str>) -> Command {
|
|
let target = printer.map(str::trim).filter(|p| !p.is_empty()).or_else(|| default_printer.map(str::trim).filter(|p| !p.is_empty()));
|
|
if let Some(name) = target {
|
|
let mut cmd = Command::new("rundll32.exe");
|
|
cmd.args(["printui.dll,PrintUIEntry", "/e", "/n", name]);
|
|
cmd
|
|
} else {
|
|
let mut cmd = Command::new("explorer");
|
|
cmd.arg("ms-settings:printers");
|
|
cmd
|
|
}
|
|
}
|
|
pub fn setup(printer: Option<&str>) -> Result<(), String> {
|
|
#[cfg(windows)]
|
|
{
|
|
let list = printers();
|
|
let default = list.iter().find(|p| p.default).or_else(|| list.first()).map(|p| p.name.as_str());
|
|
// Only an installed printer's own name reaches the command line: `printer` can come
|
|
// from a command or MCP, and PrintUIEntry reads options from its arguments.
|
|
let printer = printer.map(str::trim).filter(|p| list.iter().any(|q| q.name == *p));
|
|
let mut cmd = setup_command(printer, default);
|
|
match cmd.spawn() {
|
|
Ok(_) => Ok(()),
|
|
Err(_) => Command::new("explorer").arg("ms-settings:printers").spawn().map(|_| ()).map_err(|e| e.to_string()),
|
|
}
|
|
}
|
|
#[cfg(target_os = "macos")]
|
|
{
|
|
let _ = printer;
|
|
Command::new("open")
|
|
.arg("x-apple.systempreferences:com.apple.Print-Scan-Settings.extension")
|
|
.spawn()
|
|
.map(|_| ())
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
#[cfg(not(any(windows, target_os = "macos")))]
|
|
{
|
|
if Command::new("system-config-printer").spawn().is_ok() {
|
|
return Ok(());
|
|
}
|
|
let url = printer.map_or_else(|| "http://localhost:631/printers/".to_string(), |p| format!("http://localhost:631/printers/{p}"));
|
|
Command::new("xdg-open").arg(url).spawn().map(|_| ()).map_err(|e| e.to_string())
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(all(test, windows))]
|
|
mod tests {
|
|
use super::system::setup_command;
|
|
use std::ffi::OsStr;
|
|
|
|
#[test]
|
|
fn setup_command_uses_printui_for_specified_printer() {
|
|
let cmd = setup_command(Some("Office Laser"), None);
|
|
assert_eq!(cmd.get_program(), "rundll32.exe");
|
|
let args: Vec<&OsStr> = cmd.get_args().collect();
|
|
assert_eq!(args, ["printui.dll,PrintUIEntry", "/e", "/n", "Office Laser"]);
|
|
}
|
|
|
|
#[test]
|
|
fn setup_command_trims_whitespace_and_skips_empty() {
|
|
let cmd = setup_command(Some(" "), Some("Default Proofer"));
|
|
assert_eq!(cmd.get_program(), "rundll32.exe");
|
|
let args: Vec<&OsStr> = cmd.get_args().collect();
|
|
assert_eq!(args, ["printui.dll,PrintUIEntry", "/e", "/n", "Default Proofer"]);
|
|
}
|
|
|
|
#[test]
|
|
fn setup_command_falls_back_to_settings_when_no_printer() {
|
|
let cmd = setup_command(None, None);
|
|
assert_eq!(cmd.get_program(), "explorer");
|
|
let args: Vec<&OsStr> = cmd.get_args().collect();
|
|
assert_eq!(args, ["ms-settings:printers"]);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod turn_tests {
|
|
use super::sent;
|
|
|
|
#[test]
|
|
fn a_printer_name_ending_in_parentheses_stays_whole_in_a_translation() {
|
|
use vectorcraft_ui_egui::i18n::{Lang, LangExt, message};
|
|
let Some(de) = Lang::from_code("de") else { panic!("no German catalog") };
|
|
let printer = Some("HP LaserJet (Copy 1)");
|
|
assert_eq!(message(de, &sent("Poster", printer, "")), "“Poster” an HP LaserJet (Copy 1) gesendet");
|
|
assert_eq!(
|
|
message(de, &sent("Poster", printer, "job 12")),
|
|
"“Poster” an HP LaserJet (Copy 1) gesendet. Antwort der Druckwarteschlange: job 12"
|
|
);
|
|
}
|
|
}
|