mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Print: keep the saved printer while the hosted dialog's printer list is pending
The Print dialog now picks its printer through the shared print panel, which replaces the chosen printer whenever a list it receives does not name it. While another print holds the spooler, the dialog sent its cached (or empty) list, so a saved printer that list did not name was replaced and its driver settings went with the next print. While the spooler is busy, a cached list without the chosen printer is no longer sent: the chosen printer and its driver settings stay, and Print stays unavailable until a fresh list arrives. A fresh list without the chosen printer replaces it, and the replaced printer's driver settings are dropped. Driver settings remembered without a printer (the system default) stay. The saved-settings tests now read the chosen printer from the dialog's settings and run on every platform.
This commit is contained in:
1 parent
df4872078f
commit
7d25f232e8
2 files changed
+103
-20
No files matched your search
@@ -1,5 +1,5 @@
|
||||
use craft::behavior::EditEvent;
|
||||
use craft::ui::dialog::{DialogId, DialogResult};
|
||||
use craft::ui::dialog::{DialogId, DialogResult, StackDialog as _};
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, NumberUnit, Param, ParamKind};
|
||||
use craft::ui::runtime::env::UnitKind;
|
||||
use craft::ui::runtime::requests::PathKind;
|
||||
@@ -288,16 +288,26 @@ fn take_printer_lists(app: &mut PhotocraftApp) -> Vec<Printer> {
|
||||
/// Answers the dialog's request for a printer list. The list goes through a channel to
|
||||
/// `HostedPrint::listen`, so `State::poll`'s choice rule picks the printer. While another print holds the spooler the
|
||||
/// list is the cached one (or empty), and the dialog asks again on its next pass, after a short wait, so the real
|
||||
/// list arrives once the spooler is free.
|
||||
/// list arrives once the spooler is free. A cached list without the chosen printer is not sent, so the chosen
|
||||
/// printer and its driver settings stay, and Print stays unavailable, until a fresh list arrives. A fresh list
|
||||
/// without the chosen printer replaces it (an empty list leaves no printer chosen), and the driver settings of the
|
||||
/// replaced printer are dropped. With no printer chosen (the system's default), the driver settings stay.
|
||||
fn refresh_printers(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
let (list, listed) = match photocraft_engine::print_cmds::try_with_spooler(|| take_printer_lists(app)) {
|
||||
Some(list) => (list, true),
|
||||
None => (take_printer_lists(app), false),
|
||||
};
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let _ = tx.send(list);
|
||||
if let Some(dialog) = app.framework.dialogs.get_mut::<HostedPrint>() {
|
||||
dialog.listen(rx);
|
||||
let chosen = dialog.json_fields().and_then(|f| f.get("printer").and_then(Value::as_str).map(str::to_owned)).unwrap_or_default();
|
||||
let missing = !chosen.is_empty() && !list.iter().any(|p| p.name == chosen);
|
||||
let replaces = listed && missing;
|
||||
if listed || !missing {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let _ = tx.send(list);
|
||||
if dialog.listen(rx) && replaces {
|
||||
dialog.fields_mut().remove("printerSettings");
|
||||
}
|
||||
}
|
||||
if !listed {
|
||||
dialog.request_refresh();
|
||||
ctx.request_repaint_after(std::time::Duration::from_millis(100));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use craft::print::ui::HostedPrint;
|
||||
use craft::ui::dialog::DialogId;
|
||||
use craft::ui::dialog::{DialogId, StackDialog as _};
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedSaveForWeb, Resample, SAVE_PATH_KEY, WebFormat, WebSlices};
|
||||
|
||||
use super::*;
|
||||
@@ -57,20 +57,37 @@ fn print_fields(app: &mut PhotocraftApp, id: DialogId) -> &mut DialogFields {
|
||||
app.framework.dialogs.get_mut_by_id::<HostedPrint>(id).expect("Print is open").fields_mut()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
/// A default printer named `name`, as a printer source lists it.
|
||||
fn listed_printer(name: &str) -> craft::print::Printer {
|
||||
craft::print::Printer { name: name.into(), default: true, two_sided: None, description: String::new() }
|
||||
}
|
||||
|
||||
/// The printer the open Print dialog has chosen.
|
||||
fn chosen_printer(app: &PhotocraftApp, id: DialogId) -> Value {
|
||||
app.framework.dialogs.get_by_id::<HostedPrint>(id).and_then(|dialog| dialog.json_fields()).map_or(Value::Null, |f| f["printer"].clone())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_dialog_keeps_saved_settings_when_the_cached_printer_list_is_empty() {
|
||||
check_saved_settings_with_cached_printers(&[]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn print_dialog_keeps_saved_settings_when_the_cached_printer_list_is_stale() {
|
||||
check_saved_settings_with_cached_printers(&["Old printer"]);
|
||||
}
|
||||
|
||||
/// Opens Print while another thread holds the spooler. The printer list kept from before (`cached`) does not name
|
||||
/// the remembered printer. The dialog keeps the remembered printer and its driver settings while the list is
|
||||
/// pending, and still keeps them once the spooler is free and the fresh list names the printer.
|
||||
fn check_saved_settings_with_cached_printers(cached: &'static [&'static str]) {
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
let mut app = app();
|
||||
let ctx = app.ctx().clone();
|
||||
let available = Arc::new(AtomicBool::new(false));
|
||||
let listed = Arc::clone(&available);
|
||||
app.session.printer_source = Some(Arc::new(move || {
|
||||
if listed.load(Ordering::SeqCst) {
|
||||
vec![craft::print::Printer { name: "Saved printer".into(), default: true, two_sided: None, description: String::new() }]
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
if listed.load(Ordering::SeqCst) { vec![listed_printer("Saved printer")] } else { cached.iter().map(|name| listed_printer(name)).collect() }
|
||||
}));
|
||||
app.session.prefs.edit(|p| {
|
||||
p.dialogs.insert(photocraft_engine::print_cmds::REMEMBERED.into(), json!({"printer": "Saved printer", "printerSettings": "0011"}));
|
||||
@@ -81,24 +98,80 @@ fn print_dialog_keeps_saved_settings_when_the_cached_printer_list_is_empty() {
|
||||
let (release, released_rx) = std::sync::mpsc::channel::<()>();
|
||||
let holder = std::thread::spawn(move || {
|
||||
photocraft_engine::print_cmds::with_spooler(|| {
|
||||
assert!(photocraft_engine::print_cmds::rgb_printers(&lister).unwrap().is_empty());
|
||||
// Both lists the dialog reads while the spooler is busy are kept from this listing.
|
||||
let _ = photocraft_engine::print_cmds::rgb_printers(&lister);
|
||||
let kept = photocraft_engine::print_cmds::printers_without_waiting(&mut lister);
|
||||
available.store(true, Ordering::SeqCst);
|
||||
held_tx.send(()).unwrap();
|
||||
let _ = released_rx.recv_timeout(std::time::Duration::from_secs(5));
|
||||
held_tx.send(kept["printers"].as_array().map_or(0, Vec::len)).unwrap();
|
||||
let _ = released_rx.recv();
|
||||
});
|
||||
});
|
||||
held.recv().unwrap();
|
||||
let kept = held.recv().unwrap();
|
||||
let id = open_print(&mut app);
|
||||
let fields = print_fields(&mut app, id);
|
||||
assert_eq!((field(fields, "printer"), field(fields, "printerSettings")), (json!("Saved printer"), json!("0011")));
|
||||
pass(&mut app);
|
||||
take(&mut app, &ctx);
|
||||
pass(&mut app);
|
||||
let pending = (chosen_printer(&app, id), field(print_fields(&mut app, id), "printerSettings"), print_ready(&mut app, id));
|
||||
let _ = release.send(());
|
||||
holder.join().unwrap();
|
||||
assert_eq!(kept, cached.len(), "the printers kept from before");
|
||||
assert_eq!(pending, (json!("Saved printer"), json!("0011"), false), "while the list is pending, cached {cached:?}");
|
||||
photocraft_engine::print_cmds::with_spooler(|| {
|
||||
pass(&mut app);
|
||||
take(&mut app, &ctx);
|
||||
let fields = print_fields(&mut app, id);
|
||||
assert_eq!((field(fields, "printer"), field(fields, "printerSettings")), (json!("Saved printer"), json!("0011")));
|
||||
pass(&mut app);
|
||||
});
|
||||
let fresh = (chosen_printer(&app, id), field(print_fields(&mut app, id), "printerSettings"), print_ready(&mut app, id));
|
||||
assert_eq!(fresh, (json!("Saved printer"), json!("0011"), true), "after the fresh list, cached {cached:?}");
|
||||
}
|
||||
|
||||
/// A fresh, empty list leaves no printer chosen and Print unavailable, and drops the remembered printer's driver
|
||||
/// settings, so a printer listed later does not get them.
|
||||
#[test]
|
||||
fn an_empty_fresh_printer_list_drops_the_saved_driver_settings() {
|
||||
let mut app = app();
|
||||
let ctx = app.ctx().clone();
|
||||
app.session.printer_source = Some(Arc::new(Vec::new));
|
||||
app.session.prefs.edit(|p| {
|
||||
p.dialogs.insert(photocraft_engine::print_cmds::REMEMBERED.into(), json!({"printer": "Saved printer", "printerSettings": "0011"}));
|
||||
});
|
||||
let id = open_print(&mut app);
|
||||
photocraft_engine::print_cmds::with_spooler(|| {
|
||||
pass(&mut app);
|
||||
take(&mut app, &ctx);
|
||||
pass(&mut app);
|
||||
});
|
||||
let shown = (chosen_printer(&app, id), field(print_fields(&mut app, id), "printerSettings"), print_ready(&mut app, id));
|
||||
assert_eq!(shown, (json!(""), Value::Null, false));
|
||||
}
|
||||
|
||||
/// Whether the open Print dialog's Print would start a job.
|
||||
fn print_ready(app: &mut PhotocraftApp, id: DialogId) -> bool {
|
||||
let ctx = app.ctx().clone();
|
||||
app.framework.dialogs.get_mut_by_id::<HostedPrint>(id).expect("Print is open").confirm(&ctx).is_some()
|
||||
}
|
||||
|
||||
/// A fresh list without the remembered printer replaces it with the default printer, and the driver settings of
|
||||
/// the replaced printer are dropped. Settings remembered without a printer (the default one) stay.
|
||||
#[test]
|
||||
fn a_fresh_printer_list_without_the_saved_printer_drops_its_driver_settings() {
|
||||
for (remembered, settings) in
|
||||
[(json!({"printer": "Gone printer", "printerSettings": "0011"}), Value::Null), (json!({"printerSettings": "0011"}), json!("0011"))]
|
||||
{
|
||||
let mut app = app();
|
||||
let ctx = app.ctx().clone();
|
||||
app.session.prefs.edit(|p| {
|
||||
p.dialogs.insert(photocraft_engine::print_cmds::REMEMBERED.into(), remembered.clone());
|
||||
});
|
||||
let id = open_print(&mut app);
|
||||
photocraft_engine::print_cmds::with_spooler(|| {
|
||||
pass(&mut app);
|
||||
take(&mut app, &ctx);
|
||||
pass(&mut app);
|
||||
});
|
||||
assert_eq!(chosen_printer(&app, id), json!("Test printer"), "{remembered}");
|
||||
assert_eq!(field(print_fields(&mut app, id), "printerSettings"), settings, "{remembered}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user