diff --git a/crates/ui-egui/src/workflows/print_ui/body.rs b/crates/ui-egui/src/workflows/print_ui/body.rs index d9a0f7280f..0bdfa965f6 100644 --- a/crates/ui-egui/src/workflows/print_ui/body.rs +++ b/crates/ui-egui/src/workflows/print_ui/body.rs @@ -1,882 +1,678 @@ -use super::draft::{CUSTOM_SCALE, Handling, PrintDraft, Which}; -use super::{ - Binding, BookletSubset, Color32, Content, Orientation, PREVIEW_BOX, PREVIEW_H, PREVIEW_W, PageOrder, Pos2, Rect, SizeMode, Stroke, Subset, - Tokens, pos2, print, printer_label, spool, still_looking, theme, vec2, widgets, -}; +//! The Print dialog's rows: the app's sections for `HostedPrint`, and the conversion between the +//! draft and the dialog's field values. +//! +//! The draft is the model; the fields are what the rows edit. The shared job settings (printer, +//! copies, collate, grayscale, two-sided) live in the dialog's `State`, not in the fields. -/// The narrowest the title is still worth cutting: an ellipsis and a couple of pixels at the title's -/// font. Given less room than this, [`egui::Sides::truncate`] cannot cut any further and the label -/// is painted back over the buttons, which is the defect of #5545; the title is left out instead. -const TITLE_MIN_WIDTH: f32 = 16.0; +use craft::print::ui::PrintSection; +use craft_ui::buttons::Button; +use craft_ui::choice::Segmented; +use craft_ui::contract::{Bind, Entry, Outcome as _, Props as _, Show as _, ShowContainer as _, clicked}; +use craft_ui::layout_components::Row; +use craft_ui::patterns::form_dialogs::{DialogFields, Param, ParamKind}; +use craft_ui::runtime::env::{Label, UnitKind}; +use pdfcraft_engine::print::{self, Binding, BookletSubset, Content, Orientation, PageOrder, Subset, spool}; -/// The panel's own header: "Printer properties" on the left, Done and Reset on the right. -/// -/// The buttons take their room first, from the right (`shrink_left`), and the title gets what is -/// left of the row: [`egui::Sides::truncate`] cuts it with an ellipsis instead of letting a label wider than the row -/// spill over it. Laid out the other way round, the label is placed first at its natural width and -/// the buttons are painted back over it, which is what a translation of "Reset to printer -/// defaults" long enough to fill the row did in Italian (#5545). -/// -/// The title comes back as `None` when the row is too narrow for even an ellipsis, which a catalog -/// whose two buttons nearly fill the row reaches (Bulgarian, Ukrainian, Spanish at about 450 px). -fn driver_options_header(ui: &mut egui::Ui, show: &mut bool, can_reset: bool) -> (Option, egui::Response) { - egui::Sides::new().shrink_left().truncate().show( - ui, - |ui| { - let room = ui.available_width(); - (room >= TITLE_MIN_WIDTH).then(|| ui.add(egui::Label::new(egui::RichText::new(tl!("Printer properties")).font(theme::semibold(13.0))))) - }, - |ui| { - if ui.button(tl!("Done")).clicked() { - *show = false; +use super::draft::{CUSTOM_SCALE, Handling, IMAGE_DPI, PrintDraft, Which}; +use crate::print_paper::CustomPaper; +use crate::units::Unit; + +const WHICH: [(Which, &str); 5] = + [(Which::All, "all"), (Which::Current, "current"), (Which::Range, "range"), (Which::Area, "area"), (Which::Selected, "selected")]; +const HANDLING: [(Handling, &str); 5] = + [(Handling::Size, "size"), (Handling::Poster, "poster"), (Handling::Multiple, "multiple"), (Handling::Booklet, "booklet"), (Handling::Label, "label")]; +const SUBSET: [(Subset, &str); 3] = [(Subset::All, "all"), (Subset::Odd, "odd"), (Subset::Even, "even")]; +const ORIENTATION: [(Orientation, &str); 3] = + [(Orientation::Auto, "auto"), (Orientation::Portrait, "portrait"), (Orientation::Landscape, "landscape")]; +const ORDER: [(PageOrder, &str); 5] = [ + (PageOrder::Horizontal, "horizontal"), + (PageOrder::HorizontalReversed, "horizontal_reversed"), + (PageOrder::Vertical, "vertical"), + (PageOrder::VerticalReversed, "vertical_reversed"), + (PageOrder::CutStack, "cut_stack"), +]; +const BOOKLET: [(BookletSubset, &str); 3] = + [(BookletSubset::BothSides, "both"), (BookletSubset::FrontOnly, "front"), (BookletSubset::BackOnly, "back")]; +const BINDING: [(Binding, &str); 2] = [(Binding::Left, "left"), (Binding::Right, "right")]; +const CONTENT: [(Content, &str); 4] = [ + (Content::Document, "document"), + (Content::DocumentAndMarkups, "document_and_markups"), + (Content::DocumentAndStamps, "document_and_stamps"), + (Content::FormFieldsOnly, "form_fields_only"), +]; +/// The pages-per-sheet counts the dialog offers. +const PER_SHEET: [usize; 5] = [2, 4, 6, 9, 16]; +/// The id of the typed columns by rows grid among the pages-per-sheet choices. +const CUSTOM_GRID: &str = "custom"; + +fn id_of(table: &[(T, &'static str)], value: T) -> &'static str { + table.iter().find(|(v, _)| *v == value).map_or("", |(_, id)| id) +} + +fn value_of(table: &[(T, &'static str)], id: &str) -> Option { + table.iter().find(|(_, i)| *i == id).map(|(v, _)| *v) +} + +/// The sizing tab a `handling.` action names. +pub(super) fn handling_from_id(id: &str) -> Option { + value_of(&HANDLING, id) +} + +fn lbl(text: impl AsRef) -> Label { + Label::from(text.as_ref().to_owned()) +} + +/// A row label: the dialog's captions end in a colon, a row's label does not. +fn row_label(text: impl AsRef) -> Label { + lbl(text.as_ref().trim_end_matches(':')) +} + +/// The options of a choice row: `labelled` pairs each value with its caption, the ids come from `table`. +fn options(table: &[(T, &'static str)], labelled: Vec<(T, Label)>) -> Vec<(String, Label)> { + labelled.into_iter().map(|(v, l)| (id_of(table, v).to_owned(), l)).collect() +} + +fn choice(key: &str, label: Label, options: Vec<(String, Label)>, current: &str) -> Param { + Param::new(key, label, ParamKind::choice(options, Some(current.to_owned()))) +} + +fn radio(key: &str, label: Label, options: Vec<(String, Label)>, current: &str) -> Param { + Param::new(key, label, ParamKind::radio(options, Some(current.to_owned()))) +} + +fn check(key: &str, label: Label, on: bool) -> Param { + Param::new(key, label, ParamKind::bool(on)) +} + +fn number(key: &str, label: Label, (lo, hi): (f64, f64), value: f64, step: f64, decimals: u32, unit: UnitKind) -> Param { + Param::new(key, label, ParamKind::number(Some(lo), Some(hi), value, step, decimals)).unit(unit) +} + +fn note(key: &str, text: impl AsRef) -> Param { + Param::read_only_text(key, Label::default(), lbl(text)) +} + +fn unit_kind(unit: Unit) -> UnitKind { + match unit { + Unit::Inches => UnitKind::Inches, + Unit::Millimetres => UnitKind::Millimetres, + Unit::Centimetres => UnitKind::Centimetres, + Unit::Points => UnitKind::Points, + } +} + +/// A row of push buttons; a press files its id as the dialog's action. Each button is the id, its caption, whether it is +/// enabled and its tooltip. +fn button_row(key: &str, label: Label, buttons: Vec<(&'static str, Label, bool, Option