Save for Web: Delete Color skips selected colors the table no longer shows

A selection made before the reduction or the color count changed, or set
through ui.dialog.set, could name a color the table no longer holds. Delete
Color stayed enabled for it, added the absent color to colorTable.deleted
and fixed the table as a Custom palette although no shown color was
deleted. Delete Color is now enabled only for selected colors the table
shows and deletes only those; a selection with none of them is cleared and
the table is left unchanged.
This commit is contained in:
dotCooCoo
2026-11-17 17:56:55 -08:00
parent f6c512920a
commit 5ac41ee089
3 changed files with 41 additions and 9 deletions
+18 -8
View File
@@ -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<String> =
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<String> =
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<String> {
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<String> {
let many: Vec<String> =
@@ -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<String> {
@@ -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<String> = ["#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]
+3 -1
View File
@@ -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.