Skip to content

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

Merge PR #347

Claudeb5fcf58e55b6
diff --cc crates/ui-egui/src/import.rs
index 993c67d23,7465ca363..2122aa8d5
--- a/crates/ui-egui/src/import.rs
+++ b/crates/ui-egui/src/import.rs
@@@ -78,10 -53,9 +78,13 @@@ pub struct ImportDialog
pub metadata_preset: String,
/// Copy: raws are copied as DNG.
pub dng: bool,
+ /// Per candidate: the library already has it in Recently Deleted.
+ pub trashed: Vec<bool>,
+ /// What to do with those: "" (leave them), `restore` or `fresh` (the `onDeleted` param).
+ pub on_deleted: String,
+ /// The candidate clicked last: where a Shift-click range starts ([`ImportDialog::click`]).
+ #[serde(skip)]
+ pub last_clicked: Option<usize>,
}
impl ImportDialog {
@@@ -90,26 -64,26 +93,44 @@@
ImportDialog { candidates, checked, ..Default::default() }
}
pub fn importable(&self, i: usize) -> bool {
- self.candidates.get(i).is_some_and(|c| c.duplicate.is_none() && c.error.is_none())
+ self.candidates.get(i).is_some_and(|c| c.error.is_none() && (c.duplicate.is_none() || self.is_trashed(i) && !self.on_deleted.is_empty()))
+ }
+ /// Candidate `i` is a file whose photo is in Recently Deleted.
+ pub fn is_trashed(&self, i: usize) -> bool {
+ self.trashed.get(i).copied().unwrap_or(false)
+ }
+ /// Choose what happens to the files in Recently Deleted (`""`, `restore` or `fresh`); they are
+ /// checked once there is a choice and unchecked again without one.
+ pub fn set_on_deleted(&mut self, choice: &str) {
+ // switching between restore and fresh keeps the user's per-cell choices
+ let changed = self.on_deleted.is_empty() != choice.is_empty();
+ self.on_deleted = choice.to_string();
+ for i in (0..self.candidates.len()).filter(|_| changed) {
+ if self.is_trashed(i)
+ && let Some(c) = self.checked.get_mut(i)
+ {
+ *c = !choice.is_empty();
+ }
+ }
}
+ /// A click on candidate `i` toggles its checkbox. With `shift`, every importable candidate
+ /// from the last clicked one to `i` takes `i`'s new state, as in a file manager: click the
+ /// first, Shift-click the last, and the whole range is checked (or unchecked).
+ pub fn click(&mut self, i: usize, shift: bool) {
+ if !self.importable(i) {
+ return;
+ }
+ let Some(on) = self.checked.get(i).map(|c| !c) else { return };
+ let from = if shift { self.last_clicked.filter(|a| *a < self.checked.len()).unwrap_or(i) } else { i };
+ for k in from.min(i)..=from.max(i) {
+ if self.importable(k)
+ && let Some(c) = self.checked.get_mut(k)
+ {
+ *c = on;
+ }
+ }
+ self.last_clicked = Some(i);
+ }
pub fn selected_paths(&self) -> Vec<String> {
self.candidates
.iter()
@@@ -1293,16 -1206,48 +1315,61 @@@ fn field<R>(ui: &mut egui::Ui, label: &
mod tests {
use super::*;
+ #[test]
+ fn grid_height_follows_the_window() {
+ // the grid takes what the options and buttons leave, in whole points
+ assert_eq!(grid_height(700.0, 300.0, 142.0), 400.0);
+ assert_eq!(grid_height(1000.5, 300.0, 142.0), 700.0);
+ // never less than one row of thumbnails
+ assert_eq!(grid_height(200.0, 300.0, 142.0), 142.0);
+ // a bad measurement doesn't take the grid away or make it endless
+ assert_eq!(grid_height(700.0, -50.0, 142.0), 700.0);
+ assert_eq!(grid_height(700.0, f32::NAN, 142.0), 700.0);
+ assert_eq!(grid_height(f32::INFINITY, 300.0, 142.0), DIALOG_SIZE[1] - DEFAULT_BELOW_GRID);
+ }
++
+ /// Six candidates, all checked; candidate 3 is a duplicate (unchecked, not importable).
+ fn dialog() -> ImportDialog {
+ let mut candidates: Vec<ImportCandidate> = (0..6).map(|i| ImportCandidate { path: format!("img{i}.jpg"), ..Default::default() }).collect();
+ candidates[3].duplicate = Some("content".into());
+ ImportDialog::new(candidates)
+ }
+
+ #[test]
+ fn click_toggles_one_candidate() {
+ let mut d = dialog();
+ d.click(1, false);
+ assert_eq!(d.checked, [true, false, true, false, true, true]);
+ d.click(1, false);
+ assert_eq!(d.checked, [true, true, true, false, true, true]);
+ d.click(3, false);
+ assert_eq!(d.checked, [true, true, true, false, true, true], "a duplicate can't be checked");
+ }
+
+ #[test]
+ fn shift_click_sets_the_range_to_the_clicked_state() {
+ let mut d = dialog();
+ d.click(1, false);
+ d.click(4, true);
+ assert_eq!(d.checked, [true, false, false, false, false, true], "1..=4 unchecked, the duplicate left alone");
+ assert_eq!(d.selected_paths(), ["img0.jpg", "img5.jpg"]);
+ // backwards from the new anchor (4) re-checks 2..=4
+ d.click(2, true);
+ assert_eq!(d.checked, [true, false, true, false, true, true]);
+ }
+
+ #[test]
+ fn shift_click_without_an_earlier_click_toggles_one() {
+ let mut d = dialog();
+ d.click(2, true);
+ assert_eq!(d.checked, [true, true, false, false, true, true]);
+ }
+
+ #[test]
+ fn shift_click_with_a_stale_anchor_toggles_one() {
+ let mut d = dialog();
+ d.last_clicked = Some(99);
+ d.click(2, true);
+ assert_eq!(d.checked, [true, true, false, false, true, true]);
+ }
}