mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 16:47:49 +00:00
print: in the sections layout an app's row with a printer setting's key (copies, duplex, grayscale) takes the value automation sets and is the one the listing reports, as the printer panel shows only the printer there
This commit is contained in:
@@ -557,6 +557,17 @@ fn name_row(ui: &mut Ui, words: &PrintDialogWords, check: Option<&NameRefuses<'_
|
||||
usable_name(&shown, check.and_then(|check| check(&shown)))
|
||||
}
|
||||
|
||||
/// The rows that show `Settings` key `key` (`copies`, `duplex`, `grayscale`) in place of the printer panel: in the
|
||||
/// sections layout the panel shows only the printer, and an app's section or header row with the key is the one the user
|
||||
/// sees, so automation sets and lists that row.
|
||||
fn row_of_setting<'p>(layout: PrintLayout, sections: &'p [PrintSection], header: &'p [Param], key: &str) -> Option<&'p [Param]> {
|
||||
if layout != PrintLayout::Sections || !matches!(key, "copies" | "duplex" | "grayscale") {
|
||||
return None;
|
||||
}
|
||||
let mut rows = sections.iter().map(|section| section.params.as_slice()).chain([header]);
|
||||
rows.find(|params| params.iter().any(|param| param.key == key))
|
||||
}
|
||||
|
||||
/// `params` as rows over `values`; the colour, path and eyedropper requests they file go to `requests`.
|
||||
fn param_rows(ui: &mut Ui, requests: &mut HostRequests, title: &Label, params: &[Param], values: &mut DialogFields) {
|
||||
let mut rows = ParamRows::new(title.clone(), params, Bind::bound(values)).show(ui);
|
||||
@@ -1253,7 +1264,8 @@ impl StackDialog for HostedPrint {
|
||||
|
||||
/// The `Settings` keys (`printer`, `copies`, `duplex`, `grayscale`), each section parameter's value as
|
||||
/// [`DialogFields`] serializes it, and the dialog's own: `section` (the shown section), `sheet`, `preset` (the held
|
||||
/// preset) and, in the preset editor, `name`.
|
||||
/// preset) and, in the preset editor, `name`. A `Settings` key an app's row shows in the sections layout lists the
|
||||
/// row's value.
|
||||
fn json_fields(&self) -> Option<serde_json::Value> {
|
||||
let mut map = match serde_json::to_value(&self.fields).ok()? {
|
||||
serde_json::Value::Object(map) => map,
|
||||
@@ -1261,9 +1273,11 @@ impl StackDialog for HostedPrint {
|
||||
};
|
||||
let settings = &self.state.settings;
|
||||
map.insert("printer".to_owned(), settings.printer.clone().into());
|
||||
map.insert("copies".to_owned(), settings.copies.into());
|
||||
map.insert("duplex".to_owned(), duplex_json(settings.duplex));
|
||||
map.insert("grayscale".to_owned(), settings.grayscale.into());
|
||||
for (key, value) in [("copies", settings.copies.into()), ("duplex", duplex_json(settings.duplex)), ("grayscale", settings.grayscale.into())] {
|
||||
if row_of_setting(self.layout, &self.sections, &self.header, key).is_none() {
|
||||
map.insert(key.to_owned(), value);
|
||||
}
|
||||
}
|
||||
map.insert("section".to_owned(), self.shown_section().map_or(serde_json::Value::Null, Into::into));
|
||||
map.insert("sheet".to_owned(), self.sheet_index.into());
|
||||
let held = self.presets.as_ref().and_then(|presets| presets.held.clone());
|
||||
@@ -1278,8 +1292,12 @@ impl StackDialog for HostedPrint {
|
||||
}
|
||||
|
||||
/// Sets a `Settings` key, the dialog's own (`section`, `sheet`, `name`, `preset`, and `choice` = `"done"`), or a
|
||||
/// section parameter through the shared parameter-field rule.
|
||||
/// section parameter through the shared parameter-field rule. A `Settings` key an app's row shows in the sections
|
||||
/// layout sets the row.
|
||||
fn set_json_field(&mut self, _: &egui::Context, field: &str, value: serde_json::Value) -> Result<(), String> {
|
||||
if let Some(params) = row_of_setting(self.layout, &self.sections, &self.header, field) {
|
||||
return set_param_field(params, &mut self.fields, field, value);
|
||||
}
|
||||
match field {
|
||||
"printer" => {
|
||||
let name = value.as_str().filter(|name| self.state.printers.iter().any(|printer| printer.name == *name));
|
||||
|
||||
@@ -225,3 +225,19 @@ fn the_sections_layout_shows_the_picked_section() {
|
||||
assert!(fixture.by_label("Mark weight").is_some(), "the picked section shows");
|
||||
assert!(fixture.by_label("Copies").is_none(), "in place of the first");
|
||||
}
|
||||
|
||||
/// In the sections layout the printer panel shows only the printer, so the app's own Copies row is the one automation
|
||||
/// sets and lists; the dialog's hidden copies setting keeps its value.
|
||||
#[trial]
|
||||
fn a_section_row_takes_a_printer_setting_set_by_automation_in_the_sections_layout() {
|
||||
use craft_print::ui::{PrintLayout, PrintSection};
|
||||
use craft_ui::patterns::form_dialogs::{Param, ParamKind};
|
||||
let ctx = egui::Context::default();
|
||||
let general = vec![Param::new("copies", "Copies", ParamKind::int(Some(1), Some(99), 1))];
|
||||
let mut dialog =
|
||||
HostedPrint::new(State::default(), DialogFields::default(), vec![PrintSection::new("General", general)]).layout(PrintLayout::Sections);
|
||||
dialog.set_json_field(&ctx, "copies", serde_json::json!(4)).expect("the row takes the copies");
|
||||
assert_eq!(dialog.fields().number("copies"), Some(4.0), "the app's row holds the value");
|
||||
let listed = dialog.json_fields().expect("a listing");
|
||||
assert_eq!(listed["copies"].as_f64(), Some(4.0), "the listing reports the row: {listed}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user