ui-widgets: a painter picture can fill its slot (wordcraft#7317 C3-1) (#3131)

* ui-widgets: painter pictures take an explicit size (wordcraft#7317 C3-1)

Add PictureSize::Points(u16) and make Picture::painter take a PictureSize,
so a painter no longer falls back silently to the 18 pt icon side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE

* ui-widgets: PictureSize doc names the points variant

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE

* Picture: PictureSize::Fill takes the room its parent gives, replacing Points(u16)

A painter picture can fill a dialog's preview slot at its aspect, with no raw
point size (design-lint props/point-size). painter() keeps its original
signature; callers opt in with .extent(PictureSize::Fill). measure reports the
offered room so a Fill picture never overflows a narrower one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE

* Picture: a Fill picture in an unbounded room takes the icon side's height

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Ball
2026-11-23 17:41:18 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent e3ca63165d
commit 126ec28dca
4 changed files with 54 additions and 3 deletions
+11 -2
View File
@@ -14,7 +14,7 @@ use egui::{ColorImage, Id, Rect, Response, Vec2};
#[cfg(feature = "suite")] #[cfg(feature = "suite")]
pub(crate) mod stories; pub(crate) mod stories;
/// How large a picture is: its height, from a token. /// How large a picture is: its height, from a token, or the room its parent gives.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive] #[non_exhaustive]
pub enum PictureSize { pub enum PictureSize {
@@ -29,6 +29,9 @@ pub enum PictureSize {
/// The image at its own size in device pixels, one texel to a pixel (a bitmap drawn for the screen's density). /// The image at its own size in device pixels, one texel to a pixel (a bitmap drawn for the screen's density).
/// A painter or a picture made off the frame has no own size and takes the icon side. /// A painter or a picture made off the frame has no own size and takes the icon side.
DevicePixels, DevicePixels,
/// The full width of the room the parent gives, as high as the picture's `aspect` makes it: for a preview that
/// fills a dialog's slot. With no room offered it takes the icon side as its height.
Fill,
} }
/// The style of a picture. /// The style of a picture.
@@ -118,7 +121,7 @@ impl Component for Picture<'_> {
fn style(cx: &StyleCx<'_>, key: PictureStyleKey) -> PictureStyle { fn style(cx: &StyleCx<'_>, key: PictureStyleKey) -> PictureStyle {
let thumbs = &cx.tokens.size.thumb; let thumbs = &cx.tokens.size.thumb;
let height = match key.extent { let height = match key.extent {
PictureSize::Icon | PictureSize::DevicePixels => cx.control().icon, PictureSize::Icon | PictureSize::DevicePixels | PictureSize::Fill => cx.control().icon,
PictureSize::Thumb(step) => step.pick([thumbs.sm, thumbs.md, thumbs.lg]), PictureSize::Thumb(step) => step.pick([thumbs.sm, thumbs.md, thumbs.lg]),
PictureSize::Sample(step) => step.pick([thumbs.sample.sm, thumbs.sample.md, thumbs.sample.lg]), PictureSize::Sample(step) => step.pick([thumbs.sample.sm, thumbs.sample.md, thumbs.sample.lg]),
}; };
@@ -133,6 +136,12 @@ impl Component for Picture<'_> {
let texture = store(ctx).get_or_put(ctx, (cx.resolved.id, *key), self.revision, *image); let texture = store(ctx).get_or_put(ctx, (cx.resolved.id, *key), self.revision, *image);
return texture.size_vec2() / ctx.pixels_per_point(); return texture.size_vec2() / ctx.pixels_per_point();
} }
if self.extent == PictureSize::Fill {
// The room less the padding the pipeline adds, so the picture shrinks to a narrower room and never overflows it.
// An unbounded room (a horizontal row or scroll) gives no width to fill: the icon side then sets the height.
let width = cx.room.filter(|room| room.is_finite()).map_or_else(|| self.aspect.of(cx.style.height), |room| room.max(0.0));
return Vec2::new(width, self.aspect.high(width));
}
let height = cx.style.height; let height = cx.style.height;
Vec2::new(self.aspect.of(height), height) Vec2::new(self.aspect.of(height), height)
} }
@@ -45,3 +45,9 @@ fn picture_rendered(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
fn picture_painter(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { fn picture_painter(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
Picture::painter("Swatch", ThumbAspect::new(4, 1), |cx, rect| cx.checker(rect)).extent(PictureSize::Thumb(ThumbSize::Large)).show(ui).into() Picture::painter("Swatch", ThumbAspect::new(4, 1), |cx, rect| cx.checker(rect)).extent(PictureSize::Thumb(ThumbSize::Large)).show(ui).into()
} }
/// A painter preview that fills the room its parent gives, 4 wide to 1 high.
#[story(Picture<'static>, covers = [], drive = Drive::Never)]
fn picture_fill(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
Picture::painter("Preview", ThumbAspect::new(4, 1), |cx, rect| cx.checker(rect)).extent(PictureSize::Fill).show(ui).into()
}
+4
View File
@@ -25,6 +25,10 @@ impl ThumbAspect {
Self((f32::from(wide) / f32::from(high.max(1))).max(f32::EPSILON)) Self((f32::from(wide) / f32::from(high.max(1))).max(f32::EPSILON))
} }
pub(crate) fn high(self, width: f32) -> f32 {
width / self.0
}
pub(crate) fn of(self, height: f32) -> f32 { pub(crate) fn of(self, height: f32) -> f32 {
self.0 * height self.0 * height
} }
+33 -1
View File
@@ -1,7 +1,8 @@
//! `Picture` sized in device pixels. //! `Picture` sized in device pixels.
use craft_ui::contract::Show as _; use craft_ui::contract::{Props as _, Show as _};
use craft_ui::display::{Picture, PictureSize}; use craft_ui::display::{Picture, PictureSize};
use craft_ui::list::ThumbAspect;
use craft_ui::runtime::env::Scale; use craft_ui::runtime::env::Scale;
use craft_ui::testing::{Fixture, trial}; use craft_ui::testing::{Fixture, trial};
use egui::{Color32, ColorImage, Vec2}; use egui::{Color32, ColorImage, Vec2};
@@ -34,3 +35,34 @@ fn a_device_pixel_picture_keeps_one_texel_to_a_pixel_at_every_scale() {
); );
} }
} }
/// A `Fill` painter picture takes the room its parent gives: wider in a wider room, never wider than the room, and
/// as high as its aspect makes it at that width.
#[trial]
fn a_fill_painter_picture_takes_the_room_it_is_given_at_its_aspect() {
let rooms = [120.0_f32, 300.0];
let seen = Rc::new(Cell::new([Vec2::ZERO; 2]));
let out = seen.clone();
let mut fixture = Fixture::new(move |ui| {
let mut sizes = [Vec2::ZERO; 2];
for (size, room) in sizes.iter_mut().zip(rooms) {
ui.allocate_ui(Vec2::new(room, 400.0), |ui| {
ui.set_max_width(room);
*size = Picture::painter("Preview", ThumbAspect::new(3, 1), |_, _| ())
.extent(PictureSize::Fill)
.id_salt(room.to_bits())
.show(ui)
.rect
.size();
});
}
out.set(sizes);
});
fixture.settle();
let [narrow, wide] = seen.get();
assert!(wide.x > narrow.x + 1.0, "a wider room gives a wider picture: {wide:?} against {narrow:?}");
for (size, room) in [narrow, wide].into_iter().zip(rooms) {
assert!(size.x <= room + 0.01, "{size:?} fits a room {room} wide");
assert!((size.x / size.y - 3.0).abs() < 0.05, "{size:?} keeps the 3:1 aspect");
}
}