1use std::rc::Rc;
6use std::sync::atomic::{AtomicU64, Ordering};
7
8use crate::geometry::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,
139}
140
141pub(crate) struct CaptureNode {
142 pub(crate) id: u64,
143 pub(crate) source: Option<CaptureSource>,
145 pub(crate) live: bool,
146 pub(crate) paint_cursors: bool,
147 pub(crate) fit: Fit,
148 pub(crate) frame: Option<CaptureFrame>,
149}
150
151impl CaptureNode {
152 pub(crate) fn new() -> CaptureNode {
153 CaptureNode {
154 id: NEXT_VIEW.fetch_add(1, Ordering::Relaxed),
155 source: None,
156 live: false,
157 paint_cursors: false,
158 fit: Fit::default(),
159 frame: None,
160 }
161 }
162}
163
164impl Tree {
165 pub(crate) fn bind_capture(&mut self, id: NodeId, props: ScreencopyProps) {
168 self.captures.push(id);
169 self.bind(id, props.source, "source", |tree, id, value| {
170 tree.update_capture(id, |capture| {
171 if capture.source.as_ref() == Some(&value) {
172 return Change::None;
173 }
174 capture.source = Some(value);
175 match capture.frame.take() {
177 Some(_) => Change::Paint,
178 None => Change::None,
179 }
180 });
181 });
182 self.bind_opt(id, props.live, "live", |tree, id, value| {
183 tree.update_capture(id, |capture| {
184 capture.live = value;
185 Change::None
186 });
187 });
188 self.bind_opt(
189 id,
190 props.paint_cursors,
191 "paint_cursors",
192 |tree, id, value| {
193 tree.update_capture(id, |capture| {
194 capture.paint_cursors = value;
195 Change::None
196 });
197 },
198 );
199 self.bind_opt(id, props.fit, "fit", |tree, id, value| {
200 tree.update_capture(id, |capture| match replace(&mut capture.fit, value) {
201 Change::None => Change::None,
202 _ => Change::Paint,
203 });
204 });
205 }
206
207 fn update_capture(&mut self, id: NodeId, f: impl FnOnce(&mut CaptureNode) -> Change) {
208 self.update(id, |node| match &mut node.kind {
209 NodeKind::Capture(capture) => f(capture),
210 _ => Change::None,
211 });
212 }
213}
214
215impl Window {
216 pub fn captures(&self) -> Vec<CaptureRequest> {
219 let tree = self.tree.borrow();
220 tree
221 .captures
222 .iter()
223 .filter_map(|&id| {
224 let node = tree.node(id)?;
225 let NodeKind::Capture(capture) = &node.kind else {
226 return None;
227 };
228 let content = node.content;
229 if node.rect.is_none() || content.is_empty() {
230 return None;
231 }
232 Some(CaptureRequest {
233 id: capture.id,
234 source: capture.source.clone()?,
235 live: capture.live,
236 paint_cursors: capture.paint_cursors,
237 size: Size::new(content.width, content.height),
238 })
239 })
240 .collect()
241 }
242
243 pub fn show_capture(&self, id: u64, frame: CaptureFrame) -> bool {
248 let mut tree = borrow(&self.tree);
249 let found = tree.captures.iter().copied().find(|&node| {
250 matches!(tree.node(node).map(|node| &node.kind),
251 Some(NodeKind::Capture(capture)) if capture.id == id)
252 });
253 let Some(node) = found else {
254 return false;
255 };
256 let mut shown = false;
257 tree.update(node, |node| match &mut node.kind {
258 NodeKind::Capture(capture) if capture.frame.as_ref() != Some(&frame) => {
259 capture.frame = Some(frame);
260 shown = true;
261 Change::Paint
262 }
263 _ => Change::None,
264 });
265 shown
266 }
267}