background: allow specifying the animation speed for images

The `File` variant for background layers may now be an object
that specifes a speed factor.  That factor is applied to the
animation frame durations in the loaded image, allowing the
playback rate to be adjusted.
This commit is contained in:
Wez Furlong committed 2022-05-31 07:32:05 -07:00
1 parent 3ef533e858
commit 4cb75c154f
3 files changed
+81 -8

No files matched your search

+50 -3
View File
@@ -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<Self, wezterm_dynamic::Error> {
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;
};
+24 -1
View File
@@ -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.
+7 -4
View File
@@ -20,16 +20,17 @@ struct CachedImage {
modified: SystemTime,
image: Arc<ImageData>,
marked: bool,
speed: f32,
}
impl CachedImage {
fn load(path: &str) -> anyhow::Result<Arc<ImageData>> {
fn load(path: &str, speed: f32) -> anyhow::Result<Arc<ImageData>> {
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 {