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>) {
203 let first = self.note_form_monitor_reported(form);
204 let old = match monitor {
205 Some(m) => self.form_monitors.insert(form, m),
206 None => self.form_monitors.remove(&form),
207 };
208 if let Some(m) = monitor.filter(|&m| old != Some(m) && !first && self.is_open(form)) {
209 self.fire(form, "on_monitor_change", &crate::EventArgs::Value(crate::PropValue::Int(m as i64)));
210 }
211 }
212}
213
214#[derive(Clone, Copy, Debug, Default, PartialEq)]
216pub struct CursorPosition {
217 pub x: i32,
219 pub y: i32,
220 pub monitor: Option<usize>,
222 pub dp_x: f32,
225 pub dp_y: f32,
226}
227
228pub const CURSOR_POSITION_RECORD: crate::prop::RecordInfo = crate::prop::RecordInfo {
230 name: "CursorPosition",
231 fields: &[
232 ("x", crate::PropKind::Int),
233 ("y", crate::PropKind::Int),
234 ("monitor", crate::PropKind::Int),
235 ("dp_x", crate::PropKind::Float),
236 ("dp_y", crate::PropKind::Float),
237 ],
238};
239
240impl CursorPosition {
241 pub fn to_json(&self) -> String {
243 format!(
244 "{{\"x\":{},\"y\":{},\"monitor\":{},\"dp_x\":{},\"dp_y\":{}}}",
245 self.x,
246 self.y,
247 self.monitor.map_or(-1, |m| m as i64),
248 self.dp_x,
249 self.dp_y
250 )
251 }
252}
253
254#[derive(Default)]
256struct DesktopState {
257 transparency: Option<bool>,
259 transparency_version: u64,
261 allow_capture: bool,
263 session: Option<(bool, bool)>,
265 session_version: u64,
267}
268
269type CaptureDone = Box<dyn FnOnce(&mut Ui, Result<crate::Image, String>)>;
271
272#[derive(Default)]
275struct CaptureWaits {
276 next: u64,
277 waits: Vec<(u64, CaptureDone)>,
278}
279
280impl Ui {
281 pub fn cursor_position(&self) -> Option<CursorPosition> {
285 let (x, y) = self.services().desktop.as_ref()?.cursor_position()?;
286 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);
287 let (dp_x, dp_y) = match monitor.map(|i| &self.monitors[i]) {
288 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),
289 _ => (x as f32, y as f32),
290 };
291 Some(CursorPosition { x, y, monitor, dp_x, dp_y })
292 }
293
294 pub fn form_at_screen(&self, x: i32, y: i32) -> Option<ComponentId> {
299 let form = self.services().desktop.as_ref()?.form_at(x, y)?;
300 self.is_open(form).then_some(form)
301 }
302
303 pub fn system_transparency(&self) -> bool {
308 self.extension_ref::<DesktopState>()
309 .and_then(|d| d.transparency)
310 .or_else(|| self.services().desktop.as_ref().and_then(|d| d.transparency()))
311 .unwrap_or(true)
312 }
313
314 pub fn set_system_transparency(&mut self, on: bool) {
316 let state = self.extension::<DesktopState>();
317 if state.transparency != Some(on) {
318 if state.transparency.is_some() {
319 state.transparency_version += 1;
320 }
321 state.transparency = Some(on);
322 }
323 }
324
325 pub fn system_transparency_version(&self) -> u64 {
328 self.extension_ref::<DesktopState>().map_or(0, |d| d.transparency_version)
329 }
330
331 pub fn session_locked(&self) -> bool {
335 self.session_state().0
336 }
337
338 pub fn session_connected(&self) -> bool {
343 self.session_state().1
344 }
345
346 fn session_state(&self) -> (bool, bool) {
348 self.extension_ref::<DesktopState>()
349 .and_then(|d| d.session)
350 .or_else(|| self.services().desktop.as_ref().and_then(|d| d.session_state()))
351 .unwrap_or((false, true))
352 }
353
354 pub fn set_system_session(&mut self, locked: bool, connected: bool) {
356 let state = self.extension::<DesktopState>();
357 if state.session != Some((locked, connected)) {
358 if state.session.is_some() {
359 state.session_version += 1;
360 }
361 state.session = Some((locked, connected));
362 }
363 }
364
365 pub fn system_session_version(&self) -> u64 {
367 self.extension_ref::<DesktopState>().map_or(0, |d| d.session_version)
368 }
369
370 pub fn set_allow_screen_capture(&mut self, on: bool) {
374 self.extension::<DesktopState>().allow_capture = on;
375 }
376
377 pub fn allow_screen_capture(&self) -> bool {
379 self.extension_ref::<DesktopState>().is_some_and(|d| d.allow_capture)
380 }
381
382 pub fn capture_monitor(&mut self, index: usize, max_width: u32, done: impl FnOnce(&mut Ui, Result<crate::Image, String>) + 'static) {
388 let done: CaptureDone = Box::new(done);
389 let refused = if !self.allow_screen_capture() {
390 Some(crate::messages::t("A captura da tela está desligada no projeto (allow_screen_capture)."))
391 } else if index >= self.monitors.len() {
392 Some(crate::messages::tf("O monitor {0} não existe.", &[&index]))
393 } else {
394 None
395 };
396 if let Some(message) = refused {
397 self.set_timeout(0, move |ui| done(ui, Err(message)));
398 return;
399 }
400 let m = &self.monitors[index];
401 let area = (m.x, m.y, m.width, m.height);
402 let waits = self.extension::<CaptureWaits>();
403 waits.next += 1;
404 let token = waits.next;
405 waits.waits.push((token, done));
406 let poster = self.poster();
407 let reply = Box::new(move |result: Result<crate::Image, String>| {
408 poster.post(move |ui| {
409 let waits = ui.extension::<CaptureWaits>();
410 if let Some(i) = waits.waits.iter().position(|(t, _)| *t == token) {
411 let (_, done) = waits.waits.remove(i);
412 done(ui, result);
413 }
414 });
415 });
416 match self.services_mut().desktop.as_mut() {
417 Some(d) => d.capture_monitor(area, max_width, reply),
418 None => reply(Err(crate::messages::t("A captura da tela não existe neste sistema."))),
419 }
420 }
421}