mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
Merge remote-tracking branch 'origin/next' into sync/next2
This commit is contained in:
@@ -14,7 +14,7 @@ use egui::{ColorImage, Id, Rect, Response, Vec2};
|
||||
#[cfg(feature = "suite")]
|
||||
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)]
|
||||
#[non_exhaustive]
|
||||
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).
|
||||
/// A painter or a picture made off the frame has no own size and takes the icon side.
|
||||
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.
|
||||
@@ -118,7 +121,7 @@ impl Component for Picture<'_> {
|
||||
fn style(cx: &StyleCx<'_>, key: PictureStyleKey) -> PictureStyle {
|
||||
let thumbs = &cx.tokens.size.thumb;
|
||||
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::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);
|
||||
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;
|
||||
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 {
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -25,6 +25,10 @@ impl ThumbAspect {
|
||||
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 {
|
||||
self.0 * height
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! `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::list::ThumbAspect;
|
||||
use craft_ui::runtime::env::Scale;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user