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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use super::IServerObject;
use crate::platform::{IBitmapImpl, IDrawingContextImpl};
use crate::{Rect, Size};
use std::cell::RefCell;
use std::rc::Rc;
/// The `Changed` delegate of a server-side surface: the callbacks of the
/// visuals that show it.
#[derive(Default)]
pub struct ServerCompositionSurfaceChanged {
handlers: RefCell<Vec<(usize, Rc<dyn Fn()>)>>,
}
impl ServerCompositionSurfaceChanged {
pub fn new() -> Self {
Self::default()
}
/// `Changed += handler`; `key` identifies the subscriber.
pub fn add(&self, key: usize, handler: Rc<dyn Fn()>) {
self.handlers.borrow_mut().push((key, handler));
}
/// `Changed -= handler`.
pub fn remove(&self, key: usize) {
let mut handlers = self.handlers.borrow_mut();
if let Some(index) = handlers.iter().rposition(|(k, _)| *k == key) {
handlers.remove(index);
}
}
/// `Changed?.Invoke()`.
pub fn invoke(&self) {
let handlers: Vec<_> = self.handlers.borrow().iter().map(|(_, h)| h.clone()).collect();
for handler in handlers {
handler();
}
}
}
/// Server-side counterpart of a composition surface: something that
/// provides a bitmap to the surface visuals that show it.
///
/// The WinUI port adds the surfaces whose content is not a bitmap (a
/// visual surface renders a visual subtree): what a surface visual or a
/// surface brush shows goes through [`natural_size`](Self::natural_size)
/// and [`draw`](Self::draw), whose defaults use the bitmap.
pub trait IServerCompositionSurface: IServerObject {
/// The current content of the surface.
fn bitmap(&self) -> Option<std::sync::Arc<crate::platform::SharedBitmapImpl>>;
/// Raised when the content changed.
fn changed(&self) -> &ServerCompositionSurfaceChanged;
/// The size of the content in device independent pixels (the natural
/// size of WinUI's surfaces), which a surface brush lays out; `None`
/// while the surface has no content. The default is the size of the
/// bitmap at its DPI.
fn natural_size(&self) -> Option<Size> {
let bitmap = self.bitmap()?;
let size = bitmap.pixel_size().to_size_with_dpi_vector(bitmap.dpi());
(size.width > 0.0 && size.height > 0.0).then_some(size)
}
/// Draws the whole content of the surface into `dest`, in the
/// coordinates of `canvas`. The default draws the bitmap.
fn draw(&self, canvas: &mut dyn IDrawingContextImpl, dest: Rect, opacity: f64) {
let Some(bitmap) = self.bitmap() else { return };
canvas.draw_bitmap(&*bitmap, opacity, Rect::from_size(bitmap.pixel_size().to_size(1.0)), dest);
}
}