mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:27:49 +00:00
look-dialogs-a: PageSetupDialog lays out as WordCraft's Page Setup: framed section boxes, margins, paper sides and grid counts two to a line, orientation and grid kinds in a row, Paper under Pages, section direction and vertical alignment in the Document Grid box, WordCraft's grid wording; trial and story
This commit is contained in:
+203
-151
@@ -17,14 +17,17 @@ use craft_behavior::{EditEvent, NumberCodec};
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_ui::choice::{Checkbox, Radio, RadioGroup};
|
||||
use craft_ui::contract::{Bind, EditResponse, Entry, FieldLayout, Pattern, Props, Remember as _, Show as _, ShowContainer as _, SideLabel, Trigger};
|
||||
use craft_ui::display::Divider;
|
||||
use craft_behavior::ColumnWidth;
|
||||
use craft_ui::display::{Divider, Text};
|
||||
use craft_ui::form::{Field, FieldGroup};
|
||||
use craft_ui::layout_components::{Grid, Row};
|
||||
use craft_ui::menu::{Menu, MenuItem};
|
||||
use craft_ui::notices::{Notice, NoticeLayout};
|
||||
use craft_ui::patterns::form_dialogs::{FormCx, FormDialog, FormOutput};
|
||||
use craft_ui::pickers::Select;
|
||||
use craft_ui::runtime::env::{Env, Label, UnitKind, Units};
|
||||
use craft_ui::sections::Section;
|
||||
use craft_ui::runtime::words::CommonWords;
|
||||
use craft_ui::sections::{GroupBox, HeaderStrength, TitleRow};
|
||||
use craft_ui::text::NumberField;
|
||||
use egui::{Response, Ui};
|
||||
|
||||
@@ -75,11 +78,11 @@ craft_ui::words! {
|
||||
section_direction: "pageSetupDialog.sectionDirection" = "Section direction";
|
||||
left_to_right: "pageSetupDialog.leftToRight" = "Left-to-right";
|
||||
right_to_left: "pageSetupDialog.rightToLeft" = "Right-to-left";
|
||||
grid: "pageSetupDialog.grid" = "Document grid";
|
||||
grid: "pageSetupDialog.grid" = "Document Grid";
|
||||
grid_none: "pageSetupDialog.gridNone" = "No grid";
|
||||
grid_lines: "pageSetupDialog.gridLines" = "Line grid only";
|
||||
grid_lines_and_chars: "pageSetupDialog.gridLinesAndChars" = "Line and character grid";
|
||||
grid_snap_to_chars: "pageSetupDialog.gridSnapToChars" = "Text snaps to character grid";
|
||||
grid_lines: "pageSetupDialog.gridLines" = "Lines only";
|
||||
grid_lines_and_chars: "pageSetupDialog.gridLinesAndChars" = "Lines and characters";
|
||||
grid_snap_to_chars: "pageSetupDialog.gridSnapToChars" = "Text snaps to characters";
|
||||
chars_per_line: "pageSetupDialog.charsPerLine" = "Characters per line";
|
||||
lines_per_page: "pageSetupDialog.linesPerPage" = "Lines per page";
|
||||
margins_dont_fit: "pageSetupDialog.marginsDontFit" = "The margins don't fit on a page this size; make the margins smaller first.";
|
||||
@@ -198,11 +201,16 @@ fn to_points(unit: UnitKind, value: f64) -> f64 {
|
||||
/// What the dialog's rows share in one pass.
|
||||
struct Rows<'a> {
|
||||
words: &'a PageSetupDialogWords,
|
||||
common: &'a CommonWords,
|
||||
units: Units,
|
||||
unit: UnitKind,
|
||||
}
|
||||
|
||||
/// The columns of a grid of number rows: a caption and its field, twice ("Top: [ ] Bottom: [ ]").
|
||||
const PAIRS: [ColumnWidth; 4] = [ColumnWidth::Content; 4];
|
||||
|
||||
/// One length of the setup: where it is and which values it takes (points).
|
||||
#[derive(Clone)]
|
||||
struct Length {
|
||||
key: &'static str,
|
||||
label: Label,
|
||||
@@ -237,9 +245,14 @@ fn apply(form: &mut FormCx<'_, '_>, setup: &mut PageSetup, edit: &EditResponse<O
|
||||
}
|
||||
|
||||
impl Rows<'_> {
|
||||
/// A length field in the dialog's unit over the setup's value, which an edit changes (clamped to the range). A value
|
||||
/// that rounds to what the field showed changes nothing, so an untouched length keeps its exact value. `false`
|
||||
/// while the field's text is not a number.
|
||||
/// The caption cell of a grid row: `label` with the language's colon, dimmed while the field is disabled.
|
||||
fn caption(&self, ui: &mut Ui, label: &Label, enabled: bool) {
|
||||
Text::new(Label::from(self.common.caption.fill(&[("caption", label.as_str())]))).enabled(enabled).show(ui);
|
||||
}
|
||||
|
||||
/// A length in the dialog's unit over the setup's value, as two grid cells (its caption and its field), which an
|
||||
/// edit changes (clamped to the range). A value that rounds to what the field showed changes nothing, so an
|
||||
/// untouched length keeps its exact value. `false` while the field's text is not a number.
|
||||
fn length(&self, ui: &mut Ui, form: &mut FormCx<'_, '_>, setup: &mut PageSetup, length: Length) -> bool {
|
||||
let Length { key, label, get, set, range } = length;
|
||||
let unit = self.unit;
|
||||
@@ -247,18 +260,14 @@ impl Rows<'_> {
|
||||
let exact = get(setup);
|
||||
let shown = rounded(in_unit(unit, exact), places);
|
||||
let codec = NumberCodec::new(self.units.reader(Some(unit)), places);
|
||||
let field = Field::new(label.clone())
|
||||
self.caption(ui, &label, true);
|
||||
let field = NumberField::new(label.clone(), Bind::given(Some(shown)))
|
||||
.id_salt(key)
|
||||
.show(ui, |ui, _| {
|
||||
NumberField::new(label.clone(), Bind::given(Some(shown)))
|
||||
.codec(codec.clone())
|
||||
.range(in_unit(unit, *range.start())..=in_unit(unit, *range.end()))
|
||||
.step(in_unit(unit, MARGIN_SCRUB_STEP))
|
||||
.unit(self.units.symbol(unit))
|
||||
.show(ui)
|
||||
})
|
||||
.inner;
|
||||
let Some(field) = field else { return true };
|
||||
.codec(codec)
|
||||
.range(in_unit(unit, *range.start())..=in_unit(unit, *range.end()))
|
||||
.step(in_unit(unit, MARGIN_SCRUB_STEP))
|
||||
.unit(self.units.symbol(unit))
|
||||
.show(ui);
|
||||
apply(form, setup, &field.edit, |next, value| {
|
||||
let points = if rounded(value, places) == shown { exact } else { to_points(unit, value).clamp(*range.start(), *range.end()) };
|
||||
set(next, points);
|
||||
@@ -266,7 +275,8 @@ impl Rows<'_> {
|
||||
!field.edit.unparsed
|
||||
}
|
||||
|
||||
/// The Margins section: the four sides and the gutter, with the notice under them while the margins do not fit.
|
||||
/// The Margins section: Top and Bottom, Left and Right, then the gutter, two to a line; the notice under them while
|
||||
/// the margins do not fit.
|
||||
fn margins(&self, ui: &mut Ui, form: &mut FormCx<'_, '_>, setup: &mut PageSetup, document_setup: bool) -> bool {
|
||||
let words = self.words;
|
||||
let (left, right) = if setup.mirrored { (&words.inside, &words.outside) } else { (&words.left, &words.right) };
|
||||
@@ -274,22 +284,22 @@ impl Rows<'_> {
|
||||
let (left, right) = (left.clone(), right.clone());
|
||||
section(ui, &words.margins, |ui| {
|
||||
let mut valid = true;
|
||||
let mut side_row = |ui: &mut Ui,
|
||||
form: &mut FormCx<'_, '_>,
|
||||
setup: &mut PageSetup,
|
||||
key: &'static str,
|
||||
label: Label,
|
||||
get: fn(&PageSetup) -> f64,
|
||||
set: fn(&mut PageSetup, f64)| {
|
||||
valid &= self.length(ui, form, setup, Length { key, label, get, set, range: side.clone() });
|
||||
};
|
||||
side_row(ui, form, setup, "top", words.top.clone(), |s| s.margins.top, |s, v| s.margins.top = v);
|
||||
side_row(ui, form, setup, "bottom", words.bottom.clone(), |s| s.margins.bottom, |s, v| s.margins.bottom = v);
|
||||
side_row(ui, form, setup, "left", left.clone(), |s| s.margins.left, |s, v| s.margins.left = v);
|
||||
side_row(ui, form, setup, "right", right.clone(), |s| s.margins.right, |s, v| s.margins.right = v);
|
||||
if !document_setup {
|
||||
side_row(ui, form, setup, "gutter", words.gutter.clone(), |s| s.gutter, |s, v| s.gutter = v);
|
||||
}
|
||||
Grid::new(&PAIRS).show(ui, |ui, _| {
|
||||
let mut side_cells = |ui: &mut Ui,
|
||||
key: &'static str,
|
||||
label: Label,
|
||||
get: fn(&PageSetup) -> f64,
|
||||
set: fn(&mut PageSetup, f64)| {
|
||||
valid &= self.length(ui, form, setup, Length { key, label, get, set, range: side.clone() });
|
||||
};
|
||||
side_cells(ui, "top", words.top.clone(), |s| s.margins.top, |s, v| s.margins.top = v);
|
||||
side_cells(ui, "bottom", words.bottom.clone(), |s| s.margins.bottom, |s, v| s.margins.bottom = v);
|
||||
side_cells(ui, "left", left.clone(), |s| s.margins.left, |s, v| s.margins.left = v);
|
||||
side_cells(ui, "right", right.clone(), |s| s.margins.right, |s, v| s.margins.right = v);
|
||||
if !document_setup {
|
||||
side_cells(ui, "gutter", words.gutter.clone(), |s| s.gutter, |s, v| s.gutter = v);
|
||||
}
|
||||
});
|
||||
valid && self.fit_notice(ui, setup)
|
||||
})
|
||||
.unwrap_or(true)
|
||||
@@ -310,8 +320,10 @@ impl Rows<'_> {
|
||||
section(ui, &words.orientation, |ui| {
|
||||
let after = SideLabel::new().layout(FieldLayout::After);
|
||||
let group = RadioGroup::new(words.orientation.clone(), Bind::given(setup.orientation)).show(ui, |ui, cx| {
|
||||
cx.labelled(ui, "portrait", words.portrait.clone(), after.clone(), Radio::new(Orientation::Portrait));
|
||||
cx.labelled(ui, "landscape", words.landscape.clone(), after.clone(), Radio::new(Orientation::Landscape));
|
||||
Row::new().show(ui, |ui, _| {
|
||||
cx.labelled(ui, "portrait", words.portrait.clone(), after.clone(), Radio::new(Orientation::Portrait));
|
||||
cx.labelled(ui, "landscape", words.landscape.clone(), after.clone(), Radio::new(Orientation::Landscape));
|
||||
});
|
||||
});
|
||||
if let Some(orientation) = group.output.value().copied() {
|
||||
setup.set_orientation(orientation);
|
||||
@@ -319,8 +331,9 @@ impl Rows<'_> {
|
||||
});
|
||||
}
|
||||
|
||||
/// The Pages section: Mirror margins, or Facing Pages and Number of Pages in Document Setup.
|
||||
fn pages(&self, ui: &mut Ui, form: &mut FormCx<'_, '_>, setup: &mut PageSetup, document_setup: bool) -> bool {
|
||||
/// The Pages section: Mirror margins with the Paper rows under their own heading (Page Setup), or Facing Pages and
|
||||
/// Number of Pages (Document Setup, whose Paper is a section of its own).
|
||||
fn pages(&self, ui: &mut Ui, form: &mut FormCx<'_, '_>, setup: &mut PageSetup, document_setup: bool, papers: &[PaperSize]) -> bool {
|
||||
let words = self.words;
|
||||
section(ui, &words.pages, |ui| {
|
||||
let label = if document_setup { &words.facing_pages } else { &words.mirror_margins };
|
||||
@@ -329,7 +342,8 @@ impl Rows<'_> {
|
||||
setup.mirrored = on;
|
||||
}
|
||||
if !document_setup {
|
||||
return true;
|
||||
TitleRow::new(words.paper.clone()).strength(HeaderStrength::Section).show(ui);
|
||||
return self.paper_rows(ui, form, setup, papers);
|
||||
}
|
||||
let shown = f64::from(setup.pages.unwrap_or(1));
|
||||
let codec = NumberCodec::new(self.units.reader(None), 0);
|
||||
@@ -353,83 +367,108 @@ impl Rows<'_> {
|
||||
.unwrap_or(true)
|
||||
}
|
||||
|
||||
/// The Paper section: the list of papers plus Custom size, and Width and Height.
|
||||
/// The Paper section of Document Setup.
|
||||
fn paper(&self, ui: &mut Ui, form: &mut FormCx<'_, '_>, setup: &mut PageSetup, papers: &[PaperSize]) -> bool {
|
||||
let words = self.words;
|
||||
section(ui, &words.paper, |ui| {
|
||||
let named = paper_named(papers, setup.width, setup.height).and_then(|paper| papers.iter().position(|other| other == paper));
|
||||
let custom = form.memory::<CustomSize>().0;
|
||||
let choice = match named {
|
||||
Some(index) if !custom => PaperChoice::Named(index),
|
||||
_ => PaperChoice::Custom,
|
||||
};
|
||||
let mut entries: Vec<Entry<PaperChoice>> =
|
||||
papers.iter().enumerate().map(|(index, paper)| Entry::new(PaperChoice::Named(index), words.paper_name(paper))).collect();
|
||||
entries.push(Entry::new(PaperChoice::Custom, words.custom_size.clone()));
|
||||
Field::new(words.paper_size.clone()).id_salt("paper").show(ui, |ui, _| {
|
||||
let picked = Select::new(words.paper_size.clone(), Bind::given(choice), &entries).show(ui);
|
||||
match picked.value().copied() {
|
||||
Some(PaperChoice::Named(index)) => {
|
||||
if let Some(paper) = papers.get(index) {
|
||||
let (width, height) = setup.orientation.turn((paper.width, paper.height));
|
||||
setup.set_size(width, height);
|
||||
}
|
||||
form.memory::<CustomSize>().0 = false;
|
||||
}
|
||||
Some(PaperChoice::Custom) => form.memory::<CustomSize>().0 = true,
|
||||
None => {}
|
||||
}
|
||||
});
|
||||
let sides = MIN_PAGE_SIDE..=MAX_PAGE_SIDE;
|
||||
let width =
|
||||
Length { key: "width", label: words.width.clone(), get: |s| s.width, set: |s, v| s.set_size(v, s.height), range: sides.clone() };
|
||||
let height = Length { key: "height", label: words.height.clone(), get: |s| s.height, set: |s, v| s.set_size(s.width, v), range: sides };
|
||||
let wide = self.length(ui, form, setup, width);
|
||||
self.length(ui, form, setup, height) && wide
|
||||
})
|
||||
.unwrap_or(true)
|
||||
section(ui, &self.words.paper, |ui| self.paper_rows(ui, form, setup, papers)).unwrap_or(true)
|
||||
}
|
||||
|
||||
/// The Document grid section, shown while the setup has a grid: the kind, then the two counts the kind enables.
|
||||
/// `false` while a count's text is not a number.
|
||||
fn grid(&self, ui: &mut Ui, form: &mut FormCx<'_, '_>, setup: &mut PageSetup) -> bool {
|
||||
let Some(grid) = setup.grid else { return true };
|
||||
/// The Paper rows: the list of papers plus Custom size, then Width and Height on one line.
|
||||
fn paper_rows(&self, ui: &mut Ui, form: &mut FormCx<'_, '_>, setup: &mut PageSetup, papers: &[PaperSize]) -> bool {
|
||||
let words = self.words;
|
||||
let named = paper_named(papers, setup.width, setup.height).and_then(|paper| papers.iter().position(|other| other == paper));
|
||||
let custom = form.memory::<CustomSize>().0;
|
||||
let choice = match named {
|
||||
Some(index) if !custom => PaperChoice::Named(index),
|
||||
_ => PaperChoice::Custom,
|
||||
};
|
||||
let mut entries: Vec<Entry<PaperChoice>> =
|
||||
papers.iter().enumerate().map(|(index, paper)| Entry::new(PaperChoice::Named(index), words.paper_name(paper))).collect();
|
||||
entries.push(Entry::new(PaperChoice::Custom, words.custom_size.clone()));
|
||||
Field::new(words.paper_size.clone()).id_salt("paper").show(ui, |ui, _| {
|
||||
let picked = Select::new(words.paper_size.clone(), Bind::given(choice), &entries).show(ui);
|
||||
match picked.value().copied() {
|
||||
Some(PaperChoice::Named(index)) => {
|
||||
if let Some(paper) = papers.get(index) {
|
||||
let (width, height) = setup.orientation.turn((paper.width, paper.height));
|
||||
setup.set_size(width, height);
|
||||
}
|
||||
form.memory::<CustomSize>().0 = false;
|
||||
}
|
||||
Some(PaperChoice::Custom) => form.memory::<CustomSize>().0 = true,
|
||||
None => {}
|
||||
}
|
||||
});
|
||||
let sides = MIN_PAGE_SIDE..=MAX_PAGE_SIDE;
|
||||
let width = Length { key: "width", label: words.width.clone(), get: |s| s.width, set: |s, v| s.set_size(v, s.height), range: sides.clone() };
|
||||
let height = Length { key: "height", label: words.height.clone(), get: |s| s.height, set: |s, v| s.set_size(s.width, v), range: sides };
|
||||
let mut valid = true;
|
||||
Grid::new(&PAIRS).show(ui, |ui, _| {
|
||||
let wide = self.length(ui, form, setup, width.clone());
|
||||
valid = self.length(ui, form, setup, height.clone()) && wide;
|
||||
});
|
||||
valid
|
||||
}
|
||||
|
||||
/// The Document Grid section, shown while the setup has a grid: the kinds in a wrapping row, the two counts the
|
||||
/// kind enables on one line, then the section direction (with `direction`) and the vertical alignment (with
|
||||
/// `layout`). Without a grid, the section direction is a section of its own and the vertical alignment heads the
|
||||
/// Headers and footers section. `false` while a count's text is not a number.
|
||||
fn grid(&self, ui: &mut Ui, form: &mut FormCx<'_, '_>, setup: &mut PageSetup, layout: bool, direction: bool) -> bool {
|
||||
let words = self.words;
|
||||
let Some(grid) = setup.grid else {
|
||||
if direction {
|
||||
section(ui, &words.section_direction, |ui| self.direction_row(ui, setup));
|
||||
}
|
||||
return true;
|
||||
};
|
||||
section(ui, &words.grid, |ui| {
|
||||
let after = SideLabel::new().layout(FieldLayout::After);
|
||||
let group = RadioGroup::new(words.grid.clone(), Bind::given(grid.kind)).show(ui, |ui, cx| {
|
||||
cx.labelled(ui, "none", words.grid_none.clone(), after.clone(), Radio::new(GridKind::None));
|
||||
cx.labelled(ui, "lines", words.grid_lines.clone(), after.clone(), Radio::new(GridKind::Lines));
|
||||
cx.labelled(ui, "linesAndChars", words.grid_lines_and_chars.clone(), after.clone(), Radio::new(GridKind::LinesAndChars));
|
||||
cx.labelled(ui, "snapToChars", words.grid_snap_to_chars.clone(), after.clone(), Radio::new(GridKind::SnapToChars));
|
||||
Row::new().wrap(true).show(ui, |ui, _| {
|
||||
cx.labelled(ui, "none", words.grid_none.clone(), after.clone(), Radio::new(GridKind::None));
|
||||
cx.labelled(ui, "lines", words.grid_lines.clone(), after.clone(), Radio::new(GridKind::Lines));
|
||||
cx.labelled(ui, "linesAndChars", words.grid_lines_and_chars.clone(), after.clone(), Radio::new(GridKind::LinesAndChars));
|
||||
cx.labelled(ui, "snapToChars", words.grid_snap_to_chars.clone(), after.clone(), Radio::new(GridKind::SnapToChars));
|
||||
});
|
||||
});
|
||||
if let (Some(kind), Some(grid)) = (group.output.value().copied(), setup.grid.as_mut()) {
|
||||
grid.kind = kind;
|
||||
}
|
||||
let kind = setup.grid.map_or(grid.kind, |grid| grid.kind);
|
||||
let chars = self.count(
|
||||
ui,
|
||||
form,
|
||||
setup,
|
||||
("chars", words.chars_per_line.clone(), kind.chars_enabled()),
|
||||
|grid| grid.chars_per_line,
|
||||
|grid, count| grid.chars_per_line = count,
|
||||
);
|
||||
let lines = self.count(
|
||||
ui,
|
||||
form,
|
||||
setup,
|
||||
("lines", words.lines_per_page.clone(), kind.lines_enabled()),
|
||||
|grid| grid.lines_per_page,
|
||||
|grid, count| grid.lines_per_page = count,
|
||||
);
|
||||
chars && lines
|
||||
let mut valid = true;
|
||||
Grid::new(&PAIRS).show(ui, |ui, _| {
|
||||
let chars = self.count(
|
||||
ui,
|
||||
form,
|
||||
setup,
|
||||
("chars", words.chars_per_line.clone(), kind.chars_enabled()),
|
||||
|grid| grid.chars_per_line,
|
||||
|grid, count| grid.chars_per_line = count,
|
||||
);
|
||||
let lines = self.count(
|
||||
ui,
|
||||
form,
|
||||
setup,
|
||||
("lines", words.lines_per_page.clone(), kind.lines_enabled()),
|
||||
|grid| grid.lines_per_page,
|
||||
|grid, count| grid.lines_per_page = count,
|
||||
);
|
||||
valid = chars && lines;
|
||||
});
|
||||
if direction {
|
||||
self.direction_row(ui, setup);
|
||||
}
|
||||
if layout {
|
||||
self.vertical_row(ui, setup);
|
||||
}
|
||||
valid
|
||||
})
|
||||
.unwrap_or(true)
|
||||
}
|
||||
|
||||
/// One count of the grid, a whole number from 1 to [`MAX_GRID_COUNT`] that `enabled` lets the user edit; `key`,
|
||||
/// `label` and `enabled` are given as one tuple. `false` while the field's text is not a number.
|
||||
/// One count of the grid as two grid cells (its caption and its field), a whole number from 1 to
|
||||
/// [`MAX_GRID_COUNT`] that `enabled` lets the user edit; `key`, `label` and `enabled` are given as one tuple.
|
||||
/// `false` while the field's text is not a number.
|
||||
fn count(
|
||||
&self,
|
||||
ui: &mut Ui,
|
||||
@@ -442,17 +481,13 @@ impl Rows<'_> {
|
||||
let Some(grid) = setup.grid else { return true };
|
||||
let shown = f64::from(get(&grid));
|
||||
let codec = NumberCodec::new(self.units.reader(None), 0);
|
||||
let field = Field::new(label.clone())
|
||||
self.caption(ui, &label, enabled);
|
||||
let field = NumberField::new(label.clone(), Bind::given(Some(shown)))
|
||||
.id_salt(key)
|
||||
.show(ui, |ui, _| {
|
||||
NumberField::new(label.clone(), Bind::given(Some(shown)))
|
||||
.codec(codec.clone())
|
||||
.range(1.0..=f64::from(MAX_GRID_COUNT))
|
||||
.enabled(enabled)
|
||||
.show(ui)
|
||||
})
|
||||
.inner;
|
||||
let Some(field) = field else { return true };
|
||||
.codec(codec)
|
||||
.range(1.0..=f64::from(MAX_GRID_COUNT))
|
||||
.enabled(enabled)
|
||||
.show(ui);
|
||||
apply(form, setup, &field.edit, |next, value| {
|
||||
if let Some(grid) = next.grid.as_mut() {
|
||||
set(grid, value.round().clamp(1.0, f64::from(MAX_GRID_COUNT)) as u32);
|
||||
@@ -461,22 +496,31 @@ impl Rows<'_> {
|
||||
!field.edit.unparsed
|
||||
}
|
||||
|
||||
/// The Headers and footers section: vertical alignment, odd and even pages, first page.
|
||||
fn layout(&self, ui: &mut Ui, setup: &mut PageSetup) {
|
||||
/// The Vertical alignment row.
|
||||
fn vertical_row(&self, ui: &mut Ui, setup: &mut PageSetup) {
|
||||
let words = self.words;
|
||||
let entries = [
|
||||
Entry::new(PageVerticalAlign::Top, words.align_top.clone()),
|
||||
Entry::new(PageVerticalAlign::Center, words.align_center.clone()),
|
||||
Entry::new(PageVerticalAlign::Justify, words.align_justified.clone()),
|
||||
Entry::new(PageVerticalAlign::Bottom, words.align_bottom.clone()),
|
||||
];
|
||||
Field::new(words.vertical_alignment.clone()).id_salt("vertical").show(ui, |ui, _| {
|
||||
let picked = Select::new(words.vertical_alignment.clone(), Bind::given(setup.vertical_align), &entries).show(ui);
|
||||
if let Some(align) = picked.value().copied() {
|
||||
setup.vertical_align = align;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The Headers and footers section: odd and even pages and first page, under the vertical alignment when
|
||||
/// `vertical` (a setup without a grid, whose Document Grid section does not show it).
|
||||
fn layout(&self, ui: &mut Ui, setup: &mut PageSetup, vertical: bool) {
|
||||
let words = self.words;
|
||||
section(ui, &words.headers_and_footers, |ui| {
|
||||
let entries = [
|
||||
Entry::new(PageVerticalAlign::Top, words.align_top.clone()),
|
||||
Entry::new(PageVerticalAlign::Center, words.align_center.clone()),
|
||||
Entry::new(PageVerticalAlign::Justify, words.align_justified.clone()),
|
||||
Entry::new(PageVerticalAlign::Bottom, words.align_bottom.clone()),
|
||||
];
|
||||
Field::new(words.vertical_alignment.clone()).id_salt("vertical").show(ui, |ui, _| {
|
||||
let picked = Select::new(words.vertical_alignment.clone(), Bind::given(setup.vertical_align), &entries).show(ui);
|
||||
if let Some(align) = picked.value().copied() {
|
||||
setup.vertical_align = align;
|
||||
}
|
||||
});
|
||||
if vertical {
|
||||
self.vertical_row(ui, setup);
|
||||
}
|
||||
let odd_even = Checkbox::new(Bind::given(setup.different_odd_even)).label(words.different_odd_even.clone()).show(ui);
|
||||
if let Some(on) = odd_even.value().copied() {
|
||||
setup.different_odd_even = on;
|
||||
@@ -488,14 +532,17 @@ impl Rows<'_> {
|
||||
});
|
||||
}
|
||||
|
||||
/// The section direction section.
|
||||
fn direction(&self, ui: &mut Ui, setup: &mut PageSetup) {
|
||||
/// The Section direction row: its caption, then Left-to-right and Right-to-left on the same line.
|
||||
fn direction_row(&self, ui: &mut Ui, setup: &mut PageSetup) {
|
||||
let words = self.words;
|
||||
section(ui, &words.section_direction, |ui| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
self.caption(ui, &words.section_direction, true);
|
||||
let after = SideLabel::new().layout(FieldLayout::After);
|
||||
let group = RadioGroup::new(words.section_direction.clone(), Bind::given(setup.right_to_left)).show(ui, |ui, cx| {
|
||||
cx.labelled(ui, "ltr", words.left_to_right.clone(), after.clone(), Radio::new(false));
|
||||
cx.labelled(ui, "rtl", words.right_to_left.clone(), after.clone(), Radio::new(true));
|
||||
Row::new().show(ui, |ui, _| {
|
||||
cx.labelled(ui, "ltr", words.left_to_right.clone(), after.clone(), Radio::new(false));
|
||||
cx.labelled(ui, "rtl", words.right_to_left.clone(), after.clone(), Radio::new(true));
|
||||
});
|
||||
});
|
||||
if let Some(on) = group.output.value().copied() {
|
||||
setup.right_to_left = on;
|
||||
@@ -504,29 +551,34 @@ impl Rows<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A section of the dialog: a rule and `title` over a field group, so the section's fields share one label column.
|
||||
/// A section of the dialog: a framed box titled `title` over a field group, so the section's fields share one label
|
||||
/// column.
|
||||
fn section<R>(ui: &mut Ui, title: &Label, mut body: impl FnMut(&mut Ui) -> R) -> Option<R> {
|
||||
Section::fixed(title.clone()).show(ui, |ui, _| FieldGroup::new(title.clone()).show(ui, |ui, _| body(ui)).inner).inner.flatten()
|
||||
GroupBox::new(title.clone(), None).show(ui, |ui, _| FieldGroup::new(title.clone()).show(ui, |ui, _| body(ui)).inner).inner.flatten()
|
||||
}
|
||||
|
||||
/// Page Setup (or Document Setup) as a form dialog over a [`PageSetup`]. OK answers the edited setup and is refused,
|
||||
/// with [`PageSetupDialogWords::margins_dont_fit`] under the fields, while the margins don't fit the paper
|
||||
/// ([`Margins::fit`](crate::setup::Margins::fit)).
|
||||
///
|
||||
/// - Margins: Top, Bottom, Left (Inside with mirrored margins), Right (Outside) and Gutter, in the dialog's
|
||||
/// Each section is a framed box; lengths and counts stand two to a line, caption then field.
|
||||
///
|
||||
/// - Margins: Top and Bottom, Left (Inside with mirrored margins) and Right (Outside), then Gutter, in the dialog's
|
||||
/// [`unit`](PageSetupDialog::unit), each a number field that reads any unit, scrubs by [`MARGIN_SCRUB_STEP`] page
|
||||
/// points and shows the unit's symbol.
|
||||
/// - Orientation: Portrait and Landscape; switching turns the size (and the margins)
|
||||
/// - Orientation: Portrait and Landscape on one line; switching turns the size (and the margins)
|
||||
/// ([`PageSetup::set_orientation`]).
|
||||
/// - Pages: Mirror margins (Word) or Facing Pages and Number of Pages (InDesign, with
|
||||
/// [`document_setup`](PageSetupDialog::document_setup)).
|
||||
/// - Paper: a list of [`papers`](PageSetupDialog::papers) plus Custom size, and Width and Height
|
||||
/// ([`MIN_PAGE_SIDE`] to [`MAX_PAGE_SIDE`]); Custom size stays chosen while the numbers match a name; a typed size
|
||||
/// wider than tall turns the orientation ([`PageSetup::set_size`]).
|
||||
/// - Document grid, while the setup has a [`grid`](PageSetup::grid): its kind and the characters per line and lines per
|
||||
/// page the kind enables.
|
||||
/// - With [`layout`](PageSetupDialog::layout): Vertical alignment, Different Odd & Even Pages, Different First Page;
|
||||
/// with [`direction`](PageSetupDialog::direction): Section direction.
|
||||
/// - Paper (under Pages in Page Setup, its own section in Document Setup): a list of
|
||||
/// [`papers`](PageSetupDialog::papers) plus Custom size, and Width and Height ([`MIN_PAGE_SIDE`] to
|
||||
/// [`MAX_PAGE_SIDE`]); Custom size stays chosen while the numbers match a name; a typed size wider than tall turns the
|
||||
/// orientation ([`PageSetup::set_size`]).
|
||||
/// - Document Grid, while the setup has a [`grid`](PageSetup::grid): its kinds in a wrapping row and the characters per
|
||||
/// line and lines per page the kind enables; then, with [`direction`](PageSetupDialog::direction), Section direction
|
||||
/// and, with [`layout`](PageSetupDialog::layout), Vertical alignment.
|
||||
/// - With [`layout`](PageSetupDialog::layout): Headers and footers, Different Odd & Even Pages and Different First
|
||||
/// Page (under Vertical alignment when the setup has no grid; Section direction is then a section of its own).
|
||||
/// - A length or count gesture previews in the setup and the cancel of the gesture restores the whole setup from
|
||||
/// before it; text no number can come from keeps OK disabled until it is fixed.
|
||||
/// - Keys: Enter is OK, Escape Cancel, and holding Alt turns Cancel into Reset. The Top field has the focus when the
|
||||
@@ -579,7 +631,8 @@ impl<'a> PageSetupDialog<'a> {
|
||||
let PageSetupDialog { setup, papers, unit, document_setup, layout, direction, open, held } = self;
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<PageSetupDialogWords>();
|
||||
let rows = Rows { words: &words, units: env.units(), unit };
|
||||
let common = env.words::<CommonWords>();
|
||||
let rows = Rows { words: &words, common: &common, units: env.units(), unit };
|
||||
let title = if document_setup { words.document_setup.clone() } else { words.title.clone() };
|
||||
let mut dialog = FormDialog::new(title, setup).outer_gesture(true);
|
||||
if let Some(open) = open {
|
||||
@@ -591,14 +644,13 @@ impl<'a> PageSetupDialog<'a> {
|
||||
dialog.show(ui, |ui, form, setup| {
|
||||
let margins = rows.margins(ui, form, setup, document_setup);
|
||||
rows.orientation(ui, setup);
|
||||
let pages = rows.pages(ui, form, setup, document_setup);
|
||||
let paper = rows.paper(ui, form, setup, papers);
|
||||
let grid = rows.grid(ui, form, setup);
|
||||
// Page Setup's Paper rows sit in the Pages section; Document Setup's are a section of their own.
|
||||
let pages = rows.pages(ui, form, setup, document_setup, papers);
|
||||
let paper = !document_setup || rows.paper(ui, form, setup, papers);
|
||||
let gridded = setup.grid.is_some();
|
||||
let grid = rows.grid(ui, form, setup, layout, direction);
|
||||
if layout {
|
||||
rows.layout(ui, setup);
|
||||
}
|
||||
if direction {
|
||||
rows.direction(ui, setup);
|
||||
rows.layout(ui, setup, !gridded);
|
||||
}
|
||||
margins && pages && paper && grid
|
||||
})
|
||||
|
||||
@@ -30,6 +30,16 @@ fn gridded() -> PageSetup {
|
||||
PageSetup { grid: Some(DocumentGrid::new(GridKind::LinesAndChars, 40, 30)), ..PageSetup::default() }
|
||||
}
|
||||
|
||||
/// WordCraft's Page Setup: a Document Grid with the section direction and the vertical alignment in its box, then
|
||||
/// Headers and footers; the margins, paper sides and grid counts two to a line.
|
||||
#[story(PageSetupDialog<'static>, covers = [], drive = Drive::Never)]
|
||||
fn page_setup_word(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let start = PageSetup { grid: Some(DocumentGrid::new(GridKind::None, 36, 39)), ..PageSetup::default() };
|
||||
let setup = data.value("setup", start);
|
||||
PageSetupDialog::new(Bind::bound(setup)).layout().direction().opened(Bind::given(true)).show(ui);
|
||||
StoryOutput::default()
|
||||
}
|
||||
|
||||
/// The dialog over a setup with a Document grid, opened by its button; the kind radios switch which counts apply.
|
||||
#[story(PageSetupDialog<'static>, covers = [], drive = Drive::Never)]
|
||||
fn page_setup_grid(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
|
||||
@@ -63,9 +63,9 @@ fn the_grid_counts_follow_the_grid_kind_and_a_refused_ok_shows_its_reason() {
|
||||
fixture.click(&radio).settle();
|
||||
};
|
||||
assert!(disabled(&fixture, "Characters per line") && disabled(&fixture, "Lines per page"), "no grid: both counts disabled");
|
||||
pick(&mut fixture, "Line grid only");
|
||||
pick(&mut fixture, "Lines only");
|
||||
assert!(disabled(&fixture, "Characters per line") && !disabled(&fixture, "Lines per page"), "lines only: just the lines count");
|
||||
pick(&mut fixture, "Line and character grid");
|
||||
pick(&mut fixture, "Lines and characters");
|
||||
assert!(!disabled(&fixture, "Characters per line") && !disabled(&fixture, "Lines per page"), "lines and characters: both counts");
|
||||
|
||||
let field = fixture.by_label("Characters per line").expect("the characters field");
|
||||
@@ -84,3 +84,33 @@ fn the_grid_counts_follow_the_grid_kind_and_a_refused_ok_shows_its_reason() {
|
||||
pick(&mut fixture, "No grid");
|
||||
assert!(fixture.by_label(reason).is_none(), "changing a value clears the refusal");
|
||||
}
|
||||
|
||||
/// Page Setup lays its rows out two to a line, as WordCraft's dialog did: Top beside Bottom, Left beside Right, Width
|
||||
/// beside Height, Portrait beside Landscape, the grid kinds and the two grid counts on one line, and the section
|
||||
/// direction on the counts' section. It then fits the default room without scrolling, Different First Page included.
|
||||
#[trial]
|
||||
fn page_setup_rows_stand_two_to_a_line() {
|
||||
let start = PageSetup { grid: Some(DocumentGrid::new(GridKind::None, 40, 30)), ..PageSetup::default() };
|
||||
let state = Rc::new(RefCell::new(start));
|
||||
let seen = Rc::clone(&state);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut setup = seen.borrow_mut();
|
||||
PageSetupDialog::new(Bind::bound(&mut *setup)).layout().direction().opened(Bind::given(true)).show(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(1440.0, 900.0)).settle();
|
||||
let rect = |label: &str| fixture.by_label(label).unwrap_or_else(|| panic!("{label} is shown")).rect;
|
||||
let same_line = |a: &str, b: &str| {
|
||||
let (a_rect, b_rect) = (rect(a), rect(b));
|
||||
assert!((a_rect.center().y - b_rect.center().y).abs() < 1.0 && a_rect.max.x <= b_rect.min.x, "{a} stands before {b} on its line");
|
||||
};
|
||||
same_line("Top", "Bottom");
|
||||
same_line("Left", "Right");
|
||||
same_line("Width", "Height");
|
||||
same_line("Portrait", "Landscape");
|
||||
same_line("No grid", "Lines only");
|
||||
same_line("Characters per line", "Lines per page");
|
||||
same_line("Left-to-right", "Right-to-left");
|
||||
assert!(rect("Top").max.y < rect("Left").min.y, "Left and Right are on the line under Top and Bottom");
|
||||
let first = rect("Different First Page");
|
||||
assert!(first.max.y <= 900.0, "the last row is in view without scrolling: {first:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user