Skip to main content

ferroui_xaml_interactions_custom/writeable_bitmap/
writeable_bitmap_ref.rs

1//! Not from the reference: the handle of a writeable bitmap that the `Bitmap` properties of the
2//! writeable bitmap behaviors, triggers and actions hold.
3//!
4//! Deviation (DEVIATIONS.md, Behaviors): a property value is cloned and compares with `==`; a
5//! [`WriteableBitmap`] of the framework is neither, so the properties typed `WriteableBitmap` in
6//! the reference hold this shared handle, which compares by identity as the reference compares
7//! its object references.
8
9use ferroui_base::media::imaging::WriteableBitmap;
10use ferroui_base::media::IImage;
11use std::fmt;
12use std::ops::Deref;
13use std::rc::Rc;
14
15/// A writeable bitmap held by a property.
16#[derive(Clone)]
17pub struct WriteableBitmapRef(pub Rc<WriteableBitmap>);
18
19impl WriteableBitmapRef {
20    /// The handle of `bitmap`.
21    pub fn new(bitmap: WriteableBitmap) -> Self {
22        Self(Rc::new(bitmap))
23    }
24
25    /// The shared bitmap.
26    pub fn get(&self) -> &Rc<WriteableBitmap> {
27        &self.0
28    }
29
30    /// The bitmap as an image (the source of an image control).
31    pub fn as_image(&self) -> Rc<dyn IImage> {
32        self.0.clone()
33    }
34}
35
36impl Deref for WriteableBitmapRef {
37    type Target = WriteableBitmap;
38
39    fn deref(&self) -> &WriteableBitmap {
40        &self.0
41    }
42}
43
44impl PartialEq for WriteableBitmapRef {
45    fn eq(&self, other: &Self) -> bool {
46        Rc::ptr_eq(&self.0, &other.0)
47    }
48}
49
50impl From<Rc<WriteableBitmap>> for WriteableBitmapRef {
51    fn from(value: Rc<WriteableBitmap>) -> Self {
52        Self(value)
53    }
54}
55
56impl fmt::Debug for WriteableBitmapRef {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        write!(f, "WriteableBitmapRef({:p})", Rc::as_ptr(&self.0))
59    }
60}