mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:27:49 +00:00
look-rest-2: WrapWidth::Column (size.dock.column) and Stack::wrap_width, a settings column beside a preview breaks its paragraphs instead of widening; trial and story
This commit is contained in:
@@ -226,6 +226,8 @@ impl LeastWidth {
|
||||
pub enum WrapWidth {
|
||||
/// `size.dialog.wrap`: a dialog's paragraphs.
|
||||
Dialog,
|
||||
/// `size.dock.column`: a page's column of settings beside a preview (a Print page), as wide as a dock column.
|
||||
Column,
|
||||
}
|
||||
|
||||
impl WrapWidth {
|
||||
@@ -233,6 +235,7 @@ impl WrapWidth {
|
||||
pub fn points(self, tokens: &craft_theme::Tokens) -> f32 {
|
||||
match self {
|
||||
WrapWidth::Dialog => tokens.size.dialog.wrap,
|
||||
WrapWidth::Column => tokens.size.dock.column,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
//! lays its children out (6.3).
|
||||
|
||||
use crate::contract::{
|
||||
Access, BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, FooterPlace, FooterRegion, Gap, HeaderRegion, LabelScheme, LeastWidth, Pad,
|
||||
Role, Scroll, Sizing, ThemeScope,
|
||||
Access, BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, FooterPlace, FooterRegion, Gap, HeaderRegion, LabelScheme, LeastWidth,
|
||||
Pad, Role, Scroll, Sizing, ThemeScope, WrapWidth,
|
||||
};
|
||||
use crate::layout::{BodyLayout, RowAlign};
|
||||
use crate::style::{ComponentStyle, StyleCx};
|
||||
@@ -269,6 +269,14 @@ impl<'a> Stack<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// The line length `wrap` of the paragraphs in a stack sized from its content: a wrapping text breaks its lines
|
||||
/// there instead of widening the stack (a Print page's settings column, `WrapWidth::Column`).
|
||||
#[must_use]
|
||||
pub fn wrap_width(mut self, wrap: WrapWidth) -> Self {
|
||||
self.props = self.props.wrap_width(wrap);
|
||||
self
|
||||
}
|
||||
|
||||
/// The padding `pad` on the header's two sides, none above or below it: a field over a table starts where the
|
||||
/// table's text does. Set it after [`header`](Stack::header).
|
||||
#[must_use]
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Stories of the layout components `Row`, `Stack` and `Grid`.
|
||||
|
||||
use super::{Grid, Row, Stack};
|
||||
use crate::contract::{Gap, Pad, Props as _, ShowContainer as _, Sizing};
|
||||
use crate::contract::{Gap, LeastWidth, Pad, Props as _, Show as _, ShowContainer as _, Sizing, WrapWidth};
|
||||
use crate::display::Text;
|
||||
use crate::story_kit::sample::{outlined, words};
|
||||
use crate::testing::{StoryData, StoryOutput, story};
|
||||
use craft_behavior::ColumnWidth;
|
||||
@@ -46,7 +47,29 @@ fn stack_fill_height(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
})
|
||||
}
|
||||
|
||||
static COLUMNS: [ColumnWidth; 2] = [ColumnWidth::Content, ColumnWidth::Fixed(60.0)];
|
||||
/// The two width roles of a content-sized stack: a side menu held at its least width with short items, and a column
|
||||
/// of settings whose paragraph breaks at the column's line length instead of widening it.
|
||||
#[story(Stack<'static>, covers = [], title = "Width roles")]
|
||||
fn stack_width_roles(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
outlined(ui, |ui| {
|
||||
Row::new()
|
||||
.gap(Gap::Section)
|
||||
.show(ui, |ui, _| {
|
||||
Stack::new().least_width(LeastWidth::SideMenu).gap(Gap::Tight).pad(Pad::Regular).show(ui, |ui, _| words(ui, 0..2));
|
||||
Stack::new().wrap_width(WrapWidth::Column).gap(Gap::Stack).pad(Pad::Regular).show(ui, |ui, _| {
|
||||
let _ = Text::new(
|
||||
"Save a copy in another format. Printing goes through a PDF you can print from any viewer, and the pages \
|
||||
picked here are the ones that print.",
|
||||
)
|
||||
.show(ui);
|
||||
words(ui, 2..3);
|
||||
});
|
||||
})
|
||||
.into()
|
||||
})
|
||||
}
|
||||
|
||||
static COLUMNS: [ColumnWidth; 2] =[ColumnWidth::Content, ColumnWidth::Fixed(60.0)];
|
||||
|
||||
#[story(Grid, covers = [Default])]
|
||||
fn grid(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
|
||||
@@ -13,4 +13,5 @@ mod rtl_scroll;
|
||||
mod scroll_region;
|
||||
mod shrink_order;
|
||||
mod stack_header;
|
||||
mod stack_widths;
|
||||
mod zero_width;
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
//! 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`).
|
||||
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{LeastWidth, Show as _, ShowContainer as _, WrapWidth};
|
||||
use craft_ui::display::Text;
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Rect, Vec2};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A paragraph far longer than any column.
|
||||
const LONG: &str = "Save a copy in another format. Printing goes through a PDF you can print from any viewer, and the pages you pick \
|
||||
here are the ones that print, at the size and orientation set below.";
|
||||
|
||||
/// The rects of the stack and its one child from the last real run, the stack built by `stack`.
|
||||
fn shown(stack: fn() -> Stack<'static>, long: bool) -> (Rect, Rect) {
|
||||
let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING)));
|
||||
let out = Rc::clone(&seen);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let child = Cell::new(Rect::NOTHING);
|
||||
let shown = stack().show(ui, |ui, _| {
|
||||
let rect = if long { Text::new(LONG).show(ui).rect } else { Button::new("Home").show(ui).rect };
|
||||
child.set(rect);
|
||||
});
|
||||
if !ui.is_sizing_pass() {
|
||||
out.set((shown.output.rect, child.get()));
|
||||
}
|
||||
});
|
||||
fixture.room(Vec2::new(1600.0, 600.0)).settle();
|
||||
seen.get()
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_stacks_least_width_holds_it_wider_than_narrow_content() {
|
||||
let (plain, button) = shown(Stack::new, false);
|
||||
let (menu, _) = shown(|| Stack::new().least_width(LeastWidth::SideMenu), false);
|
||||
assert!(plain != Rect::NOTHING && menu != Rect::NOTHING, "both stacks showed");
|
||||
assert!(plain.width() < button.width() + 2.0, "a plain stack is as wide as its button: {plain:?} for {button:?}");
|
||||
assert!(menu.width() > plain.width() + 60.0, "the side menu's least width holds it wider: {menu:?} against {plain:?}");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_stacks_wrap_width_breaks_a_long_paragraph_at_the_column() {
|
||||
let (plain, one_line) = shown(Stack::new, true);
|
||||
let (column, wrapped) = shown(|| Stack::new().wrap_width(WrapWidth::Column), true);
|
||||
assert!(plain.width() > 600.0, "without a wrap width the paragraph widens the stack to the room: {plain:?}");
|
||||
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:?}");
|
||||
}
|
||||
Reference in New Issue
Block a user