Document Setup: a grid color box opens the Color Picker

On the Transparency tab, a click on either color box beside Grid Colors,
or Enter or Space with the keyboard on it, opens the Color Picker over
Document Setup on that color. The picker's OK sets only that entry of
gridColors and goes back to Document Setup, whose Grid Colors menu then
names the pair (Custom unless it matches a preset); Document Setup's OK
applies it in one undo step. The picker's Cancel leaves both colors as
they were. Each box is a focusable button named for its color.
This commit is contained in:
dotCooCoo
2026-11-17 12:46:09 -08:00
parent ee60c51fd9
commit 5c2b3be4fc
4 changed files with 190 additions and 4 deletions
+26 -1
View File
@@ -285,6 +285,18 @@ pub(super) fn over_effect(parent: &Dialog, field: &str, colour: Color) -> Dialog
d
}
/// The entry of the dialog's color list that a Color Picker opened with [`over_list_entry`] sets.
const COLOUR_INDEX: &str = "__colourIndex";
/// The Color Picker over the dialog `parent`, on `colour`, that sets entry `index` of the dialog's
/// list of colors `field` (Document Setup's `gridColors`) with its OK and goes back to it, leaving
/// the other entries as they are. Cancel goes back to `parent` as it was. It previews nothing.
pub(super) fn over_list_entry(parent: &Dialog, field: &str, index: usize, colour: Color) -> Dialog {
let mut d = over_effect(parent, field, colour);
d.fields.insert(COLOUR_INDEX.into(), json!(index));
d
}
/// The field of a Color Picker opened over Recolor Artwork ([`over_recolor`]): the row whose new
/// color it picks.
const RECOLOR_ROW: &str = "__recolorRow";
@@ -724,7 +736,20 @@ fn swatch_list(app: &VectorcraftApp, ui: &mut Ui, d: &mut Dialog, current: &Colo
fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
if let Some(field) = d.fields.get(COLOUR_FIELD).and_then(Value::as_str) {
let mut parent = super::parent(d).ok_or("the dialog it was opened over is no longer open")?;
parent.fields.insert(field.to_string(), json!(picked(d).color.to_hex()));
let hex = json!(picked(d).color.to_hex());
match d.fields.get(COLOUR_INDEX) {
None => {
parent.fields.insert(field.to_string(), hex);
}
Some(index) => {
let entry = index
.as_u64()
.and_then(|i| usize::try_from(i).ok())
.and_then(|i| parent.fields.get_mut(field)?.as_array_mut()?.get_mut(i))
.ok_or("no such color")?;
*entry = hex;
}
}
// The dialog previews the new colour when it comes back: end this picker's interaction, as
// Cancel does, so the dialog starts its preview afresh.
let _ = app.session.cancel_interaction();
+160 -3
View File
@@ -175,6 +175,11 @@ fn general(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool
close
}
/// The id of the Transparency tab's box that shows grid color `index` (0 or 1).
fn grid_color_box_id(index: usize) -> egui::Id {
egui::Id::unique(("grid-color-box", index))
}
fn transparency(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
let t = Tokens::get(ui.ctx());
widgets::subheader(ui, tl!("Transparency Grid"));
@@ -196,16 +201,35 @@ fn transparency(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
d.fields.insert("gridColors".into(), json!(pair.map(|c| c.to_hex())));
}
});
// The two colours (the first is also the paper colour): editing either makes them Custom.
// 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.
let mut pick = None;
widgets::label_row(ui, "", LABEL, |ui| {
for i in 0..2 {
let hex = colors.get(i).map_or("", |h| h.trim_start_matches('#'));
let colour = vectorcraft_color::Color::from_hex(hex);
let name = format!("{} {}", tl!("Grid Colors:"), i + 1);
let (chip, _) = ui.allocate_exact_size(egui::vec2(18.0, 18.0), egui::Sense::hover());
if let Some(c) = vectorcraft_color::Color::from_hex(hex) {
let resp = ui.interact(chip, grid_color_box_id(i), egui::Sense::click());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &name));
let keyed = resp.has_focus()
&& ui.input_mut(|inp| {
inp.consume_key(egui::Modifiers::NONE, egui::Key::Space) | inp.consume_key(egui::Modifiers::NONE, egui::Key::Enter)
});
if resp.clicked() || keyed {
pick = Some((i, colour.unwrap_or(vectorcraft_color::Color::WHITE)));
}
if resp.hovered() {
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
}
if let Some(c) = colour {
ui.painter().rect_filled(chip, 2.0, crate::panels::c32(&c));
}
ui.painter().rect_stroke(chip, 2.0, egui::Stroke::new(1.0, t.input_border), egui::StrokeKind::Inside);
let name = format!("{} {}", tl!("Grid Colors:"), i + 1);
if resp.has_focus() {
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 = colors.clone();
v.resize(2, "#ffffff".into());
@@ -215,6 +239,9 @@ fn transparency(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) {
ui.add_space(10.0);
}
});
if let Some((i, c)) = pick {
*d = super::color_picker::over_list_entry(d, "gridColors", i, c);
}
ui.add_space(4.0);
check(ui, d, "simulatePaper", tl!("Simulate Colored Paper"));
ui.add_space(14.0);
@@ -635,4 +662,134 @@ mod tests {
assert_eq!(fill(&app, id), before);
assert_eq!(undo_steps(&app), steps);
}
/// The Light pair a new document's transparency grid starts with.
const LIGHT: [&str; 2] = ["#ffffff", "#cccccc"];
fn hex(h: &str) -> Color {
Color::from_hex(h).unwrap_or_else(|| panic!("bad hex {h}"))
}
/// The grid colors Document Setup `d` holds.
fn dialog_grid(d: &crate::state::Dialog) -> Vec<Color> {
d.fields["gridColors"].as_array().unwrap().iter().map(|c| hex(c.as_str().unwrap())).collect()
}
/// A new document with Document Setup open on its Transparency tab, drawn in the context returned.
fn transparency_tab() -> (VectorcraftApp, egui::Context) {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
app.run("file.documentSetup", json!({})).unwrap();
app.ui.dialog.as_mut().unwrap().fields.insert("tab".into(), json!("Transparency"));
let ctx = egui::Context::default();
crate::theme::install_fonts(&ctx);
crate::theme::apply(&ctx, Default::default());
for _ in 0..4 {
dialog_frame(&mut app, &ctx, vec![]);
}
(app, ctx)
}
/// Clicks the box of grid color `index`.
fn click_grid_box(app: &mut VectorcraftApp, ctx: &egui::Context, index: usize) {
let at = ctx.read_response(super::grid_color_box_id(index)).unwrap_or_else(|| panic!("no box {index}")).rect.center();
let button = |pressed| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Default::default() };
dialog_frame(app, ctx, vec![Event::PointerMoved(at), button(true)]);
dialog_frame(app, ctx, vec![button(false)]);
}
/// The Color Picker is open over Document Setup, on `color`, and Document Setup's OK has not run.
fn assert_picker_over_setup(app: &VectorcraftApp, color: &str, case: &str) {
let d = app.ui.dialog.as_ref().unwrap_or_else(|| panic!("{case}: no dialog"));
assert_eq!(d.kind, "colorPicker", "{case}");
assert_eq!(d.fields.get("color").and_then(vectorcraft_engine::cmd::color_value), Some(hex(color)), "{case}");
assert_eq!(crate::dialogs::parent(d).map(|p| p.kind), Some(super::KIND.to_string()), "{case}");
assert_eq!(undo_steps(app), 0, "{case}: Document Setup's OK has not run");
}
/// A click on either grid color's box opens the Color Picker over Document Setup on that
/// color. Its OK sets only that color, the Grid Colors menu names the pair (Custom, or the
/// preset it matches), and Document Setup's OK applies the pair in one undo step.
#[test]
fn a_grid_color_box_opens_the_color_picker_and_its_ok_sets_that_color() {
for (index, picked, pair, name) in [(0, "#00ff00", ["#00ff00", "#cccccc"], "Custom"), (1, "#c4dcff", ["#ffffff", "#c4dcff"], "Blue")] {
let (mut app, ctx) = transparency_tab();
click_grid_box(&mut app, &ctx, index);
assert_picker_over_setup(&app, LIGHT[index], name);
app.ui.dialog.as_mut().unwrap().fields.insert("color".into(), json!(picked));
crate::dialogs::confirm(&mut app).unwrap();
let d = app.ui.dialog.as_ref().unwrap();
assert_eq!((d.kind.as_str(), dialog_grid(d)), (super::KIND, pair.map(hex).to_vec()), "{name}");
assert_eq!(undo_steps(&app), 0, "{name}: the picker's OK leaves the document as it is");
dialog_frame(&mut app, &ctx, vec![]);
let texts: Vec<String> = dialog_frame(&mut app, &ctx, vec![]).into_iter().map(|(t, _)| t).collect();
assert!(texts.iter().any(|t| t == name), "{name}: {texts:?}");
crate::dialogs::confirm(&mut app).unwrap();
assert!(app.ui.dialog.is_none());
let st = app.session.active().unwrap();
assert_eq!((st.doc.setup.grid_colors, st.doc.setup.grid_colors_name()), (pair.map(hex), name));
assert_eq!(undo_steps(&app), 1, "{name}");
app.run("edit.undo", json!({})).unwrap();
assert_eq!(app.session.active().unwrap().doc.setup.grid_colors, LIGHT.map(hex), "{name}");
}
}
/// With the keyboard on a grid color's box, Enter or Space opens the Color Picker on that
/// color instead of pressing Document Setup's OK.
#[test]
fn enter_or_space_on_a_grid_color_box_opens_the_color_picker() {
for (index, key) in [(0, egui::Key::Enter), (1, egui::Key::Space)] {
let (mut app, ctx) = transparency_tab();
ctx.memory_mut(|m| m.request_focus(super::grid_color_box_id(index)));
dialog_frame(&mut app, &ctx, vec![]);
let press = Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() };
dialog_frame(&mut app, &ctx, vec![press]);
assert_picker_over_setup(&app, LIGHT[index], &format!("{key:?}"));
}
}
/// Cancel in the Color Picker goes back to Document Setup with the grid colors as they were,
/// and so does its OK on the color it opened on; Document Setup's OK then records no undo step.
#[test]
fn grid_color_picker_cancel_or_an_unchanged_ok_changes_nothing() {
for cancel in [true, false] {
let (mut app, ctx) = transparency_tab();
click_grid_box(&mut app, &ctx, 1);
assert_picker_over_setup(&app, LIGHT[1], &format!("cancel {cancel}"));
if cancel {
app.ui.dialog.as_mut().unwrap().fields.insert("color".into(), json!("#ff0000"));
crate::dialogs::cancel(&mut app);
} else {
crate::dialogs::confirm(&mut app).unwrap();
}
let d = app.ui.dialog.as_ref().unwrap();
assert_eq!((d.kind.as_str(), dialog_grid(d)), (super::KIND, LIGHT.map(hex).to_vec()), "cancel {cancel}");
crate::dialogs::confirm(&mut app).unwrap();
assert!(app.ui.dialog.is_none());
assert_eq!(app.session.active().unwrap().doc.setup.grid_colors_name(), "Light");
assert_eq!(undo_steps(&app), 0, "cancel {cancel}");
}
}
/// The picker's OK sets nothing when the dialog under it has no such color or is gone: it is
/// an error, and the picker stays open.
#[test]
fn grid_color_picker_without_its_color_or_its_dialog_is_an_error() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
app.run("file.documentSetup", json!({})).unwrap();
let setup = app.ui.dialog.clone().unwrap();
let mut short = setup.clone();
short.fields.insert("gridColors".into(), json!(["#ffffff"]));
let mut orphan = crate::dialogs::color_picker::over_list_entry(&setup, "gridColors", 0, Color::WHITE);
orphan.fields.remove(crate::dialogs::PARENT);
for picker in [crate::dialogs::color_picker::over_list_entry(&short, "gridColors", 1, Color::WHITE), orphan] {
app.ui.dialog = Some(picker);
app.ui.dialog.as_mut().unwrap().fields.insert("color".into(), json!("#ff0000"));
assert!(crate::dialogs::confirm(&mut app).is_err());
assert_eq!(app.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some("colorPicker"));
}
assert_eq!(app.session.active().unwrap().doc.setup.grid_colors, LIGHT.map(hex));
assert_eq!(undo_steps(&app), 0);
}
}
+3
View File
@@ -7385,6 +7385,9 @@ small caps size (1 to 200%) lies outside those ranges opens with the nearer end
`document.setup` does not take the color mode or the assigned profiles. The Document Setup dialog's Color tab sets
them with `edit.assignProfile` and `file.documentColorMode`: its OK runs `document.setup`, then `edit.assignProfile`,
then `file.documentColorMode` in one `command.batch`, and the document's changes are one undo step.
On the dialog's Transparency tab, a click on either grid color's box (or Enter or Space with the keyboard on it) opens
the `colorPicker` dialog over Document Setup on that color. Its OK sets only that entry of the dialog's `gridColors`
and goes back to Document Setup, whose OK runs `document.setup`; its Cancel goes back with `gridColors` unchanged.
```json
{"name":"run_command","arguments":{"command":"document.setup","params":{"gridColors":"Blue","gridSize":"large","language":"German","bleed":9}}}
+1
View File
@@ -810,6 +810,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
- **Window › New Window (G7, #4116):** New Window (`window.newWindow`) opens another view of the current document ([public source](https://helpx.adobe.com/illustrator/using/viewing-artwork.html) describes showing one document in several windows at once), with its own zoom, center, rotation, artboard and Outline or Preview, as a tab of its own; an edit in one view shows in every view. Tile, Cascade, Float in Window, Float All in Windows and Consolidate All Windows arrange each view as they arrange a document, and Next and Previous Document step through the views. Closing a view leaves the document open; closing its last view closes the document, asking to save it first. The view's number after the name in its tab ("Name:2", while the document has more than one view) is VectorCraft's own. Tests: `crates/ui-egui/src/doc_windows/tests.rs`.
- **Document Setup modal handoff:** Document Setup → Edit Artboards applies valid settings, closes the dialog and activates the Artboard tool. Pointer-click tests cover successful handoff, invalid settings, OK, Cancel and Undo in `dialogs/document_setup.rs`.
- **Document Setup › Transparency grid color boxes:** a click on either color box beside Grid Colors opens the Color Picker on that color, as [the help page](https://helpx.adobe.com/illustrator/desktop/manage-colors/apply-transparency-and-blending/view-transparency-in-artworks.html) describes choosing a custom color for each of the grid's two alternating squares. The picker's OK sets only that color in the dialog, Grid Colors then names the pair (Custom unless it matches one of VectorCraft's presets), and Document Setup's OK applies it in one undo step; the picker's Cancel leaves both colors as they were. With the keyboard on a box, Enter or Space opens the picker. The keyboard handling and staging the color until Document Setup's OK are VectorCraft's own. Tests: `a_grid_color_box_opens_the_color_picker_and_its_ok_sets_that_color` and the other grid color tests in `dialogs/document_setup.rs`.
- **Document Setup › Color:** a Color tab shows the document color mode (RGB Color or CMYK Color), a Convert to CMYK or Convert to RGB box for a mode change (cleared: the colors keep their numbers), and the RGB and CMYK profiles assigned to the document, with a working-space entry for none. OK runs `edit.assignProfile` and then `file.documentColorMode` with the dialog's other changes in one `command.batch`, and the document's changes are one undo step. An unchanged Color tab adds no color commands, and Cancel, or OK with nothing changed, records no undo step. A Color tab field with a value of the wrong kind is an error, and the dialog stays open. The tab is VectorCraft's own layout; File › Document Color Mode and Edit › Assign Profile… stay as they are. Tests in `dialogs/document_setup.rs`.
- **Assign Profile's three choices:** the Assign rows of Edit › Assign Profile… (the Separations panel's Color Settings section) offer Don't Color Manage This Document, the working space of the document's color mode (Working RGB or Working CMYK, with its name) and Profile with the other profiles of that kind, as [Assign or remove a color profile](https://help.adobe.com/archive/en_US/illustrator/cs3/illustrator_cs3_help.pdf#page=150) lists them. They act on the profile of the document's color mode: an RGB document's RGB profile, a CMYK document's CMYK profile; the other model's profile stays as it is (VectorCraft's own rule; Document Setup › Color sets both). Picking the working space assigns its profile, which stays when the working space changes later. A choice the document already has runs nothing. Tests in `panels/separations.rs`.
- **Canvas through the display's profile on Windows and X11:** Preferences › Performance › Use Monitor Profile for Canvas (on by default) converts the canvas's art, artboard paper and transparency grid from sRGB into the ICC profile of the display that shows most of the main window, after any proof, as the help page describes the monitor's profile translating the document's colors for the monitor ([the help page](https://help.adobe.com/archive/en_US/illustrator/cs4/illustrator_cs4_help.pdf#page=151)). Windows gives the display's default profile, and a display with Advanced Color on is left to Windows ([Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/wcs/advanced-color-icc-profiles)); X11 gives colord's profile for the output or the `_ICC_PROFILE` property. macOS converts the window itself, and a Wayland session stays sRGB. `ui.inspect` reports `displayProfile`. Tests in `color/src/cms/display.rs`, `ui-egui/src/display_profile.rs` and `canvas::tests::the_canvas_is_drawn_through_the_display_profile_while_the_preference_is_on`.