diff --git a/config/src/background.rs b/config/src/background.rs index cf31dfa2f..3dad7e5c3 100644 --- a/config/src/background.rs +++ b/config/src/background.rs @@ -1,11 +1,53 @@ use crate::{default_one_point_oh, Config, HsbTransform}; use luahelper::impl_lua_conversion_dynamic; -use wezterm_dynamic::{FromDynamic, ToDynamic}; +use wezterm_dynamic::{FromDynamic, FromDynamicOptions, ToDynamic, Value}; + +#[derive(Debug, Clone, FromDynamic, ToDynamic)] +pub struct ImageFileSource { + pub path: String, + + /// Adjust the animation rate for animated images + #[dynamic(default = "default_one_point_oh")] + pub speed: f32, +} + +#[derive(Debug, Clone, ToDynamic)] +pub struct ImageFileSourceWrap { + #[dynamic(flatten)] + inner: ImageFileSource, +} + +impl std::ops::Deref for ImageFileSourceWrap { + type Target = ImageFileSource; + fn deref(&self) -> &ImageFileSource { + &self.inner + } +} + +impl FromDynamic for ImageFileSourceWrap { + fn from_dynamic( + value: &Value, + options: FromDynamicOptions, + ) -> Result { + match value { + Value::String(path) => Ok(Self { + inner: ImageFileSource { + path: path.to_string(), + speed: 1.0, + }, + }), + _ => { + let inner = ImageFileSource::from_dynamic(value, options)?; + Ok(Self { inner }) + } + } + } +} #[derive(Debug, Clone, FromDynamic, ToDynamic)] pub enum BackgroundSource { Gradient(Gradient), - File(String), + File(ImageFileSourceWrap), } #[derive(Debug, Clone, FromDynamic, ToDynamic)] @@ -51,7 +93,12 @@ impl BackgroundLayer { let source = if let Some(gradient) = &cfg.window_background_gradient { BackgroundSource::Gradient(gradient.clone()) } else if let Some(path) = &cfg.window_background_image { - BackgroundSource::File(path.to_string_lossy().to_string()) + BackgroundSource::File(ImageFileSourceWrap { + inner: ImageFileSource { + path: path.to_string_lossy().to_string(), + speed: 1.0, + }, + }) } else { return None; }; diff --git a/termwiz/src/image.rs b/termwiz/src/image.rs index 25d379660..88d537e00 100644 --- a/termwiz/src/image.rs +++ b/termwiz/src/image.rs @@ -270,15 +270,38 @@ impl ImageDataType { match self { ImageDataType::EncodedFile(data) => hasher.update(data), ImageDataType::Rgba8 { data, .. } => hasher.update(data), - ImageDataType::AnimRgba8 { frames, .. } => { + ImageDataType::AnimRgba8 { + frames, durations, .. + } => { for data in frames { hasher.update(data); } + for d in durations { + let d = d.as_secs_f32(); + let b = d.to_ne_bytes(); + hasher.update(b); + } } }; hasher.finalize().into() } + /// Divides the animation frame durations by the provided + /// speed_factor, so a factor of 2 will halve the duration. + /// # Panics + /// if the speed_factor is negative, non-finite or the result + /// overflows the allow Duration range. + pub fn adjust_speed(&mut self, speed_factor: f32) { + match self { + Self::AnimRgba8 { durations, .. } => { + for d in durations { + *d = d.mul_f32(1. / speed_factor); + } + } + _ => {} + } + } + /// Decode an encoded file into either an Rgba8 or AnimRgba8 variant /// if we recognize the file format, otherwise the EncodedFile data /// is preserved as is. diff --git a/wezterm-gui/src/termwindow/background.rs b/wezterm-gui/src/termwindow/background.rs index 4b25471f4..aa7d8d26c 100644 --- a/wezterm-gui/src/termwindow/background.rs +++ b/wezterm-gui/src/termwindow/background.rs @@ -20,16 +20,17 @@ struct CachedImage { modified: SystemTime, image: Arc, marked: bool, + speed: f32, } impl CachedImage { - fn load(path: &str) -> anyhow::Result> { + fn load(path: &str, speed: f32) -> anyhow::Result> { let modified = std::fs::metadata(path) .and_then(|m| m.modified()) .with_context(|| format!("getting metadata for {}", path))?; let mut cache = IMAGE_CACHE.lock().unwrap(); if let Some(cached) = cache.get_mut(path) { - if cached.modified == modified { + if cached.modified == modified && cached.speed == speed { cached.marked = false; return Ok(Arc::clone(&cached.image)); } @@ -38,7 +39,8 @@ impl CachedImage { let data = std::fs::read(path) .with_context(|| format!("Failed to load window_background_image {}", path))?; log::trace!("loaded {}", path); - let data = ImageDataType::EncodedFile(data).decode(); + let mut data = ImageDataType::EncodedFile(data).decode(); + data.adjust_speed(speed); let image = Arc::new(ImageData::with_data(data)); cache.insert( @@ -47,6 +49,7 @@ impl CachedImage { modified, image: Arc::clone(&image), marked: false, + speed, }, ); @@ -204,7 +207,7 @@ fn load_background_layer( width, height, data, ))) } - BackgroundSource::File(path) => CachedImage::load(path)?, + BackgroundSource::File(source) => CachedImage::load(&source.path, source.speed)?, }; Ok(LoadedBackgroundLayer {