| 1 | //! `/theme` picker with live preview. |
| 2 | //! |
| 3 | //! Built on [`crate::tui::settings_picker`]: navigation, filtering ownership, |
| 4 | //! and transactional preview/commit/rollback live in the shared controller. |
| 5 | //! Theme-specific chrome (swatches, underwater surface) stays here so the |
| 6 | //! framework contract does not flatten visual character. |
| 7 | //! |
| 8 | //! Semantics preserved from the pre-framework picker: |
| 9 | //! - Up/Down emit a `ThemeSelectionUpdated{persist:false}` so the host swaps |
| 10 | //! `app.ui_theme` immediately and the whole TUI re-paints under the modal. |
| 11 | //! - Enter persists (`persist:true`); Esc emits one more |
| 12 | //! `ThemeSelectionUpdated{persist:false}` to restore the exact theme that |
| 13 | //! was active when the picker opened. |
| 14 | //! |
| 15 | //! The option list contains compiled themes followed by valid user overlays. |
| 16 | //! `underwater` is an ordinary row: the painted ocean field is the theme, not |
| 17 | //! a treatment beside it. |
| 18 | |
| 19 | use std::borrow::Cow; |
| 20 | use std::cell::RefCell; |
| 21 | |
| 22 | use crossterm::event::{KeyEvent, MouseButton, MouseEvent, MouseEventKind}; |
| 23 | use ratatui::{ |
| 24 | buffer::Buffer, |
| 25 | layout::Rect, |
| 26 | style::{Color, Modifier, Style}, |
| 27 | text::{Line, Span}, |
| 28 | widgets::{Paragraph, Widget, Wrap}, |
| 29 | }; |
| 30 | |
| 31 | use crate::tui::settings_picker::{ |
| 32 | PickerNavResult, SettingAvailability, SettingOption, SettingValues, SettingsPickerController, |
| 33 | SettingsPickerLayout, handle_nav_key, |
| 34 | }; |
| 35 | use crate::tui::views::{ |
| 36 | ActionHint, ModalKind, ModalThemeScope, ModalView, SurfaceFrame, ViewAction, ViewEvent, |
| 37 | render_modal_footer, render_panel_scroll_rail, render_surface_frame, render_vertical_divider, |
| 38 | }; |
| 39 | use codewhale_config::settings_schema::DEFAULT_TUI_THEME; |
| 40 | use codewhale_localization::{Locale, MessageId, tr}; |
| 41 | use codewhale_palette::{SELECTABLE_THEMES, ThemeId, UiTheme}; |
| 42 | use codewhale_tui_kit::menu_style; |
| 43 | |
| 44 | pub struct ThemePickerView { |
| 45 | controller: SettingsPickerController, |
| 46 | /// Exact opening state for Esc rollback. |
| 47 | original_theme_name: String, |
| 48 | /// Cursor index the controller settled on at open time (row 0 when the |
| 49 | /// persisted selector is not a compiled theme row). Enter without any |
| 50 | /// navigation commits the original name, never this fallback row. |
| 51 | opening_cursor: Option<usize>, |
| 52 | /// Cached UiTheme for `ThemeId::System`, captured once at construction |
| 53 | /// so the per-frame render doesn't re-invoke `UiTheme::detect()` (which |
| 54 | /// reads `COLORFGBG`) on every keystroke. |
| 55 | system_ui_theme: UiTheme, |
| 56 | /// User-configured background applied on top of every named-theme preview. |
| 57 | background_override: Option<Color>, |
| 58 | row_hitboxes: RefCell<Vec<(Rect, usize)>>, |
| 59 | last_mouse_selected: Option<usize>, |
| 60 | /// UI locale captured from the app at construction (#4057 wave 2). |
| 61 | locale: Locale, |
| 62 | /// Valid user overlays loaded once when the picker opens. |
| 63 | custom_themes: Vec<codewhale_palette::UserThemeOption>, |
| 64 | } |
| 65 | |
| 66 | impl ThemePickerView { |
| 67 | #[cfg(test)] |
| 68 | #[must_use] |
| 69 | pub fn new(original_name: String) -> Self { |
| 70 | Self::new_with_background(original_name, Locale::En, None) |
| 71 | } |
| 72 | |
| 73 | fn new_with_background( |
| 74 | original_name: String, |
| 75 | locale: Locale, |
| 76 | background_override: Option<Color>, |
| 77 | ) -> Self { |
| 78 | let normalized = original_name.trim().to_ascii_lowercase(); |
| 79 | let (options, custom_themes) = theme_options(&normalized); |
| 80 | let controller = SettingsPickerController::new(options, normalized.clone()); |
| 81 | let opening_cursor = controller.selected_source_index(); |
| 82 | Self { |
| 83 | controller, |
| 84 | original_theme_name: normalized, |
| 85 | opening_cursor, |
| 86 | system_ui_theme: UiTheme::detect(), |
| 87 | background_override, |
| 88 | row_hitboxes: RefCell::new(Vec::new()), |
| 89 | last_mouse_selected: None, |
| 90 | locale, |
| 91 | custom_themes, |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// Construct behind type erasure before returning to the async event loop. |
| 96 | /// Keeping the concrete picker out of that already-large future prevents |
| 97 | /// transient modal values from inflating the main-thread stack frame. |
| 98 | #[must_use] |
| 99 | pub fn boxed( |
| 100 | original_name: String, |
| 101 | locale: Locale, |
| 102 | background_override: Option<Color>, |
| 103 | ) -> Box<dyn ModalView> { |
| 104 | Box::new(Self::new_with_background( |
| 105 | original_name, |
| 106 | locale, |
| 107 | background_override, |
| 108 | )) |
| 109 | } |
| 110 | |
| 111 | fn selected_theme_name(&self) -> &str { |
| 112 | self.controller |
| 113 | .selected_id() |
| 114 | .unwrap_or(ThemeId::System.name()) |
| 115 | } |
| 116 | |
| 117 | fn custom_theme_for(&self, selector: &str) -> Option<UiTheme> { |
| 118 | self.custom_themes |
| 119 | .iter() |
| 120 | .find(|option| option.selector == selector) |
| 121 | .map(|option| option.theme) |
| 122 | } |
| 123 | |
| 124 | #[cfg(test)] |
| 125 | fn current(&self) -> ThemeId { |
| 126 | let selected = self.selected_theme_name(); |
| 127 | self.custom_themes |
| 128 | .iter() |
| 129 | .find(|option| option.selector == selected) |
| 130 | .map(|option| option.base) |
| 131 | .or_else(|| ThemeId::from_name(selected)) |
| 132 | .unwrap_or(ThemeId::System) |
| 133 | } |
| 134 | |
| 135 | #[cfg(test)] |
| 136 | fn selected(&self) -> usize { |
| 137 | self.controller.selected_source_index().unwrap_or(0) |
| 138 | } |
| 139 | |
| 140 | /// Resolve a theme to a `UiTheme`, returning the cached `System` |
| 141 | /// resolution to avoid repeated env-var reads inside `render`. |
| 142 | fn ui_theme_for_selection(&self, selection: &str) -> UiTheme { |
| 143 | let theme = if let Some(custom) = self.custom_theme_for(selection) { |
| 144 | custom |
| 145 | } else if let Some(id) = ThemeId::from_name(selection) { |
| 146 | if matches!(id, ThemeId::System) { |
| 147 | self.system_ui_theme |
| 148 | } else { |
| 149 | id.ui_theme() |
| 150 | } |
| 151 | } else { |
| 152 | self.system_ui_theme |
| 153 | }; |
| 154 | self.background_override |
| 155 | .map_or(theme, |background| theme.with_background_color(background)) |
| 156 | } |
| 157 | |
| 158 | fn preview_event(&self) -> ViewAction { |
| 159 | ViewAction::Emit(ViewEvent::ThemeSelectionUpdated { |
| 160 | theme: self.selected_theme_name().to_string(), |
| 161 | persist: false, |
| 162 | }) |
| 163 | } |
| 164 | |
| 165 | fn commit_event(&self) -> ViewAction { |
| 166 | // A commit that never moved the cursor must preserve the exact |
| 167 | // opening selector. This also protects a custom:<name> selector if |
| 168 | // its file disappears or becomes invalid while the picker is open. |
| 169 | if self.controller.selected_source_index() == self.opening_cursor { |
| 170 | return ViewAction::EmitAndClose(ViewEvent::ThemeSelectionUpdated { |
| 171 | theme: self.original_theme_name.clone(), |
| 172 | persist: true, |
| 173 | }); |
| 174 | } |
| 175 | ViewAction::EmitAndClose(ViewEvent::ThemeSelectionUpdated { |
| 176 | theme: self.selected_theme_name().to_string(), |
| 177 | persist: true, |
| 178 | }) |
| 179 | } |
| 180 | |
| 181 | fn revert_event(&self) -> ViewAction { |
| 182 | ViewAction::EmitAndClose(ViewEvent::ThemeSelectionUpdated { |
| 183 | theme: self.original_theme_name.clone(), |
| 184 | persist: false, |
| 185 | }) |
| 186 | } |
| 187 | |
| 188 | fn action_from_nav(&self, result: PickerNavResult) -> ViewAction { |
| 189 | match result { |
| 190 | PickerNavResult::Preview => self.preview_event(), |
| 191 | PickerNavResult::Commit => self.commit_event(), |
| 192 | PickerNavResult::Cancel => self.revert_event(), |
| 193 | PickerNavResult::ItemAction | PickerNavResult::None => ViewAction::None, |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | fn move_up(&mut self) -> ViewAction { |
| 198 | let result = self.controller.move_up(); |
| 199 | self.action_from_nav(result) |
| 200 | } |
| 201 | |
| 202 | fn move_down(&mut self) -> ViewAction { |
| 203 | let result = self.controller.move_down(); |
| 204 | self.action_from_nav(result) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | fn theme_options( |
| 209 | current_name: &str, |
| 210 | ) -> (Vec<SettingOption>, Vec<codewhale_palette::UserThemeOption>) { |
| 211 | theme_options_with_custom(current_name, codewhale_palette::list_user_theme_options()) |
| 212 | } |
| 213 | |
| 214 | fn theme_options_with_custom( |
| 215 | current_name: &str, |
| 216 | custom_themes: Vec<codewhale_palette::UserThemeOption>, |
| 217 | ) -> (Vec<SettingOption>, Vec<codewhale_palette::UserThemeOption>) { |
| 218 | let current = current_name.trim().to_ascii_lowercase(); |
| 219 | let mut options = SELECTABLE_THEMES |
| 220 | .iter() |
| 221 | .copied() |
| 222 | .map(|id| { |
| 223 | let name = id.name(); |
| 224 | SettingOption::builder(name, id.display_name()) |
| 225 | .summary(id.tagline()) |
| 226 | .detail(id.tagline()) |
| 227 | .help("Pick a theme with live preview") |
| 228 | .values(SettingValues::new( |
| 229 | Cow::Owned(current.clone()), |
| 230 | // Reset uses the same default as fresh terminal settings. |
| 231 | Cow::Borrowed(DEFAULT_TUI_THEME), |
| 232 | Cow::Borrowed(name), |
| 233 | )) |
| 234 | .availability(SettingAvailability::Available) |
| 235 | .tab("themes") |
| 236 | .prefer_list_when_narrow(true) |
| 237 | .build() |
| 238 | }) |
| 239 | .collect::<Vec<_>>(); |
| 240 | for custom in &custom_themes { |
| 241 | let label = custom |
| 242 | .selector |
| 243 | .strip_prefix(codewhale_palette::USER_THEME_PREFIX) |
| 244 | .map_or_else(|| custom.selector.clone(), |slug| format!("Custom: {slug}")); |
| 245 | options.push( |
| 246 | SettingOption::builder(custom.selector.clone(), label) |
| 247 | .summary(format!( |
| 248 | "User overlay · based on {}", |
| 249 | custom.base.display_name() |
| 250 | )) |
| 251 | .detail(format!( |
| 252 | "User-authored overlay based on {}", |
| 253 | custom.base.display_name() |
| 254 | )) |
| 255 | .help("Pick a user-authored theme overlay") |
| 256 | .values(SettingValues::new( |
| 257 | Cow::Owned(current.clone()), |
| 258 | Cow::Borrowed(DEFAULT_TUI_THEME), |
| 259 | Cow::Owned(custom.selector.clone()), |
| 260 | )) |
| 261 | .availability(SettingAvailability::Available) |
| 262 | .tab("themes") |
| 263 | .prefer_list_when_narrow(true) |
| 264 | .build(), |
| 265 | ); |
| 266 | } |
| 267 | (options, custom_themes) |
| 268 | } |
| 269 | |
| 270 | impl ModalView for ThemePickerView { |
| 271 | fn kind(&self) -> ModalKind { |
| 272 | ModalKind::ThemePicker |
| 273 | } |
| 274 | |
| 275 | fn as_any_mut(&mut self) -> &mut dyn std::any::Any { |
| 276 | self |
| 277 | } |
| 278 | |
| 279 | fn handle_mouse(&mut self, mouse: MouseEvent) -> ViewAction { |
| 280 | match mouse.kind { |
| 281 | MouseEventKind::Moved => { |
| 282 | // Hover-follow with live preview: the pointer highlights a |
| 283 | // row exactly like ↑/↓ does, so the surface behind the modal |
| 284 | // repaints on hover and a later Enter persists the hovered |
| 285 | // theme. Returning the preview event (not None) is what makes |
| 286 | // the highlight repaint immediately. |
| 287 | let hovered = self.row_hitboxes.borrow().iter().find_map(|(rect, idx)| { |
| 288 | rect.contains(ratatui::layout::Position::new(mouse.column, mouse.row)) |
| 289 | .then_some(*idx) |
| 290 | }); |
| 291 | match hovered { |
| 292 | Some(idx) if self.controller.selected_source_index() != Some(idx) => { |
| 293 | let nav = self.controller.select_source_index(idx); |
| 294 | self.action_from_nav(nav) |
| 295 | } |
| 296 | _ => ViewAction::None, |
| 297 | } |
| 298 | } |
| 299 | MouseEventKind::ScrollUp => { |
| 300 | self.last_mouse_selected = None; |
| 301 | self.move_up() |
| 302 | } |
| 303 | MouseEventKind::ScrollDown => { |
| 304 | self.last_mouse_selected = None; |
| 305 | self.move_down() |
| 306 | } |
| 307 | MouseEventKind::Down(MouseButton::Left) => { |
| 308 | let clicked = self.row_hitboxes.borrow().iter().find_map(|(rect, idx)| { |
| 309 | rect.contains(ratatui::layout::Position::new(mouse.column, mouse.row)) |
| 310 | .then_some(*idx) |
| 311 | }); |
| 312 | if let Some(idx) = clicked { |
| 313 | let commit = self.last_mouse_selected == Some(idx) |
| 314 | && self.controller.selected_source_index() == Some(idx); |
| 315 | let nav = self.controller.select_source_index(idx); |
| 316 | self.last_mouse_selected = Some(idx); |
| 317 | if commit { |
| 318 | self.commit_event() |
| 319 | } else { |
| 320 | self.action_from_nav(nav) |
| 321 | } |
| 322 | } else { |
| 323 | ViewAction::None |
| 324 | } |
| 325 | } |
| 326 | _ => ViewAction::None, |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | fn handle_key(&mut self, key: KeyEvent) -> ViewAction { |
| 331 | // Theme picker keeps digit-jump / vim keys; search typing stays off so |
| 332 | // `j`/`k` and `1`..=`9` retain their navigation meaning. |
| 333 | let result = handle_nav_key(&mut self.controller, key, false); |
| 334 | self.action_from_nav(result) |
| 335 | } |
| 336 | |
| 337 | fn render(&self, area: Rect, buf: &mut Buffer) { |
| 338 | self.row_hitboxes.borrow_mut().clear(); |
| 339 | let live = self.ui_theme_for_selection(self.selected_theme_name()); |
| 340 | let _theme_scope = ModalThemeScope::enter(Some(live)); |
| 341 | let source_count = self.controller.visible().len(); |
| 342 | let selected_visible = self.controller.selected_visible(); |
| 343 | let pill = format!( |
| 344 | "{}/{}", |
| 345 | selected_visible.saturating_add(1).min(source_count.max(1)), |
| 346 | source_count |
| 347 | ); |
| 348 | let (inner, _) = render_surface_frame( |
| 349 | area, |
| 350 | buf, |
| 351 | SurfaceFrame::new(tr(self.locale, MessageId::ThemeSurfaceTitle)) |
| 352 | .theme(live) |
| 353 | .right_pill(pill, Style::default().fg(live.text_muted)), |
| 354 | ); |
| 355 | |
| 356 | let translate = |id| tr(self.locale, id); |
| 357 | let content = render_modal_footer( |
| 358 | inner, |
| 359 | buf, |
| 360 | &[ |
| 361 | ActionHint::new("↑/↓", translate(MessageId::ThemeActionPreview)), |
| 362 | ActionHint::new("Enter", translate(MessageId::RouterActionSave)), |
| 363 | ActionHint::new("Esc", translate(MessageId::ThemeActionRevert)), |
| 364 | ], |
| 365 | ); |
| 366 | |
| 367 | let layout = SettingsPickerLayout::resolve(content, 34, self.controller.selected_option()); |
| 368 | let show_detail = layout.detail.is_some(); |
| 369 | let list_area = layout.list; |
| 370 | |
| 371 | let visible_rows = usize::from(list_area.height).max(1); |
| 372 | let number_width = source_count.to_string().len(); |
| 373 | let max_start = source_count.saturating_sub(visible_rows); |
| 374 | let start = selected_visible |
| 375 | .saturating_sub(visible_rows.saturating_sub(1)) |
| 376 | .min(max_start); |
| 377 | let list_area = |
| 378 | render_panel_scroll_rail(list_area, buf, source_count, start, visible_rows, true); |
| 379 | |
| 380 | let mut lines: Vec<Line> = Vec::with_capacity(visible_rows); |
| 381 | let label_col_width = if show_detail { 18 } else { 20 }; |
| 382 | |
| 383 | for (visible_idx, &source_idx) in self |
| 384 | .controller |
| 385 | .visible() |
| 386 | .iter() |
| 387 | .enumerate() |
| 388 | .skip(start) |
| 389 | .take(visible_rows) |
| 390 | { |
| 391 | let row_y = list_area.y.saturating_add(lines.len() as u16); |
| 392 | let row_rect = Rect::new(list_area.x, row_y, list_area.width, 1); |
| 393 | self.row_hitboxes.borrow_mut().push((row_rect, source_idx)); |
| 394 | let option = self |
| 395 | .controller |
| 396 | .options() |
| 397 | .get(source_idx) |
| 398 | .expect("visible source index must reference an option"); |
| 399 | let is_selected = visible_idx == selected_visible; |
| 400 | let is_saved = option.id.as_ref() == self.original_theme_name; |
| 401 | if is_selected { |
| 402 | buf.set_style(row_rect, Style::default().bg(live.selection_bg)); |
| 403 | } |
| 404 | let row_style = if is_selected { |
| 405 | menu_style::theme_selected_row_style(&live) |
| 406 | } else { |
| 407 | Style::default().fg(live.text_body) |
| 408 | }; |
| 409 | let saved_style = if is_selected { |
| 410 | Style::default() |
| 411 | .fg(live.status_working) |
| 412 | .bg(live.selection_bg) |
| 413 | .add_modifier(Modifier::BOLD) |
| 414 | } else { |
| 415 | Style::default().fg(live.success) |
| 416 | }; |
| 417 | let tagline_style = if is_selected { |
| 418 | Style::default().fg(live.text_muted).bg(live.selection_bg) |
| 419 | } else { |
| 420 | Style::default().fg(live.text_dim) |
| 421 | }; |
| 422 | let number_style = if is_selected { |
| 423 | Style::default() |
| 424 | .fg(live.status_working) |
| 425 | .bg(live.selection_bg) |
| 426 | .add_modifier(Modifier::BOLD) |
| 427 | } else { |
| 428 | Style::default().fg(live.text_hint) |
| 429 | }; |
| 430 | let spacer_style = if is_selected { |
| 431 | Style::default().bg(live.selection_bg) |
| 432 | } else { |
| 433 | Style::default() |
| 434 | }; |
| 435 | let pointer = codewhale_tui_kit::glyphs::selection_marker(is_selected); |
| 436 | let saved_mark = if is_saved { |
| 437 | codewhale_tui_kit::glyphs::CURRENT |
| 438 | } else { |
| 439 | " " |
| 440 | }; |
| 441 | |
| 442 | let row_theme = self.ui_theme_for_selection(option.id.as_ref()); |
| 443 | let swatch_colors = match crate::tui::ocean::OceanRamp::for_theme(&row_theme) { |
| 444 | Some(ramp) => [ |
| 445 | ramp.surface, |
| 446 | ramp.middle, |
| 447 | ramp.deep, |
| 448 | ramp.ambient, |
| 449 | row_theme.status_working, |
| 450 | ], |
| 451 | None => [ |
| 452 | row_theme.surface_bg, |
| 453 | row_theme.panel_bg, |
| 454 | row_theme.status_working, |
| 455 | row_theme.mode_yolo, |
| 456 | row_theme.mode_plan, |
| 457 | ], |
| 458 | }; |
| 459 | let swatch = swatch_colors |
| 460 | .into_iter() |
| 461 | .map(|color| Span::styled(" ", Style::default().bg(color))); |
| 462 | |
| 463 | let label_text = codewhale_tui_kit::ui_text::semantic_truncate( |
| 464 | option.label.as_ref(), |
| 465 | label_col_width, |
| 466 | ); |
| 467 | let label_pad = " ".repeat( |
| 468 | label_col_width |
| 469 | .saturating_sub(codewhale_tui_kit::ui_text::text_display_width(&label_text)), |
| 470 | ); |
| 471 | |
| 472 | let mut spans: Vec<Span> = Vec::with_capacity(11); |
| 473 | spans.push(Span::styled(format!(" {pointer} "), row_style)); |
| 474 | spans.push(Span::styled(format!("{saved_mark} "), saved_style)); |
| 475 | spans.push(Span::styled( |
| 476 | format!("{:>number_width$}. ", visible_idx + 1), |
| 477 | number_style, |
| 478 | )); |
| 479 | spans.push(Span::styled(format!("{label_text}{label_pad}"), row_style)); |
| 480 | spans.extend(swatch); |
| 481 | |
| 482 | if !show_detail { |
| 483 | spans.push(Span::styled(" ", spacer_style)); |
| 484 | let prefix_width = Line::from(spans.clone()).width(); |
| 485 | let tagline = codewhale_tui_kit::ui_text::semantic_truncate( |
| 486 | option.summary.as_ref(), |
| 487 | usize::from(list_area.width).saturating_sub(prefix_width), |
| 488 | ); |
| 489 | spans.push(Span::styled(tagline, tagline_style)); |
| 490 | } |
| 491 | |
| 492 | lines.push(Line::from(spans)); |
| 493 | } |
| 494 | |
| 495 | Paragraph::new(lines).render(list_area, buf); |
| 496 | |
| 497 | if let Some(detail) = layout.detail { |
| 498 | let divider_x = layout.list.right(); |
| 499 | if divider_x < detail.x { |
| 500 | render_vertical_divider( |
| 501 | Rect::new(divider_x, detail.y, 1, detail.height), |
| 502 | buf, |
| 503 | live.border, |
| 504 | ); |
| 505 | } |
| 506 | let detail_inner = Rect { |
| 507 | x: detail.x.saturating_add(1), |
| 508 | width: detail.width.saturating_sub(1), |
| 509 | ..detail |
| 510 | }; |
| 511 | if detail_inner.width >= 16 |
| 512 | && detail_inner.height >= 4 |
| 513 | && let Some(option) = self.controller.selected_option() |
| 514 | { |
| 515 | let is_saved = option.id.as_ref() == self.original_theme_name; |
| 516 | let mut preview_lines: Vec<Line> = Vec::new(); |
| 517 | let mut header_spans = vec![Span::styled( |
| 518 | option.label.as_ref().to_string(), |
| 519 | Style::default() |
| 520 | .fg(live.accent_primary) |
| 521 | .add_modifier(Modifier::BOLD), |
| 522 | )]; |
| 523 | if is_saved { |
| 524 | header_spans.push(Span::raw(" ")); |
| 525 | header_spans.push(Span::styled( |
| 526 | format!( |
| 527 | "{} {}", |
| 528 | codewhale_tui_kit::glyphs::CURRENT, |
| 529 | translate(MessageId::CtxInspActive) |
| 530 | ), |
| 531 | Style::default().fg(live.success), |
| 532 | )); |
| 533 | } else { |
| 534 | header_spans.push(Span::raw(" ")); |
| 535 | header_spans.push(Span::styled( |
| 536 | translate(MessageId::ThemeLivePreview), |
| 537 | Style::default().fg(live.text_muted), |
| 538 | )); |
| 539 | } |
| 540 | preview_lines.push(Line::from(header_spans)); |
| 541 | preview_lines.push(Line::from(Span::styled( |
| 542 | option.detail.as_ref().to_string(), |
| 543 | Style::default().fg(live.text_body), |
| 544 | ))); |
| 545 | preview_lines.push(Line::from("")); |
| 546 | preview_lines.push(Line::from(Span::styled( |
| 547 | translate(MessageId::ThemePalette), |
| 548 | Style::default() |
| 549 | .fg(live.text_muted) |
| 550 | .add_modifier(Modifier::BOLD), |
| 551 | ))); |
| 552 | preview_lines.push(Line::from(vec![ |
| 553 | Span::styled(" ", Style::default().bg(live.surface_bg)), |
| 554 | Span::styled( |
| 555 | format!(" {} ", translate(MessageId::ThemeColorSurface)), |
| 556 | Style::default().fg(live.text_muted), |
| 557 | ), |
| 558 | Span::styled(" ", Style::default().bg(live.composer_bg)), |
| 559 | Span::styled( |
| 560 | format!(" {} ", translate(MessageId::ThemeColorPanel)), |
| 561 | Style::default().fg(live.text_muted), |
| 562 | ), |
| 563 | Span::styled(" ", Style::default().bg(live.selection_bg)), |
| 564 | Span::styled( |
| 565 | format!(" {}", translate(MessageId::ThemeColorSelection)), |
| 566 | Style::default().fg(live.text_muted), |
| 567 | ), |
| 568 | ])); |
| 569 | preview_lines.push(Line::from(vec![ |
| 570 | Span::styled( |
| 571 | codewhale_tui_kit::glyphs::CURRENT, |
| 572 | Style::default().fg(live.accent_primary), |
| 573 | ), |
| 574 | Span::styled( |
| 575 | format!(" {} ", translate(MessageId::ThemeColorAccent)), |
| 576 | Style::default().fg(live.text_body), |
| 577 | ), |
| 578 | Span::styled( |
| 579 | codewhale_tui_kit::glyphs::CURRENT, |
| 580 | Style::default().fg(live.success), |
| 581 | ), |
| 582 | Span::styled( |
| 583 | format!(" {} ", translate(MessageId::ThemeColorReady)), |
| 584 | Style::default().fg(live.text_body), |
| 585 | ), |
| 586 | Span::styled("◆", Style::default().fg(live.warning)), |
| 587 | Span::styled( |
| 588 | format!(" {} ", translate(MessageId::ThemeColorNotice)), |
| 589 | Style::default().fg(live.text_body), |
| 590 | ), |
| 591 | Span::styled("✕", Style::default().fg(live.error_fg)), |
| 592 | Span::styled( |
| 593 | format!(" {}", translate(MessageId::ThemeColorError)), |
| 594 | Style::default().fg(live.text_body), |
| 595 | ), |
| 596 | ])); |
| 597 | preview_lines.push(Line::from("")); |
| 598 | preview_lines.push(Line::from(Span::styled( |
| 599 | translate(MessageId::ThemeSample), |
| 600 | Style::default() |
| 601 | .fg(live.text_muted) |
| 602 | .add_modifier(Modifier::BOLD), |
| 603 | ))); |
| 604 | preview_lines.push(Line::from(vec![ |
| 605 | Span::styled( |
| 606 | format!("{} ", translate(MessageId::ThemeSamplePrimary)), |
| 607 | Style::default().fg(live.text_body), |
| 608 | ), |
| 609 | Span::styled( |
| 610 | format!("· {}", translate(MessageId::ThemeSampleMuted)), |
| 611 | Style::default().fg(live.text_muted), |
| 612 | ), |
| 613 | ])); |
| 614 | preview_lines.push(Line::from(Span::styled( |
| 615 | format!(" ▸ {} ", translate(MessageId::ThemeSampleHighlight)), |
| 616 | menu_style::theme_selected_row_style(&live), |
| 617 | ))); |
| 618 | Paragraph::new(preview_lines) |
| 619 | .wrap(Wrap { trim: false }) |
| 620 | .render(detail_inner, buf); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | #[cfg(test)] |
| 627 | mod tests { |
| 628 | use super::*; |
| 629 | use crossterm::event::{KeyCode, KeyModifiers}; |
| 630 | |
| 631 | fn key(code: KeyCode) -> KeyEvent { |
| 632 | KeyEvent::new(code, KeyModifiers::NONE) |
| 633 | } |
| 634 | |
| 635 | fn selected_values(action: &ViewAction) -> Option<(&str, bool)> { |
| 636 | match action { |
| 637 | ViewAction::Emit(ViewEvent::ThemeSelectionUpdated { theme, persist }) |
| 638 | | ViewAction::EmitAndClose(ViewEvent::ThemeSelectionUpdated { theme, persist }) => { |
| 639 | Some((theme.as_str(), *persist)) |
| 640 | } |
| 641 | _ => None, |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | fn selected_name(action: &ViewAction) -> Option<&str> { |
| 646 | selected_values(action).map(|(theme, _)| theme) |
| 647 | } |
| 648 | |
| 649 | #[test] |
| 650 | fn opens_at_persisted_theme() { |
| 651 | let v = ThemePickerView::new("tokyo-night".to_string()); |
| 652 | assert_eq!(v.current(), ThemeId::TokyoNight); |
| 653 | } |
| 654 | |
| 655 | #[test] |
| 656 | fn unknown_persisted_name_falls_back_to_first_row() { |
| 657 | let v = ThemePickerView::new("not-a-real-theme".to_string()); |
| 658 | assert_eq!(v.selected(), 0); |
| 659 | assert_eq!(v.current(), ThemeId::System); |
| 660 | } |
| 661 | |
| 662 | #[test] |
| 663 | fn arrow_down_previews_next_theme() { |
| 664 | let mut v = ThemePickerView::new("system".to_string()); |
| 665 | let action = v.handle_key(key(KeyCode::Down)); |
| 666 | assert!(matches!(action, ViewAction::Emit(_))); |
| 667 | assert_eq!(selected_name(&action), Some(ThemeId::Terminal.name())); |
| 668 | assert_eq!(selected_values(&action), Some(("terminal", false))); |
| 669 | } |
| 670 | |
| 671 | #[test] |
| 672 | fn mouse_wheel_previews_and_second_underwater_click_commits() { |
| 673 | let mut v = ThemePickerView::new("system".to_string()); |
| 674 | let wheel = v.handle_mouse(MouseEvent { |
| 675 | kind: MouseEventKind::ScrollDown, |
| 676 | column: 0, |
| 677 | row: 0, |
| 678 | modifiers: KeyModifiers::NONE, |
| 679 | }); |
| 680 | assert!(matches!(wheel, ViewAction::Emit(_))); |
| 681 | assert_eq!(selected_name(&wheel), Some(ThemeId::Terminal.name())); |
| 682 | |
| 683 | let area = Rect::new(0, 0, 100, 30); |
| 684 | let mut buf = Buffer::empty(area); |
| 685 | v.render(area, &mut buf); |
| 686 | let underwater_source = v |
| 687 | .controller |
| 688 | .options() |
| 689 | .iter() |
| 690 | .position(|option| option.id.as_ref() == ThemeId::Underwater.name()) |
| 691 | .expect("Underwater row"); |
| 692 | let (rect, idx) = v |
| 693 | .row_hitboxes |
| 694 | .borrow() |
| 695 | .iter() |
| 696 | .copied() |
| 697 | .find(|(_, source)| *source == underwater_source) |
| 698 | .expect("rendered Underwater hitbox"); |
| 699 | let click = MouseEvent { |
| 700 | kind: MouseEventKind::Down(MouseButton::Left), |
| 701 | column: rect.x, |
| 702 | row: rect.y, |
| 703 | modifiers: KeyModifiers::NONE, |
| 704 | }; |
| 705 | let preview = v.handle_mouse(click); |
| 706 | assert!(matches!(preview, ViewAction::Emit(_))); |
| 707 | assert_eq!(v.selected(), idx); |
| 708 | assert_eq!(selected_values(&preview), Some(("underwater", false))); |
| 709 | let commit = v.handle_mouse(click); |
| 710 | assert!(matches!(commit, ViewAction::EmitAndClose(_))); |
| 711 | assert_eq!(selected_values(&commit), Some(("underwater", true))); |
| 712 | } |
| 713 | |
| 714 | #[test] |
| 715 | fn hover_moves_highlight_and_previews_without_persisting() { |
| 716 | let mut v = ThemePickerView::new("system".to_string()); |
| 717 | let area = Rect::new(0, 0, 100, 30); |
| 718 | let mut buf = Buffer::empty(area); |
| 719 | v.render(area, &mut buf); |
| 720 | let underwater_source = v |
| 721 | .controller |
| 722 | .options() |
| 723 | .iter() |
| 724 | .position(|option| option.id.as_ref() == ThemeId::Underwater.name()) |
| 725 | .expect("Underwater row"); |
| 726 | let (rect, idx) = v |
| 727 | .row_hitboxes |
| 728 | .borrow() |
| 729 | .iter() |
| 730 | .copied() |
| 731 | .find(|(_, source)| *source == underwater_source) |
| 732 | .expect("rendered Underwater hitbox"); |
| 733 | let hover = MouseEvent { |
| 734 | kind: MouseEventKind::Moved, |
| 735 | column: rect.x, |
| 736 | row: rect.y, |
| 737 | modifiers: KeyModifiers::NONE, |
| 738 | }; |
| 739 | // Hovering a new row highlights it and previews (persist:false), |
| 740 | // exactly like keyboard navigation. |
| 741 | let action = v.handle_mouse(hover); |
| 742 | assert!(matches!(action, ViewAction::Emit(_))); |
| 743 | assert_eq!(selected_values(&action), Some(("underwater", false))); |
| 744 | assert_eq!(v.selected(), idx); |
| 745 | // Hovering the already-highlighted row is a no-op. |
| 746 | assert!(matches!(v.handle_mouse(hover), ViewAction::None)); |
| 747 | // Hovering outside every row is a no-op. |
| 748 | let outside = MouseEvent { |
| 749 | kind: MouseEventKind::Moved, |
| 750 | column: 99, |
| 751 | row: 29, |
| 752 | modifiers: KeyModifiers::NONE, |
| 753 | }; |
| 754 | assert!(matches!(v.handle_mouse(outside), ViewAction::None)); |
| 755 | } |
| 756 | |
| 757 | #[test] |
| 758 | fn arrow_navigation_wraps_at_picker_edges() { |
| 759 | let mut v = ThemePickerView::new("system".to_string()); |
| 760 | let last = SELECTABLE_THEMES.last().unwrap(); |
| 761 | |
| 762 | let action = v.handle_key(key(KeyCode::Up)); |
| 763 | assert_eq!(selected_name(&action), Some(last.name())); |
| 764 | |
| 765 | let action = v.handle_key(key(KeyCode::Down)); |
| 766 | assert_eq!(selected_name(&action), Some(SELECTABLE_THEMES[0].name())); |
| 767 | } |
| 768 | |
| 769 | #[test] |
| 770 | fn enter_commits_with_persist_true() { |
| 771 | let mut v = ThemePickerView::new("system".to_string()); |
| 772 | v.handle_key(key(KeyCode::Char('9'))); // -> Grayscale |
| 773 | let action = v.handle_key(key(KeyCode::Enter)); |
| 774 | match action { |
| 775 | ViewAction::EmitAndClose(ViewEvent::ThemeSelectionUpdated { theme, persist }) => { |
| 776 | assert_eq!(theme, ThemeId::Grayscale.name()); |
| 777 | assert!(persist); |
| 778 | } |
| 779 | other => panic!("expected commit, got {other:?}"), |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | #[test] |
| 784 | fn enter_without_navigating_preserves_a_custom_theme_selector() { |
| 785 | // The picker's rows are compiled themes only; a persisted |
| 786 | // custom:<name> selector opens on no row, and Enter without |
| 787 | // navigation must not replace it with a compiled row. |
| 788 | let mut v = ThemePickerView::new("custom:midnight".to_string()); |
| 789 | let action = v.handle_key(key(KeyCode::Enter)); |
| 790 | assert_eq!( |
| 791 | selected_values(&action), |
| 792 | Some(("custom:midnight", true)), |
| 793 | "committing without moving the cursor must not replace the persisted selector" |
| 794 | ); |
| 795 | } |
| 796 | |
| 797 | #[test] |
| 798 | fn custom_theme_rows_preview_and_commit_their_selector() { |
| 799 | let mut custom_theme = ThemeId::Whale.ui_theme(); |
| 800 | custom_theme.accent_primary = Color::Rgb(0x12, 0x34, 0x56); |
| 801 | let custom = codewhale_palette::UserThemeOption { |
| 802 | selector: "custom:midnight".to_string(), |
| 803 | base: ThemeId::Whale, |
| 804 | theme: custom_theme, |
| 805 | }; |
| 806 | let (options, custom_themes) = theme_options_with_custom("custom:midnight", vec![custom]); |
| 807 | let controller = SettingsPickerController::new(options, "custom:midnight"); |
| 808 | let view = ThemePickerView { |
| 809 | opening_cursor: controller.selected_source_index(), |
| 810 | controller, |
| 811 | original_theme_name: "custom:midnight".to_string(), |
| 812 | system_ui_theme: UiTheme::detect(), |
| 813 | background_override: None, |
| 814 | row_hitboxes: RefCell::new(Vec::new()), |
| 815 | last_mouse_selected: None, |
| 816 | locale: Locale::En, |
| 817 | custom_themes, |
| 818 | }; |
| 819 | |
| 820 | assert_eq!(view.controller.selected_id(), Some("custom:midnight")); |
| 821 | assert_eq!(view.current(), ThemeId::Whale); |
| 822 | assert_eq!( |
| 823 | view.ui_theme_for_selection("custom:midnight") |
| 824 | .accent_primary, |
| 825 | Color::Rgb(0x12, 0x34, 0x56) |
| 826 | ); |
| 827 | assert_eq!( |
| 828 | selected_values(&view.preview_event()), |
| 829 | Some(("custom:midnight", false)) |
| 830 | ); |
| 831 | assert_eq!( |
| 832 | selected_values(&view.commit_event()), |
| 833 | Some(("custom:midnight", true)) |
| 834 | ); |
| 835 | } |
| 836 | |
| 837 | #[test] |
| 838 | fn enter_after_navigating_away_still_commits_the_chosen_option() { |
| 839 | let mut v = ThemePickerView::new("dracula".to_string()); |
| 840 | v.handle_key(key(KeyCode::Down)); |
| 841 | let action = v.handle_key(key(KeyCode::Enter)); |
| 842 | let (theme, persist) = selected_values(&action).expect("expected a commit"); |
| 843 | assert_ne!( |
| 844 | theme, "dracula", |
| 845 | "navigation should have moved off the opening row" |
| 846 | ); |
| 847 | assert!(persist); |
| 848 | } |
| 849 | |
| 850 | #[test] |
| 851 | fn esc_reverts_to_exact_original_theme() { |
| 852 | let mut v = ThemePickerView::new("dracula".to_string()); |
| 853 | v.handle_key(key(KeyCode::Up)); |
| 854 | v.handle_key(key(KeyCode::Up)); |
| 855 | let action = v.handle_key(key(KeyCode::Esc)); |
| 856 | match action { |
| 857 | ViewAction::EmitAndClose(ViewEvent::ThemeSelectionUpdated { theme, persist }) => { |
| 858 | assert_eq!(theme, "dracula"); |
| 859 | assert!(!persist); |
| 860 | } |
| 861 | other => panic!("expected revert, got {other:?}"), |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | #[test] |
| 866 | fn digit_jumps_to_shoreline_and_previews() { |
| 867 | let mut v = ThemePickerView::new("system".to_string()); |
| 868 | let action = v.handle_key(key(KeyCode::Char('3'))); |
| 869 | // Shoreline follows System and Terminal. |
| 870 | assert_eq!(selected_values(&action), Some(("shoreline", false))); |
| 871 | } |
| 872 | |
| 873 | #[test] |
| 874 | fn digit_zero_is_rejected_not_remapped_to_row_zero() { |
| 875 | let mut v = ThemePickerView::new("dracula".to_string()); |
| 876 | let before = v.selected(); |
| 877 | let action = v.handle_key(key(KeyCode::Char('0'))); |
| 878 | assert!(matches!(action, ViewAction::None)); |
| 879 | assert_eq!(v.selected(), before, "'0' should not move the cursor"); |
| 880 | } |
| 881 | |
| 882 | #[test] |
| 883 | fn render_does_not_panic_on_zero_sized_area() { |
| 884 | // The picker historically panicked here via .max(W).max(H) floors |
| 885 | // that produced dimensions larger than the available area, then |
| 886 | // underflowed the centering arithmetic. |
| 887 | let v = ThemePickerView::new("system".to_string()); |
| 888 | let outer = ratatui::layout::Rect::new(0, 0, 10, 10); |
| 889 | let area = ratatui::layout::Rect::new(0, 0, 0, 0); |
| 890 | let mut buf = ratatui::buffer::Buffer::empty(outer); |
| 891 | v.render(area, &mut buf); |
| 892 | } |
| 893 | |
| 894 | #[test] |
| 895 | fn render_does_not_panic_on_tiny_area() { |
| 896 | // 20×6 is smaller than every soft floor the picker prefers. |
| 897 | let v = ThemePickerView::new("system".to_string()); |
| 898 | let area = ratatui::layout::Rect::new(0, 0, 20, 6); |
| 899 | let mut buf = ratatui::buffer::Buffer::empty(area); |
| 900 | v.render(area, &mut buf); |
| 901 | } |
| 902 | |
| 903 | #[test] |
| 904 | fn every_selectable_theme_previews_and_renders_through_the_same_surface() { |
| 905 | let area = ratatui::layout::Rect::new(0, 0, 100, 32); |
| 906 | let mut view = ThemePickerView::new("system".to_string()); |
| 907 | |
| 908 | for expected in SELECTABLE_THEMES.iter().copied() { |
| 909 | let index = view |
| 910 | .controller |
| 911 | .options() |
| 912 | .iter() |
| 913 | .position(|option| option.id.as_ref() == expected.name()) |
| 914 | .expect("selectable theme option"); |
| 915 | let _ = view.controller.select_source_index(index); |
| 916 | assert_eq!(view.current(), expected); |
| 917 | assert_eq!( |
| 918 | selected_values(&view.preview_event()), |
| 919 | Some((expected.name(), false)) |
| 920 | ); |
| 921 | |
| 922 | let mut buf = ratatui::buffer::Buffer::empty(area); |
| 923 | view.render(area, &mut buf); |
| 924 | let text = buf |
| 925 | .content() |
| 926 | .iter() |
| 927 | .map(|cell| cell.symbol()) |
| 928 | .collect::<String>(); |
| 929 | assert!( |
| 930 | text.contains(expected.display_name()), |
| 931 | "{} was not represented in its live preview surface", |
| 932 | expected.name() |
| 933 | ); |
| 934 | assert!(text.contains("Enter save")); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | #[test] |
| 939 | fn theme_preview_and_footer_use_the_selected_locale() { |
| 940 | let area = Rect::new(0, 0, 140, 32); |
| 941 | let mut view = ThemePickerView::new_with_background("system".to_string(), Locale::Fr, None); |
| 942 | view.handle_key(key(KeyCode::Down)); |
| 943 | let mut buf = Buffer::empty(area); |
| 944 | view.render(area, &mut buf); |
| 945 | let text = buf |
| 946 | .content() |
| 947 | .iter() |
| 948 | .map(|cell| cell.symbol()) |
| 949 | .collect::<String>(); |
| 950 | |
| 951 | assert!(text.contains("aperçu en direct"), "{text}"); |
| 952 | assert!(text.contains("Exemple"), "{text}"); |
| 953 | assert!(text.contains("Enter enregistrer"), "{text}"); |
| 954 | } |
| 955 | |
| 956 | #[test] |
| 957 | fn render_semantically_truncates_taglines_at_narrow_width() { |
| 958 | let v = ThemePickerView::new("system".to_string()); |
| 959 | let area = ratatui::layout::Rect::new(0, 0, 56, 12); |
| 960 | let mut buf = ratatui::buffer::Buffer::empty(area); |
| 961 | v.render(area, &mut buf); |
| 962 | let rows = (0..area.height) |
| 963 | .map(|y| { |
| 964 | (0..area.width) |
| 965 | .map(|x| buf[(x, y)].symbol()) |
| 966 | .collect::<String>() |
| 967 | }) |
| 968 | .collect::<Vec<_>>(); |
| 969 | let text = rows.join("\n"); |
| 970 | |
| 971 | assert!(text.contains('…'), "{text}"); |
| 972 | for (idx, row) in rows.iter().enumerate() { |
| 973 | assert!( |
| 974 | codewhale_tui_kit::ui_text::text_display_width(row) <= usize::from(area.width), |
| 975 | "line {idx} overflows: {row:?}" |
| 976 | ); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /// The four terminal sizes the v0.8.66 modal blocker (#3732) requires |
| 981 | /// every overlay to remain readable and fully operable at. |
| 982 | const BLOCKER_SIZES: [(u16, u16); 4] = [(80, 24), (100, 30), (120, 32), (160, 40)]; |
| 983 | |
| 984 | #[test] |
| 985 | fn theme_picker_is_usable_and_opaque_at_blocker_sizes() { |
| 986 | use crate::tui::views::ViewStack; |
| 987 | use ratatui::{buffer::Buffer, layout::Rect}; |
| 988 | use unicode_width::UnicodeWidthStr; |
| 989 | |
| 990 | for (w, h) in BLOCKER_SIZES { |
| 991 | let area = Rect::new(0, 0, w, h); |
| 992 | let mut buf = Buffer::empty(area); |
| 993 | for y in 0..h { |
| 994 | for x in 0..w { |
| 995 | buf[(x, y)].set_symbol("X"); |
| 996 | } |
| 997 | } |
| 998 | let mut stack = ViewStack::new(); |
| 999 | stack.push(ThemePickerView::new("system".to_string())); |
| 1000 | stack.render(area, &mut buf); |
| 1001 | |
| 1002 | let rows: Vec<String> = (0..h) |
| 1003 | .map(|y| { |
| 1004 | (0..w) |
| 1005 | .map(|x| buf[(x, y)].symbol().to_string()) |
| 1006 | .collect::<String>() |
| 1007 | }) |
| 1008 | .collect(); |
| 1009 | let text = rows.join("\n"); |
| 1010 | |
| 1011 | for label in ["preview", "save", "revert"] { |
| 1012 | assert!(text.contains(label), "{w}x{h}: missing footer '{label}'"); |
| 1013 | } |
| 1014 | assert!( |
| 1015 | !text.contains('X'), |
| 1016 | "{w}x{h}: background bleed-through into modal surface" |
| 1017 | ); |
| 1018 | // The theme picker paints the *live* theme surface (not the shared |
| 1019 | // ink), so assert the center cell is painted (no surviving |
| 1020 | // sentinel) rather than checking a fixed background color. |
| 1021 | assert_ne!( |
| 1022 | buf[(w / 2, h / 2)].symbol(), |
| 1023 | "X", |
| 1024 | "{w}x{h}: modal interior must be painted" |
| 1025 | ); |
| 1026 | for (y, row) in rows.iter().enumerate() { |
| 1027 | assert!( |
| 1028 | UnicodeWidthStr::width(row.trim_end()) <= w as usize, |
| 1029 | "{w}x{h}: row {y} overflows width: {row:?}" |
| 1030 | ); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | #[test] |
| 1036 | fn theme_picker_uses_shared_settings_controller() { |
| 1037 | let v = ThemePickerView::new("dracula".to_string()); |
| 1038 | assert_eq!(v.controller.original_id(), "dracula"); |
| 1039 | assert_eq!(v.controller.selected_id(), Some("dracula")); |
| 1040 | // One row per selectable theme: no modifier rows beside them. |
| 1041 | assert_eq!(v.controller.visible().len(), SELECTABLE_THEMES.len()); |
| 1042 | } |
| 1043 | |
| 1044 | /// Goldens are stored without cell padding: every row is right-trimmed |
| 1045 | /// and trailing empty rows are dropped, so `git diff --check` stays |
| 1046 | /// clean. |
| 1047 | fn trim_golden_rows(text: &str) -> String { |
| 1048 | let mut rows: Vec<&str> = text.lines().map(str::trim_end).collect(); |
| 1049 | while rows.last().is_some_and(|row| row.is_empty()) { |
| 1050 | rows.pop(); |
| 1051 | } |
| 1052 | let mut out = rows.join("\n"); |
| 1053 | out.push('\n'); |
| 1054 | out |
| 1055 | } |
| 1056 | |
| 1057 | /// Slice C: cell-exact goldens for the picker surface with the default |
| 1058 | /// theme selected — 14 rows plus the preview footer. A visual change |
| 1059 | /// that cannot show as a golden diff did not happen. Re-bless with |
| 1060 | /// `CODEWHALE_BLESS_GOLDENS=1`. |
| 1061 | #[test] |
| 1062 | fn theme_picker_matches_goldens_at_blocker_sizes() { |
| 1063 | use crate::tui::golden_harness::{assert_matches_golden, render_golden_text}; |
| 1064 | for (w, h) in [(80u16, 24u16), (120u16, 32u16)] { |
| 1065 | let rendered = render_golden_text(w, h, |buf| { |
| 1066 | ThemePickerView::new("underwater".to_string()).render(Rect::new(0, 0, w, h), buf); |
| 1067 | }); |
| 1068 | assert_matches_golden( |
| 1069 | &format!("theme_picker_{w}x{h}"), |
| 1070 | &trim_golden_rows(&rendered), |
| 1071 | ); |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 |