Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions crates/catalog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,18 @@ impl Catalog {
out
}

/// A loop through `filter`'s own album field (not its rules): smart album `owner` filtered to
/// itself, or to an album that leads back to it.
/// What is wrong with `filter`'s own album field (not its rules): it names an album that is
/// gone (a view of an album, saved as a smart album, after that album was deleted), or, for
/// smart album `owner`, itself or an album that leads back to it.
pub fn album_filter_problem(&self, filter: &Filter, owner: Option<AlbumId>) -> Option<rules::Problem> {
let (a, owner) = (filter.album?, owner?);
(a == owner || self.album_reaches(a, owner)).then(|| rules::Problem {
path: Vec::new(),
field: Some("album".into()),
issue: rules::Issue::AlbumLoop,
message: format!("its album filter (album {}) would make this album include itself", a.0),
})
let a = filter.album?;
let problem = |issue, message| rules::Problem { path: Vec::new(), field: Some("album".into()), issue, message };
if !self.albums.contains_key(&a) {
return Some(problem(rules::Issue::NoSuchAlbum, format!("its album filter names album {}, which is gone", a.0)));
}
let owner = owner?;
(a == owner || self.album_reaches(a, owner))
.then(|| problem(rules::Issue::AlbumLoop, format!("its album filter (album {}) would make this album include itself", a.0)))
}

/// The photos of an album: the stored list, or a smart album's current matches (id order).
Expand Down
8 changes: 7 additions & 1 deletion crates/engine/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,14 @@ pub fn always(_: &Session) -> std::result::Result<(), String> {
pub fn has_active(s: &Session) -> std::result::Result<(), String> {
s.active().map(|_| ()).ok_or_else(|| "no photo selected".into())
}
/// A selection of photos that are there: one whose photos an undo took away (they stay selected
/// for the redo) is none, as [`Session::targets`] would be empty. What counts is what a command acts on
/// ([`Session::selected`]): the active photo only stands in when nothing else is selected.
pub fn has_selection(s: &Session) -> std::result::Result<(), String> {
if s.selection.ids.is_empty() && s.selection.active.is_none() { Err("no photos selected".into()) } else { Ok(()) }
// (the rule of `Session::selected`, without making its list: this is asked for every command, every frame)
let there = |id: &lightcraft_catalog::PhotoId| s.catalog.photo(*id).is_some();
let any = if s.selection.ids.is_empty() { s.selection.active.iter().any(there) } else { s.selection.ids.iter().any(there) };
if any { Ok(()) } else { Err("no photos selected".into()) }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
/// Albums hold library photos: a selection of only browsed (Local) photos has nothing an album
/// can take (issue #684; Add to My Photos first).
Expand Down
7 changes: 6 additions & 1 deletion crates/engine/src/cmd/library/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub(super) fn view_rules(s: &Session) -> lightcraft_catalog::Filter {
LibrarySource::Album(a) => s.catalog.album(a).and_then(|a| a.smart.as_deref().cloned()),
_ => None,
};
match smart {
let mut rules = match smart {
Some(base) => {
// overlay the fields the filter bar changed
let cur = serde_json::to_value(&s.filter).unwrap_or_default();
Expand All @@ -81,7 +81,12 @@ pub(super) fn view_rules(s: &Session) -> lightcraft_catalog::Filter {
}
f
}
};
// a view of a smart album limited to an album that is gone is saved without that limit
if rules.album.is_some_and(|a| s.catalog.album(a).is_none()) {
rules.album = None;
}
rules
}

impl Session {
Expand Down
27 changes: 22 additions & 5 deletions crates/engine/src/cmd/library/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ pub fn specs() -> Vec<CommandSpec> {
if let Some(problems) = f.rule_set.as_ref().filter(|_| sets_rules).map(|rs| rs.check(&s.catalog)).filter(|p| !p.is_empty()) {
return Err(bad("library.filter", problems.iter().map(ToString::to_string).collect::<Vec<_>>().join("; ")));
}
// an album this call names is one the library has (one named before and since
// deleted is let go of, `Session::reconcile_view`)
if p.get("album").is_some_and(|a| !a.is_null()) && f.album.is_some_and(|a| s.catalog.album(a).is_none()) {
return Err(bad("library.filter", "no such album"));
}
s.filter = f;
s.shelved_filter = None;
Ok(json!({"count": s.visible().len()}))
Expand Down Expand Up @@ -874,14 +879,20 @@ pub fn specs() -> Vec<CommandSpec> {
view
} else {
let replace = bool_or(p, "replace", false);
let folder = cur.library_folder.clone();
let (folder, album) = (cur.library_folder.clone(), cur.album);
let base = if replace { Default::default() } else { cur };
let mut r = merge_rules(&base, p.get("rules").unwrap_or(&Value::Null), "album.setRules", &s.catalog, Some(id))?;
// the rules dialog has no folder field: replacing its rules keeps the folder
// unless the call says (`libraryFolder: null`) to drop it
if replace && p.get("rules").and_then(|r| r.get("libraryFolder")).is_none() {
r.library_folder = folder;
}
// nor one for the album a saved view is limited to
// (`album: null` drops it). One that is gone is let go of: this is where
// the mark on such an album sends the person
if replace && p.get("rules").and_then(|r| r.get("album")).is_none() {
r.album = album.filter(|a| s.catalog.album(*a).is_some());
}
r
};
let set = Op::SetAlbumRules { id, rules: Box::new(rules) };
Expand Down Expand Up @@ -932,10 +943,9 @@ pub fn specs() -> Vec<CommandSpec> {
}
}
let ops = order.into_iter().rev().map(|id| Op::RemoveAlbum { id }).collect();
// a deleted album that was being shown gives way to All Photos, as after any
// other way it goes (`Session::reconcile_view`)
s.commit(if folder { "Delete Folder" } else { "Delete Album" }, Op::Batch { ops })?;
if matches!(s.source, LibrarySource::Album(current) if removed.contains(&current)) {
s.source = LibrarySource::All;
}
ok()
}
),
Expand Down Expand Up @@ -1028,12 +1038,19 @@ pub fn specs() -> Vec<CommandSpec> {
has_library_selection,
|s, p| {
let id = album_param(p, "id", "album.addPhotos")?;
let targets = crate::cmd::photo_ids(p, "album.addPhotos")?.unwrap_or_else(|| s.selected());
// photos that are there: an album doesn't list one that is gone (a selection
// dragged here after an undo took one of its photos away)
let mut targets = crate::cmd::photo_ids(p, "album.addPhotos")?.unwrap_or_else(|| s.selected());
targets.retain(|t| s.catalog.photo(*t).is_some());
let al = s.catalog.album(id).ok_or_else(|| bad("album.addPhotos", "no such album"))?;
if al.is_smart() || al.folder {
return Err(bad("album.addPhotos", "smart albums and folders can't hold photos"));
}
let (targets, skipped) = album_targets_or_err(s, "album.addPhotos", targets)?;
// nothing left to add: the album is unchanged, so no undo step
if targets.is_empty() {
return Ok(json!({"added": 0, "skipped": skipped}));
}
let mut photos = al.photos.clone();
let before = photos.len();
// looked up, not searched for: adding thousands to a big album stays linear
Expand Down
40 changes: 39 additions & 1 deletion crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ impl Session {
self.auto_write_sidecars_since(log_start);
}
}
if self.depth == 0 {
// whatever the command changed, and however it ended: the view points at what is there
self.reconcile_view();
}
if self.depth == 0 && self.library.is_some() {
// Make the command durable before reporting success. When the command's own records
// can't be appended, it fails with `NotSaved`: the change stays applied in memory and
Expand Down Expand Up @@ -780,6 +784,7 @@ impl Session {
}
};
self.show_undone(&e.op);
self.reconcile_view();
self.pending_log.push(e.op);
self.redo.push(UndoEntry {
label: e.label.clone(),
Expand All @@ -805,6 +810,7 @@ impl Session {
}
};
self.show_undone(&e.op);
self.reconcile_view();
self.pending_log.push(e.op);
self.undo.push(UndoEntry {
label: e.label.clone(),
Expand Down Expand Up @@ -1063,8 +1069,32 @@ impl Session {
self.shelved_filter.is_some()
}

/// Makes the view show only albums the library holds: one that is gone is no longer the
/// source (All Photos is) or the filter's album. THE place this is checked: commands, undo
/// and redo, opening a library and every look at the grid run it, so whatever removed an
/// album (a command, an undo, a background task) or named one (a saved filter, a saved view)
/// need not know. Two lookups: it runs every time.
///
/// An album that comes back (its deletion undone) is not shown again by itself, as after
/// `album.delete` and undo before: the grid stays where the person was put.
///
/// What is only looked up when it is used is left as it is, and so comes back with an undo:
/// the target album (a missing one means the Quick Collection) and the selection (`active`
/// and `targets` leave out photos that are gone).
pub fn reconcile_view(&mut self) {
let cat = &self.catalog;
let gone = |a: &lightcraft_catalog::AlbumId| cat.album(*a).is_none();
if matches!(&self.source, LibrarySource::Album(a) if gone(a)) {
self.source = LibrarySource::All;
}
if self.filter.album.as_ref().is_some_and(gone) {
self.filter.album = None;
}
}

/// Photos shown in the grid/filmstrip for the current source, filter and sort.
pub fn visible(&mut self) -> &[PhotoId] {
self.reconcile_view();
let mut key =
(self.catalog.revision, format!("{:?}|{:?}|{:?}|{:?}|{:?}", self.source, self.filter, self.sort, self.browse, self.library_folder));
// What a held view is told apart by: what is shown and how, and for a smart album its rules (rules that were
Expand Down Expand Up @@ -1252,6 +1282,7 @@ impl Session {
/// Photos in the current source (folder, album, …) before the filter bar and search narrow
/// them; `None` where that is not a separate number (Missing Photos).
pub fn source_total(&mut self) -> Option<usize> {
self.reconcile_view();
if self.source == LibrarySource::Missing {
return None;
}
Expand Down Expand Up @@ -1336,8 +1367,15 @@ impl Session {
}

/// The photos commands act on when a call names none: the selection, else the active photo.
/// As far as its photos are there: one taken away by undo stays selected for the redo, and is
/// acted on by nothing meanwhile.
pub fn selected(&self) -> Vec<PhotoId> {
if self.selection.ids.is_empty() { self.selection.active.into_iter().collect() } else { self.selection.ids.clone() }
let there = |id: &PhotoId| self.catalog.photo(*id).is_some();
if self.selection.ids.is_empty() {
self.selection.active.into_iter().filter(there).collect()
} else {
self.selection.ids.iter().copied().filter(there).collect()
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions crates/engine/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,15 @@ impl Session {
self.selection = Selection::default();
self.source = LibrarySource::All;
self.library_folder = None;
// what names albums and photos by number means others in another library: no filter
// (as after a restart; the view file keeps none), no target album, no photo come from
self.filter = Default::default();
self.shelved_filter = None;
self.target_album = None;
self.previous_active = None;
self.active_mask = None;
self.active_spot = None;
self.before.clear();
if report.created && seed_demo {
crate::demo::load(self);
journal.snapshot(&self.catalog)?;
Expand Down Expand Up @@ -558,6 +567,8 @@ impl Session {
self.selection.ids.retain(|id| self.catalog.photo(*id).is_some());
self.selection.active = self.selection.active.filter(|id| self.catalog.photo(*id).is_some());
}
// the saved view may name an album the library no longer has
self.reconcile_view();
// not on Missing Photos: working that view out checks every original (minutes on a
// sleeping NAS), and the app checks them in the background once it runs
if self.selection.active.is_none()
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod slideshow_export;
mod spots;
mod sync;
mod view_follow;
mod view_reconcile;
pub(super) mod xmp;

fn demo() -> Session {
Expand Down
Loading
Loading