forge
Explore
Log in

Don't you dare go Hollow

Empty start, and opening is not playing

Browse files at this commit

c5f4646231b9f3c3bd6e8084fce305adfa95ff0eAhmet Kaan GümüşParent: d598c98
  • feat
    • empty start, and opening is not playing
Show full message
✨ feat(app): empty start, and opening is not playing

With no argument the window opens empty at 720p instead of exiting: it
says what it is waiting for, the whole window is already the drop target,
and the first media import or .edith load becomes the timeline. Player
.session is an Option and every action that needs one refuses in the
notice slot or dims its button rather than acting on nothing.

The first-render play() is gone with it, so a file arrives paused on its
first frame -- a project on its saved playhead -- and the play binding is
what starts it.
crates/app/src/main.rsmodified+215 -90
@@ -67,7 +67,10 @@ const KEYS_ROW_H: f32 = HIT_MIN;
6767 const ESCAPE: &str = "escape";
6868
6969 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>,
7174 /// Timeline seconds -> frame index, so the clock can be compared to what
7275 /// the decoder hands over.
7376 fps: f64,
@@ -96,8 +99,6 @@ struct Player {
9699 scrubbing: bool,
97100 last_scrub: Instant,
98101 last_target: u32,
99- /// Playback was started once, on the first render. The play binding owns it after that.
100- launched: bool,
101102 /// The running export. While it owns the UI the editor is read-only.
102103 export: Option<ExportHandle>,
103104 /// The export above was cancelled and is only winding down. The editor is
@@ -138,7 +139,12 @@ impl Player {
138139 /// the channel and only the last of them is shown, which *is* the
139140 /// drop-when-behind policy. A frame that is not due yet waits in `held`.
140141 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;
142148 let mut newest: Option<Frame> = None;
143149 loop {
144150 let frame = match self.held.take() {
@@ -146,10 +152,10 @@ impl Player {
146152 // Nothing waiting means either a clip boundary being rebuilt or
147153 // the real end of the timeline, and only the engine can tell
148154 // them apart -- `frame.index` is already a timeline index.
149- None => match self.session.try_frame() {
155+ None => match session.try_frame() {
150156 Some(frame) => frame,
151157 None => {
152- self.eos = self.session.is_eos();
158+ self.eos = session.is_eos();
153159 break;
154160 }
155161 },
@@ -190,7 +196,7 @@ impl Player {
190196 "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s",
191197 self.displayed,
192198 self.dropped,
193- self.session.now()
199+ session.now()
194200 );
195201 }
196202 }
@@ -211,7 +217,10 @@ impl Player {
211217 if self.exporting().is_some() {
212218 return;
213219 }
214- self.session.seek(t);
220+ let Some(session) = &mut self.session else {
221+ return;
222+ };
223+ session.seek(t);
215224 self.reset_after_reseek();
216225 cx.notify();
217226 }
@@ -222,7 +231,9 @@ impl Player {
222231 if self.exporting().is_some() {
223232 return;
224233 }
225- self.session.cut_at(self.session.now());
234+ if let Some(session) = &mut self.session {
235+ session.cut_at(session.now());
236+ }
226237 self.selected = None;
227238 cx.notify();
228239 }
@@ -233,11 +244,11 @@ impl Player {
233244 if self.exporting().is_some() {
234245 return;
235246 }
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 {
241252 self.reset_after_reseek();
242253 }
243254 cx.notify();
@@ -245,7 +256,8 @@ impl Player {
245256
246257 /// Copies the selected clip. Nothing on screen changes, so no notify.
247258 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)) {
249261 self.clipboard = Some(clip);
250262 }
251263 }
@@ -254,9 +266,11 @@ impl Player {
254266 /// like a delete this owes the flag reset -- and the selection, whose index
255267 /// the insert has just moved.
256268 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 {
260274 self.selected = None;
261275 self.reset_after_reseek();
262276 }
@@ -271,18 +285,42 @@ impl Player {
271285 if self.exporting().is_some() {
272286 return;
273287 }
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(())) => {
276293 self.reset_after_reseek();
277294 format!("IMPORTED {}", file_name(path))
278295 }
279- Err(e) => format!("IMPORT FAILED: {e}"),
296+ Some(Err(e)) => format!("IMPORT FAILED: {e}"),
297+ None => self.open_media(path),
280298 };
281299 eprintln!("{text}");
282300 self.notice = Some(text.into());
283301 cx.notify();
284302 }
285303
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+
286324 /// The Import button: asks the desktop for a path and takes it the same way
287325 /// a drop would. The chooser is another process and the user may sit in it,
288326 /// so it runs on a background thread -- blocking here would freeze the
@@ -322,11 +360,11 @@ impl Player {
322360 }
323361 let text = match PlaybackSession::open_project(path) {
324362 Ok(session) => {
325- self.session = session;
326- self.fps = self.session.meta().frame_rate;
363+ self.fps = session.meta().frame_rate;
327364 // A project is named after itself but still exports beside its
328365 // 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);
330368 self.project_path = path.to_path_buf();
331369 self.name = file_name(path).into();
332370 // A copied clip names its source by index, which means a
@@ -354,9 +392,14 @@ impl Player {
354392 /// Writes the timeline back to its project file. Overwrites silently, like
355393 /// an export: the path was chosen once and the notice is the confirmation.
356394 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(),
360403 };
361404 eprintln!("{text}");
362405 self.notice = Some(text.into());
@@ -364,7 +407,7 @@ impl Player {
364407 }
365408
366409 fn undo(&mut self, cx: &mut Context<Self>) {
367- if self.session.undo() {
410+ if self.session.as_mut().is_some_and(PlaybackSession::undo) {
368411 self.reset_after_reseek();
369412 }
370413 self.selected = None;
@@ -404,7 +447,10 @@ impl Player {
404447 /// and the release, which must land exactly even when the throttle below
405448 /// would have skipped them.
406449 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();
408454 let target = (t * self.fps) as u32;
409455 if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) {
410456 self.last_target = target;
@@ -421,9 +467,11 @@ impl Player {
421467 }
422468 if self.done {
423469 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();
427475 // Past EOF nothing else asks for a repaint.
428476 cx.notify();
429477 }
@@ -444,8 +492,15 @@ impl Player {
444492 if self.export.is_some() {
445493 return;
446494 }
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));
449504 cx.notify();
450505 }
451506
@@ -481,17 +536,19 @@ impl Player {
481536
482537 impl Render for Player {
483538 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();
489544 }
490- self.session.tick();
491545 self.pump(window);
492546 self.poll_export();
493547 // 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);
495552 // A paused timeline has nothing to animate; the toggle handlers notify,
496553 // which is what starts the loop again. A paused seek keeps the loop
497554 // running by itself until `pump` has the frame it asked for. An export
@@ -505,14 +562,20 @@ impl Render for Player {
505562
506563 // Read per render, never cached: a delete shortens the timeline and the
507564 // 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);
509569 // The clock keeps running after the last frame (wall time takes over at
510570 // audio EOF) while the picture is frozen, so the timeline the UI shows is
511571 // the clamped one, pinned to the out-point once playback is done.
512572 let position = if self.done {
513573 duration
514574 } else {
515- self.session.now().clamp(0., duration)
575+ self.session
576+ .as_ref()
577+ .map_or(0., PlaybackSession::now)
578+ .clamp(0., duration)
516579 };
517580
518581 div()
@@ -654,7 +717,21 @@ impl Render for Player {
654717 .children(
655718 self.image
656719 .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+ }),
658735 ),
659736 )
660737 // Above the panel and only when there is one to show, so it costs
@@ -684,6 +761,9 @@ impl Player {
684761 // percentage and the accent bar are the same number, so the playhead
685762 // fill doubles as the progress bar for free.
686763 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;
687767 let key = |action| self.keymap.display(action);
688768 let (hint, filled) = if let Some(export) = self.exporting() {
689769 let progress = export.progress();
@@ -732,7 +812,7 @@ impl Player {
732812 Some(transport_glyph(playing).into_any_element()),
733813 if playing { "Pause" } else { "Play" },
734814 key(ActionId::Play),
735- !exporting,
815+ live,
736816 cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)),
737817 ))
738818 .child(separator())
@@ -744,7 +824,7 @@ impl Player {
744824 "{} — splits the clip under the playhead",
745825 key(ActionId::Cut)
746826 ),
747- !exporting,
827+ live,
748828 cx.listener(|this, _: &ClickEvent, _, cx| this.cut(cx)),
749829 ))
750830 .child(control(
@@ -756,7 +836,7 @@ impl Player {
756836 } else {
757837 format!("{} — click a clip below first", key(ActionId::Delete))
758838 },
759- !exporting && self.selected.is_some(),
839+ live && self.selected.is_some(),
760840 cx.listener(|this, _: &ClickEvent, _, cx| this.delete_selected(cx)),
761841 ))
762842 .child(separator())
@@ -776,7 +856,7 @@ impl Player {
776856 "{} — writes the timeline out beside the source",
777857 key(ActionId::Export)
778858 ),
779- self.export.is_none(),
859+ live && self.export.is_none(),
780860 cx.listener(|this, _: &ClickEvent, _, cx| this.start_export(cx)),
781861 ))
782862 .child(control(
@@ -784,7 +864,7 @@ impl Player {
784864 None,
785865 "Save",
786866 format!("{} — writes the project file", key(ActionId::Save)),
787- !exporting,
867+ live,
788868 cx.listener(|this, _: &ClickEvent, _, cx| this.save_project(cx)),
789869 ))
790870 // No shortcut of its own -- and closed while an export runs,
@@ -1024,7 +1104,9 @@ impl Player {
10241104 .overflow_hidden()
10251105 .children(
10261106 self.session
1027- .clip_spans_by_source()
1107+ .as_ref()
1108+ .map(PlaybackSession::clip_spans_by_source)
1109+ .unwrap_or_default()
10281110 .into_iter()
10291111 .enumerate()
10301112 .map(|(i, (_, len, source))| {
@@ -1275,6 +1357,24 @@ fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 {
12751357 ((x - bounds.left()) / bounds.size.width).clamp(0., 1.)
12761358 }
12771359
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+
12781378 /// The second lane: still a placeholder, deliberately empty.
12791379 fn track_lane() -> impl IntoElement {
12801380 div()
@@ -1524,26 +1624,31 @@ mod tests {
15241624 }
15251625
15261626 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);
15321628 // 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+ }
15461650 }
1651+ None => None,
15471652 };
15481653 // A keymap file that cannot be read leaves the defaults in force, and takes
15491654 // the notice slot ahead of an import refusal below: it is about every key
@@ -1555,34 +1660,49 @@ fn main() {
15551660 // The first file makes the timeline, the rest are appended to it. A refusal
15561661 // is not fatal -- the others still load -- but the window must not open
15571662 // 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+ {
15601668 let text = format!("IMPORT FAILED: {e}");
1561- eprintln!("{arg}: {text}");
1669+ eprintln!("{extra}: {text}");
15621670 notice.get_or_insert(text);
15631671 }
15641672 }
1565- let meta = *session.meta();
1673+ let meta = session.as_ref().map(|session| *session.meta());
15661674 // Beside the media even for a project: an export has never landed anywhere
15671675 // 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(),
15731684 };
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+ }
15791698
15801699 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);
15861706 cx.open_window(
15871707 WindowOptions {
15881708 window_bounds: Some(WindowBounds::Windowed(bounds)),
@@ -1594,21 +1714,26 @@ fn main() {
15941714 },
15951715 |window, cx| {
15961716 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(),
15971722 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),
15991726 name: name.clone(),
16001727 image: None,
16011728 held: None,
16021729 eos: false,
16031730 done: false,
1604- pending_seek: false,
16051731 ruler: Rc::default(),
16061732 selected: None,
16071733 clipboard: None,
16081734 scrubbing: false,
16091735 last_scrub: Instant::now(),
16101736 last_target: 0,
1611- launched: false,
16121737 export: None,
16131738 cancelling: false,
16141739 export_path: out.clone(),