mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-29 21:58:03 +00:00
Merge pull request #2922 from OneSlips/pr/keyword-list-lines
LRC-LIB-KEYWORDS: a keyword list written as one metadata entry is read as one keyword a line
This commit is contained in:
@@ -86,17 +86,39 @@ pub fn to_file(keywords: &[String]) -> FileKeywords {
|
||||
/// A file's keywords as a photo carries them: the paths of `lr:hierarchicalSubject`, then the
|
||||
/// names of `dc:subject` that aren't levels of those paths (Lightroom Classic writes each keyword's
|
||||
/// name, its parents' and synonyms flat besides its path). Each once, cleaned.
|
||||
///
|
||||
/// An entry holding several lines is several keywords: some cameras' metadata presets write the
|
||||
/// whole list as one `dc:subject` item, a keyword a line ("Beautiful Bride
 Bride and
|
||||
/// Groom…"). Read as one, it would be a single keyword many lines tall.
|
||||
pub fn from_file(flat: &[String], hierarchical: &[String]) -> Vec<String> {
|
||||
let mut out: Vec<String> = hierarchical.iter().map(|k| clean(k)).collect();
|
||||
let lines = |v: &[String]| -> Vec<String> { v.iter().flat_map(|k| split_lines(k)).collect() };
|
||||
let (flat, hierarchical) = (lines(flat), lines(hierarchical));
|
||||
let mut out: Vec<String> = hierarchical.iter().map(|k| clean(k)).filter(|k| !k.is_empty()).collect();
|
||||
// One lowered set of every hierarchical level, not a search of `hierarchical` for each flat
|
||||
// keyword: disjoint lists from sidecars made the old approach quadratic in both (CWE-407), and
|
||||
// `same` allocates two lower-cased strings per comparison.
|
||||
let levels: std::collections::HashSet<String> = hierarchical.iter().flat_map(|h| h.split(SEP).map(|l| l.trim().to_lowercase())).collect();
|
||||
out.extend(flat.iter().map(|k| clean(k)).filter(|k| !levels.contains(&k.to_lowercase())));
|
||||
out.extend(flat.iter().map(|k| clean(k)).filter(|k| !k.is_empty()).filter(|k| !levels.contains(&k.to_lowercase())));
|
||||
dedupe(&mut out);
|
||||
out
|
||||
}
|
||||
|
||||
/// The keywords of one metadata entry: one a line (any of `\r`, `\n`, the Unicode line and
|
||||
/// paragraph separators), blank lines dropped.
|
||||
pub fn split_lines(k: &str) -> Vec<String> {
|
||||
k.split(['\r', '\n', '\u{2028}', '\u{2029}']).map(str::trim).filter(|l| !l.is_empty()).map(str::to_string).collect()
|
||||
}
|
||||
|
||||
/// `text` on one line: line breaks become spaces (a name drawn in a row must never spill over
|
||||
/// the rows around it).
|
||||
pub fn one_line(text: &str) -> std::borrow::Cow<'_, str> {
|
||||
if text.contains(['\r', '\n', '\u{2028}', '\u{2029}']) {
|
||||
std::borrow::Cow::Owned(split_lines(text).join(" "))
|
||||
} else {
|
||||
std::borrow::Cow::Borrowed(text)
|
||||
}
|
||||
}
|
||||
|
||||
/// The deepest a keyword list file may nest keywords: a file is input, so its nesting is bounded.
|
||||
pub const MAX_LEVELS: usize = 64;
|
||||
|
||||
|
||||
@@ -1,6 +1,18 @@
|
||||
use super::*;
|
||||
use crate::{Filter, Photo, PhotoId, Source};
|
||||
|
||||
/// A camera preset's keyword list written as one `dc:subject` item, a keyword a line, is one
|
||||
/// keyword a line; names stay one line wherever they are drawn.
|
||||
#[test]
|
||||
fn a_list_in_one_entry_is_a_keyword_a_line() {
|
||||
let flat = vec!["Beautiful Bride\r\n Bride and Groom\r\n\r\n Happy Couple".to_string(), "Solo".to_string()];
|
||||
assert_eq!(from_file(&flat, &[]), vec!["Beautiful Bride", "Bride and Groom", "Happy Couple", "Solo"]);
|
||||
assert_eq!(from_file(&["a\nb".to_string()], &["x|a".to_string()]), vec!["x|a", "b"], "a level of a path isn't listed twice");
|
||||
assert_eq!(one_line("a\r\n b"), "a b");
|
||||
assert!(matches!(one_line("plain"), std::borrow::Cow::Borrowed(_)));
|
||||
assert!(split_lines("\r\n").is_empty());
|
||||
}
|
||||
|
||||
fn lib(kws: &[&[&str]]) -> (Catalog, Vec<PhotoId>) {
|
||||
let mut c = Catalog::new();
|
||||
let mut ids = Vec::new();
|
||||
|
||||
@@ -758,6 +758,13 @@ impl Session {
|
||||
lib.forgot_local = Some(plan);
|
||||
}
|
||||
}
|
||||
// keywords read whole from a list a line long (before they were split) become one each
|
||||
let journaled = self.pending_log.len();
|
||||
if (self.split_multiline_keywords() > 0 || self.pending_log.len() > journaled)
|
||||
&& let Err(e) = self.persist()
|
||||
{
|
||||
log::error!("library: {e}");
|
||||
}
|
||||
// smart previews written by an older camera-look fit are brought up to date in the background
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if on_disk {
|
||||
@@ -770,6 +777,68 @@ impl Session {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Photos whose keywords hold line breaks (a camera preset's whole keyword list read as one
|
||||
/// keyword by an earlier version) get one keyword a line, journaled, not an undo step. Returns
|
||||
/// how many photos changed. A listed keyword with line breaks hands its options to the names it
|
||||
/// splits into (a name listed already keeps its own, but is left out of export when either was),
|
||||
/// and with automatic XMP writing on, the repaired photos' sidecars are written as a command's are.
|
||||
pub fn split_multiline_keywords(&mut self) -> usize {
|
||||
let breaks = |k: &str| k.contains(['\r', '\n', '\u{2028}', '\u{2029}']);
|
||||
let log_start = self.pending_log.len();
|
||||
let listed: Vec<(String, lightcraft_catalog::keywords::KeywordInfo)> =
|
||||
self.catalog.listed_keywords().filter(|k| breaks(&k.path)).map(|k| (k.path.clone(), k.info.clone())).collect();
|
||||
for (path, info) in listed {
|
||||
let mut ops = Vec::new();
|
||||
for (i, name) in lightcraft_catalog::keywords::from_file(std::slice::from_ref(&path), &[]).into_iter().enumerate() {
|
||||
// where new keywords go stays one keyword: the first name it splits into, listed already or not
|
||||
let parent = info.new_keywords_inside && i == 0;
|
||||
let moved = match self.catalog.keyword_info(&name) {
|
||||
Some(kept) => {
|
||||
let mut kept = kept.clone();
|
||||
kept.include_on_export &= info.include_on_export;
|
||||
kept.new_keywords_inside |= parent;
|
||||
kept
|
||||
}
|
||||
None => lightcraft_catalog::keywords::KeywordInfo { new_keywords_inside: parent, ..info.clone() },
|
||||
};
|
||||
ops.push(lightcraft_catalog::Op::SetKeyword { path: name, info: Some(moved) });
|
||||
}
|
||||
ops.push(lightcraft_catalog::Op::SetKeyword { path, info: None });
|
||||
for op in ops {
|
||||
match self.catalog.apply(op.clone()) {
|
||||
Ok(_) => self.pending_log.push(op),
|
||||
Err(e) => log::warn!("split keywords: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
let fix: Vec<(lightcraft_catalog::PhotoId, lightcraft_catalog::Meta)> = self
|
||||
.catalog
|
||||
.photos()
|
||||
.filter(|p| p.meta.keywords.iter().any(|k| breaks(k)))
|
||||
.map(|p| {
|
||||
let mut meta = p.meta.clone();
|
||||
meta.keywords = lightcraft_catalog::keywords::from_file(&meta.keywords, &[]);
|
||||
(p.id, meta)
|
||||
})
|
||||
.collect();
|
||||
let n = fix.len();
|
||||
for (id, meta) in fix {
|
||||
let op = lightcraft_catalog::Op::SetMeta { id, meta: Box::new(meta) };
|
||||
match self.catalog.apply(op.clone()) {
|
||||
Ok(_) => self.pending_log.push(op),
|
||||
Err(e) => log::warn!("split keywords: {e}"),
|
||||
}
|
||||
}
|
||||
if n > 0 {
|
||||
log::info!("split the line-separated keywords of {n} photo(s) into one keyword a line");
|
||||
}
|
||||
// not run through a command, so the sidecars are written here, as after a command
|
||||
if self.xmp.auto_write && self.pending_log.len() > log_start {
|
||||
self.auto_write_sidecars_since(log_start);
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
/// Write pending ops to the log (fsynced, or handed to the log's own thread natively), compact when due, and save changed presets.
|
||||
/// Called after every top-level command; cheap when nothing changed. Fails (with
|
||||
/// [`EngineError::NotSaved`]) only when the queued ops couldn't be written; they stay queued.
|
||||
|
||||
@@ -537,3 +537,73 @@ fn importing_keywords_reads_a_list() {
|
||||
assert!(s.execute("keyword.import", &json!({})).is_err(), "a path or text");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A library whose photos got a whole keyword list as one keyword (line breaks inside) is
|
||||
/// repaired when it opens: one keyword a line, journaled.
|
||||
#[test]
|
||||
fn line_separated_keywords_are_split_when_the_library_opens() {
|
||||
let mut s = Session::with_demo();
|
||||
let id = s.visible_cloned()[0];
|
||||
let mut meta = s.catalog.photo(id).unwrap().meta.clone();
|
||||
meta.keywords = vec!["Bride\r\n Groom\r\n Happy Couple".into()];
|
||||
s.catalog.apply(lightcraft_catalog::Op::SetMeta { id, meta: Box::new(meta) }).unwrap();
|
||||
assert_eq!(s.split_multiline_keywords(), 1);
|
||||
assert_eq!(s.catalog.photo(id).unwrap().meta.keywords, vec!["Bride", "Groom", "Happy Couple"]);
|
||||
assert_eq!(s.split_multiline_keywords(), 0, "nothing left to split");
|
||||
}
|
||||
|
||||
/// A listed keyword with line breaks hands its options to the names it splits into, so a keyword left
|
||||
/// out of export stays out (CodeRabbit on #2922); a name listed already keeps its own options but is
|
||||
/// left out of export when either was.
|
||||
#[test]
|
||||
fn a_split_keyword_keeps_its_export_options() {
|
||||
use lightcraft_catalog::Op;
|
||||
use lightcraft_catalog::keywords::KeywordInfo;
|
||||
let mut s = Session::with_demo();
|
||||
let id = s.visible_cloned()[0];
|
||||
let hidden = KeywordInfo { include_on_export: false, ..KeywordInfo::default() };
|
||||
let groom = KeywordInfo { person: true, ..KeywordInfo::default() };
|
||||
s.catalog.apply(Op::SetKeyword { path: "Bride\nGroom".into(), info: Some(hidden) }).unwrap();
|
||||
s.catalog.apply(Op::SetKeyword { path: "Groom".into(), info: Some(groom) }).unwrap();
|
||||
let mut meta = s.catalog.photo(id).unwrap().meta.clone();
|
||||
meta.keywords = vec!["Bride\nGroom".into()];
|
||||
s.catalog.apply(Op::SetMeta { id, meta: Box::new(meta) }).unwrap();
|
||||
assert_eq!(s.split_multiline_keywords(), 1);
|
||||
let bride = s.catalog.keyword_info("Bride").expect("Bride is listed with the options it came with");
|
||||
assert!(!bride.include_on_export, "{bride:?}");
|
||||
let groom = s.catalog.keyword_info("Groom").unwrap();
|
||||
assert!(!groom.include_on_export && groom.person, "kept its own options, left out of export: {groom:?}");
|
||||
assert!(s.catalog.keyword_info("Bride\nGroom").is_none(), "the multi-line record is gone");
|
||||
}
|
||||
|
||||
/// A split keyword that is where new keywords go hands that to one of its names only, so there is still one
|
||||
/// default parent (CodeRabbit on #2922).
|
||||
#[test]
|
||||
fn a_split_default_parent_stays_one_keyword() {
|
||||
use lightcraft_catalog::Op;
|
||||
use lightcraft_catalog::keywords::KeywordInfo;
|
||||
let mut s = Session::with_demo();
|
||||
let inside = KeywordInfo { new_keywords_inside: true, ..KeywordInfo::default() };
|
||||
s.catalog.apply(Op::SetKeyword { path: "Ceremony\nReception".into(), info: Some(inside) }).unwrap();
|
||||
s.split_multiline_keywords();
|
||||
let parents: Vec<&str> = s.catalog.listed_keywords().filter(|k| k.info.new_keywords_inside).map(|k| k.path.as_str()).collect();
|
||||
assert_eq!(parents, ["Ceremony"]);
|
||||
assert_eq!(s.catalog.default_keyword_parent().as_deref(), Some("Ceremony"));
|
||||
}
|
||||
|
||||
/// The first name a default parent splits into stays the default parent when it is listed already (its other
|
||||
/// options are its own).
|
||||
#[test]
|
||||
fn a_split_default_parent_stays_one_when_its_first_name_is_listed() {
|
||||
use lightcraft_catalog::Op;
|
||||
use lightcraft_catalog::keywords::KeywordInfo;
|
||||
let mut s = Session::with_demo();
|
||||
let own = KeywordInfo { include_on_export: true, ..KeywordInfo::default() };
|
||||
s.catalog.apply(Op::SetKeyword { path: "Ceremony".into(), info: Some(own) }).unwrap();
|
||||
let inside = KeywordInfo { new_keywords_inside: true, ..KeywordInfo::default() };
|
||||
s.catalog.apply(Op::SetKeyword { path: "Ceremony\nReception".into(), info: Some(inside) }).unwrap();
|
||||
s.split_multiline_keywords();
|
||||
let parents: Vec<&str> = s.catalog.listed_keywords().filter(|k| k.info.new_keywords_inside).map(|k| k.path.as_str()).collect();
|
||||
assert_eq!(parents, ["Ceremony"]);
|
||||
assert_eq!(s.catalog.default_keyword_parent().as_deref(), Some("Ceremony"));
|
||||
}
|
||||
|
||||
@@ -263,6 +263,25 @@ fn reading_metadata_leaves_the_sidecar_alone_with_auto_write_on() {
|
||||
let _ = std::fs::remove_dir_all(&src);
|
||||
}
|
||||
|
||||
/// The open-time keyword repair runs outside a command, so it writes the repaired photos' sidecars itself
|
||||
/// when automatic XMP writing is on (CodeRabbit on #2922): another app reading the sidecar sees one keyword a line.
|
||||
#[test]
|
||||
fn the_keyword_repair_writes_the_sidecar_with_auto_write_on() {
|
||||
let src = temp_dir("split-keywords-autowrite");
|
||||
write_png(&src.join("frame.png"), 6);
|
||||
let mut s = Session::new().with_fs();
|
||||
s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
|
||||
s.execute("library.xmpPreferences", &json!({"autoWrite": true})).unwrap();
|
||||
let id = only(&s);
|
||||
let mut meta = s.catalog.photo(id).unwrap().meta.clone();
|
||||
meta.keywords = vec!["Bride\r\nGroom".into()];
|
||||
s.catalog.apply(lightcraft_catalog::Op::SetMeta { id, meta: Box::new(meta) }).unwrap();
|
||||
assert_eq!(s.split_multiline_keywords(), 1);
|
||||
let xmp = std::fs::read_to_string(src.join("frame.xmp")).expect("the sidecar is written");
|
||||
assert!(xmp.contains(">Bride<") && xmp.contains(">Groom<") && !xmp.contains("Bride\r") && !xmp.contains("Bride\n"), "{xmp}");
|
||||
let _ = std::fs::remove_dir_all(&src);
|
||||
}
|
||||
|
||||
/// Issue #934, the round trip the write-back broke: after a read with auto-write on, another app
|
||||
/// changes `crs:Exposure2012` from 0.5 to 1.2 (keeping the rest of the file), and the next read
|
||||
/// takes 1.2.
|
||||
|
||||
@@ -1897,7 +1897,7 @@ fn keyword_tag_body(app: &mut LightcraftApp, ui: &mut egui::Ui, t: &Tokens, conf
|
||||
ui.add_space(6.0);
|
||||
if editing.is_none() {
|
||||
if let Some(p) = parent {
|
||||
let r = ui.checkbox(inside, crate::i18n::tr_format!("Put inside “{parent}”", parent = p.replace('|', " › ")));
|
||||
let r = ui.checkbox(inside, crate::panels::keyword_list::put_inside_label(p));
|
||||
crate::widgets::register(ui, "check:keywordInside", r.rect);
|
||||
}
|
||||
let n = app.session.selection.ids.len();
|
||||
|
||||
@@ -136,7 +136,7 @@ pub fn drag_feedback(app: &mut LightcraftApp, ctx: &egui::Context) {
|
||||
}
|
||||
let frame = ctx.cumulative_frame_nr();
|
||||
let hint = ctx.data(|d| d.get_temp::<(u64, String)>(egui::Id::unique(KEYWORD_HINT))).filter(|(f, _)| *f == frame).map(|(_, text)| text);
|
||||
let name = hint.unwrap_or_else(|| keyword.rsplit('|').next().unwrap_or(&keyword).to_string());
|
||||
let name = hint.unwrap_or_else(|| drag_leaf(&keyword));
|
||||
egui::Area::new(egui::Id::unique("drag-keyword")).order(egui::Order::Tooltip).interactable(false).fixed_pos(at + vec2(14.0, 10.0)).show(
|
||||
ctx,
|
||||
|ui| {
|
||||
@@ -171,12 +171,34 @@ fn drop_keyword(app: &mut LightcraftApp, ctx: &egui::Context, keyword: &str, par
|
||||
}
|
||||
}
|
||||
|
||||
/// The name a dragged keyword's label shows: its last level, on one line.
|
||||
fn drag_leaf(keyword: &str) -> String {
|
||||
spoken_path(keyword.rsplit('|').next().unwrap_or(keyword))
|
||||
}
|
||||
|
||||
/// A keyword's path as it is read out: on one line, as its name is painted.
|
||||
fn spoken_path(path: &str) -> String {
|
||||
lightcraft_catalog::keywords::one_line(path).into_owned()
|
||||
}
|
||||
|
||||
/// A keyword's path as its row's hover text shows it: on one line, levels joined by ›.
|
||||
fn hover_path(path: &str) -> String {
|
||||
spoken_path(path).replace('|', " › ")
|
||||
}
|
||||
|
||||
/// The Create Keyword Tag dialog's "Put inside …" tick box: the parent keyword on one line, levels joined by ›.
|
||||
pub(crate) fn put_inside_label(parent: &str) -> String {
|
||||
crate::i18n::tr_format!("Put inside “{parent}”", parent = hover_path(parent))
|
||||
}
|
||||
|
||||
/// One keyword's row: the triangle, the tick box, the name, the count, and on hover the arrow.
|
||||
/// `can_fold`: the triangle opens and closes the level (not while a filter opens it).
|
||||
fn row(app: &mut LightcraftApp, ui: &mut egui::Ui, r: &Row, selection: &[PhotoId], ticks: &Ticks, can_fold: bool) {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), ROW_H), Sense::click_and_drag());
|
||||
crate::widgets::announce(&resp, r.path.clone());
|
||||
// what is read out and hovered shows the path on one line, as the name is painted
|
||||
let visible_path = spoken_path(&r.path);
|
||||
crate::widgets::announce(&resp, visible_path.clone());
|
||||
register(ui, format!("keywordRow:{}", r.path), rect);
|
||||
if resp.drag_started() {
|
||||
app.ui.dragging_keyword = Some(r.path.clone());
|
||||
@@ -218,7 +240,7 @@ fn row(app: &mut LightcraftApp, ui: &mut egui::Ui, r: &Row, selection: &[PhotoId
|
||||
// (it senses drags too, so a press there that moves doesn't pick the keyword up)
|
||||
let sense = if on { Sense::click_and_drag() } else { Sense::hover() };
|
||||
let tb = ui.interact(boxr.expand(3.0), egui::Id::unique(("keyword-list-tick", r.path.to_lowercase())), sense);
|
||||
crate::widgets::announce(&tb, format!("{} {}", crate::i18n::tr("Keyword"), r.path));
|
||||
crate::widgets::announce(&tb, format!("{} {}", crate::i18n::tr("Keyword"), visible_path));
|
||||
register(ui, format!("keywordCheck:{}", r.path), boxr);
|
||||
let edge = if on { t.text_label } else { t.text_dim.gamma_multiply(0.5) };
|
||||
match state {
|
||||
@@ -284,7 +306,8 @@ fn row(app: &mut LightcraftApp, ui: &mut egui::Ui, r: &Row, selection: &[PhotoId
|
||||
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 flat = lightcraft_catalog::keywords::one_line(&r.name);
|
||||
let name = if measure(&flat) <= room { flat.to_string() } else { crate::widgets::elide_head(&flat, 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();
|
||||
@@ -301,7 +324,7 @@ fn row(app: &mut LightcraftApp, ui: &mut egui::Ui, r: &Row, selection: &[PhotoId
|
||||
ui.interact(plus, egui::Id::unique(("keyword-list-shortcut", r.path.to_lowercase())), Sense::hover())
|
||||
.on_hover_text(crate::i18n::tr("Part of the keyword shortcut"));
|
||||
}
|
||||
let resp = resp.on_hover_text(r.path.replace('|', " › "));
|
||||
let resp = resp.on_hover_text(hover_path(&r.path));
|
||||
if resp.clicked() {
|
||||
app.ui.keyword_list_selected = Some(r.path.clone());
|
||||
}
|
||||
@@ -316,7 +339,7 @@ const INSIDE_NAMED: usize = 4;
|
||||
|
||||
/// "Inside: Rome, Venice": the applied keywords inside a keyword, the first few by name.
|
||||
fn inside_said(inside: &[String]) -> Option<String> {
|
||||
let named = inside.iter().take(INSIDE_NAMED).map(String::as_str).collect::<Vec<_>>().join(", ");
|
||||
let named = inside.iter().take(INSIDE_NAMED).map(|name| spoken_path(name)).collect::<Vec<_>>().join(", ");
|
||||
let more = inside.len().saturating_sub(INSIDE_NAMED);
|
||||
let names = if more > 0 { format!("{named} {}", crate::i18n::tr_format!("+{n} more", n = more)) } else { named };
|
||||
(!inside.is_empty()).then(|| crate::i18n::tr_format!("Inside: {names}", names = names))
|
||||
@@ -370,7 +393,7 @@ fn drop_photos_on_keyword(app: &mut LightcraftApp, ui: &mut egui::Ui, ids: &[u64
|
||||
}
|
||||
return;
|
||||
}
|
||||
let keyword = path.replace('|', " › ");
|
||||
let keyword = hover_path(path);
|
||||
super::grid::drag_hint(
|
||||
ui.ctx(),
|
||||
crate::i18n::tr_format!("Add “{keyword}” to {n} photo{}", if n == 1 { "" } else { "s" }, keyword = keyword, n = n),
|
||||
@@ -384,7 +407,7 @@ fn drop_photos_on_keyword(app: &mut LightcraftApp, ui: &mut egui::Ui, ids: &[u64
|
||||
|
||||
/// Gives the keyword `path` to the dragged photos and toasts how many (`n`) lacked it.
|
||||
fn give_keyword(app: &mut LightcraftApp, ctx: &egui::Context, ids: &[u64], path: &str, n: usize) {
|
||||
let keyword = path.replace('|', " › ");
|
||||
let keyword = hover_path(path);
|
||||
match app.run("photo.setMeta", json!({"ids": ids, "addKeywords": [path]})) {
|
||||
Ok(_) => app.toast(ctx, crate::i18n::tr_format!("Added “{keyword}” to {n} photo{}", if n == 1 { "" } else { "s" }, keyword = keyword, n = n)),
|
||||
Err(e) => app.toast(ctx, e),
|
||||
@@ -393,7 +416,7 @@ fn give_keyword(app: &mut LightcraftApp, ctx: &egui::Context, ids: &[u64], path:
|
||||
|
||||
/// A keyword's context menu.
|
||||
fn menu(app: &mut LightcraftApp, ui: &mut egui::Ui, path: &str, selection: &[PhotoId]) {
|
||||
let name = path.replace('|', " › ");
|
||||
let name = hover_path(path);
|
||||
let item = |ui: &mut egui::Ui, id: &str, label: &str, enabled: bool| {
|
||||
let r = ui.add_enabled(enabled, egui::Button::new(label));
|
||||
register(ui, format!("keywordMenu:{id}"), r.rect);
|
||||
@@ -657,6 +680,36 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A path holding line breaks (CR, LF, U+2028, U+2029) is read out and hovered on one line, as its
|
||||
/// name is painted (CodeRabbit on #2922).
|
||||
#[test]
|
||||
fn a_keyword_path_with_line_breaks_is_read_and_hovered_on_one_line() {
|
||||
let path = "Wedding|Bride\r\nGroom\u{2028}Guests\u{2029}Cake";
|
||||
let breaks = |s: &str| s.contains(['\r', '\n', '\u{2028}', '\u{2029}']);
|
||||
assert!(!breaks(&spoken_path(path)), "{:?}", spoken_path(path));
|
||||
assert!(!breaks(&hover_path(path)), "{:?}", hover_path(path));
|
||||
assert!(hover_path(path).starts_with("Wedding › Bride"), "{:?}", hover_path(path));
|
||||
}
|
||||
|
||||
/// The dragged keyword's label and the tick box's "Inside: …" name keywords on one line too (CodeRabbit on
|
||||
/// #2922).
|
||||
#[test]
|
||||
fn a_dragged_keyword_and_the_keywords_inside_one_are_named_on_one_line() {
|
||||
let breaks = |s: &str| s.contains(['\r', '\n', '\u{2028}', '\u{2029}']);
|
||||
assert_eq!(drag_leaf("Wedding|Bride\nGroom"), "Bride Groom");
|
||||
let inside = inside_said(&["Rome\r\nCentre".to_string(), "Venice\u{2029}Lido".to_string()]).unwrap();
|
||||
assert!(!breaks(&inside), "{inside:?}");
|
||||
assert!(inside.contains("Rome Centre") && inside.contains("Venice Lido"), "{inside:?}");
|
||||
}
|
||||
|
||||
/// The Create Keyword Tag dialog names a parent keyword holding line breaks on one line (CodeRabbit on #2922).
|
||||
#[test]
|
||||
fn the_put_inside_box_names_its_parent_on_one_line() {
|
||||
let label = put_inside_label("Wedding|Bride\r\nGroom\u{2028}Guests");
|
||||
assert!(!label.contains(['\r', '\n', '\u{2028}', '\u{2029}']), "{label:?}");
|
||||
assert!(label.contains("Wedding › Bride Groom"), "{label:?}");
|
||||
}
|
||||
|
||||
fn library(keywords: &[&[&str]]) -> (Catalog, Vec<PhotoId>) {
|
||||
let mut c = Catalog::new();
|
||||
let mut ids = Vec::new();
|
||||
|
||||
@@ -37,7 +37,7 @@ fn keyword_row(app: &mut LightcraftApp, ui: &mut egui::Ui, n: &KeywordNode, id:
|
||||
let inside = if n.children.iter().any(|c| !c.children.is_empty()) { Inside::OpensAndFolds } else { Inside::Alone };
|
||||
tree_toggle(ui, &resp, indent, k, open, format!("keywordToggle:{}", n.path), &n.name, inside);
|
||||
}
|
||||
let resp = resp.on_hover_text(if n.children.is_empty() { n.path.clone() } else { format!("{} (includes the keywords below it)", n.path) });
|
||||
let resp = resp.on_hover_text(keyword_tip(&n.path, !n.children.is_empty()));
|
||||
if resp.clicked() {
|
||||
let v = if sel { serde_json::Value::Null } else { json!(n.path) };
|
||||
browse_all_photos(app, !sel);
|
||||
@@ -63,3 +63,23 @@ fn keyword_row(app: &mut LightcraftApp, ui: &mut egui::Ui, n: &KeywordNode, id:
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// A keyword row's hover text, on one line like the row's name (a path holding line breaks must not
|
||||
/// spill into a tall tooltip).
|
||||
fn keyword_tip(path: &str, has_children: bool) -> String {
|
||||
let path = lightcraft_catalog::keywords::one_line(path);
|
||||
if has_children { format!("{path} (includes the keywords below it)") } else { path.into_owned() }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
/// A sidebar keyword's hover text is on one line, as its name is painted (CodeRabbit on #2922).
|
||||
#[test]
|
||||
fn a_keyword_path_with_line_breaks_is_hovered_on_one_line() {
|
||||
let path = "Wedding|Bride\r\nGroom\u{2028}Guests\u{2029}Cake";
|
||||
for children in [false, true] {
|
||||
let tip = super::keyword_tip(path, children);
|
||||
assert!(!tip.contains(['\r', '\n', '\u{2028}', '\u{2029}']), "{tip:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +157,10 @@ impl<'a> Row<'a> {
|
||||
fn draw_row(app: &mut LightcraftApp, ui: &mut egui::Ui, row: Row) -> egui::Response {
|
||||
let Row { id, icon, label, spoken, count, selected, indent, sense, mark } = row;
|
||||
let label = if matches!(id, "all" | "recentlyAdded" | "picks" | "missing" | "recentlyDeleted") { crate::i18n::tr(label) } else { label };
|
||||
// a name is drawn on one line: one holding line breaks (a whole keyword list read as one
|
||||
// keyword) would spill over the rows around it
|
||||
let one_line = lightcraft_catalog::keywords::one_line(label);
|
||||
let label: &str = &one_line;
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let indent = drawn_indent(ui, indent);
|
||||
let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), ROW_HEIGHT), sense);
|
||||
@@ -242,7 +246,8 @@ pub(super) fn out_of_sight(app: &LightcraftApp, ui: &mut egui::Ui, id: &str, lab
|
||||
let indent = drawn_indent(ui, indent);
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let painter = ui.painter();
|
||||
let name_w = painter.layout_no_wrap(label.to_string(), t.font(13.5), t.text_label).size().x;
|
||||
// measured as the row paints it: on one line
|
||||
let name_w = painter.layout_no_wrap(lightcraft_catalog::keywords::one_line(label).into_owned(), t.font(13.5), t.text_label).size().x;
|
||||
let count_w = count.filter(|_| app.ui.show_counts).map(|n| painter.layout_no_wrap(n.to_string(), t.font(12.5), t.text_dim).size().x);
|
||||
let mark_w = if mark { MARK_W } else { 0.0 };
|
||||
let mut needed = row_need(indent, name_w, mark_w, count_w);
|
||||
|
||||
@@ -85,3 +85,24 @@ fn probe_keyword_sidebar_cost() {
|
||||
}
|
||||
eprintln!("frame after a rating: {:?}", t.elapsed() / 10);
|
||||
}
|
||||
|
||||
/// A keyword holding line breaks (a camera preset's whole keyword list read as one keyword by
|
||||
/// 0.8.x: "Beautiful Bride\r\n Bride and Groom\r\n …") was drawn many lines tall over the rows
|
||||
/// around it: the ghost text in the Keywords section. A row's name is drawn on one line.
|
||||
#[test]
|
||||
fn a_keyword_with_line_breaks_is_drawn_on_one_line() {
|
||||
let mut s = Session::new();
|
||||
let list: Vec<String> = (0..20).map(|i| format!("Wedding Keyword {i:02}")).collect();
|
||||
let mut p =
|
||||
Photo::new(PhotoId(1), Source::File { path: "/lightcraft-kw-test/IMG_1.jpg".into() }, "IMG_1.jpg", "JPEG", 60, 40, "2026-01-01T00:00:00");
|
||||
p.meta.keywords = vec![list.join("\r\n "), "Solo".into()];
|
||||
s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
|
||||
let app = LightcraftApp::new(s, Services { png: None, ..Default::default() });
|
||||
let mut h = Headless::new(app, [1400.0, 900.0], 1.0);
|
||||
let r = h.request("ui.set", json!({"view": "photoGrid", "leftPanel": true}), T);
|
||||
assert_eq!(r["ok"], true, "{r}");
|
||||
h.settle(SETTLE);
|
||||
let solo = h.app.widgets.iter().find(|(w, _)| w == "label:keyword:Solo").map(|(_, r)| *r).expect("the Solo row");
|
||||
let long = h.app.widgets.iter().find(|(w, _)| w.starts_with("label:keyword:Wedding Keyword 00")).map(|(_, r)| *r).expect("the long row");
|
||||
assert!(long.height() <= solo.height() + 0.5, "one line, as tall as any name: {long:?} vs {solo:?}");
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -457,6 +457,11 @@
|
||||
- Fielded search tokens now take inclusive comparisons: `rating:>=3`, `iso:<=400`, `f:>=2.8`, `focal:<=50`. Before,
|
||||
the `=` was read as part of the number, so such a search quietly showed no photos at all.
|
||||
|
||||
### Keywords
|
||||
- **No more ghost text in the Keywords list.** Some cameras write their whole keyword list as one metadata entry,
|
||||
a keyword a line; LightCraft now reads it as one keyword a line, and repairs libraries that already hold such a
|
||||
keyword when they open. A name with a line break in it is drawn on one line in the sidebar and the Keyword List.
|
||||
|
||||
### Import rename numbering stops safely at its limit
|
||||
- Copy and Move imports can use the last representable rename number once. If more renamed files remain, each is
|
||||
reported as failed with guidance to lower `renameStart` or leave `rename` blank; completed transfers stay
|
||||
|
||||
Reference in New Issue
Block a user