Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

KEYC-METADATA: keyword.setShortcut refuses non-text keywords, and long names leave room for the marks

dotCooCooc41108775f60
diff --git a/crates/engine/src/cmd/keywords.rs b/crates/engine/src/cmd/keywords.rs
index e3f832e2e..b77bba8de 100644
--- a/crates/engine/src/cmd/keywords.rs
+++ b/crates/engine/src/cmd/keywords.rs
@@ -144,12 +144,16 @@ pub fn slots(typed: &[String]) -> Vec<String> {
/// cleaned, a new name goes inside the default parent as the Keywording box puts it, and each is
/// kept once whatever its case; empty ones are dropped. The text Set Keyword Shortcut… shows (the
/// keyword shortcut's keywords joined with ", ") keeps them as they are, commas in a name included.
-fn shortcut_keywords(s: &Session, p: &Value) -> Vec<String> {
+/// Any other value is an error, and the keyword shortcut is left as it was.
+fn shortcut_keywords(s: &Session, p: &Value) -> Result<Vec<String>> {
let now = s.catalog.keyword_shortcut();
+ let invalid = || bad("keyword.setShortcut", "`keywords` must be a string or an array of strings");
let typed: Vec<String> = match p.get("keywords") {
- Some(Value::String(text)) if !now.is_empty() && text.trim() == now.join(", ") => return now,
+ Some(Value::String(text)) if !now.is_empty() && text.trim() == now.join(", ") => return Ok(now),
Some(Value::String(text)) => text.split(',').map(str::to_string).collect(),
- _ => strs(p, "keywords"),
+ Some(Value::Array(a)) => a.iter().map(|k| k.as_str().map(str::to_string).ok_or_else(invalid)).collect::<Result<_>>()?,
+ None | Some(Value::Null) => Vec::new(),
+ Some(_) => return Err(invalid()),
};
let mut out: Vec<String> = Vec::new();
for k in typed {
@@ -158,7 +162,7 @@ fn shortcut_keywords(s: &Session, p: &Value) -> Vec<String> {
out.push(k);
}
}
- out
+ Ok(out)
}
/// The nine keywords ⌥1–⌥9 apply: the current set's, or the recent ones.
@@ -343,7 +347,7 @@ pub fn specs() -> Vec<CommandSpec> {
"{keywords: [..] or \"a, b\" (none clears it)}: sets the keyword shortcut, a mark on its keywords in the keyword list (a new name is listed; one undo step) → {keywords}",
always,
|s, p| {
- let op = s.catalog.set_keyword_shortcut_ops(&shortcut_keywords(s, p));
+ let op = s.catalog.set_keyword_shortcut_ops(&shortcut_keywords(s, p)?);
commit_keywords(s, "Set Keyword Shortcut", op, |k| Some(k.to_string()))?;
Ok(json!({"keywords": s.catalog.keyword_shortcut()}))
}
diff --git a/crates/engine/src/tests_keyword_list.rs b/crates/engine/src/tests_keyword_list.rs
index ec85f7653..b2b2414e4 100644
--- a/crates/engine/src/tests_keyword_list.rs
+++ b/crates/engine/src/tests_keyword_list.rs
@@ -229,6 +229,19 @@ fn the_keyword_shortcut_keeps_a_name_with_a_comma() {
assert_eq!(r["keywords"], json!(["France", "Paris"]), "changed text is split at its commas");
}
+/// A `keywords` value that is neither text nor a list of text is an error, and the keyword
+/// shortcut keeps its keywords.
+#[test]
+fn the_keyword_shortcut_refuses_keywords_that_are_not_text() {
+ let mut s = Session::with_demo();
+ s.execute("keyword.setShortcut", &json!({"keywords": ["beach", "sunset"]})).unwrap();
+ for keywords in [json!(7), json!(["beach", 7]), json!({"name": "beach"})] {
+ let e = s.execute("keyword.setShortcut", &json!({"keywords": keywords})).unwrap_err().to_string();
+ assert!(e.contains("`keywords` must be a string or an array of strings"), "{keywords}: {e}");
+ assert_eq!(s.execute("keyword.shortcut", &json!({})).unwrap()["keywords"], json!(["beach", "sunset"]), "{keywords}");
+ }
+}
+
/// The keyword shortcut follows its keywords when they are moved, renamed or merged, and loses one
/// that is deleted; setting it is one undo step, and undo brings a deleted one back.
#[test]
diff --git a/crates/ui-egui/src/panels/keyword_list.rs b/crates/ui-egui/src/panels/keyword_list.rs
index 24c1a61b3..c2a51d525 100644
--- a/crates/ui-egui/src/panels/keyword_list.rs
+++ b/crates/ui-egui/src/panels/keyword_list.rs
@@ -270,22 +270,25 @@ fn row(app: &mut LightcraftApp, ui: &mut egui::Ui, r: &Row, selection: &[PhotoId
super::left::browse_all_photos(app, true);
let _ = app.run("library.filter", json!({"keyword": r.path}));
}
- // the name, cut short before the arrow
- let room = (arrow.left() - 4.0 - (x + 34.0)).max(0.0);
+ // the name, cut short before the arrow, leaving room for the dot and the + that follow it
+ let is_default = app.session.catalog.default_keyword_parent().as_deref().is_some_and(|k| same(k, &r.path));
+ let in_shortcut = app.session.catalog.keyword_info(&r.path).is_some_and(|i| i.keyword_shortcut);
let font = t.font(13.0);
let measure = |s: &str| ui.painter().layout_no_wrap(s.to_string(), font.clone(), t.text_label).size().x;
+ let marks = if is_default { 10.0 } else { 0.0 } + if in_shortcut { 5.0 + measure("+") } else { 0.0 };
+ let room = (arrow.left() - 4.0 - (x + 34.0) - marks).max(0.0);
let name = if measure(&r.name) <= room { r.name.clone() } else { crate::widgets::elide_head(&r.name, room, measure) };
let label = ui.painter().text(pos2(x + 34.0, cy), Align2::LEFT_CENTER, name, font.clone(), if picked { t.text } else { t.text_label });
// where new keywords go (Put New Keywords Inside This Keyword): a dot after the name
let mut after = label.right();
- if app.session.catalog.default_keyword_parent().as_deref().is_some_and(|k| same(k, &r.path)) {
+ if is_default {
let dot = Rect::from_center_size(pos2(label.right() + 7.0, cy), vec2(6.0, 6.0));
ui.painter().circle_filled(dot.center(), 3.0, t.accent);
register(ui, format!("keywordDefault:{}", r.path), dot);
after = dot.right();
}
// part of the keyword shortcut (⇧K adds it): a + after the name
- if app.session.catalog.keyword_info(&r.path).is_some_and(|i| i.keyword_shortcut) {
+ if in_shortcut {
let plus = ui.painter().text(pos2(after + 5.0, cy), Align2::LEFT_CENTER, "+", font.clone(), t.accent);
register(ui, format!("keywordShortcut:{}", r.path), plus);
ui.interact(plus, egui::Id::unique(("keyword-list-shortcut", r.path.to_lowercase())), Sense::hover())
diff --git a/crates/ui-egui/src/tests_keyword_list.rs b/crates/ui-egui/src/tests_keyword_list.rs
index 6d797e84b..69a9c2cbf 100644
--- a/crates/ui-egui/src/tests_keyword_list.rs
+++ b/crates/ui-egui/src/tests_keyword_list.rs
@@ -258,6 +258,23 @@ fn the_keyword_list_sets_and_marks_the_keyword_shortcut() {
assert!(!has(&h, "keywordShortcut:Weddings"), "no longer in the keyword shortcut");
}
+/// A name too long for its row is cut short before the dot and the +, so both stay clear of the
+/// Show photos arrow.
+#[test]
+fn a_long_keyword_name_leaves_room_for_its_marks() {
+ let (mut h, _) = keywords_panel();
+ let name = "Weddings and receptions at the harbor in the summer of the year before last";
+ run(&mut h, "keyword.create", json!({"name": name}));
+ run(&mut h, "keyword.setDefaultParent", json!({"keyword": name}));
+ run(&mut h, "keyword.setShortcut", json!({"keywords": [name]}));
+ find(&mut h, "receptions");
+ let arrow = rect(&h, &format!("keywordShow:{name}"));
+ for mark in ["keywordDefault", "keywordShortcut"] {
+ let r = rect(&h, &format!("{mark}:{name}"));
+ assert!(r.right() <= arrow.left(), "{mark} {r:?} runs into the arrow {arrow:?}");
+ }
+}
+
/// With a keyword shortcut set, a photo's context menu offers Add Keyword with its keywords, which
/// gives them to the selected photos; with none it offers nothing.
#[test]