added album to photos, renamed gallery view

This commit is contained in:
nvrl committed 2026-07-31 20:31:25 +02:00
1 parent d988236bfa
commit 2336ad9e87
25 files changed
+1510 -378

No files matched your search

+82 -150
View File
@@ -4,6 +4,7 @@ pub(crate) mod pages;
pub(crate) mod widgets;
use pages::activity::*;
use pages::albums::*;
use pages::browser::*;
use pages::devices::*;
use pages::locations::*;
@@ -43,8 +44,8 @@ use pdfs_core::auth;
use pdfs_core::config::AppDirs;
use pdfs_core::control::{
ActivityEntry, ActivityKind, BookmarkInfo, DeviceInfo, DirEntry, ErrorKind, InvitationInfo,
JobItem, PhotoItem, PhotoKind, PublicLinkInfo, RefreshScope, Request, Response,
ActivityEntry, ActivityKind, AlbumInfo, BookmarkInfo, DeviceInfo, DirEntry, ErrorKind,
InvitationInfo, JobItem, PhotoItem, PhotoKind, PublicLinkInfo, RefreshScope, Request, Response,
RestorableFolder, RestoreItem, SearchHit, ShareEntry, ShareEntryKind, SharedItem,
SyncFolderInfo, SyncPhase, SyncProgress, TransferDirection, TransferItem, pending_summary,
send,
@@ -138,23 +139,10 @@ impl Ui {
}
}
/// The aspect ratio (w/h) to lay `uid`'s tile out at: the real one once its
/// thumbnail has been seen, otherwise [`RATIO_UNKNOWN`].
fn ratio(&self, uid: &str) -> f64 {
self.gallery
.photo_ratio
.borrow()
.get(uid)
.copied()
.unwrap_or(RATIO_UNKNOWN)
}
/// Remember a decoded thumbnail and the aspect ratio it revealed. Returns
/// true when the ratio is *new information* — the caller re-justifies the
/// affected rows, since they were laid out against a guess.
fn store_texture(&self, uid: &str, texture: gtk4::gdk::Texture) -> bool {
let (w, h) = (texture.width(), texture.height());
/// Remember a decoded thumbnail, evicting the oldest once the cache is full.
/// Scrolling back over a day then repaints from memory rather than decoding
/// the same JPEGs off disk again.
fn store_texture(&self, uid: &str, texture: gtk4::gdk::Texture) {
let mut cache = self.gallery.photo_tex.borrow_mut();
let mut order = self.gallery.photo_tex_order.borrow_mut();
if cache.insert(uid.to_string(), texture).is_none() {
@@ -165,58 +153,6 @@ impl Ui {
cache.remove(&old);
}
}
drop((cache, order));
if h <= 0 {
return false;
}
let ratio = f64::from(w) / f64::from(h);
let mut ratios = self.gallery.photo_ratio.borrow_mut();
match ratios.insert(uid.to_string(), ratio) {
// Only a *changed* ratio invalidates a layout; re-decoding a photo we
// already sized correctly must not trigger another pass.
Some(prev) if (prev - ratio).abs() < f64::EPSILON => false,
_ => {
self.gallery.photo_ratio_dirty.set(true);
true
}
}
}
/// Where the learned aspect ratios are persisted between runs.
fn ratio_path(&self) -> PathBuf {
self.dirs.cache_dir().join("photo-ratios.json")
}
/// Load the persisted aspect ratios. A missing or corrupt file just means the
/// first paint uses [`RATIO_UNKNOWN`] and re-justifies as thumbnails land.
fn load_ratios(&self) {
let path = self.ratio_path();
let Ok(text) = std::fs::read_to_string(&path) else {
return;
};
match serde_json::from_str::<HashMap<String, f64>>(&text) {
Ok(map) => *self.gallery.photo_ratio.borrow_mut() = map,
Err(e) => tracing::debug!("ignoring {}: {e}", path.display()),
}
}
/// Persist the learned aspect ratios, if any were learned since the last save.
fn save_ratios(&self) {
if !self.gallery.photo_ratio_dirty.replace(false) {
return;
}
let path = self.ratio_path();
let ratios = self.gallery.photo_ratio.borrow();
let write = serde_json::to_vec(&*ratios)
.map_err(std::io::Error::other)
.and_then(|bytes| {
std::fs::create_dir_all(path.parent().unwrap_or(Path::new("/")))?;
std::fs::write(&path, bytes)
});
if let Err(e) = write {
tracing::debug!("cannot save {}: {e}", path.display());
}
}
}
@@ -287,15 +223,23 @@ fn load_proton_theme() {
.viewer-spinner {{ color: white; }}\n\
.viewer-status {{ color: white; font-size: 1rem; background-color: rgba(0, 0, 0, 0.75); padding: 12px 24px; border-radius: 12px; }}\n\
.viewer-info-panel {{ background-color: @window_bg_color; border-left: 1px solid alpha(currentColor, 0.12); }}\n\
.gallery-day {{ font-size: 1.05rem; font-weight: 700; padding: 2px 2px 6px 2px; }}\n\
.photo-tile {{ padding: 0; margin: 0; min-height: 0; min-width: 0; border-radius: 10px; background: alpha(currentColor, 0.06); box-shadow: none; transition: filter 160ms ease, box-shadow 160ms ease; }}\n\
.photo-tile:hover {{ filter: brightness(1.06); box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35); }}\n\
.gallery-day {{ font-size: 0.82rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: alpha(currentColor, 0.65); padding: 4px 2px 8px 2px; }}\n\
.photo-tile {{ padding: 0; margin: 0; min-height: 0; min-width: 0; border-radius: 14px; background: alpha(currentColor, 0.06); box-shadow: none; transition: box-shadow 180ms ease, background 180ms ease; }}\n\
.photo-tile:hover {{ box-shadow: 0 8px 22px rgba(0, 0, 0, 0.45); }}\n\
.photo-tile:focus {{ outline: 2px solid {PROTON_PURPLE}; outline-offset: -2px; }}\n\
.photo-thumb {{ transition: transform 220ms ease; }}\n\
.photo-tile:hover .photo-thumb {{ transform: scale(1.06); }}\n\
.photo-placeholder {{ color: alpha(currentColor, 0.35); background: alpha(currentColor, 0.07); }}\n\
.photo-caption {{ font-size: 0.78rem; color: white; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9); padding: 22px 10px 6px 10px; opacity: 0; transition: opacity 160ms ease; }}\n\
.photo-caption {{ font-size: 0.78rem; color: white; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9); padding: 22px 10px 6px 10px; opacity: 0; transition: opacity 180ms ease; }}\n\
.photo-video-badge {{ color: white; background: rgba(0, 0, 0, 0.45); border-radius: 999px; padding: 8px; min-width: 20px; min-height: 20px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5); transition: background 160ms ease; }}\n\
.photo-tile:hover .photo-video-badge {{ background: alpha({PROTON_PURPLE}, 0.85); }}\n\
.photo-tile:hover .photo-caption {{ opacity: 1; background: linear-gradient(to top, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0)); }}\n\
.album-card {{ padding: 0; border-radius: 16px; transition: background 180ms ease; }}\n\
.album-card:hover {{ background: alpha(currentColor, 0.07); }}\n\
.album-cover {{ border-radius: 16px; background: alpha(currentColor, 0.06); box-shadow: 0 4px 16px rgba(0, 0, 0, 0.32); transition: box-shadow 180ms ease; }}\n\
.album-card:hover .album-cover {{ box-shadow: 0 10px 26px rgba(0, 0, 0, 0.5); }}\n\
.album-card:hover .photo-thumb {{ transform: scale(1.05); }}\n\
.view-switch button {{ padding-left: 18px; padding-right: 18px; }}\n\
.card {{ border-radius: 8px; transition: transform 0.2s ease, filter 0.2s ease; margin: 4px; }}\n\
.card:hover {{ transform: scale(1.02); filter: brightness(0.9); }}\n\
.navigation-sidebar row {{ border-radius: 8px; margin: 2px 6px; }}\n\
@@ -462,9 +406,21 @@ fn build_window(app: &adw::Application) {
retry: gallery_widgets.retry.clone(),
more: gallery_widgets.more.clone(),
upload: gallery_widgets.upload.clone(),
title: gallery_widgets.title.clone(),
subtitle: gallery_widgets.subtitle.clone(),
albums: gallery_widgets.albums.clone(),
albums_stack: gallery_widgets.albums_stack.clone(),
albums_status: gallery_widgets.albums_status.clone(),
photos_btn: gallery_widgets.photos_btn.clone(),
albums_btn: gallery_widgets.albums_btn.clone(),
view_switch: gallery_widgets.view_switch.clone(),
albums_loading: Cell::new(false),
album: RefCell::new(None),
back: gallery_widgets.back.clone(),
filters: gallery_widgets.filters.clone(),
kind: Cell::new(None),
tabs: gallery_widgets.tabs.clone(),
counts: Cell::new(None),
dates: gallery_widgets.dates.clone(),
date_ranges: RefCell::new(vec![None]),
range: Cell::new(None),
@@ -473,8 +429,6 @@ fn build_window(app: &adw::Application) {
width: Cell::new(0),
photo_tex: RefCell::new(HashMap::new()),
photo_tex_order: RefCell::new(VecDeque::new()),
photo_ratio: RefCell::new(HashMap::new()),
photo_ratio_dirty: Cell::new(false),
photo_nothumb: RefCell::new(HashSet::new()),
thumb_wanted: RefCell::new(HashMap::new()),
thumb_queue: RefCell::new(VecDeque::new()),
@@ -538,8 +492,6 @@ fn build_window(app: &adw::Application) {
key: RefCell::new(None),
},
});
ui.load_ratios();
wire_login(&ui);
wire_logout(&ui, &main_widgets.logout_button);
wire_settings(
@@ -558,6 +510,7 @@ fn build_window(app: &adw::Application) {
wire_details(&ui);
wire_search(&ui);
wire_gallery(&ui, &gallery_widgets.list, &gallery_widgets.scroll);
wire_albums(&ui);
wire_trash(&ui, &trash_widgets.list, &trash_widgets.empty);
wire_shared(&ui, &shared_widgets.retry, &shared_widgets.add_bookmark);
wire_shared_by_me(&ui, &shared_by_me_widgets.retry);
@@ -626,12 +579,10 @@ fn build_window(app: &adw::Application) {
glib::ControlFlow::Continue
});
let cell = RefCell::new(Some(source));
let ui_close = ui.clone();
window.connect_close_request(move |_| {
if let Some(id) = cell.borrow_mut().take() {
id.remove();
}
ui_close.save_ratios();
glib::Propagation::Proceed
});
@@ -646,7 +597,7 @@ const DESTINATIONS: [(&str, &str, &str); 9] = [
("shared", "Shared with me", "system-users-symbolic"),
("locations", "Locations", "drive-harddisk-symbolic"),
("devices", "Computers", "computer-symbolic"),
("gallery", "Photos", "image-x-generic-symbolic"),
("gallery", "Gallery", "image-x-generic-symbolic"),
("activity", "Activity", "document-open-recent-symbolic"),
("trash", "Trash", "user-trash-symbolic"),
("main", "Settings", "emblem-system-symbolic"),
@@ -749,7 +700,15 @@ fn reload_current_page(ui: &Rc<Ui>) {
let path = ui.browser.path.borrow().clone();
refresh_then(ui, RefreshScope::Dir { path }, load_browser);
}
Some("gallery") => refresh_then(ui, RefreshScope::Photos, |ui| load_gallery(ui, false)),
// One scope covers the whole photos view, so Refresh reloads whichever of
// the two — album grid or timeline/album — is actually on screen.
Some("gallery") => refresh_then(ui, RefreshScope::Photos, |ui| {
if ui.gallery.content.visible_child_name().as_deref() == Some("albums") {
load_albums(ui);
} else {
load_gallery(ui, false);
}
}),
Some("trash") => refresh_then(ui, RefreshScope::Trash, load_trash),
Some("shared") => {
ui.shared.loaded_at.set(None);
@@ -1064,81 +1023,54 @@ fn human_bytes(bytes: u64) -> String {
mod tests {
use super::*;
/// A justified row must fill the content width exactly: tile widths plus the
/// gaps between them add up to the width, with nothing left ragged.
fn row_width(row: &[(i32, i32)]) -> i32 {
let gaps = TILE_GAP * (row.len().saturating_sub(1)) as i32;
row.iter().map(|(width, _)| *width).sum::<i32>() + gaps
/// The full width one row of `columns` tiles occupies, gaps included.
fn row_width(columns: usize, edge: i32) -> i32 {
edge * columns as i32 + TILE_GAP * (columns as i32 - 1)
}
#[test]
fn justified_rows_fill_the_width_exactly() {
let ratios = [
1.5, 0.75, 1.5, 1.33, 0.66, 1.5, 1.0, 2.2, 1.5, 0.8, 1.5, 1.6,
];
let rows = plan_rows(&ratios, 180.0, 1000.0);
assert!(
rows.len() > 1,
"12 photos at 180px should need several rows"
);
// Every row but the trailing one is justified.
for row in &rows[..rows.len() - 1] {
assert_eq!(row_width(row), 1000);
fn the_grid_spans_the_content_width() {
// Whatever the width, the columns plus their gaps land on it (bar the
// few px integer division cannot divide), so no ragged right margin.
for width in [640, 900, 1000, 1440, 1920, 2560] {
let (columns, edge) = plan_grid(TILE_DEFAULT, width);
let used = row_width(columns, edge);
assert!(
used <= width && width - used < columns as i32,
"{columns} x {edge}px = {used}px does not span {width}px"
);
}
}
#[test]
fn every_photo_lands_in_exactly_one_row() {
let ratios: Vec<f64> = (0..37).map(|i| 0.5 + f64::from(i % 5) * 0.4).collect();
let rows = plan_rows(&ratios, 180.0, 1000.0);
let tiles: usize = rows.iter().map(Vec::len).sum();
assert_eq!(tiles, ratios.len());
}
#[test]
fn tiles_keep_their_aspect_ratio() {
let rows = plan_rows(&[1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], 180.0, 1000.0);
// All but the last tile of a justified row are sized straight from the
// ratio, so w/h is the photo's own — no cropping needed to place it.
for row in &rows {
for (width, height) in &row[..row.len() - 1] {
let ratio = f64::from(*width) / f64::from(*height);
assert!((ratio - 1.5).abs() < 0.02, "{width}x{height} is not 3:2");
}
}
}
#[test]
fn trailing_row_is_not_stretched() {
// Two photos can't fill a 1000px row at 180px tall, so they must keep the
// target height rather than being blown up to a screen-wide row.
let rows = plan_rows(&[1.5, 1.5], 180.0, 1000.0);
assert_eq!(rows.len(), 1);
assert!(rows[0].iter().all(|(_, height)| *height == 180));
assert!(row_width(&rows[0]) < 1000);
}
#[test]
fn an_extreme_panorama_cannot_squash_its_row() {
// Unclamped, a 20:1 panorama would drag its whole row down to ~40px tall.
// Clamped at RATIO_MAX it stays a wide tile in a row of normal height.
let rows = plan_rows(&[20.0, 1.5, 1.5], 180.0, 1000.0);
let (width, height) = rows[0][0];
assert!(height > 120, "row squashed to {height}px");
assert!(width <= 1000, "tile overflows the row at {width}px");
assert_eq!(row_width(&rows[0]), 1000);
}
#[test]
fn narrow_widths_and_no_photos_are_survivable() {
assert!(plan_rows(&[], 180.0, 1000.0).is_empty());
// Width below the floor is clamped rather than producing zero-px tiles.
let rows = plan_rows(&[1.5, 1.5], 180.0, 0.0);
fn tiles_stay_near_the_target_size() {
// The column count rounds and the edge absorbs the remainder, so a tile
// is never more than one gap-and-a-bit away from what was asked for.
let (columns, edge) = plan_grid(TILE_DEFAULT, 1920);
assert!(columns >= 7, "1920px should hold several 220px tiles");
assert!(
rows.iter()
.flatten()
.all(|(width, height)| *width > 0 && *height > 0)
(edge - TILE_DEFAULT).abs() < TILE_DEFAULT / 3,
"{edge}px is not near the {TILE_DEFAULT}px target"
);
}
#[test]
fn zooming_in_widens_the_tiles_and_drops_columns() {
let (wide_columns, small_edge) = plan_grid(TILE_MIN, 1200);
let (few_columns, big_edge) = plan_grid(TILE_MAX, 1200);
assert!(wide_columns > few_columns);
assert!(big_edge > small_edge);
}
#[test]
fn a_window_narrower_than_one_tile_still_gets_a_column() {
// A single column at whatever fits, rather than zero columns (which would
// divide by zero) or a zero-px tile.
let (columns, edge) = plan_grid(TILE_DEFAULT, 40);
assert_eq!(columns, 1);
assert!(edge > 0);
let (columns, edge) = plan_grid(TILE_DEFAULT, 0);
assert_eq!(columns, 1);
assert!(edge > 0);
}
}
+265
View File
@@ -0,0 +1,265 @@
//! The Albums view of the Photos page: a grid of album covers, and the album
//! that opens when one is clicked.
//!
//! An open album reuses the gallery wholesale — same square grid, same
//! on-demand thumbnails, same lightbox — by parking the album in
//! [`GalleryState::album`] and letting [`load_gallery`] page
//! [`Request::AlbumPhotos`] instead of the timeline. Only the header and the
//! filter bar differ, because an album is served whole rather than filtered.
use crate::*;
/// Edge length in px of an album cover in the grid. Bigger than a timeline tile:
/// there are far fewer albums than photos, and the cover is the only thing
/// identifying one at a glance.
const COVER_EDGE: i32 = 200;
/// Show the Albums grid and (re)load it. Called when the Albums toggle goes on.
pub(crate) fn show_albums(ui: &Rc<Ui>) {
close_album(ui);
ui.gallery.content.set_visible_child_name("albums");
load_albums(ui);
}
/// Ask the daemon for the album listing and rebuild the grid.
pub(crate) fn load_albums(ui: &Rc<Ui>) {
if ui.gallery.albums_loading.get() {
return;
}
ui.gallery.albums_loading.set(true);
albums_status(
ui,
"view-grid-symbolic",
"Loading albums…",
"Reading your Proton Drive albums.",
);
let rx = spawn_request(ui.dirs.control_socket(), Request::PhotoAlbums);
let ui = ui.clone();
glib::spawn_future_local(async move {
let result = rx.recv().await;
ui.gallery.albums_loading.set(false);
match result {
Ok(Ok(Response::Albums {
available: false, ..
})) => albums_status(
&ui,
"image-missing-symbolic",
"No photo library",
"This Proton account doesn't have Photos enabled.",
),
Ok(Ok(Response::Albums { items, .. })) if items.is_empty() => albums_status(
&ui,
"view-grid-symbolic",
"No albums",
"Albums you create in Proton Photos appear here.",
),
Ok(Ok(Response::Albums { items, .. })) => {
fill_albums(&ui, &items);
ui.gallery.albums_stack.set_visible_child_name("grid");
}
Ok(Ok(Response::Error { message, kind })) => {
toast_failure(&ui, "Couldn't load albums", &message, kind);
albums_status(
&ui,
"dialog-warning-symbolic",
"Couldn't load albums",
&message,
);
}
Ok(Ok(_)) | Ok(Err(_)) | Err(_) => albums_status(
&ui,
"network-offline-symbolic",
"Not connected",
"The Proton Drive mount service didn't respond.",
),
}
});
}
/// Replace the grid's cards with `albums`, in the order the daemon gave them
/// (newest activity first).
fn fill_albums(ui: &Rc<Ui>, albums: &[AlbumInfo]) {
while let Some(child) = ui.gallery.albums.first_child() {
ui.gallery.albums.remove(&child);
}
for album in albums {
ui.gallery.albums.append(&album_card(ui, album));
}
// The covers are ordinary photos as far as the daemon is concerned, so the
// batch that fills them is the gallery's own.
schedule_thumbs(ui);
}
/// One album as a clickable card: its cover, its name, and how many photos it
/// holds. An album shared with us says so — it lives on someone else's volume,
/// and that is worth knowing before opening it.
fn album_card(ui: &Rc<Ui>, album: &AlbumInfo) -> gtk4::Button {
let picture = gtk4::Picture::builder()
// Fills and crops the cover square, like a timeline tile.
.content_fit(gtk4::ContentFit::Cover)
.can_shrink(true)
.hexpand(true)
.vexpand(true)
.build();
picture.add_css_class("photo-thumb");
let placeholder = gtk4::Image::builder()
.icon_name("view-grid-symbolic")
.pixel_size(32)
.halign(gtk4::Align::Center)
.valign(gtk4::Align::Center)
.build();
placeholder.add_css_class("photo-placeholder");
let cover = gtk4::Overlay::new();
cover.set_child(Some(&placeholder));
cover.add_overlay(&picture);
cover.set_size_request(COVER_EDGE, COVER_EDGE);
cover.set_overflow(gtk4::Overflow::Hidden);
cover.add_css_class("album-cover");
let name = gtk4::Label::builder()
.label(&album.name)
.halign(gtk4::Align::Start)
.xalign(0.0)
.ellipsize(gtk4::pango::EllipsizeMode::End)
.build();
name.add_css_class("heading");
let count = gtk4::Label::builder()
.label(album_subtitle(album))
.halign(gtk4::Align::Start)
.xalign(0.0)
.ellipsize(gtk4::pango::EllipsizeMode::End)
.build();
count.add_css_class("dim-label");
count.add_css_class("caption");
let card = gtk4::Box::new(gtk4::Orientation::Vertical, 4);
card.append(&cover);
card.append(&name);
card.append(&count);
let button = gtk4::Button::builder().child(&card).build();
button.add_css_class("flat");
button.add_css_class("album-card");
button.set_tooltip_text(Some(&album.name));
if let Some(uid) = album.cover_uid.clone() {
want_cover(ui, uid, &picture);
}
let ui_open = ui.clone();
let album = album.clone();
button.connect_clicked(move |_| open_album(&ui_open, album.clone()));
button
}
/// "12 photos", plus where the album came from when it isn't ours.
fn album_subtitle(album: &AlbumInfo) -> String {
let photos = match album.photo_count {
1 => "1 photo".to_string(),
n => format!("{n} photos"),
};
if album.shared {
format!("{photos} · shared with me")
} else {
photos
}
}
/// Queue an album cover for the gallery's own thumbnail machinery: same batching,
/// same texture cache, same decode pacing as a timeline tile.
fn want_cover(ui: &Rc<Ui>, uid: String, picture: &gtk4::Picture) {
if let Some(texture) = ui.gallery.photo_tex.borrow().get(&uid) {
picture.set_paintable(Some(texture));
return;
}
if ui.gallery.photo_nothumb.borrow().contains(&uid) {
return;
}
ui.gallery
.thumb_wanted
.borrow_mut()
.insert(uid.clone(), picture.clone());
let mut queue = ui.gallery.thumb_queue.borrow_mut();
if !queue.contains(&uid) {
queue.push_back(uid);
}
}
/// Open one album in the gallery: the timeline view, paged from the album
/// instead, with the filters that don't apply to it out of the way.
pub(crate) fn open_album(ui: &Rc<Ui>, album: AlbumInfo) {
ui.gallery.title.set_label(&album.name);
ui.gallery.subtitle.set_label(&album_subtitle(&album));
ui.gallery.subtitle.set_visible(true);
*ui.gallery.album.borrow_mut() = Some(album);
// An album page carries no kind or date filter, and Upload targets the
// timeline rather than an album — hide those rather than offer controls that
// would silently do something else. The Photos/Albums switcher goes too: an
// album is a level below it, and back is the way out.
ui.gallery.filters.set_visible(false);
ui.gallery.view_switch.set_visible(false);
ui.gallery.upload.set_visible(false);
ui.gallery.back.set_visible(true);
// A stale kind/date filter would otherwise be sent with the album request.
ui.gallery.kind.set(None);
ui.gallery.range.set(None);
ui.gallery.content.set_visible_child_name("timeline");
load_gallery(ui, false);
}
/// Leave an open album, restoring the timeline's header and filters. A no-op
/// when no album is open, so callers can use it as "make sure we're not in one".
pub(crate) fn close_album(ui: &Rc<Ui>) {
if ui.gallery.album.borrow_mut().take().is_none() {
return;
}
ui.gallery.title.set_label("Gallery");
ui.gallery.filters.set_visible(true);
ui.gallery.view_switch.set_visible(true);
ui.gallery.upload.set_visible(true);
ui.gallery.back.set_visible(false);
}
/// Show the album grid's own status page (loading, empty, or an error).
fn albums_status(ui: &Rc<Ui>, icon: &str, title: &str, description: &str) {
ui.gallery.albums_status.set_icon_name(Some(icon));
ui.gallery.albums_status.set_title(title);
ui.gallery.albums_status.set_description(Some(description));
ui.gallery.albums_stack.set_visible_child_name("status");
}
/// Wire the Photos/Albums switcher and the back button. The switcher swaps the
/// whole content area; back leaves an open album for the grid it came from.
pub(crate) fn wire_albums(ui: &Rc<Ui>) {
let ui_albums = ui.clone();
ui.gallery.albums_btn.clone().connect_toggled(move |btn| {
if btn.is_active() {
show_albums(&ui_albums);
}
});
// Only the button being switched *on* acts: the group fires `toggled` for
// the one going off too, and acting on both would load twice.
let ui_photos = ui.clone();
ui.gallery.photos_btn.clone().connect_toggled(move |btn| {
if !btn.is_active() {
return;
}
// The timeline is reloaded rather than restored, because an open album
// left the model holding its own photos.
close_album(&ui_photos);
ui_photos.gallery.content.set_visible_child_name("timeline");
load_gallery(&ui_photos, false);
});
let ui_back = ui.clone();
ui.gallery.back.clone().connect_clicked(move |_| {
show_albums(&ui_back);
});
}
+1
View File
@@ -1,4 +1,5 @@
pub(crate) mod activity;
pub(crate) mod albums;
pub(crate) mod browser;
pub(crate) mod devices;
pub(crate) mod locations;
+265 -192
View File
@@ -8,24 +8,53 @@ pub(crate) struct GalleryState {
/// The day sections rendered by the Photos ListView, rebuilt from
/// [`Self::model`] by [`repaint_gallery`].
pub(crate) groups: gio::ListStore,
/// Target row height in px, retuned by Ctrl+scroll / Ctrl+± (see
/// [`zoom_gallery`]). Each tile keeps its own aspect ratio; rows are
/// justified to the content width around this height.
/// Target tile edge in px, retuned by Ctrl+scroll / Ctrl+± (see
/// [`zoom_gallery`]). The grid fits as many square tiles of about this size
/// as the content width holds.
pub(crate) tile: Cell<i32>,
/// Swaps the Photos content area between the timeline and its status page.
/// Swaps the Photos content area between the timeline, its status page, and
/// the Albums grid.
pub(crate) content: gtk4::Stack,
pub(crate) status: adw::StatusPage,
pub(crate) retry: gtk4::Button,
pub(crate) more: gtk4::Button,
pub(crate) upload: gtk4::Button,
/// "Photos", or the album's name while one is open.
pub(crate) title: gtk4::Label,
/// "1,204 photos" under the page title.
pub(crate) subtitle: gtk4::Label,
/// The album grid, and the stack that swaps it for its own status page.
pub(crate) albums: gtk4::FlowBox,
pub(crate) albums_stack: gtk4::Stack,
pub(crate) albums_status: adw::StatusPage,
/// The Photos/Albums switcher, and the box holding it — hidden while an
/// album is open, where the back button leads instead.
pub(crate) photos_btn: gtk4::ToggleButton,
pub(crate) albums_btn: gtk4::ToggleButton,
pub(crate) view_switch: gtk4::Box,
/// True while the album listing is in flight, so a re-entry into the Albums
/// view can't stack requests.
pub(crate) albums_loading: Cell<bool>,
/// The album currently open, or `None` when the timeline is showing. Set by
/// [`open_album`]; read by [`load_gallery`], which pages that album instead
/// of the timeline while it is set.
pub(crate) album: RefCell<Option<AlbumInfo>>,
/// Leaves an open album for the grid it was opened from. Visible only while
/// an album is open.
pub(crate) back: gtk4::Button,
/// The kind toggles and the date jump, hidden while an album is open — an
/// album page is served whole, not filtered.
pub(crate) filters: gtk4::Box,
/// Which tab (Photos / Videos / Raw) the timeline is filtered to, or `None`
/// for All. Read by [`load_gallery`] and set by the filter toggles.
pub(crate) kind: Cell<Option<PhotoKind>>,
/// The filter toggles, index-aligned with [`kind_for_tab`], kept so their
/// labels can carry live per-kind counts.
pub(crate) tabs: [gtk4::ToggleButton; 4],
/// Whole-timeline `(photos, videos, raw)` counts from the last page that
/// carried them, so the subtitle can say how big the library *is* rather
/// than how much of it has been paged in.
pub(crate) counts: Cell<Option<(usize, usize, usize)>>,
/// The date-jump dropdown ("All dates" then a month per timeline entry), and
/// the `[from, to)` window each of its rows selects (index-aligned; `None` is
/// "All dates"). Selecting a row loads that month via [`load_gallery`].
@@ -40,21 +69,14 @@ pub(crate) struct GalleryState {
/// True while a timeline page is in flight, so the scroll-to-the-end paging
/// can't fire a second request for the page already coming.
pub(crate) loading: Cell<bool>,
/// Content width the sections are currently justified to. Updated when the
/// ListView is resized, which re-justifies the visible sections.
/// Content width the grid is currently laid out to. Updated when the
/// ListView is resized, which re-flows the visible sections.
pub(crate) width: Cell<i32>,
/// Decoded thumbnails by photo uid, with the insertion order that evicts the
/// oldest past [`TEXTURE_CACHE_MAX`]. Scrolling back over a day therefore
/// repaints from memory instead of re-decoding from disk.
pub(crate) photo_tex: RefCell<HashMap<String, gtk4::gdk::Texture>>,
pub(crate) photo_tex_order: RefCell<VecDeque<String>>,
/// Aspect ratio (w/h) per uid, learned when a thumbnail is decoded and
/// persisted to disk, so a relaunch justifies its rows correctly on the first
/// frame instead of reflowing as thumbnails arrive.
pub(crate) photo_ratio: RefCell<HashMap<String, f64>>,
/// Ratios learned since the last save, so the ratio file is only rewritten
/// when it actually changed.
pub(crate) photo_ratio_dirty: Cell<bool>,
/// Photos the daemon reported as having no thumbnail at all, so a tile that
/// can never be filled isn't requested again on every scroll past it.
pub(crate) photo_nothumb: RefCell<HashSet<String>>,
@@ -75,46 +97,33 @@ pub(crate) struct GalleryState {
pub(crate) decode_queue: RefCell<VecDeque<(String, String)>>,
pub(crate) decode_idle: Cell<bool>,
/// Pending debounce timers for the thumbnail queue flush and the section
/// re-justify, replaced on each new trigger so only the last one fires.
/// re-flow, replaced on each new trigger so only the last one fires.
pub(crate) thumb_source: RefCell<Option<glib::SourceId>>,
pub(crate) relayout_source: RefCell<Option<glib::SourceId>>,
/// The day sections currently realised by the ListView, by their index in
/// [`Self::groups`]. A learned ratio or a resize re-justifies these
/// in place — rebuilding the ListStore instead would reset the scroll
/// position out from under the user.
/// [`Self::groups`]. A resize or a zoom step re-flows these in place —
/// rebuilding the ListStore instead would reset the scroll position out from
/// under the user.
pub(crate) bound: RefCell<HashMap<u32, gtk4::Box>>,
}
/// How many photos to pull per [`Request::PhotosTimeline`] page.
pub(crate) const PHOTOS_PAGE: usize = 60;
/// Gallery row height in px: the zoom range, its default, and the step one
/// Ctrl+scroll notch (or Ctrl+±) moves it by. Rows are justified to the full
/// content width, so this is the *target* height a row lands near, not an exact
/// tile size (see [`justify_rows`]).
/// Gallery tile edge in px: the zoom range, its default, and the step one
/// Ctrl+scroll notch (or Ctrl+±) moves it by. The grid divides the content width
/// evenly, so this is the *target* a tile lands near rather than its exact size
/// (see [`plan_grid`]).
pub(crate) const TILE_MIN: i32 = 90;
pub(crate) const TILE_MAX: i32 = 340;
pub(crate) const TILE_DEFAULT: i32 = 220;
pub(crate) const TILE_DEFAULT: i32 = 180;
pub(crate) const TILE_STEP: i32 = 30;
/// Gap between tiles, horizontally and vertically.
pub(crate) const TILE_GAP: i32 = 6;
/// Aspect ratio (w/h) assumed for a photo whose thumbnail hasn't been decoded
/// yet, so a tile can be laid out before its image exists. Landscape 3:2, the
/// commonest camera/phone ratio; once the thumbnail lands the real ratio
/// replaces it and the section re-justifies in place.
pub(crate) const RATIO_UNKNOWN: f64 = 1.5;
/// Aspect ratios are clamped to this range before a row is packed: one absurd
/// panorama, or a sliver of a portrait, must not be able to squash the row it
/// lands in down to nothing. The tile still shows the whole image.
pub(crate) const RATIO_MIN: f64 = 0.4;
pub(crate) const RATIO_MAX: f64 = 3.5;
pub(crate) const TILE_GAP: i32 = 8;
/// How many thumbnails one on-demand [`Request::PhotoThumbs`] batch asks for.
/// Small, so the first tiles on screen fill in quickly rather than the whole
@@ -134,7 +143,7 @@ pub(crate) const THUMB_RETRY: Duration = Duration::from_secs(4);
/// this caps the gallery's footprint while covering several screens of scroll.
pub(crate) const TEXTURE_CACHE_MAX: usize = 600;
/// Pause after a resize/zoom before the visible sections are re-justified.
/// Pause after a resize/zoom before the visible sections are re-flowed.
pub(crate) const RELAYOUT_DEBOUNCE: Duration = Duration::from_millis(80);
/// One day-section of the photos timeline: a heading plus the photos captured
@@ -153,9 +162,11 @@ pub(crate) struct GalleryWidgets {
pub(crate) model: gio::ListStore,
/// Day sections rendered by the ListView, derived from `model`.
pub(crate) groups: gio::ListStore,
/// Swaps between the timeline and the empty/loading/error status page.
/// Swaps between the timeline, the empty/loading/error status page, and the
/// Albums grid.
pub(crate) content: gtk4::Stack,
pub(crate) status: adw::StatusPage,
pub(crate) title: gtk4::Label,
pub(crate) subtitle: gtk4::Label,
pub(crate) more: gtk4::Button,
pub(crate) list: gtk4::ListView,
@@ -163,6 +174,17 @@ pub(crate) struct GalleryWidgets {
pub(crate) retry: gtk4::Button,
pub(crate) upload: gtk4::Button,
pub(crate) refresh: gtk4::Button,
/// The Albums grid, its own status page and the stack between them, plus the
/// Photos/Albums switcher and the back button out of an album.
pub(crate) albums: gtk4::FlowBox,
pub(crate) albums_stack: gtk4::Stack,
pub(crate) albums_status: adw::StatusPage,
pub(crate) photos_btn: gtk4::ToggleButton,
pub(crate) albums_btn: gtk4::ToggleButton,
pub(crate) view_switch: gtk4::Box,
pub(crate) back: gtk4::Button,
/// The kind toggles and date jump, as one box so an album view can hide them.
pub(crate) filters: gtk4::Box,
/// The All / Photos / Videos / Raw filter toggles, in that order (index maps
/// to [`kind_for_tab`]).
pub(crate) tabs: [gtk4::ToggleButton; 4],
@@ -217,7 +239,7 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) {
more.add_css_class("pill");
more.set_visible(false);
// Horizontal scrolling is never wanted: rows are justified to the viewport
// Horizontal scrolling is never wanted: the grid is sized to the viewport
// width, and a stray hscrollbar would fight the layout.
let scroll = gtk4::ScrolledWindow::builder()
.vexpand(true)
@@ -226,7 +248,7 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) {
.build();
let title_label = gtk4::Label::builder()
.label("Photos")
.label("Gallery")
.halign(gtk4::Align::Start)
.build();
title_label.add_css_class("title-2");
@@ -243,6 +265,17 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) {
titles.append(&title_label);
titles.append(&subtitle);
// Leaves an open album for the grid it came from. Only an open album shows
// it — the Albums toggle is what leaves the grid itself.
let back = gtk4::Button::builder()
.icon_name("go-previous-symbolic")
.tooltip_text("Back to albums")
.valign(gtk4::Align::Center)
.visible(false)
.build();
back.add_css_class("flat");
back.add_css_class("circular");
let upload = gtk4::Button::builder()
.label("Upload")
.icon_name("list-add-symbolic")
@@ -253,6 +286,7 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) {
let refresh = refresh_button();
let header_box = gtk4::Box::new(gtk4::Orientation::Horizontal, 8);
header_box.append(&back);
header_box.append(&titles);
header_box.append(&refresh);
header_box.append(&upload);
@@ -287,23 +321,73 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) {
dates.add_css_class("pill");
dates.set_tooltip_text(Some("Jump to a month"));
let filter_bar = gtk4::Box::new(gtk4::Orientation::Horizontal, 8);
filter_bar.append(&tab_group);
// The kind toggles and the date jump travel together: they filter the
// timeline, and neither applies to the album grid or to an open album.
let filters = gtk4::Box::new(gtk4::Orientation::Horizontal, 8);
filters.set_hexpand(true);
filters.append(&tab_group);
let spacer = gtk4::Box::new(gtk4::Orientation::Horizontal, 0);
spacer.set_hexpand(true);
filter_bar.append(&spacer);
filter_bar.append(&dates);
filters.append(&spacer);
filters.append(&dates);
// The page's two views, as one segmented control under the title: the
// timeline, and the albums. This is navigation, not a filter — which is why
// it sits above the filter row rather than beside the kind toggles.
let photos_btn = gtk4::ToggleButton::builder()
.label("Photos")
.active(true)
.build();
let albums_btn = gtk4::ToggleButton::builder().label("Albums").build();
albums_btn.set_group(Some(&photos_btn));
let view_switch = gtk4::Box::new(gtk4::Orientation::Horizontal, 0);
view_switch.add_css_class("linked");
view_switch.add_css_class("view-switch");
view_switch.set_halign(gtk4::Align::Start);
for btn in [&photos_btn, &albums_btn] {
btn.add_css_class("pill");
view_switch.append(btn);
}
let filter_bar = gtk4::Box::new(gtk4::Orientation::Horizontal, 8);
filter_bar.append(&filters);
// The timeline (plus its pager) or the status page, never both.
let timeline = gtk4::Box::new(gtk4::Orientation::Vertical, 12);
timeline.append(&scroll);
timeline.append(&more);
// The album grid: cover-first cards that flow to the width they are given.
let albums = gtk4::FlowBox::builder()
.selection_mode(gtk4::SelectionMode::None)
.homogeneous(true)
.row_spacing(TILE_GAP as u32 * 2)
.column_spacing(TILE_GAP as u32 * 2)
.min_children_per_line(2)
.max_children_per_line(8)
.valign(gtk4::Align::Start)
.build();
let albums_scroll = gtk4::ScrolledWindow::builder()
.vexpand(true)
.hscrollbar_policy(gtk4::PolicyType::Never)
.child(&albums)
.build();
let albums_status = adw::StatusPage::builder()
.icon_name("view-grid-symbolic")
.vexpand(true)
.build();
albums_status.add_css_class("compact");
let albums_stack = gtk4::Stack::new();
albums_stack.set_vexpand(true);
albums_stack.add_named(&albums_scroll, Some("grid"));
albums_stack.add_named(&albums_status, Some("status"));
let content = gtk4::Stack::new();
content.set_vexpand(true);
content.set_transition_type(gtk4::StackTransitionType::Crossfade);
content.add_named(&timeline, Some("timeline"));
content.add_named(&status, Some("status"));
content.add_named(&albums_stack, Some("albums"));
let inner = gtk4::Box::new(gtk4::Orientation::Vertical, 12);
inner.set_margin_top(12);
@@ -311,6 +395,7 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) {
inner.set_margin_start(12);
inner.set_margin_end(12);
inner.append(&header_box);
inner.append(&view_switch);
inner.append(&filter_bar);
inner.append(&content);
@@ -321,6 +406,7 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) {
groups,
content,
status,
title: title_label,
subtitle,
more,
list,
@@ -330,6 +416,14 @@ pub(crate) fn build_gallery_page() -> (gtk4::Widget, GalleryWidgets) {
refresh,
tabs,
dates,
albums,
albums_stack,
albums_status,
photos_btn,
albums_btn,
view_switch,
back,
filters,
},
)
}
@@ -416,16 +510,31 @@ pub(crate) fn refresh_photo_months(ui: &Rc<Ui>) {
/// behind it is disabled — you can't filter to an empty set — but the currently
/// selected one stays clickable so you can always switch back off it.
pub(crate) fn update_gallery_tabs(ui: &Rc<Ui>, counts: (usize, usize, usize)) {
ui.gallery.counts.set(Some(counts));
let (photos, videos, raw) = counts;
let totals = [photos + videos + raw, photos, videos, raw];
for (index, tab) in ui.gallery.tabs.iter().enumerate() {
let name = ["All", "Photos", "Videos", "Raw"][index];
let n = totals[index];
tab.set_label(&format!("{name} {n}"));
tab.set_label(&format!("{name} {}", thousands(n)));
tab.set_sensitive(n > 0 || tab.is_active());
}
}
/// `1422` as `1,422`. Six-figure libraries are ordinary, and an unseparated run
/// of digits in a tab label is unreadable at a glance.
pub(crate) fn thousands(n: usize) -> String {
let digits = n.to_string();
let mut out = String::with_capacity(digits.len() + digits.len() / 3);
for (i, c) in digits.chars().enumerate() {
if i > 0 && (digits.len() - i).is_multiple_of(3) {
out.push(',');
}
out.push(c);
}
out
}
/// Wire the gallery: install the section factory, the zoom gestures, the pager
/// and the upload button. Activating a thumbnail downloads the photo and opens it
/// in the in-app lightbox.
@@ -485,8 +594,8 @@ pub(crate) fn wire_gallery(ui: &Rc<Ui>, list: &gtk4::ListView, scroll: &gtk4::Sc
});
list.set_factory(Some(&factory));
// Rows are justified to the content width, so a resize re-justifies whatever
// is on screen (offscreen sections pick the new width up when they bind).
// The grid divides the content width, so a resize re-flows whatever is on
// screen (offscreen sections pick the new width up when they bind).
let ui_width = ui.clone();
list.connect_notify_local(Some("width"), move |list, _| {
let width = list.width();
@@ -673,114 +782,55 @@ pub(crate) fn wire_gallery(ui: &Rc<Ui>, list: &gtk4::ListView, scroll: &gtk4::Sc
});
}
/// One tile in a justified row: the photo, and the pixel size it was justified to.
/// One tile of the grid: the photo, and the square edge it was sized to.
pub(crate) struct Tile {
pub(crate) photo: PhotoItem,
pub(crate) width: i32,
pub(crate) height: i32,
pub(crate) edge: i32,
}
/// Break one day's photos into rows that each fill `width` exactly, at heights
/// near [`Ui::gallery_tile`] — the layout every modern photo gallery uses, and
/// the reason nothing here has to be cropped: each tile keeps its own aspect
/// ratio, and it is the row *height* that flexes to make the widths add up.
/// Break one day's photos into rows of equal square tiles that span `width` —
/// the layout a phone gallery uses, and the reason a day holding two photos
/// looks like every other day rather than like a mistake.
///
/// Greedy, one pass: keep adding photos to the row: the more photos share it, the
/// shorter it has to be to fit. The moment that height drops to the target, the
/// row is as full as it should be and is emitted. The trailing row keeps the
/// target height instead of being stretched, so a day with three photos doesn't
/// blow them up to fill a screen-wide row.
///
/// Photos whose thumbnail hasn't been decoded yet are laid out at
/// [`RATIO_UNKNOWN`]; [`Ui::store_texture`] reports the real ratio when it lands
/// and the section is re-justified in place.
pub(crate) fn justify_rows(ui: &Rc<Ui>, photos: &[PhotoItem], width: i32) -> Vec<Vec<Tile>> {
let ratios: Vec<f64> = photos.iter().map(|photo| ui.ratio(&photo.uid)).collect();
let target = f64::from(ui.gallery.tile.get());
let plan = plan_rows(&ratios, target, f64::from(width));
let mut photos = photos.iter();
plan.into_iter()
/// Each tile is centre-cropped to its square (the full frame is one click away
/// in the lightbox), so nothing here depends on knowing a photo's aspect ratio
/// and a row never has to be re-flowed when a thumbnail finally lands.
pub(crate) fn grid_rows(ui: &Rc<Ui>, photos: &[PhotoItem], width: i32) -> Vec<Vec<Tile>> {
let (columns, edge) = plan_grid(ui.gallery.tile.get(), width);
photos
.chunks(columns)
.map(|row| {
row.into_iter()
.filter_map(|(width, height)| {
photos.next().map(|photo| Tile {
photo: photo.clone(),
width,
height,
})
row.iter()
.map(|photo| Tile {
photo: photo.clone(),
edge,
})
.collect()
})
.collect()
}
/// The row-packing math behind [`justify_rows`], over nothing but aspect ratios:
/// takes each photo's ratio (w/h) in order and returns the pixel `(width,
/// height)` of every tile, grouped into rows.
pub(crate) fn plan_rows(ratios: &[f64], target: f64, width: f64) -> Vec<Vec<(i32, i32)>> {
let avail = width.max(f64::from(TILE_MIN));
let gap = f64::from(TILE_GAP);
// The height a row of `n` photos with `sum_ratio` total ratio must take for
// its widths (plus gaps) to add up to exactly `avail`.
let row_height = |sum_ratio: f64, n: usize| {
let gaps = gap * (n.saturating_sub(1)) as f64;
(avail - gaps) / sum_ratio
};
let mut rows: Vec<Vec<(i32, i32)>> = Vec::new();
let mut row: Vec<f64> = Vec::new();
let mut sum_ratio = 0.0;
for ratio in ratios {
let ratio = ratio.clamp(RATIO_MIN, RATIO_MAX);
row.push(ratio);
sum_ratio += ratio;
let height = row_height(sum_ratio, row.len());
if height <= target {
rows.push(size_row(&row, height, avail, true));
row.clear();
sum_ratio = 0.0;
}
}
if !row.is_empty() {
let height = row_height(sum_ratio, row.len()).min(target);
rows.push(size_row(&row, height, avail, false));
}
rows
/// The grid math: how many columns fit in `width` at roughly `target` px per
/// tile, and the exact square edge that divides the width between them.
///
/// The column count is what rounds — the edge then absorbs the remainder, so the
/// grid spans the full width at every window size instead of leaving a ragged
/// margin. Never fewer than one column, however narrow the window gets.
pub(crate) fn plan_grid(target: i32, width: i32) -> (usize, i32) {
let avail = width.max(TILE_MIN);
let target = target.clamp(TILE_MIN, TILE_MAX);
// A row of n tiles occupies n*edge + (n-1)*gap, so n tiles of the target size
// fit while n*(target + gap) - gap <= avail.
let columns = ((avail + TILE_GAP) / (target + TILE_GAP)).max(1) as usize;
let gaps = TILE_GAP * (columns as i32 - 1);
let edge = ((avail - gaps) / columns as i32).max(1);
(columns, edge)
}
/// Size one row's tiles at `height`. A `justified` row is nudged so its widths
/// plus gaps hit `avail` exactly — rounding each width independently leaves a
/// few px of ragged right edge, so the last tile absorbs the remainder.
pub(crate) fn size_row(
ratios: &[f64],
height: f64,
avail: f64,
justified: bool,
) -> Vec<(i32, i32)> {
let height = height.round().max(1.0);
let mut sizes: Vec<(i32, i32)> = ratios
.iter()
.map(|ratio| ((ratio * height).round().max(1.0) as i32, height as i32))
.collect();
if justified {
let gaps = TILE_GAP * (sizes.len().saturating_sub(1)) as i32;
let used: i32 = sizes.iter().rev().skip(1).map(|(width, _)| *width).sum();
if let Some(last) = sizes.last_mut() {
last.0 = (avail as i32 - gaps - used).max(1);
}
}
sizes
}
/// (Re)build a bound day-section's tiles: justify this day's photos to the
/// (Re)build a bound day-section's tiles: lay this day's photos out to the
/// current content width and hand each tile whatever thumbnail is already in
/// memory, queueing the rest. Replaces the section's rows in place, leaving the
/// heading — so a re-justify never touches the ListView's model or scroll.
/// heading — so a re-flow never touches the ListView's model or scroll.
pub(crate) fn fill_section(ui: &Rc<Ui>, section: &gtk4::Box, photos: &[PhotoItem]) {
let Some(heading) = section.first_child() else {
return;
@@ -791,8 +841,11 @@ pub(crate) fn fill_section(ui: &Rc<Ui>, section: &gtk4::Box, photos: &[PhotoItem
let width = gallery_width(ui);
let rows = gtk4::Box::new(gtk4::Orientation::Vertical, TILE_GAP);
for row in justify_rows(ui, photos, width) {
for row in grid_rows(ui, photos, width) {
let row_box = gtk4::Box::new(gtk4::Orientation::Horizontal, TILE_GAP);
// A short last row stays left-aligned at the same tile size rather than
// stretching: an even grid is the whole point of the square layout.
row_box.set_halign(gtk4::Align::Start);
for tile in row {
row_box.append(&photo_tile(ui, tile));
}
@@ -802,8 +855,8 @@ pub(crate) fn fill_section(ui: &Rc<Ui>, section: &gtk4::Box, photos: &[PhotoItem
schedule_thumbs(ui);
}
/// The width justified rows are laid out to: the ListView's own width, less a
/// couple of px so a rounding error can't push a row into a horizontal overflow.
/// The width the grid is laid out to: the ListView's own width, less a couple of
/// px so a rounding error can't push a row into a horizontal overflow.
/// Falls back to a sane guess before the first allocation.
pub(crate) fn gallery_width(ui: &Rc<Ui>) -> i32 {
match ui.gallery.width.get() {
@@ -822,12 +875,17 @@ pub(crate) fn gallery_width(ui: &Rc<Ui>) -> i32 {
/// one keeps an image glyph instead of an empty rectangle.
pub(crate) fn photo_tile(ui: &Rc<Ui>, tile: Tile) -> gtk4::Button {
let picture = gtk4::Picture::builder()
// The tile is exactly the thumbnail's own aspect ratio, so Cover scales
// it and crops nothing; it only bites during the brief window where the
// ratio is still a guess, and the re-justify then fixes the tile.
// Cover fills the square and crops the overflow, so a portrait photo
// sits flush in its tile instead of floating in letterbox bars. The
// expands are what make the picture take the whole overlay: without
// them it is allocated its natural size and the crop never happens.
.content_fit(gtk4::ContentFit::Cover)
.can_shrink(true)
.hexpand(true)
.vexpand(true)
.build();
// Zoomed on hover by the stylesheet; the tile clips the overflow.
picture.add_css_class("photo-thumb");
let placeholder = gtk4::Image::builder()
.icon_name("image-x-generic-symbolic")
@@ -871,8 +929,8 @@ pub(crate) fn photo_tile(ui: &Rc<Ui>, tile: Tile) -> gtk4::Button {
let button = gtk4::Button::builder()
.child(&overlay)
.width_request(tile.width)
.height_request(tile.height)
.width_request(tile.edge)
.height_request(tile.edge)
.tooltip_text(format_capture_time(tile.photo.capture_time))
.build();
button.add_css_class("photo-tile");
@@ -1052,7 +1110,6 @@ pub(crate) fn schedule_decode(ui: &Rc<Ui>) {
let mut queue = ui.gallery.decode_queue.borrow_mut();
(0..4).filter_map(|_| queue.pop_front()).collect()
};
let mut relayout = false;
for (uid, path) in batch {
let texture = match gtk4::gdk::Texture::from_filename(&path) {
Ok(texture) => texture,
@@ -1062,28 +1119,23 @@ pub(crate) fn schedule_decode(ui: &Rc<Ui>) {
continue;
}
};
// A ratio we hadn't seen means the tile was sized against a guess.
relayout |= ui.store_texture(&uid, texture.clone());
ui.store_texture(&uid, texture.clone());
if let Some(picture) = ui.gallery.thumb_wanted.borrow_mut().remove(&uid) {
picture.set_paintable(Some(&texture));
}
}
if relayout {
schedule_relayout(&ui);
}
if ui.gallery.decode_queue.borrow().is_empty() {
ui.gallery.decode_idle.set(false);
ui.save_ratios();
return glib::ControlFlow::Break;
}
glib::ControlFlow::Continue
});
}
/// Re-justify the sections on screen shortly. Debounced, because the triggers
/// (a window resize, a zoom step, a burst of decoded thumbnails) all arrive in
/// floods and only the final state matters.
/// Re-flow the sections on screen shortly. Debounced, because the triggers (a
/// window resize, a zoom step) arrive in floods and only the final state
/// matters.
pub(crate) fn schedule_relayout(ui: &Rc<Ui>) {
if let Some(id) = ui.gallery.relayout_source.borrow_mut().take() {
id.remove();
@@ -1097,9 +1149,8 @@ pub(crate) fn schedule_relayout(ui: &Rc<Ui>) {
}
/// Rebuild the tiles of the day sections currently on screen, at the current
/// width, zoom and set of known aspect ratios. Sections that are *not* realised
/// need no work: they justify themselves against the current state when the
/// ListView binds them.
/// width and zoom. Sections that are *not* realised need no work: they lay
/// themselves out against the current state when the ListView binds them.
pub(crate) fn relayout_gallery(ui: &Rc<Ui>) {
let bound: Vec<(u32, gtk4::Box)> = ui
.gallery
@@ -1120,12 +1171,12 @@ pub(crate) fn relayout_gallery(ui: &Rc<Ui>) {
}
}
/// Step the row height by `delta` px and re-justify, clamped to the zoom range.
/// Step the tile size by `delta` px and re-flow, clamped to the zoom range.
pub(crate) fn zoom_gallery(ui: &Rc<Ui>, delta: i32) {
set_gallery_tile(ui, ui.gallery.tile.get() + delta);
}
/// Set the row height (clamped) and re-justify the visible sections at it.
/// Set the tile size (clamped) and re-flow the visible sections at it.
pub(crate) fn set_gallery_tile(ui: &Rc<Ui>, tile: i32) {
let tile = tile.clamp(TILE_MIN, TILE_MAX);
if tile == ui.gallery.tile.get() {
@@ -1175,17 +1226,35 @@ pub(crate) fn repaint_gallery(ui: &Rc<Ui>) {
store.splice(len, store.n_items() - len, &[] as &[BoxedAnyObject]);
}
let count = ui.gallery.model.n_items();
ui.gallery.subtitle.set_visible(count > 0);
let loaded = ui.gallery.model.n_items() as usize;
ui.gallery.subtitle.set_visible(loaded > 0);
// An album counts what the server says it holds, not how much of it has been
// paged in — the subtitle would otherwise climb as the user scrolls.
if ui.gallery.album.borrow().is_some() {
return;
}
// The noun tracks the active filter, so a Videos tab doesn't count "photos".
let (one, many) = match ui.gallery.kind.get() {
let kind = ui.gallery.kind.get();
let (one, many) = match kind {
Some(PhotoKind::Video) => ("video", "videos"),
Some(PhotoKind::Raw) => ("raw photo", "raw photos"),
_ => ("photo", "photos"),
};
ui.gallery.subtitle.set_label(&match count {
// The whole library for this filter, not the page count — the subtitle sits
// next to tabs carrying the same totals, and the two disagreeing reads as a
// bug. A date jump is the exception: there the window is the subject.
let total = match (ui.gallery.range.get(), ui.gallery.counts.get()) {
(None, Some((photos, videos, raw))) => match kind {
Some(PhotoKind::Photo) => photos,
Some(PhotoKind::Video) => videos,
Some(PhotoKind::Raw) => raw,
None => photos + videos + raw,
},
_ => loaded,
};
ui.gallery.subtitle.set_label(&match total {
1 => format!("1 {one}"),
n => format!("{n} {many}"),
n => format!("{} {many}", thousands(n)),
});
}
@@ -1272,6 +1341,9 @@ pub(crate) fn load_gallery(ui: &Rc<Ui>, append: bool) {
if ui.gallery.loading.get() {
return;
}
// An open album pages itself instead of the timeline; everything downstream —
// the model, the sections, the thumbnails, the lightbox — is the same.
let album = ui.gallery.album.borrow().as_ref().map(|a| a.uid.clone());
if !append {
// Fresh load: clear the timeline and show Loading until the first page lands.
ui.gallery.model.remove_all();
@@ -1284,8 +1356,9 @@ pub(crate) fn load_gallery(ui: &Rc<Ui>, append: bool) {
);
// Rebuild the date jump for the current kind, but only for a full-span
// load — a jump *to* a month sets a range and reloads, and refreshing the
// dropdown then would fight the selection the user just made.
if ui.gallery.range.get().is_none() {
// dropdown then would fight the selection the user just made. An album
// has no date jump at all.
if album.is_none() && ui.gallery.range.get().is_none() {
refresh_photo_months(ui);
}
}
@@ -1294,15 +1367,20 @@ pub(crate) fn load_gallery(ui: &Rc<Ui>, append: bool) {
ui.gallery.more.set_sensitive(false);
ui.busy_begin();
let rx = spawn_request(
ui.dirs.control_socket(),
Request::PhotosTimeline {
let request = match album {
Some(uid) => Request::AlbumPhotos {
uid,
offset,
limit: PHOTOS_PAGE,
},
None => Request::PhotosTimeline {
offset,
limit: PHOTOS_PAGE,
kind: ui.gallery.kind.get(),
range: ui.gallery.range.get(),
},
);
};
let rx = spawn_request(ui.dirs.control_socket(), request);
let ui = ui.clone();
glib::spawn_future_local(async move {
let result = rx.recv().await;
@@ -1329,20 +1407,13 @@ pub(crate) fn load_gallery(ui: &Rc<Ui>, append: bool) {
if let Some(counts) = counts {
update_gallery_tabs(&ui, counts);
}
// Take the daemon's word on ratios and thumbnail verdicts before
// anything is laid out: it persists both, so the very first frame
// justifies its rows correctly instead of guessing RATIO_UNKNOWN
// and reflowing as the images land.
// Take the daemon's word on which photos can never have a
// thumbnail, so their tiles show a placeholder from the first
// frame instead of queueing a request that can only fail.
{
let mut ratios = ui.gallery.photo_ratio.borrow_mut();
let mut nothumb = ui.gallery.photo_nothumb.borrow_mut();
for item in &items {
if let Some(ratio) = item.ratio {
ratios.insert(item.uid.clone(), ratio);
}
if item.no_thumb {
nothumb.insert(item.uid.clone());
}
for item in items.iter().filter(|item| item.no_thumb) {
nothumb.insert(item.uid.clone());
}
}
for item in &items {
@@ -1350,13 +1421,15 @@ pub(crate) fn load_gallery(ui: &Rc<Ui>, append: bool) {
}
repaint_gallery(&ui);
if ui.gallery.model.n_items() == 0 {
gallery_status(
&ui,
"image-x-generic-symbolic",
"No photos yet",
"Photos you upload to Proton Drive appear here.",
false,
);
let (title, description) = if ui.gallery.album.borrow().is_some() {
("Empty album", "This album has no photos in it.")
} else {
(
"No photos yet",
"Photos you upload to Proton Drive appear here.",
)
};
gallery_status(&ui, "image-x-generic-symbolic", title, description, false);
return;
}
ui.gallery.content.set_visible_child_name("timeline");