Skip to main content

ferroui_base/rendering/composition/server/
server_composition_surface.rs

1use super::IServerObject;
2use crate::platform::IBitmapImpl;
3use std::cell::RefCell;
4use std::rc::Rc;
5
6/// The `Changed` delegate of a server-side surface: the callbacks of the
7/// visuals that show it.
8#[derive(Default)]
9pub struct ServerCompositionSurfaceChanged {
10    handlers: RefCell<Vec<(usize, Rc<dyn Fn()>)>>,
11}
12
13impl ServerCompositionSurfaceChanged {
14    pub fn new() -> Self {
15        Self::default()
16    }
17
18    /// `Changed += handler`; `key` identifies the subscriber.
19    pub fn add(&self, key: usize, handler: Rc<dyn Fn()>) {
20        self.handlers.borrow_mut().push((key, handler));
21    }
22
23    /// `Changed -= handler`.
24    pub fn remove(&self, key: usize) {
25        let mut handlers = self.handlers.borrow_mut();
26        if let Some(index) = handlers.iter().rposition(|(k, _)| *k == key) {
27            handlers.remove(index);
28        }
29    }
30
31    /// `Changed?.Invoke()`.
32    pub fn invoke(&self) {
33        let handlers: Vec<_> = self.handlers.borrow().iter().map(|(_, h)| h.clone()).collect();
34        for handler in handlers {
35            handler();
36        }
37    }
38}
39
40/// Server-side counterpart of a composition surface: something that
41/// provides a bitmap to the surface visuals that show it.
42pub trait IServerCompositionSurface: IServerObject {
43    /// The current content of the surface.
44    fn bitmap(&self) -> Option<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
45
46    /// Raised when the content changed.
47    fn changed(&self) -> &ServerCompositionSurfaceChanged;
48}