Compare commits

...

2 Commits

Author SHA1 Message Date
99de8bca54 Fix clippy warnings 2026-05-16 10:03:59 +02:00
900260f67b Parallelize Api 2026-05-16 10:02:22 +02:00
2 changed files with 57 additions and 87 deletions

View File

@@ -1,44 +1,38 @@
use std::{collections::HashMap, iter::repeat, sync::Arc}; use std::{
collections::HashMap,
iter::repeat,
sync::{Arc, Mutex},
};
use anyhow::{Context as _, anyhow}; use anyhow::{Context as _, anyhow};
use immich_sdk::{AssetId, AssetVisibility}; use immich_sdk::{AssetId, AssetVisibility};
use kameo::{
Actor,
prelude::{Context, Message},
};
use slint::{Rgba8Pixel, SharedPixelBuffer}; use slint::{Rgba8Pixel, SharedPixelBuffer};
use crate::thumbhash::thumbhashes_to_pixels; use crate::thumbhash::thumbhashes_to_pixels;
pub type TimeBucketKey = String; pub type TimeBucketKey = String;
#[derive(Actor)]
pub struct Api { pub struct Api {
client: immich_sdk::Client, client: immich_sdk::Client,
buckets: HashMap<TimeBucketKey, Arc<TimeBucket>>, buckets: Mutex<HashMap<TimeBucketKey, Arc<TimeBucket>>>,
thumbnails: HashMap<AssetId, Arc<AssetThumbnail>>, // TODO thumbnails: Mutex<HashMap<AssetId, Arc<AssetThumbnail>>>,
} }
impl Api { impl Api {
pub fn new(client: immich_sdk::Client) -> Self { pub fn new(client: immich_sdk::Client) -> Arc<Self> {
Self { Arc::new(Self {
client, client,
buckets: Default::default(), buckets: Default::default(),
thumbnails: Default::default(), thumbnails: Default::default(),
} })
} }
} }
pub struct GetTimeBuckets;
pub struct TimeBucketRef { pub struct TimeBucketRef {
pub key: TimeBucketKey, pub key: TimeBucketKey,
pub count: usize, pub count: usize,
} }
pub struct GetTimeBucket {
pub time_bucket: TimeBucketKey,
}
pub struct TimeBucket { pub struct TimeBucket {
pub key: TimeBucketKey, pub key: TimeBucketKey,
pub entries: Arc<[TimeBucketEntry]>, pub entries: Arc<[TimeBucketEntry]>,
@@ -54,23 +48,13 @@ pub struct TimeBucketEntry {
pub visibility: AssetVisibility, pub visibility: AssetVisibility,
} }
pub struct GetAssetThumbnail {
pub id: AssetId,
}
pub struct AssetThumbnail { pub struct AssetThumbnail {
pub id: AssetId, pub id: AssetId,
pub thumbnail: SharedPixelBuffer<Rgba8Pixel>, pub thumbnail: SharedPixelBuffer<Rgba8Pixel>,
} }
impl Message<GetTimeBuckets> for Api { impl Api {
type Reply = anyhow::Result<Arc<[TimeBucketRef]>>; pub async fn get_time_buckets(&self) -> anyhow::Result<Vec<TimeBucketRef>> {
async fn handle(
&mut self,
_msg: GetTimeBuckets,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
let buckets = self let buckets = self
.client .client
.timeline() .timeline()
@@ -90,30 +74,22 @@ impl Message<GetTimeBuckets> for Api {
}) })
.collect()) .collect())
} }
}
impl Message<GetTimeBucket> for Api { pub async fn get_time_bucket(
type Reply = anyhow::Result<Arc<TimeBucket>>; &self,
time_bucket: TimeBucketKey,
async fn handle( ) -> anyhow::Result<Arc<TimeBucket>> {
&mut self, if let Some(time_bucket) = self.buckets.lock().unwrap().get(&time_bucket).cloned() {
msg: GetTimeBucket,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
if let Some(time_bucket) = self.buckets.get(&msg.time_bucket).cloned() {
return Ok(time_bucket); return Ok(time_bucket);
} }
let bucket = self let bucket = self
.client .client
.timeline() .timeline()
.bucket(&msg.time_bucket) .bucket(&time_bucket)
.execute() .execute()
.await .await
.context(anyhow!( .context(anyhow!("Failed to fetch time bucket {:?}", &time_bucket))
"Failed to fetch time bucket {:?}",
&msg.time_bucket
))
.inspect_err(|e| { .inspect_err(|e| {
tracing::error!("{e:?}"); tracing::error!("{e:?}");
})?; })?;
@@ -144,40 +120,38 @@ impl Message<GetTimeBucket> for Api {
.collect(); .collect();
let bucket = Arc::new(TimeBucket { let bucket = Arc::new(TimeBucket {
key: msg.time_bucket, key: time_bucket,
entries, entries,
}); });
self.buckets.insert(bucket.key.clone(), bucket.clone()); self.buckets
.lock()
.unwrap()
.insert(bucket.key.clone(), bucket.clone());
Ok(bucket) Ok(bucket)
} }
}
impl Message<GetAssetThumbnail> for Api { pub async fn get_asset_thumbnail(
type Reply = anyhow::Result<Arc<AssetThumbnail>>; &self,
asset_id: AssetId,
async fn handle( ) -> anyhow::Result<Arc<AssetThumbnail>> {
&mut self, if let Some(thumbnail) = self.thumbnails.lock().unwrap().get(&asset_id).cloned() {
msg: GetAssetThumbnail,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
if let Some(thumbnail) = self.thumbnails.get(&msg.id).cloned() {
return Ok(thumbnail); return Ok(thumbnail);
} }
let response = self let response = self
.client .client
.assets() .assets()
.thumbnail(msg.id) .thumbnail(asset_id)
.size(immich_sdk::AssetMediaSize::Thumbnail) .size(immich_sdk::AssetMediaSize::Thumbnail)
.execute() .execute()
.await .await
.context(anyhow!("Failed to get asset thumbnail for {}", msg.id))?; .context(anyhow!("Failed to get asset thumbnail for {asset_id}"))?;
let thumbnail = response let thumbnail = response
.decode() .decode()
.context(anyhow!("Failed to decode asset thumbnail for {}", msg.id))? .context(anyhow!("Failed to decode asset thumbnail for {asset_id}"))?
.into_rgba8(); .into_rgba8();
let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice( let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
@@ -187,11 +161,14 @@ impl Message<GetAssetThumbnail> for Api {
); );
let thumbnail = Arc::new(AssetThumbnail { let thumbnail = Arc::new(AssetThumbnail {
id: msg.id, id: asset_id,
thumbnail: pixel_buffer, thumbnail: pixel_buffer,
}); });
self.thumbnails.insert(msg.id, Arc::clone(&thumbnail)); self.thumbnails
.lock()
.unwrap()
.insert(asset_id, Arc::clone(&thumbnail));
Ok(thumbnail) Ok(thumbnail)
} }

View File

@@ -1,11 +1,10 @@
// Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms. // Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::{mem, ops::Deref}; use std::{mem, ops::Deref, sync::Arc};
use clap::Parser; use clap::Parser;
use immich_sdk::AssetId; use immich_sdk::AssetId;
use kameo::actor::{ActorRef, Spawn};
use slint::{ use slint::{
ComponentHandle as _, Model, ModelRc, SharedPixelBuffer, SharedString, ToSharedString, ComponentHandle as _, Model, ModelRc, SharedPixelBuffer, SharedString, ToSharedString,
VecModel, Weak, VecModel, Weak,
@@ -13,7 +12,7 @@ use slint::{
use tracing::Level; use tracing::Level;
use crate::{ use crate::{
api::{Api, GetAssetThumbnail, GetTimeBucket, GetTimeBuckets, TimeBucketKey}, api::{Api, TimeBucketKey},
ui::{AppWindow, ImageBucket}, ui::{AppWindow, ImageBucket},
}; };
@@ -66,8 +65,7 @@ fn main() -> anyhow::Result<()> {
let immich_config = let immich_config =
immich_sdk::Config::new(opt.immich_base_url).with_api_key(opt.immich_api_key); immich_sdk::Config::new(opt.immich_base_url).with_api_key(opt.immich_api_key);
let api = Api::new(immich_sdk::Client::new(immich_config).unwrap()); let api_ = Api::new(immich_sdk::Client::new(immich_config).unwrap());
let api_ = Api::spawn(api);
let app = ui::AppWindow::new()?; let app = ui::AppWindow::new()?;
let global = app.global::<ui::Global>(); let global = app.global::<ui::Global>();
@@ -76,7 +74,7 @@ fn main() -> anyhow::Result<()> {
let app_weak = app.as_weak(); let app_weak = app.as_weak();
let api = api_.clone(); let api = api_.clone();
tokio::spawn(async move { tokio::spawn(async move {
let Ok(buckets) = api.ask(GetTimeBuckets).await else { let Ok(buckets) = api.get_time_buckets().await else {
return; return;
}; };
@@ -132,13 +130,13 @@ fn main() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
fn calculate_timeline_visibility(app: &AppWindow, api: ActorRef<Api>, scroll: f32) { fn calculate_timeline_visibility(app: &AppWindow, api: Arc<Api>, scroll: f32) {
let global = app.global::<ui::Global>(); let global = app.global::<ui::Global>();
global.set_timeline_scroll(scroll); global.set_timeline_scroll(scroll);
let window_height = app.get_window_height(); let window_height = app.get_window_height();
let visible_range = scroll..=(scroll + window_height); let visible_range = scroll..=(scroll + window_height);
let buckets = get_image_buckets(&app); let buckets = get_image_buckets(app);
for i in 0..buckets.row_count() { for i in 0..buckets.row_count() {
let Some(mut bucket) = buckets.row_data(i) else { let Some(mut bucket) = buckets.row_data(i) else {
@@ -147,7 +145,7 @@ fn calculate_timeline_visibility(app: &AppWindow, api: ActorRef<Api>, scroll: f3
let top_y = bucket.y; let top_y = bucket.y;
let bottom_y = bucket.y + bucket.height; let bottom_y = bucket.y + bucket.height;
let is_visible = &top_y <= &visible_range.end() && visible_range.start() <= &bottom_y; let is_visible = &top_y <= visible_range.end() && visible_range.start() <= &bottom_y;
let visibility = if is_visible { let visibility = if is_visible {
ui::Visibility::InView ui::Visibility::InView
@@ -178,12 +176,12 @@ fn calculate_timeline_visibility(app: &AppWindow, api: ActorRef<Api>, scroll: f3
} }
} }
fn calculate_timeline_layout(app: &AppWindow, api: ActorRef<Api>, timeline_width: f32) { fn calculate_timeline_layout(app: &AppWindow, api: Arc<Api>, timeline_width: f32) {
let global = app.global::<ui::Global>(); let global = app.global::<ui::Global>();
let min_image_size = global.get_min_image_size(); let min_image_size = global.get_min_image_size();
let image_margin = global.get_image_margin(); let image_margin = global.get_image_margin();
let min_size_with_margin = min_image_size + image_margin; let min_size_with_margin = min_image_size + image_margin;
let buckets = get_image_buckets(&app); let buckets = get_image_buckets(app);
let count_x = (timeline_width / min_size_with_margin).floor() as usize; let count_x = (timeline_width / min_size_with_margin).floor() as usize;
let remaining_length = timeline_width.rem_euclid(min_size_with_margin); let remaining_length = timeline_width.rem_euclid(min_size_with_margin);
@@ -249,7 +247,7 @@ fn placeholder_preview() -> slint::Image {
fn unload_bucket(time_bucket: &TimeBucketKey, app: &AppWindow) { fn unload_bucket(time_bucket: &TimeBucketKey, app: &AppWindow) {
let buckets = get_image_buckets(app); let buckets = get_image_buckets(app);
let Some(i) = buckets.iter().position(|b| &b.key == &time_bucket) else { let Some(i) = buckets.iter().position(|b| b.key == time_bucket) else {
return; return;
}; };
let bucket = buckets.row_data(i).expect("i is in the list"); let bucket = buckets.row_data(i).expect("i is in the list");
@@ -267,21 +265,16 @@ fn unload_bucket(time_bucket: &TimeBucketKey, app: &AppWindow) {
// TODO: write `bucket` into `buckets?` // TODO: write `bucket` into `buckets?`
} }
fn load_bucket(time_bucket: TimeBucketKey, app_weak: Weak<AppWindow>, api: ActorRef<Api>) { fn load_bucket(time_bucket: TimeBucketKey, app_weak: Weak<AppWindow>, api: Arc<Api>) {
tokio::spawn(async move { tokio::spawn(async move {
let Ok(api_bucket) = api let Ok(api_bucket) = api.get_time_bucket(time_bucket.clone()).await else {
.ask(GetTimeBucket {
time_bucket: time_bucket.clone(),
})
.await
else {
return; return;
}; };
let _ = app_weak.upgrade_in_event_loop(move |app| { let _ = app_weak.upgrade_in_event_loop(move |app| {
let buckets = get_image_buckets(&app); let buckets = get_image_buckets(&app);
let Some(i) = buckets.iter().position(|b| &b.key == &time_bucket) else { let Some(i) = buckets.iter().position(|b| b.key == time_bucket) else {
return; return;
}; };
let mut bucket = buckets.row_data(i).expect("i is in the list"); let mut bucket = buckets.row_data(i).expect("i is in the list");
@@ -312,13 +305,13 @@ fn load_bucket(time_bucket: TimeBucketKey, app_weak: Weak<AppWindow>, api: Actor
fn load_thumbnail( fn load_thumbnail(
time_bucket: TimeBucketKey, time_bucket: TimeBucketKey,
id: AssetId, asset_id: AssetId,
app_weak: Weak<AppWindow>, app_weak: Weak<AppWindow>,
api: ActorRef<Api>, api: Arc<Api>,
) { ) {
tokio::spawn(async move { tokio::spawn(async move {
tracing::debug!("Fetching thumbnail for {id}"); tracing::debug!("Fetching thumbnail for {asset_id}");
let thumbnail = match api.ask(GetAssetThumbnail { id }).await { let thumbnail = match api.get_asset_thumbnail(asset_id).await {
Ok(thumbnail) => thumbnail, Ok(thumbnail) => thumbnail,
Err(e) => { Err(e) => {
tracing::error!("{e:?}"); tracing::error!("{e:?}");
@@ -328,13 +321,13 @@ fn load_thumbnail(
let _ = app_weak.upgrade_in_event_loop(move |app| { let _ = app_weak.upgrade_in_event_loop(move |app| {
let buckets = get_image_buckets(&app); let buckets = get_image_buckets(&app);
let Some(i) = buckets.iter().position(|b| &b.key == &time_bucket) else { let Some(i) = buckets.iter().position(|b| b.key == time_bucket) else {
return; return;
}; };
let bucket = buckets.row_data(i).expect("i is in the list"); let bucket = buckets.row_data(i).expect("i is in the list");
let id_str = id.to_string(); let id_str = asset_id.to_string();
let Some(i) = bucket.previews.iter().position(|p| &p.asset_id == &id_str) else { let Some(i) = bucket.previews.iter().position(|p| p.asset_id == id_str) else {
return; return;
}; };
let mut preview = bucket.previews.row_data(i).expect("i is in the list"); let mut preview = bucket.previews.row_data(i).expect("i is in the list");