Ctrl+s save, .edith load via drop and argv
Browse files at this commit85ac45bd6cf0d9577dfefe96069d9756cc00c63eParent: ac60b80- feat
- ctrl+s save, .edith load via drop and argv
1 changed file +137 -12
- crates/app/src/main.rs +137 -12
crates/app/src/main.rsmodified+137 -12
| @@ -78,6 +78,10 @@ struct Player { | ||
| 78 | 78 | /// Where an export writes. Built once from the source path, which is not |
| 79 | 79 | /// otherwise kept. |
| 80 | 80 | 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, | |
| 81 | 85 | /// What the last export left in the timecode slot, and when. Cleared by |
| 82 | 86 | /// `NOTICE` passing or by the next key, whichever comes first. |
| 83 | 87 | notice: Option<(SharedString, Instant)>, |
| @@ -238,6 +242,57 @@ impl Player { | ||
| 238 | 242 | cx.notify(); |
| 239 | 243 | } |
| 240 | 244 | |
| 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 | + | |
| 241 | 296 | fn undo(&mut self, cx: &mut Context<Self>) { |
| 242 | 297 | if self.session.undo() { |
| 243 | 298 | self.reset_after_reseek(); |
| @@ -399,6 +454,7 @@ impl Render for Player { | ||
| 399 | 454 | ) { |
| 400 | 455 | ("space", _) => this.toggle_or_restart(cx), |
| 401 | 456 | ("e", false) => this.start_export(cx), |
| 457 | + ("s", true) => this.save_project(cx), | |
| 402 | 458 | ("c", true) => this.copy_selected(), |
| 403 | 459 | ("v", true) => this.paste(cx), |
| 404 | 460 | ("c", false) => this.cut(cx), |
| @@ -413,7 +469,12 @@ impl Render for Player { | ||
| 413 | 469 | // that covers the picture as well as the panel. |
| 414 | 470 | .on_drop(cx.listener(|this, paths: &gpui::ExternalPaths, _, cx| { |
| 415 | 471 | 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 | + } | |
| 417 | 478 | } |
| 418 | 479 | })) |
| 419 | 480 | // Scrubbing is tracked on the root because the pointer leaves the |
| @@ -628,12 +689,29 @@ const NOTICE: Duration = Duration::from_secs(2); | ||
| 628 | 689 | |
| 629 | 690 | /// Where an export goes: the source path with `.export.mp4` for an extension, |
| 630 | 691 | /// 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(); | |
| 633 | 694 | path.set_extension("export.mp4"); |
| 634 | 695 | path |
| 635 | 696 | } |
| 636 | 697 | |
| 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 | + | |
| 637 | 715 | /// The tail of a path, for showing. A path that is all root has none, and reads |
| 638 | 716 | /// as itself. |
| 639 | 717 | fn file_name(path: &std::path::Path) -> String { |
| @@ -775,8 +853,8 @@ fn timecode(t: f64, fps: f64) -> String { | ||
| 775 | 853 | #[cfg(test)] |
| 776 | 854 | mod tests { |
| 777 | 855 | 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, | |
| 780 | 858 | }; |
| 781 | 859 | use gpui::{Bounds, Pixels, point, px, size}; |
| 782 | 860 | use std::time::Duration; |
| @@ -843,6 +921,38 @@ mod tests { | ||
| 843 | 921 | ); |
| 844 | 922 | } |
| 845 | 923 | |
| 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 | + | |
| 846 | 956 | #[test] |
| 847 | 957 | fn clip_boxes_split_the_lane_by_duration() { |
| 848 | 958 | // 1 s + 3 s of a 4 s timeline: a quarter and three quarters. |
| @@ -870,12 +980,22 @@ mod tests { | ||
| 870 | 980 | |
| 871 | 981 | fn main() { |
| 872 | 982 | 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 ...]"); | |
| 874 | 984 | std::process::exit(2); |
| 875 | 985 | }; |
| 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 { | |
| 877 | 995 | Ok(v) => v, |
| 878 | 996 | 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. | |
| 879 | 999 | eprintln!("cannot open {path}: {e}"); |
| 880 | 1000 | std::process::exit(1); |
| 881 | 1001 | } |
| @@ -892,11 +1012,15 @@ fn main() { | ||
| 892 | 1012 | } |
| 893 | 1013 | } |
| 894 | 1014 | 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(); | |
| 900 | 1024 | println!( |
| 901 | 1025 | "{path}: {}x{} @ {:.2} fps, {} samples", |
| 902 | 1026 | meta.width, meta.height, meta.frame_rate, meta.frame_count |
| @@ -937,6 +1061,7 @@ fn main() { | ||
| 937 | 1061 | export: None, |
| 938 | 1062 | cancelling: false, |
| 939 | 1063 | export_path: out.clone(), |
| 1064 | + project_path: project.clone(), | |
| 940 | 1065 | notice: notice.clone().map(|text| (text.into(), Instant::now())), |
| 941 | 1066 | displayed: 0, |
| 942 | 1067 | dropped: 0, |