Multi-file import — drag-drop, argv, per-source tints
Browse files at this commit80f10f56cc8946d916444f4980479fa452a9a8fbParent: 68b0ca2- feat
- multi-file import — drag-drop, argv, per-source tints
1 changed file +78 -8
- crates/app/src/main.rs +78 -8
crates/app/src/main.rsmodified+78 -8
| @@ -21,6 +21,11 @@ const INK: u32 = 0xc8c8c8; | |||
| 21 | const ACCENT: u32 = 0x4a9eff; | 21 | const ACCENT: u32 = 0x4a9eff; |
| 22 | /// The accent at surface brightness: a selected clip is tinted, not lit up. | 22 | /// The accent at surface brightness: a selected clip is tinted, not lit up. |
| 23 | const SELECTED: u32 = 0x2a4a6b; | 23 | const SELECTED: u32 = 0x2a4a6b; |
| 24 | +/// One per source, so a clip that came from an imported file reads as coming | ||
| 25 | +/// from somewhere else. Same brightness as `SURFACE` and only the hue moves: | ||
| 26 | +/// the lane stays in the dark family the rest of the chrome lives in | ||
| 27 | +/// (ledger:187), and the first source keeps `SURFACE` exactly. | ||
| 28 | +const SOURCE_TINTS: [u32; 4] = [SURFACE, 0x3b3329, 0x293b33, 0x33293b]; | ||
| 24 | 29 | ||
| 25 | /// Fixed so the video region takes every pixel the window gains and the controls | 30 | /// Fixed so the video region takes every pixel the window gains and the controls |
| 26 | /// never clip at 640x360. | 31 | /// never clip at 640x360. |
| @@ -213,6 +218,26 @@ impl Player { | |||
| 213 | cx.notify(); | 218 | cx.notify(); |
| 214 | } | 219 | } |
| 215 | 220 | ||
| 221 | + /// Appends a file to the end of the timeline. A drop is not a key press, so | ||
| 222 | + /// the export guard on the key handler does not cover it and this checks for | ||
| 223 | + /// itself. The engine reseeks, so like a delete it owes the flag reset; a | ||
| 224 | + /// refusal is shown as the engine worded it and changes nothing. | ||
| 225 | + fn import(&mut self, path: &std::path::Path, cx: &mut Context<Self>) { | ||
| 226 | + if self.exporting().is_some() { | ||
| 227 | + return; | ||
| 228 | + } | ||
| 229 | + let text = match self.session.import(path) { | ||
| 230 | + Ok(()) => { | ||
| 231 | + self.reset_after_reseek(); | ||
| 232 | + format!("IMPORTED {}", file_name(path)) | ||
| 233 | + } | ||
| 234 | + Err(e) => format!("IMPORT FAILED: {e}"), | ||
| 235 | + }; | ||
| 236 | + eprintln!("{text}"); | ||
| 237 | + self.notice = Some((text.into(), Instant::now())); | ||
| 238 | + cx.notify(); | ||
| 239 | + } | ||
| 240 | + | ||
| 216 | fn undo(&mut self, cx: &mut Context<Self>) { | 241 | fn undo(&mut self, cx: &mut Context<Self>) { |
| 217 | if self.session.undo() { | 242 | if self.session.undo() { |
| 218 | self.reset_after_reseek(); | 243 | self.reset_after_reseek(); |
| @@ -382,6 +407,15 @@ impl Render for Player { | |||
| 382 | _ => {} | 407 | _ => {} |
| 383 | } | 408 | } |
| 384 | })) | 409 | })) |
| 410 | + // The whole window is the drop target: gpui turns an external file | ||
| 411 | + // drop into an `ExternalPaths` drag (window.rs:3626) delivered as a | ||
| 412 | + // mouse-up to every hovered hitbox, and the root's is the only one | ||
| 413 | + // that covers the picture as well as the panel. | ||
| 414 | + .on_drop(cx.listener(|this, paths: &gpui::ExternalPaths, _, cx| { | ||
| 415 | + for path in paths.paths() { | ||
| 416 | + this.import(path, cx); | ||
| 417 | + } | ||
| 418 | + })) | ||
| 385 | // Scrubbing is tracked on the root because the pointer leaves the | 419 | // Scrubbing is tracked on the root because the pointer leaves the |
| 386 | // 6 px ruler on the first drag and its own listeners then stop | 420 | // 6 px ruler on the first drag and its own listeners then stop |
| 387 | // firing; the root's hitbox is the whole window. | 421 | // firing; the root's hitbox is the whole window. |
| @@ -554,18 +588,19 @@ impl Player { | |||
| 554 | .overflow_hidden() | 588 | .overflow_hidden() |
| 555 | .children( | 589 | .children( |
| 556 | self.session | 590 | self.session |
| 557 | - .clip_spans() | 591 | + .clip_spans_by_source() |
| 558 | .into_iter() | 592 | .into_iter() |
| 559 | .enumerate() | 593 | .enumerate() |
| 560 | - .map(|(i, (_, len))| { | 594 | + .map(|(i, (_, len, source))| { |
| 561 | let selected = self.selected == Some(i); | 595 | let selected = self.selected == Some(i); |
| 596 | + let tint = source_tint(source); | ||
| 562 | div() | 597 | div() |
| 563 | .h_full() | 598 | .h_full() |
| 564 | .w(relative(width_frac(len, duration))) | 599 | .w(relative(width_frac(len, duration))) |
| 565 | .rounded(px(3.)) | 600 | .rounded(px(3.)) |
| 566 | .border_1() | 601 | .border_1() |
| 567 | - .border_color(rgb(if selected { ACCENT } else { SURFACE })) | 602 | + .border_color(rgb(if selected { ACCENT } else { tint })) |
| 568 | - .bg(rgb(if selected { SELECTED } else { SURFACE })) | 603 | + .bg(rgb(if selected { SELECTED } else { tint })) |
| 569 | .cursor_pointer() | 604 | .cursor_pointer() |
| 570 | .on_mouse_down( | 605 | .on_mouse_down( |
| 571 | MouseButton::Left, | 606 | MouseButton::Left, |
| @@ -608,6 +643,13 @@ fn file_name(path: &std::path::Path) -> String { | |||
| 608 | ) | 643 | ) |
| 609 | } | 644 | } |
| 610 | 645 | ||
| 646 | +/// The tint a clip from source `n` wears. Cycled rather than extended: past the | ||
| 647 | +/// palette two sources share a colour, which is a smaller lie than a fifth tint | ||
| 648 | +/// bright enough to leave the family. | ||
| 649 | +fn source_tint(source: usize) -> u32 { | ||
| 650 | + SOURCE_TINTS[source % SOURCE_TINTS.len()] | ||
| 651 | +} | ||
| 652 | + | ||
| 611 | /// A clip's share of the lane. A timeline with no length reads as one full-width | 653 | /// A clip's share of the lane. A timeline with no length reads as one full-width |
| 612 | /// box rather than as NaN, which gpui would carry into layout. | 654 | /// box rather than as NaN, which gpui would carry into layout. |
| 613 | fn width_frac(len: f64, total: f64) -> f32 { | 655 | fn width_frac(len: f64, total: f64) -> f32 { |
| @@ -732,7 +774,10 @@ fn timecode(t: f64, fps: f64) -> String { | |||
| 732 | 774 | ||
| 733 | #[cfg(test)] | 775 | #[cfg(test)] |
| 734 | mod tests { | 776 | mod tests { |
| 735 | - use super::{export_path, frac_along, scrub_due, timecode, width_frac}; | 777 | + use super::{ |
| 778 | + SOURCE_TINTS, SURFACE, export_path, frac_along, scrub_due, source_tint, timecode, | ||
| 779 | + width_frac, | ||
| 780 | + }; | ||
| 736 | use gpui::{Bounds, Pixels, point, px, size}; | 781 | use gpui::{Bounds, Pixels, point, px, size}; |
| 737 | use std::time::Duration; | 782 | use std::time::Duration; |
| 738 | 783 | ||
| @@ -807,20 +852,45 @@ mod tests { | |||
| 807 | // A timeline with no length must not hand gpui a NaN width. | 852 | // A timeline with no length must not hand gpui a NaN width. |
| 808 | assert_eq!(width_frac(0., 0.), 1.); | 853 | assert_eq!(width_frac(0., 0.), 1.); |
| 809 | } | 854 | } |
| 855 | + | ||
| 856 | + #[test] | ||
| 857 | + fn source_tints_differ_per_source_and_cycle() { | ||
| 858 | + // The file the session opened with is the unchanged lane colour. | ||
| 859 | + assert_eq!(source_tint(0), SURFACE); | ||
| 860 | + // Neighbouring sources must not share one, or an import is invisible. | ||
| 861 | + assert_ne!(source_tint(0), source_tint(1)); | ||
| 862 | + assert_ne!(source_tint(1), source_tint(2)); | ||
| 863 | + assert_ne!(source_tint(2), source_tint(3)); | ||
| 864 | + // Past the palette it wraps -- never an index panic. | ||
| 865 | + assert_eq!(source_tint(4), source_tint(0)); | ||
| 866 | + assert_eq!(source_tint(9), source_tint(1)); | ||
| 867 | + assert_eq!(source_tint(usize::MAX), SOURCE_TINTS[usize::MAX % 4]); | ||
| 868 | + } | ||
| 810 | } | 869 | } |
| 811 | 870 | ||
| 812 | fn main() { | 871 | fn main() { |
| 813 | let Some(path) = std::env::args().nth(1) else { | 872 | let Some(path) = std::env::args().nth(1) else { |
| 814 | - eprintln!("usage: app <video.mp4>"); | 873 | + eprintln!("usage: app <video.mp4> [more.mp4 ...]"); |
| 815 | std::process::exit(2); | 874 | std::process::exit(2); |
| 816 | }; | 875 | }; |
| 817 | - let session = match PlaybackSession::open(&path) { | 876 | + let mut session = match PlaybackSession::open(&path) { |
| 818 | Ok(v) => v, | 877 | Ok(v) => v, |
| 819 | Err(e) => { | 878 | Err(e) => { |
| 820 | eprintln!("cannot open {path}: {e}"); | 879 | eprintln!("cannot open {path}: {e}"); |
| 821 | std::process::exit(1); | 880 | std::process::exit(1); |
| 822 | } | 881 | } |
| 823 | }; | 882 | }; |
| 883 | + // The first file makes the timeline, the rest are appended to it. A refusal | ||
| 884 | + // is not fatal -- the others still load -- but the window must not open | ||
| 885 | + // silently pretending the file was taken, so the first one seeds the notice. | ||
| 886 | + let mut notice = None; | ||
| 887 | + for arg in std::env::args().skip(2) { | ||
| 888 | + if let Err(e) = session.import(std::path::Path::new(&arg)) { | ||
| 889 | + let text = format!("IMPORT FAILED: {e}"); | ||
| 890 | + eprintln!("{arg}: {text}"); | ||
| 891 | + notice.get_or_insert(text); | ||
| 892 | + } | ||
| 893 | + } | ||
| 824 | let meta = *session.meta(); | 894 | let meta = *session.meta(); |
| 825 | let out = export_path(&path); | 895 | let out = export_path(&path); |
| 826 | let name: SharedString = std::path::Path::new(&path) | 896 | let name: SharedString = std::path::Path::new(&path) |
| @@ -867,7 +937,7 @@ fn main() { | |||
| 867 | export: None, | 937 | export: None, |
| 868 | cancelling: false, | 938 | cancelling: false, |
| 869 | export_path: out.clone(), | 939 | export_path: out.clone(), |
| 870 | - notice: None, | 940 | + notice: notice.clone().map(|text| (text.into(), Instant::now())), |
| 871 | displayed: 0, | 941 | displayed: 0, |
| 872 | dropped: 0, | 942 | dropped: 0, |
| 873 | started: None, | 943 | started: None, |