edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·fbc29d1·Tahinli·2 months ago·972 commits
| 1 | //! The keymap: which stroke means which action, and the file it survives in. |
| 2 | //! |
| 3 | //! ```text |
| 4 | //! edith-keys 1 |
| 5 | //! play space |
| 6 | //! delete x |
| 7 | //! delete delete |
| 8 | //! save ctrl+s |
| 9 | //! ``` |
| 10 | //! |
| 11 | //! One line per binding, so an action bound to two strokes is two lines and the |
| 12 | //! format needs no list syntax. A chord is `key` or `ctrl+key`, where `key` is |
| 13 | //! exactly what gpui reports for the stroke (`escape`, not `esc`) -- the lookup |
| 14 | //! compares against that string and nothing translates in between. What a |
| 15 | //! *person* reads is [`Keymap::display`]'s business, and only there. |
| 16 | //! |
| 17 | //! The parser is strict and everything it cannot use names its 1-based line, |
| 18 | //! like the project file. Only the first line refuses the whole file: a binding |
| 19 | //! line the parser cannot use is *dropped*, named in the notice, and every other |
| 20 | //! line still stands -- one unusable line is not a reason to throw away the |
| 21 | //! rebinds around it. A missing file is not corrupt either -- it means nobody |
| 22 | //! has changed a key yet, and the defaults stand. |
| 23 | //! |
| 24 | //! Writing mirrors `engine::edith::save`: `.part`, fsync, rename, directory |
| 25 | //! fsync, so an interrupted save cannot cost the bindings that were already |
| 26 | //! there. |
| 27 | |
| 28 | // The UI consumes this module in a later step; until then nothing calls it and |
| 29 | // every item here reads as dead. |
| 30 | #![allow(dead_code)] |
| 31 | |
| 32 | use std::ffi::OsString; |
| 33 | use std::io::Write; |
| 34 | use std::path::{Path, PathBuf}; |
| 35 | |
| 36 | /// First line, exactly. The version is part of it, as in the project file. |
| 37 | const MAGIC: &str = "edith-keys 1"; |
| 38 | |
| 39 | /// The action list, written once: the enum and the display-order array come out |
| 40 | /// of the same names, so an action added here is in [`ActionId::ALL`] by |
| 41 | /// construction. It used to be two lists, and a variant added to one of them |
| 42 | /// compiled, passed every test, and was simply missing from the keys card -- |
| 43 | /// the pointer-unreachable action this editor opened with. |
| 44 | macro_rules! actions { |
| 45 | ($($(#[$note:meta])* $variant:ident),+ $(,)?) => { |
| 46 | /// Everything a stroke can ask for. Not the mouse's actions: only what a |
| 47 | /// key can reach is bindable. |
| 48 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 49 | pub enum ActionId { |
| 50 | $($(#[$note])* $variant,)+ |
| 51 | } |
| 52 | |
| 53 | impl ActionId { |
| 54 | /// Display order everywhere -- the editor lists them in it and |
| 55 | /// [`Keymap::defaults`] binds them in it. Derived from the list |
| 56 | /// above, so there is no second place to forget. |
| 57 | pub const ALL: [ActionId; [$(stringify!($variant)),+].len()] = |
| 58 | [$(ActionId::$variant),+]; |
| 59 | } |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | actions! { |
| 64 | Play, |
| 65 | /// Whether the active session -- the timeline or a library preview -- |
| 66 | /// restarts from the top on reaching its end instead of stopping there. |
| 67 | /// Live exactly where Play is: a transport with nothing under it has |
| 68 | /// nothing to loop either. |
| 69 | Loop, |
| 70 | StepBack, |
| 71 | StepForward, |
| 72 | JumpBack, |
| 73 | JumpForward, |
| 74 | GoStart, |
| 75 | GoEnd, |
| 76 | /// The two that put the playhead on a *source* sync point -- the frames a |
| 77 | /// cut may be placed on for an export to copy the film's own coded pictures |
| 78 | /// instead of decoding and coding every one of them again. Playback keys by |
| 79 | /// what they do (they move the playhead and nothing else) and editing keys |
| 80 | /// by what they are for. |
| 81 | PrevSyncPoint, |
| 82 | NextSyncPoint, |
| 83 | /// Walks to the next cut (DESIGN.md §6): the odometer the cut readout |
| 84 | /// counts against. |
| 85 | WalkCutNext, |
| 86 | /// The pair's other half, one cut earlier. |
| 87 | WalkCutPrev, |
| 88 | /// [`ActionId::WalkCutNext`]'s stride-ten (DESIGN.md §6's "shift"): its |
| 89 | /// own action, not a modifier read off `WalkCutNext`, because gpui 0.2.2 |
| 90 | /// never delivers a shift modifier alongside `.`/`,` (see [`Chord`]'s |
| 91 | /// doc) -- the *only* channel a stroke has back to [`Player::act`] is |
| 92 | /// which [`ActionId`] the keymap resolved it to, so the stride the |
| 93 | /// platform can't spell as a modifier is spelled as its own bound key |
| 94 | /// (`>`, what shift+. actually types) instead. |
| 95 | WalkCutNext10, |
| 96 | /// The pair's other half (`<`, what shift+, actually types). |
| 97 | WalkCutPrev10, |
| 98 | /// Marks the playhead as an export's in point. The out point wraps around |
| 99 | /// it if the mark lands past the current out ([`ordered_range`]). |
| 100 | SetIn, |
| 101 | /// The out point's pair. Ordered the same way against whatever in point |
| 102 | /// stands already. |
| 103 | SetOut, |
| 104 | /// Drops the mark: an export goes back to the whole timeline. |
| 105 | ClearRange, |
| 106 | Export, |
| 107 | Save, |
| 108 | Copy, |
| 109 | Paste, |
| 110 | Cut, |
| 111 | Regroup, |
| 112 | Detach, |
| 113 | Group, |
| 114 | Select, |
| 115 | SelectNext, |
| 116 | SelectPrev, |
| 117 | /// Every placement on every lane in one selection -- the companion of the |
| 118 | /// ctrl-click that builds a selection by hand, for the times everything |
| 119 | /// wants selecting at once. |
| 120 | SelectAll, |
| 121 | /// Bare escape with nothing else claiming it: empties a multi-clip |
| 122 | /// selection without touching picks, cards or fullscreen. |
| 123 | Deselect, |
| 124 | Delete, |
| 125 | Lift, |
| 126 | Color, |
| 127 | /// Where a clip sits, how big and which way up |
| 128 | /// ([`crate::transform_ui::TRANSFORM_BANDS`]) -- the geometry twin of |
| 129 | /// [`ActionId::Color`], filed right beside it. |
| 130 | Transform, |
| 131 | Fit, |
| 132 | Resolution, |
| 133 | ZoomIn, |
| 134 | ZoomOut, |
| 135 | ZoomFit, |
| 136 | Undo, |
| 137 | Redo, |
| 138 | AddVideoLane, |
| 139 | AddAudioLane, |
| 140 | RemoveVideoLane, |
| 141 | RemoveAudioLane, |
| 142 | AddSubtitleLane, |
| 143 | RemoveSubtitleLane, |
| 144 | /// The palette's own door: a file's subtitle tracks read into the list the |
| 145 | /// panel shows, which is where a lane's words are dragged *from*. Adding a |
| 146 | /// track to that list places nothing -- the pair above it is what makes the |
| 147 | /// timeline row a caption can land on. |
| 148 | ImportSubtitles, |
| 149 | /// The dock's own `Add files` verb (MOCK-SPEC "Dock" IMPORT section): |
| 150 | /// the file chooser, same door [`crate::Player::pick_and_import`] and a |
| 151 | /// drop on the window already use. |
| 152 | AddFiles, |
| 153 | /// The dock's `Paste path` verb: imports whatever file the OS clipboard |
| 154 | /// names ([`crate::Player::paste_file_path`]). |
| 155 | PasteFilePath, |
| 156 | ToggleMute, |
| 157 | VolumeUp, |
| 158 | VolumeDown, |
| 159 | Equalizer, |
| 160 | Speed, |
| 161 | Silence, |
| 162 | /// Paints a sliding waveform of this audio clip onto a video lane, so the |
| 163 | /// sound is a picture on the same timeline — preview and export both. |
| 164 | Visualizer, |
| 165 | /// Crossfades the selected audio clip into its neighbour ([`Project::crossfade`]): |
| 166 | /// two adjacent clips picked on one lane, or a single pick faded into the |
| 167 | /// clip right after it. |
| 168 | Crossfade, |
| 169 | /// No-aim trim of the subject cut's in point, frame detents, at any zoom |
| 170 | /// (DESIGN.md §6), on its designed bare `[`. [`ActionId::SelectPrev`] |
| 171 | /// moved to `{` (what shift+[ actually types) to free it -- see |
| 172 | /// keymap.rs's own defaults. |
| 173 | TrimIn, |
| 174 | /// The pair's other half, the subject cut's out point, on its designed |
| 175 | /// bare `]` -- [`ActionId::SelectNext`] moved to `}` (what shift+] |
| 176 | /// actually types) for the same reason. |
| 177 | TrimOut, |
| 178 | /// Loop-trim: loops around the subject cut while trimming it (DESIGN.md |
| 179 | /// §6, "the modernized Avid trim mode"). |
| 180 | LoopTrim, |
| 181 | /// The keyboard's own version of the pointer's drag-the-edge-to-a-spot |
| 182 | /// trim (debt #42: trim used to be pointer-only): snaps the subject |
| 183 | /// cut's in point straight to the playhead, one press, one undo step -- |
| 184 | /// [`ActionId::TrimIn`]'s frame-detent nudge covers the other case. Bare |
| 185 | /// `[`/`{`/ctrl+`[` are all spoken for (nudge, select-prev, sync point), |
| 186 | /// so this takes the next rung of the same bracket family: ctrl held on |
| 187 | /// the shifted bracket, `ctrl+{` (what ctrl+shift+[ actually types). |
| 188 | TrimInToPlayhead, |
| 189 | /// The pair's other half, the subject cut's out point, on `ctrl+}`. |
| 190 | TrimOutToPlayhead, |
| 191 | /// Dissolves the selected video clip into its neighbour |
| 192 | /// ([`Project::set_transition_out`]): two adjacent clips picked on one |
| 193 | /// lane, or a single pick dissolved into the clip right after it. A |
| 194 | /// second press on a clip that already dissolves removes it instead. |
| 195 | Dissolve, |
| 196 | Mix, |
| 197 | ToggleSnap, |
| 198 | ToggleSubtitles, |
| 199 | /// Cut on the stand-ins or on the films themselves |
| 200 | /// ([`engine::proxy`]): what the picture is decoded from, and nothing |
| 201 | /// else -- the sound and every export stay the film's whichever way it |
| 202 | /// is set. |
| 203 | ToggleProxies, |
| 204 | /// Whether a film that wants a stand-in gets one made as it is imported |
| 205 | /// ([`engine::proxy::wanted`]): what is *encoded*, where the switch above |
| 206 | /// is what is decoded. With it off the switch above is also the ask -- it |
| 207 | /// starts the ones the project is missing. |
| 208 | ToggleAutoProxies, |
| 209 | /// The window's own colours, which belong to the person looking at them and |
| 210 | /// not to the project: a list of the palettes, opened here and from the |
| 211 | /// toolbar's Theme button. It was a build feature once, which made it a |
| 212 | /// choice only whoever compiled the binary could make. |
| 213 | Theme, |
| 214 | /// The whole window, borderless over the screen: a player's own control, |
| 215 | /// on the platform's own toggle ([`gpui::Window::toggle_fullscreen`]). |
| 216 | Fullscreen, |
| 217 | CancelExport, |
| 218 | /// Opens the actions card -- the list every other action is on. A door of |
| 219 | /// its own, because a card reachable only by a button in the panel is one a |
| 220 | /// hand on the keyboard has to leave the keyboard for. |
| 221 | ShowActions, |
| 222 | /// Saves the frame on screen to a PNG: whatever is showing, the timeline's |
| 223 | /// own picture or a library preview's, since both write through the one |
| 224 | /// `self.image` the atlas already holds. |
| 225 | Screenshot, |
| 226 | /// The cue plate's font and size -- a person's own preference, kept |
| 227 | /// beside the theme and not the project, and opened the same way. |
| 228 | SubtitleStyle, |
| 229 | /// The settings page: PROJECT rows (resolution, fps, sample-rate, HDR |
| 230 | /// tonemap, the mix) beside EDITOR rows (proxies, auto-proxies, subtitle |
| 231 | /// font) -- one door, two clearly-headed sections, per the split rule |
| 232 | /// that "anything that changes the rendered output or lives in the |
| 233 | /// project file is PROJECT; anything about this machine, this person, |
| 234 | /// this window is EDITOR." Always answerable, same as `Theme`: it is |
| 235 | /// about the editor and the project's own numbers, not about a clip. |
| 236 | Settings, |
| 237 | /// Moves the keyboard ring onto the dock (`ui::stance::Surface::Dock`, |
| 238 | /// the cycle's first stop): the door into keyboard-only navigation |
| 239 | /// (`ui::stance`'s Tab/Shift-Tab ring), which nothing else in the app |
| 240 | /// opens. Bare Tab stays [`ActionId::Select`] at the root the whole time |
| 241 | /// -- this only starts the ring, it never steals the clip-selection key. |
| 242 | FocusPanels, |
| 243 | } |
| 244 | |
| 245 | impl ActionId { |
| 246 | /// What a person calls it. |
| 247 | pub fn label(self) -> &'static str { |
| 248 | match self { |
| 249 | ActionId::Play => "Play / Pause", |
| 250 | ActionId::Loop => "Loop playback", |
| 251 | ActionId::StepBack => "One frame back", |
| 252 | ActionId::StepForward => "One frame forward", |
| 253 | ActionId::JumpBack => "One second back", |
| 254 | ActionId::JumpForward => "One second forward", |
| 255 | ActionId::GoStart => "Go to start", |
| 256 | ActionId::GoEnd => "Go to end", |
| 257 | ActionId::PrevSyncPoint => "Previous sync point", |
| 258 | ActionId::NextSyncPoint => "Next sync point", |
| 259 | ActionId::WalkCutNext => "Next cut", |
| 260 | ActionId::WalkCutPrev => "Previous cut", |
| 261 | ActionId::WalkCutNext10 => "Forward ten cuts", |
| 262 | ActionId::WalkCutPrev10 => "Back ten cuts", |
| 263 | ActionId::SetIn => "Mark in", |
| 264 | ActionId::SetOut => "Mark out", |
| 265 | ActionId::ClearRange => "Clear range", |
| 266 | ActionId::Export => "Export", |
| 267 | ActionId::Save => "Save", |
| 268 | ActionId::Copy => "Copy", |
| 269 | ActionId::Paste => "Paste", |
| 270 | ActionId::Cut => "Cut", |
| 271 | ActionId::Regroup => "Regroup", |
| 272 | // Neither half's action: it takes the *pair* apart, and the menu |
| 273 | // that offers it hangs on whichever half was right-clicked -- worded |
| 274 | // from the picture's side it read as a video item on an audio clip. |
| 275 | ActionId::Detach => "Ungroup", |
| 276 | ActionId::Group => "Group", |
| 277 | ActionId::Select => "Select clip at playhead", |
| 278 | ActionId::SelectNext => "Select next clip", |
| 279 | ActionId::SelectPrev => "Select previous clip", |
| 280 | ActionId::SelectAll => "Select all", |
| 281 | ActionId::Deselect => "Deselect", |
| 282 | ActionId::Delete => "Delete", |
| 283 | ActionId::Lift => "Lift", |
| 284 | ActionId::Color => "Colour…", |
| 285 | ActionId::Transform => "Transform…", |
| 286 | ActionId::Fit => "Fit policy", |
| 287 | ActionId::Resolution => "Project resolution", |
| 288 | ActionId::ZoomIn => "Zoom in", |
| 289 | ActionId::ZoomOut => "Zoom out", |
| 290 | ActionId::ZoomFit => "Fit timeline", |
| 291 | ActionId::Undo => "Undo", |
| 292 | ActionId::Redo => "Redo", |
| 293 | ActionId::AddVideoLane => "Add video track", |
| 294 | ActionId::AddAudioLane => "Add audio track", |
| 295 | ActionId::RemoveVideoLane => "Remove video track", |
| 296 | ActionId::RemoveAudioLane => "Remove audio track", |
| 297 | ActionId::AddSubtitleLane => "Add subtitle track", |
| 298 | ActionId::RemoveSubtitleLane => "Remove subtitle track", |
| 299 | ActionId::ImportSubtitles => "Add subtitles…", |
| 300 | ActionId::AddFiles => "Add files…", |
| 301 | ActionId::PasteFilePath => "Paste path", |
| 302 | ActionId::ToggleMute => "Mute / Unmute", |
| 303 | ActionId::VolumeUp => "Volume up", |
| 304 | ActionId::VolumeDown => "Volume down", |
| 305 | ActionId::Equalizer => "Equalizer", |
| 306 | ActionId::Speed => "Speed…", |
| 307 | ActionId::Silence => "Silences…", |
| 308 | ActionId::Visualizer => "Create visualizer", |
| 309 | ActionId::Crossfade => "Crossfade to next", |
| 310 | ActionId::TrimIn => "Trim in", |
| 311 | ActionId::TrimOut => "Trim out", |
| 312 | ActionId::LoopTrim => "Loop-trim", |
| 313 | ActionId::TrimInToPlayhead => "Trim in to playhead", |
| 314 | ActionId::TrimOutToPlayhead => "Trim out to playhead", |
| 315 | ActionId::Dissolve => "Dissolve to next", |
| 316 | ActionId::Mix => "Mix…", |
| 317 | ActionId::ToggleSnap => "Snap", |
| 318 | ActionId::ToggleSubtitles => "Subtitles", |
| 319 | ActionId::ToggleProxies => "Proxies", |
| 320 | ActionId::ToggleAutoProxies => "Proxies on import", |
| 321 | ActionId::Theme => "Theme…", |
| 322 | ActionId::Fullscreen => "Fullscreen", |
| 323 | ActionId::CancelExport => "Cancel export", |
| 324 | ActionId::ShowActions => "All actions…", |
| 325 | ActionId::Screenshot => "Save frame as PNG", |
| 326 | ActionId::SubtitleStyle => "Subtitle style…", |
| 327 | ActionId::Settings => "Settings…", |
| 328 | ActionId::FocusPanels => "Focus panels", |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /// What the file calls it: ASCII, no spaces, one spelling per variant and |
| 333 | /// never the label, which is free to be reworded. |
| 334 | fn name(self) -> &'static str { |
| 335 | match self { |
| 336 | ActionId::Play => "play", |
| 337 | ActionId::Loop => "loop", |
| 338 | ActionId::StepBack => "step-back", |
| 339 | ActionId::StepForward => "step-forward", |
| 340 | ActionId::JumpBack => "jump-back", |
| 341 | ActionId::JumpForward => "jump-forward", |
| 342 | ActionId::GoStart => "go-start", |
| 343 | ActionId::GoEnd => "go-end", |
| 344 | ActionId::PrevSyncPoint => "prev-sync-point", |
| 345 | ActionId::NextSyncPoint => "next-sync-point", |
| 346 | ActionId::WalkCutNext => "walk-cut-next", |
| 347 | ActionId::WalkCutPrev => "walk-cut-prev", |
| 348 | ActionId::WalkCutNext10 => "walk-cut-next-10", |
| 349 | ActionId::WalkCutPrev10 => "walk-cut-prev-10", |
| 350 | ActionId::SetIn => "set-in", |
| 351 | ActionId::SetOut => "set-out", |
| 352 | ActionId::ClearRange => "clear-range", |
| 353 | ActionId::Export => "export", |
| 354 | ActionId::Save => "save", |
| 355 | ActionId::Copy => "copy", |
| 356 | ActionId::Paste => "paste", |
| 357 | ActionId::Cut => "cut", |
| 358 | ActionId::Regroup => "regroup", |
| 359 | ActionId::Detach => "detach", |
| 360 | ActionId::Group => "group", |
| 361 | ActionId::Select => "select", |
| 362 | ActionId::SelectNext => "select-next", |
| 363 | ActionId::SelectPrev => "select-prev", |
| 364 | ActionId::SelectAll => "select-all", |
| 365 | ActionId::Deselect => "deselect", |
| 366 | ActionId::Delete => "delete", |
| 367 | ActionId::Lift => "lift", |
| 368 | ActionId::Color => "color", |
| 369 | ActionId::Transform => "transform", |
| 370 | ActionId::Fit => "fit", |
| 371 | ActionId::Resolution => "resolution", |
| 372 | ActionId::ZoomIn => "zoom-in", |
| 373 | ActionId::ZoomOut => "zoom-out", |
| 374 | ActionId::ZoomFit => "zoom-fit", |
| 375 | ActionId::Undo => "undo", |
| 376 | ActionId::Redo => "redo", |
| 377 | ActionId::AddVideoLane => "add-video-lane", |
| 378 | ActionId::AddAudioLane => "add-audio-lane", |
| 379 | ActionId::RemoveVideoLane => "remove-video-lane", |
| 380 | ActionId::RemoveAudioLane => "remove-audio-lane", |
| 381 | ActionId::AddSubtitleLane => "add-subtitle-lane", |
| 382 | ActionId::RemoveSubtitleLane => "remove-subtitle-lane", |
| 383 | ActionId::ImportSubtitles => "import-subtitles", |
| 384 | ActionId::AddFiles => "add-files", |
| 385 | ActionId::PasteFilePath => "paste-file-path", |
| 386 | ActionId::ToggleMute => "toggle-mute", |
| 387 | ActionId::VolumeUp => "volume-up", |
| 388 | ActionId::VolumeDown => "volume-down", |
| 389 | ActionId::Equalizer => "equalizer", |
| 390 | ActionId::Speed => "speed", |
| 391 | ActionId::Silence => "silence", |
| 392 | ActionId::Visualizer => "visualizer", |
| 393 | ActionId::Crossfade => "crossfade", |
| 394 | ActionId::TrimIn => "trim-in", |
| 395 | ActionId::TrimOut => "trim-out", |
| 396 | ActionId::LoopTrim => "loop-trim", |
| 397 | ActionId::TrimInToPlayhead => "trim-in-to-playhead", |
| 398 | ActionId::TrimOutToPlayhead => "trim-out-to-playhead", |
| 399 | ActionId::Dissolve => "dissolve", |
| 400 | ActionId::Mix => "mix", |
| 401 | ActionId::ToggleSnap => "toggle-snap", |
| 402 | ActionId::ToggleSubtitles => "toggle-subtitles", |
| 403 | ActionId::ToggleProxies => "toggle-proxies", |
| 404 | ActionId::ToggleAutoProxies => "toggle-auto-proxies", |
| 405 | ActionId::Theme => "theme", |
| 406 | ActionId::Fullscreen => "fullscreen", |
| 407 | ActionId::CancelExport => "cancel-export", |
| 408 | ActionId::ShowActions => "show-actions", |
| 409 | ActionId::Screenshot => "screenshot", |
| 410 | ActionId::SubtitleStyle => "subtitle-style", |
| 411 | ActionId::Settings => "settings", |
| 412 | ActionId::FocusPanels => "focus-panels", |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | fn from_name(name: &str) -> Option<Self> { |
| 417 | Self::ALL.into_iter().find(|a| a.name() == name) |
| 418 | } |
| 419 | |
| 420 | /// Which heading the keys menu files it under. Every action has one, so a |
| 421 | /// new action is listed the moment it exists -- there is no second list to |
| 422 | /// forget. |
| 423 | pub fn category(self) -> Category { |
| 424 | match self { |
| 425 | ActionId::Play |
| 426 | | ActionId::Loop |
| 427 | | ActionId::StepBack |
| 428 | | ActionId::StepForward |
| 429 | | ActionId::JumpBack |
| 430 | | ActionId::JumpForward |
| 431 | | ActionId::GoStart |
| 432 | | ActionId::GoEnd |
| 433 | | ActionId::PrevSyncPoint |
| 434 | | ActionId::NextSyncPoint |
| 435 | | ActionId::WalkCutNext |
| 436 | | ActionId::WalkCutPrev |
| 437 | | ActionId::WalkCutNext10 |
| 438 | | ActionId::WalkCutPrev10 |
| 439 | | ActionId::SetIn |
| 440 | | ActionId::SetOut |
| 441 | | ActionId::ClearRange => Category::Playback, |
| 442 | ActionId::Copy |
| 443 | | ActionId::Paste |
| 444 | | ActionId::Select |
| 445 | | ActionId::SelectNext |
| 446 | | ActionId::SelectPrev |
| 447 | | ActionId::SelectAll |
| 448 | | ActionId::Deselect |
| 449 | | ActionId::Delete |
| 450 | | ActionId::Lift |
| 451 | | ActionId::Color |
| 452 | | ActionId::Transform |
| 453 | | ActionId::Fit |
| 454 | // A rate is the clip's, not the sound's: it re-times the picture |
| 455 | // and the sound together, and the card opens on whichever half was |
| 456 | // clicked. |
| 457 | | ActionId::Speed |
| 458 | // The scan reads a clip's sound, but what it does is edit the |
| 459 | // timeline the clip is on -- it is a clip card like the three |
| 460 | // above it, opened on whichever half was picked. |
| 461 | | ActionId::Silence |
| 462 | | ActionId::Visualizer |
| 463 | | ActionId::Crossfade |
| 464 | | ActionId::TrimIn |
| 465 | | ActionId::TrimOut |
| 466 | | ActionId::LoopTrim |
| 467 | | ActionId::TrimInToPlayhead |
| 468 | | ActionId::TrimOutToPlayhead |
| 469 | | ActionId::Dissolve => Category::Clips, |
| 470 | // The project's own picture size is not a clip's business, and not |
| 471 | // a file operation either: it is what the viewer is looking at. |
| 472 | // Neither is how much of the timeline the panel shows: a zoom edits |
| 473 | // nothing, it magnifies. |
| 474 | // ...and whether the cues are drawn over that picture: a subtitle |
| 475 | // edits nothing either, it is part of what is being watched. |
| 476 | ActionId::Resolution |
| 477 | | ActionId::ZoomIn |
| 478 | | ActionId::ZoomOut |
| 479 | | ActionId::ZoomFit |
| 480 | | ActionId::ToggleSubtitles |
| 481 | // ...and which file the picture is decoded from, which changes |
| 482 | // nothing that is saved and nothing that is exported: a stand-in |
| 483 | // is what one watches while cutting, not what one delivers. |
| 484 | | ActionId::ToggleProxies |
| 485 | // ...and whether those stand-ins are made without being asked: the |
| 486 | // other half of the same switch, filed beside it so a card reader |
| 487 | // meets the pair the panel shows as a pair. |
| 488 | | ActionId::ToggleAutoProxies |
| 489 | // ...and what the whole window is painted in: it edits nothing at |
| 490 | // all, it is what one is looking *with*. |
| 491 | | ActionId::Theme |
| 492 | // ...and what the cue plate is drawn in: a person's own reading |
| 493 | // preference, same as the theme beside it. |
| 494 | | ActionId::SubtitleStyle |
| 495 | // ...and whether that window fills the screen: a way of looking, |
| 496 | // same as the zoom and the theme beside it. |
| 497 | | ActionId::Fullscreen |
| 498 | // The settings page: it changes no clip and no lane, only the |
| 499 | // project's own numbers and this window's preferences. |
| 500 | | ActionId::Settings |
| 501 | // Moves the ring, not a clip or a lane: the same "how one is |
| 502 | // looking/reaching, not what is being edited" shape as the rest |
| 503 | // of this heading. |
| 504 | | ActionId::FocusPanels => Category::View, |
| 505 | ActionId::Cut |
| 506 | | ActionId::Regroup |
| 507 | | ActionId::Detach |
| 508 | | ActionId::Group |
| 509 | | ActionId::Undo |
| 510 | | ActionId::Redo |
| 511 | | ActionId::AddVideoLane |
| 512 | | ActionId::AddAudioLane |
| 513 | | ActionId::RemoveVideoLane |
| 514 | // Not a view setting despite being a pointer aid: it decides where |
| 515 | // a drag actually lands, which is an edit and not a magnification. |
| 516 | | ActionId::ToggleSnap |
| 517 | // The subtitle pair is a track pair like the lanes above it: what |
| 518 | // they add is on the timeline and goes into the file an export |
| 519 | // writes, where the toggle two headings down only decides whether |
| 520 | // this window draws it. |
| 521 | | ActionId::AddSubtitleLane |
| 522 | | ActionId::RemoveSubtitleLane |
| 523 | // ...and the door that fills the palette those lanes are dragged |
| 524 | // from: it edits no lane, but what it brings in is the project's |
| 525 | // and is saved with it, which is not a view setting. |
| 526 | | ActionId::ImportSubtitles |
| 527 | | ActionId::RemoveAudioLane => Category::Editing, |
| 528 | ActionId::ToggleMute |
| 529 | | ActionId::VolumeUp |
| 530 | | ActionId::VolumeDown |
| 531 | | ActionId::Equalizer |
| 532 | // The mix is the project's sound -- what every track plays at and |
| 533 | // what the whole of it is held under -- where the three above it |
| 534 | // are this machine's monitoring. Both are audio, and the card says |
| 535 | // which is which. |
| 536 | | ActionId::Mix => Category::Audio, |
| 537 | // Under the heading the panel button sits in, beside the save and |
| 538 | // the export it stands next to on screen. |
| 539 | ActionId::Save |
| 540 | | ActionId::Export |
| 541 | | ActionId::CancelExport |
| 542 | | ActionId::ShowActions |
| 543 | // It writes a file like the three above it, and touches no edit |
| 544 | // list -- what an export's own allow-list already means it stays |
| 545 | // out of, not what puts it in this heading. |
| 546 | | ActionId::Screenshot |
| 547 | // The dock's import pair brings a file *in*, the same heading a |
| 548 | // file going *out* (Export, Save) already sits under. |
| 549 | | ActionId::AddFiles |
| 550 | | ActionId::PasteFilePath => Category::File, |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /// A heading in the keys menu, and the order the headings come in. |
| 556 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 557 | pub enum Category { |
| 558 | Playback, |
| 559 | Editing, |
| 560 | Clips, |
| 561 | Audio, |
| 562 | File, |
| 563 | View, |
| 564 | } |
| 565 | |
| 566 | impl Category { |
| 567 | pub const ALL: [Category; 6] = [ |
| 568 | Category::Playback, |
| 569 | Category::Editing, |
| 570 | Category::Clips, |
| 571 | Category::Audio, |
| 572 | Category::File, |
| 573 | Category::View, |
| 574 | ]; |
| 575 | |
| 576 | pub fn label(self) -> &'static str { |
| 577 | match self { |
| 578 | Category::Playback => "Playback", |
| 579 | Category::Editing => "Editing", |
| 580 | Category::Clips => "Clips", |
| 581 | Category::Audio => "Audio", |
| 582 | Category::File => "File", |
| 583 | Category::View => "View", |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /// A stroke that works but nobody may rebind: the modal cards read the keyboard |
| 589 | /// themselves, and what closes a card must not be a thing a user can take away. |
| 590 | /// Listed here anyway, because the keys menu shows every stroke that works -- |
| 591 | /// `no_stroke_is_missing_from_the_keys_menu` reads the key handler's own source |
| 592 | /// and fails on any key that reaches neither this table nor a binding. |
| 593 | pub struct Fixed { |
| 594 | /// As a person reads it, which for a single key is [`Chord::pretty`]'s |
| 595 | /// spelling and for a family of keys is the family (`0–9`). A `String` |
| 596 | /// because one of them is generated: the codec row's keys are the export |
| 597 | /// card's own table, not a line typed a second time here. |
| 598 | pub chord: String, |
| 599 | pub label: &'static str, |
| 600 | pub category: Category, |
| 601 | /// What a hand with only a mouse does instead. Every stroke here is |
| 602 | /// card-local, and a card whose rows only a keyboard can reach is half this |
| 603 | /// editor's users locked out of it, so each row names the thing on its card |
| 604 | /// -- `every_action_is_reachable_without_the_keyboard` looks each id up in |
| 605 | /// the render's own source. |
| 606 | pub reach: Reach, |
| 607 | } |
| 608 | |
| 609 | /// The pointer's answer to a fixed stroke. |
| 610 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 611 | pub enum Reach { |
| 612 | /// The id of the element on the card that does the same thing by click or |
| 613 | /// by drag. |
| 614 | Click(&'static str), |
| 615 | /// Not an action a pointer takes: the way *out* of a card, and the hold |
| 616 | /// that only repeats what a drag already does continuously. A variant |
| 617 | /// rather than an omission, so a row with nothing to click is a decision |
| 618 | /// somebody wrote down instead of a line missing from a list. |
| 619 | Gesture, |
| 620 | } |
| 621 | |
| 622 | pub static FIXED: std::sync::LazyLock<[Fixed; 28]> = std::sync::LazyLock::new(|| { |
| 623 | [ |
| 624 | // Not a chord at all but a way of pressing one, and the only place the |
| 625 | // editor can say so: holding a key that moves a *value* runs it, and |
| 626 | // holding anything else still does what one press did. |
| 627 | Fixed { |
| 628 | chord: "hold ← → ↑ ↓".into(), |
| 629 | label: "Run a card's slider, or the volume and zoom keys, while held", |
| 630 | category: Category::View, |
| 631 | reach: Reach::Gesture, |
| 632 | }, |
| 633 | Fixed { |
| 634 | chord: "esc".into(), |
| 635 | label: "Close this card or menu, or cancel a capture", |
| 636 | category: Category::View, |
| 637 | reach: Reach::Gesture, |
| 638 | }, |
| 639 | // An open choice list is driven by the keyboard as well as clicked: the |
| 640 | // list is the door to a setting, and a door only a pointer opens is one |
| 641 | // half of this editor's users cannot use. |
| 642 | Fixed { |
| 643 | chord: "↑ ↓ enter".into(), |
| 644 | label: "Move through an open choice list, and take the row", |
| 645 | category: Category::View, |
| 646 | reach: Reach::Click("picker-row"), |
| 647 | }, |
| 648 | Fixed { |
| 649 | chord: "n".into(), |
| 650 | label: "Say the export budget: 850k, 6.5M, 6.5 or 1.2G of file", |
| 651 | category: Category::File, |
| 652 | reach: Reach::Click("budget"), |
| 653 | }, |
| 654 | Fixed { |
| 655 | chord: "− / + / ← →".into(), |
| 656 | label: "Move the export budget — shift steps whole megabits", |
| 657 | category: Category::File, |
| 658 | reach: Reach::Click("budget-track"), |
| 659 | }, |
| 660 | Fixed { |
| 661 | chord: "0–9".into(), |
| 662 | label: "Type into an open field — the budget, or a clip's duration", |
| 663 | category: Category::File, |
| 664 | reach: Reach::Click("budget"), |
| 665 | }, |
| 666 | Fixed { |
| 667 | chord: "backspace".into(), |
| 668 | label: "Erase a character in the budget or duration field", |
| 669 | category: Category::File, |
| 670 | reach: Reach::Click("budget"), |
| 671 | }, |
| 672 | Fixed { |
| 673 | chord: "d".into(), |
| 674 | label: "Choose where the export is written", |
| 675 | category: Category::File, |
| 676 | reach: Reach::Click("destination"), |
| 677 | }, |
| 678 | Fixed { |
| 679 | chord: "enter".into(), |
| 680 | label: "Start the export — or take the budget being typed", |
| 681 | category: Category::File, |
| 682 | reach: Reach::Click("export-confirm"), |
| 683 | }, |
| 684 | // The equalizer card's own input, for the same reason the export card has |
| 685 | // its own: a band nothing but a drag can reach is a band half the users of |
| 686 | // this editor cannot move at all. Card-local, so none of them is bindable. |
| 687 | Fixed { |
| 688 | chord: "1–9, 0".into(), |
| 689 | label: "Pick an equalizer band", |
| 690 | category: Category::Audio, |
| 691 | reach: Reach::Click("eq-graph"), |
| 692 | }, |
| 693 | Fixed { |
| 694 | chord: "up".into(), |
| 695 | label: "Raise the picked band 1 dB", |
| 696 | category: Category::Audio, |
| 697 | reach: Reach::Click("eq-gain-up"), |
| 698 | }, |
| 699 | Fixed { |
| 700 | chord: "down".into(), |
| 701 | label: "Lower the picked band 1 dB", |
| 702 | category: Category::Audio, |
| 703 | reach: Reach::Click("eq-gain-down"), |
| 704 | }, |
| 705 | Fixed { |
| 706 | chord: "← / →".into(), |
| 707 | label: "Move the picked band down or up in frequency", |
| 708 | category: Category::Audio, |
| 709 | reach: Reach::Click("eq-freq-up"), |
| 710 | }, |
| 711 | Fixed { |
| 712 | chord: "shift ← / →".into(), |
| 713 | label: "Widen or narrow the picked band (its Q)", |
| 714 | category: Category::Audio, |
| 715 | reach: Reach::Click("eq-q-wider"), |
| 716 | }, |
| 717 | Fixed { |
| 718 | chord: "a".into(), |
| 719 | label: "Add an equalizer band beside the picked one", |
| 720 | category: Category::Audio, |
| 721 | reach: Reach::Click("eq-add"), |
| 722 | }, |
| 723 | Fixed { |
| 724 | chord: "x".into(), |
| 725 | label: "Remove the picked equalizer band", |
| 726 | category: Category::Audio, |
| 727 | reach: Reach::Click("eq-remove"), |
| 728 | }, |
| 729 | Fixed { |
| 730 | chord: "f".into(), |
| 731 | label: "Flatten the picked band (double-click does the same)", |
| 732 | category: Category::Audio, |
| 733 | reach: Reach::Click("eq-flat-band"), |
| 734 | }, |
| 735 | Fixed { |
| 736 | chord: "r".into(), |
| 737 | label: "Flatten every band", |
| 738 | category: Category::Audio, |
| 739 | reach: Reach::Click("eq-reset"), |
| 740 | }, |
| 741 | Fixed { |
| 742 | chord: "s".into(), |
| 743 | label: "Show or hide the spectrum behind the curve", |
| 744 | category: Category::Audio, |
| 745 | reach: Reach::Click("eq-spectrum"), |
| 746 | }, |
| 747 | // The colour card's own three, which mean nothing outside it -- the same |
| 748 | // card-local input the export card's digits are. |
| 749 | Fixed { |
| 750 | chord: "↑ / ↓".into(), |
| 751 | label: "Pick a colour slider", |
| 752 | category: Category::Clips, |
| 753 | reach: Reach::Click("color-row"), |
| 754 | }, |
| 755 | Fixed { |
| 756 | chord: "← / →".into(), |
| 757 | label: "Move the picked colour slider", |
| 758 | category: Category::Clips, |
| 759 | reach: Reach::Click("color-bar"), |
| 760 | }, |
| 761 | Fixed { |
| 762 | chord: "r".into(), |
| 763 | label: "Take the colour grade off the clip", |
| 764 | category: Category::Clips, |
| 765 | reach: Reach::Click("color-reset"), |
| 766 | }, |
| 767 | // The transform card's own three, the same shape as the colour card's: |
| 768 | // up/down pick a band (position, scale, rotate, crop), left/right move |
| 769 | // it, and `r` resets to identity. |
| 770 | Fixed { |
| 771 | chord: "↑ / ↓".into(), |
| 772 | label: "Pick a transform band", |
| 773 | category: Category::Clips, |
| 774 | reach: Reach::Click("transform-row"), |
| 775 | }, |
| 776 | Fixed { |
| 777 | chord: "← / →".into(), |
| 778 | label: "Move the picked transform band", |
| 779 | category: Category::Clips, |
| 780 | reach: Reach::Click("transform-bar"), |
| 781 | }, |
| 782 | Fixed { |
| 783 | chord: "r".into(), |
| 784 | label: "Take the transform off the clip", |
| 785 | category: Category::Clips, |
| 786 | reach: Reach::Click("transform-reset"), |
| 787 | }, |
| 788 | // The silence card's two apply keys. Card-local like every stroke above -- |
| 789 | // they mean nothing while it is closed -- but the card is the one place in |
| 790 | // this editor where a key rewrites forty places at once, so both of them |
| 791 | // are listed rather than hidden in the card's own hint line. |
| 792 | Fixed { |
| 793 | chord: "enter".into(), |
| 794 | label: "Cut every silence the card found", |
| 795 | category: Category::Clips, |
| 796 | reach: Reach::Click("silence-apply"), |
| 797 | }, |
| 798 | Fixed { |
| 799 | chord: "f".into(), |
| 800 | label: "Speed the silences up instead of cutting them", |
| 801 | category: Category::Clips, |
| 802 | reach: Reach::Click("silence-apply"), |
| 803 | }, |
| 804 | Fixed { |
| 805 | chord: "k".into(), |
| 806 | label: "Keep only the silences the card found", |
| 807 | category: Category::Clips, |
| 808 | reach: Reach::Click("silence-keep-only"), |
| 809 | }, |
| 810 | ] |
| 811 | }); |
| 812 | |
| 813 | /// A stroke as the key handler sees it: gpui's key name plus the one |
| 814 | /// modifier this editor binds. Nothing else is a chord here -- alt is part |
| 815 | /// of the key name gpui reports, and a second modifier would need the file |
| 816 | /// format to grow before it could be spelled. |
| 817 | /// |
| 818 | /// There is no `shift` field. gpui 0.2.2 (linux/platform.rs:909-917) clears |
| 819 | /// the shift modifier for any single-char key whose upper and lower case are |
| 820 | /// the same -- every symbol key (`,` `.` `[` `]` `/` `=` ...) -- so a chord |
| 821 | /// carrying `shift` could never be struck for any of them; the shifted |
| 822 | /// stroke arrives instead as the key it types (`shift+.` is the key `>`). |
| 823 | /// The one place this editor wanted shift on a symbol (the odometer's |
| 824 | /// stride-ten, DESIGN.md §6) is bound on that literal key instead |
| 825 | /// (`keymap.rs`'s own `defaults`), and a non-symbol key that wants shift |
| 826 | /// (the EQ card's arrows) reads `event.keystroke.modifiers.shift` straight |
| 827 | /// off the keystroke, upstream of this struct entirely -- so the field had |
| 828 | /// zero production callers and is gone rather than kept dead. |
| 829 | #[derive(Clone, PartialEq, Eq, Debug)] |
| 830 | pub struct Chord { |
| 831 | pub key: String, |
| 832 | pub ctrl: bool, |
| 833 | } |
| 834 | |
| 835 | impl Chord { |
| 836 | /// The file's spelling, and the only one [`parse`] accepts back. |
| 837 | fn text(&self) -> String { |
| 838 | if self.ctrl { |
| 839 | format!("ctrl+{}", self.key) |
| 840 | } else { |
| 841 | self.key.clone() |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | /// The spelling a person reads. Identical to [`Chord::text`] but for the one |
| 846 | /// key whose usual name is shorter than gpui's. Public because a |
| 847 | /// keybindings row shows one chord, where [`Keymap::display`] shows every |
| 848 | /// chord an action answers to. |
| 849 | pub fn pretty(&self) -> String { |
| 850 | let key = if self.key == "escape" { |
| 851 | "esc" |
| 852 | } else { |
| 853 | &self.key |
| 854 | }; |
| 855 | if self.ctrl { |
| 856 | format!("ctrl+{key}") |
| 857 | } else { |
| 858 | key.to_string() |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /// The spelling a badge with room for one token reads (MOCK-SPEC.md |
| 863 | /// "Spine": `del`, `z`, not `x or delete`, `ctrl+z`): `^` in place of |
| 864 | /// `ctrl+`, and the handful of key names whose everyday short form isn't |
| 865 | /// gpui's own (`delete` -> `del`, `escape` -> `esc`, `space` -> `spc`). |
| 866 | /// [`Chord::pretty`] stays the keys overlay's own spelling -- this is for |
| 867 | /// a glyph badge, not the full truth. |
| 868 | pub fn compact(&self) -> String { |
| 869 | let key = match self.key.as_str() { |
| 870 | "escape" => "esc", |
| 871 | "delete" => "del", |
| 872 | "space" => "spc", |
| 873 | other => other, |
| 874 | }; |
| 875 | if self.ctrl { |
| 876 | format!("^{key}") |
| 877 | } else { |
| 878 | key.to_string() |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | /// Whether the file can carry this stroke and read it back as itself. gpui |
| 883 | /// reports strokes this format cannot spell -- shift+= arrives as the key |
| 884 | /// `"+"`, which is the chord grammar's own separator -- and binding one |
| 885 | /// would write a line the next load has to drop. Asked before a capture is |
| 886 | /// taken, so a stroke that cannot be saved is refused while the user is |
| 887 | /// still looking at the row. |
| 888 | pub fn bindable(&self) -> bool { |
| 889 | Chord::parse(&self.text(), 0).is_ok_and(|round_trip| round_trip == *self) |
| 890 | } |
| 891 | |
| 892 | /// Strict: exactly one optional `ctrl+`, then a key gpui could have |
| 893 | /// reported. A spelling this cannot emit back unchanged is refused |
| 894 | /// rather than normalised, because the round-trip is what makes the file |
| 895 | /// editable by hand at all. |
| 896 | fn parse(text: &str, n: usize) -> Result<Self, String> { |
| 897 | let (ctrl, key) = match text.strip_prefix("ctrl+") { |
| 898 | Some(rest) => (true, rest), |
| 899 | None => (false, text), |
| 900 | }; |
| 901 | if key.is_empty() |
| 902 | || key.contains(' ') |
| 903 | || key.contains('+') |
| 904 | || key.chars().any(|c| c.is_ascii_uppercase()) |
| 905 | { |
| 906 | return Err(format!("line {n}: {text:?} is not a chord")); |
| 907 | } |
| 908 | Ok(Chord { |
| 909 | key: key.to_string(), |
| 910 | ctrl, |
| 911 | }) |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | /// One stroke, one action. Two strokes for the same action are two bindings. |
| 916 | #[derive(Clone, PartialEq, Eq, Debug)] |
| 917 | pub struct Binding { |
| 918 | pub action: ActionId, |
| 919 | pub chord: Chord, |
| 920 | } |
| 921 | |
| 922 | /// Every binding in force, in display order. No chord appears twice -- that is |
| 923 | /// the invariant [`parse`] defends, and it is what lets [`Keymap::lookup`] |
| 924 | /// answer with the first match. |
| 925 | #[derive(Clone, PartialEq, Eq, Debug)] |
| 926 | pub struct Keymap { |
| 927 | bindings: Vec<Binding>, |
| 928 | } |
| 929 | |
| 930 | impl Keymap { |
| 931 | /// The bindings the editor has always had, in the order the editor lists |
| 932 | /// them. Delete and undo get two strokes each because both have always |
| 933 | /// answered to two. |
| 934 | pub fn defaults() -> Self { |
| 935 | let b = |action, key: &str, ctrl| Binding { |
| 936 | action, |
| 937 | chord: Chord { |
| 938 | key: key.to_string(), |
| 939 | ctrl, |
| 940 | }, |
| 941 | }; |
| 942 | Keymap { |
| 943 | bindings: vec![ |
| 944 | b(ActionId::Play, "space", false), |
| 945 | // "r" for repeat, free and bare: its ctrl chord is already the |
| 946 | // project resolution, and the two read as a pair the way the |
| 947 | // mute and its level do. |
| 948 | b(ActionId::Loop, "r", false), |
| 949 | // Seeking without a pointer, on the keys every player already |
| 950 | // seeks with (gpui names them "left"/"right"/"home"/"end", |
| 951 | // platform.rs:872-877). The colour card reads the same arrows |
| 952 | // for its sliders, and a card answers its own strokes before the |
| 953 | // keymap is consulted -- the arrows among them -- so these move |
| 954 | // the playhead only while no card is open. The document chords |
| 955 | // are the exception and are not these: `Undo`, `Redo`, `Save`, |
| 956 | // `Copy` and `Paste` reach the keymap whatever is up |
| 957 | // (`ui::stance::card_safe_action`). |
| 958 | b(ActionId::StepBack, "left", false), |
| 959 | b(ActionId::StepForward, "right", false), |
| 960 | b(ActionId::JumpBack, "left", true), |
| 961 | b(ActionId::JumpForward, "right", true), |
| 962 | b(ActionId::GoStart, "home", false), |
| 963 | b(ActionId::GoEnd, "end", false), |
| 964 | // The sync-point pair, on the brackets that already mean "the |
| 965 | // one before / the one after" here (they select the clip either |
| 966 | // side) -- with ctrl, which was free on both, because these |
| 967 | // step through the same timeline by the source's own grid. |
| 968 | b(ActionId::PrevSyncPoint, "[", true), |
| 969 | b(ActionId::NextSyncPoint, "]", true), |
| 970 | // The cut odometer (DESIGN.md §6): bare walks one cut, shift |
| 971 | // strides ten. gpui 0.2.2 (linux/platform.rs:909-917) clears |
| 972 | // the shift modifier for any single-char key whose upper and |
| 973 | // lower case are the same -- every one of these symbols -- |
| 974 | // so `shift+.` never arrives; what arrives instead is the |
| 975 | // key the shifted stroke actually types, `>`/`<`. Bound |
| 976 | // directly, on the same physical chord, since that is what |
| 977 | // the platform hands the key handler. |
| 978 | b(ActionId::WalkCutNext, ".", false), |
| 979 | b(ActionId::WalkCutPrev, ",", false), |
| 980 | b(ActionId::WalkCutNext10, ">", false), |
| 981 | b(ActionId::WalkCutPrev10, "<", false), |
| 982 | // The mark pair, on the letters every editor already means them |
| 983 | // by: bare, since a mark is set as often as a step and nothing |
| 984 | // else in this table answers to either. |
| 985 | b(ActionId::SetIn, "i", false), |
| 986 | b(ActionId::SetOut, "o", false), |
| 987 | // The clear takes the ctrl chord: not a stroke to hit setting |
| 988 | // one mark and reaching for the other. Free -- "u" bare is the |
| 989 | // silence card, and nothing else in this table answers to |
| 990 | // ctrl+u. |
| 991 | b(ActionId::ClearRange, "u", true), |
| 992 | b(ActionId::Export, "e", false), |
| 993 | b(ActionId::Save, "s", true), |
| 994 | b(ActionId::Copy, "c", true), |
| 995 | b(ActionId::Paste, "v", true), |
| 996 | // Split, on the bare letter DESIGN.md §6 names for it: the |
| 997 | // burst-use command owns the cheap key (§11's frequency |
| 998 | // argument). Bare "c" moves to AddSubtitleLane below, which |
| 999 | // frees up cleanly since a subtitle track also opens on its |
| 1000 | // own initial: captions. |
| 1001 | b(ActionId::Cut, "s", false), |
| 1002 | // The two lane keys, on the letters they are named after: both |
| 1003 | // were free, and neither is next to a key that edits. |
| 1004 | b(ActionId::Regroup, "g", false), |
| 1005 | // The pair that takes a take apart and puts it back: "d" for |
| 1006 | // detach was free and is one press like the rest of the clip |
| 1007 | // keys, and grouping two clips is the "g" that rejoins a cut one |
| 1008 | // lane over -- the chord beside it, so the two read as one idea. |
| 1009 | b(ActionId::Detach, "d", false), |
| 1010 | b(ActionId::Group, "g", true), |
| 1011 | // Selection without a pointer. Tab is what a keyboard already |
| 1012 | // means by "move on to the next thing", and the two braces |
| 1013 | // are the pair either side of it in the lane -- shift+tab is |
| 1014 | // the obvious partner and is unspellable here, since a chord |
| 1015 | // carries only ctrl and `parse` refuses an uppercase key. Bare |
| 1016 | // brackets are DESIGN.md §6's own trim pair now, so selection |
| 1017 | // takes the braces the shifted brackets actually type: same |
| 1018 | // two keys, telling "walk the lane" apart from "trim the cut". |
| 1019 | b(ActionId::Select, "tab", false), |
| 1020 | // Same platform fact as the odometer above: shift+] never |
| 1021 | // arrives as shift+], it arrives as the key `}` (and shift+[ |
| 1022 | // as `{`) -- bound on the literal the platform delivers. |
| 1023 | b(ActionId::SelectNext, "}", false), |
| 1024 | b(ActionId::SelectPrev, "{", false), |
| 1025 | // Select-all, on the chord every editor and every text field |
| 1026 | // already means it by -- which is why the audio-lane removal |
| 1027 | // below had to move off ctrl+a: a stroke that universal was |
| 1028 | // never really the removal's to keep. |
| 1029 | b(ActionId::SelectAll, "a", true), |
| 1030 | // Bare escape, last in the precedence chain behind every card, |
| 1031 | // search and fullscreen close (render.rs) -- just empties the |
| 1032 | // selection when nothing else claims the key. |
| 1033 | b(ActionId::Deselect, "escape", false), |
| 1034 | b(ActionId::Delete, "x", false), |
| 1035 | b(ActionId::Delete, "delete", false), |
| 1036 | b(ActionId::Lift, "l", false), |
| 1037 | // The c of colour is now AddSubtitleLane's (captions) and |
| 1038 | // ctrl+c is Copy, so the grade takes k: free, next to nothing |
| 1039 | // that edits, and one press like the |
| 1040 | // rest of the clip keys. |
| 1041 | b(ActionId::Color, "k", false), |
| 1042 | // The placement card takes the ctrl chord of the grade's own |
| 1043 | // key: the two sit beside each other on a clip and neither |
| 1044 | // steals a bare letter that already means something else. |
| 1045 | b(ActionId::Transform, "k", true), |
| 1046 | // The fit policy is a clip key like the grade beside it: "p" for |
| 1047 | // policy, free, and one press like the rest of them. The project |
| 1048 | // resolution is not a clip key and takes a ctrl chord, next to |
| 1049 | // nothing that edits and out of the way of a stray press -- |
| 1050 | // it changes what every clip is composed onto. |
| 1051 | b(ActionId::Fit, "p", false), |
| 1052 | b(ActionId::Resolution, "r", true), |
| 1053 | // The zoom pair, on the chords every editor and every browser |
| 1054 | // magnifies with. The bare "=" and "-" are the volume keys, so |
| 1055 | // these take the ctrl versions of them -- and ctrl+0 is the |
| 1056 | // "back to 100%" of the same family, which here is the whole |
| 1057 | // timeline across the bed. |
| 1058 | b(ActionId::ZoomIn, "=", true), |
| 1059 | b(ActionId::ZoomOut, "-", true), |
| 1060 | b(ActionId::ZoomFit, "0", true), |
| 1061 | b(ActionId::Undo, "z", false), |
| 1062 | b(ActionId::Undo, "z", true), |
| 1063 | // Bare "y" is SubtitleStyle's, so redo takes only the ctrl |
| 1064 | // chord -- the conventional pairing with ctrl+z either way. |
| 1065 | b(ActionId::Redo, "y", true), |
| 1066 | // The unshifted initials of what they add. Both were free -- |
| 1067 | // the copy and paste chords are the *ctrl* ones -- and a track |
| 1068 | // is added often enough to deserve a key that is one press. |
| 1069 | b(ActionId::AddVideoLane, "v", false), |
| 1070 | b(ActionId::AddAudioLane, "a", false), |
| 1071 | // ...and the pair that takes one back, on ctrl chords: removing |
| 1072 | // a track is not a stroke to hit by accident (the resolution key |
| 1073 | // is out of the way for the same reason). Ctrl+v would be the |
| 1074 | // matching initial and is the paste, so the video one takes the |
| 1075 | // key beside it. |
| 1076 | b(ActionId::RemoveVideoLane, "b", true), |
| 1077 | // Ctrl+a is the select-all above now, so the audio removal |
| 1078 | // takes the free key beside its own letter: "a" neighbours |
| 1079 | // "q" on the row, and ctrl+q was taken by nothing. |
| 1080 | b(ActionId::RemoveAudioLane, "q", true), |
| 1081 | // The subtitle pair reads the same way, on a different letter |
| 1082 | // now that bare "s" is split's (DESIGN.md §6): "c" is free |
| 1083 | // once split moves off it, and it is still an initial the |
| 1084 | // track answers to -- captions. The removal takes the other |
| 1085 | // letter subtitles already answer to here: "t" shows and |
| 1086 | // hides them, and ctrl+t takes one off the timeline. |
| 1087 | b(ActionId::AddSubtitleLane, "c", false), |
| 1088 | b(ActionId::RemoveSubtitleLane, "t", true), |
| 1089 | // Reading a file's tracks into the palette is not adding a |
| 1090 | // track to the timeline, so it is not the bare "s": ctrl+i, the |
| 1091 | // i of import, free and next to nothing that deletes. |
| 1092 | b(ActionId::ImportSubtitles, "i", true), |
| 1093 | // The dock's own import pair (MOCK-SPEC "Dock" IMPORT |
| 1094 | // section). Add files takes the chord the mock draws, ^o |
| 1095 | // (freed above): it is the burst-use command an editor |
| 1096 | // reaches for constantly, where the setting it displaces is |
| 1097 | // thrown once a project. Paste path stays off ^v -- that |
| 1098 | // chord is the timeline's own Paste, asked for far more |
| 1099 | // often while cutting than a clipboard path is pasted while |
| 1100 | // importing -- and takes ctrl+l instead, the browser's own |
| 1101 | // "go to this location" chord, which is what pasting a path |
| 1102 | // to jump straight to a file already is. |
| 1103 | b(ActionId::AddFiles, "o", true), |
| 1104 | b(ActionId::PasteFilePath, "l", true), |
| 1105 | b(ActionId::ToggleMute, "m", false), |
| 1106 | // The unshifted pair, which is what gpui reports for those two |
| 1107 | // keys ("=" and "-", platform.rs:862): the volume keys every |
| 1108 | // player has, without asking for shift to make the "+". |
| 1109 | b(ActionId::VolumeUp, "=", false), |
| 1110 | b(ActionId::VolumeDown, "-", false), |
| 1111 | // The one free letter that says the word: "e" is already the |
| 1112 | // export and an equalizer is the q in nobody else's way. |
| 1113 | b(ActionId::Equalizer, "q", false), |
| 1114 | // "s" is the save's ctrl chord, so the speed card takes the |
| 1115 | // unshifted letter beside it: "j" was free, sits under the |
| 1116 | // right hand with the other clip keys (k grades, l lifts), and |
| 1117 | // is not next to anything that deletes. |
| 1118 | b(ActionId::Speed, "j", false), |
| 1119 | // The silence card takes "u": free, sits with the other clip-card |
| 1120 | // keys under the right hand, and is nowhere near the two that |
| 1121 | // delete (x and delete) -- what it opens is a card that can cut |
| 1122 | // forty places at once. |
| 1123 | b(ActionId::Silence, "u", false), |
| 1124 | // Waveform-as-video: `h` was free (Theme is ctrl+h, Screenshot is w). |
| 1125 | b(ActionId::Visualizer, "h", false), |
| 1126 | // Crossfade takes ctrl+f: the mnemonic letter for "fade", with |
| 1127 | // ctrl its own room now that plain "f" is the mix card's own |
| 1128 | // (below) -- the same letter, the modifier telling them apart. |
| 1129 | b(ActionId::Crossfade, "f", true), |
| 1130 | // DESIGN.md §6's own chords: bare `[` `]` are the no-aim trim |
| 1131 | // pair. SelectPrev/SelectNext moved to `{` `}` above to make |
| 1132 | // room -- the bare bracket trims the subject cut, the brace |
| 1133 | // (what the shifted bracket actually types) walks the |
| 1134 | // selection past it, so the family reads as one idea under |
| 1135 | // one pair of keys. |
| 1136 | b(ActionId::TrimIn, "[", false), |
| 1137 | b(ActionId::TrimOut, "]", false), |
| 1138 | // Loop-trim (DESIGN.md §6, "the modernized Avid trim mode"): |
| 1139 | // bare "/" was free. |
| 1140 | b(ActionId::LoopTrim, "/", false), |
| 1141 | // Trim-to-playhead (debt #42): the whole bracket family is |
| 1142 | // already spoken for -- bare `[`/`]` nudge, `{`/`}` walk the |
| 1143 | // selection, ctrl+`[`/ctrl+`]` jump sync points -- so this |
| 1144 | // takes the one rung left in the same family, ctrl held on |
| 1145 | // the shifted bracket (`ctrl+{`/`ctrl+}`, what |
| 1146 | // ctrl+shift+[/ctrl+shift+] actually type per this table's |
| 1147 | // own shift note above). |
| 1148 | b(ActionId::TrimInToPlayhead, "{", true), |
| 1149 | b(ActionId::TrimOutToPlayhead, "}", true), |
| 1150 | // Dissolve takes ctrl+x: the join it draws on the timeline is |
| 1151 | // an X (widgets.rs, dissolve_glyph) -- "d" itself is taken |
| 1152 | // both plain (Detach) and, in the keymap's own rebind test, |
| 1153 | // as the free chord it rebinds Delete onto, so this reaches |
| 1154 | // for the shape instead of the word. |
| 1155 | b(ActionId::Dissolve, "x", true), |
| 1156 | // The mix card takes "f", for the faders on it: "m" would be |
| 1157 | // the word but it is the monitoring mute, which is this |
| 1158 | // machine's volume and not the project's -- two things one key |
| 1159 | // must not mean. |
| 1160 | b(ActionId::Mix, "f", false), |
| 1161 | // The snap takes "n", which is what every other editor toggles |
| 1162 | // it with: free here, and nowhere near the two keys that delete. |
| 1163 | b(ActionId::ToggleSnap, "n", false), |
| 1164 | // The subtitles take "t", for the word: free, one press like |
| 1165 | // the rest of the things one *looks* at, and nowhere near the |
| 1166 | // two keys that delete. |
| 1167 | b(ActionId::ToggleSubtitles, "t", false), |
| 1168 | // The stand-ins take ctrl+p, the p of the word: the unshifted |
| 1169 | // one is the fit policy, and a ctrl chord is right for a switch |
| 1170 | // thrown once for a whole session rather than one wanted under |
| 1171 | // a hand -- the theme takes ctrl+h for that reason. |
| 1172 | b(ActionId::ToggleProxies, "p", true), |
| 1173 | // ctrl+o moved to Add files (below, with `ImportSubtitles` -- |
| 1174 | // display order follows [`ActionId::ALL`]) per DESIGN §11's |
| 1175 | // frequency check: a burst-use import command over a |
| 1176 | // once-a-project setting. The auto-proxy switch takes "m" |
| 1177 | // instead, free and still a stand-in word ("make" -- what |
| 1178 | // the switch decides is whether one gets made on import). |
| 1179 | b(ActionId::ToggleAutoProxies, "m", true), |
| 1180 | // The theme takes ctrl+h -- the h of the word, since "t" is the |
| 1181 | // subtitles and ctrl+t takes one off -- and a ctrl chord because |
| 1182 | // it is a preference set once, not a stroke wanted under a hand |
| 1183 | // that is editing. |
| 1184 | b(ActionId::Theme, "h", true), |
| 1185 | // Every other player's key for it, and free here: nothing in |
| 1186 | // this table answers to "f11" (gpui lowercases the keysym |
| 1187 | // name it gets from xkb, same as "f1" below). |
| 1188 | b(ActionId::Fullscreen, "f11", false), |
| 1189 | // A chord, and the only escape in this table: bare `esc` is the |
| 1190 | // stroke a hand throws at anything on screen, and throwing it |
| 1191 | // at a running export used to delete an hour of encoding. The |
| 1192 | // key stays the one people look for -- the modifier is what |
| 1193 | // makes it an intention ([`crate::cancels_export`]). |
| 1194 | b(ActionId::CancelExport, "escape", true), |
| 1195 | // The help key (DESIGN.md §9, amended 2026-09-09): it swings |
| 1196 | // the dock to its KEYS tab, and a second press (or `escape`) |
| 1197 | // swings it back -- a toggle, not a hold, not a modal. |
| 1198 | // Its own key, not `shift+/`: gpui reports a shifted symbol as |
| 1199 | // the glyph itself, so `"?"` is what a stroke of it reports. |
| 1200 | b(ActionId::ShowActions, "?", false), |
| 1201 | // "w" is free entirely, bare and ctrl both -- nothing else in |
| 1202 | // this table answers to it. |
| 1203 | b(ActionId::Screenshot, "w", false), |
| 1204 | // Bare "y" is free entirely, and unshifted: a preference |
| 1205 | // opened as often as the theme, not a switch thrown once a |
| 1206 | // session. |
| 1207 | b(ActionId::SubtitleStyle, "y", false), |
| 1208 | // The consumer-editor convention (Cmd/Ctrl+,) -- free here: |
| 1209 | // bare "," is WalkCutPrev, and nothing else answers to the |
| 1210 | // ctrl chord. |
| 1211 | b(ActionId::Settings, ",", true), |
| 1212 | // The standard pane-cycle key on every desktop that has one |
| 1213 | // (browsers, IDEs) -- free here, and Tab itself cannot spell |
| 1214 | // this: bare Tab is already [`ActionId::Select`] at the root |
| 1215 | // (a bare-key rebind would steal clip selection), and its |
| 1216 | // shift pair is unspellable in this table's chord (`ctrl` |
| 1217 | // only, see [`ActionId::Select`]'s own comment above). |
| 1218 | b(ActionId::FocusPanels, "f6", false), |
| 1219 | ], |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | /// What a stroke means, if anything. `key` is gpui's key name and `ctrl` its |
| 1224 | /// control modifier -- the pair the key handler already matches on. There |
| 1225 | /// is no shift parameter: see [`Chord`]'s own doc for why. |
| 1226 | pub fn lookup(&self, key: &str, ctrl: bool) -> Option<ActionId> { |
| 1227 | self.bindings |
| 1228 | .iter() |
| 1229 | .find(|b| b.chord.ctrl == ctrl && b.chord.key == key) |
| 1230 | .map(|b| b.action) |
| 1231 | } |
| 1232 | |
| 1233 | /// Every stroke that reaches an action, as one readable phrase: `x or |
| 1234 | /// delete`. An action nobody can reach says so rather than reading blank. |
| 1235 | pub fn display(&self, action: ActionId) -> String { |
| 1236 | let strokes: Vec<String> = self |
| 1237 | .bindings |
| 1238 | .iter() |
| 1239 | .filter(|b| b.action == action) |
| 1240 | .map(|b| b.chord.pretty()) |
| 1241 | .collect(); |
| 1242 | if strokes.is_empty() { |
| 1243 | "unbound".to_string() |
| 1244 | } else { |
| 1245 | strokes.join(" or ") |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | /// The primary chord for `action`, compact ([`Chord::compact`]) -- a |
| 1250 | /// badge that has room for one token, not [`Keymap::display`]'s sentence. |
| 1251 | /// The first bound stroke wins (`defaults`' own insertion order, and |
| 1252 | /// [`parse`]'s "no chord appears twice" leaves at most one anyway); an |
| 1253 | /// action with no stroke reads `--` rather than the unbound sentence, |
| 1254 | /// since a badge is not a warning. |
| 1255 | pub fn chord(&self, action: ActionId) -> String { |
| 1256 | self.bindings |
| 1257 | .iter() |
| 1258 | .find(|b| b.action == action) |
| 1259 | .map(|b| b.chord.compact()) |
| 1260 | .unwrap_or_else(|| "--".to_string()) |
| 1261 | } |
| 1262 | |
| 1263 | /// Every binding, in display order. |
| 1264 | pub fn entries(&self) -> &[Binding] { |
| 1265 | &self.bindings |
| 1266 | } |
| 1267 | |
| 1268 | /// The keymap in force, and what to tell the user if the file could not be |
| 1269 | /// used. A file that is merely absent is the normal case and says nothing; |
| 1270 | /// anything else -- unreadable, or a line this parser refuses -- falls back |
| 1271 | /// to the defaults *and* is reported, because a keymap silently not applied |
| 1272 | /// is a keyboard that has quietly changed under the user. |
| 1273 | pub fn load() -> (Self, Option<String>) { |
| 1274 | Self::load_from(&Self::config_path()) |
| 1275 | } |
| 1276 | |
| 1277 | /// All of [`Keymap::load`] but the path, which the config directory decides |
| 1278 | /// and a test must not be allowed to. |
| 1279 | fn load_from(path: &Path) -> (Self, Option<String>) { |
| 1280 | let text = match std::fs::read_to_string(path) { |
| 1281 | Ok(text) => text, |
| 1282 | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return (Self::defaults(), None), |
| 1283 | Err(e) => { |
| 1284 | return ( |
| 1285 | Self::defaults(), |
| 1286 | Some(format!( |
| 1287 | "KEYBINDINGS IGNORED — {}: {e} — the defaults are in force", |
| 1288 | path.display() |
| 1289 | )), |
| 1290 | ); |
| 1291 | } |
| 1292 | }; |
| 1293 | match parse(&text) { |
| 1294 | Ok((keymap, dropped)) if dropped.is_empty() => (keymap, None), |
| 1295 | // The lines it could use are in force; the ones it could not are |
| 1296 | // named, and only those are lost. |
| 1297 | Ok((keymap, dropped)) => ( |
| 1298 | keymap, |
| 1299 | Some(format!( |
| 1300 | "KEYBINDINGS PART-READ — {}, {} — those strokes are unbound", |
| 1301 | path.display(), |
| 1302 | dropped.join("; ") |
| 1303 | )), |
| 1304 | ), |
| 1305 | Err(e) => ( |
| 1306 | Self::defaults(), |
| 1307 | Some(format!( |
| 1308 | "KEYBINDINGS IGNORED — {}, {e} — the defaults are in force", |
| 1309 | path.display() |
| 1310 | )), |
| 1311 | ), |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | /// Writes the keymap, atomically, creating the config directory if this is |
| 1316 | /// the first save. Until the rename the previous bindings are still the |
| 1317 | /// whole truth on disk. |
| 1318 | pub fn save(&self) -> std::io::Result<()> { |
| 1319 | self.save_to(&Self::config_path()) |
| 1320 | } |
| 1321 | |
| 1322 | /// All of [`Keymap::save`] but the path, for the same reason as |
| 1323 | /// [`Keymap::load_from`]. |
| 1324 | fn save_to(&self, path: &Path) -> std::io::Result<()> { |
| 1325 | let dir = path.parent().unwrap_or(Path::new(".")).to_path_buf(); |
| 1326 | std::fs::create_dir_all(&dir)?; |
| 1327 | let mut part = path.to_path_buf().into_os_string(); |
| 1328 | part.push(".part"); |
| 1329 | let part = PathBuf::from(part); |
| 1330 | let result = std::fs::File::create(&part) |
| 1331 | .and_then(|mut f| { |
| 1332 | f.write_all(emit(self).as_bytes())?; |
| 1333 | f.sync_all() |
| 1334 | }) |
| 1335 | .and_then(|()| std::fs::rename(&part, path)) |
| 1336 | .and_then(|()| std::fs::File::open(&dir)?.sync_all()); |
| 1337 | if result.is_err() { |
| 1338 | let _ = std::fs::remove_file(&part); |
| 1339 | } |
| 1340 | result |
| 1341 | } |
| 1342 | |
| 1343 | /// Where the bindings live: the XDG config directory, or `~/.config` when |
| 1344 | /// the desktop has not named one. |
| 1345 | pub fn config_path() -> PathBuf { |
| 1346 | config_path_in( |
| 1347 | std::env::var_os("XDG_CONFIG_HOME"), |
| 1348 | std::env::var_os("HOME"), |
| 1349 | ) |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | /// The path rule, with the environment handed in so it can be checked. An |
| 1354 | /// `XDG_CONFIG_HOME` that is empty is one the spec says to ignore; with no |
| 1355 | /// `HOME` either there is nowhere but here, and a relative path is still better |
| 1356 | /// than dropping the file in `/`. |
| 1357 | fn config_path_in(xdg: Option<OsString>, home: Option<OsString>) -> PathBuf { |
| 1358 | let dir = xdg |
| 1359 | .filter(|v| !v.is_empty()) |
| 1360 | .map(PathBuf::from) |
| 1361 | .or_else(|| { |
| 1362 | home.filter(|v| !v.is_empty()) |
| 1363 | .map(|h| PathBuf::from(h).join(".config")) |
| 1364 | }) |
| 1365 | .unwrap_or_else(|| PathBuf::from(".config")); |
| 1366 | dir.join("edith").join("keybindings") |
| 1367 | } |
| 1368 | |
| 1369 | fn emit(keymap: &Keymap) -> String { |
| 1370 | let mut out = String::from(MAGIC); |
| 1371 | out.push('\n'); |
| 1372 | for b in &keymap.bindings { |
| 1373 | out.push_str(b.action.name()); |
| 1374 | out.push(' '); |
| 1375 | out.push_str(&b.chord.text()); |
| 1376 | out.push('\n'); |
| 1377 | } |
| 1378 | out |
| 1379 | } |
| 1380 | |
| 1381 | /// The magic line is the whole of what makes this a keybindings file, so it is |
| 1382 | /// the only thing refused outright. A *binding* line this parser cannot use is |
| 1383 | /// dropped instead, and named in the returned warnings: one unusable line used |
| 1384 | /// to cost every other rebind in the file, which is the user's data. What |
| 1385 | /// survives is still exactly what emits back, so the by-hand round-trip holds |
| 1386 | /// for every line that is kept. |
| 1387 | fn parse(text: &str) -> Result<(Keymap, Vec<String>), String> { |
| 1388 | // One trailing newline terminates the last line rather than starting an |
| 1389 | // empty one; a second is an empty line and is refused below. |
| 1390 | let body = text.strip_suffix('\n').unwrap_or(text); |
| 1391 | let mut lines = body.split('\n').enumerate(); |
| 1392 | let (_, first) = lines.next().unwrap_or((0, "")); |
| 1393 | if first != MAGIC { |
| 1394 | return Err(match first.strip_prefix("edith-keys ") { |
| 1395 | Some(v) => format!("line 1: unsupported version {v}"), |
| 1396 | None => "line 1: not a keybindings file".to_string(), |
| 1397 | }); |
| 1398 | } |
| 1399 | |
| 1400 | let mut bindings: Vec<Binding> = Vec::new(); |
| 1401 | let mut dropped: Vec<String> = Vec::new(); |
| 1402 | for (i, line) in lines { |
| 1403 | let n = i + 1; |
| 1404 | let Some((name, chord)) = line.split_once(' ') else { |
| 1405 | dropped.push(format!("line {n}: {line:?} is not `<action> <chord>`")); |
| 1406 | continue; |
| 1407 | }; |
| 1408 | let Some(action) = ActionId::from_name(name) else { |
| 1409 | dropped.push(format!("line {n}: unknown action {name:?}")); |
| 1410 | continue; |
| 1411 | }; |
| 1412 | let chord = match Chord::parse(chord, n) { |
| 1413 | Ok(chord) => chord, |
| 1414 | Err(e) => { |
| 1415 | dropped.push(e); |
| 1416 | continue; |
| 1417 | } |
| 1418 | }; |
| 1419 | // One stroke, one meaning: the second line to claim it would be the |
| 1420 | // dead one, and which of them died would depend on line order. The |
| 1421 | // first line keeps the stroke, which is the order the file reads in. |
| 1422 | if let Some(held) = bindings.iter().find(|b| b.chord == chord) { |
| 1423 | dropped.push(format!( |
| 1424 | "line {n}: {} is already {}'s", |
| 1425 | chord.text(), |
| 1426 | held.action.name() |
| 1427 | )); |
| 1428 | continue; |
| 1429 | } |
| 1430 | bindings.push(Binding { action, chord }); |
| 1431 | } |
| 1432 | Ok((Keymap { bindings }, dropped)) |
| 1433 | } |
| 1434 | |
| 1435 | #[cfg(test)] |
| 1436 | mod tests { |
| 1437 | use engine::scratch::Scratch; |
| 1438 | |
| 1439 | use super::{config_path_in, emit, parse, ActionId, Chord, Keymap}; |
| 1440 | |
| 1441 | fn chord(key: &str, ctrl: bool) -> Chord { |
| 1442 | Chord { |
| 1443 | key: key.to_string(), |
| 1444 | ctrl, |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /// A file every line of which the parser could use -- what most assertions |
| 1449 | /// are about. A dropped line here is a failure, not a warning. |
| 1450 | fn whole(text: &str) -> Keymap { |
| 1451 | let (keymap, dropped) = parse(text).expect("a keybindings file"); |
| 1452 | assert!(dropped.is_empty(), "unexpected dropped lines: {dropped:?}"); |
| 1453 | keymap |
| 1454 | } |
| 1455 | |
| 1456 | #[test] |
| 1457 | fn every_default_stroke_reaches_its_action() { |
| 1458 | let k = Keymap::defaults(); |
| 1459 | assert_eq!(k.entries().len(), 79); |
| 1460 | // The cut odometer: bare walks one, `<`/`>` (what shift+,/shift+. |
| 1461 | // actually type) strides ten. |
| 1462 | assert_eq!(k.lookup(".", false), Some(ActionId::WalkCutNext)); |
| 1463 | assert_eq!(k.lookup(",", false), Some(ActionId::WalkCutPrev)); |
| 1464 | assert_eq!(k.lookup("/", false), Some(ActionId::LoopTrim)); |
| 1465 | assert_eq!(k.lookup(">", false), Some(ActionId::WalkCutNext10)); |
| 1466 | assert_eq!(k.lookup("<", false), Some(ActionId::WalkCutPrev10)); |
| 1467 | // The trim pair, on its designed bare brackets (DESIGN.md §6). |
| 1468 | assert_eq!(k.lookup("[", false), Some(ActionId::TrimIn)); |
| 1469 | assert_eq!(k.lookup("]", false), Some(ActionId::TrimOut)); |
| 1470 | // Trim-to-playhead (debt #42): the same family, one rung further. |
| 1471 | assert_eq!(k.lookup("{", true), Some(ActionId::TrimInToPlayhead)); |
| 1472 | assert_eq!(k.lookup("}", true), Some(ActionId::TrimOutToPlayhead)); |
| 1473 | assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); |
| 1474 | assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); |
| 1475 | assert_eq!(k.lookup("y", false), Some(ActionId::SubtitleStyle)); |
| 1476 | assert_eq!(k.lookup("p", true), Some(ActionId::ToggleProxies)); |
| 1477 | assert_eq!(k.lookup("m", true), Some(ActionId::ToggleAutoProxies)); |
| 1478 | assert_eq!(k.lookup("o", true), Some(ActionId::AddFiles)); |
| 1479 | assert_eq!(k.lookup("l", true), Some(ActionId::PasteFilePath)); |
| 1480 | assert_eq!(k.lookup("?", false), Some(ActionId::ShowActions)); |
| 1481 | assert_eq!(k.lookup("h", true), Some(ActionId::Theme)); |
| 1482 | assert_eq!(k.lookup("h", false), Some(ActionId::Visualizer)); |
| 1483 | assert_eq!(k.lookup("space", false), Some(ActionId::Play)); |
| 1484 | // The seek keys: bare arrows a frame, ctrl arrows a second, and the two |
| 1485 | // ends of the timeline. |
| 1486 | assert_eq!(k.lookup("left", false), Some(ActionId::StepBack)); |
| 1487 | assert_eq!(k.lookup("right", false), Some(ActionId::StepForward)); |
| 1488 | assert_eq!(k.lookup("left", true), Some(ActionId::JumpBack)); |
| 1489 | assert_eq!(k.lookup("right", true), Some(ActionId::JumpForward)); |
| 1490 | assert_eq!(k.lookup("home", false), Some(ActionId::GoStart)); |
| 1491 | assert_eq!(k.lookup("end", false), Some(ActionId::GoEnd)); |
| 1492 | // ...and the source's own grid, which is neither a frame nor a second: |
| 1493 | // the brackets bare select the clip either side, with ctrl they step |
| 1494 | // the sync points a cut has to land on to be copied. |
| 1495 | assert_eq!(k.lookup("[", true), Some(ActionId::PrevSyncPoint)); |
| 1496 | assert_eq!(k.lookup("]", true), Some(ActionId::NextSyncPoint)); |
| 1497 | assert_eq!(k.lookup("}", false), Some(ActionId::SelectNext)); |
| 1498 | assert_eq!(k.lookup("{", false), Some(ActionId::SelectPrev)); |
| 1499 | assert_eq!(k.lookup("i", false), Some(ActionId::SetIn)); |
| 1500 | assert_eq!(k.lookup("o", false), Some(ActionId::SetOut)); |
| 1501 | assert_eq!(k.lookup("u", true), Some(ActionId::ClearRange)); |
| 1502 | assert_eq!(k.lookup("e", false), Some(ActionId::Export)); |
| 1503 | assert_eq!(k.lookup("s", true), Some(ActionId::Save)); |
| 1504 | assert_eq!(k.lookup("c", true), Some(ActionId::Copy)); |
| 1505 | assert_eq!(k.lookup("v", true), Some(ActionId::Paste)); |
| 1506 | assert_eq!(k.lookup("s", false), Some(ActionId::Cut)); |
| 1507 | assert_eq!(k.lookup("g", false), Some(ActionId::Regroup)); |
| 1508 | // The take apart / put back pair beside it. |
| 1509 | assert_eq!(k.lookup("d", false), Some(ActionId::Detach)); |
| 1510 | assert_eq!(k.lookup("g", true), Some(ActionId::Group)); |
| 1511 | // The keyboard's way onto a clip, and the pair that walks the lane. |
| 1512 | assert_eq!(k.lookup("tab", false), Some(ActionId::Select)); |
| 1513 | assert_eq!(k.lookup("}", false), Some(ActionId::SelectNext)); |
| 1514 | assert_eq!(k.lookup("{", false), Some(ActionId::SelectPrev)); |
| 1515 | assert_eq!(k.lookup("x", false), Some(ActionId::Delete)); |
| 1516 | assert_eq!(k.lookup("delete", false), Some(ActionId::Delete)); |
| 1517 | assert_eq!(k.lookup("l", false), Some(ActionId::Lift)); |
| 1518 | assert_eq!(k.lookup("k", false), Some(ActionId::Color)); |
| 1519 | assert_eq!(k.lookup("p", false), Some(ActionId::Fit)); |
| 1520 | assert_eq!(k.lookup("r", true), Some(ActionId::Resolution)); |
| 1521 | assert_eq!(k.lookup("z", false), Some(ActionId::Undo)); |
| 1522 | assert_eq!(k.lookup("z", true), Some(ActionId::Undo)); |
| 1523 | assert_eq!(k.lookup("y", true), Some(ActionId::Redo)); |
| 1524 | // The track keys are the bare letters; the ctrl ones stay copy/paste. |
| 1525 | assert_eq!(k.lookup("v", false), Some(ActionId::AddVideoLane)); |
| 1526 | assert_eq!(k.lookup("a", false), Some(ActionId::AddAudioLane)); |
| 1527 | // ...and the ctrl ones take a track away, out of the way of a stray |
| 1528 | // press. Ctrl+v is the paste, so the video one sits beside it; ctrl+a |
| 1529 | // is the select-all every editor shares, so the audio one sits beside |
| 1530 | // its own letter instead. |
| 1531 | assert_eq!(k.lookup("b", true), Some(ActionId::RemoveVideoLane)); |
| 1532 | assert_eq!(k.lookup("q", true), Some(ActionId::RemoveAudioLane)); |
| 1533 | assert_eq!(k.lookup("a", true), Some(ActionId::SelectAll)); |
| 1534 | // The subtitle pair, the third kind of track: the initial adds and the |
| 1535 | // subtitle letter's chord takes it back. |
| 1536 | assert_eq!(k.lookup("c", false), Some(ActionId::AddSubtitleLane)); |
| 1537 | assert_eq!(k.lookup("t", true), Some(ActionId::RemoveSubtitleLane)); |
| 1538 | // ...and the palette's own door, which adds no track to the timeline. |
| 1539 | assert_eq!(k.lookup("i", true), Some(ActionId::ImportSubtitles)); |
| 1540 | assert_eq!(k.lookup("m", false), Some(ActionId::ToggleMute)); |
| 1541 | assert_eq!(k.lookup("q", false), Some(ActionId::Equalizer)); |
| 1542 | // The volume pair is the unshifted one, so neither needs a modifier |
| 1543 | // and neither is the "+" gpui reports for shift+=. |
| 1544 | assert_eq!(k.lookup("=", false), Some(ActionId::VolumeUp)); |
| 1545 | assert_eq!(k.lookup("-", false), Some(ActionId::VolumeDown)); |
| 1546 | assert_eq!(k.lookup("+", false), None); |
| 1547 | // The chord cancels an export; bare escape falls through to Deselect. |
| 1548 | assert_eq!(k.lookup("escape", true), Some(ActionId::CancelExport)); |
| 1549 | assert_eq!(k.lookup("escape", false), Some(ActionId::Deselect)); |
| 1550 | // The modifier is half the chord: ctrl+e is not e. |
| 1551 | assert_eq!(k.lookup("e", true), None); |
| 1552 | assert_eq!(k.lookup("space", true), None); |
| 1553 | assert_eq!(k.lookup("j", false), Some(ActionId::Speed)); |
| 1554 | assert_eq!(k.lookup("u", false), Some(ActionId::Silence)); |
| 1555 | assert_eq!(k.lookup("j", true), None); |
| 1556 | // Nothing is bound twice, or lookup's first match would be a coin toss. |
| 1557 | for (i, a) in k.entries().iter().enumerate() { |
| 1558 | assert!( |
| 1559 | !k.entries()[i + 1..].iter().any(|b| b.chord == a.chord), |
| 1560 | "{:?} bound twice", |
| 1561 | a.chord |
| 1562 | ); |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | #[test] |
| 1567 | fn a_file_survives_the_round_trip_exactly() { |
| 1568 | let text = emit(&Keymap::defaults()); |
| 1569 | assert!(text.starts_with("edith-keys 1\nplay space\n")); |
| 1570 | assert!(text.contains("save ctrl+s\n")); |
| 1571 | assert!(text.contains("cancel-export ctrl+escape\n")); |
| 1572 | // Both directions: the file this writes is the file it reads back, and |
| 1573 | // a file read in comes out byte for byte as it went. |
| 1574 | assert_eq!(whole(&text), Keymap::defaults()); |
| 1575 | assert_eq!(emit(&whole(&text)), text); |
| 1576 | let handwritten = "edith-keys 1\nundo ctrl+z\nplay space\n"; |
| 1577 | assert_eq!(emit(&whole(handwritten)), handwritten); |
| 1578 | // A keymap that binds nothing is legal -- and unusable, which is the |
| 1579 | // user's business, not the parser's. |
| 1580 | assert_eq!(whole("edith-keys 1\n").entries(), &[]); |
| 1581 | } |
| 1582 | |
| 1583 | #[test] |
| 1584 | fn every_refusal_names_its_line() { |
| 1585 | // Only the first line refuses the whole file. |
| 1586 | let err = |text: &str| parse(text).unwrap_err(); |
| 1587 | assert_eq!(err(""), "line 1: not a keybindings file"); |
| 1588 | assert_eq!(err("nonsense\n"), "line 1: not a keybindings file"); |
| 1589 | assert_eq!(err("edith-keys 2\n"), "line 1: unsupported version 2"); |
| 1590 | |
| 1591 | // Every other kind of bad line costs that line and nothing else. |
| 1592 | let dropped = |text: &str| parse(text).unwrap().1; |
| 1593 | assert_eq!( |
| 1594 | dropped("edith-keys 1\nfly space\n"), |
| 1595 | ["line 2: unknown action \"fly\""] |
| 1596 | ); |
| 1597 | assert_eq!( |
| 1598 | dropped("edith-keys 1\nplay space\nplayspace\n"), |
| 1599 | ["line 3: \"playspace\" is not `<action> <chord>`"] |
| 1600 | ); |
| 1601 | // An empty line is not optional whitespace, it is a line. |
| 1602 | assert_eq!( |
| 1603 | dropped("edith-keys 1\n\n"), |
| 1604 | ["line 2: \"\" is not `<action> <chord>`"] |
| 1605 | ); |
| 1606 | assert_eq!( |
| 1607 | dropped("edith-keys 1\nplay ctrl+alt+x\n"), |
| 1608 | ["line 2: \"ctrl+alt+x\" is not a chord"] |
| 1609 | ); |
| 1610 | assert_eq!( |
| 1611 | dropped("edith-keys 1\nplay ctrl+\n"), |
| 1612 | ["line 2: \"ctrl+\" is not a chord"] |
| 1613 | ); |
| 1614 | // gpui reports lowercase key names, so an uppercase one would simply |
| 1615 | // never match -- dropped rather than quietly dead. |
| 1616 | assert_eq!( |
| 1617 | dropped("edith-keys 1\nplay X\n"), |
| 1618 | ["line 2: \"X\" is not a chord"] |
| 1619 | ); |
| 1620 | // The same stroke twice: whichever line lost would depend on order, so |
| 1621 | // the second one is the one that goes. |
| 1622 | assert_eq!( |
| 1623 | dropped("edith-keys 1\nplay space\ncut space\n"), |
| 1624 | ["line 3: space is already play's"] |
| 1625 | ); |
| 1626 | } |
| 1627 | |
| 1628 | /// The rebind data-loss path: one line the parser cannot use must not cost |
| 1629 | /// the rebinds around it (a `capture` of shift+= wrote `play +`, and the |
| 1630 | /// next load threw the whole file away and went back to the defaults). |
| 1631 | #[test] |
| 1632 | fn one_bad_line_costs_only_that_line() { |
| 1633 | let text = "edith-keys 1\nplay +\nexport ctrl+w\ncut j\n"; |
| 1634 | let (keymap, dropped) = parse(text).expect("the magic line is still good"); |
| 1635 | assert_eq!(dropped, ["line 2: \"+\" is not a chord"]); |
| 1636 | // The lines around it are in force, which is the whole point. |
| 1637 | assert_eq!(keymap.lookup("w", true), Some(ActionId::Export)); |
| 1638 | assert_eq!(keymap.lookup("j", false), Some(ActionId::Cut)); |
| 1639 | // ...and the one that went is unbound, not silently something else. |
| 1640 | assert_eq!(keymap.display(ActionId::Play), "unbound"); |
| 1641 | // What survives still round-trips: the kept lines emit back as read. |
| 1642 | assert_eq!(emit(&keymap), "edith-keys 1\nexport ctrl+w\ncut j\n"); |
| 1643 | |
| 1644 | // The notice names the line and the file, and the defaults do *not* |
| 1645 | // take over -- that was the data loss. |
| 1646 | let dir = Scratch::dir("edith-keymap-drop"); |
| 1647 | let path = dir.join("edith").join("keybindings"); |
| 1648 | std::fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 1649 | std::fs::write(&path, text).unwrap(); |
| 1650 | let (read, notice) = Keymap::load_from(&path); |
| 1651 | assert_eq!(read, keymap); |
| 1652 | let notice = notice.expect("a part-read file must say so"); |
| 1653 | assert!(notice.contains("line 2:"), "{notice}"); |
| 1654 | assert!(notice.contains("keybindings"), "{notice}"); |
| 1655 | std::fs::remove_dir_all(&dir).ok(); |
| 1656 | } |
| 1657 | |
| 1658 | /// The other half of the same bug: a stroke the file cannot spell is refused |
| 1659 | /// at the capture, before it can ever be written. |
| 1660 | #[test] |
| 1661 | fn only_a_stroke_the_file_can_spell_is_bindable() { |
| 1662 | for (key, ctrl) in [ |
| 1663 | ("space", false), |
| 1664 | ("=", false), |
| 1665 | ("s", true), |
| 1666 | ("escape", false), |
| 1667 | ] { |
| 1668 | assert!(chord(key, ctrl).bindable(), "{key}"); |
| 1669 | } |
| 1670 | // gpui's name for shift+=, and the grammar's own separator. |
| 1671 | assert!(!chord("+", false).bindable()); |
| 1672 | assert!(!chord("+", true).bindable()); |
| 1673 | // The rest of what `Chord::parse` will not take back. |
| 1674 | assert!(!chord("", false).bindable()); |
| 1675 | assert!(!chord("X", false).bindable()); |
| 1676 | assert!(!chord("page up", false).bindable()); |
| 1677 | // A bindable chord is one the file's own grammar can spell: what it |
| 1678 | // writes is what the next load reads, for every stroke that passes |
| 1679 | // here. |
| 1680 | let k = whole("edith-keys 1\nplay ctrl+w\n"); |
| 1681 | assert_eq!(whole(&emit(&k)), k); |
| 1682 | } |
| 1683 | |
| 1684 | #[test] |
| 1685 | fn display_reads_as_a_person_would_say_it() { |
| 1686 | let k = Keymap::defaults(); |
| 1687 | assert_eq!(k.display(ActionId::Play), "space"); |
| 1688 | assert_eq!(k.display(ActionId::Save), "ctrl+s"); |
| 1689 | assert_eq!(k.display(ActionId::Delete), "x or delete"); |
| 1690 | assert_eq!(k.display(ActionId::Undo), "z or ctrl+z"); |
| 1691 | // The one key whose common name is not gpui's. |
| 1692 | assert_eq!(k.display(ActionId::CancelExport), "ctrl+esc"); |
| 1693 | assert_eq!(whole("edith-keys 1\n").display(ActionId::Cut), "unbound"); |
| 1694 | // The label is the editor's column, and never the file's word for it. |
| 1695 | assert_eq!(ActionId::CancelExport.label(), "Cancel export"); |
| 1696 | assert_eq!(k.display(ActionId::ToggleSnap), "n"); |
| 1697 | assert_eq!(k.display(ActionId::ShowActions), "?"); |
| 1698 | assert_eq!(k.display(ActionId::ToggleSubtitles), "t"); |
| 1699 | assert_eq!(k.display(ActionId::ZoomIn), "ctrl+="); |
| 1700 | } |
| 1701 | |
| 1702 | /// DESIGN §8: a label names the verb. Prose truncates -- the KEYS list |
| 1703 | /// ellipsises it, a menu row's fixed plate cuts it mid-word, a hover plate |
| 1704 | /// the same -- so four words is the whole budget, and what a parenthesis |
| 1705 | /// used to carry (why an action refuses) lives in the refusal string the |
| 1706 | /// oracle and the engine already word. |
| 1707 | #[test] |
| 1708 | fn a_label_names_the_verb_in_four_words() { |
| 1709 | let mut seen: Vec<&'static str> = Vec::new(); |
| 1710 | for action in ActionId::ALL { |
| 1711 | let label = action.label(); |
| 1712 | let words = label.split_whitespace().count(); |
| 1713 | assert!(words <= 4, "{action:?}: {words} words -- {label:?}"); |
| 1714 | assert!( |
| 1715 | !label.contains('('), |
| 1716 | "{action:?}: a parenthesis -- {label:?}" |
| 1717 | ); |
| 1718 | assert!( |
| 1719 | !label.to_lowercase().starts_with("the "), |
| 1720 | "{action:?}: leads with an article -- {label:?}" |
| 1721 | ); |
| 1722 | assert!(!seen.contains(&label), "two actions both named {label:?}"); |
| 1723 | seen.push(label); |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | /// [`Keymap::chord`] is the badge's one token, not [`Keymap::display`]'s |
| 1728 | /// sentence: the primary (first-bound) stroke only, `^` for ctrl, and the |
| 1729 | /// short names a glyph badge actually has room for. |
| 1730 | #[test] |
| 1731 | fn chord_is_the_primary_stroke_compact() { |
| 1732 | let k = Keymap::defaults(); |
| 1733 | assert_eq!(k.chord(ActionId::Delete), "x"); |
| 1734 | assert_eq!(k.chord(ActionId::Undo), "z"); |
| 1735 | assert_eq!(k.chord(ActionId::AddFiles), "^o"); |
| 1736 | assert_eq!(k.chord(ActionId::CancelExport), "^esc"); |
| 1737 | assert_eq!(k.chord(ActionId::Play), "spc"); |
| 1738 | assert_eq!(whole("edith-keys 1\n").chord(ActionId::Cut), "--"); |
| 1739 | } |
| 1740 | |
| 1741 | #[test] |
| 1742 | fn a_saved_keymap_comes_back_off_the_disk() { |
| 1743 | // Its own directory, never the real config one: this test writes. |
| 1744 | let dir = Scratch::dir("edith-keymap"); |
| 1745 | let path = dir.join("edith").join("keybindings"); |
| 1746 | // A defaults file with Export's own line swapped for a free stroke -- |
| 1747 | // parsed rather than rebound, since nothing left in the API mutates |
| 1748 | // a `Keymap` in place. |
| 1749 | let text: String = emit(&Keymap::defaults()) |
| 1750 | .lines() |
| 1751 | .filter(|l| !l.starts_with("export ")) |
| 1752 | .chain(std::iter::once("export ctrl+w")) |
| 1753 | .collect::<Vec<_>>() |
| 1754 | .join("\n") |
| 1755 | + "\n"; |
| 1756 | let written = whole(&text); |
| 1757 | // The config directory does not exist yet -- the first save makes it. |
| 1758 | written.save_to(&path).unwrap(); |
| 1759 | let (read, notice) = Keymap::load_from(&path); |
| 1760 | assert_eq!(notice, None); |
| 1761 | assert_eq!(read, written); |
| 1762 | assert_eq!(read.lookup("w", true), Some(ActionId::Export)); |
| 1763 | assert_eq!(read.lookup("e", false), None); |
| 1764 | // The rename left nothing half-written behind. |
| 1765 | assert!(!dir.join("edith").join("keybindings.part").exists()); |
| 1766 | // A file that is merely absent is not an error and says nothing. |
| 1767 | let (fresh, notice) = Keymap::load_from(&dir.join("nothing-here")); |
| 1768 | assert_eq!(fresh, Keymap::defaults()); |
| 1769 | assert_eq!(notice, None); |
| 1770 | // A file that is not one at all falls back whole, and names its line. |
| 1771 | std::fs::write(&path, "edith-keys 9\nplay space\n").unwrap(); |
| 1772 | let (fallback, notice) = Keymap::load_from(&path); |
| 1773 | assert_eq!(fallback, Keymap::defaults()); |
| 1774 | let notice = notice.expect("a refused file must say so"); |
| 1775 | assert!(notice.contains("line 1:"), "{notice}"); |
| 1776 | assert!(notice.contains("keybindings"), "{notice}"); |
| 1777 | // One unusable line inside a good file costs that line only -- the |
| 1778 | // defaults do not come back over the rebinds that read fine. |
| 1779 | std::fs::write(&path, "edith-keys 1\nplay ctrl+alt+x\nexport ctrl+w\n").unwrap(); |
| 1780 | let (kept, notice) = Keymap::load_from(&path); |
| 1781 | assert_eq!(kept.lookup("w", true), Some(ActionId::Export)); |
| 1782 | assert_eq!(kept.display(ActionId::Play), "unbound"); |
| 1783 | let notice = notice.expect("a part-read file must say so"); |
| 1784 | assert!(notice.contains("line 2:"), "{notice}"); |
| 1785 | assert!(notice.contains("keybindings"), "{notice}"); |
| 1786 | // Saving over it is a whole replacement, not an edit. |
| 1787 | Keymap::defaults().save_to(&path).unwrap(); |
| 1788 | assert_eq!(Keymap::load_from(&path), (Keymap::defaults(), None)); |
| 1789 | std::fs::remove_dir_all(&dir).ok(); |
| 1790 | } |
| 1791 | |
| 1792 | #[test] |
| 1793 | fn the_config_path_follows_xdg_then_home() { |
| 1794 | let p = |xdg: Option<&str>, home: Option<&str>| { |
| 1795 | config_path_in(xdg.map(Into::into), home.map(Into::into)) |
| 1796 | }; |
| 1797 | assert_eq!( |
| 1798 | p(Some("/x/config"), Some("/home/u")), |
| 1799 | std::path::Path::new("/x/config/edith/keybindings") |
| 1800 | ); |
| 1801 | assert_eq!( |
| 1802 | p(None, Some("/home/u")), |
| 1803 | std::path::Path::new("/home/u/.config/edith/keybindings") |
| 1804 | ); |
| 1805 | // An empty XDG_CONFIG_HOME is one the spec says to ignore. |
| 1806 | assert_eq!( |
| 1807 | p(Some(""), Some("/home/u")), |
| 1808 | std::path::Path::new("/home/u/.config/edith/keybindings") |
| 1809 | ); |
| 1810 | // Nowhere to put it but here -- still never `/edith/keybindings`. |
| 1811 | assert_eq!( |
| 1812 | p(None, None), |
| 1813 | std::path::Path::new(".config/edith/keybindings") |
| 1814 | ); |
| 1815 | } |
| 1816 | } |