Merge pull request #8463 from dotCooCoo/feat/document-setup-grid-preview

Document Setup: the Transparency tab previews the grid
This commit is contained in:
BFlat
2026-11-19 00:42:49 +07:00
committed by GitHub
4 changed files with 237 additions and 18 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ use fill::{fill_view, polygon_area};
#[cfg(test)]
use grid::ruler_rects;
use grid::{LineLook, PIXEL_GRID, PIXEL_GRID_ZOOM, checker, clip_segment, grid, ruler_input, rulers, rulers_hidden, textured_quad, view_bounds};
pub(crate) use grid::{RulerScale, checker_texture, ruler_label, ruler_origin_dragging};
pub(crate) use grid::{RulerScale, checker_texture, checker_tile, ruler_label, ruler_origin_dragging};
#[cfg(test)]
pub(crate) use grid::{TRANSPARENCY_GRID, vertical_label_height};
use home::home;
+15 -4
View File
@@ -11,20 +11,31 @@ pub(crate) fn checker_texture(
setup: &vectorcraft_doc::DocSetup,
display: Option<&vectorcraft_color::cms::display::DisplayTransform>,
) -> (egui::TextureHandle, f32) {
let colors = setup.grid_colors.map(|c| crate::display_profile::color32(display, crate::panels::c32(&c)));
// A tile of its own with a display profile, so a preview drawn in sRGB beside the canvas
// doesn't make the canvas's tile again each frame.
let id = egui::Id::unique("transparency-grid-tile").with(display.is_some());
(checker_tile(ctx, setup.grid_colors, display, id), setup.grid_size.cell())
}
/// The transparency grid's 2 × 2-cell tile in the colors `grid_colors` (the first in the top-left
/// and bottom-right cells), converted into the display profile `display` when there is one, and
/// cached under `id` until the colors change.
pub(crate) fn checker_tile(
ctx: &egui::Context,
grid_colors: [vectorcraft_color::Color; 2],
display: Option<&vectorcraft_color::cms::display::DisplayTransform>,
id: egui::Id,
) -> egui::TextureHandle {
let colors = grid_colors.map(|c| crate::display_profile::color32(display, crate::panels::c32(&c)));
let cached = ctx.data(|d| d.get_temp::<([Color32; 2], egui::TextureHandle)>(id)).filter(|(c, _)| *c == colors);
let tex = cached.map(|(_, t)| t).unwrap_or_else(|| {
cached.map(|(_, t)| t).unwrap_or_else(|| {
let [a, b] = colors;
let img = egui::ColorImage::new([2, 2], vec![a, b, b, a]);
let opts = egui::TextureOptions { wrap_mode: egui::TextureWrapMode::Repeat, ..egui::TextureOptions::NEAREST };
let t = ctx.load_texture(TRANSPARENCY_GRID, img, opts);
ctx.data_mut(|d| d.insert_temp(id, (colors, t.clone())));
t
});
(tex, setup.grid_size.cell())
})
}
/// Name of the transparency grid's texture.
+220 -13
View File
@@ -180,15 +180,65 @@ fn grid_color_box_id(index: usize) -> egui::Id {
egui::Id::unique(("grid-color-box", index))
}
/// The grid colors' hex codes that Document Setup `d` holds now (empty strings for entries that are
/// not strings).
fn grid_colors(d: &Dialog) -> Vec<String> {
d.fields.get("gridColors").and_then(Value::as_array).map(|a| a.iter().map(|c| c.as_str().unwrap_or("").to_string()).collect()).unwrap_or_default()
/// The size of the Transparency tab's grid preview, in points.
const GRID_PREVIEW: egui::Vec2 = egui::vec2(112.0, 72.0);
/// The grid size and the two colors the grid preview shows for the dialog's fields `fields`, read
/// as `document.setup` reads them (a preset name or two colors): a field that is missing or not a
/// valid value shows as `setup`, the document's setting.
fn preview_grid(fields: &Map<String, Value>, setup: &vectorcraft_doc::DocSetup) -> (GridSize, [vectorcraft_color::Color; 2]) {
let size = fields.get("gridSize").and_then(Value::as_str).and_then(GridSize::parse).unwrap_or(setup.grid_size);
(size, grid_colors(fields, setup))
}
/// The two grid colors the dialog's fields `fields` hold, read as `document.setup` reads
/// `gridColors` (a preset name, in any letter case, or two colors): a field that is missing or not
/// a valid value gives `setup`'s colors, the document's setting.
fn grid_colors(fields: &Map<String, Value>, setup: &vectorcraft_doc::DocSetup) -> [vectorcraft_color::Color; 2] {
let colors = match fields.get("gridColors") {
Some(Value::String(name)) => GRID_COLOR_PRESETS.iter().find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(_, pair)| *pair),
Some(Value::Array(a)) if a.len() == 2 => a.iter().map(vectorcraft_engine::cmd::color_value).collect::<Option<Vec<_>>>().map(|c| [c[0], c[1]]),
_ => None,
};
colors.unwrap_or(setup.grid_colors)
}
/// The checkered preview of the grid the dialog's Grid Size and Grid Colors hold, drawn with the
/// canvas's transparency grid tile and cell size: the first color in the top-left cell. It goes
/// beside the rows that start at `top`, against the right edge, or below them when they leave no
/// room. It shows the grid whatever Simulate Colored Paper and View › Show Transparency Grid are,
/// and changes nothing in the document.
fn grid_preview(app: &VectorcraftApp, ui: &mut egui::Ui, d: &Dialog, top: f32) {
let t = Tokens::get(ui.ctx());
let fallback = vectorcraft_doc::DocSetup::default();
let setup = app.session.active().map_or(&fallback, |st| &st.doc.setup);
let (size, colors) = preview_grid(&d.fields, setup);
let right = ui.max_rect().right();
let rect = if ui.min_rect().right() + 16.0 + GRID_PREVIEW.x <= right {
egui::Rect::from_min_size(egui::pos2(right - GRID_PREVIEW.x, top), GRID_PREVIEW)
} else {
ui.add_space(4.0);
ui.allocate_exact_size(GRID_PREVIEW, egui::Sense::hover()).0
};
let resp = ui.interact(rect, grid_preview_id(), egui::Sense::hover());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Image, true, tl!("Transparency Grid")));
let display = app.display_profile.transform(app.session.prefs.monitor_profile_display);
let id = egui::Id::unique("document-setup-grid-preview").with(display.is_some());
let tile = crate::canvas::checker_tile(ui.ctx(), colors, display.as_deref(), id);
let uv = egui::Rect::from_min_max(egui::Pos2::ZERO, (rect.size() / (2.0 * size.cell())).to_pos2());
ui.painter().image(tile.id(), rect, uv, egui::Color32::WHITE);
ui.painter().rect_stroke(rect, 0.0, egui::Stroke::new(1.0, t.input_border), egui::StrokeKind::Outside);
}
/// The id of the Transparency tab's grid preview.
fn grid_preview_id() -> egui::Id {
egui::Id::unique("grid-preview")
}
fn transparency(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
let t = Tokens::get(ui.ctx());
widgets::subheader(ui, tl!("Transparency Grid"));
let top = ui.cursor().top();
let sizes: Vec<(&str, &str)> = GridSize::ALL.iter().map(|g| (g.id(), g.label())).collect();
choice(ui, d, "gridSize", tl!("Grid Size:"), &sizes);
// A preset name in the fields (any case, as `document.setup` takes it) becomes the preset's two
@@ -198,10 +248,15 @@ fn transparency(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
{
d.fields.insert("gridColors".into(), json!(pair.map(|c| c.to_hex())));
}
let colors = grid_colors(d);
// The pair the field holds, or the document's pair when it holds no two colors, so each box and
// hex field shows a color and the picker and hex fields set an entry of a two-color list.
let fallback = vectorcraft_doc::DocSetup::default();
let setup = app.session.active().map_or(&fallback, |st| &st.doc.setup);
let pair = |d: &Dialog| -> Vec<String> { grid_colors(&d.fields, setup).iter().map(vectorcraft_color::Color::to_hex).collect() };
let colors = pair(d);
let preset = GRID_COLOR_PRESETS
.iter()
.find(|(_, pair)| colors.len() == 2 && colors.iter().zip(pair).all(|(h, c)| h.eq_ignore_ascii_case(&c.to_hex())))
.find(|(_, pair)| colors.iter().zip(pair).all(|(h, c)| h.eq_ignore_ascii_case(&c.to_hex())))
.map_or("Custom", |(n, _)| n);
let names: Vec<&str> = GRID_COLOR_PRESETS.iter().map(|(n, _)| *n).chain(["Custom"]).collect();
widgets::label_row(ui, tl!("Grid Colors:"), LABEL, |ui| {
@@ -211,10 +266,10 @@ fn transparency(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
});
// The two colours (the first is also the paper colour): editing either makes them Custom. A
// click on a color's box, or Space or Enter with the keyboard on it, opens the Color Picker on
// that color (white when its hex is not a color), and the picker's OK sets that color. The
// picker opens after the row, on the color Document Setup holds then: a press on a box ends a
// hex field's editing in the same frame, and that field's edit is the color the picker opens on.
// A hex field's edit replaces its own color in the pair Document Setup holds when it commits.
// that color, and the picker's OK sets that color. The picker opens after the row, on the color
// Document Setup holds then: a press on a box ends a hex field's editing in the same frame, and
// that field's edit is the color the picker opens on. A hex field's edit replaces its own color
// in the pair Document Setup holds when it commits.
let mut pick = None;
widgets::label_row(ui, "", LABEL, |ui| {
for i in 0..2 {
@@ -242,16 +297,23 @@ fn transparency(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
ui.painter().rect_stroke(chip.expand(2.0), 3.0, ui.visuals().selection.stroke, egui::StrokeKind::Outside);
}
if let Some(c) = widgets::hex_field_named(ui, ("grid-color", i), Some(&name), hex).and_then(|h| vectorcraft_color::Color::from_hex(&h)) {
let mut v = grid_colors(d);
v.resize(2, "#ffffff".into());
let mut v = pair(d);
v[i] = c.to_hex();
d.fields.insert("gridColors".into(), json!(v));
}
ui.add_space(10.0);
}
});
grid_preview(app, ui, d, top);
if let Some(i) = pick {
let c = grid_colors(d).get(i).and_then(|h| vectorcraft_color::Color::from_hex(h.trim_start_matches('#')));
// A field that is not two colors becomes the pair the boxes show, so the picker's OK has an
// entry to set.
let v = pair(d);
let entries = d.fields.get("gridColors").and_then(Value::as_array);
if !entries.is_some_and(|a| a.len() == 2 && a.iter().all(|c| vectorcraft_engine::cmd::color_value(c).is_some())) {
d.fields.insert("gridColors".into(), json!(v));
}
let c = v.get(i).and_then(|h| vectorcraft_color::Color::from_hex(h.trim_start_matches('#')));
*d = super::color_picker::over_list_entry(d, "gridColors", i, c.unwrap_or(vectorcraft_color::Color::WHITE));
}
ui.add_space(4.0);
@@ -783,6 +845,36 @@ mod tests {
}
}
/// When Grid Colors holds a preset name (in any letter case) or a list that is not two colors,
/// the boxes and hex fields show the preset's pair or the document's, a box opens the Color
/// Picker on its color, and the picker's OK sets only that entry of a two-color list.
#[test]
fn a_grid_color_box_opens_on_a_preset_name_or_a_short_list() {
for (field, index, opens, kept, name) in [
(json!("Blue"), 1, "#c4dcff", "#ffffff", "Blue"),
(json!("purple"), 0, "#ffffff", "#e2c9ff", "Purple"),
(json!(["#ff0000"]), 1, "#cccccc", "#ffffff", "Light"),
] {
let (mut app, ctx) = transparency_tab();
app.ui.dialog.as_mut().unwrap().fields.insert("gridColors".into(), field.clone());
dialog_frame(&mut app, &ctx, vec![]);
let texts: Vec<String> = dialog_frame(&mut app, &ctx, vec![]).into_iter().map(|(t, _)| t).collect();
for shown in [name, opens.trim_start_matches('#'), kept.trim_start_matches('#')] {
assert!(texts.iter().any(|t| t.eq_ignore_ascii_case(shown)), "{field}: no {shown} in {texts:?}");
}
click_grid_box(&mut app, &ctx, index);
assert_picker_over_setup(&app, opens, &field.to_string());
app.ui.dialog.as_mut().unwrap().fields.insert("color".into(), json!("#00ff00"));
crate::dialogs::confirm(&mut app).unwrap();
let d = app.ui.dialog.as_ref().unwrap();
let mut pair = [hex(kept); 2];
pair[index] = hex("#00ff00");
assert_eq!((d.kind.as_str(), dialog_grid(d)), (super::KIND, pair.to_vec()), "{field}");
crate::dialogs::confirm(&mut app).unwrap();
assert_eq!(app.session.active().unwrap().doc.setup.grid_colors, pair, "{field}");
}
}
/// With a preset name in Document Setup's `gridColors` field, the grid color boxes show the
/// preset's colors, and the Color Picker opens on the clicked one; its OK sets that color and
/// keeps the preset's other one.
@@ -912,4 +1004,119 @@ mod tests {
assert_eq!(app.session.active().unwrap().doc.setup.grid_colors, LIGHT.map(hex));
assert_eq!(undo_steps(&app), 0);
}
/// The grid preview Document Setup draws, in a fresh context: its rect, the texture
/// coordinates of its bottom-right corner (two cells per unit) and the tile's four pixels.
fn grid_preview(app: &mut VectorcraftApp) -> (Rect, egui::Vec2, Vec<egui::Color32>) {
let ctx = egui::Context::default();
crate::theme::install_fonts(&ctx);
crate::theme::apply(&ctx, Default::default());
let (mut mesh, mut pixels) = (None, None);
for _ in 0..3 {
let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1200.0, 900.0))), ..Default::default() };
let mut out = ctx.run_ui(raw, |ui| crate::dialogs::show(app, ui.ctx()));
let tiles: Vec<egui::TextureId> =
ctx.tex_manager().read().allocated().filter(|(_, m)| m.name == crate::canvas::TRANSPARENCY_GRID).map(|(id, _)| *id).collect();
for (id, deltas) in &out.textures_delta.set {
if tiles.contains(id)
&& let Some(d) = deltas.last()
{
let egui::ImageData::Color(img) = &d.image;
pixels = Some(img.pixels.clone());
}
}
out.textures_delta.clear();
mesh = out.shapes.iter().find_map(|c| match &c.shape {
egui::Shape::Mesh(m) if tiles.contains(&m.texture_id) => Some(m.clone()),
_ => None,
});
}
let mesh = mesh.unwrap_or_else(|| panic!("no grid preview"));
let uv = mesh.vertices.iter().fold(vec2(0.0, 0.0), |a, v| a.max(v.uv.to_vec2()));
(mesh.calc_bounds(), uv, pixels.unwrap_or_else(|| panic!("no tile")))
}
/// The tile's pixels for the grid colors `a` and `b`: `a` in the top-left and bottom-right cells.
fn tile(a: &str, b: &str) -> Vec<egui::Color32> {
let c = |h| crate::panels::c32(&hex(h));
vec![c(a), c(b), c(b), c(a)]
}
/// The Transparency tab shows a checkered preview of the grid its Grid Size and Grid Colors
/// hold before OK: the canvas's cells, the first color in the top-left cell. Simulate Colored
/// Paper leaves it checkered, and neither it nor Cancel changes the document.
#[test]
fn the_transparency_tab_previews_the_pending_grid_size_and_colors() {
let (mut app, _) = transparency_tab();
let (rect, uv, pixels) = grid_preview(&mut app);
assert_eq!(rect.size(), super::GRID_PREVIEW);
assert_eq!((uv, pixels), (rect.size() / 16.0, tile(LIGHT[0], LIGHT[1])), "Medium, Light");
for (size, cell) in [("small", 4.0), ("large", 16.0)] {
let d = app.ui.dialog.as_mut().unwrap();
d.fields.insert("gridSize".into(), json!(size));
d.fields.insert("gridColors".into(), json!(["#ff0000", "#0000ff"]));
let (rect, uv, pixels) = grid_preview(&mut app);
assert_eq!((uv, pixels), (rect.size() / (2.0 * cell), tile("#ff0000", "#0000ff")), "{size}");
}
app.ui.dialog.as_mut().unwrap().fields.insert("simulatePaper".into(), json!(true));
let (rect, uv, pixels) = grid_preview(&mut app);
assert_eq!((uv, pixels), (rect.size() / 32.0, tile("#ff0000", "#0000ff")), "Simulate Colored Paper");
let st = app.session.active().unwrap();
assert_eq!((st.doc.setup.grid_size, st.doc.setup.grid_colors), (vectorcraft_doc::GridSize::Medium, LIGHT.map(hex)));
assert_eq!(undo_steps(&app), 0);
crate::dialogs::cancel(&mut app);
assert!(app.ui.dialog.is_none());
let st = app.session.active().unwrap();
assert_eq!(
(st.doc.setup.grid_size, st.doc.setup.grid_colors, st.doc.setup.simulate_paper),
(vectorcraft_doc::GridSize::Medium, LIGHT.map(hex), false)
);
assert_eq!(undo_steps(&app), 0);
}
/// The grid preview sits beside Grid Size, Grid Colors and the color boxes, inside the dialog,
/// and covers none of the tab's text or color boxes.
#[test]
fn the_grid_preview_sits_beside_the_grid_rows() {
let (mut app, ctx) = transparency_tab();
let texts = dialog_frame(&mut app, &ctx, vec![]);
let (rect, _, _) = grid_preview(&mut app);
let label = texts.iter().find(|(t, _)| t == "Grid Size:").unwrap_or_else(|| panic!("no Grid Size: {texts:?}")).1;
assert!(rect.top() <= label.bottom() && rect.left() > label.right(), "{rect:?} beside {label:?}");
for (text, r) in &texts {
assert!(!r.intersects(rect), "{text:?} at {r:?} under the preview {rect:?}");
}
for i in 0..2 {
let r = ctx.read_response(super::grid_color_box_id(i)).unwrap().rect;
assert!(!r.intersects(rect), "box {i} at {r:?} under the preview {rect:?}");
}
// The dialog's content is 540 points wide from its heading's left edge.
let left = texts.iter().find(|(t, _)| t == "Document Setup").unwrap_or_else(|| panic!("no heading: {texts:?}")).1.left();
assert!(rect.right() <= left + 540.0 + 0.5, "{rect:?} inside the dialog from {left}");
}
/// A grid size or color pair the dialog holds that is not a valid value shows as the
/// document's setting.
#[test]
fn the_grid_preview_shows_the_document_setting_for_a_bad_field() {
let setup = vectorcraft_doc::DocSetup {
grid_size: vectorcraft_doc::GridSize::Large,
grid_colors: [hex("#111111"), hex("#222222")],
..Default::default()
};
let fields = |v: serde_json::Value| v.as_object().unwrap().clone();
for bad in [
json!({}),
json!({"gridSize": "huge", "gridColors": ["#ff0000"]}),
json!({"gridSize": 3, "gridColors": ["#ff0000", "zz"]}),
json!({"gridColors": "Plaid"}),
json!({"gridColors": ["#ff0000", "#00ff00", "#0000ff"]}),
] {
assert_eq!(super::preview_grid(&fields(bad.clone()), &setup), (setup.grid_size, setup.grid_colors), "{bad}");
}
let good = fields(json!({"gridSize": "small", "gridColors": ["#ff0000", "#0000ff"]}));
assert_eq!(super::preview_grid(&good, &setup), (vectorcraft_doc::GridSize::Small, [hex("#ff0000"), hex("#0000ff")]));
let preset = fields(json!({"gridColors": "blue"}));
assert_eq!(super::preview_grid(&preset, &setup), (vectorcraft_doc::GridSize::Large, [hex("#ffffff"), hex("#c4dcff")]));
}
}