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