fix: refine thumbnail spacing and indicate excerpt overflow

This commit is contained in:
2026-10-11 11:38:11 +02:00
parent 01f51be18e
commit 4205b56653
2 changed files with 92 additions and 37 deletions
+4 -29
View File
@@ -108,7 +108,7 @@ impl Thumbnail {
if let Some(size) = image.protocol.size_for(resize.clone(), area.into()) {
frame.render_stateful_widget(
StatefulImage::new().resize(resize),
centered(area, size),
right_aligned(area, size),
&mut image.protocol,
);
if image.protocol.protocol_type().is_some() {
@@ -235,11 +235,11 @@ fn decode(bytes: Vec<u8>) -> Result<image::DynamicImage, String> {
.map_err(|error| format!("Could not decode thumbnail JPEG: {error}"))
}
fn centered(area: Rect, size: Size) -> Rect {
fn right_aligned(area: Rect, size: Size) -> Rect {
let width = size.width.min(area.width);
let height = size.height.min(area.height);
Rect::new(
area.x + (area.width - width) / 2,
area.x + area.width - width,
area.y + (area.height - height) / 2,
width,
height,
@@ -332,9 +332,9 @@ mod tests {
let top = pixels.iter().map(|&(_, y)| y).min().unwrap();
let bottom = pixels.iter().map(|&(_, y)| y).max().unwrap() + 1;
assert!(left >= area.x && right <= area.right());
assert_eq!(right, area.right()); // Keep the image against the metadata-facing edge.
assert!(top >= area.y && bottom <= area.bottom());
assert!(right - left == area.width || bottom - top == area.height);
assert!((left - area.x).abs_diff(area.right() - right) <= 1);
assert!((top - area.y).abs_diff(area.bottom() - bottom) <= 1);
let width = u32::from(right - left) * u32::from(font.width);
let height = u32::from(bottom - top) * u32::from(font.height);
@@ -344,29 +344,4 @@ mod tests {
);
}
}
#[test]
fn resized_image_preserves_aspect_and_stays_inside_popup() {
let picker = Picker::halfblocks();
let font = picker.font_size();
let protocol = picker.new_resize_protocol(image::DynamicImage::new_rgb8(480, 360));
let area = Rect::new(7, 3, 80, 10);
let size = protocol.size_for(Resize::Fit(None), area.into());
let image_area = centered(area, size);
assert!(image_area.x >= area.x && image_area.right() <= area.right());
assert!(image_area.y >= area.y && image_area.bottom() <= area.bottom());
let left = image_area.x - area.x;
let right = area.right() - image_area.right();
let top = image_area.y - area.y;
let bottom = area.bottom() - image_area.bottom();
assert!(left.abs_diff(right) <= 1 && top.abs_diff(bottom) <= 1);
let pixel_width = u32::from(image_area.width) * u32::from(font.width);
let pixel_height = u32::from(image_area.height) * u32::from(font.height);
// Cell rounding may add at most one cell to either fitted pixel dimension.
assert!(
pixel_width.abs_diff(pixel_height * 4 / 3)
<= u32::from(font.width) + u32::from(font.height) * 4 / 3
);
assert_eq!(centered(Rect::new(7, 3, 0, 0), size), Rect::new(7, 3, 0, 0));
}
}