ferroui_base/media/i_image.rs
1use crate::media::imaging::IBitmap;
2use crate::media::{DrawingContext, IAffectsRender};
3use crate::{FerroObject, Rect, Size};
4use std::any::Any;
5
6/// Represents a bitmap or drawing which can be drawn.
7///
8/// Implemented by the bitmap types and, through an adapter, by the image
9/// classes (cropped bitmaps and drawing images): a handle of such a class
10/// converts to `Rc<dyn IImage>` with `into()`. The `as_*` members replace
11/// interface casts (`image as IBitmap`).
12pub trait IImage: 'static {
13 /// The size of the image, in device independent pixels.
14 fn size(&self) -> Size;
15
16 /// Draws the image to a drawing context: the part of the image inside
17 /// `source_rect` is drawn into `dest_rect`.
18 fn draw(&self, context: &mut DrawingContext<'_>, source_rect: Rect, dest_rect: Rect);
19
20 /// The implementing value, for downcasts to concrete image types. For
21 /// image classes use [`as_object`](Self::as_object).
22 fn as_any(&self) -> &dyn Any;
23
24 /// The object behind the image when it is an image class.
25 fn as_object(&self) -> Option<&FerroObject> {
26 None
27 }
28
29 /// The image viewed as a bitmap, when it is one.
30 fn as_bitmap(&self) -> Option<&dyn IBitmap> {
31 None
32 }
33
34 /// The image viewed as [`IAffectsRender`], when it can change.
35 fn as_affects_render(&self) -> Option<&dyn IAffectsRender> {
36 None
37 }
38
39 /// The identity of the image, used for reference equality.
40 #[doc(hidden)]
41 fn reference_id(&self) -> *const () {
42 self as *const Self as *const ()
43 }
44}
45
46/// Images compare by reference.
47impl PartialEq for dyn IImage {
48 #[inline]
49 fn eq(&self, other: &Self) -> bool {
50 self.reference_id() == other.reference_id()
51 }
52}
53
54impl std::fmt::Debug for dyn IImage {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 write!(f, "IImage({})", self.size())
57 }
58}