forge
Explore
Log in

Don't you dare go Hollow

Ctrl+s save, .edith load via drop and argv

Browse files at this commit

85ac45bd6cf0d9577dfefe96069d9756cc00c63eAhmet Kaan GümüşParent: ac60b80
  • feat
    • ctrl+s save, .edith load via drop and argv
Show full message
✨ feat(app): ctrl+s save, .edith load via drop and argv

Ctrl+s writes the timeline to the project it was loaded from, or to a
new one beside the media it started as. A dropped .edith replaces the
whole session -- built first, swapped only on success, so a refusal
leaves what is playing alone -- and argv takes one in place of a media
file, resuming at its saved playhead.
crates/app/src/main.rsmodified+137 -12
@@ -78,6 +78,10 @@ struct Player {
7878 /// Where an export writes. Built once from the source path, which is not
7979 /// otherwise kept.
8080 export_path: PathBuf,
81+ /// Where ctrl+s writes: the project this timeline was loaded from, or the
82+ /// one derived beside the media it started as. Saving twice overwrites the
83+ /// same file rather than making a second one.
84+ project_path: PathBuf,
8185 /// What the last export left in the timecode slot, and when. Cleared by
8286 /// `NOTICE` passing or by the next key, whichever comes first.
8387 notice: Option<(SharedString, Instant)>,
@@ -238,6 +242,57 @@ impl Player {
238242 cx.notify();
239243 }
240244
245+ /// Swaps the whole timeline for one restored from a `.edith`. Like an
246+ /// import this arrives by drop and so checks the export guard for itself.
247+ /// The new session is built before anything is replaced, so a refusal is
248+ /// shown as the engine worded it and leaves what is playing alone.
249+ fn load_project(&mut self, path: &std::path::Path, cx: &mut Context<Self>) {
250+ if self.exporting().is_some() {
251+ return;
252+ }
253+ let text = match PlaybackSession::open_project(path) {
254+ Ok(session) => {
255+ self.session = session;
256+ self.fps = self.session.meta().frame_rate;
257+ // A project is named after itself but still exports beside its
258+ // media: that is the only place an export has ever landed.
259+ self.export_path = export_path(&self.session.sources()[0]);
260+ self.project_path = path.to_path_buf();
261+ self.name = file_name(path).into();
262+ // A copied clip names its source by index, which means a
263+ // different file -- or none -- in another project.
264+ self.clipboard = None;
265+ self.selected = None;
266+ // The counters describe one timeline; the eof line must not
267+ // report the old one's frames against the new one.
268+ self.displayed = 0;
269+ self.dropped = 0;
270+ self.started = None;
271+ // Loaded paused at its saved playhead, so the still it owes
272+ // reaches the screen the way a seek's does. The old picture is
273+ // released by the swap in `pump`, as after any other seek.
274+ self.reset_after_reseek();
275+ format!("LOADED {}", file_name(path))
276+ }
277+ Err(e) => format!("OPEN FAILED: {e}"),
278+ };
279+ eprintln!("{text}");
280+ self.notice = Some((text.into(), Instant::now()));
281+ cx.notify();
282+ }
283+
284+ /// Writes the timeline back to its project file. Overwrites silently, like
285+ /// an export: the path was chosen once and the notice is the confirmation.
286+ fn save_project(&mut self, cx: &mut Context<Self>) {
287+ let text = match self.session.save_project(&self.project_path) {
288+ Ok(()) => format!("SAVED {}", file_name(&self.project_path)),
289+ Err(e) => format!("SAVE FAILED: {e}"),
290+ };
291+ eprintln!("{text}");
292+ self.notice = Some((text.into(), Instant::now()));
293+ cx.notify();
294+ }
295+
241296 fn undo(&mut self, cx: &mut Context<Self>) {
242297 if self.session.undo() {
243298 self.reset_after_reseek();
@@ -399,6 +454,7 @@ impl Render for Player {
399454 ) {
400455 ("space", _) => this.toggle_or_restart(cx),
401456 ("e", false) => this.start_export(cx),
457+ ("s", true) => this.save_project(cx),
402458 ("c", true) => this.copy_selected(),
403459 ("v", true) => this.paste(cx),
404460 ("c", false) => this.cut(cx),
@@ -413,7 +469,12 @@ impl Render for Player {
413469 // that covers the picture as well as the panel.
414470 .on_drop(cx.listener(|this, paths: &gpui::ExternalPaths, _, cx| {
415471 for path in paths.paths() {
416- this.import(path, cx);
472+ // A project replaces the timeline, media is appended to it.
473+ if is_project(path) {
474+ this.load_project(path, cx);
475+ } else {
476+ this.import(path, cx);
477+ }
417478 }
418479 }))
419480 // Scrubbing is tracked on the root because the pointer leaves the
@@ -628,12 +689,29 @@ const NOTICE: Duration = Duration::from_secs(2);
628689
629690 /// Where an export goes: the source path with `.export.mp4` for an extension,
630691 /// so it lands beside the original and can never be the original.
631-fn export_path(source: &str) -> PathBuf {
632- let mut path = PathBuf::from(source);
692+fn export_path(source: impl Into<PathBuf>) -> PathBuf {
693+ let mut path = source.into();
633694 path.set_extension("export.mp4");
634695 path
635696 }
636697
698+/// Where ctrl+s goes when the timeline did not come from a project file: the
699+/// media path with `.edith` for an extension, beside it like an export. A
700+/// project loaded from disk keeps its own path instead, so saving it twice
701+/// writes the same file.
702+fn project_path(source: impl Into<PathBuf>) -> PathBuf {
703+ let mut path = source.into();
704+ path.set_extension("edith");
705+ path
706+}
707+
708+/// Whether a dropped or named path is a project rather than media. Exactly the
709+/// lowercase extension `save_project` writes -- anything else goes to the
710+/// demuxer, which is the one that can say what it really is.
711+fn is_project(path: &std::path::Path) -> bool {
712+ path.extension().is_some_and(|e| e == "edith")
713+}
714+
637715 /// The tail of a path, for showing. A path that is all root has none, and reads
638716 /// as itself.
639717 fn file_name(path: &std::path::Path) -> String {
@@ -775,8 +853,8 @@ fn timecode(t: f64, fps: f64) -> String {
775853 #[cfg(test)]
776854 mod tests {
777855 use super::{
778- SOURCE_TINTS, SURFACE, export_path, frac_along, scrub_due, source_tint, timecode,
779- width_frac,
856+ SOURCE_TINTS, SURFACE, export_path, frac_along, is_project, project_path, scrub_due,
857+ source_tint, timecode, width_frac,
780858 };
781859 use gpui::{Bounds, Pixels, point, px, size};
782860 use std::time::Duration;
@@ -843,6 +921,38 @@ mod tests {
843921 );
844922 }
845923
924+ #[test]
925+ fn the_first_save_lands_beside_the_media() {
926+ assert_eq!(
927+ project_path("assets/test_av.mp4"),
928+ std::path::Path::new("assets/test_av.edith")
929+ );
930+ // The same rule an export follows: only the last extension moves.
931+ assert_eq!(
932+ project_path("a.export.mp4"),
933+ std::path::Path::new("a.export.edith")
934+ );
935+ assert_eq!(project_path("clip"), std::path::Path::new("clip.edith"));
936+ // Saving a loaded project writes the file it came from, not a second.
937+ assert_eq!(project_path("a.edith"), std::path::Path::new("a.edith"));
938+ }
939+
940+ #[test]
941+ fn only_an_exact_edith_extension_is_a_project() {
942+ let p = std::path::Path::new;
943+ assert!(is_project(p("a.edith")));
944+ // A dotted directory must not decide it -- the file name does.
945+ assert!(is_project(p("/v.mp4/a.edith")));
946+ assert!(!is_project(p("a.mp4")));
947+ assert!(!is_project(p("/v.edith/a.mp4")));
948+ // Exactly what `save_project` writes: a dropped `.EDITH` goes to the
949+ // demuxer and is refused there, not parsed as a project.
950+ assert!(!is_project(p("a.EDITH")));
951+ // An extension, never a bare name.
952+ assert!(!is_project(p("edith")));
953+ assert!(!is_project(p("a.edith.mp4")));
954+ }
955+
846956 #[test]
847957 fn clip_boxes_split_the_lane_by_duration() {
848958 // 1 s + 3 s of a 4 s timeline: a quarter and three quarters.
@@ -870,12 +980,22 @@ mod tests {
870980
871981 fn main() {
872982 let Some(path) = std::env::args().nth(1) else {
873- eprintln!("usage: app <video.mp4> [more.mp4 ...]");
983+ eprintln!("usage: app <video.mp4|project.edith> [more.mp4 ...]");
874984 std::process::exit(2);
875985 };
876- let mut session = match PlaybackSession::open(&path) {
986+ let arg = PathBuf::from(&path);
987+ // A `.edith` restores a whole timeline, anything else *is* the timeline.
988+ // Either way the rest of argv is appended to what comes out.
989+ let opened = if is_project(&arg) {
990+ PlaybackSession::open_project(&arg)
991+ } else {
992+ PlaybackSession::open(&arg)
993+ };
994+ let mut session = match opened {
877995 Ok(v) => v,
878996 Err(e) => {
997+ // A failed load by drop leaves the running session alone; here
998+ // there is no session to leave, so the refusal is the whole run.
879999 eprintln!("cannot open {path}: {e}");
8801000 std::process::exit(1);
8811001 }
@@ -892,11 +1012,15 @@ fn main() {
8921012 }
8931013 }
8941014 let meta = *session.meta();
895- let out = export_path(&path);
896- let name: SharedString = std::path::Path::new(&path)
897- .file_name()
898- .map_or_else(|| path.clone(), |n| n.to_string_lossy().into_owned())
899- .into();
1015+ // Beside the media even for a project: an export has never landed anywhere
1016+ // but next to the picture it came from.
1017+ let out = export_path(&session.sources()[0]);
1018+ let project = if is_project(&arg) {
1019+ arg.clone()
1020+ } else {
1021+ project_path(&arg)
1022+ };
1023+ let name: SharedString = file_name(&arg).into();
9001024 println!(
9011025 "{path}: {}x{} @ {:.2} fps, {} samples",
9021026 meta.width, meta.height, meta.frame_rate, meta.frame_count
@@ -937,6 +1061,7 @@ fn main() {
9371061 export: None,
9381062 cancelling: false,
9391063 export_path: out.clone(),
1064+ project_path: project.clone(),
9401065 notice: notice.clone().map(|text| (text.into(), Instant::now())),
9411066 displayed: 0,
9421067 dropped: 0,