@@ -0,0 +1,451 @@ |
| | 1 | +use anyhow::{Context, Result}; |
| | 2 | +use gartk_core::{Color, InputEvent, Key, KeyEvent, Rect, Theme}; |
| | 3 | +use gartk_render::{Renderer, TextStyle, copy_surface_to_window}; |
| | 4 | +use gartk_x11::{ |
| | 5 | + Connection, EventLoop, EventLoopConfig, Window, WindowConfig, monitor_at_pointer, |
| | 6 | + primary_monitor, |
| | 7 | +}; |
| | 8 | +use std::time::{Duration, Instant}; |
| | 9 | +use x11rb::protocol::xproto::ConnectionExt; |
| | 10 | + |
| | 11 | +const DIALOG_WIDTH: u32 = 560; |
| | 12 | +const DIALOG_HEIGHT: u32 = 240; |
| | 13 | +const CARD_PADDING: i32 = 18; |
| | 14 | + |
| | 15 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| | 16 | +pub enum PromptMode { |
| | 17 | + Secret, |
| | 18 | + Plain, |
| | 19 | +} |
| | 20 | + |
| | 21 | +#[derive(Debug, Clone)] |
| | 22 | +pub struct PromptRequest { |
| | 23 | + pub message: String, |
| | 24 | + pub mode: PromptMode, |
| | 25 | + pub timeout_secs: u64, |
| | 26 | +} |
| | 27 | + |
| | 28 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| | 29 | +pub enum PromptExit { |
| | 30 | + Submitted(String), |
| | 31 | + Canceled, |
| | 32 | + TimedOut, |
| | 33 | +} |
| | 34 | + |
| | 35 | +struct PromptDialog { |
| | 36 | + window: Window, |
| | 37 | + renderer: Renderer, |
| | 38 | + gc: u32, |
| | 39 | + request: PromptRequest, |
| | 40 | + input: String, |
| | 41 | + cursor: usize, |
| | 42 | + exit: Option<PromptExit>, |
| | 43 | + deadline: Option<Instant>, |
| | 44 | + remaining_secs: Option<u64>, |
| | 45 | + card_background: Color, |
| | 46 | + card_border: Color, |
| | 47 | + accent: Color, |
| | 48 | +} |
| | 49 | + |
| | 50 | +pub fn run_prompt_dialog(request: PromptRequest) -> Result<PromptExit> { |
| | 51 | + let conn = Connection::connect(None).context("failed to connect to X11 display")?; |
| | 52 | + let (x, y) = centered_position(&conn, DIALOG_WIDTH, DIALOG_HEIGHT); |
| | 53 | + let window = Window::create( |
| | 54 | + conn.clone(), |
| | 55 | + WindowConfig::dialog() |
| | 56 | + .title("garcard authentication") |
| | 57 | + .class("garcard") |
| | 58 | + .position(x, y) |
| | 59 | + .size(DIALOG_WIDTH, DIALOG_HEIGHT) |
| | 60 | + .transparent(true) |
| | 61 | + .modal(true), |
| | 62 | + ) |
| | 63 | + .context("failed to create prompt window")?; |
| | 64 | + window.focus().context("failed to focus prompt window")?; |
| | 65 | + |
| | 66 | + let mut dialog = PromptDialog::new(window, request)?; |
| | 67 | + dialog.run() |
| | 68 | +} |
| | 69 | + |
| | 70 | +fn centered_position(conn: &Connection, width: u32, height: u32) -> (i32, i32) { |
| | 71 | + let monitor = monitor_at_pointer(conn) |
| | 72 | + .or_else(|_| primary_monitor(conn)) |
| | 73 | + .ok(); |
| | 74 | + if let Some(monitor) = monitor { |
| | 75 | + let x = monitor.rect.x + (monitor.rect.width as i32 - width as i32) / 2; |
| | 76 | + let y = monitor.rect.y + (monitor.rect.height as i32 - height as i32) / 3; |
| | 77 | + return (x, y); |
| | 78 | + } |
| | 79 | + |
| | 80 | + ( |
| | 81 | + (conn.screen_width() as i32 - width as i32) / 2, |
| | 82 | + (conn.screen_height() as i32 - height as i32) / 3, |
| | 83 | + ) |
| | 84 | +} |
| | 85 | + |
| | 86 | +impl PromptDialog { |
| | 87 | + fn new(window: Window, request: PromptRequest) -> Result<Self> { |
| | 88 | + let mut theme = Theme::dark(); |
| | 89 | + theme.font_family = "Sans".to_string(); |
| | 90 | + theme.font_size = 14.0; |
| | 91 | + let card_background = Color::from_hex("#111318").expect("valid card color"); |
| | 92 | + let card_border = Color::from_hex("#2c3442").expect("valid border color"); |
| | 93 | + let accent = Color::from_hex("#8ab4f8").expect("valid accent color"); |
| | 94 | + let size = window.size(); |
| | 95 | + let renderer = Renderer::with_theme(size.width, size.height, theme)?; |
| | 96 | + |
| | 97 | + let conn = window.connection(); |
| | 98 | + let gc = conn.generate_id()?; |
| | 99 | + conn.inner() |
| | 100 | + .create_gc(gc, window.id(), &Default::default())?; |
| | 101 | + conn.flush()?; |
| | 102 | + |
| | 103 | + let deadline = if request.timeout_secs > 0 { |
| | 104 | + Some(Instant::now() + Duration::from_secs(request.timeout_secs)) |
| | 105 | + } else { |
| | 106 | + None |
| | 107 | + }; |
| | 108 | + |
| | 109 | + Ok(Self { |
| | 110 | + window, |
| | 111 | + renderer, |
| | 112 | + gc, |
| | 113 | + request, |
| | 114 | + input: String::new(), |
| | 115 | + cursor: 0, |
| | 116 | + exit: None, |
| | 117 | + deadline, |
| | 118 | + remaining_secs: None, |
| | 119 | + card_background, |
| | 120 | + card_border, |
| | 121 | + accent, |
| | 122 | + }) |
| | 123 | + } |
| | 124 | + |
| | 125 | + fn run(&mut self) -> Result<PromptExit> { |
| | 126 | + let mut event_loop = EventLoop::new(&self.window, EventLoopConfig::default())?; |
| | 127 | + self.refresh_timeout(); |
| | 128 | + self.render()?; |
| | 129 | + |
| | 130 | + event_loop.run(|ev, event| { |
| | 131 | + match event { |
| | 132 | + InputEvent::Key(key_event) if key_event.pressed => { |
| | 133 | + self.handle_key(&key_event); |
| | 134 | + ev.request_redraw(); |
| | 135 | + } |
| | 136 | + InputEvent::Resize { width, height } => { |
| | 137 | + if let Err(err) = self.renderer.resize(width, height) { |
| | 138 | + tracing::error!(error = %err, "failed to resize prompt renderer"); |
| | 139 | + self.exit = Some(PromptExit::Canceled); |
| | 140 | + } |
| | 141 | + ev.request_redraw(); |
| | 142 | + } |
| | 143 | + InputEvent::Expose => { |
| | 144 | + ev.request_redraw(); |
| | 145 | + } |
| | 146 | + InputEvent::CloseRequested => { |
| | 147 | + self.exit = Some(PromptExit::Canceled); |
| | 148 | + } |
| | 149 | + InputEvent::Idle => { |
| | 150 | + if self.refresh_timeout() { |
| | 151 | + ev.request_redraw(); |
| | 152 | + } |
| | 153 | + } |
| | 154 | + _ => {} |
| | 155 | + } |
| | 156 | + |
| | 157 | + if ev.needs_redraw() { |
| | 158 | + let _ = self.render(); |
| | 159 | + ev.redraw_done(); |
| | 160 | + } |
| | 161 | + |
| | 162 | + Ok(self.exit.is_none()) |
| | 163 | + })?; |
| | 164 | + |
| | 165 | + Ok(self.exit.take().unwrap_or(PromptExit::Canceled)) |
| | 166 | + } |
| | 167 | + |
| | 168 | + fn refresh_timeout(&mut self) -> bool { |
| | 169 | + let Some(deadline) = self.deadline else { |
| | 170 | + return false; |
| | 171 | + }; |
| | 172 | + |
| | 173 | + let now = Instant::now(); |
| | 174 | + if now >= deadline { |
| | 175 | + self.exit = Some(PromptExit::TimedOut); |
| | 176 | + return true; |
| | 177 | + } |
| | 178 | + |
| | 179 | + let remaining = deadline.duration_since(now).as_secs(); |
| | 180 | + if self.remaining_secs != Some(remaining) { |
| | 181 | + self.remaining_secs = Some(remaining); |
| | 182 | + return true; |
| | 183 | + } |
| | 184 | + |
| | 185 | + false |
| | 186 | + } |
| | 187 | + |
| | 188 | + fn handle_key(&mut self, key_event: &KeyEvent) { |
| | 189 | + match key_event.key { |
| | 190 | + Key::Escape => { |
| | 191 | + self.exit = Some(PromptExit::Canceled); |
| | 192 | + } |
| | 193 | + Key::Return => { |
| | 194 | + self.exit = Some(PromptExit::Submitted(self.input.clone())); |
| | 195 | + } |
| | 196 | + Key::Left => { |
| | 197 | + if self.cursor > 0 { |
| | 198 | + self.cursor -= 1; |
| | 199 | + } |
| | 200 | + } |
| | 201 | + Key::Right => { |
| | 202 | + if self.cursor < self.input.chars().count() { |
| | 203 | + self.cursor += 1; |
| | 204 | + } |
| | 205 | + } |
| | 206 | + Key::Home => { |
| | 207 | + self.cursor = 0; |
| | 208 | + } |
| | 209 | + Key::End => { |
| | 210 | + self.cursor = self.input.chars().count(); |
| | 211 | + } |
| | 212 | + Key::Backspace => { |
| | 213 | + remove_char_before(&mut self.input, &mut self.cursor); |
| | 214 | + } |
| | 215 | + Key::Delete => { |
| | 216 | + remove_char_at(&mut self.input, self.cursor); |
| | 217 | + } |
| | 218 | + Key::Space => { |
| | 219 | + if key_event.modifiers.is_empty() || key_event.modifiers.shift { |
| | 220 | + insert_char_at(&mut self.input, self.cursor, ' '); |
| | 221 | + self.cursor += 1; |
| | 222 | + } |
| | 223 | + } |
| | 224 | + Key::Char(ch) => { |
| | 225 | + if ch.is_control() { |
| | 226 | + return; |
| | 227 | + } |
| | 228 | + if key_event.modifiers.ctrl |
| | 229 | + || key_event.modifiers.alt |
| | 230 | + || key_event.modifiers.super_key |
| | 231 | + { |
| | 232 | + return; |
| | 233 | + } |
| | 234 | + insert_char_at(&mut self.input, self.cursor, ch); |
| | 235 | + self.cursor += 1; |
| | 236 | + } |
| | 237 | + _ => {} |
| | 238 | + } |
| | 239 | + } |
| | 240 | + |
| | 241 | + fn render(&mut self) -> Result<()> { |
| | 242 | + let size = self.renderer.size(); |
| | 243 | + let width = size.width as i32; |
| | 244 | + let height = size.height as i32; |
| | 245 | + let theme = self.renderer.theme().clone(); |
| | 246 | + |
| | 247 | + self.renderer |
| | 248 | + .clear_color(Color::from_hex("#0a0b10").expect("valid backdrop color"))?; |
| | 249 | + |
| | 250 | + let card_rect = Rect::new( |
| | 251 | + CARD_PADDING, |
| | 252 | + CARD_PADDING, |
| | 253 | + (width - CARD_PADDING * 2) as u32, |
| | 254 | + (height - CARD_PADDING * 2) as u32, |
| | 255 | + ); |
| | 256 | + self.renderer |
| | 257 | + .fill_rounded_rect(card_rect, 12.0, self.card_background)?; |
| | 258 | + self.renderer |
| | 259 | + .stroke_rounded_rect(card_rect, 12.0, self.card_border, 2.0)?; |
| | 260 | + |
| | 261 | + let title_style = TextStyle::new() |
| | 262 | + .font_family(theme.font_family.clone()) |
| | 263 | + .font_size(18.0) |
| | 264 | + .color(theme.foreground); |
| | 265 | + self.renderer.text( |
| | 266 | + "Authentication Required", |
| | 267 | + (CARD_PADDING + 16) as f64, |
| | 268 | + (CARD_PADDING + 14) as f64, |
| | 269 | + &title_style, |
| | 270 | + )?; |
| | 271 | + |
| | 272 | + let message_style = TextStyle::new() |
| | 273 | + .font_family(theme.font_family.clone()) |
| | 274 | + .font_size(13.0) |
| | 275 | + .color(theme.item_description) |
| | 276 | + .max_width(card_rect.width as i32 - 32) |
| | 277 | + .wrap(true) |
| | 278 | + .ellipsize(true); |
| | 279 | + self.renderer.text( |
| | 280 | + &self.request.message, |
| | 281 | + (CARD_PADDING + 16) as f64, |
| | 282 | + (CARD_PADDING + 46) as f64, |
| | 283 | + &message_style, |
| | 284 | + )?; |
| | 285 | + |
| | 286 | + let input_label = match self.request.mode { |
| | 287 | + PromptMode::Secret => "Password", |
| | 288 | + PromptMode::Plain => "Response", |
| | 289 | + }; |
| | 290 | + let label_style = TextStyle::new() |
| | 291 | + .font_family(theme.font_family.clone()) |
| | 292 | + .font_size(12.0) |
| | 293 | + .color(theme.input_placeholder); |
| | 294 | + self.renderer.text( |
| | 295 | + input_label, |
| | 296 | + (CARD_PADDING + 16) as f64, |
| | 297 | + (CARD_PADDING + 108) as f64, |
| | 298 | + &label_style, |
| | 299 | + )?; |
| | 300 | + |
| | 301 | + let input_rect = Rect::new( |
| | 302 | + CARD_PADDING + 16, |
| | 303 | + CARD_PADDING + 126, |
| | 304 | + (width - (CARD_PADDING + 16) * 2) as u32, |
| | 305 | + 40, |
| | 306 | + ); |
| | 307 | + self.renderer |
| | 308 | + .fill_rounded_rect(input_rect, 8.0, theme.input_background)?; |
| | 309 | + self.renderer |
| | 310 | + .stroke_rounded_rect(input_rect, 8.0, self.accent.with_alpha(0.7), 1.5)?; |
| | 311 | + |
| | 312 | + let display_input = display_value(&self.input, self.request.mode); |
| | 313 | + let input_style = TextStyle::new() |
| | 314 | + .font_family(theme.font_family.clone()) |
| | 315 | + .font_size(14.0) |
| | 316 | + .color(theme.input_foreground); |
| | 317 | + let input_y = input_rect.y + 12; |
| | 318 | + let input_x = input_rect.x + 10; |
| | 319 | + self.renderer |
| | 320 | + .text(&display_input, input_x as f64, input_y as f64, &input_style)?; |
| | 321 | + |
| | 322 | + let cursor_prefix = display_prefix(&self.input, self.cursor, self.request.mode); |
| | 323 | + let cursor_size = self.renderer.measure_text(&cursor_prefix, &input_style)?; |
| | 324 | + let cursor_x = input_x + cursor_size.width as i32; |
| | 325 | + self.renderer.fill_rect( |
| | 326 | + Rect::new(cursor_x, input_rect.y + 8, 2, input_rect.height - 16), |
| | 327 | + self.accent, |
| | 328 | + )?; |
| | 329 | + |
| | 330 | + let footer_style = TextStyle::new() |
| | 331 | + .font_family(theme.font_family) |
| | 332 | + .font_size(12.0) |
| | 333 | + .color(theme.item_description); |
| | 334 | + self.renderer.text( |
| | 335 | + "Enter submit Esc cancel", |
| | 336 | + (CARD_PADDING + 16) as f64, |
| | 337 | + (height - CARD_PADDING - 26) as f64, |
| | 338 | + &footer_style, |
| | 339 | + )?; |
| | 340 | + |
| | 341 | + if let Some(remaining) = self.remaining_secs { |
| | 342 | + let timer_text = format!("timeout {}s", remaining); |
| | 343 | + let timer_style = TextStyle::new() |
| | 344 | + .font_family("Sans") |
| | 345 | + .font_size(12.0) |
| | 346 | + .color(self.accent); |
| | 347 | + let timer_size = self.renderer.measure_text(&timer_text, &timer_style)?; |
| | 348 | + self.renderer.text( |
| | 349 | + &timer_text, |
| | 350 | + (width - CARD_PADDING - timer_size.width as i32 - 16) as f64, |
| | 351 | + (height - CARD_PADDING - 26) as f64, |
| | 352 | + &timer_style, |
| | 353 | + )?; |
| | 354 | + } |
| | 355 | + |
| | 356 | + self.renderer.flush(); |
| | 357 | + copy_surface_to_window(self.renderer.surface_mut(), &self.window, self.gc, 0, 0)?; |
| | 358 | + Ok(()) |
| | 359 | + } |
| | 360 | +} |
| | 361 | + |
| | 362 | +impl Drop for PromptDialog { |
| | 363 | + fn drop(&mut self) { |
| | 364 | + let _ = self.window.connection().inner().free_gc(self.gc); |
| | 365 | + } |
| | 366 | +} |
| | 367 | + |
| | 368 | +fn display_value(input: &str, mode: PromptMode) -> String { |
| | 369 | + match mode { |
| | 370 | + PromptMode::Secret => "*".repeat(input.chars().count()), |
| | 371 | + PromptMode::Plain => input.to_string(), |
| | 372 | + } |
| | 373 | +} |
| | 374 | + |
| | 375 | +fn display_prefix(input: &str, cursor: usize, mode: PromptMode) -> String { |
| | 376 | + let prefix = prefix_chars(input, cursor); |
| | 377 | + match mode { |
| | 378 | + PromptMode::Secret => "*".repeat(prefix.chars().count()), |
| | 379 | + PromptMode::Plain => prefix, |
| | 380 | + } |
| | 381 | +} |
| | 382 | + |
| | 383 | +fn prefix_chars(input: &str, char_count: usize) -> String { |
| | 384 | + input.chars().take(char_count).collect() |
| | 385 | +} |
| | 386 | + |
| | 387 | +fn char_to_byte_index(input: &str, char_index: usize) -> usize { |
| | 388 | + input |
| | 389 | + .char_indices() |
| | 390 | + .nth(char_index) |
| | 391 | + .map(|(byte, _)| byte) |
| | 392 | + .unwrap_or_else(|| input.len()) |
| | 393 | +} |
| | 394 | + |
| | 395 | +fn insert_char_at(input: &mut String, char_index: usize, value: char) { |
| | 396 | + let byte_index = char_to_byte_index(input, char_index); |
| | 397 | + input.insert(byte_index, value); |
| | 398 | +} |
| | 399 | + |
| | 400 | +fn remove_char_before(input: &mut String, cursor: &mut usize) { |
| | 401 | + if *cursor == 0 { |
| | 402 | + return; |
| | 403 | + } |
| | 404 | + |
| | 405 | + let end = char_to_byte_index(input, *cursor); |
| | 406 | + let start = char_to_byte_index(input, *cursor - 1); |
| | 407 | + input.drain(start..end); |
| | 408 | + *cursor -= 1; |
| | 409 | +} |
| | 410 | + |
| | 411 | +fn remove_char_at(input: &mut String, cursor: usize) { |
| | 412 | + if cursor >= input.chars().count() { |
| | 413 | + return; |
| | 414 | + } |
| | 415 | + |
| | 416 | + let start = char_to_byte_index(input, cursor); |
| | 417 | + let end = char_to_byte_index(input, cursor + 1); |
| | 418 | + input.drain(start..end); |
| | 419 | +} |
| | 420 | + |
| | 421 | +#[cfg(test)] |
| | 422 | +mod tests { |
| | 423 | + use super::*; |
| | 424 | + |
| | 425 | + #[test] |
| | 426 | + fn display_value_masks_secret_text() { |
| | 427 | + assert_eq!(display_value("secret", PromptMode::Secret), "******"); |
| | 428 | + assert_eq!(display_value("plain", PromptMode::Plain), "plain"); |
| | 429 | + } |
| | 430 | + |
| | 431 | + #[test] |
| | 432 | + fn insert_and_remove_respect_char_positions() { |
| | 433 | + let mut value = String::from("abcd"); |
| | 434 | + insert_char_at(&mut value, 2, 'X'); |
| | 435 | + assert_eq!(value, "abXcd"); |
| | 436 | + |
| | 437 | + let mut cursor = 3; |
| | 438 | + remove_char_before(&mut value, &mut cursor); |
| | 439 | + assert_eq!(value, "abcd"); |
| | 440 | + assert_eq!(cursor, 2); |
| | 441 | + |
| | 442 | + remove_char_at(&mut value, 1); |
| | 443 | + assert_eq!(value, "acd"); |
| | 444 | + } |
| | 445 | + |
| | 446 | + #[test] |
| | 447 | + fn display_prefix_uses_cursor_and_mode() { |
| | 448 | + assert_eq!(display_prefix("hello", 2, PromptMode::Plain), "he"); |
| | 449 | + assert_eq!(display_prefix("hello", 2, PromptMode::Secret), "**"); |
| | 450 | + } |
| | 451 | +} |