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 cbeedbe224..1ce1d5dac1 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 @@ -243,7 +243,7 @@ pub(crate) fn restore(table: &mut Value) { set_table(table, "customPalette", json!([])); } -/// What the table shows of `table`: its locked, transparent and shifted colours. An entry that is +/// What the table shows of `table`: its locked, transparent, shifted and web-shifted colors. An entry that is /// 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| { @@ -255,11 +255,13 @@ pub(crate) fn marks(table: &Value) -> TableMarks { } 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 shifted: Vec<([u8; 3], [u8; 3])> = shifts(table).iter().filter_map(|(k, v)| Some((parse(k)?, parse(v.as_str()?)?))).collect(); + // A color shifted by hand is not web-shifted, as the engine reports it. + let web_shifted = colors("webShift").into_iter().filter(|c| !shifted.iter().any(|(from, to)| from == c && to != c)).collect(); let mut marks = TableMarks::default(); marks.locked = colors("locked"); marks.transparent = colors("transparent"); marks.shifted = shifted; + marks.web_shifted = Some(web_shifted); marks } 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 6f8d414185..d6c5c01f57 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 @@ -3,7 +3,7 @@ use serde_json::{Value, json}; use vectorcraft_engine::cmd::webexport; -use craft::ui::patterns::form_dialogs::{ColorTableEdit, TableOrder}; +use craft::ui::patterns::form_dialogs::{ColorTableEdit, TableMarks, TableOrder}; use super::color_table::{apply, marks, restore, shift}; use super::tests::{app, frame, host, id, save, set}; @@ -58,12 +58,16 @@ fn every_table_edit_changes_the_lists_it_names() { assert_eq!(t["sort"], json!(id)); } // The marks read the same lists, and skip entries that are not colours. - let mut t = - json!({"locked": ["#000000", "nope", 7], "transparent": ["#ABCDEF"], "shifted": {"#111111": "#222222", "x": "#000000", "#333333": 5}}); + let t = json!({"locked": ["#000000", "nope", 7], "transparent": ["#ABCDEF"], "shifted": {"#111111": "#222222", "x": "#000000", "#333333": 5}}); let m = marks(&t); assert_eq!((m.locked, m.transparent, m.shifted), (vec![A], vec![[0xab, 0xcd, 0xef]], vec![(B, C)])); - t = json!(7); - assert_eq!(marks(&t), Default::default()); + // The web-shifted colors are the ones in webShift that are not shifted by hand, as the engine reports them. + let t = json!({"webShift": ["#00f", "#111111", "nope"], "shifted": {"#111111": "#222222"}}); + assert_eq!(marks(&t).web_shifted, Some(vec![[0, 0, 0xff]])); + let t = json!(7); + let mut empty = TableMarks::default(); + empty.web_shifted = Some(Vec::new()); + assert_eq!(marks(&t), empty); } #[test] @@ -223,25 +227,25 @@ fn app_frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec Option> { + let (find, _) = craft::automation::ControlRequest::new("ui.find", json!({"name": name})); + match crate::control::handle(app, ctx, &find) { + craft::automation::Reply::Done(r) if r["ok"] == true => r["result"]["rect"].as_array().map(|r| r.iter().filter_map(Value::as_f64).collect()), + _ => None, + } +} + /// Double-clicks the Color Table's cell of `hex` in drawn frames, as a user opens it, and gives the Color Picker the /// app opens over the dialog for it. fn open_cell(app: &mut VectorcraftApp, ctx: &egui::Context, hex: &str) -> craft::ui::dialog::DialogId { let parent = id(app); - // The cells are named by their index and colour. - let find = |app: &mut VectorcraftApp, name: &str| { - let (find, _) = craft::automation::ControlRequest::new("ui.find", json!({"name": name})); - match crate::control::handle(app, ctx, &find) { - craft::automation::Reply::Done(r) if r["ok"] == true => { - r["result"]["rect"].as_array().map(|r| r.iter().filter_map(Value::as_f64).collect::>()) - } - _ => None, - } - }; // The dialog centres itself again as its panes and table fill in: the cell is clicked once it stays put. + // The cells are named by their index and colour. let mut rect = None; for _ in 0..20 { app_frame(app, ctx, vec![]); - let now = (0..256).find_map(|i| find(app, &format!("{i} {hex}"))); + let now = (0..256).find_map(|i| find(app, ctx, &format!("{i} {hex}"))); if now.is_some() && now == rect { break; } @@ -324,6 +328,49 @@ fn done_right_after_the_color_picker_remembers_the_shift() { assert_eq!(remembered.get("#0000ff").map(String::as_str), Some("#00ff00")); } +/// The dialog is told which colors are web-shifted: a web-shifted selection offers Unshift and not Web Shift. A +/// selection that also holds a color shifted by hand offers both, and its Unshift takes the web shift away and +/// keeps the hand shift. +#[test] +fn the_dialog_offers_unshift_for_web_shifted_colors_and_keeps_a_hand_shift() { + let dir = vectorcraft_testkit::temp_dir("sfw-web-shifted"); + 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); + } + app.web_export.table = json!({"webShift": ["#0000ff"], "shifted": {"#cc3300": "#00ff00"}}); + let ctx = app.ui_ctx.clone().expect("the app has a window"); + crate::dialogs::host::pump(&mut app, &ctx); + let mut rect = None; + for _ in 0..20 { + app_frame(&mut app, &ctx, vec![]); + let now = (0..256).find_map(|i| find(&mut app, &ctx, &format!("{i} #0000ff"))); + if now.is_some() && now == rect { + break; + } + rect = now; + } + assert!(rect.is_some(), "the table shows #0000ff"); + let frames = |app: &mut VectorcraftApp| (0..3).for_each(|_| app_frame(app, &ctx, vec![])); + set(&mut app, "colorSelection", json!(["#0000ff"])); + frames(&mut app); + let shown = |app: &mut VectorcraftApp| (find(app, &ctx, "Shift to Web Palette").is_some(), find(app, &ctx, "Unshift Colors").is_some()); + assert_eq!(shown(&mut app), (false, true), "a web-shifted selection offers Unshift alone"); + set(&mut app, "colorSelection", json!(["#0000ff", "#cc3300"])); + frames(&mut app); + assert_eq!(shown(&mut app), (true, true), "a mixed selection offers both"); + let rect = find(&mut app, &ctx, "Unshift Colors").expect("the Unshift button"); + let at = egui::pos2((rect[0] + rect[2] / 2.0) as f32, (rect[1] + rect[3] / 2.0) as f32); + let button = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + for events in [vec![egui::Event::PointerMoved(at)], vec![button(true)], vec![button(false)], vec![], vec![]] { + app_frame(&mut app, &ctx, events); + } + assert_eq!((&app.web_export.table["webShift"], &app.web_export.table["shifted"]), (&json!([]), &json!({"#cc3300": "#00ff00"}))); +} + #[test] fn restore_brings_back_a_preview_that_failed() { let dir = vectorcraft_testkit::temp_dir("sfw-restore-table");