diff --git a/crates/font-manager/src/picker.rs b/crates/font-manager/src/picker.rs index 279f9a21a..bae9da499 100644 --- a/crates/font-manager/src/picker.rs +++ b/crates/font-manager/src/picker.rs @@ -18,7 +18,7 @@ use craft_ui::contract::{ Bind, Common, Component, Cx, Edit as _, EditResponse, Focus as _, Input, MeasureCx, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _, Props as _, Remember as _, Role, WordSetCx as _, }; -use craft_ui::buttons::IconButton; +use craft_ui::buttons::{ControlPlace, IconButton}; use craft_ui::list::{ThumbSize, TrailingPart}; use craft_ui::primitives::{IconSpec, TextKey, TextOptions, TextRun}; use craft_ui::runtime::env::{EnvView, Label}; @@ -588,8 +588,10 @@ impl Component for FontPicker<'_> { type Style = craft_ui::text::FieldStyle; type Output = FontPickerOutput; + /// A field; in a ribbon (`ControlPlace::Ribbon`) the small field length, as a ribbon's font box is. fn style(cx: &craft_ui::style::StyleCx<'_>, _: FontPickerStyleKey) -> craft_ui::text::FieldStyle { - craft_ui::text::FieldStyle::at(cx) + let field = craft_ui::text::FieldStyle::at(cx); + if cx.env::() == ControlPlace::Ribbon { field.small_length(cx) } else { field } } /// The wheel steps the family through the list. @@ -598,9 +600,12 @@ impl Component for FontPicker<'_> { } /// The width of the longest family names in common use, so the field keeps its size whichever family is picked. + /// In a ribbon the field's length alone sets its width (the style's small field length): a longer name scrolls + /// in the field, as in a word processor's font box. fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { let current = cx.value().map_or("", String::as_str); let widest = [NAME_SAMPLE, current].into_iter().map(|text| cx.field_text(&TextRun::new(text))).fold(Vec2::ZERO, Vec2::max); + let widest = if cx.env.get::() == ControlPlace::Ribbon { Vec2::new(0.0, widest.y) } else { widest }; let name = cx.words::().list.clone(); let note = self.missing_note(current, &cx.words::()); Vec2::new(widest.x + reserve(cx.style, &[], &Self::trailing(name, note.is_some())), widest.y) diff --git a/crates/menu/src/ribbon.rs b/crates/menu/src/ribbon.rs index 1619db64c..ee16e9c10 100644 --- a/crates/menu/src/ribbon.rs +++ b/crates/menu/src/ribbon.rs @@ -32,6 +32,7 @@ use egui::{Id, Pos2, Rect, Response, Ui, Vec2, vec2}; pub use craft_ui::buttons::GroupLevel; pub use gallery::{Gallery, GalleryFace, GalleryItem, GalleryMore, GalleryOutput}; pub use group::RibbonGroup; +use group::BodyHeight; pub use plan::{GroupWidths, plan}; craft_ui::words! { @@ -261,15 +262,24 @@ fn show_row( let distance = cx.style::().map_or(0.0, |style| style.slide); let lead = gap.clamp(-1.0, 1.0) * side * distance; let chevron = cx.style::().map_or(0.0, |style| style.chevron); + let least = cx.style::().map_or(0.0, |style| style.body); // The row moves through the slide container's own offset, which places its body on a layer of its own. let shown = Region::slide(lead) .show(ui, |ui, _| { + // The titles sit under the tallest content of the last pass; a change shows in the next frame. + let slot = base.with("ribbon.body"); + let last = with_session(ui.ctx(), slot, |kept: &mut BodyHeight| kept.0); let row = Row::new().scroll(Scroll::horizontal()).fill_width().show(ui, |ui, _| { - let mut row = RibbonRow { ui, base, groups: Vec::new() }; + let mut row = RibbonRow { ui, base, groups: Vec::new(), body: last.max(least), tallest: 0.0 }; body(&mut row, key); - row.groups + (row.groups, row.tallest) }); - (row.output.id, row.output.rect, row.inner.unwrap_or_default()) + let (groups, tallest) = row.inner.unwrap_or_default(); + if tallest > 0.0 && (tallest - last).abs() >= 0.5 { + with_session(ui.ctx(), slot, |kept: &mut BodyHeight| kept.0 = tallest); + ui.ctx().request_repaint(); + } + (row.output.id, row.output.rect, groups) }) .inner; if let Some((owner, view, groups)) = shown { @@ -350,6 +360,10 @@ pub struct RibbonRow<'a> { base: Id, /// The rect of each group this run of the row showed, in order: the units the edge chevrons step by. groups: Vec, + /// The height under each open group's top at which its title row sits: the tallest content of the last pass. + body: f32, + /// The tallest open group content of this run, every level the row measured included. + tallest: f32, } /// What the ribbon reports for a pass. @@ -386,6 +400,9 @@ struct RegionStyle { slide: f32, /// The width of an edge chevron: a hit target's. chevron: f32, + /// The least height of the group row's body, under which each group's title sits: three controls stacked with the + /// row gap between them, the ribbon's three rows of small buttons. Taller content lowers every title with it. + body: f32, } /// What a row region reports. @@ -443,7 +460,8 @@ impl Component for Region { type Output = RegionOutput; fn style(cx: &StyleCx<'_>, _: RegionStyleKey) -> RegionStyle { - RegionStyle { metrics: Metrics::new(), slide: cx.tokens.motion.slide as f32, chevron: cx.tokens.size.hit.target } + let body = 3.0 * cx.control().height + 2.0 * cx.tokens.space.gap.row; + RegionStyle { metrics: Metrics::new(), slide: cx.tokens.motion.slide as f32, chevron: cx.tokens.size.hit.target, body } } fn sense(&self) -> Sense { diff --git a/crates/menu/src/ribbon/gallery.rs b/crates/menu/src/ribbon/gallery.rs index ea0c60eb6..b7619224d 100644 --- a/crates/menu/src/ribbon/gallery.rs +++ b/crates/menu/src/ribbon/gallery.rs @@ -16,11 +16,11 @@ use craft_ui::contract::{ use craft_ui::display::{Picture, PictureSize}; use craft_ui::list::{ThumbAspect, ThumbSize}; use craft_ui::primitives::textures::{Revision, ThumbSource, Thumbnails}; -use craft_ui::primitives::{ContentPainterCx, PaintShape}; +use craft_ui::primitives::{ContentPainterCx, HAlign, PaintShape, TextRun}; use craft_ui::runtime::env::Label; use craft_ui::runtime::focus::KeyUse; -use craft_ui::style::{ComponentStyle, Flags, FocusLook, StyleColor, StyleCx}; -use craft_ui::tile_grid::{Tile, TileGrid}; +use craft_ui::style::{ComponentStyle, Flags, FocusLook, StyleColor, StyleCx, TypeRole}; +use craft_ui::tile_grid::{CaptionPlace, Tile, TileGrid}; use egui::{ColorImage, CornerRadius, Rect, Response, Ui, Vec2}; use super::GroupLevel; @@ -54,6 +54,8 @@ craft_ui::words! { /// - The up and down buttons (and the Up and Down keys on the strip) move the shown tiles by the count shown; the /// shown run never runs past the last full run, so no tile is empty, and it falls back when the items shrink. /// - More opens the whole list as a grid, runs a command, or opens the app's own body. +/// - Each tile's picture is [`aspect`](Gallery::aspect) times as wide as the medium thumbnail is high (square unless +/// set), and with `CaptionPlace::Below` the tile names its item under the picture (a style gallery's 'Normal'). #[must_use = "show the gallery with `show`"] #[derive(craft_ui::contract::Component, craft_ui::contract::Props)] #[component(overlay, probe(Gallery<'static, usize>))] @@ -65,6 +67,10 @@ pub struct Gallery<'a, K: Clone + Eq + Hash + Send + Sync + 'static> { full: u8, compact: u8, more: GalleryMore<'a>, + /// The width of each tile's picture as a multiple of its height. + aspect: ThumbAspect, + /// Where a tile shows its item's name: `Below` the picture, else nowhere. + caption: CaptionPlace, /// Each shown tile's rect. laid: Vec, picked: Option, @@ -94,6 +100,8 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> Gallery<'a, K> { full: 1, compact: 1, more: GalleryMore::Grid, + aspect: ThumbAspect::SQUARE, + caption: CaptionPlace::None, laid: Vec::new(), picked: None, menu: None, @@ -114,6 +122,25 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> Gallery<'a, K> { self } + /// The width of each tile's picture as a multiple of its height, the medium thumbnail's (`size.thumb.md`): a wide + /// sample (a paragraph style's first letters) fills a wide tile instead of shrinking into a square one. + pub fn aspect(mut self, aspect: ThumbAspect) -> Self { + self.aspect = aspect; + self + } + + /// Where each tile shows its item's name: `CaptionPlace::Below` names it under the picture, in the caption role; + /// `None` (the default) and `Start` show no name. The name stays each tile's accessible name either way. + pub fn caption(mut self, caption: CaptionPlace) -> Self { + self.caption = caption; + self + } + + /// Whether the tiles name their items under the pictures. + fn captioned(&self) -> bool { + self.caption == CaptionPlace::Below + } + /// The tiles the level shows at most. fn visible(&self, level: GroupLevel) -> usize { usize::from(if level == GroupLevel::Compact { self.compact } else { self.full }).max(1) @@ -134,7 +161,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> Gallery<'a, K> { fn sizes(&self, cx: &MeasureCx<'_, Self>) -> Sizes { let words = cx.env.words::(); let button = cx.face(IconButton::new(IconId::new("more")).label(words.more.clone())); - Sizes { tile: cx.face(GalleryTile::blank()), button, gap: cx.gap() } + Sizes { tile: cx.face(GalleryTile::blank(self.aspect, self.captioned())), button, gap: cx.gap() } } } @@ -172,28 +199,30 @@ pub enum GalleryFace<'a> { Paint(&'a dyn Fn(&ContentPainterCx, Rect)), } -/// The size every tile's picture has, so a tile measures the same with and without its picture. -fn thumb(picture: Picture<'_>) -> Picture<'_> { - picture.extent(PictureSize::Thumb(ThumbSize::Medium)) +/// The size every tile's picture has, `aspect` times as wide as high, so a tile measures the same with and without +/// its picture. +fn thumb(picture: Picture<'_>, aspect: ThumbAspect) -> Picture<'_> { + picture.extent(PictureSize::Thumb(ThumbSize::Medium)).aspect(aspect) } /// A picture that paints nothing, for measuring a tile. -fn blank_picture() -> Picture<'static> { - thumb(Picture::painter(Label::default(), ThumbAspect::SQUARE, |_: &ContentPainterCx<'_>, _: Rect| {})) +fn blank_picture(aspect: ThumbAspect) -> Picture<'static> { + thumb(Picture::painter(Label::default(), aspect, |_: &ContentPainterCx<'_>, _: Rect| {}), aspect) } impl<'a> GalleryFace<'a> { - /// The picture this face shows. It is decoration: the tile that holds it carries the name. - fn picture(&self) -> Picture<'a> { + /// The picture this face shows, `aspect` times as wide as high. It is decoration: the tile that holds it carries + /// the name. + fn picture(&self, aspect: ThumbAspect) -> Picture<'a> { let picture = match self { GalleryFace::Image { key, revision, image } => Picture::new(Label::default(), *key, *image).revision(*revision), GalleryFace::Rendered { key, thumbnails, source } => Picture::rendered(Label::default(), *key, *thumbnails, *source), GalleryFace::Paint(paint) => { let paint = *paint; - Picture::painter(Label::default(), ThumbAspect::SQUARE, move |cx: &ContentPainterCx<'_>, rect: Rect| paint(cx, rect)) + Picture::painter(Label::default(), aspect, move |cx: &ContentPainterCx<'_>, rect: Rect| paint(cx, rect)) } }; - thumb(picture) + thumb(picture, aspect) } } @@ -257,11 +286,11 @@ struct GridResult { /// The whole list as a grid of the same faces; a click or Enter picks, and a context request is forwarded. The /// grid sizes its columns from the room it is given. -fn grid(ui: &mut Ui, label: &Label, items: &[GalleryItem<'_, K>], current: Option) -> GridResult { +fn grid(ui: &mut Ui, label: &Label, items: &[GalleryItem<'_, K>], current: Option, aspect: ThumbAspect) -> GridResult { let selection = Selection::new(current.into_iter().collect(), current, current); let grid = TileGrid::::new(label.clone()).select(SelectMode::Single).selection(Bind::given(selection)); let out = grid.show_items(ui, &Indexes(items.len()), &mut |key, _| match items.get(*key) { - Some(item) => Tile::new(item.label.clone()).picture(ThumbAspect::SQUARE, item.face.picture()), + Some(item) => Tile::new(item.label.clone()).picture(aspect, item.face.picture(aspect)), None => Tile::new(Label::default()), }); let mut result = GridResult::default(); @@ -299,9 +328,16 @@ struct GalleryTileStyle { fill: StyleColor, /// The current tile's edge. edge: StyleColor, + /// The ink of the item's name under the picture, in a captioned gallery. + caption: StyleColor, + /// Between the picture and the caption. + gap: f32, + /// The picture's height: the medium thumbnail's (`size.thumb.md`), as `thumb` sizes it. + thumb: f32, } -/// The face a tile part wears: its picture inside the tile's selection look. +/// The face a tile part wears: its picture, and in a captioned gallery the item's name under it, inside the tile's +/// selection look. #[derive(craft_ui::contract::Component)] #[component(unregistered)] struct GalleryTile<'a> { @@ -311,20 +347,27 @@ struct GalleryTile<'a> { picture: Option>, /// Whether the tile is the gallery's current item. selected: bool, + /// The width of the picture as a multiple of its height. + aspect: ThumbAspect, + /// The item's name under the picture; `None` in a gallery without captions. + caption: Option