Skip to main content

photon_ui/components/
image_widget.rs

1use crate::{
2    Component,
3    RenderError,
4    Rendered,
5};
6
7/// A placeholder component for inline terminal images.
8///
9/// The actual image data is passed to the renderer via
10/// [`ImageCommand`](crate::renderer::ImageCommand); this widget only renders a
11/// placeholder text line. Image protocol encoding (Kitty, iTerm2) is handled by
12/// [`crate::image`].
13pub struct ImageWidget {
14    _data: Vec<u8>,
15    _mime_type: String,
16    placeholder: String,
17}
18
19impl ImageWidget {
20    /// Create a new image widget.
21    ///
22    /// `data` is the raw image bytes. `placeholder` defaults to `"[image]"`.
23    pub fn new(data: Vec<u8>, mime_type: impl Into<String>, placeholder: Option<String>) -> Self {
24        Self {
25            _data: data,
26            _mime_type: mime_type.into(),
27            placeholder: placeholder.unwrap_or_else(|| "[image]".to_string()),
28        }
29    }
30}
31
32impl Component for ImageWidget {
33    fn render(&self, _width: u16) -> Result<Rendered, RenderError> {
34        Ok(Rendered {
35            lines: vec![self.placeholder.clone()],
36            cursor: None,
37            images: Vec::new(),
38        })
39    }
40}