HostedPrint: the app can mark its settings invalid, which disables Print

This commit is contained in:
Dylan M. Taylor
2026-11-22 05:29:17 -05:00
parent 3d31bbfbae
commit a303e0440c
2 changed files with 26 additions and 2 deletions
+12 -2
View File
@@ -958,6 +958,9 @@ pub struct HostedPrint {
hold: bool,
/// The Print the app holds back until it answers, and its refusal.
held: HeldAnswer<PrintAnswer>,
/// The app's say on whether the settings can print (a page range that names no page cannot): Print is disabled while
/// it is false, on top of the state's own readiness.
settings_valid: bool,
}
/// Adds the app's own keys to the automation listing of the sections' values.
@@ -985,9 +988,16 @@ impl HostedPrint {
printer_chosen: None,
hold: false,
held: HeldAnswer::new(),
settings_valid: true,
}
}
/// Tells the dialog whether the settings the app owns are valid (they are until told otherwise). While they are not,
/// Print is disabled and Enter prints nothing.
pub fn set_settings_valid(&mut self, valid: bool) {
self.settings_valid = valid;
}
/// Lists keys the sections' values do not show as they are: `form` gets the values and the listing, and may replace or
/// add entries (a custom paper as `[width, height]`). It runs after the `Settings` keys are added.
#[must_use]
@@ -1173,7 +1183,7 @@ impl StackDialog for HostedPrint {
let answers = if craft_ui::runtime::measure::active(ui.ctx()) { Vec::new() } else { self.requests.take_answers() };
let finish_held = self.held.is_accepted();
// Print waits for the app's answer: a second press cannot file over the held one.
let ready = self.state.ready() && !self.held.is_waiting();
let ready = self.state.ready() && self.settings_valid && !self.held.is_waiting();
let (busy, discovering) = (self.state.busy(), self.state.discovering());
let refusal = self.held.refusal().map(|(reason, refused)| (reason.clone(), refused.settings.clone(), refused.fields.clone()));
let mut dialog = PrintDialog::new(Bind::bound(&mut self.state.settings), &self.state.printers, Bind::bound(&mut self.fields), sheet)
@@ -1229,7 +1239,7 @@ impl StackDialog for HostedPrint {
if self.held.is_accepted() {
return self.held.finish();
}
if self.held.is_waiting() || !self.state.ready() {
if self.held.is_waiting() || !self.settings_valid || !self.state.ready() {
return None;
}
let answer = self.answer(PrintFinish::Print);
+14
View File
@@ -244,6 +244,20 @@ fn a_refused_print_keeps_a_held_dialog_open_until_a_value_changes() {
assert!(answered.settings.grayscale, "the dialog closes with the held answer");
}
/// The app can mark the settings it owns invalid (an empty page range): Print is disabled until they are valid again.
#[trial]
fn print_is_disabled_while_the_app_calls_the_settings_invalid() {
let twice = Rc::new(Cell::new(false));
let (mut fixture, host) = held_print(&twice);
assert!(!print_disabled(&fixture), "the settings are valid until the app says otherwise");
on_print(&host, |dialog| dialog.set_settings_valid(false));
fixture.idle(4);
assert!(print_disabled(&fixture), "Print is disabled on invalid settings");
on_print(&host, |dialog| dialog.set_settings_valid(true));
fixture.idle(4);
assert!(!print_disabled(&fixture), "valid again, Print is enabled");
}
/// Taking back a held Print, as the app does when its Save as PDF picker is cancelled, enables Print again with the
/// values kept and no refusal, and the next Print files again.
#[trial]