feat(core,fuse,gui): find photos that look alike

The duplicate finder gains an Exact / Similar switch. Similar asks the
daemon for PhotoSimilar, which starts a background pass that hashes the
thumbnail of every shot not hashed yet and replies at once with the sets
found so far and how far hashing has got. The dialog shows a progress bar
and asks again until the pass is done, then lists the sets.

The pass takes thumbnails from the server only: a photo without one is
skipped for the run instead of downloaded whole, and the dialog says how
many photos could not be compared. Decoding and hashing are spread over
the machine's cores.

Every similar photo starts ticked Keep, since photos that look alike can
be different shots. A similar photo stands for its whole shot, so one not
kept goes to Trash with the rest of its shot.
This commit is contained in:
nvrl committed 2026-09-28 09:16:54 +02:00
1 parent c98ed0d83b
commit a90eeecda9
16 files changed
+1276 -250

No files matched your search

+277 -47
View File
@@ -6,14 +6,58 @@ const COPY_SIZE: i32 = 150;
/// Each set's copies with their Keep toggles.
type SetToggles = Vec<Vec<(PhotoItem, gtk4::ToggleButton)>>;
/// How often the dialog asks again while the daemon hashes thumbnails.
const HASHING_POLL: std::time::Duration = std::time::Duration::from_secs(1);
/// What the finder looks for.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Finder {
/// Byte-identical copies, going by the server's content hash.
Exact,
/// Shots that look alike, going by their thumbnails.
Similar,
}
/// The dialog's parts that a search fills in.
struct FinderView {
ui: Rc<Ui>,
status: adw::StatusPage,
progress: gtk4::ProgressBar,
note: gtk4::Label,
stack: gtk4::Stack,
trash: gtk4::Button,
toolbar: adw::ToolbarView,
/// Bumped by every new search and when the dialog closes, so a reply or a
/// poll for an older one is dropped.
generation: Cell<u32>,
/// The Trash button's handler for the review on show.
clicked: RefCell<Option<glib::SignalHandlerId>>,
}
/// Open the duplicate finder: every set of photos stored more than once, side
/// by side, with the copy worth keeping ticked. The copies left unticked go to
/// Trash — one file each, never the rest of their shot.
/// Trash — one file each, never the rest of their shot. The Similar switch
/// looks for shots that look alike instead.
pub(crate) fn show_duplicates(ui: &Rc<Ui>) {
let progress = gtk4::ProgressBar::builder()
.show_text(true)
.width_request(280)
.halign(gtk4::Align::Center)
.visible(false)
.build();
let note = gtk4::Label::builder()
.wrap(true)
.justify(gtk4::Justification::Center)
.visible(false)
.build();
note.add_css_class("dim-label");
let status_extra = gtk4::Box::new(gtk4::Orientation::Vertical, 12);
status_extra.append(&progress);
status_extra.append(&note);
let status = adw::StatusPage::builder()
.icon_name("edit-copy-symbolic")
.title(gettext("Looking for duplicates…"))
.vexpand(true)
.child(&status_extra)
.build();
status.add_css_class("compact");
@@ -21,7 +65,6 @@ pub(crate) fn show_duplicates(ui: &Rc<Ui>) {
stack.add_named(&status, Some("status"));
let trash = gtk4::Button::builder()
.label(gettext("Move to Trash"))
.halign(gtk4::Align::End)
.sensitive(false)
.build();
@@ -32,8 +75,25 @@ pub(crate) fn show_duplicates(ui: &Rc<Ui>) {
bottom.set_halign(gtk4::Align::End);
bottom.append(&trash);
let exact = gtk4::ToggleButton::builder()
.label(pgettext("duplicates", "Exact"))
.tooltip_text(gettext("Photos stored more than once"))
.active(true)
.build();
let similar = gtk4::ToggleButton::builder()
.label(pgettext("duplicates", "Similar"))
.tooltip_text(gettext("Photos that look alike"))
.group(&exact)
.build();
let switch = gtk4::Box::new(gtk4::Orientation::Horizontal, 0);
switch.add_css_class("linked");
switch.append(&exact);
switch.append(&similar);
let header = adw::HeaderBar::new();
header.set_title_widget(Some(&switch));
let toolbar = adw::ToolbarView::new();
toolbar.add_top_bar(&adw::HeaderBar::new());
toolbar.add_top_bar(&header);
toolbar.set_content(Some(&stack));
toolbar.add_bottom_bar(&bottom);
toolbar.set_reveal_bottom_bars(false);
@@ -46,47 +106,189 @@ pub(crate) fn show_duplicates(ui: &Rc<Ui>) {
.build();
dialog.present(ui_window(ui).as_ref());
let rx = spawn_request(ui.dirs.control_socket(), Request::PhotoDuplicates);
let ui = ui.clone();
let view = Rc::new(FinderView {
ui: ui.clone(),
status,
progress,
note,
stack,
trash,
toolbar,
generation: Cell::new(0),
clicked: RefCell::new(None),
});
let view_closed = view.clone();
dialog.connect_closed(move |_| {
view_closed
.generation
.set(view_closed.generation.get().wrapping_add(1));
});
let view_switch = view.clone();
similar.connect_toggled(move |similar| {
let finder = if similar.is_active() {
Finder::Similar
} else {
Finder::Exact
};
search(&view_switch, finder);
});
search(&view, Finder::Exact);
}
/// Drop what the dialog shows and look again, for `finder`.
fn search(view: &Rc<FinderView>, finder: Finder) {
let generation = view.generation.get().wrapping_add(1);
view.generation.set(generation);
if let Some(review) = view.stack.child_by_name("review") {
view.stack.remove(&review);
}
if let Some(handler) = view.clicked.take() {
view.trash.disconnect(handler);
}
view.toolbar.set_reveal_bottom_bars(false);
view.status.set_icon_name(Some("edit-copy-symbolic"));
view.status.set_title(&match finder {
Finder::Exact => gettext("Looking for duplicates…"),
Finder::Similar => gettext("Looking for similar photos…"),
});
view.status.set_description(None);
view.progress.set_visible(false);
view.note.set_visible(false);
view.stack.set_visible_child_name("status");
ask(view, finder, generation);
}
/// Ask the daemon for `finder`'s sets and show them, or, while it is still
/// hashing thumbnails, how far it has got — and ask again in a moment.
fn ask(view: &Rc<FinderView>, finder: Finder, generation: u32) {
let request = match finder {
Finder::Exact => Request::PhotoDuplicates,
Finder::Similar => Request::PhotoSimilar,
};
let rx = spawn_request(view.ui.dirs.control_socket(), request);
let view = view.clone();
glib::spawn_future_local(async move {
let sets = match rx.recv().await {
Ok(Ok(Response::PhotoDuplicates { sets })) => sets,
let reply = rx.recv().await;
if view.generation.get() != generation {
return;
}
let failed = match finder {
Finder::Exact => gettext("Couldn't look for duplicates"),
Finder::Similar => gettext("Couldn't look for similar photos"),
};
let (sets, uncompared) = match reply {
Ok(Ok(Response::PhotoDuplicates { sets })) => (sets, 0),
Ok(Ok(Response::PhotoSimilar {
hashing: Some(MappingProgress { done, total }),
..
})) => {
view.status.set_title(&gettext("Comparing photos…"));
view.status.set_description(Some(&gettext(
"Photos are compared by their thumbnails. The first time takes a while.",
)));
view.progress.set_fraction(if total == 0 {
0.0
} else {
done as f64 / total as f64
});
view.progress.set_text(Some(&gettext_f(
// Translators: {done} and {total} are numbers of photos.
"{done} of {total}",
&[("done", &thousands(done)), ("total", &thousands(total))],
)));
view.progress.set_visible(true);
glib::timeout_add_local_once(HASHING_POLL, move || {
if view.generation.get() == generation {
ask(&view, finder, generation);
}
});
return;
}
Ok(Ok(Response::PhotoSimilar {
sets,
hashing: None,
uncompared,
})) => (sets, uncompared),
Ok(Ok(Response::Error { message, .. })) => {
status.set_icon_name(Some("dialog-warning-symbolic"));
status.set_title(&gettext("Couldn't look for duplicates"));
status.set_description(Some(&message));
view.status.set_icon_name(Some("dialog-warning-symbolic"));
view.status.set_title(&failed);
view.status.set_description(Some(&message));
view.progress.set_visible(false);
return;
}
_ => {
status.set_icon_name(Some("dialog-warning-symbolic"));
status.set_title(&gettext("Couldn't look for duplicates"));
status.set_description(Some(&gettext("The mount service didn't respond.")));
view.status.set_icon_name(Some("dialog-warning-symbolic"));
view.status.set_title(&failed);
view.status
.set_description(Some(&gettext("The mount service didn't respond.")));
view.progress.set_visible(false);
return;
}
};
view.progress.set_visible(false);
if sets.is_empty() {
status.set_icon_name(Some("emblem-ok-symbolic"));
status.set_title(&gettext("No duplicates"));
status.set_description(Some(&gettext(
"Every photo in your library is stored once.",
)));
view.status.set_icon_name(Some("emblem-ok-symbolic"));
match finder {
Finder::Exact => {
view.status.set_title(&gettext("No duplicates"));
view.status.set_description(Some(&gettext(
"Every photo in your library is stored once.",
)));
}
Finder::Similar => {
view.status.set_title(&gettext("No similar photos"));
view.status.set_description(Some(&gettext(
"No two photos in your library look alike.",
)));
}
}
if uncompared > 0 {
view.note.set_label(&uncompared_note(uncompared));
view.note.set_visible(true);
}
return;
}
let review = duplicate_review(&ui, sets, &trash);
stack.add_named(&review, Some("review"));
stack.set_visible_child_name("review");
toolbar.set_reveal_bottom_bars(true);
schedule_thumbs(&ui);
let (review, handler) = duplicate_review(&view.ui, sets, &view.trash, finder, uncompared);
view.clicked.replace(Some(handler));
view.stack.add_named(&review, Some("review"));
view.stack.set_visible_child_name("review");
view.toolbar.set_reveal_bottom_bars(true);
schedule_thumbs(&view.ui);
});
}
/// The review list: one section per set, each copy a thumbnail with a Keep
/// toggle. Wires `trash` to move every copy not kept to Trash.
fn duplicate_review(ui: &Rc<Ui>, sets: Vec<Vec<PhotoItem>>, trash: &gtk4::Button) -> gtk4::Widget {
/// Says how many photos the similar finder left out for want of a thumbnail.
fn uncompared_note(n: usize) -> String {
ngettext_f(
"{n} photo couldn't be compared because it has no thumbnail yet.",
"{n} photos couldn't be compared because they have no thumbnail yet.",
n as u64,
&[],
)
}
/// The review list: one section per set, each photo a thumbnail with a Keep
/// toggle. Wires `trash` to move every photo not kept to Trash, and returns
/// that handler so a new search can take it off again.
///
/// Exact copies start with only the first of each set kept. Similar photos
/// all start kept: photos that look alike can still be different shots.
fn duplicate_review(
ui: &Rc<Ui>,
sets: Vec<Vec<PhotoItem>>,
trash: &gtk4::Button,
finder: Finder,
uncompared: usize,
) -> (gtk4::Widget, glib::SignalHandlerId) {
let intro = gtk4::Label::builder()
.label(gettext(
"These photos are stored more than once. The copies you don't keep move to Trash, where you can restore them.",
))
.label(match finder {
Finder::Exact => gettext(
"These photos are stored more than once. The copies you don't keep move to Trash, where you can restore them.",
),
Finder::Similar => gettext(
"These photos look alike. They can be copies of one photo or different shots of one moment, so all of them are kept until you untick them. The photos you don't keep move to Trash, where you can restore them.",
),
})
.wrap(true)
.xalign(0.0)
.build();
@@ -98,6 +300,15 @@ fn duplicate_review(ui: &Rc<Ui>, sets: Vec<Vec<PhotoItem>>, trash: &gtk4::Button
content.set_margin_start(18);
content.set_margin_end(18);
content.append(&intro);
if uncompared > 0 {
let note = gtk4::Label::builder()
.label(uncompared_note(uncompared))
.wrap(true)
.xalign(0.0)
.build();
note.add_css_class("dim-label");
content.append(&note);
}
// Every copy's toggle, by set, so the button can count what goes.
let toggles: Rc<RefCell<SetToggles>> = Rc::new(RefCell::new(Vec::new()));
@@ -106,19 +317,28 @@ fn duplicate_review(ui: &Rc<Ui>, sets: Vec<Vec<PhotoItem>>, trash: &gtk4::Button
continue;
};
let date = section_heading(first.capture_time, Grouping::Day);
// Translators: heading of one set in the duplicates dialog; {date} is the day the photo was taken.
let title = ngettext_f(
"{date}, {n} copy",
"{date}, {n} copies",
set.len() as u64,
&[("date", &date)],
);
let title = match finder {
// Translators: heading of one set in the duplicates dialog; {date} is the day the photo was taken.
Finder::Exact => ngettext_f(
"{date}, {n} copy",
"{date}, {n} copies",
set.len() as u64,
&[("date", &date)],
),
// Translators: heading of one set of photos that look alike; {date} is the day the first was taken.
Finder::Similar => ngettext_f(
"{date}, {n} photo",
"{date}, {n} photos",
set.len() as u64,
&[("date", &date)],
),
};
let heading = gtk4::Label::builder().label(title).xalign(0.0).build();
heading.add_css_class("heading");
let row = gtk4::Box::new(gtk4::Orientation::Horizontal, 12);
let mut set_toggles = Vec::new();
for (index, photo) in set.into_iter().enumerate() {
let (card, keep) = copy_card(ui, &photo, index == 0);
let (card, keep) = copy_card(ui, &photo, index == 0 || finder == Finder::Similar);
row.append(&card);
set_toggles.push((photo, keep));
}
@@ -139,12 +359,20 @@ fn duplicate_review(ui: &Rc<Ui>, sets: Vec<Vec<PhotoItem>>, trash: &gtk4::Button
let trash = trash.clone();
move || {
let n = doomed(&toggles.borrow()).len();
trash.set_label(&ngettext_f(
"Move {n} copy to Trash",
"Move {n} copies to Trash",
n as u64,
&[],
));
trash.set_label(&match finder {
Finder::Exact => ngettext_f(
"Move {n} copy to Trash",
"Move {n} copies to Trash",
n as u64,
&[],
),
Finder::Similar => ngettext_f(
"Move {n} photo to Trash",
"Move {n} photos to Trash",
n as u64,
&[],
),
});
trash.set_sensitive(n > 0);
}
};
@@ -171,12 +399,14 @@ fn duplicate_review(ui: &Rc<Ui>, sets: Vec<Vec<PhotoItem>>, trash: &gtk4::Button
}
let ui_trash = ui.clone();
trash.connect_clicked(move |button| {
let handler = trash.connect_clicked(move |button| {
let uids = doomed(&toggles.borrow());
if uids.is_empty() {
return;
}
trash_photos(&ui_trash, uids, true);
// An exact copy is one file of its shot; a similar photo stands for
// its whole shot, raw and all.
trash_photos(&ui_trash, uids, finder == Finder::Exact);
if let Some(dialog) = button
.ancestor(adw::Dialog::static_type())
.and_downcast::<adw::Dialog>()
@@ -190,7 +420,7 @@ fn duplicate_review(ui: &Rc<Ui>, sets: Vec<Vec<PhotoItem>>, trash: &gtk4::Button
.vexpand(true)
.child(&content)
.build();
scroll.upcast()
(scroll.upcast(), handler)
}
/// The uids of every copy not kept.