| 1 | //! Confirmation popup for resuming a session from the launch card. |
| 2 | //! |
| 3 | //! Resuming replaces the whole session context, and a single click used to do |
| 4 | //! it instantly — founder live-test: "you just click it and boom you're there |
| 5 | //! ... you don't realize it's happening". The first attempt at a fix put an |
| 6 | //! arming line over the composer dock, which read as one more piece of chrome |
| 7 | //! rather than as a question ("that's even more confusing tbh"). This is the |
| 8 | //! question, as a popup, with the session it is about named in it. |
| 9 | |
| 10 | use crossterm::event::{ |
| 11 | KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseButton, MouseEvent, MouseEventKind, |
| 12 | }; |
| 13 | use ratatui::layout::Rect; |
| 14 | use ratatui::prelude::*; |
| 15 | use ratatui::widgets::{Paragraph, Widget, Wrap}; |
| 16 | |
| 17 | use crate::tui::views::rounded::render_rounded_modal; |
| 18 | use crate::tui::views::{ModalKind, ModalView, ViewAction, ViewEvent, centered_modal_area}; |
| 19 | use codewhale_localization::{Locale, MessageId, tr}; |
| 20 | use codewhale_palette as palette; |
| 21 | |
| 22 | const MIN_HEIGHT: u16 = 7; |
| 23 | pub(crate) const MIN_CANVAS_HEIGHT: u16 = MIN_HEIGHT + 2; |
| 24 | |
| 25 | pub struct LaunchResumeConfirmView { |
| 26 | session_id: String, |
| 27 | title: String, |
| 28 | detail: String, |
| 29 | locale: Locale, |
| 30 | /// The painted popup, so a click outside it can dismiss. |
| 31 | last_area: std::cell::Cell<Option<Rect>>, |
| 32 | buttons: std::cell::Cell<[Rect; 2]>, |
| 33 | selected: usize, |
| 34 | hovered: Option<usize>, |
| 35 | } |
| 36 | |
| 37 | impl LaunchResumeConfirmView { |
| 38 | #[must_use] |
| 39 | pub fn new(session_id: String, title: String, detail: String, locale: Locale) -> Self { |
| 40 | Self { |
| 41 | session_id, |
| 42 | title, |
| 43 | detail, |
| 44 | locale, |
| 45 | last_area: std::cell::Cell::new(None), |
| 46 | buttons: std::cell::Cell::new([Rect::default(); 2]), |
| 47 | selected: 0, |
| 48 | hovered: None, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | fn confirm(&self) -> ViewAction { |
| 53 | ViewAction::EmitAndClose(ViewEvent::LaunchResumeConfirmed { |
| 54 | session_id: self.session_id.clone(), |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl ModalView for LaunchResumeConfirmView { |
| 60 | fn kind(&self) -> ModalKind { |
| 61 | ModalKind::LaunchResumeConfirm |
| 62 | } |
| 63 | |
| 64 | fn handle_key(&mut self, key: KeyEvent) -> ViewAction { |
| 65 | // Resuming replaces the whole session, so only a plain press answers: |
| 66 | // a release/repeat, or a Ctrl/Alt/Super/Meta/Hyper chord that happens |
| 67 | // to end in Enter or `y`, is not a confirmation (U01-05). Shift stays |
| 68 | // allowed for `Y`. |
| 69 | if key.kind != KeyEventKind::Press { |
| 70 | return ViewAction::None; |
| 71 | } |
| 72 | let chord = key.modifiers.intersects( |
| 73 | KeyModifiers::CONTROL |
| 74 | | KeyModifiers::ALT |
| 75 | | KeyModifiers::SUPER |
| 76 | | KeyModifiers::META |
| 77 | | KeyModifiers::HYPER, |
| 78 | ); |
| 79 | match key.code { |
| 80 | KeyCode::Enter if !chord => { |
| 81 | if self.selected == 0 { |
| 82 | self.confirm() |
| 83 | } else { |
| 84 | ViewAction::Close |
| 85 | } |
| 86 | } |
| 87 | KeyCode::Tab | KeyCode::BackTab | KeyCode::Left | KeyCode::Right => { |
| 88 | self.selected = 1 - self.selected; |
| 89 | ViewAction::None |
| 90 | } |
| 91 | // `y` is the habit every terminal confirmation teaches; Esc and |
| 92 | // `n` both back out. Nothing else acts, so a stray keystroke |
| 93 | // cannot resume a session by accident. |
| 94 | KeyCode::Char('y') | KeyCode::Char('Y') if !chord => self.confirm(), |
| 95 | KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('N') => ViewAction::Close, |
| 96 | _ => ViewAction::None, |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | fn handle_mouse(&mut self, mouse: MouseEvent) -> ViewAction { |
| 101 | let target = self |
| 102 | .buttons |
| 103 | .get() |
| 104 | .iter() |
| 105 | .position(|rect| crate::tui::mouse_ui::mouse_hits_rect(mouse, Some(*rect))); |
| 106 | if matches!(mouse.kind, MouseEventKind::Moved) { |
| 107 | self.hovered = target; |
| 108 | return ViewAction::None; |
| 109 | } |
| 110 | if !matches!(mouse.kind, MouseEventKind::Down(MouseButton::Left)) { |
| 111 | return ViewAction::None; |
| 112 | } |
| 113 | if let Some(index) = target { |
| 114 | return if index == 0 { |
| 115 | self.confirm() |
| 116 | } else { |
| 117 | ViewAction::Close |
| 118 | }; |
| 119 | } |
| 120 | // A click outside the popup is a dismissal, never a confirmation: |
| 121 | // the whole point is that resuming needs a deliberate act. |
| 122 | match self.last_area.get() { |
| 123 | Some(area) if crate::tui::mouse_ui::mouse_hits_rect(mouse, Some(area)) => { |
| 124 | ViewAction::None |
| 125 | } |
| 126 | Some(_) => ViewAction::Close, |
| 127 | None => ViewAction::None, |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | fn occupied_region(&self, area: Rect) -> Rect { |
| 132 | centered_modal_area(area, 64, 11, 32, MIN_HEIGHT) |
| 133 | } |
| 134 | |
| 135 | fn as_any_mut(&mut self) -> &mut dyn std::any::Any { |
| 136 | self |
| 137 | } |
| 138 | |
| 139 | fn render(&self, area: Rect, buf: &mut Buffer) { |
| 140 | let popup = self.occupied_region(area); |
| 141 | self.last_area.set(Some(popup)); |
| 142 | let inner = render_rounded_modal( |
| 143 | area, |
| 144 | popup, |
| 145 | buf, |
| 146 | tr(self.locale, MessageId::LaunchResumeConfirmTitle), |
| 147 | None, |
| 148 | true, |
| 149 | ); |
| 150 | |
| 151 | let body_height = inner.height.saturating_sub(1); |
| 152 | let warning = Paragraph::new(tr(self.locale, MessageId::LaunchResumeConfirmBody)) |
| 153 | .style(Style::default().fg(palette::TEXT_SOFT)) |
| 154 | .wrap(Wrap { trim: true }); |
| 155 | let warning_height = warning |
| 156 | .line_count(inner.width) |
| 157 | .min(usize::from(body_height)) as u16; |
| 158 | let spare = body_height.saturating_sub(warning_height); |
| 159 | let mut y = inner.y; |
| 160 | if spare > 0 { |
| 161 | Paragraph::new(codewhale_tui_kit::ui_text::truncate_line_to_width( |
| 162 | &self.title, |
| 163 | usize::from(inner.width), |
| 164 | )) |
| 165 | .style(Style::default().fg(palette::TEXT_PRIMARY).bold()) |
| 166 | .render(Rect::new(inner.x, y, inner.width, 1), buf); |
| 167 | y += 1; |
| 168 | } |
| 169 | if spare > 1 { |
| 170 | Paragraph::new(codewhale_tui_kit::ui_text::truncate_line_to_width( |
| 171 | &self.detail, |
| 172 | usize::from(inner.width), |
| 173 | )) |
| 174 | .style(Style::default().fg(palette::TEXT_MUTED)) |
| 175 | .render(Rect::new(inner.x, y, inner.width, 1), buf); |
| 176 | y += 1; |
| 177 | } |
| 178 | if spare > 2 { |
| 179 | y += 1; |
| 180 | } |
| 181 | warning.render(Rect::new(inner.x, y, inner.width, warning_height), buf); |
| 182 | |
| 183 | let button_width = inner.width.saturating_sub(1) / 2; |
| 184 | let y = inner.bottom().saturating_sub(1); |
| 185 | let buttons = [ |
| 186 | Rect::new(inner.x, y, button_width, u16::from(inner.height > 0)), |
| 187 | Rect::new( |
| 188 | inner.x + button_width + 1, |
| 189 | y, |
| 190 | button_width, |
| 191 | u16::from(inner.height > 0), |
| 192 | ), |
| 193 | ]; |
| 194 | self.buttons.set(buttons); |
| 195 | for (index, id) in [ |
| 196 | MessageId::LaunchResumeConfirmResume, |
| 197 | MessageId::LaunchResumeConfirmCancel, |
| 198 | ] |
| 199 | .into_iter() |
| 200 | .enumerate() |
| 201 | { |
| 202 | let label = tr(self.locale, id); |
| 203 | let key = if self.selected == index { |
| 204 | "Enter" |
| 205 | } else if index == 1 { |
| 206 | "Esc" |
| 207 | } else { |
| 208 | "" |
| 209 | }; |
| 210 | let text = if !key.is_empty() |
| 211 | && unicode_width::UnicodeWidthStr::width(label.as_ref()) + key.len() + 3 |
| 212 | <= usize::from(button_width) |
| 213 | { |
| 214 | format!("{label} {key}") |
| 215 | } else { |
| 216 | label.into_owned() |
| 217 | }; |
| 218 | let style = if self.selected == index { |
| 219 | codewhale_tui_kit::menu_style::selected_row_style() |
| 220 | } else if self.hovered == Some(index) { |
| 221 | codewhale_tui_kit::menu_style::hovered_row_style().fg(palette::TEXT_PRIMARY) |
| 222 | } else { |
| 223 | Style::default() |
| 224 | .bg(palette::SURFACE_ELEVATED) |
| 225 | .fg(palette::TEXT_PRIMARY) |
| 226 | }; |
| 227 | Paragraph::new(text) |
| 228 | .centered() |
| 229 | .style(style) |
| 230 | .render(buttons[index], buf); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | #[cfg(test)] |
| 236 | mod tests { |
| 237 | use super::*; |
| 238 | use crossterm::event::KeyModifiers; |
| 239 | |
| 240 | fn view() -> LaunchResumeConfirmView { |
| 241 | LaunchResumeConfirmView::new( |
| 242 | "sess-1".to_string(), |
| 243 | "refactor the parser".to_string(), |
| 244 | "3h ago · 12 msgs".to_string(), |
| 245 | Locale::En, |
| 246 | ) |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn enter_confirms_and_esc_walks_away() { |
| 251 | let mut confirm = view(); |
| 252 | match confirm.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)) { |
| 253 | ViewAction::EmitAndClose(ViewEvent::LaunchResumeConfirmed { session_id }) => { |
| 254 | assert_eq!(session_id, "sess-1"); |
| 255 | } |
| 256 | other => panic!("Enter must confirm, got {other:?}"), |
| 257 | } |
| 258 | |
| 259 | let mut confirm = view(); |
| 260 | assert!(matches!( |
| 261 | confirm.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)), |
| 262 | ViewAction::Close |
| 263 | )); |
| 264 | |
| 265 | // A stray keystroke resumes nothing: the whole point is that this |
| 266 | // takes a deliberate act. |
| 267 | let mut confirm = view(); |
| 268 | assert!(matches!( |
| 269 | confirm.handle_key(KeyEvent::new(KeyCode::Char('k'), KeyModifiers::NONE)), |
| 270 | ViewAction::None |
| 271 | )); |
| 272 | |
| 273 | // U01-05: a chord that ends in Enter or `y`, and a key release, are |
| 274 | // not a confirmation. |
| 275 | let dispatched = |code, modifiers| { |
| 276 | KeyEvent::new( |
| 277 | code, |
| 278 | crate::tui::composer_ui::normalize_macos_modifiers(modifiers), |
| 279 | ) |
| 280 | }; |
| 281 | for key in [ |
| 282 | KeyEvent::new(KeyCode::Enter, KeyModifiers::CONTROL), |
| 283 | KeyEvent::new(KeyCode::Char('y'), KeyModifiers::ALT), |
| 284 | KeyEvent::new(KeyCode::Char('y'), KeyModifiers::SUPER), |
| 285 | dispatched(KeyCode::Char('y'), KeyModifiers::SUPER), |
| 286 | dispatched(KeyCode::Enter, KeyModifiers::META), |
| 287 | dispatched(KeyCode::Char('y'), KeyModifiers::META), |
| 288 | dispatched(KeyCode::Enter, KeyModifiers::HYPER), |
| 289 | dispatched( |
| 290 | KeyCode::Char('Y'), |
| 291 | KeyModifiers::HYPER | KeyModifiers::SHIFT, |
| 292 | ), |
| 293 | dispatched(KeyCode::Char('y'), KeyModifiers::META | KeyModifiers::SHIFT), |
| 294 | KeyEvent::new_with_kind( |
| 295 | KeyCode::Enter, |
| 296 | KeyModifiers::NONE, |
| 297 | crossterm::event::KeyEventKind::Release, |
| 298 | ), |
| 299 | KeyEvent::new_with_kind( |
| 300 | KeyCode::Char('y'), |
| 301 | KeyModifiers::NONE, |
| 302 | crossterm::event::KeyEventKind::Repeat, |
| 303 | ), |
| 304 | ] { |
| 305 | let mut confirm = view(); |
| 306 | assert!( |
| 307 | matches!(confirm.handle_key(key), ViewAction::None), |
| 308 | "{key:?} must not resume" |
| 309 | ); |
| 310 | } |
| 311 | |
| 312 | for key in [ |
| 313 | dispatched(KeyCode::Enter, KeyModifiers::NONE), |
| 314 | dispatched(KeyCode::Char('y'), KeyModifiers::NONE), |
| 315 | dispatched(KeyCode::Char('Y'), KeyModifiers::SHIFT), |
| 316 | ] { |
| 317 | let mut confirm = view(); |
| 318 | assert!( |
| 319 | matches!( |
| 320 | confirm.handle_key(key), |
| 321 | ViewAction::EmitAndClose(ViewEvent::LaunchResumeConfirmed { .. }) |
| 322 | ), |
| 323 | "{key:?} must still confirm" |
| 324 | ); |
| 325 | } |
| 326 | |
| 327 | for modifiers in [ |
| 328 | KeyModifiers::META, |
| 329 | KeyModifiers::HYPER, |
| 330 | KeyModifiers::CONTROL, |
| 331 | ] { |
| 332 | let mut confirm = view(); |
| 333 | assert!( |
| 334 | matches!( |
| 335 | confirm.handle_key(dispatched(KeyCode::Esc, modifiers)), |
| 336 | ViewAction::Close |
| 337 | ), |
| 338 | "{modifiers:?}+Esc must still back out" |
| 339 | ); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn the_popup_stays_above_the_composer_on_a_short_terminal() { |
| 345 | let area = Rect::new(0, 0, 60, 20); |
| 346 | let composer_top = 14; |
| 347 | let mut stack = crate::tui::views::ViewStack::new(); |
| 348 | stack.push(view()); |
| 349 | assert_eq!( |
| 350 | stack.top_occupied_region(area).map(|popup| popup.bottom()), |
| 351 | Some(15), |
| 352 | "centered in the whole frame, the popup's border lands on the composer's" |
| 353 | ); |
| 354 | stack.set_composer_top(Some(composer_top)); |
| 355 | let popup = stack.top_occupied_region(area).expect("a popup is open"); |
| 356 | assert!(popup.bottom() <= composer_top, "{popup:?}"); |
| 357 | let mut buf = Buffer::empty(area); |
| 358 | stack.render(area, &mut buf); |
| 359 | for y in composer_top..area.height { |
| 360 | let row: String = (0..area.width).map(|x| buf[(x, y)].symbol()).collect(); |
| 361 | assert!(row.trim().is_empty(), "row {y} is the composer's: {row:?}"); |
| 362 | } |
| 363 | let painted: String = (0..composer_top) |
| 364 | .flat_map(|y| (0..area.width).map(move |x| (x, y))) |
| 365 | .map(|at| buf[at].symbol()) |
| 366 | .collect(); |
| 367 | assert!( |
| 368 | painted.contains("12 msgs") && painted.contains("Esc"), |
| 369 | "{painted}" |
| 370 | ); |
| 371 | |
| 372 | stack.set_composer_top(Some(MIN_CANVAS_HEIGHT - 1)); |
| 373 | assert_eq!( |
| 374 | stack.top_occupied_region(area).map(|popup| popup.bottom()), |
| 375 | Some(15), |
| 376 | "too little room above the composer keeps the popup readable over it" |
| 377 | ); |
| 378 | } |
| 379 | |
| 380 | #[test] |
| 381 | fn the_popup_names_the_session_it_is_asking_about() { |
| 382 | let confirm = view(); |
| 383 | let area = Rect::new(0, 0, 100, 30); |
| 384 | let mut buf = Buffer::empty(area); |
| 385 | confirm.render(area, &mut buf); |
| 386 | let painted: String = (0..area.height) |
| 387 | .map(|y| { |
| 388 | (0..area.width) |
| 389 | .map(|x| buf[(x, y)].symbol()) |
| 390 | .collect::<String>() |
| 391 | }) |
| 392 | .collect::<Vec<_>>() |
| 393 | .join("\n"); |
| 394 | assert!( |
| 395 | painted.contains("refactor the parser"), |
| 396 | "the session title is in the popup:\n{painted}" |
| 397 | ); |
| 398 | assert!( |
| 399 | painted.contains("12 msgs"), |
| 400 | "so is enough detail to recognise it:\n{painted}" |
| 401 | ); |
| 402 | } |
| 403 | } |
| 404 |