mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 08:44:24 +00:00
Bare tiles pack tight
A tile without a caption keeps only gap.tight around its picture, the room its selection ring draws in, and a grid of such tiles puts no gap between them. GlyphsPanel's zoom takes the old cell range, 24 to 64 pt, with a default near the old 32 pt.
This commit is contained in:
@@ -159,7 +159,7 @@ impl Outcome for GlyphsPanelOutput {
|
||||
/// How many recent glyphs are kept (Photoshop).
|
||||
const RECENT_LIMIT: usize = 25;
|
||||
/// The cell size of the grid and its zoom: factors of the medium thumbnail.
|
||||
pub const ZOOM: TileZoom = TileZoom::bounded(0.75, 2.0, 0.125, 1.0);
|
||||
pub const ZOOM: TileZoom = TileZoom::bounded(1.0, 2.625, 0.125, 1.25);
|
||||
/// The cells of the recents strip, as a factor of the medium thumbnail.
|
||||
const RECENT_ZOOM: f32 = 0.75;
|
||||
/// The room round a glyph in its cell, as a fraction of the cell.
|
||||
|
||||
@@ -139,13 +139,19 @@ pub struct TileGridStyle {
|
||||
thumbs: [f32; 3],
|
||||
/// What a tile adds around its picture.
|
||||
around: AroundPicture,
|
||||
/// The gaps of the arrangement and the height of a group header.
|
||||
/// The gaps of the arrangement and the height of a group header, for captioned tiles and for bare ones.
|
||||
spacing: ArrangeSpacing,
|
||||
bare_spacing: ArrangeSpacing,
|
||||
}
|
||||
|
||||
impl TileGridStyle {
|
||||
fn build(cx: &StyleCx<'_>, card: bool) -> TileGridStyle {
|
||||
let colors = &cx.tokens.color;
|
||||
let spacing = ArrangeSpacing::new(
|
||||
if card { cx.tokens.space.gap.section } else { cx.tokens.space.gap.stack },
|
||||
cx.tokens.space.gap.inline,
|
||||
cx.tokens.size.panel_header,
|
||||
);
|
||||
TileGridStyle {
|
||||
metrics: Metrics::new(),
|
||||
pinned: cx.color(colors.surface.panel),
|
||||
@@ -154,14 +160,17 @@ impl TileGridStyle {
|
||||
card: cx.tokens.size.thumb.card,
|
||||
thumbs: [cx.tokens.size.thumb.sm, cx.tokens.size.thumb.md, cx.tokens.size.thumb.lg],
|
||||
around: Tile::around(cx, card),
|
||||
spacing: ArrangeSpacing::new(
|
||||
if card { cx.tokens.space.gap.section } else { cx.tokens.space.gap.stack },
|
||||
cx.tokens.space.gap.inline,
|
||||
cx.tokens.size.panel_header,
|
||||
),
|
||||
spacing,
|
||||
// Bare tiles sit with no gap between them: the room around each picture keeps them apart.
|
||||
bare_spacing: if card { spacing } else { ArrangeSpacing { gap: 0.0, ..spacing } },
|
||||
}
|
||||
}
|
||||
|
||||
/// The spacing of a grid whose tiles have their caption in `place`.
|
||||
fn spacing(&self, place: CaptionPlace) -> ArrangeSpacing {
|
||||
if place == CaptionPlace::None { self.bare_spacing } else { self.spacing }
|
||||
}
|
||||
|
||||
/// The side of a picture at `zoom`, a factor on the thumbnail of `size`, or on the card thumbnail for `None`.
|
||||
fn picture_side(&self, zoom: f32, size: Option<ThumbSize>) -> f32 {
|
||||
size.map_or(self.card, |size| size.pick(self.thumbs)) * zoom.max(f32::EPSILON)
|
||||
@@ -386,7 +395,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> {
|
||||
/// the picture plus what the tile adds around it in every tiling.
|
||||
fn laid_out(&self, previous: Option<GridLayout>, source: &dyn ItemSource<K>, width: f32, item: Option<Vec2>, cx: &StyleCx<'_>) -> GridLayout {
|
||||
let style = TileGridStyle::build(cx, self.card);
|
||||
let spacing = style.spacing;
|
||||
let spacing = style.spacing(self.caption);
|
||||
let around = style.around.get(self.caption, self.detail_line);
|
||||
// A card never outgrows the grid: in a column narrower than a card the card takes the column.
|
||||
let mut side = self.picture_side(&style, item, around).min(width - 2.0 * spacing.inset - around.size().x).max(1.0);
|
||||
@@ -789,7 +798,8 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Collection for TileGrid<'_, K
|
||||
let card = style.card(self.picture_side(&style, item, around), aspect, around).x;
|
||||
let columns = self.card_columns as f32;
|
||||
// Half a point over, so the division that counts the columns back never rounds below `columns`.
|
||||
columns * card + (columns - 1.0) * style.spacing.gap + 2.0 * style.spacing.inset + 0.5
|
||||
let spacing = style.spacing(self.caption);
|
||||
columns * card + (columns - 1.0) * spacing.gap + 2.0 * spacing.inset + 0.5
|
||||
}
|
||||
|
||||
fn item_layout_from(
|
||||
|
||||
@@ -109,7 +109,6 @@ struct Sizes {
|
||||
start: f32,
|
||||
/// The picture width of a tile measured on its own: least, and ideal (a grid's card thumbnail at zoom 1).
|
||||
thumbs: [f32; 2],
|
||||
around: AroundPicture,
|
||||
}
|
||||
|
||||
/// What a tile adds around its picture for each caption arrangement, from the sizes: the grid sizes its cards with it.
|
||||
@@ -122,10 +121,8 @@ pub(super) struct AroundPicture {
|
||||
}
|
||||
|
||||
impl AroundPicture {
|
||||
fn of(sizes: &Sizes) -> AroundPicture {
|
||||
let around = |place: CaptionPlace, detail: bool| {
|
||||
Around::new(sizes.inset + sizes.column(place), sizes.inset, sizes.inset, sizes.inset + sizes.caption(place, detail))
|
||||
};
|
||||
fn of(cx: &StyleCx<'_>, card: bool) -> AroundPicture {
|
||||
let around = |place: CaptionPlace, detail: bool| Sizes::of(cx, card, place).around(place, detail);
|
||||
AroundPicture {
|
||||
below: around(CaptionPlace::Below, false),
|
||||
below_detail: around(CaptionPlace::Below, true),
|
||||
@@ -146,13 +143,20 @@ impl AroundPicture {
|
||||
}
|
||||
|
||||
impl Sizes {
|
||||
fn of(cx: &StyleCx<'_>, card: bool) -> Sizes {
|
||||
/// The sizes of a tile with its caption in `place`. A bare tile keeps only the room its selection ring draws in, so
|
||||
/// a grid packs bare tiles close.
|
||||
fn of(cx: &StyleCx<'_>, card: bool, place: CaptionPlace) -> Sizes {
|
||||
let (tokens, control) = (cx.tokens, cx.control());
|
||||
let line = tokens.typography.get(TypeRole::Caption).line_height;
|
||||
let name_line = if card { tokens.typography.get(TypeRole::BodyStrong).line_height } else { line };
|
||||
let sizes = Sizes {
|
||||
let inset = match (card, place) {
|
||||
(true, _) => tokens.space.pad.control,
|
||||
(false, CaptionPlace::None) => tokens.space.gap.tight,
|
||||
(false, _) => tokens.space.gap.inline,
|
||||
};
|
||||
Sizes {
|
||||
card,
|
||||
inset: if card { tokens.space.pad.control } else { tokens.space.gap.inline },
|
||||
inset,
|
||||
tight: tokens.space.gap.tight,
|
||||
bar: control.height,
|
||||
glyph: control.icon,
|
||||
@@ -164,9 +168,12 @@ impl Sizes {
|
||||
detail_line: line,
|
||||
start: control.height,
|
||||
thumbs: [tokens.size.thumb.sm, tokens.size.thumb.card],
|
||||
around: AroundPicture::default(),
|
||||
};
|
||||
Sizes { around: AroundPicture::of(&sizes), ..sizes }
|
||||
}
|
||||
}
|
||||
|
||||
/// What the tile adds around its picture with its caption in `place`, with a detail line reserved or not.
|
||||
fn around(&self, place: CaptionPlace, detail: bool) -> Around {
|
||||
Around::new(self.inset + self.column(place), self.inset, self.inset, self.inset + self.caption(place, detail))
|
||||
}
|
||||
|
||||
/// The height of the caption band under the picture.
|
||||
@@ -287,6 +294,7 @@ pub struct Tile<'a> {
|
||||
#[common]
|
||||
common: Common,
|
||||
/// Where the caption goes: the grid's, applied to its tiles.
|
||||
#[style_key]
|
||||
#[prop]
|
||||
caption: CaptionPlace,
|
||||
/// The card has a line for the detail, whether this tile has one or not: the grid's, applied to its tiles.
|
||||
@@ -446,12 +454,12 @@ impl<'a> Tile<'a> {
|
||||
/// What a tile adds around its picture: its padding, the caption band under the picture or the column at its
|
||||
/// reading start. A tile grid sizes a tile as the picture plus this.
|
||||
pub fn around_picture(cx: &StyleCx<'_>, place: CaptionPlace, detail: bool) -> Around {
|
||||
Self::around(cx, false).get(place, detail)
|
||||
Sizes::of(cx, false, place).around(place, detail)
|
||||
}
|
||||
|
||||
/// What a tile adds around its picture for every caption arrangement.
|
||||
pub(super) fn around(cx: &StyleCx<'_>, card: bool) -> AroundPicture {
|
||||
Sizes::of(cx, card).around
|
||||
AroundPicture::of(cx, card)
|
||||
}
|
||||
|
||||
/// Whether the caption band holds a detail line.
|
||||
@@ -619,7 +627,7 @@ impl Component for Tile<'_> {
|
||||
let (state, text) = (&colors.state, &colors.text);
|
||||
let clear = Rgba::TRANSPARENT;
|
||||
let c = |rgba: Rgba| cx.color(rgba);
|
||||
let sizes = Sizes::of(cx, key.card);
|
||||
let sizes = Sizes::of(cx, key.card, key.caption);
|
||||
// Selection and hover are rings around the picture; only a card has a fill of its own.
|
||||
let fill = if key.card {
|
||||
let raised = colors.surface.raised;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//! Content cells: a grid sizes its cards from what its tiles measure, and keeps that measure while nothing it reads
|
||||
//! changes.
|
||||
//! Cell sizes: a grid sizes content cells from what its tiles measure and keeps that measure while nothing it reads
|
||||
//! changes, and packs tiles without a caption closer than captioned ones.
|
||||
|
||||
use craft_behavior::{IconId, keyed};
|
||||
use craft_ui::contract::ShowCollection as _;
|
||||
@@ -39,3 +39,26 @@ fn content_cells_fit_the_icons_and_keep_their_measure() {
|
||||
cells.frame(vec![pointer::moved(over)]);
|
||||
assert!(widest_runs(cells.ctx()).is_empty(), "a pointer move measured the tiles again: {:?}", widest_runs(cells.ctx()));
|
||||
}
|
||||
|
||||
fn photo_grid(caption: CaptionPlace) -> Fixture {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let source = keyed(&KEYS, |key: &usize| *key);
|
||||
let grid = TileGrid::new("Photos").caption(caption).rows_stamp(1);
|
||||
let _ = grid.show_items(ui, &source, &mut |key, _| Tile::new(format!("Photo {key}")));
|
||||
});
|
||||
fixture.room(Vec2::new(600.0, 400.0));
|
||||
fixture.settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
/// Tiles without a caption pack closer than captioned tiles of the same picture size: neighbours in a row are nearer.
|
||||
#[trial]
|
||||
fn bare_tiles_pack_closer_than_captioned_tiles() {
|
||||
let pitch = |fixture: &Fixture| {
|
||||
let at = |key: usize| fixture.by_label(&format!("Photo {key}")).expect("the tile has a node").rect;
|
||||
assert!((at(0).top() - at(1).top()).abs() < 0.5, "the first two tiles share a row");
|
||||
(at(1).center().x - at(0).center().x).abs()
|
||||
};
|
||||
let (bare, captioned) = (photo_grid(CaptionPlace::None), photo_grid(CaptionPlace::Below));
|
||||
assert!(pitch(&bare) < pitch(&captioned), "bare tiles sit closer than captioned ones");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user