Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
Decode profile effects at the card's size, with fewer frames (#44)
youtsuho0ae98a861169
diff --git a/apps/rustcord/src/ui/profile_effect.rs b/apps/rustcord/src/ui/profile_effect.rsindex dec55de..938a4bb 100644--- a/apps/rustcord/src/ui/profile_effect.rs+++ b/apps/rustcord/src/ui/profile_effect.rs@@ -100,7 +100,10 @@ impl RustCord {// The clock starts once every layer is ready to play.let now = ctx.input(|i| i.time);let clock_id = egui::Id::new(("profile_effect_clock", slot));- let ready = layers.iter().filter_map(|l| l["src"].as_str()).all(|src| crate::video::frame_at(&ctx, src, 0, false).is_some());+ // Each layer decoded as wide as the card is on screen, not at its own size.+ let card_px = rect.width() * ctx.pixels_per_point();+ let key = |src: &str| crate::video::effect_key(src, card_px);+ let ready = layers.iter().filter_map(|l| l["src"].as_str()).all(|src| crate::video::frame_at(&ctx, &key(src), 0, false).is_some());let clock = ctx.data(|d| d.get_temp::<Clock>(clock_id)).filter(|c| ready && c.0 == sku.0 && c.1 == opening);let Some((_, _, start)) = clock else {ctx.data_mut(|d| d.insert_temp::<Clock>(clock_id, (sku.0, opening, now)));@@ -113,7 +116,7 @@ impl RustCord {let num = |key: &str| layer[key].as_f64().unwrap_or(0.0);match phase(time, num("start"), num("duration"), layer["loop"].as_bool().unwrap_or(false), num("loopDelay")) {Phase::Showing { at, looping } => {- if let Some(uri) = crate::video::frame_at(&ctx, src, at as u64, looping) {+ if let Some(uri) = crate::video::frame_at(&ctx, &key(src), at as u64, looping) {draw(&canvas, uri, Some(layer));}// (A layer at its last frame asks for nothing: come back when it ends.)diff --git a/apps/rustcord/src/video.rs b/apps/rustcord/src/video.rsindex cae2810..6495e72 100644--- a/apps/rustcord/src/video.rs+++ b/apps/rustcord/src/video.rs@@ -28,6 +28,17 @@ const MAX_FRAMES: usize = 150;/// plays every tile in view at once.const MAX_THUMB_FRAMES: usize = 75;const THUMB_TAG: &str = "@thumb=";+const EFFECT_TAG: &str = "@effect=";+/// Largest size a profile effect layer is decoded to (Discord's are smaller).+const EFFECT_MAX: (usize, usize) = (640, 1100);++/// The key of profile effect layer `url`'s clip decoded for a card `w` px+/// wide (rounded up to 64 so cards of nearly the same size share it): the+/// layer is drawn as wide as the card, so pixels beyond that are never seen.+pub fn effect_key(url: &str, w: f32) -> String {+ let up = ((w.max(64.0) / 64.0).ceil() as usize * 64).min(EFFECT_MAX.0);+ format!("{url}#{EFFECT_TAG}{up}")+}/// The key of `url`'s clip decoded for a tile `w`×`h` px (rounded up to 32 so/// a slightly different layout reuses it), with fewer frames kept: a grid@@ -46,6 +57,10 @@ fn decode_limits(key: &str) -> (usize, usize, usize) {/// thumbnail's.fn decode_limits_within(key: &str, normal: (usize, usize)) -> (usize, usize, usize) {let fragment = key.rsplit('#').next().unwrap_or("");+ if let Some(w) = fragment.strip_prefix(EFFECT_TAG).and_then(|w| w.parse::<usize>().ok()) {+ // As wide as the card; the height follows the picture's shape.+ return (w.clamp(1, EFFECT_MAX.0), EFFECT_MAX.1, MAX_FRAMES);+ }let thumb = fragment.rsplit_once(THUMB_TAG).and_then(|(_, s)| s.split_once('x')).and_then(|(w, h)| Some((w.parse().ok()?, h.trim_end_matches('~').parse().ok()?)));match thumb {Some((w, h)) => (usize::min(w, MAX_W), usize::min(h, MAX_W), MAX_THUMB_FRAMES),@@ -272,9 +287,9 @@ pub fn install(ctx: &egui::Context) {decode_gif(&bytes, decode_limits(&key))} else if bytes.starts_with(b"\x89PNG") {// Avatar decorations and profile effects: animated PNG (APNG).- decode_apng(&bytes, decode_limits_within(&key, (640, 1100)))+ decode_apng(&bytes, decode_limits_within(&key, EFFECT_MAX))} else if bytes.len() > 12 && &bytes[..4] == b"RIFF" && &bytes[8..12] == b"WEBP" {- decode_webp(&bytes, decode_limits_within(&key, (640, 1100)))+ decode_webp(&bytes, decode_limits_within(&key, EFFECT_MAX))} else {decode_mp4(&bytes, decode_limits(&key))};@@ -1026,4 +1041,23 @@ mod tests {assert!(matches!(clip_key("https://a/x.gif?size=96"), std::borrow::Cow::Borrowed(_)));assert!(matches!(clip_key("https://a/x.mp4#anim"), std::borrow::Cow::Borrowed(_)));}++ #[test]+ fn effect_layers_decode_at_card_size() {+ let url = "https://cdn.discordapp.com/assets/collectibles/effect.png";+ // Rounded up to 64 px, never past the usual limit.+ let key = effect_key(url, 300.0);+ assert_eq!(key, format!("{url}#{EFFECT_TAG}320"));+ assert_eq!(effect_key(url, 290.0), key);+ assert_eq!(decode_limits_within(&key, EFFECT_MAX), (320, EFFECT_MAX.1, MAX_FRAMES));+ assert_eq!(decode_limits_within(&effect_key(url, 4000.0), EFFECT_MAX).0, EFFECT_MAX.0);+ assert_eq!(decode_limits_within(&effect_key(url, 0.0), EFFECT_MAX).0, 64);+ // A layer without the tag (avatar decorations) keeps the usual limits.+ assert_eq!(decode_limits_within(url, EFFECT_MAX), (EFFECT_MAX.0, EFFECT_MAX.1, MAX_FRAMES));+ // The file is still the layer's own address, and frames find their clip.+ assert_eq!(clip_key(&key), key);+ assert_eq!(clip_source(&key), url);+ let frame = format!("{key}{FRAME_TAG}12");+ assert_eq!(split_frame_uri(&frame), Some((key.as_str(), 12)));+ }}