mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Document print dialog: compact inline printer rows, caption sections, no repeated radio heading, untitled section
This commit is contained in:
@@ -7,6 +7,7 @@ use craft_behavior::{CursorIcon, EditEvent, EditEvents, Metrics, Mixed, PartKey,
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{
|
||||
LabelScheme,
|
||||
Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Input, MeasureCx, Name, NoValue, Outcome, PaintUi, PartsCx, Pointer as _,
|
||||
Props as _, Role, Scroll, Show as _, ShowContainer as _, WordSetCx as _, clicked,
|
||||
};
|
||||
@@ -23,7 +24,7 @@ use craft_ui::runtime::env::{Env, Label};
|
||||
use craft_ui::runtime::focus::{ActivateKeys, KeepKeys, KeyUse};
|
||||
use craft_ui::runtime::overlay::CloseReason;
|
||||
use craft_ui::runtime::requests::HostAnswer;
|
||||
use craft_ui::sections::{DialogRole, FooterButton, Section};
|
||||
use craft_ui::sections::{DialogRole, FooterButton, HeaderStrength, Section};
|
||||
use craft_ui::style::{ComponentStyle, Corners, RuleKind, StyleColor, StyleCx, SurfaceKind, TypeRole};
|
||||
use egui::load::SizedTexture;
|
||||
use egui::{Color32, Mesh, Pos2, Rect, Response, Shape, TextureId, Ui, Vec2, pos2, vec2};
|
||||
@@ -244,12 +245,14 @@ pub struct PrintSection {
|
||||
folded: Option<bool>,
|
||||
/// The title-row buttons: the id a press files, the label and whether the button is enabled.
|
||||
buttons: Vec<(String, Label, bool)>,
|
||||
/// The rows stand without the section's heading in a document dialog (`PrintLayout::Document`).
|
||||
untitled: bool,
|
||||
}
|
||||
|
||||
impl PrintSection {
|
||||
/// The section titled `title` with its `params` rows.
|
||||
pub fn new(title: impl Into<Label>, params: Vec<Param>) -> Self {
|
||||
Self { title: title.into(), params, folded: None, buttons: Vec::new() }
|
||||
Self { title: title.into(), params, folded: None, buttons: Vec::new(), untitled: false }
|
||||
}
|
||||
|
||||
/// Collapses the section when `open` is false, through `Section::initially_open`.
|
||||
@@ -259,6 +262,14 @@ impl PrintSection {
|
||||
self
|
||||
}
|
||||
|
||||
/// In a document dialog (`PrintLayout::Document`) the section's rows follow the ones above with no heading of their
|
||||
/// own, as production's Paper rows follow Two-sided.
|
||||
#[must_use]
|
||||
pub fn untitled(mut self) -> Self {
|
||||
self.untitled = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// A title-row button; a press is filed as `HostRequests::request_action(id)`.
|
||||
#[must_use]
|
||||
pub fn button(mut self, id: impl Into<String>, label: impl Into<Label>, enabled: bool) -> Self {
|
||||
@@ -267,9 +278,19 @@ impl PrintSection {
|
||||
}
|
||||
|
||||
/// Shows the section with its rows over `values`. Its rows' requests and a title button's press go into `requests`.
|
||||
fn show(&self, ui: &mut Ui, at: usize, values: &mut DialogFields, requests: &mut HostRequests) {
|
||||
fn show(&self, ui: &mut Ui, at: usize, inline: bool, values: &mut DialogFields, requests: &mut HostRequests) {
|
||||
// Declared before `section`, whose controls closure borrows it.
|
||||
let mut pressed = None;
|
||||
// A document dialog's rows: the captions at the row start, the heading a small caption over them.
|
||||
let rows = |ui: &mut Ui, values: &mut DialogFields, requests: &mut HostRequests| {
|
||||
let rows = ParamRows::new(self.title.clone(), &self.params, Bind::bound(&mut *values)).heading(self.title.clone());
|
||||
let mut rows = if inline { rows.labels(LabelScheme::Inline) } else { rows }.show(ui);
|
||||
requests.absorb(&mut rows);
|
||||
};
|
||||
if inline && self.untitled && self.buttons.is_empty() {
|
||||
rows(ui, values, requests);
|
||||
return;
|
||||
}
|
||||
let mut section = match self.folded {
|
||||
Some(open) => Section::new(self.title.clone()).initially_open(open),
|
||||
None => Section::fixed(self.title.clone()),
|
||||
@@ -286,10 +307,10 @@ impl PrintSection {
|
||||
}
|
||||
});
|
||||
}
|
||||
section.id_salt(("section", at)).show(ui, |ui, _| {
|
||||
let mut rows = ParamRows::new(self.title.clone(), &self.params, Bind::bound(&mut *values)).show(ui);
|
||||
requests.absorb(&mut rows);
|
||||
});
|
||||
if inline {
|
||||
section = section.strength(HeaderStrength::Caption);
|
||||
}
|
||||
section.id_salt(("section", at)).show(ui, |ui, _| rows(ui, values, requests));
|
||||
if let Some(id) = pressed {
|
||||
requests.request_action(id);
|
||||
}
|
||||
@@ -851,22 +872,29 @@ impl<'a> PrintDialog<'a> {
|
||||
}
|
||||
// Printer Setup and the app's sections.
|
||||
let mut setup = |ui: &mut Ui, values: &mut DialogFields, requests: &mut HostRequests| {
|
||||
Section::fixed(words.printer_setup.clone()).id_salt("printer").show(ui, |ui, _| {
|
||||
let mut printer = |ui: &mut Ui| {
|
||||
let panel = PrintPanel::new(settings.reborrowed(), printers)
|
||||
.discovering(discovering)
|
||||
.busy(busy)
|
||||
.driver(driver)
|
||||
.destinations(destinations)
|
||||
.duplex_by_layout(duplex_by_layout)
|
||||
.inline(document)
|
||||
.show(ui);
|
||||
refresh |= panel.refresh;
|
||||
settings_clicked |= panel.settings_clicked;
|
||||
if panel.printer_chosen.is_some() {
|
||||
printer_chosen = panel.printer_chosen;
|
||||
}
|
||||
});
|
||||
};
|
||||
// A document dialog's printer rows head the dialog with no heading of their own, as production's.
|
||||
if document {
|
||||
printer(ui);
|
||||
} else {
|
||||
Section::fixed(words.printer_setup.clone()).id_salt("printer").show(ui, |ui, _| printer(ui));
|
||||
}
|
||||
for (at, section) in sections.iter().enumerate() {
|
||||
section.show(ui, at, values, requests);
|
||||
section.show(ui, at, document, values, requests);
|
||||
}
|
||||
};
|
||||
// The sheet preview, the pager under it in the `Document` layout, the readout and the notes.
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use super::{Destination, Duplex, Printer, Settings, shown_names};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::choice::Checkbox;
|
||||
use craft_ui::contract::{Bind, Entry, FieldLayout, Held, NoValue, Outcome, Props as _, Show as _, ShowContainer as _, SideLabel, clicked};
|
||||
use craft_ui::contract::{Bind, Entry, FieldLayout, Held, LabelScheme, NoValue, Outcome, Props as _, Show as _, ShowContainer as _, SideLabel, clicked};
|
||||
use craft_ui::display::{Shade, Text};
|
||||
use craft_ui::form::{Field, FieldGroup};
|
||||
use craft_ui::layout_components::Row;
|
||||
@@ -29,6 +29,7 @@ craft_ui::words! {
|
||||
long_edge: "printPanel.longEdge" = "Long edge";
|
||||
short_edge: "printPanel.shortEdge" = "Short edge";
|
||||
grayscale: "printPanel.grayscale" = "Grayscale";
|
||||
print_in_grayscale: "printPanel.printInGrayscale" = "Print in grayscale";
|
||||
print_settings: "printPanel.printSettings" = "Print Settings…";
|
||||
orientation: "printPanel.orientation" = "Orientation";
|
||||
portrait: "printPanel.portrait" = "Portrait";
|
||||
@@ -124,6 +125,11 @@ pub struct PrintPanel<'a> {
|
||||
/// The layout sets two-sided printing: the Two-sided row is left out.
|
||||
#[prop]
|
||||
duplex_by_layout: bool,
|
||||
/// A document dialog's compact rows, as production's: each caption stands at the row start with its control right
|
||||
/// after it, Refresh is left out (the printers are found as the dialog opens) and Grayscale reads "Print in
|
||||
/// grayscale".
|
||||
#[prop]
|
||||
inline: bool,
|
||||
/// The driver's paper in points, as the caller last took it.
|
||||
#[prop(quantity = "paper size the printer driver reports, in points")]
|
||||
page: Option<egui::Vec2>,
|
||||
@@ -143,6 +149,7 @@ impl<'a> PrintPanel<'a> {
|
||||
driver: DriverControls::None,
|
||||
destinations: Destinations::Printers,
|
||||
duplex_by_layout: false,
|
||||
inline: false,
|
||||
page: None,
|
||||
page_error: None,
|
||||
}
|
||||
@@ -150,7 +157,7 @@ impl<'a> PrintPanel<'a> {
|
||||
|
||||
/// Shows the panel.
|
||||
pub fn show(self, ui: &mut Ui) -> PrintPanelOutput {
|
||||
let PrintPanel { settings, printers, discovering, busy, driver, destinations, duplex_by_layout, page, page_error } = self;
|
||||
let PrintPanel { settings, printers, discovering, busy, driver, destinations, duplex_by_layout, inline, page, page_error } = self;
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<PrintWords>();
|
||||
let mut queues: Vec<Entry<Destination>> = printers
|
||||
@@ -177,7 +184,9 @@ impl<'a> PrintPanel<'a> {
|
||||
let mut printer_chosen = None;
|
||||
let mut settings_clicked = false;
|
||||
let mut turned = None;
|
||||
let mut shown = FieldGroup::new(words.title.clone()).enabled(!busy).show(ui, |ui, cx| {
|
||||
let group = FieldGroup::new(words.title.clone()).enabled(!busy);
|
||||
let group = if inline { group.labels(LabelScheme::Inline) } else { group };
|
||||
let mut shown = group.show(ui, |ui, cx| {
|
||||
let before = held.read(cx)?;
|
||||
let mut now = before.clone();
|
||||
Field::new(words.printer.clone()).id_salt("printer").show(ui, |ui, _| {
|
||||
@@ -192,8 +201,10 @@ impl<'a> PrintPanel<'a> {
|
||||
}
|
||||
now.destination.clone_from(destination);
|
||||
}
|
||||
let button = Button::new(words.refresh.clone()).enabled(!discovering).id_salt("refresh").show(ui);
|
||||
refresh |= clicked(&button);
|
||||
if !inline {
|
||||
let button = Button::new(words.refresh.clone()).enabled(!discovering).id_salt("refresh").show(ui);
|
||||
refresh |= clicked(&button);
|
||||
}
|
||||
if driver == DriverControls::Properties {
|
||||
let queue = matches!(&now.destination, Destination::Printer(name) if !name.is_empty());
|
||||
let button = Button::new(words.properties.clone()).enabled(queue).id_salt("properties").show(ui);
|
||||
@@ -220,7 +231,7 @@ impl<'a> PrintPanel<'a> {
|
||||
now.collate = on;
|
||||
}
|
||||
let grayscale =
|
||||
cx.labelled(ui, "grayscale", words.grayscale.clone(), after, Checkbox::new(Bind::given(now.grayscale)).enabled(free));
|
||||
cx.labelled(ui, "grayscale", if inline { words.print_in_grayscale.clone() } else { words.grayscale.clone() }, after, Checkbox::new(Bind::given(now.grayscale)).enabled(free));
|
||||
if let Some(on) = grayscale.value().copied() {
|
||||
now.grayscale = on;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ use craft_print::ui::{
|
||||
};
|
||||
use craft_ui::contract::Bind;
|
||||
use craft_ui::dialog::{DialogHost, DialogResult, HeldOk, StackDialog as _, dialog_id};
|
||||
use craft_ui::patterns::form_dialogs::{DialogFields, FieldValue, PanePager, Param};
|
||||
use craft_ui::patterns::form_dialogs::{DialogFields, FieldValue, PanePager, Param, ParamKind};
|
||||
use craft_ui::primitives::TextureCache;
|
||||
use craft_ui::runtime::env::{Label, Scale};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
@@ -478,3 +478,42 @@ fn the_preview_tints_the_pages_of_a_grayscale_job() {
|
||||
let tint = page_tint(Settings { grayscale: true, ..Settings::default() });
|
||||
assert!(tint.r() == tint.g() && tint.g() == tint.b() && tint.r() < 250 && tint.r() > 150, "a grayscale job tints the pictures grey: {tint:?}");
|
||||
}
|
||||
|
||||
/// A document print dialog over a Pages to Print section holding a radio of the same name.
|
||||
fn document_dialog() -> Fixture {
|
||||
let mut settings = Settings::default();
|
||||
let mut fields = DialogFields::default();
|
||||
let printers = vec![Printer::new("Office")];
|
||||
let options = |names: [&'static str; 2]| names.into_iter().map(|name| (name.to_owned(), Label::from(name))).collect::<Vec<_>>();
|
||||
let sections = vec![PrintSection::new(
|
||||
"Pages to Print",
|
||||
vec![Param::new("which", "Pages to Print", ParamKind::radio(options(["All pages", "Current page"]), Some("All pages".into())))],
|
||||
)];
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let page = SheetPage::new([pos2(72.0, 72.0), pos2(300.0, 72.0), pos2(300.0, 400.0), pos2(72.0, 400.0)], "1");
|
||||
let sheet = PrintSheet::pages(LETTER, vec![page], "Letter".into());
|
||||
let _ = PrintDialog::new(Bind::bound(&mut settings), &printers, Bind::bound(&mut fields), &sheet)
|
||||
.layout(PrintLayout::Document)
|
||||
.sections(§ions)
|
||||
.ready(true)
|
||||
.show(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(1400.0, 900.0));
|
||||
fixture.settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
/// A document print dialog is compact, as production's: the printer rows head it with no "Printer Setup" heading and no
|
||||
/// Refresh button, a section's radio group does not repeat the section's heading, and its options stand on one line.
|
||||
#[trial]
|
||||
fn a_document_print_dialog_has_compact_inline_rows() {
|
||||
let fixture = document_dialog();
|
||||
let texts = fixture.texts();
|
||||
let count = |text: &str| texts.iter().filter(|run| run.text == text).count();
|
||||
assert_eq!(count("Printer Setup"), 0, "the printer rows carry no heading");
|
||||
assert_eq!(fixture.by_label("Refresh").is_none(), true, "the printers are found as the dialog opens: no Refresh");
|
||||
assert_eq!(count("Pages to Print"), 1, "the heading is shown once, not again as the radio's caption");
|
||||
let all = texts.iter().find(|run| run.text == "All pages").expect("the first option is painted");
|
||||
let current = texts.iter().find(|run| run.text == "Current page").expect("the second option is painted");
|
||||
assert!((all.rect.center().y - current.rect.center().y).abs() < 2.0, "the options stand on one line: {:?} {:?}", all.rect, current.rect);
|
||||
}
|
||||
|
||||
@@ -142,6 +142,7 @@ pub struct ParamRows<'a> {
|
||||
preview_colors: &'a [(String, Srgba)],
|
||||
sliders: SliderFieldLayout,
|
||||
labels: Option<LabelScheme>,
|
||||
heading: Option<Label>,
|
||||
}
|
||||
|
||||
impl<'a> ParamRows<'a> {
|
||||
@@ -158,9 +159,17 @@ impl<'a> ParamRows<'a> {
|
||||
preview_colors: &[],
|
||||
sliders: SliderFieldLayout::Inline,
|
||||
labels: None,
|
||||
heading: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The heading the rows stand under, when the host shows one. In a `LabelScheme::Inline` form a radio row of the
|
||||
/// same name leaves its caption out, as production's Pages to Print options follow their heading.
|
||||
pub fn heading(mut self, heading: impl Into<Label>) -> Self {
|
||||
self.heading = Some(heading.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// How the range rows arrange their slider and number field: `Inline` (the default) puts them on one line beside
|
||||
/// the row's label; `Stacked` puts the label and the number field on one line and the slider across the width
|
||||
/// under them.
|
||||
@@ -211,7 +220,7 @@ impl<'a> ParamRows<'a> {
|
||||
|
||||
/// Shows the rows across the width offered.
|
||||
pub fn show(self, ui: &mut Ui) -> ParamRowsOutput {
|
||||
let ParamRows { label, params, mut fields, mut seed, read_only, canvas, patterns, preview_colors, sliders, labels } = self;
|
||||
let ParamRows { label, params, mut fields, mut seed, read_only, canvas, patterns, preview_colors, sliders, labels, heading } = self;
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<ParamWords>();
|
||||
let units = env.units();
|
||||
@@ -220,6 +229,7 @@ impl<'a> ParamRows<'a> {
|
||||
pass.previews = preview_colors;
|
||||
pass.sliders = sliders;
|
||||
pass.shaded = labels == Some(LabelScheme::Inline);
|
||||
pass.heading = heading.map(|heading| plain(&heading).as_str().to_owned());
|
||||
let mut changed = false;
|
||||
let mut values = None;
|
||||
let mut edited = Vec::new();
|
||||
@@ -315,6 +325,8 @@ struct Pass<'p> {
|
||||
sliders: SliderFieldLayout,
|
||||
/// The option groups are shaded: a card under each section's rows (`ParamRows::labels`).
|
||||
shaded: bool,
|
||||
/// The heading the rows stand under (`ParamRows::heading`).
|
||||
heading: Option<String>,
|
||||
}
|
||||
|
||||
/// A parameter as the pass shows it: its key, its label without the colon, and whether it takes input.
|
||||
@@ -370,6 +382,7 @@ impl<'p> Pass<'p> {
|
||||
previews: &[],
|
||||
sliders: SliderFieldLayout::Inline,
|
||||
shaded: false,
|
||||
heading: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -435,6 +448,7 @@ impl<'p> Pass<'p> {
|
||||
let end = tail.iter().position(is_section).unwrap_or(tail.len());
|
||||
let (body, after) = tail.split_at(end);
|
||||
if head.visible(&self.work) {
|
||||
let outer = self.heading.replace(plain(&head.label).as_str().to_owned());
|
||||
if self.shaded {
|
||||
// A document dialog's option group: a caption over its rows on a card.
|
||||
Section::fixed(plain(&head.label)).id_salt(head.key.as_str()).strength(HeaderStrength::Caption).show(ui, |ui, _| {
|
||||
@@ -451,6 +465,7 @@ impl<'p> Pass<'p> {
|
||||
}
|
||||
});
|
||||
}
|
||||
self.heading = outer;
|
||||
}
|
||||
rest = after;
|
||||
}
|
||||
@@ -785,7 +800,9 @@ impl<'p> Pass<'p> {
|
||||
// A document dialog sets the caption and its options on one line, as production's do.
|
||||
if inline {
|
||||
Row::new().wrap(true).show(ui, |ui, _| {
|
||||
Text::new(line.name.clone()).show(ui);
|
||||
if self.heading.as_deref() != Some(plain(&line.name).as_str()) {
|
||||
Text::new(line.name.clone()).show(ui);
|
||||
}
|
||||
body(ui);
|
||||
});
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user