mirror of
https://github.com/openai/codex.git
synced 2026-12-06 01:10:58 +00:00
## Why Composer thumbnails and transcript tool previews read and decoded files during rendering. A JPEG whose decoded pixels exceeded the 32 MiB allocation budget could lose its preview even when the encoded file fit within the file size limit. ## What changed - Load visible composer thumbnails and transcript previews on background workers and request a redraw when the result is ready. Reuse prepared tool previews when the viewport changes size. - Share the sidebar's scaled JPEG fallback with composer and transcript previews. Preserve the oriented source aspect ratio and enforce the encoded file size limit separately from the decode budget. - Check that the owning thread has a local workspace before reading an inspected image. Generated artifacts can still load from the daemon host. ## Testing Cover large JPEG previews in the composer and transcript, thumbnail refresh after an attachment is replaced, text fallback for oversized PNGs, and an inspected path shared by local and remote workspaces. GitOrigin-RevId: f2c8948a9abaa2527932423224b6ebeda3e122ae
161 lines
5.3 KiB
Rust
161 lines
5.3 KiB
Rust
//! Decode a workspace image once and reuse the transcript's terminal image lifecycle.
|
||
|
||
use std::sync::Arc;
|
||
|
||
use color_eyre::eyre::OptionExt;
|
||
use color_eyre::eyre::Result;
|
||
use image::DynamicImage;
|
||
use image::ImageFormat;
|
||
use ratatui::buffer::Buffer;
|
||
use ratatui::layout::Rect;
|
||
use ratatui::style::Stylize;
|
||
use ratatui::text::Line;
|
||
use ratatui::widgets::Widget;
|
||
|
||
use crate::image_decode;
|
||
use crate::image_decode::MAX_PREVIEW_EDGE;
|
||
use crate::pets::ImageProtocol;
|
||
use crate::transcript_image::ImagePlacement;
|
||
use crate::transcript_image::TranscriptImage;
|
||
use crate::transcript_image::terminal_cell_size;
|
||
|
||
#[cfg(test)]
|
||
#[path = "sidebar_image_tests.rs"]
|
||
mod tests;
|
||
|
||
#[derive(Debug)]
|
||
pub(crate) enum DocumentContent {
|
||
Text(String),
|
||
Image(Box<SidebarImage>),
|
||
}
|
||
|
||
pub(crate) struct SidebarImage {
|
||
label: String,
|
||
source_size: (u32, u32),
|
||
raster: DynamicImage,
|
||
fitted: Option<Arc<TranscriptImage>>,
|
||
}
|
||
|
||
impl std::fmt::Debug for SidebarImage {
|
||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
f.debug_struct("SidebarImage")
|
||
.field("label", &self.label)
|
||
.finish_non_exhaustive()
|
||
}
|
||
}
|
||
|
||
pub(crate) fn supports_path(path: &str) -> bool {
|
||
path.rsplit_once('.')
|
||
.and_then(|(_, extension)| ImageFormat::from_extension(extension))
|
||
.is_some_and(supported_format)
|
||
}
|
||
|
||
fn supported_format(format: ImageFormat) -> bool {
|
||
matches!(
|
||
format,
|
||
ImageFormat::Png | ImageFormat::Jpeg | ImageFormat::Gif | ImageFormat::WebP
|
||
)
|
||
}
|
||
|
||
impl SidebarImage {
|
||
pub(crate) fn decode(bytes: &[u8]) -> Result<Self> {
|
||
let format = image::guess_format(bytes)
|
||
.ok()
|
||
.filter(|format| supported_format(*format))
|
||
.ok_or_eyre("Unsupported image format")?;
|
||
let preview = image_decode::decode_preview(bytes, format)?;
|
||
let (width, height) = preview.size;
|
||
let image = preview.image;
|
||
Ok(Self {
|
||
label: format!(
|
||
"{} · {width} × {height}",
|
||
format.extensions_str()[0].to_ascii_uppercase()
|
||
),
|
||
source_size: (width, height),
|
||
raster: if image.width() > MAX_PREVIEW_EDGE || image.height() > MAX_PREVIEW_EDGE {
|
||
image.thumbnail(MAX_PREVIEW_EDGE, MAX_PREVIEW_EDGE)
|
||
} else {
|
||
image
|
||
},
|
||
fitted: None,
|
||
})
|
||
}
|
||
|
||
pub(crate) fn paint(
|
||
&mut self,
|
||
area: Rect,
|
||
buf: &mut Buffer,
|
||
protocol: Option<ImageProtocol>,
|
||
) -> Option<ImagePlacement> {
|
||
Line::from(self.label.as_str()).dim().render(area, buf);
|
||
let available_rows = area.height.saturating_sub(1 + u16::from(area.height > 2));
|
||
// tmux uses Kitty's virtual placements. The shared placeholder renderer
|
||
// currently emits row marks for up to six rows.
|
||
let rows = if crate::tui::fullscreen_virtual_image_base().is_some() {
|
||
available_rows.min(6)
|
||
} else {
|
||
available_rows
|
||
};
|
||
let body = Rect::new(area.x, area.y.saturating_add(1), area.width, rows);
|
||
if body.is_empty() {
|
||
return None;
|
||
}
|
||
let Some((protocol, (cw, ch))) = protocol.and_then(|protocol| {
|
||
let size = terminal_cell_size()
|
||
.or_else(|| (protocol != ImageProtocol::Sixel).then_some((8, 16)))?;
|
||
Some((protocol, size))
|
||
}) else {
|
||
Line::from("Image preview unavailable in this terminal").render(body, buf);
|
||
return None;
|
||
};
|
||
let (width, height) = self.source_size;
|
||
let (max_width, max_height) = match protocol {
|
||
ImageProtocol::Sixel => (160, 80),
|
||
ImageProtocol::Kitty | ImageProtocol::KittyLocalFile => (width, height),
|
||
};
|
||
let scale = (f64::from((u32::from(body.width) * cw).min(max_width)) / f64::from(width))
|
||
.min(f64::from((u32::from(body.height) * ch).min(max_height)) / f64::from(height))
|
||
.min(1.0);
|
||
let pixel_width = (f64::from(width) * scale).round().max(1.0) as u32;
|
||
let pixel_height = (f64::from(height) * scale).round().max(1.0) as u32;
|
||
let columns = pixel_width.div_ceil(cw) as u16;
|
||
let rows = pixel_height.div_ceil(ch) as u16;
|
||
if !self.fitted.as_ref().is_some_and(|image| {
|
||
image.protocol == protocol
|
||
&& image.cell_width == cw
|
||
&& image.cell_height == ch
|
||
&& image.columns == columns
|
||
&& image.rows == rows
|
||
}) {
|
||
let pixels = self
|
||
.raster
|
||
.thumbnail(
|
||
pixel_width.min(self.raster.width()),
|
||
pixel_height.min(self.raster.height()),
|
||
)
|
||
.to_rgba8();
|
||
self.fitted = Some(Arc::new(TranscriptImage {
|
||
protocol,
|
||
columns,
|
||
rows,
|
||
pixels,
|
||
cell_width: cw,
|
||
cell_height: ch,
|
||
}));
|
||
}
|
||
let image = self.fitted.as_ref()?;
|
||
Some(ImagePlacement {
|
||
image: Arc::clone(image),
|
||
area: Rect::new(
|
||
body.x + (body.width - image.columns) / 2,
|
||
body.y + (body.height - image.rows) / 2,
|
||
image.columns,
|
||
image.rows,
|
||
),
|
||
first_row: 0,
|
||
file: None,
|
||
terminal_id: None,
|
||
})
|
||
}
|
||
}
|