mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:27:49 +00:00
Merge branch 'red/look-rest-3' into area/ribbon-fw
This commit is contained in:
@@ -334,36 +334,54 @@ impl Rows<'_> {
|
||||
/// 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 };
|
||||
let check = Checkbox::new(Bind::given(setup.mirrored)).label(label.clone()).show(ui);
|
||||
if let Some(on) = check.value().copied() {
|
||||
setup.mirrored = on;
|
||||
}
|
||||
if !document_setup {
|
||||
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);
|
||||
let field = Field::new(words.number_of_pages.clone())
|
||||
.id_salt("pages")
|
||||
.show(ui, |ui, _| {
|
||||
NumberField::new(words.number_of_pages.clone(), Bind::given(Some(shown)))
|
||||
.codec(codec.clone())
|
||||
.range(1.0..=f64::from(MAX_PAGES))
|
||||
.show(ui)
|
||||
})
|
||||
.inner;
|
||||
let Some(field) = field else { return true };
|
||||
apply(form, setup, &field.edit, |next, value| {
|
||||
if value != shown {
|
||||
next.pages = Some(value.round() as u32);
|
||||
let title = &words.pages;
|
||||
// The check box stands at the section's start, as WordCraft's dialog has it: outside the field group, whose
|
||||
// label column (Paper size:, Width:) would set it in the field column.
|
||||
GroupBox::new(title.clone(), None)
|
||||
.show(ui, |ui, _| {
|
||||
let label = if document_setup { &words.facing_pages } else { &words.mirror_margins };
|
||||
let check = Checkbox::new(Bind::given(setup.mirrored)).label(label.clone()).show(ui);
|
||||
if let Some(on) = check.value().copied() {
|
||||
setup.mirrored = on;
|
||||
}
|
||||
});
|
||||
!field.edit.unparsed
|
||||
})
|
||||
.unwrap_or(true)
|
||||
if !document_setup {
|
||||
TitleRow::new(words.paper.clone()).strength(HeaderStrength::Section).show(ui);
|
||||
}
|
||||
FieldGroup::new(title.clone())
|
||||
.show(ui, |ui, _| {
|
||||
if !document_setup {
|
||||
return self.paper_rows(ui, form, setup, papers);
|
||||
}
|
||||
self.page_count(ui, form, setup)
|
||||
})
|
||||
.inner
|
||||
})
|
||||
.inner
|
||||
.flatten()
|
||||
.unwrap_or(true)
|
||||
}
|
||||
|
||||
/// Document Setup's Number of Pages row; `false` while its text is not a number.
|
||||
fn page_count(&self, ui: &mut Ui, form: &mut FormCx<'_, '_>, setup: &mut PageSetup) -> bool {
|
||||
let words = self.words;
|
||||
let shown = f64::from(setup.pages.unwrap_or(1));
|
||||
let codec = NumberCodec::new(self.units.reader(None), 0);
|
||||
let field = Field::new(words.number_of_pages.clone())
|
||||
.id_salt("pages")
|
||||
.show(ui, |ui, _| {
|
||||
NumberField::new(words.number_of_pages.clone(), Bind::given(Some(shown)))
|
||||
.codec(codec.clone())
|
||||
.range(1.0..=f64::from(MAX_PAGES))
|
||||
.show(ui)
|
||||
})
|
||||
.inner;
|
||||
let Some(field) = field else { return true };
|
||||
apply(form, setup, &field.edit, |next, value| {
|
||||
if value != shown {
|
||||
next.pages = Some(value.round() as u32);
|
||||
}
|
||||
});
|
||||
!field.edit.unparsed
|
||||
}
|
||||
|
||||
/// The Paper section of Document Setup.
|
||||
|
||||
@@ -86,8 +86,10 @@ fn the_grid_counts_follow_the_grid_kind_and_a_refused_ok_shows_its_reason() {
|
||||
}
|
||||
|
||||
/// 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.
|
||||
/// beside Height, Portrait beside Landscape, the grid kinds side by side (Text snaps to characters on a second line, at
|
||||
/// the dialog's wrap width) and the two grid counts on one line, and the section direction on the counts' section.
|
||||
/// Mirror margins stands at its section's start, as the Headers and footers check boxes do, not in the field column.
|
||||
/// 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() };
|
||||
@@ -111,6 +113,8 @@ fn page_setup_rows_stand_two_to_a_line() {
|
||||
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");
|
||||
assert!(rect("No grid").max.y <= rect("Text snaps to characters").min.y, "the last grid kind wraps onto a second line");
|
||||
let first = rect("Different First Page");
|
||||
assert!((rect("Mirror margins").min.x - first.min.x).abs() < 1.0, "Mirror margins stands at its section's start");
|
||||
assert!(first.max.y <= 900.0, "the last row is in view without scrolling: {first:?}");
|
||||
}
|
||||
|
||||
@@ -270,7 +270,9 @@ pub(crate) fn flow_leaf<C: Show>(
|
||||
let placed = leaf(ui, &env, &at, &mut child, trailing);
|
||||
// In a column that sizes from its content the row is the label plus the control, never the room around them.
|
||||
// A row of a horizontal flow is as wide as its label and control: the room it has there is unbounded.
|
||||
let content = env.content.0 || ui.layout().is_horizontal();
|
||||
// So is a grid's cell: the room before it is the whole grid's, and a row spanning it measures its column as wide
|
||||
// as the grid, so the grid could never set two such cells beside each other (two columns of check boxes).
|
||||
let content = env.content.0 || ui.layout().is_horizontal() || crate::layout::grid_cell(ui).is_some();
|
||||
let control_width = if content { placed.control.width().min(ideal.x) } else { placed.control.width() };
|
||||
let rtl = env.direction.is_rtl();
|
||||
let cell = Rect::from_min_max(placed.control.min, egui::pos2(placed.control.max.x, placed.control.min.y + ideal.y));
|
||||
|
||||
@@ -1523,6 +1523,14 @@ pub(crate) fn run_body_measured<R>(
|
||||
BodyLayout::Row { wrap, align } => {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let rect = match *align {
|
||||
// A wrapping row wraps at the line length its container sets for paragraphs (a dialog's), as the
|
||||
// paragraphs beside it do: a dialog sized from its content stays as wide as its text, and a row of
|
||||
// radios that would not fit there takes a second line instead of widening the dialog.
|
||||
// A room unbounded at its end edge (a measuring run's) is cut from its start edge.
|
||||
RowAlign::Start | RowAlign::SpaceBetween if *wrap => env
|
||||
.measure()
|
||||
.filter(|measure| *measure < room.width() && (!rtl || room.right().is_finite()))
|
||||
.map(|measure| Flow::new(room, rtl).span(0.0, measure)),
|
||||
RowAlign::Start | RowAlign::SpaceBetween => None,
|
||||
RowAlign::Center | RowAlign::End => {
|
||||
let last = this_pass(&ctx, sizing, id);
|
||||
|
||||
@@ -363,11 +363,16 @@ impl Shown<'_> {
|
||||
tabs.extend(app.iter().enumerate().filter_map(|(at, title)| u8::try_from(at).ok().map(|at| (AboutTab::App(at), title.clone()))));
|
||||
let source = keyed(&tabs[..], |tab: &(AboutTab, Label)| tab.0).with_text(|tab| Cow::Borrowed(tab.1.as_str()));
|
||||
let shown = view.borrow().tab;
|
||||
// The strip sits in the stack's header, which is as tall as what it holds: it takes its tabs' height.
|
||||
let out =
|
||||
TabStrip::new(title.clone()).kind(TabKind::Text).shown(shown).natural_height(true).show_items(ui, &source, &mut |key, _| {
|
||||
Tab::new(tabs.iter().find(|tab| tab.0 == *key).map_or_else(Label::default, |tab| tab.1.clone()))
|
||||
});
|
||||
// The strip sits in the stack's header, which is as tall as what it holds: it takes its tabs' height. The listed
|
||||
// layout's tabs are a strip of tab faces over a rule, as WordCraft's About had them; the centred layout's are
|
||||
// its text.
|
||||
let kind = match self.layout {
|
||||
AboutLayout::Listed => TabKind::Panel,
|
||||
AboutLayout::Centred => TabKind::Text,
|
||||
};
|
||||
let out = TabStrip::new(title.clone()).kind(kind).shown(shown).natural_height(true).show_items(ui, &source, &mut |key, _| {
|
||||
Tab::new(tabs.iter().find(|tab| tab.0 == *key).map_or_else(Label::default, |tab| tab.1.clone()))
|
||||
});
|
||||
for event in out.response.events {
|
||||
if let CollectionEvent::Selection(SelectionChange::Set(keys)) = event
|
||||
&& let Some(picked) = keys.last()
|
||||
|
||||
@@ -87,6 +87,24 @@ fn the_listed_about_is_a_wide_dialog() {
|
||||
assert!(listed > centred + 200.0, "the listed About is a wide dialog: {listed} against {centred}");
|
||||
}
|
||||
|
||||
/// The listed layout's tabs are tab faces, as WordCraft's About's strip: each title is set in from its tab's start edge.
|
||||
/// The centred layout's are their text alone, starting at the tab's edge.
|
||||
#[trial]
|
||||
fn the_listed_about_shows_tab_faces() {
|
||||
let inset = |layout: AboutLayout| {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let _ = AboutDialog::new(&INFO, &CREDITS).layout(layout).show(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(1400.0, 1000.0)).idle(30);
|
||||
let tab = fixture.by_role(Role::TAB).into_iter().find(|tab| fixture.tree().name_of(tab.node) == Some("Contributors")).expect("the tab");
|
||||
let text = fixture.texts().iter().find(|painted| painted.text == "Contributors").map(|painted| painted.rect).expect("its title");
|
||||
text.left() - tab.rect.left()
|
||||
};
|
||||
let (listed, centred) = (inset(AboutLayout::Listed), inset(AboutLayout::Centred));
|
||||
assert!(centred.abs() < 1.0, "a centred tab is its text: {centred}");
|
||||
assert!(listed > 3.0, "a listed tab is a face with its title set in: {listed}");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn about_tab_is_a_field() {
|
||||
let host = Rc::new(RefCell::new(DialogHost::new()));
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
//! The columns of a grid keep their least: a column whose content takes the room it is offered (wrapping text, a
|
||||
//! picture) leaves the others theirs, and a grid whose columns do not fit even at their least lays its cells out one
|
||||
//! under another. A picture takes the width it is given.
|
||||
//! under another. A picture takes the width it is given; a check box with its label after it takes the two's width.
|
||||
|
||||
use craft_behavior::{ColumnWidth, Mixed};
|
||||
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use craft_ui::choice::Checkbox;
|
||||
use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _};
|
||||
use craft_ui::display::Text;
|
||||
use craft_ui::layout_components::{Grid, Stack};
|
||||
use craft_ui::testing::{Frames, shrink_least_runs, trial};
|
||||
@@ -88,3 +89,26 @@ fn a_picture_takes_the_width_it_is_given() {
|
||||
let (wide, wider) = (width(500.0), width(700.0));
|
||||
assert!((wide - 500.0).abs() <= SNAP && (wider - 700.0).abs() <= SNAP, "the picture follows its room");
|
||||
}
|
||||
|
||||
/// Check boxes with their labels after them stand two to a line in a grid of two content columns, as a dialog's
|
||||
/// Effects box (Bold beside Italic, Underline beside Strikethrough): each cell is as wide as its box and label, not
|
||||
/// the whole grid, so the columns fit beside each other.
|
||||
#[trial]
|
||||
fn labelled_check_boxes_stand_two_to_a_line() {
|
||||
let seen = Cell::new([Rect::NOTHING; 4]);
|
||||
let _ = in_room(400.0, |ui| {
|
||||
Grid::new(HALVES).show(ui, |ui, _| {
|
||||
let mut rects = [Rect::NOTHING; 4];
|
||||
for (at, label) in ["Bold", "Italic", "Underline", "Strikethrough"].into_iter().enumerate() {
|
||||
rects[at] = Checkbox::new(Bind::given(false)).label(label).id_salt(label).show(ui).response.rect;
|
||||
}
|
||||
seen.set(rects);
|
||||
});
|
||||
});
|
||||
let [bold, italic, underline, strike] = seen.get();
|
||||
let beside = |a: Rect, b: Rect| (a.center().y - b.center().y).abs() <= SNAP && a.right() <= b.left() + SNAP;
|
||||
assert!(beside(bold, italic), "Italic stands beside Bold: {bold:?} {italic:?}");
|
||||
assert!(beside(underline, strike), "Strikethrough stands beside Underline: {underline:?} {strike:?}");
|
||||
assert!(underline.top() >= bold.bottom() - SNAP, "the second pair is on the next line");
|
||||
assert!((italic.left() - strike.left()).abs() <= SNAP, "the second column starts at one edge");
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
//! A content-sized `Stack`'s width roles: `least_width` keeps it at least the role's width with narrow content (a
|
||||
//! File page's side menu, `LeastWidth::SideMenu`), and `wrap_width` breaks a long paragraph at the role's line length
|
||||
//! instead of letting it widen the stack (a Print page's settings column, `WrapWidth::Column`).
|
||||
//! instead of letting it widen the stack (a Print page's settings column, `WrapWidth::Column`), and breaks a wrapping
|
||||
//! row there too.
|
||||
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{LeastWidth, Show as _, ShowContainer as _, WrapWidth};
|
||||
use craft_ui::contract::{LeastWidth, Props as _, Show as _, ShowContainer as _, WrapWidth};
|
||||
use craft_ui::display::Text;
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::layout_components::{Row, Stack};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Rect, Vec2};
|
||||
use std::cell::Cell;
|
||||
@@ -50,3 +51,44 @@ fn a_stacks_wrap_width_breaks_a_long_paragraph_at_the_column() {
|
||||
assert!(column.width() < plain.width() / 2.0, "the column's wrap width keeps the stack narrow: {column:?} against {plain:?}");
|
||||
assert!(wrapped.height() > one_line.height() * 2.0, "the paragraph breaks into lines: {wrapped:?} against {one_line:?}");
|
||||
}
|
||||
|
||||
/// Labels whose row is far wider than a column.
|
||||
const KINDS: [&str; 6] = ["No grid", "Lines only", "Lines and characters", "Text snaps to characters", "Left-to-right", "Right-to-left"];
|
||||
|
||||
/// A wrapping row in a stack with a wrap width breaks at the stack's line length, as its paragraphs do (a dialog's
|
||||
/// row of radios takes a second line), instead of widening the stack to keep its children on one line.
|
||||
#[trial]
|
||||
fn a_wrapping_row_breaks_at_the_stacks_wrap_width() {
|
||||
let shown = |wrap: Option<WrapWidth>| {
|
||||
let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING, Rect::NOTHING)));
|
||||
let out = Rc::clone(&seen);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let (first, last) = (Cell::new(Rect::NOTHING), Cell::new(Rect::NOTHING));
|
||||
let stack = match wrap {
|
||||
Some(wrap) => Stack::new().wrap_width(wrap),
|
||||
None => Stack::new(),
|
||||
};
|
||||
let shown = stack.show(ui, |ui, _| {
|
||||
Row::new().wrap(true).show(ui, |ui, _| {
|
||||
for (at, kind) in KINDS.into_iter().enumerate() {
|
||||
let rect = Button::new(kind).id_salt(kind).show(ui).rect;
|
||||
if at == 0 {
|
||||
first.set(rect);
|
||||
}
|
||||
last.set(rect);
|
||||
}
|
||||
});
|
||||
});
|
||||
if !ui.is_sizing_pass() {
|
||||
out.set((shown.output.rect, first.get(), last.get()));
|
||||
}
|
||||
});
|
||||
fixture.room(Vec2::new(1600.0, 600.0)).settle();
|
||||
seen.get()
|
||||
};
|
||||
let (plain, plain_first, plain_last) = shown(None);
|
||||
let (column, first, last) = shown(Some(WrapWidth::Column));
|
||||
assert!((plain_first.center().y - plain_last.center().y).abs() < 1.0, "without a wrap width the row keeps one line: {plain:?}");
|
||||
assert!(last.top() >= first.bottom(), "the row breaks onto a second line: {first:?} then {last:?}");
|
||||
assert!(column.width() < plain.width() * 0.75, "the wrap width keeps the stack narrow: {column:?} against {plain:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user