Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions crates/ui-egui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl LightcraftApp {
compare_windows: [None; 2],
compare_tiles: Default::default(),
compare_hold_until: f64::NEG_INFINITY,
survey_motion: Default::default(),
merge: merge::MergeState::default(),
import: None,
scan: None,
Expand Down
1 change: 1 addition & 0 deletions crates/ui-egui/src/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ impl Headless {
|| self.app.import.is_some()
|| self.app.export.is_some()
|| self.app.session.denoise_busy()
|| self.app.survey_motion.moving(self.time)
|| !self.app.tasks.is_empty()
|| !self.app.synthetic.is_empty()
|| !self.app.pending_gestures.is_empty()
Expand Down
2 changes: 2 additions & 0 deletions crates/ui-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ pub struct LightcraftApp {
pub(crate) compare_tiles: std::collections::HashMap<(u8, u64), region::RegionView>,
/// Until when (s) a pinch or scroll in Compare / Reference holds the windows there are.
pub(crate) compare_hold_until: f64,
/// Survey tiles gliding to new cells, and the render slot each surveyed photo keeps.
pub(crate) survey_motion: panels::compare::survey::SurveyMotion,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
/// Photo Merge dialog previews and background merges.
pub merge: merge::MergeState,
/// An import in progress (the import review dialog's batches).
Expand Down
61 changes: 31 additions & 30 deletions crates/ui-egui/src/panels/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::state::{ViewMode, Zoom};
use crate::theme::Tokens;
use crate::widgets::register;

pub mod survey;

/// Most photos a survey shows.
pub const SURVEY_MAX: usize = 48;

Expand Down Expand Up @@ -208,6 +210,17 @@ fn request_tile_renders(app: &mut LightcraftApp, id: PhotoId, slot: Slot, want:
}
}

/// The photo's frame as shown: cropped, rotated and lens-corrected.
fn frame_of(photo: &lightcraft_catalog::Photo) -> lightcraft_pipeline::geometry::Frame {
lightcraft_pipeline::geometry::Frame::with_lens(
photo.width.max(1) as usize,
photo.height.max(1) as usize,
&photo.develop,
true,
photo.embedded_lens.as_ref(),
)
}

/// Draw one photo fitted into `area` (rendered at its display size into `slot`), with its caption
/// strip below. Returns the image rect and the click/drag response.
fn photo_tile(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId, slot: Slot, area: Rect, label: &str, zoom: Zoom) -> (Rect, egui::Response) {
Expand All @@ -218,13 +231,7 @@ fn photo_tile(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId, slot: Slo
let Some(photo) = app.session.catalog.photo(id).cloned() else { return (area, resp) };
let caption_h = 26.0;
let img_area = Rect::from_min_max(area.min, pos2(area.right(), area.bottom() - caption_h));
let frame = lightcraft_pipeline::geometry::Frame::with_lens(
photo.width.max(1) as usize,
photo.height.max(1) as usize,
&photo.develop,
true,
photo.embedded_lens.as_ref(),
);
let frame = frame_of(&photo);
let aspect = frame.aspect() as f32;
let native = super::detail::output_px(&frame);
let mut img = super::detail::fit_rect(img_area, aspect, zoom, native, ppp, app.ui.pan);
Expand All @@ -241,7 +248,10 @@ fn photo_tile(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId, slot: Slo
let texture_side = super::detail::texture_side(ui.ctx());
let want =
(img.width().max(img.height()).min(img_area.width().max(img_area.height()) * 4.0) * ppp).min(2560.0_f32.min(texture_side as f32)) as usize;
request_tile_renders(app, id, slot, want, aspect);
// a survey tile asks for its render once it has landed, not one per size it passes on the way
if !(app.ui.view == ViewMode::Survey && app.survey_motion.moving(ui.input(|i| i.time))) {
request_tile_renders(app, id, slot, want, aspect);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// zoomed past what that render holds (1:1 to check focus): the part on screen as a window, as
// in the loupe. Compare and Reference only; Survey tiles are fit views.
let pane = match slot {
Expand Down Expand Up @@ -535,21 +545,6 @@ pub fn show_compare(app: &mut LightcraftApp, ui: &mut egui::Ui) {
ui.painter().line_segment([pos2(mid, area.top()), pos2(mid, area.bottom())], Stroke::new(1.0, Tokens::get(ui.ctx()).divider));
}

/// Columns for `n` tiles of aspect ~3:2 in `area`, maximizing the tile size.
pub fn survey_columns(n: usize, area: Rect) -> usize {
let mut best = (1, 0.0f32);
for cols in 1..=n.max(1) {
let rows = n.div_ceil(cols);
let w = area.width() / cols as f32;
let h = area.height() / rows as f32;
let s = w.min(h * 1.5);
if s > best.1 {
best = (cols, s);
}
}
best.0
}

pub fn show_survey(app: &mut LightcraftApp, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
let canvas = area_and_filmstrip(app, ui);
Expand All @@ -559,13 +554,19 @@ pub fn show_survey(app: &mut LightcraftApp, ui: &mut egui::Ui) {
return;
}
let area = canvas.shrink(14.0);
let cols = survey_columns(photos.len(), area);
let rows = photos.len().div_ceil(cols);
let (cw, ch) = (area.width() / cols as f32, area.height() / rows as f32);
for (i, id) in photos.iter().enumerate() {
let (c, r) = (i % cols, i / cols);
let cell = Rect::from_min_size(pos2(area.left() + c as f32 * cw, area.top() + r as f32 * ch), vec2(cw, ch)).shrink(6.0);
let (img, resp) = photo_tile(app, ui, *id, Slot::Compare(i as u8), cell, "", Zoom::Fit);
let aspects: Vec<f32> = photos.iter().map(|id| app.session.catalog.photo(*id).map_or(1.5, |p| frame_of(p).aspect() as f32)).collect();
let cells = survey::cells(&aspects, area);
let now = ui.input(|i| i.time);
let placed = app.survey_motion.place(&photos, &cells, area, ui.ctx().cumulative_frame_nr(), now);
let (slots, freed) = app.survey_motion.assign_slots(&photos);
for s in freed {
app.renderer.release(Slot::Compare(s));
}
if app.survey_motion.moving(now) {
ui.ctx().request_repaint();
}
for ((id, cell), slot) in photos.iter().zip(placed).zip(slots) {
let (img, resp) = photo_tile(app, ui, *id, Slot::Compare(slot), cell.shrink(survey::GAP), "", Zoom::Fit);
if app.session.selection.active == Some(*id) {
app.image_rect = Some(img);
}
Expand Down
229 changes: 229 additions & 0 deletions crates/ui-egui/src/panels/compare/survey.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
//! Survey layout: the cells the photos are tiled in, fitted to their shapes, and the glide that
//! moves the tiles left to their new cells when a photo leaves or joins the survey, so it is
//! plain which photo went where.

use std::collections::HashMap;

use egui::{Rect, pos2, vec2};
use lightcraft_catalog::PhotoId;

/// Space a tile keeps below its photo for the caption (as `photo_tile` draws it).
pub const CAPTION_H: f32 = 26.0;
/// Margin around each tile inside its cell.
pub const GAP: f32 = 6.0;
/// Seconds a tile takes to glide to its new cell.
pub const GLIDE_SECS: f64 = 0.2;

/// A width / height ratio that layout arithmetic can use (a hostile size gives a square).
fn usable_aspect(a: f32) -> f32 {
if a.is_finite() && a > 0.0 { a.clamp(0.05, 20.0) } else { 1.0 }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// Image area photos with `aspects` show in a grid of `cols` equal cells in `area`.
fn shown_area(aspects: &[f32], cols: usize, area: Rect) -> f32 {
let rows = aspects.len().div_ceil(cols.max(1));
let iw = area.width() / cols.max(1) as f32 - 2.0 * GAP;
let ih = area.height() / rows.max(1) as f32 - 2.0 * GAP - CAPTION_H;
if iw <= 0.0 || ih <= 0.0 {
return 0.0;
}
aspects
.iter()
.map(|&a| {
let a = usable_aspect(a);
let w = iw.min(ih * a);
w * w / a
})
.sum()
}

/// The cell of each photo (width / height `aspects`, in survey order) in `area`: equal cells in
/// rows, with the column count that shows the most of the photos, so the survey fits their shapes
/// to the screen as Lightroom Classic's does (four portrait photos side by side on a wide screen,
/// not two by two). An incomplete last row is centred.
pub fn cells(aspects: &[f32], area: Rect) -> Vec<Rect> {
let n = aspects.len();
if n == 0 {
return Vec::new();
}
let mut best = (1, f32::NEG_INFINITY);
for cols in 1..=n {
let shown = shown_area(aspects, cols, area);
if shown > best.1 {
best = (cols, shown);
}
}
let cols = best.0;
let rows = n.div_ceil(cols);
let (cw, ch) = (area.width() / cols as f32, area.height() / rows as f32);
let last_row = n - (rows - 1) * cols;
(0..n)
.map(|i| {
let (c, r) = (i % cols, i / cols);
let indent = if r == rows - 1 { (cols - last_row) as f32 * cw / 2.0 } else { 0.0 };
Rect::from_min_size(pos2(area.left() + indent + c as f32 * cw, area.top() + r as f32 * ch), vec2(cw, ch))
})
.collect()
}

/// One tile's way from where it was drawn to its cell.
#[derive(Clone, Copy, Debug)]
struct Glide {
from: Rect,
to: Rect,
start: f64,
}

impl Glide {
fn at(&self, now: f64) -> Rect {
let t = ((now - self.start) / GLIDE_SECS).clamp(0.0, 1.0) as f32;
// ease out: quick to leave, gentle to land
let e = 1.0 - (1.0 - t).powi(3);
self.from.lerp_towards(&self.to, e)
}

fn done(&self, now: f64) -> bool {
// (a hair early: the clock's rounding must not keep a landed tile moving)
now - self.start >= GLIDE_SECS - 1e-9
}
}

/// Where the survey's tiles are drawn while they glide, and which render slot each photo keeps
/// (a photo keeps its slot while it is shown, so the tiles that move keep their renders).
#[derive(Default)]
pub struct SurveyMotion {
/// The survey area of the last frame drawn, and its frame number.
last: Option<(Rect, u64)>,
tiles: HashMap<PhotoId, Glide>,
slots: HashMap<PhotoId, u8>,
}

impl SurveyMotion {
/// Where to draw `photos` (in survey order) at `now`, given their `cells` in `area` this frame
/// (`frame`, egui's cumulative frame number). A tile whose cell changed since the last frame glides
/// there from where it was drawn; a photo that joined appears in its cell. A survey just opened
/// or a canvas that changed size is laid out at once.
pub fn place(&mut self, photos: &[PhotoId], cells: &[Rect], area: Rect, frame: u64, now: f64) -> Vec<Rect> {
let continued = self.last.is_some_and(|(a, p)| a == area && frame <= p.saturating_add(1));
self.last = Some((area, frame));
let mut next = HashMap::with_capacity(photos.len());
let placed = photos
.iter()
.zip(cells)
.map(|(id, &to)| {
let glide = match self.tiles.get(id) {
Some(g) if continued && g.to == to => *g,
Some(g) if continued => Glide { from: g.at(now), to, start: now },
_ => Glide { from: to, to, start: now },
};
next.insert(*id, glide);
glide.at(now)
})
.collect();
self.tiles = next;
placed
}

/// Whether a tile is still on its way.
pub fn moving(&self, now: f64) -> bool {
self.tiles.values().any(|g| !g.done(now) && g.from != g.to)
}

/// The render slot of each of `photos`: the one it had, else the lowest free one. Returns the
/// slots that photos no longer shown gave up, whose renders can be freed.
pub fn assign_slots(&mut self, photos: &[PhotoId]) -> (Vec<u8>, Vec<u8>) {
let freed: Vec<u8> = self.slots.iter().filter(|(id, _)| !photos.contains(id)).map(|(_, s)| *s).collect();
self.slots.retain(|id, _| photos.contains(id));
let used: Vec<u8> = self.slots.values().copied().collect();
let mut free = (0..=u8::MAX).filter(move |s| !used.contains(s));
let slots = photos.iter().map(|id| *self.slots.entry(*id).or_insert_with(|| free.next().unwrap_or(u8::MAX))).collect();
(slots, freed)
}
}

#[cfg(test)]
mod tests {
use super::{CAPTION_H, GAP, GLIDE_SECS, SurveyMotion, cells};
use egui::{Rect, pos2, vec2};
use lightcraft_catalog::PhotoId;

const WIDE: Rect = Rect { min: pos2(0.0, 0.0), max: pos2(1600.0, 800.0) };

fn cols(cells: &[Rect]) -> usize {
cells.iter().filter(|c| c.top() == cells[0].top()).count()
}

#[test]
fn four_portraits_stand_side_by_side_on_a_wide_screen() {
let c = cells(&[2.0 / 3.0; 4], WIDE);
assert_eq!(cols(&c), 4, "{c:?}");
}

#[test]
fn four_landscapes_on_a_wide_screen_take_two_rows() {
let c = cells(&[1.5; 4], WIDE);
assert_eq!(cols(&c), 2, "{c:?}");
}

#[test]
fn the_last_row_is_centred() {
let c = cells(&[1.5; 5], WIDE);
assert_eq!(cols(&c), 3, "{c:?}");
let (row, last) = (&c[..3], &c[3..]);
assert_eq!(last.len(), 2);
let mid = |r: &[Rect]| (r[0].left() + r[r.len() - 1].right()) / 2.0;
assert!((mid(row) - mid(last)).abs() < 0.01, "{c:?}");
}

#[test]
fn cells_stay_inside_the_area_and_survive_hostile_sizes() {
for aspects in [vec![f32::NAN, 0.0, -1.0, f32::INFINITY, 1e9], vec![1.5; 48]] {
let c = cells(&aspects, WIDE);
assert_eq!(c.len(), aspects.len());
assert!(c.iter().all(|r| WIDE.expand(0.01).contains_rect(*r)), "{c:?}");
}
assert!(cells(&[], WIDE).is_empty());
// a canvas too small for any tile still lays them out, one column
let tiny = Rect::from_min_size(pos2(0.0, 0.0), vec2(2.0 * GAP, CAPTION_H));
assert_eq!(cells(&[1.0; 3], tiny).len(), 3);
}

#[test]
fn a_tile_glides_to_its_new_cell_and_lands_there() {
let (a, b) = (PhotoId(1), PhotoId(2));
let mut m = SurveyMotion::default();
let first = cells(&[1.5; 2], WIDE);
assert_eq!(m.place(&[a, b], &first, WIDE, 1, 0.0), first, "a new survey is laid out at once");
assert!(!m.moving(0.0));
// a leaves: b moves to the single cell
let one = cells(&[1.5], WIDE);
let mid = m.place(&[b], &one, WIDE, 2, 1.0);
assert_eq!(mid[0], first[1], "it starts where it was drawn");
assert!(m.moving(1.0));
let half = m.place(&[b], &one, WIDE, 3, 1.0 + GLIDE_SECS / 2.0)[0];
assert!(half != first[1] && half != one[0], "on its way: {half:?}");
assert_eq!(m.place(&[b], &one, WIDE, 4, 1.0 + GLIDE_SECS)[0], one[0], "it lands in its cell");
assert!(!m.moving(1.0 + GLIDE_SECS));
}

#[test]
fn a_reopened_survey_or_a_resized_canvas_does_not_glide() {
let (a, b) = (PhotoId(1), PhotoId(2));
let mut m = SurveyMotion::default();
m.place(&[a, b], &cells(&[1.5; 2], WIDE), WIDE, 1, 0.0);
let one = cells(&[1.5], WIDE);
assert_eq!(m.place(&[b], &one, WIDE, 9, 1.0), one, "frames skipped: the survey was not shown");
let narrow = Rect::from_min_size(pos2(0.0, 0.0), vec2(900.0, 800.0));
let c = cells(&[1.5], narrow);
assert_eq!(m.place(&[b], &c, narrow, 10, 2.0), c, "a new canvas size");
}

#[test]
fn photos_keep_their_slots_and_new_ones_take_freed_slots() {
let (a, b, c, d) = (PhotoId(1), PhotoId(2), PhotoId(3), PhotoId(4));
let mut m = SurveyMotion::default();
assert_eq!(m.assign_slots(&[a, b, c]), (vec![0, 1, 2], vec![]));
assert_eq!(m.assign_slots(&[a, c]), (vec![0, 2], vec![1]), "c keeps its render");
assert_eq!(m.assign_slots(&[a, c, d]), (vec![0, 2, 1], vec![]), "d takes the freed slot");
}
}
Loading
Loading