compose_rust/native_view.rs
1//! Where a window's platform views are made: the handle the Renderer reports for each
2//! window that holds a `NativeView`.
3//!
4//! A platform view (a web view, a video surface, a map) is made by the application, or by a
5//! crate it uses, against the window it will sit in: an `NSView` is made for the window's
6//! content view, a child `HWND` for the top-level window. Only the Renderer knows those, so
7//! it reports one `NativeViewParent` per window once a `NativeView` is in it, again when the
8//! handle or the backing scale changes, and with a zero handle when the window closes. This
9//! module keeps the last report of each window for the code that makes the views.
10//!
11//! The view is then handed over with `ui::NativeView` (or `Batch::attach_native_view`) and
12//! stays the application's: the Renderer places it and never destroys it.
13
14use crate::schema::{NativeHandle, NativeParentKind};
15use std::cell::{Cell, RefCell};
16use std::sync::Arc;
17
18/// What one window reported about the handle its platform views are made against.
19#[derive(Clone, Copy, Debug, PartialEq)]
20pub struct NativeParent {
21 /// The `Window` node the report is about, or 0 for the single window.
22 pub window: u32,
23 /// The handle a view is made against. Zero once the window has closed.
24 pub handle: NativeHandle,
25 pub kind: NativeParentKind,
26 /// The window's backing scale: how many pixels make one point.
27 pub scale: f32,
28}
29
30struct Subscriber {
31 id: u64,
32 notify: Arc<dyn Fn() + Send + Sync>,
33}
34
35thread_local! {
36 static PARENTS: RefCell<Vec<NativeParent>> = const { RefCell::new(Vec::new()) };
37 static SUBSCRIBERS: RefCell<Vec<Subscriber>> = const { RefCell::new(Vec::new()) };
38 static NEXT_SUBSCRIBER_ID: Cell<u64> = const { Cell::new(1) };
39}
40
41/// The last report for `window` (0 for the single window), or `None` before there is one.
42/// A window that closed reports a zero handle, which is returned as it came.
43pub fn native_parent(window: u32) -> Option<NativeParent> {
44 PARENTS.with_borrow(|parents| {
45 parents
46 .iter()
47 .find(|parent| parent.window == window)
48 .copied()
49 })
50}
51
52/// Every window's last report, in the order they first reported.
53pub fn native_parents() -> Vec<NativeParent> {
54 PARENTS.with_borrow(Clone::clone)
55}
56
57/// Records a report and wakes whoever subscribed. Returns whether anyone was woken; a
58/// report equal to the last one for its window changes nothing and wakes nobody.
59pub(crate) fn publish(parent: NativeParent) -> bool {
60 let changed = PARENTS.with_borrow_mut(|parents| {
61 match parents
62 .iter_mut()
63 .find(|known| known.window == parent.window)
64 {
65 Some(known) if *known == parent => false,
66 Some(known) => {
67 *known = parent;
68 true
69 }
70 None => {
71 parents.push(parent);
72 true
73 }
74 }
75 });
76 if !changed {
77 return false;
78 }
79 SUBSCRIBERS.with_borrow(|subscribers| {
80 for subscriber in subscribers {
81 (subscriber.notify)();
82 }
83 !subscribers.is_empty()
84 })
85}
86
87/// A registration for parent reports. Dropping it ends the registration.
88pub struct NativeParentSubscription {
89 id: u64,
90}
91
92/// Registers `notify` to be called each time a window reports a different parent.
93pub fn subscribe_native_parent(notify: Arc<dyn Fn() + Send + Sync>) -> NativeParentSubscription {
94 let id = NEXT_SUBSCRIBER_ID.with(|next| {
95 let id = next.get();
96 next.set(id + 1);
97 id
98 });
99 SUBSCRIBERS.with_borrow_mut(|subscribers| subscribers.push(Subscriber { id, notify }));
100 NativeParentSubscription { id }
101}
102
103impl Drop for NativeParentSubscription {
104 fn drop(&mut self) {
105 SUBSCRIBERS.with_borrow_mut(|subscribers| {
106 subscribers.retain(|subscriber| subscriber.id != self.id);
107 });
108 }
109}
110
111/// Forgets every report and subscription. Used when a Host is made and between tests.
112#[doc(hidden)]
113pub fn reset_native_parents() {
114 PARENTS.with_borrow_mut(Vec::clear);
115 SUBSCRIBERS.with_borrow_mut(Vec::clear);
116}