Skip to main content

blockworx/document_ng/
image.rs

1use egui::Rect;
2
3/// The image a [`Image`] draws: either an SVG document (kept verbatim) or a PNG
4/// whose bytes are embedded in the document, so an image always saves and loads
5/// self-contained.
6#[derive(Clone, Debug, PartialEq)]
7pub enum ImageData {
8    /// Verbatim SVG document text, as read from the source file.
9    Svg(String),
10    /// Embedded PNG bytes, as read from the source file.
11    Png(Vec<u8>),
12}
13
14/// A free-floating image annotation drawn stretched to fill its rectangle
15/// [`inner`](Self::inner) — the image's aspect ratio is not preserved, so the box
16/// can be resized freely by its corners (a deliberate choice; an aspect-lock is a
17/// future opt-in).
18///
19/// [`image`](Self::image) is opaque content we never edit — either verbatim SVG
20/// text or embedded PNG bytes (see [`ImageData`]). A `Image` is used two ways:
21/// as a background [`Image`](super::Block::images) annotation stored per-block
22/// (like [`TextBox`](super::TextBox)es and [`Comment`](super::Comment)s — no pins,
23/// no routing, not in the hierarchy), and as a block's foreground
24/// [`icon`](super::Block::icon). The render backend caches the image bytes
25/// internally (a `bytes → ImageHandle` table inside the canvas), so the model
26/// never holds a canvas handle.
27#[derive(Clone, Debug, PartialEq)]
28pub struct Image {
29    /// The drawn image.
30    pub image: ImageData,
31    /// The rectangle the image is stretched to fill. Unlike blocks and comments,
32    /// images/icons are *not* grid-quantized — they size and position freely, so
33    /// this is a plain [`Rect`], not a `GridRect`.
34    pub inner: Rect,
35}
36
37impl Image {
38    /// A new image holding `image`, covering `rect` (free, no grid snapping).
39    pub fn new(image: ImageData, rect: Rect) -> Self {
40        Self { image, inner: rect }
41    }
42}