Save for Web: a color shift survives Done right after the Color Picker

The Color Picker's OK for a color table cell now shifts the color in the
table at once, so Done or Save before the dialog draws again keeps the
shift. The table marks list a color written twice in one list once.
This commit is contained in:
dotCooCoo
2026-11-24 18:07:00 -08:00
parent 14fb78aa39
commit c5c54e15f2
4 changed files with 81 additions and 12 deletions
+7 -2
View File
@@ -6,7 +6,8 @@
//! `artboard.setProps {index, color}`.
//!
//! A command dialog's colour row raises a `ColorRequest`; [`pump`] opens a picker over that dialog
//! and answers the row with its OK.
//! and answers the row with its OK. The OK for a Save for Web colour table cell shifts the cell's
//! colour in the table instead ([`super::save_for_web::take_palette_pick`]).
//!
//! Fields agents set with `ui.dialog.set`: the framework picker's (`color`, `mode`, `webColors`,
//! `swatches`, `library`). `stroke`, `artboard` and `color` come from the opener.
@@ -253,7 +254,11 @@ fn finish(
) {
let preview = std::mem::take(&mut open.preview);
match (result, &open.target) {
(Some(DialogResult::Ok(r)), Target::Answer { parent, key }) => dialogs.with(|h| h.answer_color(*parent, key, r.picked.color)),
(Some(DialogResult::Ok(r)), Target::Answer { parent, key }) => {
if !super::save_for_web::take_palette_pick(app, *parent, key, open.start, r.picked.color) {
dialogs.with(|h| h.answer_color(*parent, key, r.picked.color));
}
}
(Some(DialogResult::Ok(r)), _) => {
if let Err(e) = commit(app, open, preview, &r.picked) {
app.status(e);
+35 -8
View File
@@ -19,10 +19,12 @@ use vectorcraft_render::encode::quantize::Reduction;
use craft::behavior::PixelSize;
use craft::host::PickRequest;
use craft::theme::Srgba;
use craft::ui::dialog::{DialogId, DialogResult, StackDialog};
use craft::ui::patterns::form_dialogs::{
BrowserPreview, ColorTable, ColorTableEdit, ColorTableEvent, HostedSaveForWeb, OptimizeAnswer, OptimizeRequest, OptimizeScope, OptimizeStart,
SaveForWebOptions, SaveForWebResult, WantedVariant, WebDocument, WebFormat, WebPreset, WebPresetEvent, WebSlice, WebSlices, WebVariant,
PALETTE_COLOR_KEY, SaveForWebOptions, SaveForWebResult, WantedVariant, WebDocument, WebFormat, WebPreset, WebPresetEvent, WebSlice, WebSlices,
WebVariant,
};
use craft::ui::primitives::{Revision, TextureStore, TextureStoreOptions};
use craft::ui::runtime::requests::PathRequest;
@@ -432,7 +434,7 @@ fn on_dialog<R>(host: &craft::ui::shell::WindowDialogs, f: impl FnOnce(&mut Host
/// Answers the open dialog once per frame: its result when it closed, else its requests.
/// Colour requests (the matte, a palette cell) are routed by the host-wide colour picker routing;
/// a palette answer comes back as a `Shift` table event.
/// a palette cell's pick is applied to the table by [`take_palette_pick`].
pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) {
let Some(host) = app.dialogs() else { return };
match host.with(|h| h.take::<HostedSaveForWeb>()) {
@@ -511,12 +513,7 @@ fn answer_variants(app: &mut VectorcraftApp, ctx: &egui::Context, host: &craft::
/// Applies the colour table edits the dialog filed. A Delete clears the dialog's colour selection.
fn edit_table(app: &mut VectorcraftApp, ctx: &egui::Context, host: &craft::ui::shell::WindowDialogs, asked: &Asked) {
let shown = app
.session
.web_settings(&settings_of(&asked.options, &app.web_export.table, None))
.and_then(|s| encoded(app, &s))
.map(|o| o.colors.iter().map(|c| c.source).collect::<Vec<[u8; 3]>>());
let shown = match shown {
let shown = match shown_colors(app, &asked.options) {
Ok(shown) => shown,
Err(e) => return app.status(e),
};
@@ -533,6 +530,36 @@ fn edit_table(app: &mut VectorcraftApp, ctx: &egui::Context, host: &craft::ui::s
}
}
/// The source colours of the palette the dialog's settings `options` show, with the table as it stands.
fn shown_colors(app: &mut VectorcraftApp, options: &SaveForWebOptions) -> Result<Vec<[u8; 3]>, String> {
let settings = app.session.web_settings(&settings_of(options, &app.web_export.table, None))?;
Ok(encoded(app, &settings)?.colors.iter().map(|c| c.source).collect())
}
/// The Color Picker's OK for the colour table cell `key` of the open dialog `parent`: the cell's colour `from`
/// is shifted to `to` in the table at once, so Done or Save in the dialog's next pass keeps the shift. A colour
/// the palette no longer shows is not shifted, and a refused shift shows in the status line. `false` when
/// `parent` is not Save for Web or `key` is not its palette, and the picker answers the row instead.
pub(crate) fn take_palette_pick(app: &mut VectorcraftApp, parent: DialogId, key: &str, from: Srgba, to: Srgba) -> bool {
let Some(host) = app.dialogs() else { return false };
if key != PALETTE_COLOR_KEY {
return false;
}
let Some(options) = host.with(|h| h.get_mut_by_id::<HostedSaveForWeb>(parent).map(|d| d.current().clone())) else {
return false;
};
let rgb = |c: Srgba| {
let [r, g, b, _] = c.to_rgba().0;
[r, g, b]
};
let edit = ColorTableEdit::Shift(rgb(to));
let shifted = shown_colors(app, &options).and_then(|shown| color_table::apply(&mut app.web_export.table, &edit, &[rgb(from)], &shown));
if let Err(e) = shifted {
app.status(e);
}
true
}
/// Runs what the preset list asked for and shows a failure in the status line.
fn store_presets(app: &mut VectorcraftApp, events: Vec<WebPresetEvent>) {
for event in events {
@@ -241,9 +241,17 @@ pub(crate) fn restore(table: &mut Value) {
}
/// What the table shows of `table`: its locked, transparent and shifted colours. An entry that is
/// not a colour is skipped.
/// not a colour is skipped, and a colour written more than once in a list is listed once.
pub(crate) fn marks(table: &Value) -> TableMarks {
let colors = |key: &str| list(table, key).iter().filter_map(|c| parse(c)).collect();
let colors = |key: &str| {
let mut out: Vec<[u8; 3]> = Vec::new();
for c in list(table, key).iter().filter_map(|c| parse(c)) {
if !out.contains(&c) {
out.push(c);
}
}
out
};
let shifted = shifts(table).iter().filter_map(|(k, v)| Some((parse(k)?, parse(v.as_str()?)?))).collect();
// PLAN: TableMarks lacks `web_shifted` (ex.W9 request); the Web Shift/Unshift label needs it.
let mut marks = TableMarks::default();
@@ -279,6 +279,35 @@ fn a_picked_color_becomes_a_shift_and_cancel_shifts_nothing() {
assert_eq!(app.web_export.table["shifted"], json!({"#0000ff": "#00ff00"}));
}
/// Done right after the Color Picker's OK, before the dialog draws again, remembers the picked shift.
#[test]
fn done_right_after_the_color_picker_remembers_the_shift() {
let dir = vectorcraft_testkit::temp_dir("sfw-shift-done");
let (mut app, _) = app(&dir, false);
app.run("shape.rectangle", json!({"x": 40, "y": 0, "width": 20, "height": 40})).unwrap();
app.run("paint.setFill", json!({"color": "#0000ff"})).unwrap();
app.run("file.saveForWeb", json!({})).unwrap();
for (k, v) in [("format", json!("gif")), ("antiAlias", json!("none")), ("dither", json!("none"))] {
set(&mut app, k, v);
}
let ctx = app.ui_ctx.clone().expect("the app has a window");
crate::dialogs::host::pump(&mut app, &ctx);
let picker = open_cell(&mut app, &ctx, "#0000ff");
host(&app).with(|h| h.set_json_field(&ctx, picker, "color", json!("#00ff00"))).unwrap();
host(&app).with(|h| h.confirm_by_id(&ctx, picker));
// The frame that settles the picker, then Done before the dialog's next pass.
app_frame(&mut app, &ctx, vec![]);
set(&mut app, "choice", json!("done"));
let dialog = id(&app);
host(&app).with(|h| h.confirm_by_id(&ctx, dialog));
for _ in 0..3 {
app_frame(&mut app, &ctx, vec![]);
}
assert!(host(&app).with(|h| h.get::<craft::ui::patterns::form_dialogs::HostedSaveForWeb>().is_none()), "Done closes the dialog");
let remembered = app.session.last_web_settings().color_table.shifted;
assert_eq!(remembered.get("#0000ff").map(String::as_str), Some("#00ff00"));
}
#[test]
fn restore_brings_back_a_preview_that_failed() {
let dir = vectorcraft_testkit::temp_dir("sfw-restore-table");