From c5c54e15f25d6fe10f3160ee87eb6582f63255ec Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 18:07:00 -0800 Subject: [PATCH] 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. --- crates/ui-egui/src/dialogs/color_picker.rs | 9 +++- crates/ui-egui/src/dialogs/save_for_web.rs | 43 +++++++++++++++---- .../src/dialogs/save_for_web/color_table.rs | 12 +++++- .../src/dialogs/save_for_web/tests_table.rs | 29 +++++++++++++ 4 files changed, 81 insertions(+), 12 deletions(-) diff --git a/crates/ui-egui/src/dialogs/color_picker.rs b/crates/ui-egui/src/dialogs/color_picker.rs index 0c1f50cbb1..af1b318728 100644 --- a/crates/ui-egui/src/dialogs/color_picker.rs +++ b/crates/ui-egui/src/dialogs/color_picker.rs @@ -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); diff --git a/crates/ui-egui/src/dialogs/save_for_web.rs b/crates/ui-egui/src/dialogs/save_for_web.rs index b338d45611..aaaf3d4e1c 100644 --- a/crates/ui-egui/src/dialogs/save_for_web.rs +++ b/crates/ui-egui/src/dialogs/save_for_web.rs @@ -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(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::()) { @@ -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::>()); - 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, 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::(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) { for event in events { diff --git a/crates/ui-egui/src/dialogs/save_for_web/color_table.rs b/crates/ui-egui/src/dialogs/save_for_web/color_table.rs index 280fcae18a..39b03bc9d9 100644 --- a/crates/ui-egui/src/dialogs/save_for_web/color_table.rs +++ b/crates/ui-egui/src/dialogs/save_for_web/color_table.rs @@ -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(); diff --git a/crates/ui-egui/src/dialogs/save_for_web/tests_table.rs b/crates/ui-egui/src/dialogs/save_for_web/tests_table.rs index 60b12ec807..6dabd4c119 100644 --- a/crates/ui-egui/src/dialogs/save_for_web/tests_table.rs +++ b/crates/ui-egui/src/dialogs/save_for_web/tests_table.rs @@ -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::().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");