mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 10:13:09 -06:00
Format: OK with the Font dialog's Color untouched keeps the stored color
The Font dialog's OK sent the `color` it was opened with back to `format.fontDialog`. For a preset or system name that `color_value` does not know, that color is the RGB it is drawn with, so OK replaced the name even when only another field changed. A color with more than 256 transforms made OK fail with "invalid `color`", so the other edits were not applied either. OK now sends no `color` when the Color field is untouched, and the text keeps its color reference as stored. An edited Color field is sent as before. Tests: a UI test opens the dialog on an unknown preset with 257 transforms and on an unknown system color, ticks Bold and presses OK; an engine test sends the dialog's fields without `color` and checks that the stored color, size and bold are kept.
This commit is contained in:
@@ -431,20 +431,28 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_dialog_ok_accepts_preset_and_system_names_it_does_not_know() {
|
||||
fn font_dialog_ok_without_color_keeps_colors_a_command_cannot_give() {
|
||||
use deckcraft_color::Rgba;
|
||||
let last = Rgba::rgb(0x12, 0x34, 0x56);
|
||||
let preset = ColorRef { base: ColorBase::Preset { name: "notAColor".into() }, mods: vec![] }.with(ColorTransform::LumMod(50_000));
|
||||
let system = ColorRef { base: ColorBase::System { name: "notAColor".into(), last }, mods: vec![] };
|
||||
for (color, drawn) in [(preset, ColorRef::rgb(Rgba::BLACK).with(ColorTransform::LumMod(50_000))), (system, ColorRef::rgb(last))] {
|
||||
// More transforms than a command's `mods` list may carry, as an opened file can hold.
|
||||
let many = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![ColorTransform::Inv; crate::cmd::MAX_COLOR_MODS + 1] };
|
||||
for color in [preset, system, many] {
|
||||
let mut s = shape();
|
||||
let fill = Fill::Solid { color };
|
||||
let fill = Fill::Solid { color: color.clone() };
|
||||
format_run(&mut s, &|r| r.fill = Some(fill.clone())).unwrap();
|
||||
s.ui_requests.clear();
|
||||
s.execute("format.fontDialog", &json!({})).unwrap();
|
||||
let Some(UiRequest::Dialog { params, .. }) = s.ui_requests.last().cloned() else { panic!("dialog request") };
|
||||
s.execute("format.fontDialog", &json!({"color": params["color"], "size": 20.0})).unwrap();
|
||||
assert_eq!(props(&s).fill, Some(Fill::Solid { color: drawn }), "{}", params["color"]);
|
||||
let Some(UiRequest::Dialog { mut params, .. }) = s.ui_requests.last().cloned() else { panic!("dialog request") };
|
||||
// OK with the Color field untouched and another field edited, as the dialog sends it.
|
||||
params.as_object_mut().unwrap().remove("color");
|
||||
params["size"] = json!(20.0);
|
||||
params["bold"] = json!(true);
|
||||
s.execute("format.fontDialog", ¶ms).unwrap();
|
||||
let r = props(&s);
|
||||
assert_eq!((r.size, r.bold), (Some(20.0), Some(true)));
|
||||
assert_eq!(r.fill, Some(Fill::Solid { color }));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -450,7 +450,8 @@ fn color_command_value(c: &deckcraft_model::ColorRef) -> Value {
|
||||
// `#RRGGBB`, or `#RRGGBBAA` for a color that is not opaque.
|
||||
let hex = |c: &deckcraft_color::Rgba| if c.a == 255 { format!("#{}", c.hex()) } else { format!("#{}{:02X}", c.hex(), c.a) };
|
||||
// A preset or system name that `color_value` does not know (kept from an opened file) is given as
|
||||
// the color it is drawn with, so OK does not reject it.
|
||||
// the color it is drawn with, so a caller that sends this `color` back gets a valid color. The
|
||||
// Font dialog sends no `color` when its Color field is untouched, so the run keeps the stored name.
|
||||
let base = match &c.base {
|
||||
ColorBase::Preset { name } if !deckcraft_color::is_preset_name(name) => ColorBase::Rgb { rgb: deckcraft_color::Rgba::BLACK },
|
||||
ColorBase::System { name, last } if !deckcraft_color::is_system_name(name) => ColorBase::Rgb { rgb: *last },
|
||||
|
||||
@@ -147,13 +147,9 @@ pub(super) fn font_dialog(app: &mut SlideApp, ui: &mut Ui, d: &mut Dialog) -> bo
|
||||
"caps": d.get("caps", "none"),
|
||||
"spacing": spacing,
|
||||
});
|
||||
// An untouched Color field sends no `color`, so the text keeps its color reference as stored.
|
||||
let color = d.get("color", "");
|
||||
let seed = d.get("colorSeed", "");
|
||||
if color == seed {
|
||||
if let Some(c) = d.params.get("color").filter(|v| !v.is_null()) {
|
||||
params["color"] = c.clone();
|
||||
}
|
||||
} else if !color.trim().is_empty() {
|
||||
if color != d.get("colorSeed", "") && !color.trim().is_empty() {
|
||||
params["color"] = json!(color.trim());
|
||||
}
|
||||
let _ = app.run("format.fontDialog", params);
|
||||
|
||||
@@ -87,6 +87,43 @@ fn preset_and_system_colors_show_in_the_dialog_and_ok_keeps_them() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ok_with_color_untouched_keeps_a_color_the_dialog_cannot_send_back() {
|
||||
use deckcraft_color::{ColorTransform, Rgba};
|
||||
use deckcraft_model::{ColorBase, ColorRef, Fill};
|
||||
// Names that `color_value` does not know, as an opened file can hold, and more transforms than a
|
||||
// command's `mods` list may carry.
|
||||
let unknown_preset = ColorRef { base: ColorBase::Preset { name: "notAColor".into() }, mods: vec![ColorTransform::Inv; 257] };
|
||||
let unknown_system = ColorRef { base: ColorBase::System { name: "notAColor".into(), last: Rgba::rgb(0x12, 0x34, 0x56) }, mods: vec![] };
|
||||
for color in [unknown_preset, unknown_system] {
|
||||
let mut h = harness();
|
||||
set_fill(&mut h, Fill::Solid { color });
|
||||
let before = fill_of(&h);
|
||||
h.get_by_label("Font…").click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().dialog.as_ref().map(|d| d.id.as_str()), Some("font"));
|
||||
h.get_by_label("Bold").click_accesskit();
|
||||
h.run_steps(1);
|
||||
h.get_by_label("OK").click_accesskit();
|
||||
h.run_steps(1);
|
||||
assert!(h.state().dialog.is_none(), "status {:?}", h.state().status);
|
||||
assert_eq!(bold_of(&h), Some(true), "status {:?}", h.state().status);
|
||||
assert_eq!(fill_of(&h), before);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the first run's fill directly, so the test can hold a color that a command would reject.
|
||||
fn set_fill(h: &mut Harness<'_, SlideApp>, fill: deckcraft_model::Fill) {
|
||||
let st = h.state_mut().session.active_mut().unwrap();
|
||||
let id = st.selection.shapes[0];
|
||||
let slide = st.selection.slide;
|
||||
let doc = std::sync::Arc::make_mut(&mut st.doc);
|
||||
let slide = std::sync::Arc::make_mut(&mut doc.slides[slide]);
|
||||
let shape = slide.shapes.iter_mut().find(|sh| sh.id == id).unwrap();
|
||||
let run = shape.text.as_mut().and_then(|t| t.paragraphs.first_mut()).and_then(|p| p.runs.first_mut()).unwrap();
|
||||
run.props.fill = Some(fill);
|
||||
}
|
||||
|
||||
fn fill_of(h: &Harness<'_, SlideApp>) -> Option<deckcraft_model::Fill> {
|
||||
let st = h.state().session.active().unwrap();
|
||||
st.selected_shapes()
|
||||
|
||||
Reference in New Issue
Block a user