| 1 | use std::time::Duration; |
| 2 | |
| 3 | use codewhale_palette::UiTheme; |
| 4 | use ratatui::{ |
| 5 | buffer::Buffer, |
| 6 | layout::Rect, |
| 7 | style::{Modifier, Style}, |
| 8 | }; |
| 9 | use tachyonfx::{EffectRenderer, Interpolation, fx}; |
| 10 | use unicode_width::UnicodeWidthStr; |
| 11 | |
| 12 | pub(super) const PULSE: Duration = Duration::from_millis(240); |
| 13 | |
| 14 | pub(super) fn is_shortcut(chord: &str) -> bool { |
| 15 | !chord.is_empty() && !chord.starts_with(['/', '@']) |
| 16 | } |
| 17 | |
| 18 | fn key_face(key: &str, ascii: bool) -> String { |
| 19 | let key = if ascii { |
| 20 | match key { |
| 21 | "⌥" => "Alt", |
| 22 | "↑" => "Up", |
| 23 | "↓" => "Down", |
| 24 | "←" => "Left", |
| 25 | "→" => "Right", |
| 26 | _ => key, |
| 27 | } |
| 28 | } else { |
| 29 | key |
| 30 | }; |
| 31 | format!("[{key}]") |
| 32 | } |
| 33 | |
| 34 | fn alternatives(label: &str, ascii: bool) -> Vec<String> { |
| 35 | let mut alternatives = Vec::new(); |
| 36 | for chord in label.split(" / ").filter(|chord| is_shortcut(chord)) { |
| 37 | let chord = chord.split(" (").next().unwrap_or(chord).replace('⌥', "⌥+"); |
| 38 | let sequence = chord.split_whitespace().collect::<Vec<_>>(); |
| 39 | let Some((&last, prior)) = sequence.split_last() else { |
| 40 | continue; |
| 41 | }; |
| 42 | let mut prefix = String::new(); |
| 43 | for press in prior { |
| 44 | prefix.push_str(&key_face(press, ascii)); |
| 45 | prefix.push_str(if ascii { " > " } else { " → " }); |
| 46 | } |
| 47 | let (modifiers, key) = last.rsplit_once('+').unwrap_or(("", last)); |
| 48 | for modifier in modifiers.split('+').filter(|part| !part.is_empty()) { |
| 49 | prefix.push_str(&key_face(modifier, ascii)); |
| 50 | prefix.push('+'); |
| 51 | } |
| 52 | if key == "/" { |
| 53 | alternatives.push(format!("{prefix}{}", key_face(key, ascii))); |
| 54 | } else { |
| 55 | for key in key.split('/') { |
| 56 | let face = match key.split_once('-') { |
| 57 | Some((low, high)) if low.len() == 1 && high.len() == 1 => { |
| 58 | let separator = if ascii { "-" } else { "–" }; |
| 59 | format!( |
| 60 | "{}{separator}{}", |
| 61 | key_face(low, ascii), |
| 62 | key_face(high, ascii) |
| 63 | ) |
| 64 | } |
| 65 | _ => key_face(key, ascii), |
| 66 | }; |
| 67 | alternatives.push(format!("{prefix}{face}")); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | alternatives |
| 72 | } |
| 73 | |
| 74 | pub(super) fn paint( |
| 75 | chord: &str, |
| 76 | area: Rect, |
| 77 | buf: &mut Buffer, |
| 78 | theme: &UiTheme, |
| 79 | elapsed: Option<Duration>, |
| 80 | ascii: bool, |
| 81 | ) { |
| 82 | let area = area.intersection(buf.area); |
| 83 | if area.is_empty() { |
| 84 | return; |
| 85 | } |
| 86 | let style = Style::new() |
| 87 | .bg(theme.elevated_bg) |
| 88 | .fg(codewhale_palette::enforce_contrast( |
| 89 | theme.text_body, |
| 90 | theme.elevated_bg, |
| 91 | 4.5, |
| 92 | )) |
| 93 | .add_modifier(Modifier::BOLD); |
| 94 | let mut x = area.left(); |
| 95 | for chord in alternatives(chord, ascii) { |
| 96 | let width = chord.width(); |
| 97 | let gap = if x == area.left() { 0 } else { 3 }; |
| 98 | if width + gap > usize::from(area.right() - x) { |
| 99 | continue; |
| 100 | } |
| 101 | if gap > 0 { |
| 102 | buf.set_string(x, area.y, " / ", Style::new().fg(theme.text_muted)); |
| 103 | x += 3; |
| 104 | } |
| 105 | buf.set_string(x, area.y, &chord, style); |
| 106 | if !ascii && let Some(age) = elapsed.filter(|age| *age < PULSE) { |
| 107 | let ink = |
| 108 | codewhale_palette::enforce_contrast(theme.accent_secondary, theme.elevated_bg, 4.5); |
| 109 | let mut press = fx::fade_from_fg(ink, (PULSE, Interpolation::QuadOut)); |
| 110 | buf.render_effect(&mut press, Rect::new(x, area.y, width as u16, 1), age); |
| 111 | } |
| 112 | x += width as u16; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #[cfg(test)] |
| 117 | mod tests { |
| 118 | use super::*; |
| 119 | |
| 120 | #[test] |
| 121 | fn chord_faces_preserve_alternatives_modifiers_and_sequences() { |
| 122 | assert_eq!( |
| 123 | alternatives("⌥Shift+G / Ctrl+←/→ / Esc Esc / Ctrl+/ / /help", true), |
| 124 | [ |
| 125 | "[Alt]+[Shift]+[G]", |
| 126 | "[Ctrl]+[Left]", |
| 127 | "[Ctrl]+[Right]", |
| 128 | "[Esc] > [Esc]", |
| 129 | "[Ctrl]+[/]" |
| 130 | ] |
| 131 | ); |
| 132 | assert_eq!(alternatives("⌥1-8 / @path", false), ["[⌥]+[1]–[8]"]); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn press_changes_only_ink_and_finishes_at_the_static_frame() { |
| 137 | let area = Rect::new(2, 1, 17, 1); |
| 138 | let render = |age, ascii| { |
| 139 | let mut buf = Buffer::empty(Rect::new(0, 0, 32, 3)); |
| 140 | paint( |
| 141 | "Ctrl+Home / Ctrl+End", |
| 142 | area, |
| 143 | &mut buf, |
| 144 | &codewhale_palette::UI_THEME, |
| 145 | age, |
| 146 | ascii, |
| 147 | ); |
| 148 | buf |
| 149 | }; |
| 150 | let rest = render(None, false); |
| 151 | let first = render(Some(Duration::ZERO), false); |
| 152 | let mid = render(Some(PULSE / 2), false); |
| 153 | assert_ne!(first[(2, 1)].fg, rest[(2, 1)].fg); |
| 154 | assert_ne!(first[(2, 1)].fg, mid[(2, 1)].fg); |
| 155 | for x in 0..32 { |
| 156 | assert_eq!(first[(x, 1)].symbol(), rest[(x, 1)].symbol()); |
| 157 | } |
| 158 | assert_eq!(first[(14, 1)].symbol(), "]"); |
| 159 | assert_eq!(first[(15, 1)].symbol(), " "); |
| 160 | assert_eq!(render(Some(PULSE), false), rest); |
| 161 | assert_eq!(render(Some(Duration::ZERO), true), render(None, true)); |
| 162 | } |
| 163 | } |
| 164 |