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 ("primary", crate::PropKind::Bool),
27 ("x", crate::PropKind::Int),
28 ("y", crate::PropKind::Int),
29 ("width", crate::PropKind::Int),
30 ("height", crate::PropKind::Int),
31 ("work_x", crate::PropKind::Int),
32 ("work_y", crate::PropKind::Int),
33 ("work_width", crate::PropKind::Int),
34 ("work_height", crate::PropKind::Int),
35 ("scale", crate::PropKind::Float),
36 ("refresh_hz", crate::PropKind::Float),
37 ("modes", crate::PropKind::RecordList(&VIDEO_MODE_RECORD)),
38 ],
39};
40
41#[derive(Clone, Debug, Default, PartialEq)]
42pub struct VideoMode {
43 pub width: u32,
44 pub height: u32,
45 pub bit_depth: u16,
46 pub refresh_hz: f64,
47}
48
49#[derive(Clone, Debug, Default, PartialEq)]
51pub struct Monitor {
52 pub name: String,
54 pub device: String,
57 pub primary: bool,
58 pub x: i32,
59 pub y: i32,
60 pub width: u32,
61 pub height: u32,
62 pub work_x: i32,
64 pub work_y: i32,
65 pub work_width: u32,
66 pub work_height: u32,
67 pub scale: f64,
69 pub refresh_hz: f64,
71 pub modes: Vec<VideoMode>,
72}
73
74impl Monitor {
75 pub fn work_area(&self) -> (i32, i32, u32, u32) {
77 (self.work_x, self.work_y, self.work_width, self.work_height)
78 }
79
80 pub fn to_json(&self, index: usize) -> String {
82 let text = |s: &str| {
83 let mut out = String::from("\"");
84 for c in s.chars() {
85 match c {
86 '"' => out.push_str("\\\""),
87 '\\' => out.push_str("\\\\"),
88 c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
89 c => out.push(c),
90 }
91 }
92 out.push('"');
93 out
94 };
95 let modes: Vec<String> = self
96 .modes
97 .iter()
98 .map(|m| format!("{{\"width\":{},\"height\":{},\"bit_depth\":{},\"refresh_hz\":{}}}", m.width, m.height, m.bit_depth, m.refresh_hz))
99 .collect();
100 format!(
101 "{{\"index\":{index},\"name\":{},\"device\":{},\"primary\":{},\"x\":{},\"y\":{},\"width\":{},\"height\":{},\"work_x\":{},\"work_y\":{},\"work_width\":{},\"work_height\":{},\"scale\":{},\"refresh_hz\":{},\"modes\":[{}]}}",
102 text(&self.name),
103 text(&self.device),
104 self.primary,
105 self.x,
106 self.y,
107 self.width,
108 self.height,
109 self.work_x,
110 self.work_y,
111 self.work_width,
112 self.work_height,
113 self.scale,
114 self.refresh_hz,
115 modes.join(",")
116 )
117 }
118}
119
120#[derive(Clone, Debug, Default, PartialEq)]
122pub struct SavedPlacement {
123 pub monitor: String,
125 pub placement: WindowPlacement,
126}
127
128pub fn restore_placement(saved: &SavedPlacement, monitors: &[Monitor]) -> Option<WindowPlacement> {
133 let p = saved.placement;
134 monitors.iter().find(|m| m.device == saved.monitor)?;
135 let visible = monitors.iter().any(|m| {
136 let (x, y, w, h) = m.work_area();
137 let left = p.x.max(x);
138 let right = (p.x + p.width.max(120) as i32).min(x + w as i32);
139 let top = p.y.max(y);
140 let bottom = (p.y + 40).min(y + h as i32);
141 right - left >= 120 && bottom - top >= 40
142 });
143 visible.then_some(p)
144}
145
146impl Ui {
147 pub fn monitors(&self) -> &[Monitor] {
149 &self.monitors
150 }
151
152 pub fn monitors_version(&self) -> u64 {
155 self.monitors_version
156 }
157
158 pub fn set_monitors(&mut self, monitors: Vec<Monitor>) {
160 if monitors != self.monitors {
161 self.monitors = monitors;
162 self.monitors_version += 1;
163 }
164 }
165
166 pub fn form_monitor(&self, form: ComponentId) -> Option<usize> {
168 self.form_monitors.get(&self.root_of(form)).copied()
169 }
170
171 pub fn is_minimized(&self, form: ComponentId) -> bool {
174 self.minimized_forms.contains(&self.root_of(form))
175 }
176
177 pub fn set_form_minimized(&mut self, form: ComponentId, minimized: bool) {
179 if minimized {
180 self.minimized_forms.insert(form);
181 } else {
182 self.minimized_forms.remove(&form);
183 }
184 }
185
186 pub fn set_form_monitor(&mut self, form: ComponentId, monitor: Option<usize>) {
188 match monitor {
189 Some(m) => {
190 self.form_monitors.insert(form, m);
191 }
192 None => {
193 self.form_monitors.remove(&form);
194 }
195 }
196 }
197}