mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 10:13:09 -06:00
Format: the Font dialog keeps the order of a color's transforms
The dialog's color parameter lists transforms in order under `mods`, in the form the model stores them, so OK keeps repeats such as [inv, inv] and transforms that are not in alphabetical order. Color parameters accept `mods` alongside the keyed form.
This commit is contained in:
@@ -590,9 +590,9 @@ fn font_edits(p: &Value) -> Result<FontEdits> {
|
||||
fn color_command_value(c: &deckcraft_model::ColorRef) -> Value {
|
||||
use deckcraft_model::ColorBase;
|
||||
let mut o = serde_json::Map::new();
|
||||
for m in &c.mods {
|
||||
// `inv`, `comp` and `gray` have no value.
|
||||
o.insert(m.xml_name().to_string(), m.value().map_or(json!(true), |v| json!(v)));
|
||||
// An ordered list, so the order of the transforms and repeated ones such as `[inv, inv]` are kept.
|
||||
if !c.mods.is_empty() {
|
||||
o.insert("mods".into(), json!(c.mods));
|
||||
}
|
||||
// `#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) };
|
||||
@@ -1556,7 +1556,10 @@ mod tests {
|
||||
let inverted = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }.with(ColorTransform::Inv);
|
||||
let translucent = ColorRef { base: ColorBase::System { name: "windowText".into(), last: Rgba::rgba(0x12, 0x34, 0x56, 0x80) }, mods: vec![] };
|
||||
let rgb = ColorRef::rgb(Rgba::rgba(0x12, 0x34, 0x56, 0x80));
|
||||
for color in [preset, system, inverted, translucent, rgb] {
|
||||
// A repeated transform, and two transforms in an order that is not alphabetical.
|
||||
let twice = inverted.clone().with(ColorTransform::Inv);
|
||||
let ordered = ColorRef::scheme(SchemeSlot::Accent2).with(ColorTransform::LumOff(20_000)).with(ColorTransform::LumMod(50_000));
|
||||
for color in [preset, system, inverted, translucent, rgb, twice, ordered] {
|
||||
let mut s = shape();
|
||||
let fill = Fill::Solid { color: color.clone() };
|
||||
format_run(&mut s, &|r| r.fill = Some(fill.clone())).unwrap();
|
||||
|
||||
@@ -372,7 +372,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"}`.
|
||||
/// `{"system":"windowText","last":"#000000"}`. `"mods"` lists transforms in order:
|
||||
/// `{"preset":"red","mods":[{"op":"inv"},{"op":"inv"}]}`.
|
||||
pub(crate) fn color_param(p: &Value, key: &str) -> Option<deckcraft_model::ColorRef> {
|
||||
let v = p.get(key)?;
|
||||
color_value(v)
|
||||
@@ -412,6 +413,10 @@ pub(crate) fn color_value(v: &Value) -> Option<deckcraft_model::ColorRef> {
|
||||
c.mods.push(t);
|
||||
}
|
||||
}
|
||||
// `mods` lists transforms in order, repeats included: `[{"op":"inv"},{"op":"lumMod","val":50000}]`.
|
||||
if let Some(list) = o.get("mods") {
|
||||
c.mods.extend(serde_json::from_value::<Vec<deckcraft_color::ColorTransform>>(list.clone()).ok()?);
|
||||
}
|
||||
Some(c)
|
||||
}
|
||||
_ => None,
|
||||
@@ -669,5 +674,9 @@ mod tests {
|
||||
assert!(color_value(&json!({"preset": name})).is_some(), "{name}");
|
||||
}
|
||||
assert!(color_value(&json!({"system": "3dDkShadow", "last": "#000000"})).is_some());
|
||||
let listed = color_value(&json!({"preset": "red", "mods": [{"op": "inv"}, {"op": "lumOff", "val": 20_000}, {"op": "inv"}]}));
|
||||
let mods = vec![ColorTransform::Inv, ColorTransform::LumOff(20_000), ColorTransform::Inv];
|
||||
assert_eq!(listed, Some(ColorRef { base: ColorBase::Preset { name: "red".into() }, mods }));
|
||||
assert_eq!(color_value(&json!({"preset": "red", "mods": [{"op": "notATransform"}]})), None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,9 +66,12 @@ fn command_t_opens_the_font_dialog_and_cancel_keeps_the_text() {
|
||||
|
||||
#[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")]
|
||||
{
|
||||
for (color, shown) in [
|
||||
(json!({"preset": "red", "lumMod": 50000}), "red"),
|
||||
(json!({"system": "windowText", "last": "#123456", "alpha": 60000}), "#123456"),
|
||||
(json!({"preset": "red", "mods": [{"op": "inv"}, {"op": "inv"}]}), "red"),
|
||||
(json!({"scheme": "accent2", "mods": [{"op": "lumOff", "val": 20000}, {"op": "lumMod", "val": 50000}]}), "accent2"),
|
||||
] {
|
||||
let mut h = harness();
|
||||
h.state_mut().session.execute("format.fontDialog", &json!({ "color": color })).unwrap();
|
||||
let before = fill_of(&h);
|
||||
|
||||
Reference in New Issue
Block a user