diff --git a/crates/ui-egui/src/dialogs/save_for_web.rs b/crates/ui-egui/src/dialogs/save_for_web.rs index 1fed4791e9..8cfb7bc6f8 100644 --- a/crates/ui-egui/src/dialogs/save_for_web.rs +++ b/crates/ui-egui/src/dialogs/save_for_web.rs @@ -1205,14 +1205,18 @@ fn color_table(ui: &mut egui::Ui, d: &mut Dialog, s: Option<&WebSettings>) -> Re } }); }); - // Delete Color (the button and the table's menu) and Restore Generated Table. + // Delete Color (the button and the table's menu) and Restore Generated Table. Delete Color + // takes only selected colors the table shows (`palette`, by source). + let palette: Vec = + shot.colors.iter().filter(|c| !c.transparent).map(|c| hex(c.source)).chain(shot.mapped.iter().map(|c| hex(*c))).collect(); + let deletable = !selected_in(d, &palette).is_empty(); let (mut delete, mut restore, mut unshift_all) = (false, false, false); ui.horizontal(|ui| { - delete |= widgets::icon_button_enabled(ui, "trash-2", tl!("Delete Color"), false, !picked.is_empty(), 24.0).clicked(); + delete |= widgets::icon_button_enabled(ui, "trash-2", tl!("Delete Color"), false, deletable, 24.0).clicked(); let menu = widgets::icon_button(ui, "menu", tl!("Color Table"), false, 24.0); let open = menu.clicked(); let _ = widgets::popover(&menu, open, |ui| { - if widgets::menu_item(ui, tl!("Delete Color"), !picked.is_empty(), false) { + if widgets::menu_item(ui, tl!("Delete Color"), deletable, false) { delete = true; ui.close(); } @@ -1242,13 +1246,16 @@ fn color_table(ui: &mut egui::Ui, d: &mut Dialog, s: Option<&WebSettings>) -> Re unshift_all_colors(d); } if delete { - let palette: Vec = - shot.colors.iter().filter(|c| !c.transparent).map(|c| hex(c.source)).chain(shot.mapped.iter().map(|c| hex(*c))).collect(); delete_colors(d, &palette)?; } Ok(()) } +/// The selected colors (by source) that `palette`, the colors the table shows, holds. +pub(super) fn selected_in(d: &Dialog, palette: &[String]) -> Vec { + selection(d).into_iter().filter(|c| palette.iter().any(|p| p.eq_ignore_ascii_case(c))).collect() +} + /// The colour table's selected colors, by source: `__colors`, else `__color`. pub(super) fn selection(d: &Dialog) -> Vec { let many: Vec = @@ -1298,11 +1305,14 @@ pub(super) fn pick(d: &mut Dialog, order: &[String], color: &str, shift: bool, c /// Delete Color: the selected colors join the color table's `deleted` list and leave its other /// lists. The first deletion fixes the palette (`customPalette`) to `palette`, the table's colors /// (by source) with the deleted ones, so the colors left do not change. A deletion that leaves no -/// color, or only colors mapped to transparent, is refused with nothing changed. The selection is -/// cleared. +/// color, or only colors mapped to transparent, is refused with nothing changed. Only selected +/// colors that `palette` holds are deleted; when it holds none of them, the table is left as it is +/// and the selection is cleared. The selection is cleared after a deletion too. pub(super) fn delete_colors(d: &mut Dialog, palette: &[String]) -> Result<(), String> { - let picked = selection(d); + let picked = selected_in(d, palette); if picked.is_empty() { + d.fields.remove("__colors"); + d.fields.remove("__color"); return Ok(()); } let list = |d: &Dialog, key: &str| -> Vec { diff --git a/crates/ui-egui/src/dialogs/tests_save_for_web.rs b/crates/ui-egui/src/dialogs/tests_save_for_web.rs index b0d3fd6fdd..41894ee6dd 100644 --- a/crates/ui-egui/src/dialogs/tests_save_for_web.rs +++ b/crates/ui-egui/src/dialogs/tests_save_for_web.rs @@ -783,6 +783,26 @@ fn colour_table_selection_and_delete_color() { assert_eq!((&d.fields["colorTable"]["deleted"], &d.fields["colorTable"]["customPalette"]), (&json!([]), &json!([]))); } +/// Delete Color takes only selected colors the table shows: a selection the table no longer holds +/// (after a change of reduction or color count, or set through `ui.dialog.set`) deletes nothing, +/// fixes no palette and is cleared, and a selection partly in the table deletes only what it shows. +#[test] +fn delete_color_skips_selected_colors_the_table_does_not_show() { + let mut d = Dialog::new(save_for_web::KIND, json!({})); + let shown: Vec = ["#000000", "#111111"].map(String::from).to_vec(); + let table = |d: &Dialog, key: &str| d.fields.get("colorTable").and_then(|t| t.get(key)).cloned(); + d.fields.insert("__colors".into(), json!(["#ABCDEF"])); + d.fields.insert("__color".into(), json!("#abcdef")); + assert!(save_for_web::selected_in(&d, &shown).is_empty()); + save_for_web::delete_colors(&mut d, &shown).unwrap(); + assert_eq!((table(&d, "customPalette"), table(&d, "deleted")), (None, None)); + assert!(!d.fields.contains_key("__colors") && !d.fields.contains_key("__color")); + d.fields.insert("__colors".into(), json!(["#abcdef", "#111111"])); + assert_eq!(save_for_web::selected_in(&d, &shown), ["#111111"]); + save_for_web::delete_colors(&mut d, &shown).unwrap(); + assert_eq!((table(&d, "customPalette"), table(&d, "deleted")), (Some(json!(shown)), Some(json!(["#111111"])))); +} + /// A shift by hand replaces a web shift and selects the color; a shift to the color itself takes it /// away; Unshift All Colors clears both kinds; Delete Color takes a deleted color's shift away. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index 370e9584f6..5633e11788 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9048,7 +9048,9 @@ at most 256 colors, and `#rrggbbaa` is refused. A `reduction` change without `co both lists. JPEG and PNG-24 have no color table and do not read them. Presets and `webExport.settings` keep them. In the dialog a click selects one color, Shift-click selects the colors between it and the last one clicked, and Ctrl-click (Command-click on macOS) adds or removes one (`__colors` holds the selection, `__color` the last one -clicked). The trash button and Delete Color in the table's menu delete the selection in one edit, Restore Generated +clicked). The trash button and Delete Color in the table's menu delete the selected colors the table shows in one edit, +and are off when it shows none of them (a selected color can leave the table when the reduction or the color count +changes). Restore Generated Table in that menu clears `deleted` and `customPalette`, and the table reads Custom while either is set. Choosing another reduction in the dialog also clears them.