diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rs index 234476167e1..2935a22753c 100644 --- a/crates/ui-egui/src/panels/detail.rs +++ b/crates/ui-egui/src/panels/detail.rs @@ -2144,8 +2144,10 @@ pub(crate) fn filmstrip(app: &mut LightcraftApp, ui: &mut egui::Ui, r: Rect) { let mut child = ui.new_child(egui::UiBuilder::new().max_rect(r.shrink2(vec2(0.0, 4.0)))); // a vertical mouse wheel scrolls the strip sideways (horizontal trackpad scrolls still work) child.style_mut().always_scroll_the_only_direction = true; - // only a new active photo (or the strip appearing) scrolls it; see `grid::follow_active` - let follow = super::grid::follow_active(child.ctx(), egui::Id::new("film-follow-active"), active); + // only a new active photo, the active photo at another place (another source, filter or sort) + // or the strip appearing scrolls it; see `grid::follow_active` + let index = active.and_then(|a| ids.iter().position(|x| *x == a)); + let follow = super::grid::follow_active(child.ctx(), egui::Id::new("film-follow-active"), active, index); egui::ScrollArea::horizontal().id_salt("filmstrip").auto_shrink([false, false]).show_viewport(&mut child, |ui, vp| { let (area, _) = ui.allocate_exact_size(vec2(ids.len() as f32 * cell_w, r.height() - 16.0), Sense::hover()); app.film_scroll = Some(vp.left()); diff --git a/crates/ui-egui/src/panels/grid.rs b/crates/ui-egui/src/panels/grid.rs index 7b6d5cf61c7..cf7f02db59f 100644 --- a/crates/ui-egui/src/panels/grid.rs +++ b/crates/ui-egui/src/panels/grid.rs @@ -227,10 +227,12 @@ fn show_inner(app: &mut LightcraftApp, ui: &mut egui::Ui) { let stacks = app.caches.grid.stacks(stats, &app.session.catalog, generation); let total_h = lay.height + 12.0; let active = app.session.selection.active; - // bring the active photo into view when it changes (keyboard, click, command) or the grid - // comes back on screen; otherwise the scroll position is the user's - let scroll_to: Option = if follow_active(ui.ctx(), egui::Id::new("grid-follow-active"), active) { - active.and_then(|a| ids.iter().position(|x| *x == a)).and_then(|i| lay.cells.get(i).copied()) + // bring the active photo into view when it changes (keyboard, click, command), moves in the + // grid (another source, filter or sort) or the grid comes back on screen; otherwise the scroll + // position is the user's + let index = active.and_then(|a| ids.iter().position(|x| *x == a)); + let scroll_to: Option = if follow_active(ui.ctx(), egui::Id::new("grid-follow-active"), active, index) { + index.and_then(|i| lay.cells.get(i).copied()) } else { None }; @@ -291,16 +293,17 @@ fn show_inner(app: &mut LightcraftApp, ui: &mut egui::Ui) { } /// Should a scrolling photo strip (`key`: the grid, the filmstrip) bring the active photo into -/// view on this pass? Yes when the active photo changed since the strip was last drawn, or when -/// the strip was not drawn on the previous pass (view switch, panel shown again). Otherwise the -/// scroll position is the user's: time passing, finished renders, edits to the photo or a released -/// scroll bar never move it (issue #11). -pub(crate) fn follow_active(ctx: &egui::Context, key: egui::Id, active: Option) -> bool { +/// view on this pass? Yes when the active photo changed since the strip was last drawn, when it +/// moved to another place in the strip (`index`: another album or folder shown, a filter or the +/// sort order changed), or when the strip was not drawn on the previous pass (view switch, panel +/// shown again). Otherwise the scroll position is the user's: time passing, finished renders, +/// edits to the photo or a released scroll bar never move it (issue #11). +pub(crate) fn follow_active(ctx: &egui::Context, key: egui::Id, active: Option, index: Option) -> bool { let pass = ctx.cumulative_pass_nr(); - let last: Option<(Option, u64)> = ctx.data(|d| d.get_temp(key)); - ctx.data_mut(|d| d.insert_temp(key, (active, pass))); + let last: Option<(Option, Option, u64)> = ctx.data(|d| d.get_temp(key)); + ctx.data_mut(|d| d.insert_temp(key, (active, index, pass))); match last { - Some((was, drawn)) => was != active || drawn + 1 < pass, + Some((was, at, drawn)) => was != active || at != index || drawn + 1 < pass, None => true, } } diff --git a/crates/ui-egui/src/tests_scroll.rs b/crates/ui-egui/src/tests_scroll.rs index 1eff1ec7f92..734607f1148 100644 --- a/crates/ui-egui/src/tests_scroll.rs +++ b/crates/ui-egui/src/tests_scroll.rs @@ -208,3 +208,48 @@ fn filmstrip_shows_every_selected_photo() { assert_eq!(r["ok"], true, "{r}"); assert_eq!(fill(&mut h, second), active); } + +/// The same photo stays active while another source is shown, but sits elsewhere in it: the +/// filmstrip and the grid follow it instead of keeping the old offset, which would show other +/// photos than the active one. +#[test] +fn showing_another_source_brings_the_active_photo_back_into_view() { + for view in ["detail", "photoGrid"] { + let mut h = demo(view); + let ids = h.app.session.visible_cloned(); + let at = ids.len() / 2; + let photo = ids[at]; + // an album of the first half of All Photographs up to the active photo: an album lists + // its photos in the opposite order here, so the photo comes (near) first there + let album = h.app.run("album.create", json!({"name": "First Half", "addSelected": false})).unwrap()["id"].as_u64().unwrap(); + let rest: Vec = ids[..=at].iter().map(|p| p.0).collect(); + h.app.run("album.addPhotos", json!({"id": album, "ids": rest})).unwrap(); + h.app.run("library.select", json!({"ids": [photo.0]})).unwrap(); + idle(&mut h, 30); + let scroll = |h: &Headless| if view == "detail" { film_x(h) } else { grid_y(h) }; + // a cell far off screen is not drawn (and has no widget) at all + let in_view = |h: &Headless| { + let key = format!("{}:{}", if view == "detail" { "film" } else { "thumb" }, photo.0); + h.app.widgets.iter().find(|(w, _)| *w == key).is_some_and(|(_, cell)| { + if view == "detail" { cell.left() >= 0.0 && cell.right() <= 1200.0 } else { cell.intersects(h.app.canvas_rect.unwrap()) } + }) + }; + let before = scroll(&h); + assert!(before > 600.0, "the active photo is far from the beginning: {view}, {before}"); + assert!(in_view(&h), "{view}: in view in All Photographs"); + for (source, index) in [(json!({"kind": "album", "id": album}), None), (json!({"kind": "all"}), Some(at))] { + h.app.run("library.source", source.clone()).unwrap(); + idle(&mut h, 60); + assert_eq!(h.app.session.active(), Some(photo), "{view}, {source}: the photo stays active"); + let i = h.app.session.visible_cloned().iter().position(|p| *p == photo).unwrap(); + match index { + Some(at) => assert_eq!(i, at, "{view}, {source}"), + None => assert!(i + ids.len() / 4 < at, "{view}, {source}: the photo moved ({at} → {i})"), + } + assert!(in_view(&h), "{view}, {source}: the active photo is in view (scroll {})", scroll(&h)); + let after = scroll(&h); + idle(&mut h, 60); + assert_eq!(scroll(&h), after, "{view}, {source}: idle frames preserve the new position"); + } + } +}