diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index f8513467c..984a806e8 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -200,6 +200,12 @@ pub enum LeastWidth { DialogSideButton, /// `size.rail.width` per column: a tool rail of this many columns, as wide as the shell slot that holds it. Rail(std::num::NonZeroU8), + /// `size.dialog.least` and `size.dialog.wrap` together: a wide dialog, as wide as a dialog's least content beside a + /// paragraph's line (an About dialog whose tabs hold credit tables). + WideDialog, + /// Five `size.rail.width`: a page's side menu (a File page's list of pages), wide enough that its rows read as a + /// band beside the page, not as buttons cut to their labels. + SideMenu, } impl LeastWidth { @@ -211,6 +217,8 @@ impl LeastWidth { LeastWidth::DialogButton => tokens.size.dialog.button, LeastWidth::DialogSideButton => tokens.size.dialog.side_button, LeastWidth::Rail(columns) => tokens.size.rail.width * f32::from(columns.get()), + LeastWidth::WideDialog => tokens.size.dialog.least + tokens.size.dialog.wrap, + LeastWidth::SideMenu => tokens.size.rail.width * 5.0, } } } @@ -222,6 +230,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 { @@ -229,6 +239,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, } } } diff --git a/crates/ui-panels/src/dialog/modal.rs b/crates/ui-panels/src/dialog/modal.rs index c7af4e7db..6e63d48e2 100644 --- a/crates/ui-panels/src/dialog/modal.rs +++ b/crates/ui-panels/src/dialog/modal.rs @@ -245,6 +245,14 @@ impl<'a> Dialog<'a> { self } + /// The least width of the dialog's content from the size role `least`, in place of `size.dialog.least`: a wide + /// dialog (`LeastWidth::WideDialog`) whose tabs hold tables. + #[must_use] + pub fn least_width(mut self, least: crate::contract::LeastWidth) -> Self { + self.props = self.props.least_width(least); + self + } + /// The dialog reports the alert-dialog role instead of the dialog role. #[must_use] pub(super) fn alert(mut self) -> Self { diff --git a/crates/ui-patterns/src/patterns/form_dialogs/about.rs b/crates/ui-patterns/src/patterns/form_dialogs/about.rs index f035242f8..49b4beb8e 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/about.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/about.rs @@ -13,8 +13,8 @@ use crate::buttons::{Button, IconButton, Link}; use crate::choice::Segmented; use crate::contract::Gap; use crate::contract::{ - Bind, CollectionEvent, ColumnEvent, ColumnSort, ColumnSpec, Entry, NoValue, Outcome, Props as _, Remember as _, Scroll, SelectMode, Show as _, - ShowCollection as _, ShowContainer as _, clicked, + Bind, CollectionEvent, ColumnEvent, ColumnSort, ColumnSpec, Entry, LeastWidth, NoValue, Outcome, Props as _, Remember as _, Scroll, SelectMode, + Show as _, ShowCollection as _, ShowContainer as _, clicked, }; use crate::dialog::{Dialog, DialogId, DialogOutput, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id}; use crate::display::{Divider, Shade, Text}; @@ -298,6 +298,10 @@ impl<'a> AboutDialog<'a> { if let Some(open) = open { dialog = dialog.opened(open); } + // The listed layout is a wide dialog, as WordCraft's About, whose tabs hold the credit tables. + if layout == AboutLayout::Listed { + dialog = dialog.least_width(LeastWidth::WideDialog); + } let shown_dialog = dialog.show(ui, |ui, cx| { let mut kept = if cx.appeared() { View::default() } else { cx.memory::().clone() }; // A bound tab is the caller's each pass, so a tab set from outside shows on the next pass. diff --git a/crates/ui-patterns/tests/it/form_dialogs/about.rs b/crates/ui-patterns/tests/it/form_dialogs/about.rs index 302674a9e..ab6909f04 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/about.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/about.rs @@ -71,6 +71,22 @@ fn the_listed_about_tab_stands_one_item_to_a_line_and_closes_with_close() { assert_eq!(links.borrow().as_slice(), ["https://example.org".to_owned()]); } +/// The listed layout is a wide dialog (`LeastWidth::WideDialog`), as WordCraft's About: wider than the centred one +/// with the same text. +#[trial] +fn the_listed_about_is_a_wide_dialog() { + let width = |layout: AboutLayout| { + let mut fixture = Fixture::new(move |ui| { + let info = AboutInfo::new("PhotoCraft", "An image editor.", "1.0"); + let _ = AboutDialog::new(&info, &CREDITS).layout(layout).show(ui); + }); + fixture.room(Vec2::new(1400.0, 1000.0)).idle(30); + fixture.by_role(Role::DIALOG).first().expect("the dialog shows").rect.width() + }; + let (listed, centred) = (width(AboutLayout::Listed), width(AboutLayout::Centred)); + assert!(listed > centred + 200.0, "the listed About is a wide dialog: {listed} against {centred}"); +} + #[trial] fn about_tab_is_a_field() { let host = Rc::new(RefCell::new(DialogHost::new())); diff --git a/crates/ui-widgets/src/buttons/button.rs b/crates/ui-widgets/src/buttons/button.rs index 9ceb00cf5..2b2ef0299 100644 --- a/crates/ui-widgets/src/buttons/button.rs +++ b/crates/ui-widgets/src/buttons/button.rs @@ -10,7 +10,7 @@ use crate::primitives::{HAlign, IconSpec, PaintShape}; use crate::runtime::commands::CommandRef; use crate::runtime::env::Label; use crate::style::{ComponentStyle, DataColor, FocusLook, IconLook, StyleColor, StyleCx, SurfaceLook, TextLook, TypeRole}; -use craft_behavior::{Metrics, PartKey, PressPhase, Sense}; +use craft_behavior::{Insets, Metrics, PartKey, PressPhase, Sense}; use craft_keymap::Mods; use craft_theme::{ Rgba, Stateful, Variant, @@ -212,17 +212,17 @@ impl<'a> Button<'a> { self } - /// The face as it shows `label`: the painted text in its place when set. - fn spec<'s>(&'s self, label: &'s str) -> FaceSpec<'s> { - self.face_of(self.painted.as_ref().map_or(label, Label::as_str)) + /// The face as it shows `label` in `style`: the painted text in its place when set. + fn spec<'s>(&'s self, label: &'s str, style: &ButtonStyle) -> FaceSpec<'s> { + self.face_of(self.painted.as_ref().map_or(label, Label::as_str), style) } - /// The face showing `label` as it is. - fn face_of<'s>(&'s self, label: &'s str) -> FaceSpec<'s> { + /// The face showing `label` as it is: in the label role, unless `style` names another (a side menu's row). + fn face_of<'s>(&'s self, label: &'s str, style: &ButtonStyle) -> FaceSpec<'s> { FaceSpec { arrangement: self.arrangement, label, - role: None, + role: (style.text.role != TypeRole::Label).then_some(style.text.role), lead: self.icon.as_ref(), picture: self.picture.as_ref().map(|(aspect, _)| *aspect), trail: self.trailing_icon.as_ref(), @@ -251,13 +251,19 @@ impl Component for Button<'_> { if let Some(least) = key.least_width { style.metrics.min_size.x = style.metrics.min_size.x.max(least.points(cx.tokens)); } + // A side menu's row: the large body text, padded as a list row of places, not as a control. + if cx.env::() == super::ControlPlace::SideMenu && key.arrangement == Arrangement::Inline { + let pad = &cx.tokens.space.pad; + style.text.role = TypeRole::BodyLarge; + style.metrics.padding = Insets::new(pad.roomy, pad.roomy, pad.segment, pad.segment); + } style } fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { - let shown = face::measure(cx, &self.spec(cx.resolved.label)); + let shown = face::measure(cx, &self.spec(cx.resolved.label, cx.style)); match &self.room_for { - Some(room) => shown.max(face::measure(cx, &self.face_of(room.as_str()))), + Some(room) => shown.max(face::measure(cx, &self.face_of(room.as_str(), cx.style))), None => shown, } } @@ -271,12 +277,12 @@ impl Component for Button<'_> { fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) { let (area, rtl) = (cx.content_rect, cx.flow.is_rtl()); if self.loading { - let spinner = face::spinner_rect(cx, &self.spec(cx.resolved.label), &cx.style.face, cx.style.icon.side, area, rtl); + let spinner = face::spinner_rect(cx, &self.spec(cx.resolved.label, cx.style),&cx.style.face, cx.style.icon.side, area, rtl); if let Some(rect) = spinner { cx.part(SPINNER, rect); } } else if self.picture.is_some() - && let Some(rect) = face::lead_rect(cx, &self.spec(cx.resolved.label), &cx.style.face, cx.style.icon.side, area, rtl) + && let Some(rect) = face::lead_rect(cx, &self.spec(cx.resolved.label, cx.style),&cx.style.face, cx.style.icon.side, area, rtl) { cx.part(PICTURE, rect); } @@ -300,7 +306,7 @@ impl Component for Button<'_> { ui.draw().data_fill(&shape, DataColor::new(Color32::from(craft_theme::Srgba::from(tint)))); ui.draw().fill(&shape, cx.resolve(&cx.style.overlay)); } - face::paint(ui, cx, (&cx.style.text, &cx.style.icon), &cx.style.face, &self.spec(cx.resolved.label), cx.content_rect); + face::paint(ui, cx, (&cx.style.text, &cx.style.icon), &cx.style.face, &self.spec(cx.resolved.label, cx.style), cx.content_rect); if cx.part_if_declared(SPINNER).is_some() { ui.face(SPINNER, Spinner::new().size(cx.resolved.size)); } diff --git a/crates/ui-widgets/src/buttons/mod.rs b/crates/ui-widgets/src/buttons/mod.rs index e1963cd1b..84baafd34 100644 --- a/crates/ui-widgets/src/buttons/mod.rs +++ b/crates/ui-widgets/src/buttons/mod.rs @@ -43,6 +43,10 @@ pub enum GroupLevel { /// its corner round; /// - a `ComboBox` is as wide as its widest option and its chevron, not a field length, and a font picker is a short /// field (`size.length.short`) whichever family it shows. +/// +/// A page's side menu (a File page's list of pages) sets `SideMenu` for its rows: a `Button` there shows its label in +/// the large body role (`TypeRole::BodyLarge`), with `space.pad.segment` above and below it and `space.pad.roomy` +/// at its sides, so the menu reads as a list of places rather than a row of controls. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, EnvValue)] #[env(style)] #[non_exhaustive] @@ -52,6 +56,8 @@ pub enum ControlPlace { Standard, /// In a ribbon group. Ribbon, + /// In a page's side menu. + SideMenu, } /// The style context a push or split button draws in: in a ribbon (`ControlPlace::Ribbon`) the default variant diff --git a/crates/ui-widgets/src/buttons/stories.rs b/crates/ui-widgets/src/buttons/stories.rs index d06410b12..1688e9bfb 100644 --- a/crates/ui-widgets/src/buttons/stories.rs +++ b/crates/ui-widgets/src/buttons/stories.rs @@ -1,15 +1,15 @@ //! Stories of the buttons. -use super::{Arrangement, Button, GroupLevel, IconButton, Link, SplitButton, SplitMenu, ToggleButton}; -use crate::contract::{Bind, BodyCx, Entry, Props as _, Show as _, ShowContainer as _, Validity}; +use super::{Arrangement, Button, ControlPlace, GroupLevel, IconButton, Link, SplitButton, SplitMenu, ToggleButton}; +use crate::contract::{Bind, BodyCx, Entry, LeastWidth, Props as _, Show as _, ShowContainer as _, Sizing, Validity}; use crate::display::{Picture, Shade, Text}; -use crate::layout_components::{Grid, Row}; +use crate::layout_components::{Grid, Row, Stack}; use crate::list::ThumbAspect; use crate::menu::MenuItem; -use crate::primitives::{AppIcons, IconMark, IconSpec, register_app_icons}; +use crate::primitives::{AppIcons, HAlign, IconMark, IconSpec, register_app_icons}; use crate::runtime::commands::EditCommands; use crate::runtime::env::{Message, with_env}; -use crate::style::DataColor; +use crate::style::{DataColor, SurfaceKind}; use crate::testing::pictures::sample_picture; use crate::testing::{Drive, Fixture, StoryData, StoryOutput, story}; use craft_behavior::{ColumnWidth, IconId, Mixed, Proposal}; @@ -213,6 +213,32 @@ fn button_compact_in_group(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { shown.into() } +/// A page's side menu: full-width ghost buttons on the brand surface, in the `SideMenu` place, at least the side +/// menu's width, one of them current, as a File page's list of pages shows them. +#[story(Button<'static>, covers = [], drive = Drive::Never, title = "In a side menu")] +fn button_in_side_menu(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let shown = Stack::new().surface(SurfaceKind::Brand).least_width(LeastWidth::SideMenu).show(ui, |ui, _| { + with_env( + ui, + |overrides| { + overrides.values.set(ControlPlace::SideMenu); + }, + |ui| { + for (name, icon) in [("Home", IconId::new("home")), ("Open", IconId::new("open")), ("Print", IconId::new("print"))] { + Button::new(name) + .icon(icon) + .variant(Variant::GHOST) + .sizing(Sizing::FillWidth) + .align(HAlign::Start) + .selected(name == "Open") + .show(ui); + } + }, + ); + }); + shown.into() +} + /// One toggle off and one on with an icon, as the gallery card shows them. #[story(ToggleButton<'static>, covers = [], drive = Drive::Never)] fn toggle_button_on(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { diff --git a/crates/ui-widgets/src/layout_components/mod.rs b/crates/ui-widgets/src/layout_components/mod.rs index fe7b9fae5..5326396a0 100644 --- a/crates/ui-widgets/src/layout_components/mod.rs +++ b/crates/ui-widgets/src/layout_components/mod.rs @@ -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, 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}; @@ -261,6 +261,22 @@ impl<'a> Stack<'a> { self } + /// The least width of the stack's content from the size role `least`: a stack sized from its content is at least + /// that wide (a page's side menu, `LeastWidth::SideMenu`), and wider content still widens it. + #[must_use] + pub fn least_width(mut self, least: LeastWidth) -> Self { + self.props = self.props.least_width(least); + 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] diff --git a/crates/ui-widgets/src/layout_components/stories.rs b/crates/ui-widgets/src/layout_components/stories.rs index 83a05fb5d..ac6d6ceee 100644 --- a/crates/ui-widgets/src/layout_components/stories.rs +++ b/crates/ui-widgets/src/layout_components/stories.rs @@ -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 { diff --git a/crates/ui-widgets/tests/it/buttons.rs b/crates/ui-widgets/tests/it/buttons.rs index e3f5fdf5e..af1461161 100644 --- a/crates/ui-widgets/tests/it/buttons.rs +++ b/crates/ui-widgets/tests/it/buttons.rs @@ -467,3 +467,35 @@ fn a_ribbons_combo_box_is_as_wide_as_its_options() { let (ribbon, standard) = (width(true), width(false)); assert!(ribbon < standard / 2.0, "the ribbon's combo box fits two digits and its chevron: {ribbon} against {standard}"); } + +/// A page's side menu (`ControlPlace::SideMenu`) shows its buttons as rows of places: the label in the large body role +/// and padded above and below, so a row is taller and wider than the same ghost button elsewhere. A stacked button +/// there keeps its own look. +#[trial] +fn a_side_menus_buttons_are_taller_rows_with_the_large_body_label() { + let rect = |side: bool, arrangement: Arrangement| { + let mut fixture = Fixture::new(move |ui| { + let show = |ui: &mut Ui| { + let _ = Button::new("Print").icon(IconId::new("print")).variant(Variant::GHOST).arrangement(arrangement).show(ui); + }; + if side { + with_env( + ui, + |overrides| { + overrides.values.set(ControlPlace::SideMenu); + }, + show, + ); + } else { + show(ui); + } + }); + fixture.settle(); + fixture.by_label("Print").expect("the button").rect + }; + let (menu, plain) = (rect(true, Arrangement::Inline), rect(false, Arrangement::Inline)); + assert!(menu.height() > plain.height() + 8.0, "a side menu's row is padded as a list row: {} against {}", menu.height(), plain.height()); + assert!(menu.width() > plain.width() + 4.0, "its label is in the larger role: {} against {}", menu.width(), plain.width()); + let (menu, plain) = (rect(true, Arrangement::Stacked), rect(false, Arrangement::Stacked)); + assert_eq!(menu.size(), plain.size(), "a stacked button keeps its own look in a side menu"); +} diff --git a/crates/ui-widgets/tests/it/layout/mod.rs b/crates/ui-widgets/tests/it/layout/mod.rs index ed6c32e16..345f8b837 100644 --- a/crates/ui-widgets/tests/it/layout/mod.rs +++ b/crates/ui-widgets/tests/it/layout/mod.rs @@ -13,4 +13,5 @@ mod rtl_scroll; mod scroll_region; mod shrink_order; mod stack_header; +mod stack_widths; mod zero_width; diff --git a/crates/ui-widgets/tests/it/layout/stack_widths.rs b/crates/ui-widgets/tests/it/layout/stack_widths.rs new file mode 100644 index 000000000..7a910b976 --- /dev/null +++ b/crates/ui-widgets/tests/it/layout/stack_widths.rs @@ -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:?}"); +}