Skip to main content

ferroui_base/rendering/composition/server/
server_loaded_image_surface.rs

1use super::{IServerCompositionSurface, IServerObject, ServerCompositionSurfaceChanged};
2use crate::utilities::RefCounted;
3use std::any::Any;
4use std::cell::RefCell;
5use std::rc::Rc;
6
7/// The server-side counterpart of a
8/// [`LoadedImageSurface`](crate::rendering::composition::LoadedImageSurface):
9/// a surface whose content is a bitmap given when it is created.
10pub struct ServerLoadedImageSurface {
11    bitmap: RefCell<Option<RefCounted<crate::platform::SharedBitmapImpl>>>,
12    changed: ServerCompositionSurfaceChanged,
13}
14
15impl ServerLoadedImageSurface {
16    /// Creates the surface with its bitmap (`None` for an image that did not
17    /// load). The surface holds a reference to the bitmap until it is
18    /// disposed.
19    pub fn new(bitmap: Option<RefCounted<crate::platform::SharedBitmapImpl>>) -> Rc<ServerLoadedImageSurface> {
20        Rc::new(ServerLoadedImageSurface { bitmap: RefCell::new(bitmap), changed: ServerCompositionSurfaceChanged::new() })
21    }
22}
23
24impl IServerObject for ServerLoadedImageSurface {
25    fn dispose(&self) {
26        if let Some(bitmap) = self.bitmap.borrow_mut().take() {
27            bitmap.dispose();
28        }
29        self.changed.invoke();
30    }
31
32    fn as_surface(self: Rc<Self>) -> Option<Rc<dyn IServerCompositionSurface>> {
33        Some(self)
34    }
35
36    fn as_any(&self) -> &dyn Any {
37        self
38    }
39
40    fn into_any_rc(self: Rc<Self>) -> Rc<dyn Any> {
41        self
42    }
43}
44
45impl IServerCompositionSurface for ServerLoadedImageSurface {
46    fn bitmap(&self) -> Option<std::sync::Arc<crate::platform::SharedBitmapImpl>> {
47        self.bitmap.borrow().as_ref().filter(|bitmap| bitmap.is_alive()).map(|bitmap| bitmap.item())
48    }
49
50    fn changed(&self) -> &ServerCompositionSurfaceChanged {
51        &self.changed
52    }
53}