forge
Explore
Log in

Don't you dare go Hollow

Multi-file import — drag-drop, argv, per-source tints

Browse files at this commit

80f10f56cc8946d916444f4980479fa452a9a8fbAhmet Kaan GümüşParent: 68b0ca2
  • feat
    • multi-file import — drag-drop, argv, per-source tints
Show full message
✨ feat(app): multi-file import — drag-drop, argv, per-source tints
crates/app/src/main.rsmodified+78 -8
@@ -21,6 +21,11 @@ const INK: u32 = 0xc8c8c8;
2121 const ACCENT: u32 = 0x4a9eff;
2222 /// The accent at surface brightness: a selected clip is tinted, not lit up.
2323 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];
2429
2530 /// Fixed so the video region takes every pixel the window gains and the controls
2631 /// never clip at 640x360.
@@ -213,6 +218,26 @@ impl Player {
213218 cx.notify();
214219 }
215220
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+
216241 fn undo(&mut self, cx: &mut Context<Self>) {
217242 if self.session.undo() {
218243 self.reset_after_reseek();
@@ -382,6 +407,15 @@ impl Render for Player {
382407 _ => {}
383408 }
384409 }))
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+ }))
385419 // Scrubbing is tracked on the root because the pointer leaves the
386420 // 6 px ruler on the first drag and its own listeners then stop
387421 // firing; the root's hitbox is the whole window.
@@ -554,18 +588,19 @@ impl Player {
554588 .overflow_hidden()
555589 .children(
556590 self.session
557- .clip_spans()
591+ .clip_spans_by_source()
558592 .into_iter()
559593 .enumerate()
560- .map(|(i, (_, len))| {
594+ .map(|(i, (_, len, source))| {
561595 let selected = self.selected == Some(i);
596+ let tint = source_tint(source);
562597 div()
563598 .h_full()
564599 .w(relative(width_frac(len, duration)))
565600 .rounded(px(3.))
566601 .border_1()
567- .border_color(rgb(if selected { ACCENT } else { SURFACE }))
568- .bg(rgb(if selected { SELECTED } else { SURFACE }))
602+ .border_color(rgb(if selected { ACCENT } else { tint }))
603+ .bg(rgb(if selected { SELECTED } else { tint }))
569604 .cursor_pointer()
570605 .on_mouse_down(
571606 MouseButton::Left,
@@ -608,6 +643,13 @@ fn file_name(path: &std::path::Path) -> String {
608643 )
609644 }
610645
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+
611653 /// A clip's share of the lane. A timeline with no length reads as one full-width
612654 /// box rather than as NaN, which gpui would carry into layout.
613655 fn width_frac(len: f64, total: f64) -> f32 {
@@ -732,7 +774,10 @@ fn timecode(t: f64, fps: f64) -> String {
732774
733775 #[cfg(test)]
734776 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+ };
736781 use gpui::{Bounds, Pixels, point, px, size};
737782 use std::time::Duration;
738783
@@ -807,20 +852,45 @@ mod tests {
807852 // A timeline with no length must not hand gpui a NaN width.
808853 assert_eq!(width_frac(0., 0.), 1.);
809854 }
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+ }
810869 }
811870
812871 fn main() {
813872 let Some(path) = std::env::args().nth(1) else {
814- eprintln!("usage: app <video.mp4>");
873+ eprintln!("usage: app <video.mp4> [more.mp4 ...]");
815874 std::process::exit(2);
816875 };
817- let session = match PlaybackSession::open(&path) {
876+ let mut session = match PlaybackSession::open(&path) {
818877 Ok(v) => v,
819878 Err(e) => {
820879 eprintln!("cannot open {path}: {e}");
821880 std::process::exit(1);
822881 }
823882 };
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+ }
824894 let meta = *session.meta();
825895 let out = export_path(&path);
826896 let name: SharedString = std::path::Path::new(&path)
@@ -867,7 +937,7 @@ fn main() {
867937 export: None,
868938 cancelling: false,
869939 export_path: out.clone(),
870- notice: None,
940+ notice: notice.clone().map(|text| (text.into(), Instant::now())),
871941 displayed: 0,
872942 dropped: 0,
873943 started: None,