Skip to content

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

LRC-LIB-KEYWORDS: keyword counts are of photos, fast with a big selection

Nathaniel Ledford9dabd38941e6
diff --git a/crates/catalog/src/lib.rs b/crates/catalog/src/lib.rs
index a9e6acab7..c7225502b 100644
--- a/crates/catalog/src/lib.rs
+++ b/crates/catalog/src/lib.rs
@@ -382,6 +382,10 @@ impl Catalog {
pub fn keyword_info(&self, path: &str) -> Option<&keywords::KeywordInfo> {
self.keyword_list.get(&keywords::clean(path).to_lowercase()).map(|k| &k.info)
}
+ /// How the keyword list spells a listed keyword (any case), `None` when it isn't listed.
+ pub fn listed_path(&self, path: &str) -> Option<&str> {
+ self.keyword_list.get(&keywords::clean(path).to_lowercase()).map(|k| k.path.as_str())
+ }
/// The keyword list: keywords listed on their own or given attributes, by path.
pub fn listed_keywords(&self) -> impl Iterator<Item = &keywords::ListedKeyword> {
self.keyword_list.values()
diff --git a/crates/ui-egui/src/panels/keyword_list.rs b/crates/ui-egui/src/panels/keyword_list.rs
index 0dcf40d0c..4ebc57aaf 100644
--- a/crates/ui-egui/src/panels/keyword_list.rs
+++ b/crates/ui-egui/src/panels/keyword_list.rs
@@ -477,10 +477,10 @@ pub(crate) struct Ticks {
impl Ticks {
pub(crate) fn of(catalog: &Catalog, photos: &[PhotoId]) -> Ticks {
+ let photos = super::keywording::distinct(catalog, photos);
let mut counts: std::collections::HashMap<String, usize> = Default::default();
let mut seen = std::collections::HashSet::new();
- for id in photos {
- let Some(p) = catalog.photo(*id) else { continue };
+ for p in &photos {
seen.clear();
for k in &p.meta.keywords {
let key = lightcraft_catalog::keywords::clean(k).to_lowercase();
diff --git a/crates/ui-egui/src/panels/keywording.rs b/crates/ui-egui/src/panels/keywording.rs
index 6ad1364f8..64767c916 100644
--- a/crates/ui-egui/src/panels/keywording.rs
+++ b/crates/ui-egui/src/panels/keywording.rs
@@ -179,9 +179,9 @@ impl Chip {
/// for the selected `photos`, as the keyword tag options say, each with how many of them carry it;
/// by name.
pub(crate) fn will_export(catalog: &Catalog, photos: &[PhotoId]) -> Vec<Chip> {
+ let photos = distinct(catalog, photos);
let mut have: std::collections::BTreeMap<String, (String, usize)> = Default::default();
- for id in photos {
- let Some(p) = catalog.photo(*id) else { continue };
+ for p in &photos {
for name in catalog.export_keywords(&p.meta.keywords).flat {
have.entry(name.to_lowercase()).or_insert_with(|| (name.clone(), 0)).1 += 1;
}
@@ -189,15 +189,21 @@ pub(crate) fn will_export(catalog: &Catalog, photos: &[PhotoId]) -> Vec<Chip> {
have.into_values().map(|(path, have)| Chip { path, have, of: photos.len() }).collect()
}
+/// The selected photos, each once, those the library has: what "N of M" counts.
+pub(crate) fn distinct<'a>(catalog: &'a Catalog, photos: &[PhotoId]) -> Vec<&'a lightcraft_catalog::Photo> {
+ let mut seen = std::collections::HashSet::new();
+ photos.iter().filter(|id| seen.insert(**id)).filter_map(|id| catalog.photo(*id).map(|p| &**p)).collect()
+}
+
/// The keywords of the selected `photos`, each once whatever its case, with how many of them have
/// it; by name.
pub(crate) fn chips(catalog: &Catalog, photos: &[PhotoId]) -> Vec<Chip> {
use lightcraft_catalog::keywords::clean;
- // by lower-case keyword: the photos with it (each once)
+ let photos = distinct(catalog, photos);
+ // by lower-case keyword: as first seen, and the photos with it (each once)
let mut have: std::collections::BTreeMap<String, (String, usize)> = Default::default();
let mut seen = std::collections::HashSet::new();
- for id in photos {
- let Some(p) = catalog.photo(*id) else { continue };
+ for p in &photos {
seen.clear();
for k in &p.meta.keywords {
let k = clean(k);
@@ -207,7 +213,8 @@ pub(crate) fn chips(catalog: &Catalog, photos: &[PhotoId]) -> Vec<Chip> {
have.entry(k.to_lowercase()).or_insert_with(|| (k.clone(), 0)).1 += 1;
}
}
- have.into_values().map(|(k, n)| Chip { path: catalog.keyword_path(&k).unwrap_or(k), have: n, of: photos.len() }).collect()
+ // spelled as the keyword list spells it, else as first seen (no scan of the library)
+ have.into_values().map(|(k, n)| Chip { path: catalog.listed_path(&k).map(str::to_string).unwrap_or(k), have: n, of: photos.len() }).collect()
}
#[cfg(test)]
@@ -282,4 +289,26 @@ mod tests {
assert!(chars(&cut) <= 24.0);
assert!(chip_label("Places|Lisbon", false, 4.0, chars).starts_with('…'), "even with hardly any room");
}
+
+ /// A photo listed twice in the selection counts once, and one that isn't in the library not at
+ /// all: "On 1 of 2", not "On 2 of 4".
+ #[test]
+ fn the_counts_are_of_photos_not_ids() {
+ let (c, ids) = library(&[&["beach"], &["sea"]]);
+ let selection = [ids[0], ids[0], ids[1], PhotoId(999)];
+ assert_eq!(shown(&chips(&c, &selection)), [("beach", 1, 2), ("sea", 1, 2)]);
+ assert_eq!(shown(&will_export(&c, &selection)), [("beach", 1, 2), ("sea", 1, 2)]);
+ let ticks = crate::panels::keyword_list::Ticks::of(&c, &selection);
+ assert_eq!(ticks.tick("beach"), crate::panels::keyword_list::Tick::Some);
+ assert_eq!(crate::panels::keyword_list::Ticks::of(&c, &[ids[0], ids[0]]).tick("beach"), crate::panels::keyword_list::Tick::All);
+ }
+
+ /// A keyword the Keyword List has is spelled as it spells it.
+ #[test]
+ fn a_listed_keyword_is_spelled_as_listed() {
+ use lightcraft_catalog::keywords::KeywordInfo;
+ let (mut c, ids) = library(&[&["beach"]]);
+ c.apply(Op::SetKeyword { path: "Beach".into(), info: Some(KeywordInfo::default()) }).unwrap();
+ assert_eq!(shown(&chips(&c, &ids)), [("Beach", 1, 1)]);
+ }
}