1use std::rc::Rc;
6use std::sync::atomic::{AtomicU64, Ordering};
7
8use crate::geometry::{Affine, Size};
9use crate::prop::{IntoProp, Prop};
10use crate::style::Style;
11use crate::tree::{Change, NodeId, NodeKind, Tree, borrow, replace};
12use crate::widgets::{Common, Element, ElementKind, Fit, Widget, visibility_methods};
13use crate::window::Window;
14
15static NEXT_VIEW: AtomicU64 = AtomicU64::new(0);
18
19#[non_exhaustive]
21#[derive(Clone, Debug, PartialEq, Eq, Hash)]
22pub enum CaptureSource {
23 Output(String),
25 Toplevel(u64),
27}
28
29pub(crate) struct ScreencopyProps {
30 pub(crate) source: Prop<CaptureSource>,
31 pub(crate) live: Option<Prop<bool>>,
32 pub(crate) paint_cursors: Option<Prop<bool>>,
33 pub(crate) fit: Option<Prop<Fit>>,
34}
35
36pub struct ScreencopyView {
55 props: ScreencopyProps,
56 common: Common,
57}
58
59impl ScreencopyView {
60 pub fn new(source: impl IntoProp<CaptureSource>) -> ScreencopyView {
63 ScreencopyView {
64 props: ScreencopyProps {
65 source: source.into_prop(),
66 live: None,
67 paint_cursors: None,
68 fit: None,
69 },
70 common: Common::default(),
71 }
72 }
73
74 pub fn live(mut self, live: impl IntoProp<bool>) -> ScreencopyView {
77 self.props.live = Some(live.into_prop());
78 self
79 }
80
81 pub fn paint_cursors(mut self, paint: impl IntoProp<bool>) -> ScreencopyView {
83 self.props.paint_cursors = Some(paint.into_prop());
84 self
85 }
86
87 pub fn fit(mut self, fit: impl IntoProp<Fit>) -> ScreencopyView {
89 self.props.fit = Some(fit.into_prop());
90 self
91 }
92
93 pub fn style(mut self, style: impl IntoProp<Style>) -> ScreencopyView {
95 self.common.style = Some(style.into_prop());
96 self
97 }
98
99 visibility_methods!();
100}
101
102impl Widget for ScreencopyView {
103 fn into_element(self: Box<Self>) -> Element {
104 Element {
105 name: "ScreencopyView",
106 common: self.common,
107 kind: ElementKind::Capture(self.props),
108 children: Vec::new(),
109 }
110 }
111}
112
113#[derive(Clone, Debug, PartialEq, Eq)]
115pub struct CaptureFrame {
116 pub width: u32,
118 pub height: u32,
120 pub rgba: Rc<[u8]>,
123}
124
125#[derive(Clone, Debug, PartialEq)]
127pub struct CaptureRequest {
128 pub id: u64,
130 pub source: CaptureSource,
132 pub live: bool,
134 pub paint_cursors: bool,
136 pub size: Size,
140}
141
142pub(crate) struct CaptureNode {
143 pub(crate) id: u64,
144 pub(crate) source: Option<CaptureSource>,
146 pub(crate) live: bool,
147 pub(crate) paint_cursors: bool,
148 pub(crate) fit: Fit,
149 pub(crate) frame: Option<CaptureFrame>,
150}
151
152impl CaptureNode {
153 pub(crate) fn new() -> CaptureNode {
154 CaptureNode {
155 id: NEXT_VIEW.fetch_add(1, Ordering::Relaxed),
156 source: None,
157 live: false,
158 paint_cursors: false,
159 fit: Fit::default(),
160 frame: None,
161 }
162 }
163}
164
165impl Tree {
166 pub(crate) fn bind_capture(&mut self, id: NodeId, props: ScreencopyProps) {
169 self.captures.push(id);
170 self.bind(id, props.source, "source", |tree, id, value| {
171 tree.update_capture(id, |capture| {
172 if capture.source.as_ref() == Some(&value) {
173 return Change::None;
174 }
175 capture.source = Some(value);
176 match capture.frame.take() {
178 Some(_) => Change::Paint,
179 None => Change::None,
180 }
181 });
182 });
183 self.bind_opt(id, props.live, "live", |tree, id, value| {
184 tree.update_capture(id, |capture| {
185 capture.live = value;
186 Change::None
187 });
188 });
189 self.bind_opt(
190 id,
191 props.paint_cursors,
192 "paint_cursors",
193 |tree, id, value| {
194 tree.update_capture(id, |capture| {
195 capture.paint_cursors = value;
196 Change::None
197 });
198 },
199 );
200 self.bind_opt(id, props.fit, "fit", |tree, id, value| {
201 tree.update_capture(id, |capture| match replace(&mut capture.fit, value) {
202 Change::None => Change::None,
203 _ => Change::Paint,
204 });
205 });
206 }
207
208 fn in_sight(&self, id: NodeId) -> bool {
213 let Some(node) = self.node(id) else {
214 return false;
215 };
216 let mut seen = node.window_painted;
217 let mut at = id;
218 while !seen.is_empty()
219 && let Some(parent) = self.taffy.parent(at)
220 {
221 if let Some(above) = self.node(parent)
222 && (above.clips || parent == self.root)
223 {
224 seen = seen.intersect(above.window_painted);
225 }
226 at = parent;
227 }
228 !seen.is_empty()
229 }
230
231 fn zoom(&self, id: NodeId) -> f32 {
235 let mut linear: Option<Affine> = None;
236 let mut at = Some(id);
237 while let Some(node) = at {
238 if let Some(transform) = self.node(node).and_then(|node| node.transform) {
239 linear = Some(match linear {
240 Some(inner) => transform.after(&inner),
241 None => transform,
242 });
243 }
244 at = self.taffy.parent(node);
245 }
246 linear.map_or(1.0, |linear| linear.layer_zoom())
247 }
248
249 fn update_capture(&mut self, id: NodeId, f: impl FnOnce(&mut CaptureNode) -> Change) {
250 self.update(id, |node| match &mut node.kind {
251 NodeKind::Capture(capture) => f(capture),
252 _ => Change::None,
253 });
254 }
255}
256
257impl Window {
258 pub fn captures(&self) -> Vec<CaptureRequest> {
264 let tree = self.tree.borrow();
265 tree
266 .captures
267 .iter()
268 .filter_map(|&id| {
269 let node = tree.node(id)?;
270 let NodeKind::Capture(capture) = &node.kind else {
271 return None;
272 };
273 let content = node.content;
274 if node.rect.is_none() || content.is_empty() || capture.live && !tree.in_sight(id) {
275 return None;
276 }
277 let zoom = tree.zoom(id);
278 Some(CaptureRequest {
279 id: capture.id,
280 source: capture.source.clone()?,
281 live: capture.live,
282 paint_cursors: capture.paint_cursors,
283 size: Size::new(content.width * zoom, content.height * zoom),
284 })
285 })
286 .collect()
287 }
288
289 pub fn show_capture(&self, id: u64, frame: CaptureFrame) -> bool {
294 let mut tree = borrow(&self.tree);
295 let found = tree.captures.iter().copied().find(|&node| {
296 matches!(tree.node(node).map(|node| &node.kind),
297 Some(NodeKind::Capture(capture)) if capture.id == id)
298 });
299 let Some(node) = found else {
300 return false;
301 };
302 let mut shown = false;
303 tree.update(node, |node| match &mut node.kind {
304 NodeKind::Capture(capture) if capture.frame.as_ref() != Some(&frame) => {
305 capture.frame = Some(frame);
306 shown = true;
307 Change::Paint
308 }
309 _ => Change::None,
310 });
311 shown
312 }
313}