Export for Screens: an artboard's picture keeps its proportions in its square tile or row thumbnail (fit_rect), as production's did

This commit is contained in:
dotCooCoo
2026-11-24 18:36:15 -08:00
parent 9d050d2b01
commit 567dfccdfb
2 changed files with 41 additions and 2 deletions
@@ -43,7 +43,7 @@ use craft::ui::display::{Shade, Text};
use craft::ui::layout_components::{Row, Stack};
use craft::ui::list::{List, ListRow, ThumbAspect, ThumbSize, ToggleCell, ToggleState};
use craft::ui::patterns::form_dialogs::{FormDialog, PathRow};
use craft::ui::primitives::ContentPainterCx;
use craft::ui::primitives::{ContentPainterCx, fit_rect};
use craft::ui::runtime::env::Label;
use craft::ui::tabs::{Tab, TabKind, TabStrip};
use craft::ui::text::TextField;
@@ -601,8 +601,11 @@ impl ExportForScreensDialog {
let items = Items(doc.artboards.iter().map(|a| (u64::from(a.id), a.name.clone())).collect());
let caption = |at: usize| format!("{} {}", at + 1, items.0.get(at).map_or("", |(_, name)| name.as_str()));
let thumbs = &self.thumbs;
// The artboard keeps its proportions, centered in the tile or the row's thumbnail.
let paint = |at: usize, cx: &ContentPainterCx, rect: egui::Rect| match thumbs.get(at).cloned().flatten() {
Some(tex) => cx.picture(tex.id(), rect, egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)), 0.0),
Some(tex) => {
cx.picture(tex.id(), fit_rect(rect, tex.size_vec2()), egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)), 0.0)
}
None => {
cx.painter.rect_filled(rect, 0.0, egui::Color32::WHITE);
}
@@ -162,6 +162,42 @@ fn checked_artboards_read_as_a_range() {
}
}
/// The rect the frame `shapes` paint `texture` over; `None` when none paints it.
fn painted(shapes: &[egui::epaint::ClippedShape], texture: egui::TextureId) -> Option<egui::Rect> {
fn walk(shape: &egui::Shape, texture: egui::TextureId, out: &mut Option<egui::Rect>) {
match shape {
egui::Shape::Vec(all) => all.iter().for_each(|s| walk(s, texture, out)),
egui::Shape::Mesh(mesh) if mesh.texture_id == texture => {
let at = mesh.calc_bounds();
*out = Some(out.map_or(at, |r| r.union(at)));
}
_ => {}
}
}
let mut out = None;
shapes.iter().for_each(|clipped| walk(&clipped.shape, texture, &mut out));
out
}
/// The grid's tiles and the list's thumbnails are square; an artboard of another shape keeps its
/// proportions in them.
#[test]
fn an_artboards_picture_keeps_its_proportions() {
let (mut app, _) = app(1, false);
open(&mut app);
for view in ["grid", "list"] {
set(&mut app, "view", json!(view));
frame(&mut app);
let ctx = app.ui_ctx.clone().expect("a test app has a window");
let out = crate::testing::part_frame(&mut app, &ctx, Default::default(), |app, ui| app.ui(ui));
let tex = app.canvas.artboard_thumbs.as_ref().and_then(|(_, thumbs)| thumbs.first().cloned().flatten()).expect("the artboard has a picture");
let drawn = painted(&out.shapes, tex.id()).expect("the artboard's picture is drawn");
let (picture, shown) = (tex.size_vec2(), drawn.size());
assert!(picture.x > picture.y, "the 60×40 pt artboard is wider than high");
assert!((shown.x / shown.y - picture.x / picture.y).abs() < 0.02, "{view}: a {picture:?} picture is drawn {shown:?}");
}
}
#[test]
fn opens_with_the_artboards_the_artboard_tool_selected_and_exports_only_them() {
let (mut app, log) = app(4, true);