1use crate::component::ComponentId;
8use crate::forms::WindowPlacement;
9use crate::ui::Ui;
10
11pub const VIDEO_MODE_RECORD: crate::prop::RecordInfo = crate::prop::RecordInfo {
14 name: "VideoMode",
15 fields: &[("width", crate::PropKind::Int), ("height", crate::PropKind::Int), ("bit_depth", crate::PropKind::Int), ("refresh_hz", crate::PropKind::Float)],
16};
17
18pub const MONITOR_RECORD: crate::prop::RecordInfo = crate::prop::RecordInfo {
21 name: "Monitor",
22 fields: &[
23 ("index", crate::PropKind::Int),
24 ("name", crate::PropKind::String),
25 ("device", crate::PropKind::String),
26 ("device_path", crate::PropKind::String),
27 ("primary", crate::PropKind::Bool),
28 ("x", crate::PropKind::Int),
29 ("y", crate::PropKind::Int),
30 ("width", crate::PropKind::Int),
31 ("height", crate::PropKind::Int),
32 ("work_x", crate::PropKind::Int),
33 ("work_y", crate::PropKind::Int),
34 ("work_width", crate::PropKind::Int),
35 ("work_height", crate::PropKind::Int),
36 ("scale", crate::PropKind::Float),
37 ("refresh_hz", crate::PropKind::Float),
38 ("modes", crate::PropKind::RecordList(&VIDEO_MODE_RECORD)),
39 ],
40};
41
42#[derive(Clone, Debug, Default, PartialEq)]
43pub struct VideoMode {
44 pub width: u32,
45 pub height: u32,
46 pub bit_depth: u16,
47 pub refresh_hz: f64,
48}
49
50#[derive(Clone, Debug, Default, PartialEq)]
52pub struct Monitor {
53 pub name: String,
55 pub device: String,
58 pub device_path: String,
64 pub primary: bool,
65 pub x: i32,
66 pub y: i32,
67 pub width: u32,
68 pub height: u32,
69 pub work_x: i32,
71 pub work_y: i32,
72 pub work_width: u32,
73 pub work_height: u32,
74 pub scale: f64,
76 pub refresh_hz: f64,
78 pub modes: Vec<VideoMode>,
79}
80
81impl Monitor {
82 pub fn work_area(&self) -> (i32, i32, u32, u32) {
84 (self.work_x, self.work_y, self.work_width, self.work_height)
85 }
86
87 pub fn to_json(&self, index: usize) -> String {
89 let text = |s: &str| {
90 let mut out = String::from("\"");
91 for c in s.chars() {
92 match c {
93 '"' => out.push_str("\\\""),
94 '\\' => out.push_str("\\\\"),
95 c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
96 c => out.push(c),
97 }
98 }
99 out.push('"');
100 out
101 };
102 let modes: Vec<String> = self
103 .modes
104 .iter()
105 .map(|m| format!("{{\"width\":{},\"height\":{},\"bit_depth\":{},\"refresh_hz\":{}}}", m.width, m.height, m.bit_depth, m.refresh_hz))
106 .collect();
107 format!(
108 "{{\"index\":{index},\"name\":{},\"device\":{},\"device_path\":{},\"primary\":{},\"x\":{},\"y\":{},\"width\":{},\"height\":{},\"work_x\":{},\"work_y\":{},\"work_width\":{},\"work_height\":{},\"scale\":{},\"refresh_hz\":{},\"modes\":[{}]}}",
109 text(&self.name),
110 text(&self.device),
111 text(&self.device_path),
112 self.primary,
113 self.x,
114 self.y,
115 self.width,
116 self.height,
117 self.work_x,
118 self.work_y,
119 self.work_width,
120 self.work_height,
121 self.scale,
122 self.refresh_hz,
123 modes.join(",")
124 )
125 }
126}
127
128#[derive(Clone, Debug, Default, PartialEq)]
130pub struct SavedPlacement {
131 pub monitor: String,
133 pub placement: WindowPlacement,
134}
135
136pub fn restore_placement(saved: &SavedPlacement, monitors: &[Monitor]) -> Option<WindowPlacement> {
141 let p = saved.placement;
142 monitors.iter().find(|m| m.device == saved.monitor || (!m.device_path.is_empty() && m.device_path == saved.monitor))?;
143 let visible = monitors.iter().any(|m| {
144 let (x, y, w, h) = m.work_area();
145 let left = p.x.max(x);
146 let right = (p.x + p.width.max(120) as i32).min(x + w as i32);
147 let top = p.y.max(y);
148 let bottom = (p.y + 40).min(y + h as i32);
149 right - left >= 120 && bottom - top >= 40
150 });
151 visible.then_some(p)
152}
153
154impl Ui {
155 pub fn monitors(&self) -> &[Monitor] {
157 &self.monitors
158 }
159
160 pub fn monitors_version(&self) -> u64 {
163 self.monitors_version
164 }
165
166 pub fn set_monitors(&mut self, monitors: Vec<Monitor>) {
168 if monitors != self.monitors {
169 self.monitors = monitors;
170 self.monitors_version += 1;
171 }
172 }
173
174 pub fn form_monitor(&self, form: ComponentId) -> Option<usize> {
176 self.form_monitors.get(&self.root_of(form)).copied()
177 }
178
179 pub fn is_minimized(&self, form: ComponentId) -> bool {
182 self.minimized_forms.contains(&self.root_of(form))
183 }
184
185 pub fn set_form_minimized(&mut self, form: ComponentId, minimized: bool) {
187 if minimized {
188 self.minimized_forms.insert(form);
189 } else {
190 self.minimized_forms.remove(&form);
191 }
192 }
193
194 pub fn set_form_monitor(&mut self, form: ComponentId, monitor: Option<usize>) {
196 match monitor {
197 Some(m) => {
198 self.form_monitors.insert(form, m);
199 }
200 None => {
201 self.form_monitors.remove(&form);
202 }
203 }
204 }
205}
206
207#[derive(Clone, Copy, Debug, Default, PartialEq)]
209pub struct CursorPosition {
210 pub x: i32,
212 pub y: i32,
213 pub monitor: Option<usize>,
215 pub dp_x: f32,
218 pub dp_y: f32,
219}
220
221pub const CURSOR_POSITION_RECORD: crate::prop::RecordInfo = crate::prop::RecordInfo {
223 name: "CursorPosition",
224 fields: &[
225 ("x", crate::PropKind::Int),
226 ("y", crate::PropKind::Int),
227 ("monitor", crate::PropKind::Int),
228 ("dp_x", crate::PropKind::Float),
229 ("dp_y", crate::PropKind::Float),
230 ],
231};
232
233impl CursorPosition {
234 pub fn to_json(&self) -> String {
236 format!(
237 "{{\"x\":{},\"y\":{},\"monitor\":{},\"dp_x\":{},\"dp_y\":{}}}",
238 self.x,
239 self.y,
240 self.monitor.map_or(-1, |m| m as i64),
241 self.dp_x,
242 self.dp_y
243 )
244 }
245}
246
247#[derive(Default)]
249struct DesktopState {
250 transparency: Option<bool>,
252 transparency_version: u64,
254 allow_capture: bool,
256 session: Option<(bool, bool)>,
258 session_version: u64,
260}
261
262type CaptureDone = Box<dyn FnOnce(&mut Ui, Result<crate::Image, String>)>;
264
265#[derive(Default)]
268struct CaptureWaits {
269 next: u64,
270 waits: Vec<(u64, CaptureDone)>,
271}
272
273impl Ui {
274 pub fn cursor_position(&self) -> Option<CursorPosition> {
278 let (x, y) = self.services().desktop.as_ref()?.cursor_position()?;
279 let monitor = self.monitors.iter().position(|m| x >= m.x && y >= m.y && x < m.x + m.width as i32 && y < m.y + m.height as i32);
280 let (dp_x, dp_y) = match monitor.map(|i| &self.monitors[i]) {
281 Some(m) if m.scale > 0.0 => (((x - m.x) as f64 / m.scale) as f32, ((y - m.y) as f64 / m.scale) as f32),
282 _ => (x as f32, y as f32),
283 };
284 Some(CursorPosition { x, y, monitor, dp_x, dp_y })
285 }
286
287 pub fn form_at_screen(&self, x: i32, y: i32) -> Option<ComponentId> {
292 let form = self.services().desktop.as_ref()?.form_at(x, y)?;
293 self.is_open(form).then_some(form)
294 }
295
296 pub fn system_transparency(&self) -> bool {
301 self.extension_ref::<DesktopState>()
302 .and_then(|d| d.transparency)
303 .or_else(|| self.services().desktop.as_ref().and_then(|d| d.transparency()))
304 .unwrap_or(true)
305 }
306
307 pub fn set_system_transparency(&mut self, on: bool) {
309 let state = self.extension::<DesktopState>();
310 if state.transparency != Some(on) {
311 if state.transparency.is_some() {
312 state.transparency_version += 1;
313 }
314 state.transparency = Some(on);
315 }
316 }
317
318 pub fn system_transparency_version(&self) -> u64 {
321 self.extension_ref::<DesktopState>().map_or(0, |d| d.transparency_version)
322 }
323
324 pub fn session_locked(&self) -> bool {
328 self.session_state().0
329 }
330
331 pub fn session_connected(&self) -> bool {
336 self.session_state().1
337 }
338
339 fn session_state(&self) -> (bool, bool) {
341 self.extension_ref::<DesktopState>()
342 .and_then(|d| d.session)
343 .or_else(|| self.services().desktop.as_ref().and_then(|d| d.session_state()))
344 .unwrap_or((false, true))
345 }
346
347 pub fn set_system_session(&mut self, locked: bool, connected: bool) {
349 let state = self.extension::<DesktopState>();
350 if state.session != Some((locked, connected)) {
351 if state.session.is_some() {
352 state.session_version += 1;
353 }
354 state.session = Some((locked, connected));
355 }
356 }
357
358 pub fn system_session_version(&self) -> u64 {
360 self.extension_ref::<DesktopState>().map_or(0, |d| d.session_version)
361 }
362
363 pub fn set_allow_screen_capture(&mut self, on: bool) {
367 self.extension::<DesktopState>().allow_capture = on;
368 }
369
370 pub fn allow_screen_capture(&self) -> bool {
372 self.extension_ref::<DesktopState>().is_some_and(|d| d.allow_capture)
373 }
374
375 pub fn capture_monitor(&mut self, index: usize, max_width: u32, done: impl FnOnce(&mut Ui, Result<crate::Image, String>) + 'static) {
381 let done: CaptureDone = Box::new(done);
382 let refused = if !self.allow_screen_capture() {
383 Some(crate::messages::t("A captura da tela está desligada no projeto (allow_screen_capture)."))
384 } else if index >= self.monitors.len() {
385 Some(crate::messages::tf("O monitor {0} não existe.", &[&index]))
386 } else {
387 None
388 };
389 if let Some(message) = refused {
390 self.set_timeout(0, move |ui| done(ui, Err(message)));
391 return;
392 }
393 let m = &self.monitors[index];
394 let area = (m.x, m.y, m.width, m.height);
395 let waits = self.extension::<CaptureWaits>();
396 waits.next += 1;
397 let token = waits.next;
398 waits.waits.push((token, done));
399 let poster = self.poster();
400 let reply = Box::new(move |result: Result<crate::Image, String>| {
401 poster.post(move |ui| {
402 let waits = ui.extension::<CaptureWaits>();
403 if let Some(i) = waits.waits.iter().position(|(t, _)| *t == token) {
404 let (_, done) = waits.waits.remove(i);
405 done(ui, result);
406 }
407 });
408 });
409 match self.services_mut().desktop.as_mut() {
410 Some(d) => d.capture_monitor(area, max_width, reply),
411 None => reply(Err(crate::messages::t("A captura da tela não existe neste sistema."))),
412 }
413 }
414}