Initial commit

This commit is contained in:
2026-04-06 11:19:26 +02:00
commit 68e8205276
9 changed files with 7764 additions and 0 deletions

12
.cargo/config.toml Normal file
View File

@@ -0,0 +1,12 @@
[target.x86_64-pc-windows-msvc]
# Increase default stack size to avoid running out of stack
# space in debug builds. The size matches Linux's default.
rustflags = [
"-C", "link-arg=/STACK:8000000"
]
[target.aarch64-pc-windows-msvc]
# Increase default stack size to avoid running out of stack
# space in debug builds. The size matches Linux's default.
rustflags = [
"-C", "link-arg=/STACK:8000000"
]

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
/target/
# These are backup files generated by rustfmt
**/*.rs.bk

7089
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

30
Cargo.toml Normal file
View File

@@ -0,0 +1,30 @@
[package]
name = "immich-rs"
version = "0.1.0"
edition = "2024"
[dependencies]
anyhow = "1.0.102"
base64 = "0.22.1"
blurhash = "0.2.3"
clap = { version = "4.6.0", features = ["derive", "env"] }
immich-sdk.path = "../immich-sdk/"
kameo = "0.19.2"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
thumbhash = "0.1.0"
tokio = { version = "1.51.0", features = ["full"] }
tracing = "0.1.44"
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
[dependencies.slint]
version = "1.15.1"
default-features = false
features = ["backend-winit", "compat-1-2", "live-preview", "renderer-femtovg", "std"]
[build-dependencies]
slint-build = "1.15.1"
[profile.release]
codegen-units = 1
lto = true

3
build.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
slint_build::compile("ui/app-window.slint").expect("Slint build failed");
}

190
src/api.rs Normal file
View File

@@ -0,0 +1,190 @@
use std::{collections::HashMap, iter::repeat, sync::Arc};
use anyhow::{Context as _, anyhow};
use immich_sdk::{AssetId, AssetVisibility};
use kameo::{
Actor,
prelude::{Context, Message},
};
use slint::{Rgba8Pixel, SharedPixelBuffer};
use crate::thumbhash::thumbhashes_to_pixels;
pub type TimeBucketKey = String;
#[derive(Actor)]
pub struct Api {
client: immich_sdk::Client,
buckets: HashMap<TimeBucketKey, Arc<TimeBucket>>,
thumbnails: HashMap<AssetId, Arc<AssetThumbnail>>, // TODO
}
impl Api {
pub fn new(client: immich_sdk::Client) -> Self {
Self {
client,
buckets: Default::default(),
thumbnails: Default::default(),
}
}
}
pub struct GetTimeBuckets;
pub struct TimeBucketRef {
pub key: TimeBucketKey,
pub count: usize,
}
pub struct GetTimeBucket {
pub time_bucket: TimeBucketKey,
}
pub struct TimeBucket {
pub key: TimeBucketKey,
pub entries: Arc<[TimeBucketEntry]>,
}
pub struct TimeBucketEntry {
pub id: AssetId,
pub is_favorite: bool,
pub is_image: bool,
pub is_trashed: bool,
pub ratio: f64,
pub thumbhash: Option<SharedPixelBuffer<Rgba8Pixel>>,
pub visibility: AssetVisibility,
}
pub struct GetAssetThumbnail {
pub id: AssetId,
}
pub struct AssetThumbnail {
pub id: AssetId,
pub thumbnail: SharedPixelBuffer<Rgba8Pixel>,
}
impl Message<GetTimeBuckets> for Api {
type Reply = anyhow::Result<Arc<[TimeBucketRef]>>;
async fn handle(
&mut self,
_msg: GetTimeBuckets,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
let buckets = self
.client
.timeline()
.buckets()
.execute()
.await
.context(anyhow!("Failed to fetch list of time buckets",))
.inspect_err(|e| {
tracing::error!("{e:?}");
})?;
Ok(buckets
.into_iter()
.map(|b| TimeBucketRef {
key: b.time_bucket,
count: b.count.try_into().expect("count is non-negative"),
})
.collect())
}
}
impl Message<GetTimeBucket> for Api {
type Reply = anyhow::Result<Arc<TimeBucket>>;
async fn handle(
&mut self,
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);
}
let bucket = self
.client
.timeline()
.bucket(&msg.time_bucket)
.execute()
.await
.context(anyhow!(
"Failed to fetch time bucket {:?}",
&msg.time_bucket
))
.inspect_err(|e| {
tracing::error!("{e:?}");
})?;
let thumbhashes = bucket.thumbhash.into_iter().flatten();
let thumbhashes = thumbhashes_to_pixels(thumbhashes)
.context("Failed to decode thumbhashes")?
.into_iter()
.chain(repeat(None));
let entries = bucket
.id
.into_iter()
.enumerate()
.zip(thumbhashes)
.map(|((i, id), thumbhash)| {
TimeBucketEntry {
id,
is_favorite: bucket.is_favorite.get(i).copied().unwrap_or(false),
is_image: bucket.is_image.get(i).copied().unwrap_or(true),
is_trashed: bucket.is_trashed.get(i).copied().unwrap_or(false),
thumbhash,
ratio: 1.0, // TODO
visibility: bucket.visibility.get(i).cloned().unwrap(), // TODO: no unwrap
}
})
.collect();
let bucket = Arc::new(TimeBucket {
key: msg.time_bucket,
entries,
});
self.buckets.insert(bucket.key.clone(), bucket.clone());
Ok(bucket)
}
}
impl Message<GetAssetThumbnail> for Api {
type Reply = anyhow::Result<Arc<AssetThumbnail>>;
async fn handle(
&mut self,
msg: GetAssetThumbnail,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
let response = self
.client
.assets()
.thumbnail(msg.id)
.size(immich_sdk::AssetMediaSize::Thumbnail)
.execute()
.await
.context(anyhow!("Failed to get asset thumbnail for {}", msg.id))?;
let thumbnail = response
.decode()
.context(anyhow!("Failed to decode asset thumbnail for {}", msg.id))?
.into_rgba8();
let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
&thumbnail,
thumbnail.width(),
thumbnail.height(),
);
Ok(Arc::new(AssetThumbnail {
id: msg.id,
thumbnail: pixel_buffer,
}))
}
}

218
src/main.rs Normal file
View File

@@ -0,0 +1,218 @@
// 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")]
use clap::Parser;
use immich_sdk::AssetId;
use kameo::actor::{ActorRef, Spawn};
use slint::{
ComponentHandle as _, Model, ModelRc, SharedPixelBuffer, SharedString, ToSharedString,
VecModel, Weak,
};
use tracing::Level;
use crate::{
api::{Api, GetAssetThumbnail, GetTimeBucket, GetTimeBuckets, TimeBucketKey},
ui::AppWindow,
};
mod api;
mod thumbhash;
mod ui {
slint::include_modules!();
}
#[derive(clap::Parser)]
struct Opt {
#[clap(long, env = "IMMICH_BASE_URL")]
pub immich_base_url: String,
#[clap(long, env = "IMMICH_API_KEY")]
pub immich_api_key: String,
}
// enum ApiReq<M: Send + 'static>
// where
// Api: Message<M>,
// {
// AskRequest(
// AskRequest<
// 'static,
// Api,
// M,
// kameo::request::WithoutRequestTimeout,
// kameo::request::WithoutRequestTimeout,
// >,
// ),
// PendingReply(PendingReply<M, <Api as Message<M>>::Reply>),
// }
// enum ApiReqs {
// GetBuckets(ApiReq<GetTimeBuckets>),
// GetBucket(ApiReq<GetTimeBucket>),
// }
fn main() -> anyhow::Result<()> {
let opt = Opt::parse();
tracing_subscriber::fmt()
.with_max_level(Level::DEBUG)
.init();
let runtime = tokio::runtime::Runtime::new().unwrap();
let _rt_guard = runtime.enter();
let immich_config =
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::spawn(api);
let app = ui::AppWindow::new()?;
let app_weak = app.as_weak();
let api = api_.clone();
tokio::spawn(async move {
let Ok(buckets) = api.ask(GetTimeBuckets).await else {
return;
};
for bucket in buckets.iter() {
load_bucket(bucket.key.clone(), app_weak.clone(), api.clone());
}
let _ = app_weak.upgrade_in_event_loop(move |app| {
let preview_image = slint::Image::from_rgb8(SharedPixelBuffer::clone_from_slice(
&[0x33, 0x33, 0x33], // a single dark gray pixel
1,
1,
));
let global = app.global::<ui::Global>();
let buckets = buckets
.into_iter()
.map(|bucket| ui::ImageBucket {
key: bucket.key.to_shared_string(),
title: bucket.key.to_shared_string(), // TODO: format
count: bucket.count as i32,
previews: ModelRc::new(
(0..bucket.count)
.map(|_| ui::ImagePreview {
asset_id: SharedString::new(),
image: preview_image.clone(),
kind: ui::PreviewKind::None,
})
.collect::<VecModel<_>>(),
),
})
.collect::<VecModel<_>>();
global.set_image_buckets(ModelRc::new(buckets));
});
});
// FIXME: visibility-based callbacks are broken
// let app_weak = app.as_weak();
// let api = api_.clone();
// global.on_bucket_view_state(move |key, new_state| {
// eprintln!("{key} => {new_state:?}");
// // TODO
// if new_state == ui::ViewState::Hidden {
// return;
// }
// });
app.run()?;
Ok(())
}
fn load_bucket(time_bucket: TimeBucketKey, app_weak: Weak<AppWindow>, api: ActorRef<Api>) {
tokio::spawn(async move {
let Ok(api_bucket) = api
.ask(GetTimeBucket {
time_bucket: time_bucket.clone(),
})
.await
else {
return;
};
let _ = app_weak.upgrade_in_event_loop(move |app| {
let global = app.global::<ui::Global>();
let buckets = global.get_image_buckets();
let buckets = buckets
.as_any()
.downcast_ref::<VecModel<ui::ImageBucket>>()
.unwrap();
let Some(i) = buckets.iter().position(|b| &b.key == &time_bucket) else {
return;
};
let mut bucket = buckets.row_data(i).expect("i is in the list");
bucket.previews = ModelRc::new(
api_bucket
.entries
.iter()
.map(|entry| ui::ImagePreview {
asset_id: entry.id.to_shared_string(), // slint doesn't have a uuid type
kind: ui::PreviewKind::Thumbhash,
// TODO: don't unwrap
image: entry
.thumbhash
.clone()
.map(slint::Image::from_rgba8)
.unwrap(),
})
.collect::<VecModel<_>>(),
);
buckets.set_row_data(i, bucket);
for entry in api_bucket.entries.iter() {
load_thumbnail(time_bucket.clone(), entry.id, app.as_weak(), api.clone());
}
});
});
}
fn load_thumbnail(
time_bucket: TimeBucketKey,
id: AssetId,
app_weak: Weak<AppWindow>,
api: ActorRef<Api>,
) {
tokio::spawn(async move {
tracing::debug!("Fetching thumbnail for {id}");
let thumbnail = match api.ask(GetAssetThumbnail { id }).await {
Ok(thumbnail) => thumbnail,
Err(e) => {
tracing::error!("{e:?}");
return;
}
};
let _ = app_weak.upgrade_in_event_loop(move |app| {
let global = app.global::<ui::Global>();
let buckets = global.get_image_buckets();
let buckets = buckets
.as_any()
.downcast_ref::<VecModel<ui::ImageBucket>>()
.unwrap();
let Some(i) = buckets.iter().position(|b| &b.key == &time_bucket) else {
return;
};
let bucket = buckets.row_data(i).expect("i is in the list");
let id_str = id.to_string();
let Some(i) = bucket.previews.iter().position(|p| &p.asset_id == &id_str) else {
return;
};
let mut preview = bucket.previews.row_data(i).expect("i is in the list");
preview.image = slint::Image::from_rgba8(thumbnail.thumbnail.clone());
preview.kind = ui::PreviewKind::Thumbnail;
bucket.previews.set_row_data(i, preview);
});
});
}

23
src/thumbhash.rs Normal file
View File

@@ -0,0 +1,23 @@
use anyhow::anyhow;
use base64::{Engine as _, prelude::BASE64_STANDARD};
use slint::{Rgba8Pixel, SharedPixelBuffer};
pub fn thumbhashes_to_pixels(
thumbhashes: impl IntoIterator<Item = Option<impl AsRef<str>>>,
) -> anyhow::Result<Vec<Option<SharedPixelBuffer<Rgba8Pixel>>>> {
thumbhashes
.into_iter()
.map(|thash| {
let Some(thash) = thash else {
return Ok(None);
};
let thash = thash.as_ref();
let thash = BASE64_STANDARD.decode(thash)?;
let (w, h, preview) = thumbhash::thumb_hash_to_rgba(&thash)
.map_err(|e| anyhow!("Invalid thumbhash: {e:?}"))?;
Ok(Some(SharedPixelBuffer::clone_from_slice(
&preview, w as u32, h as u32,
)))
})
.collect()
}

195
ui/app-window.slint Normal file
View File

@@ -0,0 +1,195 @@
import { AboutSlint, Button, Palette, HorizontalBox, ScrollView } from "std-widgets.slint";
enum PreviewKind {
None,
Thumbhash,
Thumbnail,
}
struct ImagePreview {
asset_id: string,
image: image,
kind: PreviewKind,
}
struct ImageBucket {
key: string,
title: string,
count: int,
previews: [ImagePreview],
}
enum ViewState {
Hidden,
NearView,
InView,
}
export global Global {
in-out property <length> min-image-size: 100px;
in-out property <length> image-margin: 2px;
in-out property <[ImageBucket]> image-buckets: [
{ key: "2026-02-01", title: "Feb 1, 2026", count: 12 },
{ key: "2026-02-02", title: "Feb 2, 2026", count: 12 },
{ key: "2026-02-03", title: "Feb 3, 2026", count: 12 },
];
callback bucket-view-state(string, ViewState);
}
component Header inherits Rectangle {
width: 100%;
height: 48px;
background: Palette.alternate-background;
HorizontalBox {
height: parent.height;
Text {
text: "immich";
}
}
}
component ImagePreview inherits Rectangle {
in property <image> preview;
in property <length> size: 32px;
width: size;
height: size;
Image {
width: 100%;
height: 100%;
source: preview;
}
touch := TouchArea {
clicked => {
}
}
}
component TimelineBlock inherits VerticalLayout {
in property <int> index: -1;
in property <ImageBucket> bucket;
function calc-view-state() -> ViewState {
// TODO: fix this function
return ViewState.InView;
}
in property <ViewState> view-state: calc-view-state();
property <length> min-image-size: Global.min-image-size;
property <length> min-size-with-margin: min-image-size + Global.image-margin;
property <int> count-x: self.width / min-size-with-margin;
property <int> count-y: Math.ceil(bucket.count / count-x);
function calc-image-size() -> length {
let remaining-length = Math.mod(self.width, min-size-with-margin);
min-image-size + remaining-length / count-x
}
property <length> image-size: calc-image-size();
property <length> image-size-with-margin: image-size + Global.image-margin;
min-width: min-image-size;
alignment: start;
height: title-box.height + count-y * image-size-with-margin;
// FIXME: this triggers recursion errors in slint.
// changed view-state => {
// Global.bucket-view-state(bucket.key, view-state);
// }
// TODO: don't render subtree when self.view-state = Hidden
title-box := HorizontalBox {
alignment: space-between;
height: 36px;
Text {
text: bucket.title;
}
// TODO: checkbox thingy
Text {
text: "O";
}
}
// TODO: don't render subtree when self.view-state = Hidden
image-box := Rectangle {
width: 100%;
height: count-y * image-size-with-margin;
for preview[i] in bucket.previews : ImagePreview {
preview: preview.image;
size: image-size;
x: Global.image-margin / 2 + Math.mod(i, count-x) * (Global.image-margin + image-size);
y: Math.floor(i / count-x) * (image-size + Global.image-margin);
}
}
}
// component ImageViewer inherits Rectangle {
// in property <image> image;
// width: 100%;
// height: 100%;
// background: black;
// }
export component AppWindow inherits Window {
VerticalLayout {
padding: 0px;
width: 100%;
Header {}
ScrollView {
width: 100%;
mouse-drag-pan-enabled: true;
// viewport-height: 20000px;
// self.viewport-y goes into the negative. invert it to make things easier.
// property <length> viewport-top-y: -self.viewport-y;
// property <length> viewport-bottom-y: viewport-top-y + self.height;
// property <length> almost-visible-margin: self.height;
// function is-visible(top-y: length, bottom-y: length) -> bool {
// top-y <= viewport-bottom-y && viewport-top-y <= bottom-y
// }
// function is-almost-visible(top-y: length, bottom-y: length) -> bool {
// top-y <= (viewport-bottom-y + almost-visible-margin)
// && (viewport-top-y - almost-visible-margin) <= bottom-y
// }
// function view-state(top-y: length, bottom-y: length) -> ViewState {
// if is-visible(top-y, bottom-y) {
// return ViewState.InView;
// }
// if is-almost-visible(top-y, bottom-y) {
// return ViewState.NearView;
// }
// return ViewState.Hidden;
// }
VerticalLayout {
alignment: start;
padding: 0px;
width: 100%;
for bucket[i] in Global.image-buckets : TimelineBlock {
width: 100%;
index: i;
bucket: bucket;
// view-state: view-state(self.y, self.y + self.height);
}
}
}
}
}