@@ -0,0 +1,434 @@ |
| 1 | +//! Display settings panel for selected monitor. |
| 2 | + |
| 3 | +use std::collections::HashSet; |
| 4 | + |
| 5 | +use gartk_core::{InputEvent, Rect, Theme}; |
| 6 | +use gartk_render::{Renderer, TextAlign, TextStyle}; |
| 7 | + |
| 8 | +use crate::randr::{ModeInfo, OutputInfo}; |
| 9 | +use crate::ui::widgets::{Dropdown, Toggle}; |
| 10 | + |
| 11 | +/// Configuration values from the display panel. |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub struct DisplayPanelConfig { |
| 14 | + pub width: u32, |
| 15 | + pub height: u32, |
| 16 | + pub refresh: f64, |
| 17 | + pub rotation: u32, |
| 18 | + pub scale: f64, |
| 19 | + pub enabled: bool, |
| 20 | +} |
| 21 | + |
| 22 | +/// Result of handling a panel event. |
| 23 | +#[derive(Debug)] |
| 24 | +pub enum DisplayPanelResult { |
| 25 | + /// No action needed. |
| 26 | + None, |
| 27 | + /// Redraw the UI. |
| 28 | + Redraw, |
| 29 | + /// Configuration changed. |
| 30 | + ConfigChanged(DisplayPanelConfig), |
| 31 | +} |
| 32 | + |
| 33 | +/// Display settings panel for the selected monitor. |
| 34 | +pub struct DisplayPanel { |
| 35 | + rect: Rect, |
| 36 | + // Widgets |
| 37 | + resolution_dropdown: Dropdown, |
| 38 | + refresh_dropdown: Dropdown, |
| 39 | + rotation_dropdown: Dropdown, |
| 40 | + scale_dropdown: Dropdown, |
| 41 | + enabled_toggle: Toggle, |
| 42 | + // State |
| 43 | + selected_output: Option<String>, |
| 44 | + available_modes: Vec<ModeInfo>, |
| 45 | + // Current values |
| 46 | + current_width: u32, |
| 47 | + current_height: u32, |
| 48 | + current_refresh: f64, |
| 49 | + current_rotation: u32, |
| 50 | + current_scale: f64, |
| 51 | + current_enabled: bool, |
| 52 | +} |
| 53 | + |
| 54 | +impl DisplayPanel { |
| 55 | + /// Create a new display panel. |
| 56 | + pub fn new(rect: Rect) -> Self { |
| 57 | + // Calculate widget positions based on panel rect |
| 58 | + let row1_y = rect.y + 40; |
| 59 | + let row2_y = rect.y + 80; |
| 60 | + let col1_x = rect.x + 100; |
| 61 | + let col2_x = rect.x + (rect.width as i32 / 2) + 80; |
| 62 | + let dropdown_width = 120; |
| 63 | + let dropdown_height = 28; |
| 64 | + |
| 65 | + let resolution_dropdown = Dropdown::new(col1_x, row1_y, dropdown_width, dropdown_height); |
| 66 | + let refresh_dropdown = Dropdown::new(col2_x, row1_y, 80, dropdown_height); |
| 67 | + let rotation_dropdown = Dropdown::new(col1_x, row2_y, 80, dropdown_height); |
| 68 | + let scale_dropdown = Dropdown::new(col2_x, row2_y, 80, dropdown_height); |
| 69 | + let enabled_toggle = Toggle::new( |
| 70 | + rect.x + rect.width as i32 - 150, |
| 71 | + rect.y + 8, |
| 72 | + 140, |
| 73 | + 28, |
| 74 | + "Enabled", |
| 75 | + ); |
| 76 | + |
| 77 | + Self { |
| 78 | + rect, |
| 79 | + resolution_dropdown, |
| 80 | + refresh_dropdown, |
| 81 | + rotation_dropdown, |
| 82 | + scale_dropdown, |
| 83 | + enabled_toggle, |
| 84 | + selected_output: None, |
| 85 | + available_modes: Vec::new(), |
| 86 | + current_width: 0, |
| 87 | + current_height: 0, |
| 88 | + current_refresh: 60.0, |
| 89 | + current_rotation: 0, |
| 90 | + current_scale: 1.0, |
| 91 | + current_enabled: true, |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// Update panel to show settings for the selected monitor. |
| 96 | + pub fn set_selected_monitor( |
| 97 | + &mut self, |
| 98 | + output: Option<&OutputInfo>, |
| 99 | + width: u32, |
| 100 | + height: u32, |
| 101 | + refresh: f64, |
| 102 | + rotation: u32, |
| 103 | + scale: f64, |
| 104 | + enabled: bool, |
| 105 | + ) { |
| 106 | + if let Some(output) = output { |
| 107 | + self.selected_output = Some(output.name.clone()); |
| 108 | + self.available_modes = output.modes.clone(); |
| 109 | + |
| 110 | + // Populate resolution dropdown with unique resolutions |
| 111 | + let resolutions: Vec<String> = output |
| 112 | + .modes |
| 113 | + .iter() |
| 114 | + .map(|m| format!("{}x{}", m.width, m.height)) |
| 115 | + .collect::<HashSet<_>>() |
| 116 | + .into_iter() |
| 117 | + .collect(); |
| 118 | + let mut sorted_resolutions: Vec<String> = resolutions; |
| 119 | + sorted_resolutions.sort_by(|a, b| { |
| 120 | + // Sort by resolution (width * height) descending |
| 121 | + let parse_res = |s: &str| -> u64 { |
| 122 | + let parts: Vec<&str> = s.split('x').collect(); |
| 123 | + if parts.len() == 2 { |
| 124 | + parts[0].parse::<u64>().unwrap_or(0) |
| 125 | + * parts[1].parse::<u64>().unwrap_or(0) |
| 126 | + } else { |
| 127 | + 0 |
| 128 | + } |
| 129 | + }; |
| 130 | + parse_res(b).cmp(&parse_res(a)) |
| 131 | + }); |
| 132 | + self.resolution_dropdown.set_items(sorted_resolutions); |
| 133 | + |
| 134 | + // Set current resolution |
| 135 | + let current_res = format!("{}x{}", width, height); |
| 136 | + self.resolution_dropdown.set_selected_by_name(¤t_res); |
| 137 | + self.current_width = width; |
| 138 | + self.current_height = height; |
| 139 | + |
| 140 | + // Populate refresh rates for current resolution |
| 141 | + self.update_refresh_dropdown(width, height); |
| 142 | + |
| 143 | + // Set current refresh |
| 144 | + let current_refresh_str = format!("{:.0}Hz", refresh); |
| 145 | + self.refresh_dropdown.set_selected_by_name(¤t_refresh_str); |
| 146 | + self.current_refresh = refresh; |
| 147 | + |
| 148 | + // Rotation options |
| 149 | + self.rotation_dropdown |
| 150 | + .set_items(vec!["0".to_string(), "90".to_string(), "180".to_string(), "270".to_string()]); |
| 151 | + self.rotation_dropdown |
| 152 | + .set_selected_by_name(&rotation.to_string()); |
| 153 | + self.current_rotation = rotation; |
| 154 | + |
| 155 | + // Scale options |
| 156 | + self.scale_dropdown.set_items(vec![ |
| 157 | + "1x".to_string(), |
| 158 | + "1.25x".to_string(), |
| 159 | + "1.5x".to_string(), |
| 160 | + "1.75x".to_string(), |
| 161 | + "2x".to_string(), |
| 162 | + ]); |
| 163 | + let scale_str = format!("{}x", scale); |
| 164 | + self.scale_dropdown.set_selected_by_name(&scale_str); |
| 165 | + self.current_scale = scale; |
| 166 | + |
| 167 | + // Enable toggle |
| 168 | + self.enabled_toggle.set_value(enabled); |
| 169 | + self.current_enabled = enabled; |
| 170 | + } else { |
| 171 | + self.selected_output = None; |
| 172 | + self.available_modes.clear(); |
| 173 | + self.resolution_dropdown.set_items(Vec::new()); |
| 174 | + self.refresh_dropdown.set_items(Vec::new()); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /// Update refresh rate dropdown for the given resolution. |
| 179 | + fn update_refresh_dropdown(&mut self, width: u32, height: u32) { |
| 180 | + let refreshes: Vec<String> = self |
| 181 | + .available_modes |
| 182 | + .iter() |
| 183 | + .filter(|m| m.width as u32 == width && m.height as u32 == height) |
| 184 | + .map(|m| format!("{:.0}Hz", m.refresh)) |
| 185 | + .collect::<HashSet<_>>() |
| 186 | + .into_iter() |
| 187 | + .collect(); |
| 188 | + let mut sorted_refreshes: Vec<String> = refreshes; |
| 189 | + sorted_refreshes.sort_by(|a, b| { |
| 190 | + let parse_hz = |s: &str| -> f64 { |
| 191 | + s.trim_end_matches("Hz").parse::<f64>().unwrap_or(0.0) |
| 192 | + }; |
| 193 | + parse_hz(b).partial_cmp(&parse_hz(a)).unwrap_or(std::cmp::Ordering::Equal) |
| 194 | + }); |
| 195 | + self.refresh_dropdown.set_items(sorted_refreshes); |
| 196 | + } |
| 197 | + |
| 198 | + /// Get the current configuration. |
| 199 | + pub fn get_config(&self) -> Option<DisplayPanelConfig> { |
| 200 | + if self.selected_output.is_some() { |
| 201 | + Some(DisplayPanelConfig { |
| 202 | + width: self.current_width, |
| 203 | + height: self.current_height, |
| 204 | + refresh: self.current_refresh, |
| 205 | + rotation: self.current_rotation, |
| 206 | + scale: self.current_scale, |
| 207 | + enabled: self.current_enabled, |
| 208 | + }) |
| 209 | + } else { |
| 210 | + None |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + /// Get the selected output name. |
| 215 | + pub fn selected_output(&self) -> Option<&str> { |
| 216 | + self.selected_output.as_deref() |
| 217 | + } |
| 218 | + |
| 219 | + /// Set the panel rect. |
| 220 | + pub fn set_rect(&mut self, rect: Rect) { |
| 221 | + self.rect = rect; |
| 222 | + |
| 223 | + // Recalculate widget positions |
| 224 | + let row1_y = rect.y + 40; |
| 225 | + let row2_y = rect.y + 80; |
| 226 | + let col1_x = rect.x + 100; |
| 227 | + let col2_x = rect.x + (rect.width as i32 / 2) + 80; |
| 228 | + |
| 229 | + self.resolution_dropdown.set_position(col1_x, row1_y); |
| 230 | + self.refresh_dropdown.set_position(col2_x, row1_y); |
| 231 | + self.rotation_dropdown.set_position(col1_x, row2_y); |
| 232 | + self.scale_dropdown.set_position(col2_x, row2_y); |
| 233 | + self.enabled_toggle |
| 234 | + .set_position(rect.x + rect.width as i32 - 150, rect.y + 8); |
| 235 | + } |
| 236 | + |
| 237 | + /// Handle an input event. |
| 238 | + pub fn handle_event(&mut self, event: &InputEvent) -> DisplayPanelResult { |
| 239 | + // Only handle events if we have a selected output |
| 240 | + if self.selected_output.is_none() { |
| 241 | + return DisplayPanelResult::None; |
| 242 | + } |
| 243 | + |
| 244 | + let mut config_changed = false; |
| 245 | + |
| 246 | + // Handle enable toggle |
| 247 | + if self.enabled_toggle.handle_event(event) { |
| 248 | + self.current_enabled = self.enabled_toggle.value(); |
| 249 | + config_changed = true; |
| 250 | + } |
| 251 | + |
| 252 | + // Handle resolution dropdown |
| 253 | + if let Some(action) = self.resolution_dropdown.handle_event(event) { |
| 254 | + if let crate::ui::widgets::DropdownAction::Select(_) = action { |
| 255 | + if let Some(res) = self.resolution_dropdown.selected_item() { |
| 256 | + // Parse resolution |
| 257 | + let parts: Vec<&str> = res.split('x').collect(); |
| 258 | + if parts.len() == 2 { |
| 259 | + if let (Ok(w), Ok(h)) = (parts[0].parse::<u32>(), parts[1].parse::<u32>()) { |
| 260 | + self.current_width = w; |
| 261 | + self.current_height = h; |
| 262 | + // Update refresh rates for new resolution |
| 263 | + self.update_refresh_dropdown(w, h); |
| 264 | + // Select first available refresh rate |
| 265 | + if let Some(first_refresh) = self.refresh_dropdown.selected_item() { |
| 266 | + let hz_str = first_refresh.trim_end_matches("Hz"); |
| 267 | + if let Ok(hz) = hz_str.parse::<f64>() { |
| 268 | + self.current_refresh = hz; |
| 269 | + } |
| 270 | + } |
| 271 | + config_changed = true; |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + return if config_changed { |
| 277 | + DisplayPanelResult::ConfigChanged(self.get_config().unwrap()) |
| 278 | + } else { |
| 279 | + DisplayPanelResult::Redraw |
| 280 | + }; |
| 281 | + } |
| 282 | + |
| 283 | + // Handle refresh dropdown |
| 284 | + if let Some(action) = self.refresh_dropdown.handle_event(event) { |
| 285 | + if let crate::ui::widgets::DropdownAction::Select(_) = action { |
| 286 | + if let Some(refresh) = self.refresh_dropdown.selected_item() { |
| 287 | + let hz_str = refresh.trim_end_matches("Hz"); |
| 288 | + if let Ok(hz) = hz_str.parse::<f64>() { |
| 289 | + self.current_refresh = hz; |
| 290 | + config_changed = true; |
| 291 | + } |
| 292 | + } |
| 293 | + } |
| 294 | + return if config_changed { |
| 295 | + DisplayPanelResult::ConfigChanged(self.get_config().unwrap()) |
| 296 | + } else { |
| 297 | + DisplayPanelResult::Redraw |
| 298 | + }; |
| 299 | + } |
| 300 | + |
| 301 | + // Handle rotation dropdown |
| 302 | + if let Some(action) = self.rotation_dropdown.handle_event(event) { |
| 303 | + if let crate::ui::widgets::DropdownAction::Select(_) = action { |
| 304 | + if let Some(rot) = self.rotation_dropdown.selected_item() { |
| 305 | + if let Ok(r) = rot.parse::<u32>() { |
| 306 | + self.current_rotation = r; |
| 307 | + config_changed = true; |
| 308 | + } |
| 309 | + } |
| 310 | + } |
| 311 | + return if config_changed { |
| 312 | + DisplayPanelResult::ConfigChanged(self.get_config().unwrap()) |
| 313 | + } else { |
| 314 | + DisplayPanelResult::Redraw |
| 315 | + }; |
| 316 | + } |
| 317 | + |
| 318 | + // Handle scale dropdown |
| 319 | + if let Some(action) = self.scale_dropdown.handle_event(event) { |
| 320 | + if let crate::ui::widgets::DropdownAction::Select(_) = action { |
| 321 | + if let Some(scale) = self.scale_dropdown.selected_item() { |
| 322 | + let scale_str = scale.trim_end_matches('x'); |
| 323 | + if let Ok(s) = scale_str.parse::<f64>() { |
| 324 | + self.current_scale = s; |
| 325 | + config_changed = true; |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + return if config_changed { |
| 330 | + DisplayPanelResult::ConfigChanged(self.get_config().unwrap()) |
| 331 | + } else { |
| 332 | + DisplayPanelResult::Redraw |
| 333 | + }; |
| 334 | + } |
| 335 | + |
| 336 | + // Check dropdown expand/collapse state changes |
| 337 | + if self.resolution_dropdown.is_expanded() |
| 338 | + || self.refresh_dropdown.is_expanded() |
| 339 | + || self.rotation_dropdown.is_expanded() |
| 340 | + || self.scale_dropdown.is_expanded() |
| 341 | + { |
| 342 | + return DisplayPanelResult::Redraw; |
| 343 | + } |
| 344 | + |
| 345 | + if config_changed { |
| 346 | + DisplayPanelResult::ConfigChanged(self.get_config().unwrap()) |
| 347 | + } else { |
| 348 | + DisplayPanelResult::None |
| 349 | + } |
| 350 | + } |
| 351 | + |
| 352 | + /// Render the panel. |
| 353 | + pub fn render(&self, renderer: &Renderer, theme: &Theme) -> anyhow::Result<()> { |
| 354 | + // Panel background |
| 355 | + renderer.fill_rect(self.rect, theme.background)?; |
| 356 | + |
| 357 | + // Top border |
| 358 | + renderer.line( |
| 359 | + self.rect.x as f64, |
| 360 | + self.rect.y as f64, |
| 361 | + (self.rect.x + self.rect.width as i32) as f64, |
| 362 | + self.rect.y as f64, |
| 363 | + theme.border, |
| 364 | + 1.0, |
| 365 | + )?; |
| 366 | + |
| 367 | + let label_style = TextStyle::new() |
| 368 | + .font_family(&theme.font_family) |
| 369 | + .font_size(theme.font_size) |
| 370 | + .color(theme.item_description) |
| 371 | + .align(TextAlign::Left); |
| 372 | + |
| 373 | + let title_style = TextStyle::new() |
| 374 | + .font_family(&theme.font_family) |
| 375 | + .font_size(theme.font_size + 2.0) |
| 376 | + .color(theme.foreground) |
| 377 | + .align(TextAlign::Left); |
| 378 | + |
| 379 | + if let Some(ref name) = self.selected_output { |
| 380 | + // Title: Monitor name |
| 381 | + let title_rect = Rect::new(self.rect.x + 10, self.rect.y + 8, 200, 28); |
| 382 | + renderer.text_in_rect(name, title_rect, &title_style)?; |
| 383 | + |
| 384 | + // Enable toggle |
| 385 | + self.enabled_toggle.render(renderer, theme)?; |
| 386 | + |
| 387 | + // Row 1: Resolution and Refresh |
| 388 | + let row1_y = self.rect.y + 45; |
| 389 | + renderer.text_in_rect( |
| 390 | + "Resolution:", |
| 391 | + Rect::new(self.rect.x + 10, row1_y, 90, 28), |
| 392 | + &label_style, |
| 393 | + )?; |
| 394 | + self.resolution_dropdown.render(renderer, theme)?; |
| 395 | + |
| 396 | + renderer.text_in_rect( |
| 397 | + "Refresh:", |
| 398 | + Rect::new(self.rect.x + (self.rect.width as i32 / 2) + 10, row1_y, 70, 28), |
| 399 | + &label_style, |
| 400 | + )?; |
| 401 | + self.refresh_dropdown.render(renderer, theme)?; |
| 402 | + |
| 403 | + // Row 2: Rotation and Scale |
| 404 | + let row2_y = self.rect.y + 85; |
| 405 | + renderer.text_in_rect( |
| 406 | + "Rotation:", |
| 407 | + Rect::new(self.rect.x + 10, row2_y, 90, 28), |
| 408 | + &label_style, |
| 409 | + )?; |
| 410 | + self.rotation_dropdown.render(renderer, theme)?; |
| 411 | + |
| 412 | + renderer.text_in_rect( |
| 413 | + "Scale:", |
| 414 | + Rect::new(self.rect.x + (self.rect.width as i32 / 2) + 10, row2_y, 70, 28), |
| 415 | + &label_style, |
| 416 | + )?; |
| 417 | + self.scale_dropdown.render(renderer, theme)?; |
| 418 | + } else { |
| 419 | + // No monitor selected |
| 420 | + let hint_style = TextStyle::new() |
| 421 | + .font_family(&theme.font_family) |
| 422 | + .font_size(theme.font_size) |
| 423 | + .color(theme.item_description) |
| 424 | + .align(TextAlign::Center); |
| 425 | + renderer.text_in_rect( |
| 426 | + "Select a monitor to configure", |
| 427 | + self.rect, |
| 428 | + &hint_style, |
| 429 | + )?; |
| 430 | + } |
| 431 | + |
| 432 | + Ok(()) |
| 433 | + } |
| 434 | +} |