| 1 | |
| 2 | |
| 3 | use crate::*; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | #[derive(Clone, Copy)] |
| 11 | pub(crate) enum MenuOn { |
| 12 | Clip(usize), |
| 13 | |
| 14 | |
| 15 | Gap(u32, u32), |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | Bench, |
| 22 | |
| 23 | |
| 24 | Empty, |
| 25 | |
| 26 | |
| 27 | Head, |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | Dock, |
| 34 | |
| 35 | |
| 36 | Viewer, |
| 37 | |
| 38 | |
| 39 | SeqTab, |
| 40 | |
| 41 | |
| 42 | Bin, |
| 43 | |
| 44 | |
| 45 | Marker, |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | #[derive(Clone, Copy)] |
| 54 | pub(crate) struct ContextMenu { |
| 55 | pub(crate) lane: Lane, |
| 56 | pub(crate) on: MenuOn, |
| 57 | pub(crate) at: Point<Pixels>, |
| 58 | pub(crate) details: bool, |
| 59 | |
| 60 | pub(crate) sub: Sub, |
| 61 | } |
| 62 | |
| 63 | |
| 64 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 65 | pub(crate) enum Sub { |
| 66 | Closed, |
| 67 | |
| 68 | Adjust, |
| 69 | |
| 70 | Compound, |
| 71 | |
| 72 | Label, |
| 73 | |
| 74 | Fit, |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | #[derive(Clone)] |
| 83 | pub(crate) struct LibraryMenu { |
| 84 | pub(crate) path: PathBuf, |
| 85 | pub(crate) stream: usize, |
| 86 | pub(crate) at: Point<Pixels>, |
| 87 | pub(crate) details: bool, |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | #[derive(Clone, Copy, PartialEq)] |
| 95 | pub(crate) struct Picker { |
| 96 | pub(crate) of: Pick, |
| 97 | pub(crate) at: Point<Pixels>, |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | pub(crate) sel: usize, |
| 103 | |
| 104 | |
| 105 | pub(crate) floored: bool, |
| 106 | |
| 107 | |
| 108 | pub(crate) above: Option<f32>, |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 115 | pub(crate) enum Pick { |
| 116 | Resolution, |
| 117 | Fps, |
| 118 | |
| 119 | |
| 120 | |
| 121 | SampleRate, |
| 122 | |
| 123 | SpeechModel, |
| 124 | Fit(Lane, usize), |
| 125 | |
| 126 | Label(Lane, usize), |
| 127 | |
| 128 | KeyEase(engine::project::KeyKind), |
| 129 | |
| 130 | ViewZoom, |
| 131 | OutputDisplay, |
| 132 | ViewQuality, |
| 133 | ViewMask, |
| 134 | ViewGuides, |
| 135 | |
| 136 | MarkerColor(usize), |
| 137 | |
| 138 | |
| 139 | #[allow(dead_code)] |
| 140 | AudioRate, |
| 141 | |
| 142 | |
| 143 | |
| 144 | #[allow(dead_code)] |
| 145 | Tone, |
| 146 | |
| 147 | |
| 148 | |
| 149 | Theme, |
| 150 | |
| 151 | Accent, |
| 152 | |
| 153 | |
| 154 | |
| 155 | #[allow(dead_code)] |
| 156 | Encoder, |
| 157 | |
| 158 | |
| 159 | |
| 160 | Picture, |
| 161 | |
| 162 | Export(crate::export_ui::ExportRow), |
| 163 | |
| 164 | Bin, |
| 165 | |
| 166 | Autosave, |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | #[derive(Clone, Copy, PartialEq, Debug)] |
| 176 | pub(crate) enum Choice { |
| 177 | Size(u32, u32), |
| 178 | Fps(f64), |
| 179 | |
| 180 | |
| 181 | |
| 182 | SampleRate(Option<u32>), |
| 183 | Fit(Lane, usize, FitPolicy), |
| 184 | |
| 185 | Label(Lane, usize, u8), |
| 186 | KeyEase(engine::project::KeyKind, engine::transform::KeyEase), |
| 187 | ViewZoom(crate::viewer_view::Zoom), |
| 188 | |
| 189 | OutputDisplay(usize), |
| 190 | ViewQuality(crate::viewer_view::Quality), |
| 191 | ViewMask(crate::viewer_view::Mask), |
| 192 | ViewGuides(crate::viewer_view::Guide), |
| 193 | MarkerColor(usize, engine::project::MarkerColor), |
| 194 | AudioRate(u32), |
| 195 | Tone(Preset), |
| 196 | Theme(ui::theme::PaletteId), |
| 197 | Accent(ui::theme::AccentId), |
| 198 | Encoder(EncoderSeat), |
| 199 | Picture(Format), |
| 200 | |
| 201 | Export(crate::export_ui::ExportPick), |
| 202 | |
| 203 | Bin(u32), |
| 204 | |
| 205 | Autosave(Option<u64>), |
| 206 | |
| 207 | SpeechModel(Option<&'static str>), |
| 208 | } |
| 209 | |
| 210 | |
| 211 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 212 | pub(crate) enum LedgerItem { |
| 213 | Size, |
| 214 | Clip, |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | pub(crate) type ChoiceRow = (Choice, SharedString, SharedString, bool); |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | #[derive(Clone, Copy, PartialEq)] |
| 226 | pub(crate) enum RowItem { |
| 227 | |
| 228 | |
| 229 | Relink, |
| 230 | |
| 231 | |
| 232 | Add, |
| 233 | |
| 234 | |
| 235 | Insert, |
| 236 | |
| 237 | |
| 238 | |
| 239 | Preview, |
| 240 | |
| 241 | |
| 242 | |
| 243 | Proxy, |
| 244 | |
| 245 | |
| 246 | CreateMulticam, |
| 247 | |
| 248 | |
| 249 | Remove, |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | RemoveWithClips, |
| 255 | Reveal, |
| 256 | Properties, |
| 257 | |
| 258 | Favourite, |
| 259 | |
| 260 | MoveToBin, |
| 261 | |
| 262 | NewBin, |
| 263 | } |
| 264 | |
| 265 | |
| 266 | |
| 267 | pub(crate) const ROW_ITEMS: [RowItem; 13] = [ |
| 268 | RowItem::Relink, |
| 269 | RowItem::Add, |
| 270 | RowItem::Insert, |
| 271 | RowItem::Preview, |
| 272 | RowItem::Proxy, |
| 273 | RowItem::CreateMulticam, |
| 274 | RowItem::Favourite, |
| 275 | RowItem::MoveToBin, |
| 276 | RowItem::NewBin, |
| 277 | RowItem::Reveal, |
| 278 | RowItem::Properties, |
| 279 | RowItem::Remove, |
| 280 | RowItem::RemoveWithClips, |
| 281 | ]; |
| 282 | |
| 283 | impl RowItem { |
| 284 | |
| 285 | pub(crate) fn icon(self) -> crate::ui::icons::Icon { |
| 286 | use crate::ui::icons::Icon as I; |
| 287 | match self { |
| 288 | Self::Relink => I::Link, |
| 289 | Self::Add | Self::Insert => I::Plus, |
| 290 | Self::Preview => I::Play, |
| 291 | Self::Proxy => I::Gauge, |
| 292 | Self::Remove | Self::RemoveWithClips => I::Trash2, |
| 293 | Self::Reveal => I::FolderOpen, |
| 294 | Self::Properties => I::Info, |
| 295 | Self::Favourite => I::Star, |
| 296 | Self::MoveToBin | Self::NewBin => I::Folder, |
| 297 | Self::CreateMulticam => I::Layers, |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | pub(crate) fn label(self) -> &'static str { |
| 302 | match self { |
| 303 | Self::Relink => "Relink…", |
| 304 | Self::Add => "Append to timeline end", |
| 305 | Self::Insert => "Insert at playhead", |
| 306 | Self::CreateMulticam => "Create multicam…", |
| 307 | Self::Preview => "Open in source viewer", |
| 308 | Self::Proxy => "Make editing copy", |
| 309 | Self::Remove => "Remove from library", |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | Self::RemoveWithClips => "Remove with its clips", |
| 317 | Self::Reveal => "Reveal in files", |
| 318 | Self::Properties => "Properties", |
| 319 | Self::Favourite => "Favourite", |
| 320 | Self::MoveToBin => "Move to bin…", |
| 321 | Self::NewBin => "New bin", |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
| 326 | |
| 327 | pub(crate) fn label_in(self, ctx: RowCtx) -> &'static str { |
| 328 | match self { |
| 329 | Self::Proxy if ctx.proxy_stops => "Stop editing copy", |
| 330 | Self::Favourite if ctx.starred => "Remove from favourites", |
| 331 | _ => self.label(), |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | |
| 336 | |
| 337 | pub(crate) fn destructive(self) -> bool { |
| 338 | matches!(self, Self::Remove | Self::RemoveWithClips) |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | pub(crate) fn hint(self) -> &'static str { |
| 352 | match self { |
| 353 | Self::Add | Self::Insert | Self::CreateMulticam => "", |
| 354 | Self::Preview => "↵", |
| 355 | Self::Relink |
| 356 | | Self::Proxy |
| 357 | | Self::Remove |
| 358 | | Self::RemoveWithClips |
| 359 | | Self::Reveal |
| 360 | | Self::Properties |
| 361 | | Self::Favourite |
| 362 | | Self::MoveToBin |
| 363 | | Self::NewBin => "", |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | pub(crate) const MENU_ITEMS: [ActionId; 31] = [ |
| 373 | ActionId::Cut, |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | ActionId::TrimIn, |
| 383 | ActionId::TrimOut, |
| 384 | ActionId::LoopTrim, |
| 385 | |
| 386 | |
| 387 | ActionId::Copy, |
| 388 | ActionId::Paste, |
| 389 | |
| 390 | ActionId::ClipLabel, |
| 391 | ActionId::ClipEnabled, |
| 392 | |
| 393 | |
| 394 | |
| 395 | ActionId::Crossfade, |
| 396 | ActionId::Dissolve, |
| 397 | |
| 398 | ActionId::Color, |
| 399 | ActionId::Transform, |
| 400 | ActionId::Effects, |
| 401 | |
| 402 | ActionId::Equalizer, |
| 403 | ActionId::Silence, |
| 404 | ActionId::MarkBeats, |
| 405 | ActionId::Visualizer, |
| 406 | ActionId::Reframe, |
| 407 | ActionId::TrackMask, |
| 408 | ActionId::Speed, |
| 409 | ActionId::Reverse, |
| 410 | ActionId::Fit, |
| 411 | |
| 412 | ActionId::NestClips, |
| 413 | ActionId::BreakApart, |
| 414 | ActionId::OpenCompound, |
| 415 | ActionId::FlattenMulticam, |
| 416 | |
| 417 | |
| 418 | |
| 419 | ActionId::Group, |
| 420 | ActionId::Detach, |
| 421 | ActionId::Regroup, |
| 422 | |
| 423 | |
| 424 | ActionId::Delete, |
| 425 | ActionId::Lift, |
| 426 | ]; |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | pub(crate) const BENCH_ITEMS: [ActionId; 22] = [ |
| 438 | |
| 439 | |
| 440 | ActionId::MarkerHere, |
| 441 | ActionId::PlayheadHere, |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | ActionId::GoStart, |
| 448 | ActionId::GoEnd, |
| 449 | ActionId::SetIn, |
| 450 | ActionId::SetOut, |
| 451 | ActionId::ClearRange, |
| 452 | |
| 453 | |
| 454 | |
| 455 | ActionId::SelectAll, |
| 456 | ActionId::Paste, |
| 457 | ActionId::WalkCutPrev, |
| 458 | ActionId::WalkCutNext, |
| 459 | ActionId::WalkCutPrev10, |
| 460 | ActionId::WalkCutNext10, |
| 461 | |
| 462 | |
| 463 | ActionId::PrevMarker, |
| 464 | ActionId::NextMarker, |
| 465 | ActionId::Markers, |
| 466 | ActionId::ZoomOut, |
| 467 | ActionId::ZoomIn, |
| 468 | ActionId::ZoomFit, |
| 469 | ActionId::ToggleSnap, |
| 470 | ActionId::Undo, |
| 471 | ActionId::Redo, |
| 472 | ]; |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | pub(crate) const VIEWER_ITEMS: [ActionId; 5] = [ |
| 478 | ActionId::Play, |
| 479 | ActionId::Loop, |
| 480 | ActionId::Screenshot, |
| 481 | ActionId::Fullscreen, |
| 482 | ActionId::Settings, |
| 483 | ]; |
| 484 | |
| 485 | |
| 486 | pub(crate) const SEQ_TAB_ITEMS: [ActionId; 3] = |
| 487 | [ActionId::RenameSequence, ActionId::DuplicateSequence, ActionId::DeleteSequence]; |
| 488 | |
| 489 | |
| 490 | |
| 491 | pub(crate) const MARKER_ITEMS: [ActionId; 2] = [ActionId::RenameMarker, ActionId::DeleteMarker]; |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | pub(crate) const BIN_ITEMS: [ActionId; 3] = [ActionId::RenameBin, ActionId::NewBinInside, ActionId::RemoveBin]; |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | pub(crate) const DOCK_ITEMS: [ActionId; 13] = [ |
| 504 | ActionId::AddFiles, |
| 505 | ActionId::PasteFilePath, |
| 506 | ActionId::ImportSubtitles, |
| 507 | ActionId::NewBin, |
| 508 | ActionId::CreateMulticam, |
| 509 | ActionId::NewSolid, |
| 510 | ActionId::NewGradient, |
| 511 | ActionId::NewCountdown, |
| 512 | ActionId::NewBars, |
| 513 | ActionId::NewTone, |
| 514 | ActionId::NewBarsAndTone, |
| 515 | ActionId::NewAdjustment, |
| 516 | ActionId::NewTitle, |
| 517 | ]; |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | pub(crate) fn empty_items(_lane: Lane) -> Vec<ActionId> { |
| 525 | vec![ActionId::Paste, ActionId::AddVideoLane, ActionId::AddAudioLane] |
| 526 | } |
| 527 | |
| 528 | pub(crate) const HEAD_ADDS: [ActionId; 3] = [ |
| 529 | ActionId::AddVideoLane, |
| 530 | ActionId::AddAudioLane, |
| 531 | ActionId::AddSubtitleLane, |
| 532 | ]; |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | pub(crate) fn adjust_item(action: ActionId) -> bool { |
| 541 | matches!( |
| 542 | action, |
| 543 | ActionId::Color |
| 544 | | ActionId::Transform |
| 545 | | ActionId::Effects |
| 546 | | ActionId::Equalizer |
| 547 | | ActionId::Silence |
| 548 | | ActionId::MarkBeats |
| 549 | | ActionId::Visualizer |
| 550 | | ActionId::Reframe |
| 551 | | ActionId::TrackMask |
| 552 | | ActionId::Speed |
| 553 | ) |
| 554 | } |
| 555 | |
| 556 | |
| 557 | pub(crate) fn compound_item(action: ActionId) -> bool { |
| 558 | matches!( |
| 559 | action, |
| 560 | ActionId::NestClips | ActionId::BreakApart | ActionId::OpenCompound | ActionId::FlattenMulticam |
| 561 | ) |
| 562 | } |
| 563 | |
| 564 | pub(crate) fn destructive(action: ActionId) -> bool { |
| 565 | matches!( |
| 566 | action, |
| 567 | ActionId::Delete |
| 568 | | ActionId::Lift |
| 569 | | ActionId::RemoveVideoLane |
| 570 | | ActionId::RemoveAudioLane |
| 571 | | ActionId::RemoveSubtitleLane |
| 572 | | ActionId::DeleteSequence |
| 573 | | ActionId::RemoveBin |
| 574 | ) |
| 575 | } |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | #[derive(Clone, Copy, Debug)] |
| 585 | pub(crate) enum KeyRow { |
| 586 | Head(keymap::Category), |
| 587 | |
| 588 | Act(ActionId), |
| 589 | |
| 590 | |
| 591 | Fixed(usize), |
| 592 | } |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | pub(crate) fn keys_rows() -> Vec<KeyRow> { |
| 599 | let mut rows = Vec::new(); |
| 600 | for category in keymap::Category::ALL { |
| 601 | rows.push(KeyRow::Head(category)); |
| 602 | |
| 603 | |
| 604 | |
| 605 | rows.extend( |
| 606 | ActionId::ALL |
| 607 | .into_iter() |
| 608 | .filter(|a| a.category() == category) |
| 609 | .filter(|a| a.angle_number().is_none_or(|n| n == 1)) |
| 610 | .map(KeyRow::Act), |
| 611 | ); |
| 612 | rows.extend( |
| 613 | keymap::FIXED |
| 614 | .iter() |
| 615 | .enumerate() |
| 616 | .filter(|(_, f)| f.category == category) |
| 617 | .map(|(i, _)| KeyRow::Fixed(i)), |
| 618 | ); |
| 619 | } |
| 620 | rows |
| 621 | } |
| 622 | |
| 623 | |
| 624 | |
| 625 | pub(crate) fn keys_rows_matching(query: &str, keymap: &keymap::Keymap) -> Vec<KeyRow> { |
| 626 | let q = query.trim().to_ascii_lowercase(); |
| 627 | if q.is_empty() { |
| 628 | return keys_rows(); |
| 629 | } |
| 630 | let rows = keys_rows(); |
| 631 | let mut out = Vec::new(); |
| 632 | let mut i = 0; |
| 633 | while i < rows.len() { |
| 634 | match rows[i] { |
| 635 | KeyRow::Head(cat) => { |
| 636 | let mut kids = Vec::new(); |
| 637 | i += 1; |
| 638 | while i < rows.len() && !matches!(rows[i], KeyRow::Head(_)) { |
| 639 | if key_row_hits(&rows[i], &q, keymap) { |
| 640 | kids.push(rows[i]); |
| 641 | } |
| 642 | i += 1; |
| 643 | } |
| 644 | if !kids.is_empty() { |
| 645 | out.push(KeyRow::Head(cat)); |
| 646 | out.extend(kids); |
| 647 | } |
| 648 | } |
| 649 | _ => i += 1, |
| 650 | } |
| 651 | } |
| 652 | out |
| 653 | } |
| 654 | |
| 655 | fn key_row_hits(row: &KeyRow, q: &str, keymap: &keymap::Keymap) -> bool { |
| 656 | match *row { |
| 657 | KeyRow::Head(c) => c.label().to_ascii_lowercase().contains(q), |
| 658 | KeyRow::Act(a) => { |
| 659 | a.label().to_ascii_lowercase().contains(q) |
| 660 | || keymap.display(a).to_ascii_lowercase().contains(q) |
| 661 | || keymap::search_aliases(&keymap.display(a)) |
| 662 | .to_ascii_lowercase() |
| 663 | .contains(q) |
| 664 | } |
| 665 | KeyRow::Fixed(i) => { |
| 666 | let f = &keymap::FIXED[i]; |
| 667 | f.label.to_ascii_lowercase().contains(q) |
| 668 | || f.chord.to_ascii_lowercase().contains(q) |
| 669 | || keymap::search_aliases(&f.chord) |
| 670 | .to_ascii_lowercase() |
| 671 | .contains(q) |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | pub(crate) fn typed(key: &str) -> Option<char> { |
| 681 | match key { |
| 682 | "space" => Some(' '), |
| 683 | _ => key |
| 684 | .chars() |
| 685 | .next() |
| 686 | .filter(|c| c.is_ascii_graphic() && key.chars().count() == 1), |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 694 | pub(crate) struct PaletteRow { |
| 695 | pub(crate) action: ActionId, |
| 696 | |
| 697 | |
| 698 | pub(crate) refused: Option<&'static str>, |
| 699 | } |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | pub(crate) fn palette_rows( |
| 711 | query: &str, |
| 712 | enable: impl Fn(ActionId) -> crate::oracle::Enable, |
| 713 | ) -> Vec<PaletteRow> { |
| 714 | let tokens: Vec<String> = query |
| 715 | .split_whitespace() |
| 716 | .map(str::to_lowercase) |
| 717 | .collect(); |
| 718 | |
| 719 | let mut hits: Vec<(u32, usize, PaletteRow)> = Vec::new(); |
| 720 | for (i, action) in ActionId::ALL.into_iter().enumerate() { |
| 721 | let refused = match enable(action) { |
| 722 | crate::oracle::Enable::Hidden(_) => continue, |
| 723 | crate::oracle::Enable::Yes => None, |
| 724 | crate::oracle::Enable::No(why) => Some(why), |
| 725 | }; |
| 726 | let Some(score) = palette_score(action, &tokens) else { |
| 727 | continue; |
| 728 | }; |
| 729 | hits.push((score, i, PaletteRow { action, refused })); |
| 730 | } |
| 731 | if tokens.is_empty() { |
| 732 | hits.sort_by_key(|(_, i, row)| (category_rank(row.action.category()), *i)); |
| 733 | } else { |
| 734 | hits.sort_by_key(|(score, i, _)| (*score, *i)); |
| 735 | } |
| 736 | hits.into_iter().map(|(_, _, row)| row).collect() |
| 737 | } |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | fn palette_score(action: ActionId, tokens: &[String]) -> Option<u32> { |
| 743 | let label = action.label().to_lowercase(); |
| 744 | let category = action.category().label().to_lowercase(); |
| 745 | let mut total = 0; |
| 746 | for token in tokens { |
| 747 | let word_starts = |text: &str| { |
| 748 | text.split(|c: char| !c.is_alphanumeric()) |
| 749 | .any(|w| w.starts_with(token.as_str())) |
| 750 | }; |
| 751 | total += if label.starts_with(token.as_str()) { |
| 752 | 0 |
| 753 | } else if word_starts(&label) { |
| 754 | 1 |
| 755 | } else if label.contains(token.as_str()) { |
| 756 | 2 |
| 757 | } else if word_starts(&category) || category.contains(token.as_str()) { |
| 758 | 3 |
| 759 | } else { |
| 760 | return None; |
| 761 | }; |
| 762 | } |
| 763 | Some(total) |
| 764 | } |
| 765 | |
| 766 | |
| 767 | |
| 768 | fn category_rank(category: keymap::Category) -> usize { |
| 769 | keymap::Category::ALL |
| 770 | .iter() |
| 771 | .position(|c| *c == category) |
| 772 | .unwrap_or(usize::MAX) |
| 773 | } |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | pub(crate) fn palette_child_index(rows: &[PaletteRow], sel: usize, grouped: bool) -> usize { |
| 779 | if !grouped { |
| 780 | return sel; |
| 781 | } |
| 782 | |
| 783 | let headings = (0..rows.len().min(sel + 1)) |
| 784 | .filter(|&i| i == 0 || rows[i - 1].action.category() != rows[i].action.category()) |
| 785 | .count(); |
| 786 | sel + headings |
| 787 | } |
| 788 | |
| 789 | |
| 790 | |
| 791 | pub(crate) fn palette_closes_on_run(row: PaletteRow) -> bool { |
| 792 | row.refused.is_none() |
| 793 | } |
| 794 | |
| 795 | #[cfg(test)] |
| 796 | mod palette_tests { |
| 797 | use super::*; |
| 798 | use crate::oracle::Enable; |
| 799 | |
| 800 | fn all_yes(_: ActionId) -> Enable { |
| 801 | Enable::Yes |
| 802 | } |
| 803 | |
| 804 | #[test] |
| 805 | fn empty_query_lists_every_action_grouped_by_category() { |
| 806 | let rows = palette_rows("", all_yes); |
| 807 | assert_eq!(rows.len(), ActionId::ALL.len()); |
| 808 | let cats: Vec<usize> = rows |
| 809 | .iter() |
| 810 | .map(|r| category_rank(r.action.category())) |
| 811 | .collect(); |
| 812 | assert!(cats.windows(2).all(|w| w[0] <= w[1]), "not grouped: {cats:?}"); |
| 813 | } |
| 814 | |
| 815 | #[test] |
| 816 | fn a_label_prefix_ranks_above_a_word_inside_a_label() { |
| 817 | let rows = palette_rows("play", all_yes); |
| 818 | let first = rows[0].action.label().to_lowercase(); |
| 819 | assert!(first.starts_with("play"), "best match was {first:?}"); |
| 820 | assert!( |
| 821 | rows.iter().all(|r| { |
| 822 | let l = r.action.label().to_lowercase(); |
| 823 | l.contains("play") || r.action.category().label().to_lowercase().contains("play") |
| 824 | }), |
| 825 | "a row matches neither label nor category" |
| 826 | ); |
| 827 | } |
| 828 | |
| 829 | #[test] |
| 830 | fn matching_is_case_insensitive() { |
| 831 | assert_eq!(palette_rows("PLAY", all_yes), palette_rows("play", all_yes)); |
| 832 | } |
| 833 | |
| 834 | #[test] |
| 835 | fn a_category_word_lists_its_actions() { |
| 836 | let rows = palette_rows("clips", all_yes); |
| 837 | assert!(!rows.is_empty()); |
| 838 | assert!(rows.iter().all(|r| r.action.category() == keymap::Category::Clips |
| 839 | || r.action.label().to_lowercase().contains("clip"))); |
| 840 | } |
| 841 | |
| 842 | #[test] |
| 843 | fn every_word_of_the_query_must_match() { |
| 844 | assert!(palette_rows("play xyzzy", all_yes).is_empty()); |
| 845 | assert!(palette_rows("xyzzy-no-such", all_yes).is_empty()); |
| 846 | } |
| 847 | |
| 848 | #[test] |
| 849 | fn hidden_actions_are_omitted_and_refused_ones_stay_listed() { |
| 850 | let rows = palette_rows("", |a| match a { |
| 851 | ActionId::Cut => Enable::Hidden("no clip"), |
| 852 | ActionId::Copy => Enable::No("nothing selected"), |
| 853 | _ => Enable::Yes, |
| 854 | }); |
| 855 | assert!(!rows.iter().any(|r| r.action == ActionId::Cut)); |
| 856 | let copy = rows.iter().find(|r| r.action == ActionId::Copy).unwrap(); |
| 857 | assert_eq!(copy.refused, Some("nothing selected")); |
| 858 | let play = rows.iter().find(|r| r.action == ActionId::Play).unwrap(); |
| 859 | assert_eq!(play.refused, None); |
| 860 | } |
| 861 | |
| 862 | #[test] |
| 863 | fn only_a_runnable_row_closes_the_palette() { |
| 864 | let yes = PaletteRow { action: ActionId::Play, refused: None }; |
| 865 | let no = PaletteRow { action: ActionId::Copy, refused: Some("nothing selected") }; |
| 866 | assert!(palette_closes_on_run(yes)); |
| 867 | assert!(!palette_closes_on_run(no)); |
| 868 | } |
| 869 | |
| 870 | #[test] |
| 871 | fn child_index_counts_the_category_headings_above_the_row() { |
| 872 | let rows = palette_rows("", all_yes); |
| 873 | assert_eq!(palette_child_index(&rows, 0, true), 1); |
| 874 | let last = rows.len() - 1; |
| 875 | let blocks = keymap::Category::ALL |
| 876 | .iter() |
| 877 | .filter(|c| rows.iter().any(|r| r.action.category() == **c)) |
| 878 | .count(); |
| 879 | assert_eq!(palette_child_index(&rows, last, true), last + blocks); |
| 880 | assert_eq!(palette_child_index(&rows, 3, false), 3); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | #[cfg(test)] |
| 885 | mod keys_search_tests { |
| 886 | use super::*; |
| 887 | |
| 888 | |
| 889 | |
| 890 | #[test] |
| 891 | fn a_glyph_row_is_found_by_its_spoken_name() { |
| 892 | let keymap = keymap::Keymap::defaults(); |
| 893 | let finds = |q: &str, chord: &str| { |
| 894 | keys_rows_matching(q, &keymap).iter().any(|row| match row { |
| 895 | KeyRow::Fixed(i) => keymap::FIXED[*i].chord == chord, |
| 896 | _ => false, |
| 897 | }) |
| 898 | }; |
| 899 | assert!(finds("enter", "↵")); |
| 900 | assert!(finds("return", "↵")); |
| 901 | assert!(finds("up", "↑")); |
| 902 | assert!(finds("left arrow", "← / →")); |
| 903 | assert!(finds("escape", "Esc")); |
| 904 | assert!(finds("esc", "Esc")); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | pub(crate) fn icon(action: ActionId) -> Option<crate::ui::icons::Icon> { |
| 912 | use crate::ui::icons::Icon as I; |
| 913 | Some(match action { |
| 914 | ActionId::AddFiles => I::Plus, |
| 915 | ActionId::OpenProject => I::FolderOpen, |
| 916 | ActionId::NewProject => I::Clapperboard, |
| 917 | ActionId::ImportSubtitles => I::Captions, |
| 918 | ActionId::Save => I::Save, |
| 919 | ActionId::ReloadProject => I::RefreshCw, |
| 920 | ActionId::Export => I::Upload, |
| 921 | ActionId::Screenshot => I::Image, |
| 922 | ActionId::Settings => I::Settings, |
| 923 | ActionId::Undo => I::Undo2, |
| 924 | ActionId::Redo => I::Redo2, |
| 925 | ActionId::History => I::Undo2, |
| 926 | ActionId::Copy => I::Copy, |
| 927 | ActionId::Paste | ActionId::PasteFilePath => I::Clipboard, |
| 928 | ActionId::SelectAll => I::Layers, |
| 929 | ActionId::Cut => I::Scissors, |
| 930 | ActionId::Delete => I::Trash2, |
| 931 | ActionId::Lift => I::Eraser, |
| 932 | ActionId::Color => I::Palette, |
| 933 | ActionId::Transform => I::Move, |
| 934 | ActionId::Effects => I::Sparkles, |
| 935 | ActionId::Equalizer | ActionId::Mix => I::SlidersHorizontal, |
| 936 | ActionId::Speed => I::FastForward, |
| 937 | ActionId::Group | ActionId::NestClips => I::Layers, |
| 938 | ActionId::Detach => I::Unlink, |
| 939 | ActionId::ToggleMute => I::VolumeX, |
| 940 | ActionId::SetIn => I::SquareArrowDownRight, |
| 941 | ActionId::SetOut => I::SquareArrowDownLeft, |
| 942 | ActionId::ZoomIn => I::ZoomIn, |
| 943 | ActionId::ZoomOut => I::ZoomOut, |
| 944 | ActionId::ZoomFit | ActionId::ZoomToSelection => I::Scan, |
| 945 | ActionId::ViewZoom => I::ZoomIn, |
| 946 | ActionId::PlaybackQuality => I::Signal, |
| 947 | ActionId::GuideStyle => I::Grid3x3, |
| 948 | ActionId::GuideMask => I::Image, |
| 949 | ActionId::ToggleOutputWindow | ActionId::OutputDisplay => I::Scan, |
| 950 | ActionId::ToggleSnap => I::Magnet, |
| 951 | |
| 952 | ActionId::TrackSmall | ActionId::TrackMedium | ActionId::TrackLarge | ActionId::TracksSmaller | ActionId::TracksLarger => I::Scaling, |
| 953 | ActionId::AddMarker | ActionId::MarkerHere => I::Flag, |
| 954 | ActionId::RenameMarker => I::Type, |
| 955 | ActionId::DeleteMarker => I::Trash2, |
| 956 | ActionId::PlayheadHere => I::SkipBack, |
| 957 | ActionId::Captions => I::Captions, |
| 958 | ActionId::Voiceover | ActionId::RecordVoiceover => I::Mic, |
| 959 | ActionId::Play => I::Play, |
| 960 | ActionId::StepBack => I::StepBack, |
| 961 | ActionId::StepForward => I::StepForward, |
| 962 | ActionId::GoStart => I::SkipBack, |
| 963 | ActionId::GoEnd => I::SkipForward, |
| 964 | ActionId::GoToTime => I::Timer, |
| 965 | ActionId::Loop => I::Repeat, |
| 966 | ActionId::PlayAround => I::Play, |
| 967 | ActionId::ToggleWaveforms => I::AudioWaveform, |
| 968 | ActionId::ToggleScrubAudio => I::Volume2, |
| 969 | ActionId::CompareOff | ActionId::CompareSplit | ActionId::CompareSide => I::Layers, |
| 970 | ActionId::AutoColour => I::WandSparkles, |
| 971 | ActionId::SaveLook => I::Save, |
| 972 | ActionId::ToggleSubtitles => I::Captions, |
| 973 | ActionId::ShowActions => I::Search, |
| 974 | ActionId::ShowKeys => I::Keyboard, |
| 975 | ActionId::About => I::Info, |
| 976 | ActionId::Normalize | ActionId::CleanVoice => I::AudioWaveform, |
| 977 | ActionId::SubtitleStyle => I::Type, |
| 978 | ActionId::Reframe => I::Scaling, |
| 979 | ActionId::TrackMask => I::Scan, |
| 980 | ActionId::TrimIn => I::ArrowLeftToLine, |
| 981 | ActionId::TrimOut => I::ArrowRightToLine, |
| 982 | ActionId::Regroup => I::Link, |
| 983 | ActionId::LoopTrim => I::Repeat, |
| 984 | ActionId::Reverse => I::RotateCcw, |
| 985 | ActionId::Dissolve | ActionId::Crossfade | ActionId::TransitionDissolve => I::Sparkles, |
| 986 | ActionId::TransitionDipBlack => I::Square, |
| 987 | ActionId::TransitionDipWhite => I::Circle, |
| 988 | ActionId::TransitionZoom => I::ZoomIn, |
| 989 | ActionId::TransitionWipeLeft => I::SquareArrowDownLeft, |
| 990 | ActionId::TransitionWipeRight => I::SquareArrowDownRight, |
| 991 | ActionId::TransitionWipeUp => I::ArrowUp, |
| 992 | ActionId::TransitionWipeDown => I::ArrowDown, |
| 993 | ActionId::TransitionSlideLeft => I::ChevronLeft, |
| 994 | ActionId::TransitionSlideRight => I::ChevronRight, |
| 995 | ActionId::TransitionSlideUp => I::ChevronUp, |
| 996 | ActionId::TransitionSlideDown => I::ChevronDown, |
| 997 | ActionId::TransitionPushLeft | ActionId::TransitionPushRight | ActionId::TransitionPushUp | ActionId::TransitionPushDown => I::Layers, |
| 998 | ActionId::ClipLabel => I::Flag, |
| 999 | ActionId::AddVideoLane | ActionId::AddAudioLane | ActionId::AddSubtitleLane | ActionId::NewSequence => I::Plus, |
| 1000 | ActionId::RemoveVideoLane | ActionId::RemoveAudioLane | ActionId::RemoveSubtitleLane => I::Minus, |
| 1001 | ActionId::RenameLane | ActionId::RenameSequence | ActionId::RenameBin | ActionId::NewTitle => I::Type, |
| 1002 | ActionId::DeleteSequence | ActionId::RemoveBin => I::Trash2, |
| 1003 | ActionId::DuplicateSequence => I::Copy, |
| 1004 | ActionId::NewBin | ActionId::NewBinInside => I::Folder, |
| 1005 | ActionId::NewSolid => I::Square, |
| 1006 | ActionId::NewGradient => I::Palette, |
| 1007 | ActionId::NewCountdown => I::Timer, |
| 1008 | ActionId::NewBars => I::Grid3x3, |
| 1009 | ActionId::NewTone => I::Volume2, |
| 1010 | ActionId::NewBarsAndTone => I::AudioWaveform, |
| 1011 | ActionId::NewAdjustment => I::SlidersHorizontal, |
| 1012 | ActionId::NewRectangle => I::Square, |
| 1013 | ActionId::NewEllipse => I::Circle, |
| 1014 | ActionId::NewLine => I::Minus, |
| 1015 | ActionId::NewArrow => I::ArrowRight, |
| 1016 | ActionId::BreakApart => I::Unlink, |
| 1017 | ActionId::CancelExport | ActionId::Deselect => I::CircleX, |
| 1018 | ActionId::ClearRange => I::Eraser, |
| 1019 | ActionId::RenderInOut => I::SquareArrowDownRight, |
| 1020 | ActionId::RenderAll => I::Film, |
| 1021 | ActionId::CancelRender => I::CircleX, |
| 1022 | ActionId::DeleteRenderFiles => I::Trash2, |
| 1023 | ActionId::ClipEnabled | ActionId::ToggleAngleMonitor | ActionId::KeyOpacity => I::Eye, |
| 1024 | ActionId::CloseCompound | ActionId::OpenCompound | ActionId::CreateMulticam | ActionId::FlattenMulticam | ActionId::Angle1 => { |
| 1025 | I::Layers |
| 1026 | } |
| 1027 | ActionId::CloseGap | ActionId::TrimInToPlayhead | ActionId::RippleTrimInToPlayhead => I::ArrowLeftToLine, |
| 1028 | ActionId::ExtendAudio | ActionId::ExtendVideo | ActionId::TrimOutToPlayhead | ActionId::RippleTrimOutToPlayhead => { |
| 1029 | I::ArrowRightToLine |
| 1030 | } |
| 1031 | ActionId::CollectMedia | ActionId::InsertSource | ActionId::OverwriteSource => I::Download, |
| 1032 | ActionId::ExportTimelineOtio | ActionId::ExportTimelineFcpxml | ActionId::ExportTimelineEdl => I::Upload, |
| 1033 | ActionId::ImportTimelineOtio => I::Plus, |
| 1034 | ActionId::Fillers | ActionId::Silence | ActionId::Visualizer => I::AudioWaveform, |
| 1035 | ActionId::CutSelectedWords => I::Trash2, |
| 1036 | ActionId::MarkBeats => I::Timer, |
| 1037 | ActionId::Fit | ActionId::Resolution => I::Scaling, |
| 1038 | ActionId::Fullscreen => I::Scan, |
| 1039 | ActionId::FocusPanels => I::Layers, |
| 1040 | ActionId::Freeze | ActionId::ShuttleStop => I::Pause, |
| 1041 | ActionId::JumpBack | ActionId::PrevSyncPoint => I::SkipBack, |
| 1042 | ActionId::JumpForward | ActionId::NextSyncPoint => I::SkipForward, |
| 1043 | ActionId::KeyColor | ActionId::Theme | ActionId::AccentColour => I::Palette, |
| 1044 | ActionId::KeyEasing => I::Spline, |
| 1045 | ActionId::SpeedRampClear => I::Eraser, |
| 1046 | ActionId::SpeedRampDown => I::TrendingDown, |
| 1047 | ActionId::SpeedRampUp => I::TrendingUp, |
| 1048 | ActionId::SpeedRampSlowFastSlow => I::Activity, |
| 1049 | ActionId::SpeedRampFastSlowFast => I::Zap, |
| 1050 | ActionId::SpeedRampKey => I::Diamond, |
| 1051 | ActionId::ToggleProxies | ActionId::ToggleAutoProxies => I::Gauge, |
| 1052 | ActionId::KeyPlacement => I::Move, |
| 1053 | ActionId::Markers | ActionId::NextMarker | ActionId::PrevMarker => I::FlagTriangleRight, |
| 1054 | ActionId::MatchFrame | ActionId::ReplaceEdit => I::Film, |
| 1055 | ActionId::NudgeBack | ActionId::NudgeBack10 | ActionId::RollBack | ActionId::SlipBack | ActionId::SlideBack | ActionId::ShuttleBack => I::StepBack, |
| 1056 | ActionId::NudgeForward | ActionId::NudgeForward10 | ActionId::RollForward | ActionId::SlipForward | ActionId::SlideForward | ActionId::ShuttleForward => I::StepForward, |
| 1057 | ActionId::PasteAttributes | ActionId::PasteAudio | ActionId::PasteColor | ActionId::PasteEffects | ActionId::PasteSpeed | ActionId::PasteTransform => I::Clipboard, |
| 1058 | ActionId::SaveVersion | ActionId::SaveTemplate | ActionId::ExportCaptions => I::Save, |
| 1059 | ActionId::Versions => I::RotateCcw, |
| 1060 | ActionId::ProjectNotes => I::Clipboard, |
| 1061 | ActionId::Select | ActionId::SelectForward | ActionId::SelectForwardAll => I::Brackets, |
| 1062 | ActionId::SelectNext | ActionId::WalkCutNext | ActionId::WalkCutNext10 => I::ChevronRight, |
| 1063 | ActionId::SelectPrev | ActionId::WalkCutPrev | ActionId::WalkCutPrev10 => I::ChevronLeft, |
| 1064 | ActionId::VoiceDynamics => I::Mic, |
| 1065 | ActionId::VolumeDown | ActionId::VolumeUp => I::Volume2, |
| 1066 | _ => return None, |
| 1067 | }) |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | pub(crate) fn note(action: ActionId) -> Option<&'static str> { |
| 1073 | Some(match action { |
| 1074 | ActionId::SlipBack | ActionId::SlipForward => "Slip: change which part of the footage shows, keeping the clip in place", |
| 1075 | ActionId::SlideBack | ActionId::SlideForward => "Slide: move the clip between its neighbours, keeping its footage", |
| 1076 | ActionId::Detach => "Unlink: the picture and the sound of a grouped clip edit separately", |
| 1077 | _ => return None, |
| 1078 | }) |
| 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | pub(crate) fn icon_sub(name: &str) -> Option<crate::ui::icons::Icon> { |
| 1083 | use crate::ui::icons::Icon as I; |
| 1084 | Some(match name { |
| 1085 | "Paste attributes" => I::Clipboard, |
| 1086 | "Export timeline as" => I::Upload, |
| 1087 | "New from template" => I::Layers, |
| 1088 | "Compare grade" => I::Layers, |
| 1089 | "Trim" => I::Scissors, |
| 1090 | "Speed" => I::FastForward, |
| 1091 | "Adjust" => I::SlidersHorizontal, |
| 1092 | "Transitions" => I::Sparkles, |
| 1093 | "Compound" => I::Layers, |
| 1094 | "Markers" => I::Flag, |
| 1095 | "Select" => I::Brackets, |
| 1096 | "Captions" => I::Captions, |
| 1097 | "Audio" => I::AudioWaveform, |
| 1098 | "Tracks" => I::Film, |
| 1099 | "Keyframes" => I::KeyRound, |
| 1100 | "Go to" => I::SkipForward, |
| 1101 | "Sequence" => I::Film, |
| 1102 | "Render preview" => I::Zap, |
| 1103 | "New" => I::Plus, |
| 1104 | "Multicam" => I::Layers, |
| 1105 | "Nudge" => I::StepForward, |
| 1106 | "Open recent" => I::RotateCcw, |
| 1107 | _ => return None, |
| 1108 | }) |
| 1109 | } |
| 1110 | |
| 1111 | #[cfg(test)] |
| 1112 | mod icon_tests { |
| 1113 | use super::*; |
| 1114 | use crate::ui::menubar::Title; |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | #[test] |
| 1119 | fn every_menu_row_has_an_icon() { |
| 1120 | let mut actions: Vec<ActionId> = Vec::new(); |
| 1121 | for title in Title::ALL { |
| 1122 | actions.extend(title.entries().into_iter().filter_map(|e| e.action())); |
| 1123 | } |
| 1124 | for list in [ |
| 1125 | &MENU_ITEMS[..], &BENCH_ITEMS[..], &VIEWER_ITEMS[..], &SEQ_TAB_ITEMS[..], &MARKER_ITEMS[..], &BIN_ITEMS[..], &DOCK_ITEMS[..], &HEAD_ADDS[..], |
| 1126 | ] { |
| 1127 | actions.extend_from_slice(list); |
| 1128 | } |
| 1129 | for kind in [LaneKind::Video, LaneKind::Audio, LaneKind::Subtitle] { |
| 1130 | actions.extend(crate::oracle::lane_items(Lane { kind, ord: 0 })); |
| 1131 | } |
| 1132 | let mut missing: Vec<String> = actions.into_iter().filter(|a| icon(*a).is_none()).map(|a| format!("{a:?}")).collect(); |
| 1133 | missing.sort(); |
| 1134 | missing.dedup(); |
| 1135 | assert!(missing.is_empty(), "menu rows without an icon: {missing:?}"); |
| 1136 | let mut subs: Vec<&str> = Vec::new(); |
| 1137 | for title in Title::ALL { |
| 1138 | fn walk<'a>(list: &'a [crate::ui::menubar::Entry], out: &mut Vec<&'a str>) { |
| 1139 | for e in list { |
| 1140 | if let crate::ui::menubar::Entry::Sub(name, inner) = e { |
| 1141 | out.push(name); |
| 1142 | walk(inner, out); |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | walk(title.layout(), &mut subs); |
| 1147 | } |
| 1148 | let bare: Vec<&&str> = subs.iter().filter(|n| icon_sub(n).is_none()).collect(); |
| 1149 | assert!(bare.is_empty(), "flyout rows without an icon: {bare:?}"); |
| 1150 | |
| 1151 | let _ = ROW_ITEMS.map(RowItem::icon); |
| 1152 | } |
| 1153 | } |