Empty start, and opening is not playing
Browse files at this commitc5f4646231b9f3c3bd6e8084fce305adfa95ff0eParent: d598c98- feat
- empty start, and opening is not playing
1 changed file +215 -90
- crates/app/src/main.rs +215 -90
crates/app/src/main.rsmodified+215 -90
| @@ -67,7 +67,10 @@ const KEYS_ROW_H: f32 = HIT_MIN; | ||
| 67 | 67 | const ESCAPE: &str = "escape"; |
| 68 | 68 | |
| 69 | 69 | struct Player { |
| 70 | - session: PlaybackSession, | |
| 70 | + /// The timeline, once there is one. A run with no file opens without it and | |
| 71 | + /// waits: the first media import or project load is what fills it, and | |
| 72 | + /// until then every action that needs a timeline says so instead of acting. | |
| 73 | + session: Option<PlaybackSession>, | |
| 71 | 74 | /// Timeline seconds -> frame index, so the clock can be compared to what |
| 72 | 75 | /// the decoder hands over. |
| 73 | 76 | fps: f64, |
| @@ -96,8 +99,6 @@ struct Player { | ||
| 96 | 99 | scrubbing: bool, |
| 97 | 100 | last_scrub: Instant, |
| 98 | 101 | last_target: u32, |
| 99 | - /// Playback was started once, on the first render. The play binding owns it after that. | |
| 100 | - launched: bool, | |
| 101 | 102 | /// The running export. While it owns the UI the editor is read-only. |
| 102 | 103 | export: Option<ExportHandle>, |
| 103 | 104 | /// The export above was cancelled and is only winding down. The editor is |
| @@ -138,7 +139,12 @@ impl Player { | ||
| 138 | 139 | /// the channel and only the last of them is shown, which *is* the |
| 139 | 140 | /// drop-when-behind policy. A frame that is not due yet waits in `held`. |
| 140 | 141 | fn pump(&mut self, window: &mut Window) { |
| 141 | - let target = self.session.now() * self.fps; | |
| 142 | + // No timeline, nothing to catch up to: the window is showing its empty | |
| 143 | + // state and there is no decoder to drain. | |
| 144 | + let Some(session) = &mut self.session else { | |
| 145 | + return; | |
| 146 | + }; | |
| 147 | + let target = session.now() * self.fps; | |
| 142 | 148 | let mut newest: Option<Frame> = None; |
| 143 | 149 | loop { |
| 144 | 150 | let frame = match self.held.take() { |
| @@ -146,10 +152,10 @@ impl Player { | ||
| 146 | 152 | // Nothing waiting means either a clip boundary being rebuilt or |
| 147 | 153 | // the real end of the timeline, and only the engine can tell |
| 148 | 154 | // them apart -- `frame.index` is already a timeline index. |
| 149 | - None => match self.session.try_frame() { | |
| 155 | + None => match session.try_frame() { | |
| 150 | 156 | Some(frame) => frame, |
| 151 | 157 | None => { |
| 152 | - self.eos = self.session.is_eos(); | |
| 158 | + self.eos = session.is_eos(); | |
| 153 | 159 | break; |
| 154 | 160 | } |
| 155 | 161 | }, |
| @@ -190,7 +196,7 @@ impl Player { | ||
| 190 | 196 | "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s", |
| 191 | 197 | self.displayed, |
| 192 | 198 | self.dropped, |
| 193 | - self.session.now() | |
| 199 | + session.now() | |
| 194 | 200 | ); |
| 195 | 201 | } |
| 196 | 202 | } |
| @@ -211,7 +217,10 @@ impl Player { | ||
| 211 | 217 | if self.exporting().is_some() { |
| 212 | 218 | return; |
| 213 | 219 | } |
| 214 | - self.session.seek(t); | |
| 220 | + let Some(session) = &mut self.session else { | |
| 221 | + return; | |
| 222 | + }; | |
| 223 | + session.seek(t); | |
| 215 | 224 | self.reset_after_reseek(); |
| 216 | 225 | cx.notify(); |
| 217 | 226 | } |
| @@ -222,7 +231,9 @@ impl Player { | ||
| 222 | 231 | if self.exporting().is_some() { |
| 223 | 232 | return; |
| 224 | 233 | } |
| 225 | - self.session.cut_at(self.session.now()); | |
| 234 | + if let Some(session) = &mut self.session { | |
| 235 | + session.cut_at(session.now()); | |
| 236 | + } | |
| 226 | 237 | self.selected = None; |
| 227 | 238 | cx.notify(); |
| 228 | 239 | } |
| @@ -233,11 +244,11 @@ impl Player { | ||
| 233 | 244 | if self.exporting().is_some() { |
| 234 | 245 | return; |
| 235 | 246 | } |
| 236 | - if self | |
| 237 | - .selected | |
| 238 | - .take() | |
| 239 | - .is_some_and(|i| self.session.delete_clip(i)) | |
| 240 | - { | |
| 247 | + let deleted = match (&mut self.session, self.selected.take()) { | |
| 248 | + (Some(session), Some(i)) => session.delete_clip(i), | |
| 249 | + _ => false, | |
| 250 | + }; | |
| 251 | + if deleted { | |
| 241 | 252 | self.reset_after_reseek(); |
| 242 | 253 | } |
| 243 | 254 | cx.notify(); |
| @@ -245,7 +256,8 @@ impl Player { | ||
| 245 | 256 | |
| 246 | 257 | /// Copies the selected clip. Nothing on screen changes, so no notify. |
| 247 | 258 | fn copy_selected(&mut self) { |
| 248 | - if let Some(clip) = self.selected.and_then(|i| self.session.clip_at(i)) { | |
| 259 | + let session = self.session.as_ref(); | |
| 260 | + if let Some(clip) = self.selected.and_then(|i| session?.clip_at(i)) { | |
| 249 | 261 | self.clipboard = Some(clip); |
| 250 | 262 | } |
| 251 | 263 | } |
| @@ -254,9 +266,11 @@ impl Player { | ||
| 254 | 266 | /// like a delete this owes the flag reset -- and the selection, whose index |
| 255 | 267 | /// the insert has just moved. |
| 256 | 268 | fn paste(&mut self, cx: &mut Context<Self>) { |
| 257 | - if let Some(clip) = self.clipboard | |
| 258 | - && self.session.paste_at(self.session.now(), clip) | |
| 259 | - { | |
| 269 | + let pasted = match (&mut self.session, self.clipboard) { | |
| 270 | + (Some(session), Some(clip)) => session.paste_at(session.now(), clip), | |
| 271 | + _ => false, | |
| 272 | + }; | |
| 273 | + if pasted { | |
| 260 | 274 | self.selected = None; |
| 261 | 275 | self.reset_after_reseek(); |
| 262 | 276 | } |
| @@ -271,18 +285,42 @@ impl Player { | ||
| 271 | 285 | if self.exporting().is_some() { |
| 272 | 286 | return; |
| 273 | 287 | } |
| 274 | - let text = match self.session.import(path) { | |
| 275 | - Ok(()) => { | |
| 288 | + // An empty window has nothing to append to: there the first file *is* | |
| 289 | + // the timeline and is opened as one, which is the same fork a launch | |
| 290 | + // makes between its first argument and the rest. | |
| 291 | + let text = match self.session.as_mut().map(|session| session.import(path)) { | |
| 292 | + Some(Ok(())) => { | |
| 276 | 293 | self.reset_after_reseek(); |
| 277 | 294 | format!("IMPORTED {}", file_name(path)) |
| 278 | 295 | } |
| 279 | - Err(e) => format!("IMPORT FAILED: {e}"), | |
| 296 | + Some(Err(e)) => format!("IMPORT FAILED: {e}"), | |
| 297 | + None => self.open_media(path), | |
| 280 | 298 | }; |
| 281 | 299 | eprintln!("{text}"); |
| 282 | 300 | self.notice = Some(text.into()); |
| 283 | 301 | cx.notify(); |
| 284 | 302 | } |
| 285 | 303 | |
| 304 | + /// Takes a file as the whole timeline, which is what an empty window is | |
| 305 | + /// waiting for. Everything derived from the media -- the clock, the title, | |
| 306 | + /// where an export and a save go -- is set here, exactly as a launch with a | |
| 307 | + /// file argument sets it. Paused with its first frame showing, like every | |
| 308 | + /// other way a timeline arrives. | |
| 309 | + fn open_media(&mut self, path: &std::path::Path) -> String { | |
| 310 | + match PlaybackSession::open(path) { | |
| 311 | + Ok(session) => { | |
| 312 | + self.fps = session.meta().frame_rate; | |
| 313 | + self.session = Some(session); | |
| 314 | + self.export_path = export_path(path); | |
| 315 | + self.project_path = project_path(path); | |
| 316 | + self.name = file_name(path).into(); | |
| 317 | + self.reset_after_reseek(); | |
| 318 | + format!("OPENED {}", file_name(path)) | |
| 319 | + } | |
| 320 | + Err(e) => format!("OPEN FAILED: {e}"), | |
| 321 | + } | |
| 322 | + } | |
| 323 | + | |
| 286 | 324 | /// The Import button: asks the desktop for a path and takes it the same way |
| 287 | 325 | /// a drop would. The chooser is another process and the user may sit in it, |
| 288 | 326 | /// so it runs on a background thread -- blocking here would freeze the |
| @@ -322,11 +360,11 @@ impl Player { | ||
| 322 | 360 | } |
| 323 | 361 | let text = match PlaybackSession::open_project(path) { |
| 324 | 362 | Ok(session) => { |
| 325 | - self.session = session; | |
| 326 | - self.fps = self.session.meta().frame_rate; | |
| 363 | + self.fps = session.meta().frame_rate; | |
| 327 | 364 | // A project is named after itself but still exports beside its |
| 328 | 365 | // media: that is the only place an export has ever landed. |
| 329 | - self.export_path = export_path(&self.session.sources()[0]); | |
| 366 | + self.export_path = export_path(&session.sources()[0]); | |
| 367 | + self.session = Some(session); | |
| 330 | 368 | self.project_path = path.to_path_buf(); |
| 331 | 369 | self.name = file_name(path).into(); |
| 332 | 370 | // A copied clip names its source by index, which means a |
| @@ -354,9 +392,14 @@ impl Player { | ||
| 354 | 392 | /// Writes the timeline back to its project file. Overwrites silently, like |
| 355 | 393 | /// an export: the path was chosen once and the notice is the confirmation. |
| 356 | 394 | fn save_project(&mut self, cx: &mut Context<Self>) { |
| 357 | - let text = match self.session.save_project(&self.project_path) { | |
| 358 | - Ok(()) => format!("SAVED {}", file_name(&self.project_path)), | |
| 359 | - Err(e) => format!("SAVE FAILED: {e}"), | |
| 395 | + let saved = self | |
| 396 | + .session | |
| 397 | + .as_ref() | |
| 398 | + .map(|session| session.save_project(&self.project_path)); | |
| 399 | + let text = match saved { | |
| 400 | + Some(Ok(())) => format!("SAVED {}", file_name(&self.project_path)), | |
| 401 | + Some(Err(e)) => format!("SAVE FAILED: {e}"), | |
| 402 | + None => "NOTHING TO SAVE — open a file first".to_string(), | |
| 360 | 403 | }; |
| 361 | 404 | eprintln!("{text}"); |
| 362 | 405 | self.notice = Some(text.into()); |
| @@ -364,7 +407,7 @@ impl Player { | ||
| 364 | 407 | } |
| 365 | 408 | |
| 366 | 409 | fn undo(&mut self, cx: &mut Context<Self>) { |
| 367 | - if self.session.undo() { | |
| 410 | + if self.session.as_mut().is_some_and(PlaybackSession::undo) { | |
| 368 | 411 | self.reset_after_reseek(); |
| 369 | 412 | } |
| 370 | 413 | self.selected = None; |
| @@ -404,7 +447,10 @@ impl Player { | ||
| 404 | 447 | /// and the release, which must land exactly even when the throttle below |
| 405 | 448 | /// would have skipped them. |
| 406 | 449 | fn scrub_to(&mut self, x: Pixels, commit: bool, cx: &mut Context<Self>) { |
| 407 | - let t = f64::from(frac_along(x, self.ruler.get())) * self.session.timeline_duration(); | |
| 450 | + let Some(session) = &self.session else { | |
| 451 | + return; | |
| 452 | + }; | |
| 453 | + let t = f64::from(frac_along(x, self.ruler.get())) * session.timeline_duration(); | |
| 408 | 454 | let target = (t * self.fps) as u32; |
| 409 | 455 | if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) { |
| 410 | 456 | self.last_target = target; |
| @@ -421,9 +467,11 @@ impl Player { | ||
| 421 | 467 | } |
| 422 | 468 | if self.done { |
| 423 | 469 | self.seek(0., cx); |
| 424 | - self.session.play(); | |
| 425 | - } else { | |
| 426 | - self.session.toggle(); | |
| 470 | + if let Some(session) = &mut self.session { | |
| 471 | + session.play(); | |
| 472 | + } | |
| 473 | + } else if let Some(session) = &mut self.session { | |
| 474 | + session.toggle(); | |
| 427 | 475 | // Past EOF nothing else asks for a repaint. |
| 428 | 476 | cx.notify(); |
| 429 | 477 | } |
| @@ -444,8 +492,15 @@ impl Player { | ||
| 444 | 492 | if self.export.is_some() { |
| 445 | 493 | return; |
| 446 | 494 | } |
| 447 | - self.session.pause(); | |
| 448 | - self.export = Some(self.session.export_to(&self.export_path)); | |
| 495 | + // Nothing to write out, and a refusal rather than a silent button: the | |
| 496 | + // window is empty and the export path is not even chosen yet. | |
| 497 | + let Some(session) = &mut self.session else { | |
| 498 | + self.notice = Some("NOTHING TO EXPORT — open a file first".into()); | |
| 499 | + cx.notify(); | |
| 500 | + return; | |
| 501 | + }; | |
| 502 | + session.pause(); | |
| 503 | + self.export = Some(session.export_to(&self.export_path)); | |
| 449 | 504 | cx.notify(); |
| 450 | 505 | } |
| 451 | 506 | |
| @@ -481,17 +536,19 @@ impl Player { | ||
| 481 | 536 | |
| 482 | 537 | impl Render for Player { |
| 483 | 538 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 484 | - // Starts on launch: the session opens paused so the clock begins with | |
| 485 | - // the first rendered frame rather than with the process (which would | |
| 486 | - // charge window setup to the timeline and drop the frames it covers). | |
| 487 | - if !std::mem::replace(&mut self.launched, true) { | |
| 488 | - self.session.play(); | |
| 539 | + // A file that has just been opened sits on its first frame with the | |
| 540 | + // clock stopped: opening is not playing, whichever way the file | |
| 541 | + // arrived. The play binding and the transport button start it. | |
| 542 | + if let Some(session) = &mut self.session { | |
| 543 | + session.tick(); | |
| 489 | 544 | } |
| 490 | - self.session.tick(); | |
| 491 | 545 | self.pump(window); |
| 492 | 546 | self.poll_export(); |
| 493 | 547 | // No shadow flag: the clock is the only truth about play state. |
| 494 | - let playing = self.session.is_playing(); | |
| 548 | + let playing = self | |
| 549 | + .session | |
| 550 | + .as_ref() | |
| 551 | + .is_some_and(PlaybackSession::is_playing); | |
| 495 | 552 | // A paused timeline has nothing to animate; the toggle handlers notify, |
| 496 | 553 | // which is what starts the loop again. A paused seek keeps the loop |
| 497 | 554 | // running by itself until `pump` has the frame it asked for. An export |
| @@ -505,14 +562,20 @@ impl Render for Player { | ||
| 505 | 562 | |
| 506 | 563 | // Read per render, never cached: a delete shortens the timeline and the |
| 507 | 564 | // timecode, the ruler and the clamp below all have to follow it. |
| 508 | - let duration = self.session.timeline_duration(); | |
| 565 | + let duration = self | |
| 566 | + .session | |
| 567 | + .as_ref() | |
| 568 | + .map_or(0., PlaybackSession::timeline_duration); | |
| 509 | 569 | // The clock keeps running after the last frame (wall time takes over at |
| 510 | 570 | // audio EOF) while the picture is frozen, so the timeline the UI shows is |
| 511 | 571 | // the clamped one, pinned to the out-point once playback is done. |
| 512 | 572 | let position = if self.done { |
| 513 | 573 | duration |
| 514 | 574 | } else { |
| 515 | - self.session.now().clamp(0., duration) | |
| 575 | + self.session | |
| 576 | + .as_ref() | |
| 577 | + .map_or(0., PlaybackSession::now) | |
| 578 | + .clamp(0., duration) | |
| 516 | 579 | }; |
| 517 | 580 | |
| 518 | 581 | div() |
| @@ -654,7 +717,21 @@ impl Render for Player { | ||
| 654 | 717 | .children( |
| 655 | 718 | self.image |
| 656 | 719 | .clone() |
| 657 | - .map(|i| img(i).size_full().object_fit(gpui::ObjectFit::Contain)), | |
| 720 | + .map(|i| { | |
| 721 | + img(i) | |
| 722 | + .size_full() | |
| 723 | + .object_fit(gpui::ObjectFit::Contain) | |
| 724 | + .into_any_element() | |
| 725 | + }) | |
| 726 | + // With no file open the letterbox is the whole | |
| 727 | + // window, and a black rectangle says only that | |
| 728 | + // something is broken -- so it says what it wants | |
| 729 | + // instead. The window is already the drop target. | |
| 730 | + .or_else(|| { | |
| 731 | + self.session | |
| 732 | + .is_none() | |
| 733 | + .then(|| empty_hint().into_any_element()) | |
| 734 | + }), | |
| 658 | 735 | ), |
| 659 | 736 | ) |
| 660 | 737 | // Above the panel and only when there is one to show, so it costs |
| @@ -684,6 +761,9 @@ impl Player { | ||
| 684 | 761 | // percentage and the accent bar are the same number, so the playhead |
| 685 | 762 | // fill doubles as the progress bar for free. |
| 686 | 763 | let exporting = self.exporting().is_some(); |
| 764 | + // Everything but Import and Keys needs a timeline to act on: with none | |
| 765 | + // open they are dimmed rather than silently doing nothing. | |
| 766 | + let live = self.session.is_some() && !exporting; | |
| 687 | 767 | let key = |action| self.keymap.display(action); |
| 688 | 768 | let (hint, filled) = if let Some(export) = self.exporting() { |
| 689 | 769 | let progress = export.progress(); |
| @@ -732,7 +812,7 @@ impl Player { | ||
| 732 | 812 | Some(transport_glyph(playing).into_any_element()), |
| 733 | 813 | if playing { "Pause" } else { "Play" }, |
| 734 | 814 | key(ActionId::Play), |
| 735 | - !exporting, | |
| 815 | + live, | |
| 736 | 816 | cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)), |
| 737 | 817 | )) |
| 738 | 818 | .child(separator()) |
| @@ -744,7 +824,7 @@ impl Player { | ||
| 744 | 824 | "{} — splits the clip under the playhead", |
| 745 | 825 | key(ActionId::Cut) |
| 746 | 826 | ), |
| 747 | - !exporting, | |
| 827 | + live, | |
| 748 | 828 | cx.listener(|this, _: &ClickEvent, _, cx| this.cut(cx)), |
| 749 | 829 | )) |
| 750 | 830 | .child(control( |
| @@ -756,7 +836,7 @@ impl Player { | ||
| 756 | 836 | } else { |
| 757 | 837 | format!("{} — click a clip below first", key(ActionId::Delete)) |
| 758 | 838 | }, |
| 759 | - !exporting && self.selected.is_some(), | |
| 839 | + live && self.selected.is_some(), | |
| 760 | 840 | cx.listener(|this, _: &ClickEvent, _, cx| this.delete_selected(cx)), |
| 761 | 841 | )) |
| 762 | 842 | .child(separator()) |
| @@ -776,7 +856,7 @@ impl Player { | ||
| 776 | 856 | "{} — writes the timeline out beside the source", |
| 777 | 857 | key(ActionId::Export) |
| 778 | 858 | ), |
| 779 | - self.export.is_none(), | |
| 859 | + live && self.export.is_none(), | |
| 780 | 860 | cx.listener(|this, _: &ClickEvent, _, cx| this.start_export(cx)), |
| 781 | 861 | )) |
| 782 | 862 | .child(control( |
| @@ -784,7 +864,7 @@ impl Player { | ||
| 784 | 864 | None, |
| 785 | 865 | "Save", |
| 786 | 866 | format!("{} — writes the project file", key(ActionId::Save)), |
| 787 | - !exporting, | |
| 867 | + live, | |
| 788 | 868 | cx.listener(|this, _: &ClickEvent, _, cx| this.save_project(cx)), |
| 789 | 869 | )) |
| 790 | 870 | // No shortcut of its own -- and closed while an export runs, |
| @@ -1024,7 +1104,9 @@ impl Player { | ||
| 1024 | 1104 | .overflow_hidden() |
| 1025 | 1105 | .children( |
| 1026 | 1106 | self.session |
| 1027 | - .clip_spans_by_source() | |
| 1107 | + .as_ref() | |
| 1108 | + .map(PlaybackSession::clip_spans_by_source) | |
| 1109 | + .unwrap_or_default() | |
| 1028 | 1110 | .into_iter() |
| 1029 | 1111 | .enumerate() |
| 1030 | 1112 | .map(|(i, (_, len, source))| { |
| @@ -1275,6 +1357,24 @@ fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 { | ||
| 1275 | 1357 | ((x - bounds.left()) / bounds.size.width).clamp(0., 1.) |
| 1276 | 1358 | } |
| 1277 | 1359 | |
| 1360 | +/// What a window with no file open is waiting for. Both ways in are already | |
| 1361 | +/// built -- the whole window is the drop target and the Import chooser takes a | |
| 1362 | +/// project as readily as media -- so this only has to say so. | |
| 1363 | +fn empty_hint() -> impl IntoElement { | |
| 1364 | + div() | |
| 1365 | + .flex() | |
| 1366 | + .flex_col() | |
| 1367 | + .items_center() | |
| 1368 | + .gap(px(6.)) | |
| 1369 | + .text_color(rgb(INK_DIM)) | |
| 1370 | + .child("Drop a video or an .edith project here") | |
| 1371 | + .child( | |
| 1372 | + div() | |
| 1373 | + .text_size(px(11.)) | |
| 1374 | + .child("or click Import below to choose one"), | |
| 1375 | + ) | |
| 1376 | +} | |
| 1377 | + | |
| 1278 | 1378 | /// The second lane: still a placeholder, deliberately empty. |
| 1279 | 1379 | fn track_lane() -> impl IntoElement { |
| 1280 | 1380 | div() |
| @@ -1524,26 +1624,31 @@ mod tests { | ||
| 1524 | 1624 | } |
| 1525 | 1625 | |
| 1526 | 1626 | fn main() { |
| 1527 | - let Some(path) = std::env::args().nth(1) else { | |
| 1528 | - eprintln!("usage: app <video.mp4|project.edith> [more.mp4 ...]"); | |
| 1529 | - std::process::exit(2); | |
| 1530 | - }; | |
| 1531 | - let arg = PathBuf::from(&path); | |
| 1627 | + let arg = std::env::args().nth(1).map(PathBuf::from); | |
| 1532 | 1628 | // A `.edith` restores a whole timeline, anything else *is* the timeline. |
| 1533 | - // Either way the rest of argv is appended to what comes out. | |
| 1534 | - let opened = if is_project(&arg) { | |
| 1535 | - PlaybackSession::open_project(&arg) | |
| 1536 | - } else { | |
| 1537 | - PlaybackSession::open(&arg) | |
| 1538 | - }; | |
| 1539 | - let mut session = match opened { | |
| 1540 | - Ok(v) => v, | |
| 1541 | - Err(e) => { | |
| 1542 | - // A failed load by drop leaves the running session alone; here | |
| 1543 | - // there is no session to leave, so the refusal is the whole run. | |
| 1544 | - eprintln!("cannot open {path}: {e}"); | |
| 1545 | - std::process::exit(1); | |
| 1629 | + // Either way the rest of argv is appended to what comes out. No argument at | |
| 1630 | + // all opens the window empty -- the timeline then arrives by drop or by the | |
| 1631 | + // Import button, and everything below is derived from it at that point | |
| 1632 | + // instead. | |
| 1633 | + let mut session = match &arg { | |
| 1634 | + Some(arg) => { | |
| 1635 | + let opened = if is_project(arg) { | |
| 1636 | + PlaybackSession::open_project(arg) | |
| 1637 | + } else { | |
| 1638 | + PlaybackSession::open(arg) | |
| 1639 | + }; | |
| 1640 | + match opened { | |
| 1641 | + Ok(v) => Some(v), | |
| 1642 | + Err(e) => { | |
| 1643 | + // A failed load by drop leaves the running session alone; | |
| 1644 | + // here a file was named and could not be opened, so the | |
| 1645 | + // refusal is the whole run. | |
| 1646 | + eprintln!("cannot open {}: {e}", arg.display()); | |
| 1647 | + std::process::exit(1); | |
| 1648 | + } | |
| 1649 | + } | |
| 1546 | 1650 | } |
| 1651 | + None => None, | |
| 1547 | 1652 | }; |
| 1548 | 1653 | // A keymap file that cannot be read leaves the defaults in force, and takes |
| 1549 | 1654 | // the notice slot ahead of an import refusal below: it is about every key |
| @@ -1555,34 +1660,49 @@ fn main() { | ||
| 1555 | 1660 | // The first file makes the timeline, the rest are appended to it. A refusal |
| 1556 | 1661 | // is not fatal -- the others still load -- but the window must not open |
| 1557 | 1662 | // silently pretending the file was taken, so the first one seeds the notice. |
| 1558 | - for arg in std::env::args().skip(2) { | |
| 1559 | - if let Err(e) = session.import(std::path::Path::new(&arg)) { | |
| 1663 | + for extra in std::env::args().skip(2) { | |
| 1664 | + // Only reachable with a first argument, so there is a timeline. | |
| 1665 | + if let Some(session) = &mut session | |
| 1666 | + && let Err(e) = session.import(std::path::Path::new(&extra)) | |
| 1667 | + { | |
| 1560 | 1668 | let text = format!("IMPORT FAILED: {e}"); |
| 1561 | - eprintln!("{arg}: {text}"); | |
| 1669 | + eprintln!("{extra}: {text}"); | |
| 1562 | 1670 | notice.get_or_insert(text); |
| 1563 | 1671 | } |
| 1564 | 1672 | } |
| 1565 | - let meta = *session.meta(); | |
| 1673 | + let meta = session.as_ref().map(|session| *session.meta()); | |
| 1566 | 1674 | // Beside the media even for a project: an export has never landed anywhere |
| 1567 | 1675 | // but next to the picture it came from. |
| 1568 | - let out = export_path(&session.sources()[0]); | |
| 1569 | - let project = if is_project(&arg) { | |
| 1570 | - arg.clone() | |
| 1571 | - } else { | |
| 1572 | - project_path(&arg) | |
| 1676 | + let out = session | |
| 1677 | + .as_ref() | |
| 1678 | + .map_or_else(PathBuf::new, |session| export_path(&session.sources()[0])); | |
| 1679 | + let project = match &arg { | |
| 1680 | + Some(arg) if is_project(arg) => arg.clone(), | |
| 1681 | + Some(arg) => project_path(arg), | |
| 1682 | + // Chosen with the file, once there is one to choose it from. | |
| 1683 | + None => PathBuf::new(), | |
| 1573 | 1684 | }; |
| 1574 | - let name: SharedString = file_name(&arg).into(); | |
| 1575 | - println!( | |
| 1576 | - "{path}: {}x{} @ {:.2} fps, {} samples", | |
| 1577 | - meta.width, meta.height, meta.frame_rate, meta.frame_count | |
| 1578 | - ); | |
| 1685 | + let name: SharedString = arg | |
| 1686 | + .as_deref() | |
| 1687 | + .map_or_else(|| "no file open".into(), |arg| file_name(arg).into()); | |
| 1688 | + if let (Some(arg), Some(meta)) = (&arg, &meta) { | |
| 1689 | + println!( | |
| 1690 | + "{}: {}x{} @ {:.2} fps, {} samples", | |
| 1691 | + arg.display(), | |
| 1692 | + meta.width, | |
| 1693 | + meta.height, | |
| 1694 | + meta.frame_rate, | |
| 1695 | + meta.frame_count | |
| 1696 | + ); | |
| 1697 | + } | |
| 1579 | 1698 | |
| 1580 | 1699 | Application::new().run(move |cx: &mut App| { |
| 1581 | - let bounds = Bounds::centered( | |
| 1582 | - None, | |
| 1583 | - size(px(meta.width as f32), px(meta.height as f32)), | |
| 1584 | - cx, | |
| 1585 | - ); | |
| 1700 | + // The picture's own size, or 720p when there is no picture yet: the | |
| 1701 | + // empty window is a landing pad, not a sliver. | |
| 1702 | + let (w, h) = meta.map_or((1280., 720.), |meta| { | |
| 1703 | + (meta.width as f32, meta.height as f32) | |
| 1704 | + }); | |
| 1705 | + let bounds = Bounds::centered(None, size(px(w), px(h)), cx); | |
| 1586 | 1706 | cx.open_window( |
| 1587 | 1707 | WindowOptions { |
| 1588 | 1708 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| @@ -1594,21 +1714,26 @@ fn main() { | ||
| 1594 | 1714 | }, |
| 1595 | 1715 | |window, cx| { |
| 1596 | 1716 | let player = cx.new(|cx| Player { |
| 1717 | + // A file named on the command line owes its first frame the | |
| 1718 | + // same repaint a seek's still owes: nothing plays by | |
| 1719 | + // itself, so this is what carries the poster frame to the | |
| 1720 | + // screen. An empty window has none to wait for. | |
| 1721 | + pending_seek: session.is_some(), | |
| 1597 | 1722 | session, |
| 1598 | - fps: meta.frame_rate, | |
| 1723 | + // Only ever used with a timeline; 30 keeps the empty | |
| 1724 | + // timecode reading in frames rather than in NaN. | |
| 1725 | + fps: meta.map_or(30., |meta| meta.frame_rate), | |
| 1599 | 1726 | name: name.clone(), |
| 1600 | 1727 | image: None, |
| 1601 | 1728 | held: None, |
| 1602 | 1729 | eos: false, |
| 1603 | 1730 | done: false, |
| 1604 | - pending_seek: false, | |
| 1605 | 1731 | ruler: Rc::default(), |
| 1606 | 1732 | selected: None, |
| 1607 | 1733 | clipboard: None, |
| 1608 | 1734 | scrubbing: false, |
| 1609 | 1735 | last_scrub: Instant::now(), |
| 1610 | 1736 | last_target: 0, |
| 1611 | - launched: false, | |
| 1612 | 1737 | export: None, |
| 1613 | 1738 | cancelling: false, |
| 1614 | 1739 | export_path: out.clone(), |