Skip to main content

ferroui_base/media/
i_image_brush.rs

1use crate::media::ref_adapter::RefAdapter;
2use crate::media::{Brush, ITileBrush, ImageBrush};
3use crate::platform::IBitmapImpl;
4use crate::utilities::RefCounted;
5use crate::{ObjectType, Upcast};
6use std::rc::Rc;
7
8/// Paints an area with an image.
9pub trait IImageBrush: ITileBrush {
10    /// The image to draw.
11    fn source(&self) -> Option<Rc<dyn IImageBrushSource>>;
12}
13
14/// An image that can be the source of an [`IImageBrush`].
15pub trait IImageBrushSource: 'static {
16    /// The counted reference to the platform bitmap, while it is alive.
17    fn bitmap(&self) -> Option<&RefCounted<crate::platform::SharedBitmapImpl>>;
18
19    /// The platform bitmap, while it is alive.
20    fn get_bitmap(&self) -> Option<std::sync::Arc<crate::platform::SharedBitmapImpl>> {
21        self.bitmap().map(|bitmap| bitmap.item())
22    }
23
24    /// The identity of the source, used for reference equality.
25    #[doc(hidden)]
26    fn reference_id(&self) -> *const () {
27        self as *const Self as *const ()
28    }
29}
30
31/// Image brush sources compare by reference.
32impl PartialEq for dyn IImageBrushSource {
33    #[inline]
34    fn eq(&self, other: &Self) -> bool {
35        self.reference_id() == other.reference_id()
36    }
37}
38
39impl std::fmt::Debug for dyn IImageBrushSource {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        f.write_str("IImageBrushSource")
42    }
43}
44
45impl<T: ObjectType + Upcast<Brush>> IImageBrush for RefAdapter<T> {
46    fn source(&self) -> Option<Rc<dyn IImageBrushSource>> {
47        self.class::<ImageBrush>().source()
48    }
49}