Format: OK in the Font dialog keeps a preset or system text color

The Font dialog got a preset text color as its bare name, which dropped
its transforms, and a system color as its last RGB value, which dropped
the system color. Pressing OK then replaced the run's color: a preset
"red" at 50% luminance came back as plain red.

`color_command_value` now writes `{"preset": name}` and
`{"system": name, "last": "#RRGGBB"}` with the transforms, and
`color_value` reads both forms. The dialog's Color field shows the
preset's name or the system color's last value.
This commit is contained in:
dotCooCoo
2026-10-24 20:22:00 -07:00
parent 6a1c7354c1
commit 78b64ed2fc
4 changed files with 65 additions and 3 deletions
+27 -2
View File
@@ -597,8 +597,15 @@ fn color_command_value(c: &deckcraft_model::ColorRef) -> Value {
o.insert("scheme".into(), json!(slot.xml_name()));
Value::Object(o)
}
ColorBase::Preset { name } => json!(name),
ColorBase::System { last, .. } => json!(format!("#{}", last.hex())),
ColorBase::Preset { name } => {
o.insert("preset".into(), json!(name));
Value::Object(o)
}
ColorBase::System { name, last } => {
o.insert("system".into(), json!(name));
o.insert("last".into(), json!(format!("#{}", last.hex())));
Value::Object(o)
}
}
}
@@ -1497,6 +1504,24 @@ mod tests {
assert!(r.size.is_none() && r.fill.is_none());
}
#[test]
fn font_dialog_ok_keeps_preset_and_system_colors() {
let preset = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }.with(ColorTransform::LumMod(50_000));
let system =
ColorRef { base: ColorBase::System { name: "windowText".into(), last: deckcraft_color::Rgba::rgb(0x12, 0x34, 0x56) }, mods: vec![] }
.with(ColorTransform::Alpha(60_000));
for color in [preset, system] {
let mut s = shape();
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"]})).unwrap();
assert_eq!(props(&s).fill, Some(Fill::Solid { color }), "{}", params["color"]);
}
}
#[test]
fn font_dialog_with_no_fields_asks_the_ui_to_open() {
let mut s = shape();
+7
View File
@@ -371,6 +371,8 @@ pub(crate) fn ok() -> Result<Value> {
}
/// Parse a colour param: `"#RRGGBB"`, `"accent1"`…, or `{"scheme":"accent2","lumMod":75000}`.
/// A preset or system colour keeps its base: `{"preset":"red"}`,
/// `{"system":"windowText","last":"#000000"}`.
pub(crate) fn color_param(p: &Value, key: &str) -> Option<deckcraft_model::ColorRef> {
let v = p.get(key)?;
color_value(v)
@@ -391,6 +393,11 @@ pub(crate) fn color_value(v: &Value) -> Option<deckcraft_model::ColorRef> {
Value::Object(o) => {
let mut c = if let Some(s) = o.get("scheme").and_then(Value::as_str) {
ColorRef::scheme(deckcraft_color::SchemeSlot::from_xml(s)?)
} else if let Some(name) = o.get("preset").and_then(Value::as_str) {
ColorRef { base: deckcraft_model::ColorBase::Preset { name: name.to_string() }, mods: vec![] }
} else if let Some(name) = o.get("system").and_then(Value::as_str) {
let last = deckcraft_color::Rgba::from_hex(o.get("last").and_then(Value::as_str)?)?;
ColorRef { base: deckcraft_model::ColorBase::System { name: name.to_string(), last }, mods: vec![] }
} else {
ColorRef::rgb(deckcraft_color::Rgba::from_hex(o.get("rgb").and_then(Value::as_str)?)?)
};
+1 -1
View File
@@ -2798,7 +2798,7 @@ fn json_text(v: Option<&Value>, default: &str) -> String {
fn color_label(v: &Value) -> String {
match v {
Value::String(s) => s.clone(),
Value::Object(o) => o.get("scheme").or_else(|| o.get("rgb")).and_then(Value::as_str).unwrap_or("").to_string(),
Value::Object(o) => ["scheme", "rgb", "preset", "last"].iter().find_map(|k| o.get(*k)).and_then(Value::as_str).unwrap_or("").to_string(),
_ => String::new(),
}
}
+30
View File
@@ -63,3 +63,33 @@ fn command_t_opens_the_font_dialog_and_cancel_keeps_the_text() {
assert!(h.state().dialog.is_none());
assert_eq!(bold_of(&h), None);
}
#[test]
fn preset_and_system_colors_show_in_the_dialog_and_ok_keeps_them() {
for (color, shown) in
[(json!({"preset": "red", "lumMod": 50000}), "red"), (json!({"system": "windowText", "last": "#123456", "alpha": 60000}), "#123456")]
{
let mut h = harness();
h.state_mut().session.execute("format.fontDialog", &json!({ "color": color })).unwrap();
let before = fill_of(&h);
assert!(before.is_some());
h.get_by_label("Font…").click();
h.run_steps(2);
let d = h.state().dialog.as_ref().expect("font dialog");
assert_eq!(d.fields.get("color").map(String::as_str), Some(shown));
h.get_by_label("OK").click_accesskit();
h.run_steps(1);
assert!(h.state().dialog.is_none(), "status {:?}", h.state().status);
assert_eq!(fill_of(&h), before, "{shown}");
}
}
fn fill_of(h: &Harness<'_, SlideApp>) -> Option<deckcraft_model::Fill> {
let st = h.state().session.active().unwrap();
st.selected_shapes()
.first()
.and_then(|sh| sh.text.as_ref())
.and_then(|t| t.paragraphs.first())
.and_then(|p| p.runs.first())
.and_then(|r| r.props.fill.clone())
}