Merge pull request #3139 from storytold/fix/picture-no-checker

ui-widgets: a rendered picture can skip its transparency checkerboard (wordcraft#7317 C3-3)
This commit is contained in:
Martin Ball
2026-11-25 15:48:16 +00:00
committed by GitHub
3 changed files with 38 additions and 2 deletions
+6 -2
View File
@@ -86,6 +86,10 @@ pub struct Picture<'a> {
#[style_key]
#[prop]
extent: PictureSize,
/// Whether a rendered picture shows the transparency grid behind its transparent pixels (default on). Off for
/// a picture that is a mask on the surface it sits on, such as an equation.
#[prop]
checker: bool,
}
impl<'a> Picture<'a> {
@@ -117,7 +121,7 @@ impl<'a> Picture<'a> {
}
fn from_source(label: impl Into<Label>, source: Source<'a>) -> Self {
Picture { common: Common::new(), revision: Revision::default(), source, aspect: None, extent: PictureSize::Icon }.label(label)
Picture { common: Common::new(), revision: Revision::default(), source, aspect: None, extent: PictureSize::Icon, checker: true }.label(label)
}
/// The width of the picture's rect at `height`: the set aspect, else the SVG's own, else a square.
@@ -202,7 +206,7 @@ impl Component for Picture<'_> {
let rect = cx.content_rect;
let request = ThumbRequest::for_cell(rect.width().max(rect.height()), cx.ctx.pixels_per_point());
let state = thumbnails.state(&cx.ctx, *source, key, &request, Priority::VISIBLE);
thumbnails.paint(&ui.content_painter(cx), rect, &state, &ThumbPaint::default());
thumbnails.paint(&ui.content_painter(cx), rect, &state, &ThumbPaint::default().checker(self.checker));
}
Source::Svg(svg) => {
let ppp = cx.ctx.pixels_per_point();
@@ -82,3 +82,12 @@ fn picture_painter(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
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()
}
/// A rendered picture without the transparency grid, for a mask that sits on the surface.
#[story(Picture<'static>, covers = [], drive = Drive::Never)]
fn picture_rendered_without_checker(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
let options = ThumbnailOptions::new();
let thumbnails = Thumbnails::<u64>::in_context(ui.ctx(), "craft_ui::stories::picture_rendered_without_checker", options);
let source = FakeThumbSource::<u64>::new().later(true);
Picture::rendered("Mask", 1, &thumbnails, &source).extent(PictureSize::Thumb(ThumbSize::Large)).checker(false).show(ui).into()
}
+23
View File
@@ -138,3 +138,26 @@ fn a_fill_painter_picture_takes_the_room_it_is_given_at_its_aspect() {
assert!((size.x / size.y - 3.0).abs() < 0.05, "{size:?} keeps the 3:1 aspect");
}
}
/// How many shapes a rendered picture of a transparent image paints: as built, or opted out of its transparency grid.
fn shapes_of_rendered(opt_out: bool) -> usize {
use craft_ui::list::ThumbSize;
use craft_ui::primitives::textures::{FakeThumbSource, ThumbnailOptions, Thumbnails};
let mut fixture = Fixture::new(move |ui| {
let options = ThumbnailOptions::new().jobs(std::sync::Arc::new(craft_host::InlineJobs));
let thumbnails = Thumbnails::<u64>::in_context(ui.ctx(), "picture_checker_test", options);
let source = FakeThumbSource::<u64>::new();
let picture = Picture::rendered("Mask", 1, &thumbnails, &source).extent(PictureSize::Thumb(ThumbSize::Large));
// The default case sets nothing, so a default that turned the grid off fails here.
if opt_out { picture.checker(false) } else { picture }.show(ui);
});
fixture.settle();
fixture.frame_shapes().len()
}
#[trial]
fn a_rendered_picture_paints_a_checker_unless_it_opts_out() {
let with = shapes_of_rendered(false);
let without = shapes_of_rendered(true);
assert!(with > without, "the default paints {with} shapes, the opt-out {without}: the grid is the difference");
}