Empty start, and opening is not playing
Browse files at this commitc5f4646231b9f3c3bd6e8084fce305adfa95ff0eParent: d598c98- feat
- empty start, and opening is not playing
1 changed file +209 -84
- crates/app/src/main.rs +209 -84
crates/app/src/main.rsmodified+209 -84
| @@ -67,7 +67,10 @@ const KEYS_ROW_H: f32 = HIT_MIN; | |||
| 67 | const ESCAPE: &str = "escape"; | 67 | const ESCAPE: &str = "escape"; |
| 68 | 68 | ||
| 69 | struct Player { | 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 | /// Timeline seconds -> frame index, so the clock can be compared to what | 74 | /// Timeline seconds -> frame index, so the clock can be compared to what |
| 72 | /// the decoder hands over. | 75 | /// the decoder hands over. |
| 73 | fps: f64, | 76 | fps: f64, |
| @@ -96,8 +99,6 @@ struct Player { | |||
| 96 | scrubbing: bool, | 99 | scrubbing: bool, |
| 97 | last_scrub: Instant, | 100 | last_scrub: Instant, |
| 98 | last_target: u32, | 101 | last_target: u32, |
| 99 | - /// Playback was started once, on the first render. The play binding owns it after that. | ||
| 100 | - launched: bool, | ||
| 101 | /// The running export. While it owns the UI the editor is read-only. | 102 | /// The running export. While it owns the UI the editor is read-only. |
| 102 | export: Option<ExportHandle>, | 103 | export: Option<ExportHandle>, |
| 103 | /// The export above was cancelled and is only winding down. The editor is | 104 | /// The export above was cancelled and is only winding down. The editor is |
| @@ -138,7 +139,12 @@ impl Player { | |||
| 138 | /// the channel and only the last of them is shown, which *is* the | 139 | /// the channel and only the last of them is shown, which *is* the |
| 139 | /// drop-when-behind policy. A frame that is not due yet waits in `held`. | 140 | /// drop-when-behind policy. A frame that is not due yet waits in `held`. |
| 140 | fn pump(&mut self, window: &mut Window) { | 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 | let mut newest: Option<Frame> = None; | 148 | let mut newest: Option<Frame> = None; |
| 143 | loop { | 149 | loop { |
| 144 | let frame = match self.held.take() { | 150 | let frame = match self.held.take() { |
| @@ -146,10 +152,10 @@ impl Player { | |||
| 146 | // Nothing waiting means either a clip boundary being rebuilt or | 152 | // Nothing waiting means either a clip boundary being rebuilt or |
| 147 | // the real end of the timeline, and only the engine can tell | 153 | // the real end of the timeline, and only the engine can tell |
| 148 | // them apart -- `frame.index` is already a timeline index. | 154 | // them apart -- `frame.index` is already a timeline index. |
| 149 | - None => match self.session.try_frame() { | 155 | + None => match session.try_frame() { |
| 150 | Some(frame) => frame, | 156 | Some(frame) => frame, |
| 151 | None => { | 157 | None => { |
| 152 | - self.eos = self.session.is_eos(); | 158 | + self.eos = session.is_eos(); |
| 153 | break; | 159 | break; |
| 154 | } | 160 | } |
| 155 | }, | 161 | }, |
| @@ -190,7 +196,7 @@ impl Player { | |||
| 190 | "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s", | 196 | "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s", |
| 191 | self.displayed, | 197 | self.displayed, |
| 192 | self.dropped, | 198 | self.dropped, |
| 193 | - self.session.now() | 199 | + session.now() |
| 194 | ); | 200 | ); |
| 195 | } | 201 | } |
| 196 | } | 202 | } |
| @@ -211,7 +217,10 @@ impl Player { | |||
| 211 | if self.exporting().is_some() { | 217 | if self.exporting().is_some() { |
| 212 | return; | 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 | self.reset_after_reseek(); | 224 | self.reset_after_reseek(); |
| 216 | cx.notify(); | 225 | cx.notify(); |
| 217 | } | 226 | } |
| @@ -222,7 +231,9 @@ impl Player { | |||
| 222 | if self.exporting().is_some() { | 231 | if self.exporting().is_some() { |
| 223 | return; | 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 | self.selected = None; | 237 | self.selected = None; |
| 227 | cx.notify(); | 238 | cx.notify(); |
| 228 | } | 239 | } |
| @@ -233,11 +244,11 @@ impl Player { | |||
| 233 | if self.exporting().is_some() { | 244 | if self.exporting().is_some() { |
| 234 | return; | 245 | return; |
| 235 | } | 246 | } |
| 236 | - if self | 247 | + let deleted = match (&mut self.session, self.selected.take()) { |
| 237 | - .selected | 248 | + (Some(session), Some(i)) => session.delete_clip(i), |
| 238 | - .take() | 249 | + _ => false, |
| 239 | - .is_some_and(|i| self.session.delete_clip(i)) | 250 | + }; |
| 240 | - { | 251 | + if deleted { |
| 241 | self.reset_after_reseek(); | 252 | self.reset_after_reseek(); |
| 242 | } | 253 | } |
| 243 | cx.notify(); | 254 | cx.notify(); |
| @@ -245,7 +256,8 @@ impl Player { | |||
| 245 | 256 | ||
| 246 | /// Copies the selected clip. Nothing on screen changes, so no notify. | 257 | /// Copies the selected clip. Nothing on screen changes, so no notify. |
| 247 | fn copy_selected(&mut self) { | 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 | self.clipboard = Some(clip); | 261 | self.clipboard = Some(clip); |
| 250 | } | 262 | } |
| 251 | } | 263 | } |
| @@ -254,9 +266,11 @@ impl Player { | |||
| 254 | /// like a delete this owes the flag reset -- and the selection, whose index | 266 | /// like a delete this owes the flag reset -- and the selection, whose index |
| 255 | /// the insert has just moved. | 267 | /// the insert has just moved. |
| 256 | fn paste(&mut self, cx: &mut Context<Self>) { | 268 | fn paste(&mut self, cx: &mut Context<Self>) { |
| 257 | - if let Some(clip) = self.clipboard | 269 | + let pasted = match (&mut self.session, self.clipboard) { |
| 258 | - && self.session.paste_at(self.session.now(), clip) | 270 | + (Some(session), Some(clip)) => session.paste_at(session.now(), clip), |
| 259 | - { | 271 | + _ => false, |
| 272 | + }; | ||
| 273 | + if pasted { | ||
| 260 | self.selected = None; | 274 | self.selected = None; |
| 261 | self.reset_after_reseek(); | 275 | self.reset_after_reseek(); |
| 262 | } | 276 | } |
| @@ -271,18 +285,42 @@ impl Player { | |||
| 271 | if self.exporting().is_some() { | 285 | if self.exporting().is_some() { |
| 272 | return; | 286 | return; |
| 273 | } | 287 | } |
| 274 | - let text = match self.session.import(path) { | 288 | + // An empty window has nothing to append to: there the first file *is* |
| 275 | - Ok(()) => { | 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 | self.reset_after_reseek(); | 293 | self.reset_after_reseek(); |
| 277 | format!("IMPORTED {}", file_name(path)) | 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 | eprintln!("{text}"); | 299 | eprintln!("{text}"); |
| 282 | self.notice = Some(text.into()); | 300 | self.notice = Some(text.into()); |
| 283 | cx.notify(); | 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 | /// The Import button: asks the desktop for a path and takes it the same way | 324 | /// The Import button: asks the desktop for a path and takes it the same way |
| 287 | /// a drop would. The chooser is another process and the user may sit in it, | 325 | /// a drop would. The chooser is another process and the user may sit in it, |
| 288 | /// so it runs on a background thread -- blocking here would freeze the | 326 | /// so it runs on a background thread -- blocking here would freeze the |
| @@ -322,11 +360,11 @@ impl Player { | |||
| 322 | } | 360 | } |
| 323 | let text = match PlaybackSession::open_project(path) { | 361 | let text = match PlaybackSession::open_project(path) { |
| 324 | Ok(session) => { | 362 | Ok(session) => { |
| 325 | - self.session = session; | 363 | + self.fps = session.meta().frame_rate; |
| 326 | - self.fps = self.session.meta().frame_rate; | ||
| 327 | // A project is named after itself but still exports beside its | 364 | // A project is named after itself but still exports beside its |
| 328 | // media: that is the only place an export has ever landed. | 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 | self.project_path = path.to_path_buf(); | 368 | self.project_path = path.to_path_buf(); |
| 331 | self.name = file_name(path).into(); | 369 | self.name = file_name(path).into(); |
| 332 | // A copied clip names its source by index, which means a | 370 | // A copied clip names its source by index, which means a |
| @@ -354,9 +392,14 @@ impl Player { | |||
| 354 | /// Writes the timeline back to its project file. Overwrites silently, like | 392 | /// Writes the timeline back to its project file. Overwrites silently, like |
| 355 | /// an export: the path was chosen once and the notice is the confirmation. | 393 | /// an export: the path was chosen once and the notice is the confirmation. |
| 356 | fn save_project(&mut self, cx: &mut Context<Self>) { | 394 | fn save_project(&mut self, cx: &mut Context<Self>) { |
| 357 | - let text = match self.session.save_project(&self.project_path) { | 395 | + let saved = self |
| 358 | - Ok(()) => format!("SAVED {}", file_name(&self.project_path)), | 396 | + .session |
| 359 | - Err(e) => format!("SAVE FAILED: {e}"), | 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 | eprintln!("{text}"); | 404 | eprintln!("{text}"); |
| 362 | self.notice = Some(text.into()); | 405 | self.notice = Some(text.into()); |
| @@ -364,7 +407,7 @@ impl Player { | |||
| 364 | } | 407 | } |
| 365 | 408 | ||
| 366 | fn undo(&mut self, cx: &mut Context<Self>) { | 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 | self.reset_after_reseek(); | 411 | self.reset_after_reseek(); |
| 369 | } | 412 | } |
| 370 | self.selected = None; | 413 | self.selected = None; |
| @@ -404,7 +447,10 @@ impl Player { | |||
| 404 | /// and the release, which must land exactly even when the throttle below | 447 | /// and the release, which must land exactly even when the throttle below |
| 405 | /// would have skipped them. | 448 | /// would have skipped them. |
| 406 | fn scrub_to(&mut self, x: Pixels, commit: bool, cx: &mut Context<Self>) { | 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 | let target = (t * self.fps) as u32; | 454 | let target = (t * self.fps) as u32; |
| 409 | if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) { | 455 | if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) { |
| 410 | self.last_target = target; | 456 | self.last_target = target; |
| @@ -421,9 +467,11 @@ impl Player { | |||
| 421 | } | 467 | } |
| 422 | if self.done { | 468 | if self.done { |
| 423 | self.seek(0., cx); | 469 | self.seek(0., cx); |
| 424 | - self.session.play(); | 470 | + if let Some(session) = &mut self.session { |
| 425 | - } else { | 471 | + session.play(); |
| 426 | - self.session.toggle(); | 472 | + } |
| 473 | + } else if let Some(session) = &mut self.session { | ||
| 474 | + session.toggle(); | ||
| 427 | // Past EOF nothing else asks for a repaint. | 475 | // Past EOF nothing else asks for a repaint. |
| 428 | cx.notify(); | 476 | cx.notify(); |
| 429 | } | 477 | } |
| @@ -444,8 +492,15 @@ impl Player { | |||
| 444 | if self.export.is_some() { | 492 | if self.export.is_some() { |
| 445 | return; | 493 | return; |
| 446 | } | 494 | } |
| 447 | - self.session.pause(); | 495 | + // Nothing to write out, and a refusal rather than a silent button: the |
| 448 | - self.export = Some(self.session.export_to(&self.export_path)); | 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 | cx.notify(); | 504 | cx.notify(); |
| 450 | } | 505 | } |
| 451 | 506 | ||
| @@ -481,17 +536,19 @@ impl Player { | |||
| 481 | 536 | ||
| 482 | impl Render for Player { | 537 | impl Render for Player { |
| 483 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { | 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 | 539 | + // A file that has just been opened sits on its first frame with the |
| 485 | - // the first rendered frame rather than with the process (which would | 540 | + // clock stopped: opening is not playing, whichever way the file |
| 486 | - // charge window setup to the timeline and drop the frames it covers). | 541 | + // arrived. The play binding and the transport button start it. |
| 487 | - if !std::mem::replace(&mut self.launched, true) { | 542 | + if let Some(session) = &mut self.session { |
| 488 | - self.session.play(); | 543 | + session.tick(); |
| 489 | } | 544 | } |
| 490 | - self.session.tick(); | ||
| 491 | self.pump(window); | 545 | self.pump(window); |
| 492 | self.poll_export(); | 546 | self.poll_export(); |
| 493 | // No shadow flag: the clock is the only truth about play state. | 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 | // A paused timeline has nothing to animate; the toggle handlers notify, | 552 | // A paused timeline has nothing to animate; the toggle handlers notify, |
| 496 | // which is what starts the loop again. A paused seek keeps the loop | 553 | // which is what starts the loop again. A paused seek keeps the loop |
| 497 | // running by itself until `pump` has the frame it asked for. An export | 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 | // Read per render, never cached: a delete shortens the timeline and the | 563 | // Read per render, never cached: a delete shortens the timeline and the |
| 507 | // timecode, the ruler and the clamp below all have to follow it. | 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 | // The clock keeps running after the last frame (wall time takes over at | 569 | // The clock keeps running after the last frame (wall time takes over at |
| 510 | // audio EOF) while the picture is frozen, so the timeline the UI shows is | 570 | // audio EOF) while the picture is frozen, so the timeline the UI shows is |
| 511 | // the clamped one, pinned to the out-point once playback is done. | 571 | // the clamped one, pinned to the out-point once playback is done. |
| 512 | let position = if self.done { | 572 | let position = if self.done { |
| 513 | duration | 573 | duration |
| 514 | } else { | 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 | div() | 581 | div() |
| @@ -654,7 +717,21 @@ impl Render for Player { | |||
| 654 | .children( | 717 | .children( |
| 655 | self.image | 718 | self.image |
| 656 | .clone() | 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 | // Above the panel and only when there is one to show, so it costs | 737 | // Above the panel and only when there is one to show, so it costs |
| @@ -684,6 +761,9 @@ impl Player { | |||
| 684 | // percentage and the accent bar are the same number, so the playhead | 761 | // percentage and the accent bar are the same number, so the playhead |
| 685 | // fill doubles as the progress bar for free. | 762 | // fill doubles as the progress bar for free. |
| 686 | let exporting = self.exporting().is_some(); | 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 | let key = |action| self.keymap.display(action); | 767 | let key = |action| self.keymap.display(action); |
| 688 | let (hint, filled) = if let Some(export) = self.exporting() { | 768 | let (hint, filled) = if let Some(export) = self.exporting() { |
| 689 | let progress = export.progress(); | 769 | let progress = export.progress(); |
| @@ -732,7 +812,7 @@ impl Player { | |||
| 732 | Some(transport_glyph(playing).into_any_element()), | 812 | Some(transport_glyph(playing).into_any_element()), |
| 733 | if playing { "Pause" } else { "Play" }, | 813 | if playing { "Pause" } else { "Play" }, |
| 734 | key(ActionId::Play), | 814 | key(ActionId::Play), |
| 735 | - !exporting, | 815 | + live, |
| 736 | cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)), | 816 | cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)), |
| 737 | )) | 817 | )) |
| 738 | .child(separator()) | 818 | .child(separator()) |
| @@ -744,7 +824,7 @@ impl Player { | |||
| 744 | "{} — splits the clip under the playhead", | 824 | "{} — splits the clip under the playhead", |
| 745 | key(ActionId::Cut) | 825 | key(ActionId::Cut) |
| 746 | ), | 826 | ), |
| 747 | - !exporting, | 827 | + live, |
| 748 | cx.listener(|this, _: &ClickEvent, _, cx| this.cut(cx)), | 828 | cx.listener(|this, _: &ClickEvent, _, cx| this.cut(cx)), |
| 749 | )) | 829 | )) |
| 750 | .child(control( | 830 | .child(control( |
| @@ -756,7 +836,7 @@ impl Player { | |||
| 756 | } else { | 836 | } else { |
| 757 | format!("{} — click a clip below first", key(ActionId::Delete)) | 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 | cx.listener(|this, _: &ClickEvent, _, cx| this.delete_selected(cx)), | 840 | cx.listener(|this, _: &ClickEvent, _, cx| this.delete_selected(cx)), |
| 761 | )) | 841 | )) |
| 762 | .child(separator()) | 842 | .child(separator()) |
| @@ -776,7 +856,7 @@ impl Player { | |||
| 776 | "{} — writes the timeline out beside the source", | 856 | "{} — writes the timeline out beside the source", |
| 777 | key(ActionId::Export) | 857 | key(ActionId::Export) |
| 778 | ), | 858 | ), |
| 779 | - self.export.is_none(), | 859 | + live && self.export.is_none(), |
| 780 | cx.listener(|this, _: &ClickEvent, _, cx| this.start_export(cx)), | 860 | cx.listener(|this, _: &ClickEvent, _, cx| this.start_export(cx)), |
| 781 | )) | 861 | )) |
| 782 | .child(control( | 862 | .child(control( |
| @@ -784,7 +864,7 @@ impl Player { | |||
| 784 | None, | 864 | None, |
| 785 | "Save", | 865 | "Save", |
| 786 | format!("{} — writes the project file", key(ActionId::Save)), | 866 | format!("{} — writes the project file", key(ActionId::Save)), |
| 787 | - !exporting, | 867 | + live, |
| 788 | cx.listener(|this, _: &ClickEvent, _, cx| this.save_project(cx)), | 868 | cx.listener(|this, _: &ClickEvent, _, cx| this.save_project(cx)), |
| 789 | )) | 869 | )) |
| 790 | // No shortcut of its own -- and closed while an export runs, | 870 | // No shortcut of its own -- and closed while an export runs, |
| @@ -1024,7 +1104,9 @@ impl Player { | |||
| 1024 | .overflow_hidden() | 1104 | .overflow_hidden() |
| 1025 | .children( | 1105 | .children( |
| 1026 | self.session | 1106 | self.session |
| 1027 | - .clip_spans_by_source() | 1107 | + .as_ref() |
| 1108 | + .map(PlaybackSession::clip_spans_by_source) | ||
| 1109 | + .unwrap_or_default() | ||
| 1028 | .into_iter() | 1110 | .into_iter() |
| 1029 | .enumerate() | 1111 | .enumerate() |
| 1030 | .map(|(i, (_, len, source))| { | 1112 | .map(|(i, (_, len, source))| { |
| @@ -1275,6 +1357,24 @@ fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 { | |||
| 1275 | ((x - bounds.left()) / bounds.size.width).clamp(0., 1.) | 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 | /// The second lane: still a placeholder, deliberately empty. | 1378 | /// The second lane: still a placeholder, deliberately empty. |
| 1279 | fn track_lane() -> impl IntoElement { | 1379 | fn track_lane() -> impl IntoElement { |
| 1280 | div() | 1380 | div() |
| @@ -1524,26 +1624,31 @@ mod tests { | |||
| 1524 | } | 1624 | } |
| 1525 | 1625 | ||
| 1526 | fn main() { | 1626 | fn main() { |
| 1527 | - let Some(path) = std::env::args().nth(1) else { | 1627 | + let arg = std::env::args().nth(1).map(PathBuf::from); |
| 1528 | - eprintln!("usage: app <video.mp4|project.edith> [more.mp4 ...]"); | ||
| 1529 | - std::process::exit(2); | ||
| 1530 | - }; | ||
| 1531 | - let arg = PathBuf::from(&path); | ||
| 1532 | // A `.edith` restores a whole timeline, anything else *is* the timeline. | 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. | 1629 | + // Either way the rest of argv is appended to what comes out. No argument at |
| 1534 | - let opened = if is_project(&arg) { | 1630 | + // all opens the window empty -- the timeline then arrives by drop or by the |
| 1535 | - PlaybackSession::open_project(&arg) | 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) | ||
| 1536 | } else { | 1637 | } else { |
| 1537 | - PlaybackSession::open(&arg) | 1638 | + PlaybackSession::open(arg) |
| 1538 | }; | 1639 | }; |
| 1539 | - let mut session = match opened { | 1640 | + match opened { |
| 1540 | - Ok(v) => v, | 1641 | + Ok(v) => Some(v), |
| 1541 | Err(e) => { | 1642 | Err(e) => { |
| 1542 | - // A failed load by drop leaves the running session alone; here | 1643 | + // A failed load by drop leaves the running session alone; |
| 1543 | - // there is no session to leave, so the refusal is the whole run. | 1644 | + // here a file was named and could not be opened, so the |
| 1544 | - eprintln!("cannot open {path}: {e}"); | 1645 | + // refusal is the whole run. |
| 1646 | + eprintln!("cannot open {}: {e}", arg.display()); | ||
| 1545 | std::process::exit(1); | 1647 | std::process::exit(1); |
| 1546 | } | 1648 | } |
| 1649 | + } | ||
| 1650 | + } | ||
| 1651 | + None => None, | ||
| 1547 | }; | 1652 | }; |
| 1548 | // A keymap file that cannot be read leaves the defaults in force, and takes | 1653 | // A keymap file that cannot be read leaves the defaults in force, and takes |
| 1549 | // the notice slot ahead of an import refusal below: it is about every key | 1654 | // the notice slot ahead of an import refusal below: it is about every key |
| @@ -1555,34 +1660,49 @@ fn main() { | |||
| 1555 | // The first file makes the timeline, the rest are appended to it. A refusal | 1660 | // The first file makes the timeline, the rest are appended to it. A refusal |
| 1556 | // is not fatal -- the others still load -- but the window must not open | 1661 | // is not fatal -- the others still load -- but the window must not open |
| 1557 | // silently pretending the file was taken, so the first one seeds the notice. | 1662 | // silently pretending the file was taken, so the first one seeds the notice. |
| 1558 | - for arg in std::env::args().skip(2) { | 1663 | + for extra in std::env::args().skip(2) { |
| 1559 | - if let Err(e) = session.import(std::path::Path::new(&arg)) { | 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 | let text = format!("IMPORT FAILED: {e}"); | 1668 | let text = format!("IMPORT FAILED: {e}"); |
| 1561 | - eprintln!("{arg}: {text}"); | 1669 | + eprintln!("{extra}: {text}"); |
| 1562 | notice.get_or_insert(text); | 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 | // Beside the media even for a project: an export has never landed anywhere | 1674 | // Beside the media even for a project: an export has never landed anywhere |
| 1567 | // but next to the picture it came from. | 1675 | // but next to the picture it came from. |
| 1568 | - let out = export_path(&session.sources()[0]); | 1676 | + let out = session |
| 1569 | - let project = if is_project(&arg) { | 1677 | + .as_ref() |
| 1570 | - arg.clone() | 1678 | + .map_or_else(PathBuf::new, |session| export_path(&session.sources()[0])); |
| 1571 | - } else { | 1679 | + let project = match &arg { |
| 1572 | - project_path(&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(); | 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) { | ||
| 1575 | println!( | 1689 | println!( |
| 1576 | - "{path}: {}x{} @ {:.2} fps, {} samples", | 1690 | + "{}: {}x{} @ {:.2} fps, {} samples", |
| 1577 | - meta.width, meta.height, meta.frame_rate, meta.frame_count | 1691 | + arg.display(), |
| 1692 | + meta.width, | ||
| 1693 | + meta.height, | ||
| 1694 | + meta.frame_rate, | ||
| 1695 | + meta.frame_count | ||
| 1578 | ); | 1696 | ); |
| 1697 | + } | ||
| 1579 | 1698 | ||
| 1580 | Application::new().run(move |cx: &mut App| { | 1699 | Application::new().run(move |cx: &mut App| { |
| 1581 | - let bounds = Bounds::centered( | 1700 | + // The picture's own size, or 720p when there is no picture yet: the |
| 1582 | - None, | 1701 | + // empty window is a landing pad, not a sliver. |
| 1583 | - size(px(meta.width as f32), px(meta.height as f32)), | 1702 | + let (w, h) = meta.map_or((1280., 720.), |meta| { |
| 1584 | - cx, | 1703 | + (meta.width as f32, meta.height as f32) |
| 1585 | - ); | 1704 | + }); |
| 1705 | + let bounds = Bounds::centered(None, size(px(w), px(h)), cx); | ||
| 1586 | cx.open_window( | 1706 | cx.open_window( |
| 1587 | WindowOptions { | 1707 | WindowOptions { |
| 1588 | window_bounds: Some(WindowBounds::Windowed(bounds)), | 1708 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| @@ -1594,21 +1714,26 @@ fn main() { | |||
| 1594 | }, | 1714 | }, |
| 1595 | |window, cx| { | 1715 | |window, cx| { |
| 1596 | let player = cx.new(|cx| Player { | 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 | session, | 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 | name: name.clone(), | 1726 | name: name.clone(), |
| 1600 | image: None, | 1727 | image: None, |
| 1601 | held: None, | 1728 | held: None, |
| 1602 | eos: false, | 1729 | eos: false, |
| 1603 | done: false, | 1730 | done: false, |
| 1604 | - pending_seek: false, | ||
| 1605 | ruler: Rc::default(), | 1731 | ruler: Rc::default(), |
| 1606 | selected: None, | 1732 | selected: None, |
| 1607 | clipboard: None, | 1733 | clipboard: None, |
| 1608 | scrubbing: false, | 1734 | scrubbing: false, |
| 1609 | last_scrub: Instant::now(), | 1735 | last_scrub: Instant::now(), |
| 1610 | last_target: 0, | 1736 | last_target: 0, |
| 1611 | - launched: false, | ||
| 1612 | export: None, | 1737 | export: None, |
| 1613 | cancelling: false, | 1738 | cancelling: false, |
| 1614 | export_path: out.clone(), | 1739 | export_path: out.clone(), |