diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs
index df400bf..5888fd6 100644
--- a/crates/app/src/main.rs
+++ b/crates/app/src/main.rs
@@ -1,4 +1,5 @@
mod keymap;
+mod player;
mod ui;
mod audio_cards;
@@ -502,4337 +503,6 @@ struct Player {
focus: FocusHandle,
}
-impl Player {
- /// Catches the display up to the clock: everything already due is taken off
- /// the channel and only the last of them is shown, which *is* the
- /// drop-when-behind policy. A frame that is not due yet waits in `held`, and
- /// while the clock is paused *nothing* is due -- a repaint re-presents the
- /// frame already on screen, whatever asked for the repaint.
- fn pump(&mut self, window: &mut Window) {
- // Where the transport was before this drain, so the crossing into
- // `Ended` can be recognised as the one transition it is.
- let was = self.transport();
- // No timeline, nothing to catch up to: the window is showing its empty
- // state and there is no decoder to drain.
- let Some(session) = &mut self.session else {
- return;
- };
- let target = session.now() * self.fps;
- let mut newest: Option = None;
- // A frame the screen is owed: a seek's landing, and the one readiness
- // signal there is ([`Player::reset_after_reseek`]).
- let owed = self.seek_since.is_some();
- // Paused, the clock is frozen and *nothing new is due*. Whatever the
- // decoder is still handing over is the backlog it was behind by when
- // the pause landed -- frames at a position the transport has already
- // left -- and taking one per repaint is what walked the picture on
- // after the sound had stopped, at exactly the rate the pointer was
- // moved over the timeline. Gated here, at the one place a frame ever
- // reaches the screen, rather than in the handlers that repaint: a
- // hover, a notice, a resize and a vsync are all the same event to this.
- // An owed frame is still taken, playing or not -- a scrub is paused by
- // definition, and its landing is the whole point of it.
- while session.is_playing() || owed {
- let frame = match self.held.take() {
- Some(frame) => frame,
- // Nothing waiting means either a clip boundary being rebuilt or
- // the real end of the timeline, and only the engine can tell
- // them apart -- `frame.index` is already a timeline index.
- None => match session.try_frame() {
- Some(frame) => frame,
- None => break,
- },
- };
- if f64::from(frame.index) <= target {
- self.dropped += u32::from(newest.is_some());
- newest = Some(frame);
- } else {
- self.held = Some(frame);
- break;
- }
- }
-
- // How far behind the master clock the picture just handed over is, in
- // seconds. Measured off a frame that really arrived and nothing else: a
- // clip boundary being reopened delivers nothing at all for hundreds of
- // milliseconds, and restarting *that* would only cancel the open it is
- // waiting on.
- let late = newest
- .as_ref()
- .map_or(0., |f| (target - f64::from(f.index)) / self.fps);
-
- if let Some(frame) = newest {
- self.displayed += 1;
- self.seek_since = None;
- self.started.get_or_insert_with(|| {
- eprintln!("first frame displayed (index {})", frame.index);
- Instant::now()
- });
- let buf = image::RgbaImage::from_raw(frame.width, frame.height, frame.bgra)
- .expect("frame buffer sized width*height*4");
- // Counted here rather than under `color_open`, because the card
- // opens on a frame that was pumped before it: gating this on the
- // card would leave its graph flat until something reseeked. A
- // thousandth of the pixels, against a conversion that just touched
- // all of them.
- self.histogram = histogram(buf.as_raw());
- let next = Arc::new(RenderImage::new(vec![image::Frame::new(buf)]));
- if let Some(old) = self.image.replace(next) {
- // Every RenderImage gets a fresh id and its own atlas tile:
- // without this the sprite atlas grows for the whole video.
- let _ = window.drop_image(old);
- }
- }
-
- // Audio is the master clock and a decoder that cannot keep up with it
- // never gets back on its own: it hands over every frame in order,
- // whether or not its moment has passed, so what it is behind by only
- // grows -- a minute in, the picture is seconds behind what is being
- // heard, and that is the whole of "the video can't catch the audio".
- // Past `LATE_RESYNC` the backlog is abandoned and the picture restarted
- // at the clock, which touches neither the sound nor the clock
- // (`PlaybackSession::resync_picture`), so nothing the ear is following
- // moves. Never on a frame a seek owed: that one is late by however long
- // its own reopen took, and answering it with another reopen is a loop.
- //
- // corner-cut: on a machine that cannot decode the file in real time at
- // all this settles into one restart per `RESYNC_GAP` -- in sync, and
- // stuttering, which is the honest picture of what that machine can do.
- // The upgrade path is dropping late frames *inside* the worker (skip
- // the convert and the send for anything already past due), which needs
- // the deadline shared with it.
- if !owed && session.is_playing() && should_resync(late, self.resynced) {
- eprintln!("picture {late:.3}s behind the clock: restarting it there");
- session.resync_picture();
- self.held = None;
- self.resynced = Some(Instant::now());
- }
-
- if self.transport() == Transport::Ended {
- // A seek whose worker never produced a frame (vanished file) would
- // otherwise repaint at vsync forever. Held clear for as long as the
- // state does, not just on the crossing: nothing else is coming.
- self.seek_since = None;
- if was != Transport::Ended {
- // Ended is a *stopped* transport, so the clock stops with it,
- // on the out point the timecode and the playhead have been
- // showing all along. Nothing else ever stopped it: past the
- // last frame wall time takes over and `now()` walks off the end
- // of the timeline for as long as the window is left open -- and
- // the playhead is what a cut, a paste, an insert and the
- // analyser all act at, so every one of them was aiming into
- // empty space (measured: a 5 s timeline recognised its end at
- // clock 17.5 s under a slow renderer). End of stream is left
- // set, so this is still `Ended` and the next press restarts.
- if let Some(session) = &mut self.session {
- session.halt_at_end();
- }
- let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64());
- eprintln!(
- "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s",
- self.displayed,
- self.dropped,
- self.session.as_ref().map_or(0., PlaybackSession::now)
- );
- }
- }
- }
-
- /// Where the transport is, asked of the session rather than remembered:
- /// end of stream is the engine's own flag (any seek clears it, which is why
- /// an edit past the end revives the picture) and so is the clock. A held
- /// frame is one still owed to the screen, so the end is not the end yet.
- fn transport(&self) -> Transport {
- let Some(session) = &self.session else {
- return Transport::Stopped;
- };
- transport(
- session.is_playing(),
- session.is_eos() && self.held.is_none(),
- )
- }
-
- /// A frame owed to the screen after a reseek, and the buffered one dropped:
- /// what stops the picture from staying frozen on the old last frame. The
- /// end-of-stream flag itself is the engine's and its own seek clears it --
- /// edits reseek inside the engine and still owe this.
- fn reset_after_reseek(&mut self) {
- self.held = None;
- // Restarted on every reseek, not only on the first: what it measures is
- // the open now standing, which is what a person is waiting on.
- self.seek_since = Some(Instant::now());
- // A seek is a person saying where to look, so it takes the view back
- // from an earlier scroll: the frame asked for is the one to be shown.
- self.panned = false;
- // An edit moves the indices a drag in flight is holding -- a stroke
- // during one is exactly that -- and an edge committed against a moved
- // index would trim a clip nobody grabbed. Dropping it is the whole fix:
- // nothing has been written yet.
- self.trim = None;
- // ...and the shadow a drag is drawn under promises a landing on a lane
- // this edit has just reshaped. The next move of the drag draws it
- // again; until then it says nothing.
- self.ghost = None;
- }
-
- /// What an action does, wherever it was asked for -- a stroke, or the clip
- /// menu item that names the same action. One table, so the two can never
- /// come to mean different things.
- fn act(&mut self, action: ActionId, cx: &mut Context) {
- // Two doors, one oracle. This used to be the asymmetry the whole
- // toolbar was built on: the buttons dimmed themselves off
- // [`enable`] while the keyboard walked straight past it, so with no
- // file open `s` toggled the snap and `v` added a track while the very
- // same controls sat dim and *dead* to the pointer. Whatever refuses the
- // button refuses the key, in the oracle's own words -- and a refusal
- // that is silent from the keyboard is a bug the same size.
- match self.enable(action, None) {
- Enable::Yes => {}
- // A state refusal is spoken: the thing exists and cannot happen
- // *now*, which is exactly what a silent key press fails to say.
- Enable::No(why) => {
- self.notify_user(format!("{} — {why}", action.label()).into());
- cx.notify();
- return;
- }
- // A class refusal is not: the action does not exist for what is in
- // front of the user, and `esc` with nothing exporting must not
- // answer with a line about exports.
- Enable::Hidden(_) => return,
- }
- match action {
- ActionId::Play => self.toggle_or_restart(cx),
- ActionId::StepBack => self.step(-1, cx),
- ActionId::StepForward => self.step(1, cx),
- // A second is however many frames this timeline runs at.
- ActionId::JumpBack => self.step(-(self.fps.round() as i64), cx),
- ActionId::JumpForward => self.step(self.fps.round() as i64, cx),
- // The ends, as a step nothing can be far enough from.
- ActionId::GoStart => self.step(i64::MIN, cx),
- ActionId::GoEnd => self.step(i64::MAX, cx),
- // Not a step at all: the grid these land on is the *source's*, and
- // where the next one is depends on the file rather than on the rate.
- ActionId::PrevSyncPoint => self.jump_sync(false, cx),
- ActionId::NextSyncPoint => self.jump_sync(true, cx),
- ActionId::Export => self.open_export(cx),
- ActionId::Save => self.save_project(cx),
- ActionId::Copy => self.copy_selected(),
- ActionId::Paste => self.paste(cx),
- ActionId::Cut => self.cut(cx),
- ActionId::Regroup => self.regroup(cx),
- ActionId::Detach => self.detach(cx),
- ActionId::Group => self.group(cx),
- ActionId::Select => self.select_under_playhead(cx),
- ActionId::SelectNext => self.select_step(true, cx),
- ActionId::SelectPrev => self.select_step(false, cx),
- ActionId::Delete => self.delete_selected(cx),
- ActionId::Lift => self.lift_selected(cx),
- ActionId::Color => self.open_color(cx),
- ActionId::Fit => self.cycle_fit(cx),
- ActionId::Resolution => self.cycle_resolution(cx),
- // The playhead is what a key zoom is aimed at: it is the one place
- // on the timeline the user is certainly looking at, and keeping it
- // still is what every editor does.
- ActionId::ZoomIn => self.zoom(ZOOM_STEP, None, cx),
- ActionId::ZoomOut => self.zoom(1. / ZOOM_STEP, None, cx),
- ActionId::ZoomFit => self.zoom_fit(cx),
- ActionId::Undo => self.undo(cx),
- ActionId::AddVideoLane => self.add_lane(LaneKind::Video, cx),
- ActionId::AddAudioLane => self.add_lane(LaneKind::Audio, cx),
- // The last track of that kind: the one the add key put there, so the
- // two strokes undo each other press for press. Any other track goes
- // through the × in its own header.
- ActionId::RemoveVideoLane => self.remove_last_lane(LaneKind::Video, cx),
- ActionId::RemoveAudioLane => self.remove_last_lane(LaneKind::Audio, cx),
- // The same chooser the + S button opens, and the picked row -- the
- // one the panel draws highlighted -- for the removal: the × on any
- // other row is that row's own door, and both doors are one call.
- ActionId::AddSubtitleTrack => self.pick_and_add_subtitles(cx),
- ActionId::RemoveSubtitleTrack => self.remove_subtitle_track(self.sub_track, cx),
- ActionId::ToggleMute => self.set_volume(|volume| volume.muted = !volume.muted, cx),
- ActionId::VolumeUp => self.set_volume(|volume| volume.step(true), cx),
- ActionId::VolumeDown => self.set_volume(|volume| volume.step(false), cx),
- ActionId::Equalizer => self.open_eq(cx),
- ActionId::Speed => self.open_speed(cx),
- ActionId::Silence => self.open_silence(cx),
- ActionId::Mix => self.open_mix(None, cx),
- ActionId::ToggleSnap => self.toggle_snap(cx),
- ActionId::ToggleSubtitles => self.toggle_subtitles(cx),
- // The keyboard's door to the same list the toolbar button opens.
- // At the window's corner, since a stroke names no place -- and
- // [`menu_at`] keeps it on screen from there.
- ActionId::Theme => self.open_picker(Pick::Theme, Point::default(), cx),
- // Nothing to cancel while nothing is exporting; the export guard in
- // the key handler is what answers this one while there is.
- ActionId::CancelExport => {}
- ActionId::ShowActions => self.show_actions(cx),
- }
- }
-
- /// Says something to the user. The one door: every message in this editor
- /// comes through here, so "queued rather than overwritten" is a property of
- /// the field and not of seventy call sites remembering to be polite.
- ///
- /// A repeat of what is already at the back is dropped -- holding a key that
- /// refuses would otherwise fill the queue with one sentence, and the count
- /// on the bar would be a count of how long the key was held.
- fn notify_user(&mut self, message: SharedString) {
- push_notice(&mut self.notices, message);
- }
-
- /// Answers the message on the bar and brings up the next one. Whether there
- /// was one to answer, because a key that dismissed a notice owes a repaint
- /// and a key that dismissed nothing does not.
- fn dismiss_notice(&mut self) -> bool {
- self.notices.pop_front().is_some()
- }
-
- /// The magnet off and on again, in words: a snap that stops working
- /// silently reads as a bug, and one that starts working silently reads as
- /// one too. The line goes with it -- nothing is being promised any more.
- fn toggle_snap(&mut self, cx: &mut Context) {
- self.snap = !self.snap;
- self.snap_cue = None;
- self.ghost = None;
- self.notify_user(match self.snap {
- true => "SNAP ON — drags land on clip edges, the playhead and the start".into(),
- false => "SNAP OFF — drags land exactly where the hand leaves them".into(),
- });
- cx.notify();
- }
-
- /// The actions card, from its key, from the panel button, or from its own
- /// row: open, with an empty search box -- a card that opens showing the
- /// last search would hide most of the list for a reason nobody remembers.
- fn show_actions(&mut self, cx: &mut Context) {
- self.keys_open = true;
- self.keys_search.clear();
- self.scroll_keys(None);
- self.rebinding = None;
- // One card at a time, the rule the other cards follow.
- self.export_open = false;
- cx.notify();
- }
-
- /// Moves the actions card's row list by `by` pixels, or puts it back at the
- /// top (`None`). Back to the top after every keystroke that changes the
- /// search: a filtered list is shorter than the offset a scrolled one left
- /// behind, and a card showing the empty space past its last row reads as a
- /// search that found nothing.
- ///
- /// Clamped to what there is to scroll, so the list cannot be pushed off
- /// either end -- `max_offset` is what the last paint measured, which is the
- /// only place that number exists.
- fn scroll_keys(&self, by: Option) {
- let at = match by {
- Some(by) => (f32::from(self.keys_scroll.offset().y) + by)
- .clamp(-f32::from(self.keys_scroll.max_offset().height), 0.),
- None => 0.,
- };
- self.keys_scroll.set_offset(point(px(0.), px(at)));
- }
-
- /// The cues over the picture, off and on. Says which it is now *and* what is
- /// on screen while they are on: a toggle whose answer is invisible when the
- /// playhead happens to sit between two cues would read as broken.
- fn toggle_subtitles(&mut self, cx: &mut Context) {
- self.subs_on = !self.subs_on;
- // Named with its film here too: a notice saying "SUBTITLES ON — eng"
- // over a timeline holding two films' eng tracks names neither.
- let label = self
- .session
- .as_ref()
- .and_then(|session| sub_pick_name(session.subtitles(), self.sub_track))
- .unwrap_or_else(|| "nothing imported".to_string());
- self.notify_user(
- match self.subs_on {
- true => format!("SUBTITLES ON — {label}"),
- false => format!("SUBTITLES OFF — {label} is still on the timeline"),
- }
- .into(),
- );
- cx.notify();
- }
-
- /// The subtitle track the overlay and the strip are showing: the one a
- /// library row picked, or the first there is. `None` with no timeline and
- /// for an index left over from one that is gone.
- fn subtitle_track(&self) -> Option<&engine::subtitle::SubtitleTrack> {
- self.session.as_ref()?.subtitles().get(self.sub_track)
- }
-
- /// Whether the editor can be asked for `action` right now, and why not when
- /// it cannot. `on` is the clip the question is about -- the one a clip menu
- /// was opened on -- and `None` asks about the marked clip instead, which is
- /// what a menu that hangs over no clip in particular means by "this one".
- ///
- /// The player's half of [`enable`]: it reads the state, the table decides.
- fn enable(&self, action: ActionId, on: Option<(Lane, usize)>) -> Enable {
- enable(action, self.ctx(on))
- }
-
- /// The state every one of those questions is asked against, read off the
- /// player once: [`menu_items`] filters a whole menu with it, so the rows a
- /// menu draws and the answers it dims them by come from the same reading.
- fn ctx(&self, on: Option<(Lane, usize)>) -> Ctx {
- let Some(session) = &self.session else {
- return Ctx::default();
- };
- let clip = on
- .or(self.selected)
- .and_then(|(lane, idx)| session.lane_clips(lane).get(idx).map(|clip| (*clip, lane)));
- Ctx {
- clip,
- image: clip.is_some_and(|(clip, _)| {
- session
- .sources()
- .get(clip.source)
- .is_some_and(|s| engine::is_image(&s.path))
- }),
- playhead: frame_at(session.now(), self.fps),
- timeline: true,
- clipboard: self.clipboard.is_some(),
- subtitles: !session.subtitles().is_empty(),
- playable: !nothing_to_play(Some(session)),
- exporting: self.exporting().is_some(),
- }
- }
-
- /// The same reading for a library row: whether this file can join this
- /// timeline -- the very answer the list greys the row by, so the menu over a
- /// row and the row under it cannot disagree -- and how many clips play it.
- /// [`Player::ctx`] for the other panel.
- fn row_ctx(&self, path: &Path, stream: usize) -> RowCtx {
- let placed = self.session.as_ref().map_or(0, |session| {
- let of_row = session
- .sources()
- .iter()
- .position(|s| s.path == path && s.audio_stream == stream);
- of_row.map_or(0, |idx| {
- session
- .lanes()
- .into_iter()
- .flat_map(|lane| session.lane_clips(lane))
- .filter(|c| c.source == idx)
- .count()
- })
- });
- let sources = self
- .session
- .as_ref()
- .map_or(&[][..], PlaybackSession::sources);
- RowCtx {
- timeline: self.session.is_some(),
- exporting: self.exporting().is_some(),
- usable: library_rows(
- sources,
- &self.streams,
- &self.decoders,
- self.timeline_audio(),
- |path| {
- self.session
- .as_ref()
- .map_or(0, |session| session.file_frames(path))
- },
- )
- .iter()
- .any(|row| row.path == path && row.stream == stream && row.unusable.is_none()),
- placed,
- }
- }
-
- /// The one place a clip becomes *the* selected one: a click, a right-click
- /// that opens the menu, and every selection key go through here, so what a
- /// keyboard marks and what a pointer marks are the same state marked the
- /// same way (group and all -- see [`marked`]).
- fn select(&mut self, target: (Lane, usize), cx: &mut Context) {
- self.selected = Some(target);
- cx.notify();
- }
-
- /// Every clip the playhead is over, one per lane, in the order the lanes are
- /// drawn -- video first, which is the order [`PlaybackSession::lanes`] comes
- /// in. What the select key walks.
- fn under_playhead(&self) -> Vec<(Lane, usize)> {
- let Some(session) = &self.session else {
- return Vec::new();
- };
- let now = session.now();
- session
- .lanes()
- .into_iter()
- .filter_map(|lane| Some((lane, session.lane_clip_at(lane, now)?)))
- .collect()
- }
-
- /// Selects the clip under the playhead, and on a repeat press the next
- /// lane's -- so one key reaches every clip the playhead is over, which is
- /// what makes selection (and everything that acts on a selection: delete,
- /// lift, the equalizer, the grade) reachable with no pointer at all.
- fn select_under_playhead(&mut self, cx: &mut Context) {
- let under = self.under_playhead();
- let Some(&first) = under.first() else {
- self.notify_user("NOTHING UNDER THE PLAYHEAD — move it onto a clip first".into());
- cx.notify();
- return;
- };
- // Where the current selection sits in that walk decides what "again"
- // means; a selection off the playhead starts the walk over.
- let next = self
- .selected
- .and_then(|sel| under.iter().position(|&clip| clip == sel))
- .map_or(first, |at| under[(at + 1) % under.len()]);
- self.select(next, cx);
- }
-
- /// Walks the selection along its own lane, wrapping at either end. Nothing
- /// selected means nothing to walk from, so it selects under the playhead
- /// exactly as the select key does: either key can start as well as continue.
- fn select_step(&mut self, forward: bool, cx: &mut Context) {
- let clips = self
- .selected
- .zip(self.session.as_ref())
- .map_or(0, |((lane, _), session)| session.lane_clips(lane).len());
- match (self.selected, clips) {
- // An empty lane is a selection nothing can be stepped from -- as is
- // no selection at all, and the playhead answers both.
- (Some((lane, idx)), len) if len > 0 => {
- let next = if forward {
- (idx + 1) % len
- } else {
- (idx + len - 1) % len
- };
- self.select((lane, next), cx);
- }
- _ => self.select_under_playhead(cx),
- }
- }
-
- /// Cycles the fit policy of the clip the picture is coming from -- the
- /// clicked one when it is a video clip, else the composite's own, exactly as
- /// the colour card picks its target. A whole card for one four-valued
- /// setting would be a card to close; a stroke that cycles it and says what
- /// it landed on is the same setting with nothing to dismiss.
- ///
- /// Only means anything when the clip is not the project's size -- a clip
- /// that already fills the canvas looks the same under all four -- so the
- /// notice says the size it is placing, not just the word.
- fn cycle_fit(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let Some(session) = &mut self.session else {
- self.notify_user("no timeline to fit — open a file first".into());
- cx.notify();
- return;
- };
- let target = self
- .selected
- .filter(|(lane, _)| lane.kind == LaneKind::Video)
- .or_else(|| session.video_clip_at(session.now()));
- let Some((lane, idx)) = target else {
- self.notify_user("no clip under the playhead to fit".into());
- cx.notify();
- return;
- };
- let next = next_fit(session.fit_of(lane, idx));
- self.apply_fit(lane, idx, next, cx);
- }
-
- /// One clip's fit policy set, whichever asked: the stroke that steps to the
- /// next one and the list that names one outright come through here, so they
- /// cannot differ in what they do or in what they say they did.
- fn apply_fit(&mut self, lane: Lane, idx: usize, fit: FitPolicy, cx: &mut Context) {
- if let Some(session) = &mut self.session
- && session.set_fit(lane, idx, fit)
- {
- let (w, h) = session.resolution();
- self.notify_user(format!("FIT POLICY: {} on {w}x{h}", fit_label(fit)).into());
- self.reset_after_reseek();
- }
- cx.notify();
- }
-
- /// The scale against the bed it is drawn on and the timeline it is drawn
- /// from: every clamp, zoom and scroll is worked out through this, and the
- /// bed is measured off the ruler's probe rather than remembered, so a
- /// resized window is a resized view on the very next answer.
- fn view(&self) -> View {
- View {
- scale: self.scale,
- bed: f32::from(self.ruler.get().size.width),
- duration: self.drawn_duration(),
- fps: self.fps,
- }
- }
-
- /// Magnifies the timeline about a point that stays put: `anchor` is how many
- /// pixels along the bed to hold still (a ctrl+wheel holds the pointer), and
- /// with none it is the playhead -- so the frame being worked on is still the
- /// frame on screen after the zoom. Clamped at both ends by [`View`]: out
- /// stops at the whole timeline on the bed, in at a handful of frames.
- fn zoom(&mut self, factor: f32, anchor: Option, cx: &mut Context) {
- let view = self.view();
- let at = self.playhead(view.duration);
- let anchor = anchor.unwrap_or_else(|| self.scale.px_at(at).clamp(0., view.bed));
- self.scale = view.zoomed(factor, anchor);
- // The view a hand chose. A zoom about the playhead leaves it on the
- // bed, so this is given back on the very next frame and only a zoom
- // that took the head off screen -- ctrl+wheel away from it -- holds.
- self.panned = true;
- cx.notify();
- }
-
- /// All the way back out: the whole timeline across the bed, and the one
- /// thing that reads the timeline's own length to decide how wide a second
- /// is drawn.
- fn zoom_fit(&mut self, cx: &mut Context) {
- self.scale = self.view().fit();
- cx.notify();
- }
-
- /// Slides the view along the timeline by `notches` of the wheel, later in
- /// time for a positive one and [`SCROLL_NOTCH_SHARE`] of the bed each. The
- /// scale is untouched: this is the timeline's scrollbar, and the only thing
- /// on the panel that moves what is on screen without magnifying it.
- fn scroll_view(&mut self, notches: f32, cx: &mut Context) {
- let view = self.view();
- // Nothing painted yet: there is no bed to measure a notch against, and
- // a start moved against a zero width would be a jump to the head.
- if view.bed <= 0. {
- return;
- }
- self.scale = view.scrolled(notches * view.bed * SCROLL_NOTCH_SHARE);
- // The one gesture whose whole purpose is to look away from the
- // playhead: while playing it wins over the follow, which is what every
- // editor does with a scroll during playback.
- self.panned = true;
- cx.notify();
- }
-
- /// One notch of the wheel anywhere over the timeline -- the ruler or a
- /// lane's bed alike, since a hand aims at the clip it is working on and not
- /// at the strip above it. Ctrl zooms about the pointer, bare scrolls the
- /// view along: the mapping Premiere, Movavi and CapCut share, and the one
- /// the user named.
- ///
- /// The anchor is measured off the ruler's probe wherever the pointer is,
- /// because that probe *is* the bed's x-to-time mapping ([`HEADER_W`]) and
- /// every lane is drawn through the same one.
- fn timeline_wheel(&mut self, event: &ScrollWheelEvent, cx: &mut Context) {
- let d = wheel_delta(event);
- if d == 0. {
- return;
- }
- let factor = match d > 0. {
- true => ZOOM_STEP,
- false => 1. / ZOOM_STEP,
- };
- match event.modifiers.control {
- true => {
- let anchor = px_along(event.position.x, self.ruler.get());
- self.zoom(factor, Some(anchor), cx);
- }
- // Up is back towards the head of the timeline, the way a wheel up
- // is back towards the top of a page.
- false => self.scroll_view(-d.signum(), cx),
- }
- }
-
- /// Cycles the *project's* resolution through [`RESOLUTIONS`], starting from
- /// the media's own -- the one size that must stay reachable, since a project
- /// moved off it has no other way back (the resolution is not an undo step).
- /// Every clip is recomposed onto it, so this is what makes "the project
- /// resolution and the media's are different things" a thing a user can see.
- fn cycle_resolution(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let Some(session) = &mut self.session else {
- self.notify_user("no timeline to resize — open a file first".into());
- cx.notify();
- return;
- };
- let (width, height) = next_resolution(session.resolution(), session.native_resolution());
- self.apply_resolution(width, height, cx);
- }
-
- /// The project resized, whichever asked: the stroke that steps to the next
- /// size and the list that names one outright come through here.
- fn apply_resolution(&mut self, width: u32, height: u32, cx: &mut Context) {
- if let Some(session) = &mut self.session
- && session.set_resolution(width, height)
- {
- self.notify_user(format!("PROJECT: {width}x{height}").into());
- self.reset_after_reseek();
- }
- cx.notify();
- }
-
- /// The project cut at another rate: the list names one and this is where it
- /// happens, the way [`apply_resolution`](Self::apply_resolution) is for a
- /// size. The whole timeline is conformed to it by the engine
- /// ([`PlaybackSession::set_frame_rate`]) -- same seconds, same footage --
- /// and the rate the app itself counts frames in follows, since every
- /// timecode, ruler mark and step key here is measured in it.
- fn apply_frame_rate(&mut self, fps: f64, cx: &mut Context) {
- if let Some(session) = &mut self.session
- && session.set_frame_rate(fps)
- {
- self.fps = session.meta().frame_rate;
- self.notify_user(format!("PROJECT: {} fps", fps_label(fps)).into());
- self.reset_after_reseek();
- }
- cx.notify();
- }
-
- /// The project's HDR media shown another way: the list names a rendition and
- /// this is where it happens, the way [`apply_resolution`](Self::apply_resolution)
- /// is for a size. The engine remaps the frame under the playhead at once
- /// ([`PlaybackSession::set_tone`]), so the picture on screen is the picked
- /// one before the notice has faded -- and an SDR project is unmoved, which
- /// is what the notice says rather than pretending something happened.
- fn apply_tone(&mut self, preset: Preset, cx: &mut Context) {
- if let Some(session) = &mut self.session
- && session.set_tone(preset)
- {
- self.notify_user(format!("HDR: {} — affects HDR media", tone_label(preset)).into());
- self.reset_after_reseek();
- }
- cx.notify();
- }
-
- /// Opens a choice list on a setting, where it was asked for. One floating
- /// thing at a time: the click that opens it is the click that closes
- /// whatever menu it was opened from.
- fn open_picker(&mut self, of: Pick, at: Point, cx: &mut Context) {
- // On the row that is in force, so the first ↑ or ↓ steps off the
- // current value rather than off the top of the list.
- let sel = self
- .choices(of)
- .iter()
- .position(|(.., picked)| *picked)
- .unwrap_or(0);
- self.context_menu = None;
- self.library_menu = None;
- self.picker = Some(Picker { of, at, sel });
- cx.notify();
- }
-
- /// A row of the open list was picked. Closes the list first -- the rule
- /// every menu item here follows -- then does exactly what the stroke for
- /// that setting does, through the same door.
- fn choose(&mut self, choice: Choice, cx: &mut Context) {
- self.picker = None;
- match choice {
- Choice::Size(w, h) => self.apply_resolution(w, h, cx),
- Choice::Fps(fps) => self.apply_frame_rate(fps, cx),
- Choice::Fit(lane, idx, fit) => self.apply_fit(lane, idx, fit, cx),
- Choice::Tone(preset) => self.apply_tone(preset, cx),
- // In force for the next paint -- every token is read through
- // [`ui::theme::palette`], so one store repaints the whole window --
- // and kept for the next launch. A file that could not be written is
- // said out loud: the difference between "picked" and "picked for
- // good" is the user's to know.
- Choice::Theme(id) => {
- ui::theme::set(id);
- if let Err(e) = ui::theme::save(id) {
- let path = ui::theme::config_path();
- self.notify_user(
- format!("THEME COULD NOT BE KEPT — {} — {e}", path.display()).into(),
- );
- }
- cx.notify();
- }
- // The same field the row's key steps, set outright: a list picks a
- // value, it does not step to one.
- Choice::AudioRate(kbps) => {
- self.audio_kbps = kbps;
- cx.notify();
- }
- }
- }
-
- /// Every value the open list offers, in the order it lists them. Empty
- /// without a timeline, which is the state where nothing here has a value to
- /// offer -- and where the surfaces that open the list are dimmed anyway.
- fn choices(&self, of: Pick) -> Vec {
- // The palette is not the project's, so it is offered before the
- // timeline is asked about: an empty window is painted too, and its
- // Theme button is live there like the snap beside it.
- if of == Pick::Theme {
- return ui::theme::PaletteId::ALL
- .into_iter()
- .map(|id| {
- (
- Choice::Theme(id),
- id.label().into(),
- id.detail().into(),
- id == ui::theme::active(),
- )
- })
- .collect();
- }
- let Some(session) = &self.session else {
- return Vec::new();
- };
- match of {
- Pick::Resolution => {
- resolution_choices(session.resolution(), session.native_resolution())
- }
- Pick::Fps => fps_choices(session.meta().frame_rate, session.native_frame_rate()),
- Pick::Fit(lane, idx) => {
- fit_choices(lane, idx, session.fit_of(lane, idx), session.resolution())
- }
- Pick::AudioRate => audio_rate_choices(self.audio_kbps),
- Pick::Tone => tone_choices(session.tone()),
- // Answered above, with or without a timeline.
- Pick::Theme => Vec::new(),
- }
- }
-
- /// Opens the colour card on the clip a grade would go on: the clip that was
- /// clicked when it is a video one, and otherwise the clip the picture is
- /// coming from -- the one the engine's own compositing rule picks, which is
- /// what a person means by "this shot". The fallback stands even now that a
- /// selection key exists: a grade asked for with nothing selected still means
- /// the shot on screen.
- fn open_color(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let Some(session) = &self.session else {
- self.notify_user("no timeline to grade — open a file first".into());
- cx.notify();
- return;
- };
- let target = self
- .selected
- .filter(|(lane, _)| lane.kind == LaneKind::Video)
- .or_else(|| session.video_clip_at(session.now()));
- match target {
- Some(clip) => {
- self.color_open = Some(clip);
- self.color_band = 0;
- self.color_dragging = false;
- // A sample the last card held back belongs to the clip it was
- // dragged on, and this may be another one.
- self.pending_color = None;
- // One card at a time, the rule both the others already follow.
- self.keys_open = false;
- self.export_open = false;
- self.context_menu = None;
- }
- None => self.notify_user("no clip under the playhead to grade".into()),
- }
- cx.notify();
- }
-
- /// What the card's clip is graded by right now -- the identity for one
- /// nobody has graded, which is what the sliders start at. A sample a drag is
- /// still holding wins over the clip's own: it is what the hand has asked
- /// for, so it is what the sliders show and what the next sample builds on.
- fn color_params(&self) -> ColorParams {
- if let Some(params) = self.pending_color {
- return params;
- }
- self.color_open
- .zip(self.session.as_ref())
- .and_then(|((lane, idx), session)| session.color_of(lane, idx).copied())
- .unwrap_or_default()
- }
-
- /// Puts `params` on the card's clip, or takes the grade off when they are
- /// the identity -- a slider walked back to the middle leaves the clip
- /// ungraded rather than carrying a do-nothing entry, which is what keeps an
- /// untouched project byte-identical. The engine reseeks on the edit, so the
- /// frame on screen repaints through the new grade; this only owes the flags
- /// that reseek clears.
- fn set_color(&mut self, params: ColorParams, cx: &mut Context) {
- self.write_color(params, false, cx);
- }
-
- /// Both writes: `live` is the one that takes no undo step, which is what
- /// every sample *inside* a drag goes through
- /// (`PlaybackSession::set_color_live`). Either way the engine reseeks, so
- /// the picture -- and the histogram counted off it -- is regraded at once.
- fn write_color(&mut self, params: ColorParams, live: bool, cx: &mut Context) {
- // Any write supersedes a held sample, whichever way it arrived -- a key,
- // a reset, or the flush that took this one out of the stash.
- self.pending_color = None;
- let Some((lane, idx)) = self.color_open else {
- return;
- };
- let Some(session) = &mut self.session else {
- return;
- };
- let grade = Some(params).filter(|p| !p.is_identity());
- let took = match live {
- true => session.set_color_live(lane, idx, grade),
- false => session.set_color(lane, idx, grade),
- };
- if took {
- self.reset_after_reseek();
- }
- cx.notify();
- }
-
- /// Moves the picked slider by `steps` of [`COLOR_STEP`], clamped to that
- /// band's range. One edit, so one undo step per press.
- fn nudge_color(&mut self, steps: f32, cx: &mut Context) {
- let mut params = self.color_params();
- let (_, low, high) = COLOR_BANDS[self.color_band];
- let value = band_mut(&mut params, self.color_band);
- *value = (*value + steps * COLOR_STEP).clamp(low, high);
- self.set_color(params, cx);
- }
-
- /// Where the pointer sits along a slider, as that band's value: the left end
- /// of the bar is the bottom of its range and the right end the top. Called
- /// on every pointer sample, so the grade -- and the picture, and the
- /// histogram over it -- moves under the hand.
- ///
- /// `first` is the press: it takes the undo step the whole gesture rolls back
- /// to, and every sample after it is live. That is why it writes even when
- /// the value did not change -- without that snapshot the rest of the drag
- /// would be unundoable.
- ///
- /// Values land on the [`COLOR_STEP`] grid the keys use, which also bounds
- /// one drag to forty-odd entries in the project's colour table.
- ///
- /// Samples crossed while the worker still owes a frame are held rather than
- /// written ([`stash_or_write`]): a reopen costs half a second on a big film,
- /// so a bar-wide sweep that wrote every step would queue forty opens, cancel
- /// thirty-nine of them and freeze the window for the sum. What is written is
- /// one grade per frame the worker actually delivers.
- fn drag_color(&mut self, x: Pixels, first: bool, cx: &mut Context) {
- let (_, low, high) = COLOR_BANDS[self.color_band];
- let along = frac_along(x, self.color_bars[self.color_band].get());
- let value = color_snap(low + along * (high - low)).clamp(low, high);
- let mut params = self.color_params();
- let at = band_mut(&mut params, self.color_band);
- if *at == value && !first {
- return;
- }
- *at = value;
- let busy = self.seek_since.is_some();
- match stash_or_write(&mut self.pending_color, params, first, busy) {
- Some(params) => self.write_color(params, !first, cx),
- // The sliders draw off the held sample, so the handle goes on
- // following the hand while the picture catches up.
- None => cx.notify(),
- }
- }
-
- /// Opens the speed card on the clip whose rate is to change: the selected
- /// one, or -- with nothing selected -- the clip the picture is coming from,
- /// which is what a person means by "this shot". Either half of a take will
- /// do: a rate applies to the whole group, so opening it on the sound and
- /// opening it on the picture are the same card.
- fn open_speed(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let Some(session) = &self.session else {
- self.notify_user("no timeline to re-time — open a file first".into());
- cx.notify();
- return;
- };
- match self
- .selected
- .or_else(|| session.video_clip_at(session.now()))
- {
- Some(clip) => {
- self.speed_open = Some(clip);
- self.speed_dragging = false;
- // The colour card's rule: a held sample is the last clip's.
- self.pending_speed = None;
- // One card at a time, the rule the other four follow.
- self.keys_open = false;
- self.export_open = false;
- self.eq_open = None;
- self.color_open = None;
- self.close_silence();
- self.context_menu = None;
- }
- None => self.notify_user("no clip under the playhead to re-time".into()),
- }
- cx.notify();
- }
-
- /// What the card's clip plays at right now -- real time for one nobody has
- /// touched, which is where the bar starts.
- fn card_speed(&self) -> Speed {
- if let Some(speed) = self.pending_speed {
- return speed;
- }
- self.speed_open
- .zip(self.session.as_ref())
- .map_or(Speed::NORMAL, |((lane, idx), session)| {
- session.speed_of(lane, idx)
- })
- }
-
- /// Writes a rate at the card's clip and its whole group -- one undo step for
- /// the lot ([`engine::PlaybackSession::set_speed`]). The engine reseeks, so
- /// the picture runs at the new rate and the sound is resampled from the next
- /// chunk on; a refusal (a slower clip would run into its neighbour) comes
- /// back in the engine's own words and *names* the clip in the way, because
- /// "it did not fit" is not something a person can go and fix.
- fn set_speed(&mut self, speed: Speed, cx: &mut Context) {
- self.write_speed(speed, false, cx);
- }
-
- /// Both writes: `live` is the one that takes no undo step, which is what
- /// every sample *inside* a drag goes through -- so a drag from 1.00x to
- /// 2.00x is one undo press and lands back where the hand picked it up, and
- /// the whole linked group comes back with it.
- fn write_speed(&mut self, speed: Speed, live: bool, cx: &mut Context) {
- // The colour card's rule: a write supersedes whatever a drag was holding.
- self.pending_speed = None;
- let Some((lane, idx)) = self.speed_open else {
- return;
- };
- let Some(session) = &mut self.session else {
- return;
- };
- if speed != session.speed_of(lane, idx) {
- let wrote = match live {
- true => session.set_speed_live(lane, idx, speed),
- false => session.set_speed(lane, idx, speed),
- };
- match wrote {
- Ok(()) => self.reset_after_reseek(),
- Err(e) => self.notify_user(e.to_string().into()),
- }
- }
- cx.notify();
- }
-
- /// One [`SPEED_STEP`] per keystroke, clamped to what a [`Speed`] can hold.
- fn nudge_speed(&mut self, steps: i32, cx: &mut Context) {
- let at = i32::from(self.card_speed().permille()) + steps * SPEED_STEP;
- self.set_speed(speed_at(at), cx);
- }
-
- /// Where the pointer sits along the bar, as a rate: the left end is
- /// [`Speed::MIN`] and the right end [`Speed::MAX`], on the same
- /// [`SPEED_STEP`] grid the keys move on -- so a drag can land on exactly
- /// 1.00x and the same drag twice is one entry, not forty.
- /// `first` is the press: it takes the undo step the whole gesture rolls back
- /// to, and every sample after it is live -- the colour card's rule, for the
- /// colour card's reason.
- fn drag_speed(&mut self, x: Pixels, first: bool, cx: &mut Context) {
- let along = frac_along(x, self.speed_bar.get());
- let lo = f32::from(Speed::MIN.permille());
- let hi = f32::from(Speed::MAX.permille());
- let raw = lo + along * (hi - lo);
- // Snapped to the grid, then to real time itself when it is within half a
- // step of it: 1.00x is the one value a hand must be able to hit, and
- // nothing about the bar's geometry guarantees a pixel lands on it.
- let stepped = (raw / SPEED_STEP as f32).round() as i32 * SPEED_STEP;
- // Held back while the worker is busy, the colour card's way and for a
- // sharper reason: a live rate also restarts the sound, so a sweep that
- // wrote every step would restart it forty times.
- let busy = self.seek_since.is_some();
- match stash_or_write(&mut self.pending_speed, speed_at(stepped), first, busy) {
- Some(speed) => self.write_speed(speed, !first, cx),
- None => cx.notify(),
- }
- }
-
- /// Writes what a slider drag held back, now that the worker has delivered.
- /// The gate is the frame that landed and never a timer: a 100 ms tick
- /// ([`SCRUB_GAP`]) says nothing about a reopen that costs half a second, and
- /// a drag gated on one would still queue opens nobody sees.
- ///
- /// Called again by the release, where readiness is beside the point: the
- /// value the hand let go on is owed whatever the worker is doing, and a
- /// gesture may not end on a sample that was dropped.
- fn flush_drag(&mut self, cx: &mut Context) {
- if let Some(params) = self.pending_color.take() {
- self.write_color(params, true, cx);
- }
- if let Some(speed) = self.pending_speed.take() {
- self.write_speed(speed, true, cx);
- }
- }
-
- /// Opens the silence card on the clip to be scanned: the selected one, or
- /// -- with nothing selected -- the clip the picture is coming from, which is
- /// the rule the speed card follows and what a person means by "this shot".
- /// Either half of a take will do: both halves of an A/V take name the same
- /// file and play the same source frames, which is the whole of what a scan
- /// is of ([`ScanKey`]).
- ///
- /// The card is up on the next frame whatever the file is: a still is
- /// refused by name here, where the answer costs a look at the path, and
- /// everything the decoder has to open the file to know -- a track that is
- /// not there, a read that fails -- is refused the same way when the scan
- /// lands, because a fifty-second decode is not a thing to open a card
- /// behind ([`Player::start_silence_scan`]).
- fn open_silence(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let Some(session) = &self.session else {
- self.notify_user("no timeline to scan — open a file first".into());
- cx.notify();
- return;
- };
- match self
- .selected
- .or_else(|| session.video_clip_at(session.now()))
- .map(|clip| audio_half(session, clip))
- {
- Some((lane, idx)) => {
- let found = self.session.as_ref().and_then(|session| {
- let clip = *session.lane_clips(lane).get(idx)?;
- Some((session.sources().get(clip.source)?.clone(), clip))
- });
- // A still is asked *before* the decoder is: handing a png to the
- // mp4 demuxer answers "a box with a larger size than it", which
- // is a true sentence about a container and nothing a person can
- // act on. A picture has no sound for the same reason a silent
- // video has none, so it is refused in the same words.
- let Some((source, clip)) = found else {
- cx.notify();
- return;
- };
- if engine::is_image(&source.path) {
- self.notify_user(unscannable(lane, idx, &source.path).into());
- cx.notify();
- return;
- }
- self.silence_open = Some((lane, idx));
- self.silence_field = 0;
- // One card at a time, the rule the other four follow.
- self.keys_open = false;
- self.export_open = false;
- self.eq_open = None;
- self.color_open = None;
- self.speed_open = None;
- self.context_menu = None;
- // The clip's own range, not the file's: the scan reads what this
- // clip plays and nothing else, so a take cut in half costs half
- // the decode and finds only what is still on the timeline.
- let key = (
- source.path.clone(),
- source.audio_stream,
- clip.in_frame,
- clip.out_frame,
- );
- match scan_plan(
- self.silence_levels.contains_key(&key),
- self.silence_scan.as_ref().map(|scan| &scan.key),
- &key,
- ) {
- ScanPlan::Marks => self.scan_silences(),
- ScanPlan::Start => self.start_silence_scan(key, cx),
- ScanPlan::Wait => {}
- }
- }
- None => self.notify_user("no clip under the playhead to scan".into()),
- }
- cx.notify();
- }
-
- /// Opens the mix card. `lane` is the row it lands on -- the track whose
- /// header was clicked -- and `None` starts at the top, which is what the
- /// stroke means.
- ///
- /// Nothing here is a clip's, so nothing is refused for want of a selection:
- /// a timeline with no audio track at all still has a limiter to set, and a
- /// fader on an empty track is the level the next take lands at.
- fn open_mix(&mut self, lane: Option, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- self.mix_open = true;
- self.mix_field = lane
- .and_then(|lane| self.mix_lanes().iter().position(|&l| l == lane))
- .unwrap_or(0);
- // One card at a time, the rule the other five follow.
- self.keys_open = false;
- self.export_open = false;
- self.eq_open = None;
- self.color_open = None;
- self.speed_open = None;
- self.close_silence();
- self.context_menu = None;
- cx.notify();
- }
-
- /// The audio tracks the card shows a fader for, top to bottom: *every* one
- /// of them, empty ones included -- what the timeline lays out, not what the
- /// mixer happens to open (`Project::audio_lanes` leaves an empty track out,
- /// and a fader that disappeared when a track was cleared would be a setting
- /// nobody could reach).
- fn mix_lanes(&self) -> Vec {
- self.session.as_ref().map_or_else(Vec::new, |session| {
- session
- .lanes()
- .into_iter()
- .filter(|l| l.kind == LaneKind::Audio)
- .collect()
- })
- }
-
- /// Moves the row the card has picked: a fader by [`MIX_DB_STEP`], the
- /// ceiling by the same, and the switch either way (a ring of two, like the
- /// silence card's unit row).
- ///
- /// Every one of them goes through the session, which hands it straight to
- /// the running mixer: what the ear hears while the arrow is held is the mix
- /// that is being set, and nothing is rebuilt to make that true -- no reseek,
- /// so no `reset_after_reseek` and no blink in the picture behind the card
- /// ([`engine::PlaybackSession::set_lane_gain_db`]).
- fn nudge_mix(&mut self, steps: i32, cx: &mut Context) {
- let lanes = self.mix_lanes();
- let Some(session) = self.session.as_mut() else {
- return;
- };
- match lanes.get(self.mix_field) {
- Some(&lane) => {
- let at = session.lane_gain_db(lane) + steps as f32 * MIX_DB_STEP;
- session.set_lane_gain_db(lane, at);
- }
- None => {
- let limiter = session.limiter();
- let at = match self.mix_field - lanes.len() {
- 0 => Limiter {
- on: limiter.on,
- ..limiter
- }
- .with_ceiling(limiter.ceiling_db + steps as f32 * MIX_DB_STEP),
- _ => Limiter {
- on: !limiter.on,
- ..limiter
- },
- };
- session.set_limiter(at);
- }
- }
- cx.notify();
- }
-
- /// Closes it and drops the preview with it: marks left on the lane after
- /// the card is gone would name frames the next edit has already moved.
- fn close_silence(&mut self) {
- self.silence_open = None;
- self.silence_marks.clear();
- self.cancel_silence_scan();
- }
-
- /// Tells the worker nobody is waiting any more. It gives up at its next
- /// chunk and the levels it had are dropped: half a track is not an answer,
- /// and the flag stays set on the [`Arc`] the landing closure holds, which is
- /// how that closure knows to keep its hands off the card.
- fn cancel_silence_scan(&mut self) {
- if let Some(scan) = self.silence_scan.take() {
- scan.progress
- .cancel
- .store(true, std::sync::atomic::Ordering::Relaxed);
- }
- }
-
- /// Hands the decode to a worker and returns at once -- the card is drawn by
- /// the very next frame, saying it is scanning. Fifty-one seconds on a 25 GB
- /// film is what this used to cost on the render thread, with the card
- /// marked open and nothing on screen.
- ///
- /// Whatever was scanning is cancelled first: one card, one scan, and the
- /// clip that has just been asked about is the one worth the disk.
- ///
- /// Only the clip's own `[in, out)` is read -- source frames over the
- /// project's rate, the same seconds [`engine::Project`] hands the decoder
- /// for playback -- so half a take is half a wait.
- fn start_silence_scan(&mut self, key: ScanKey, cx: &mut Context) {
- self.cancel_silence_scan();
- self.silence_marks.clear();
- let progress = Arc::new(engine::silence::Progress::default());
- let range = source_secs(&key, self.fps);
- let scan = cx.background_executor().spawn({
- let (key, progress) = (key.clone(), Arc::clone(&progress));
- async move { engine::silence::levels_with_progress(&key.0, key.1, range, &progress) }
- });
- let now = Instant::now();
- self.silence_scan = Some(SilenceScan {
- key: key.clone(),
- started: now,
- progress: Arc::clone(&progress),
- seen: 0,
- since: now,
- });
- cx.spawn(async move |this, cx| {
- let landed = scan.await;
- this.update(cx, |this, cx| {
- // Cancelled means the card moved on or closed: the levels are a
- // prefix of a track nobody asked about any more.
- if progress.cancel.load(std::sync::atomic::Ordering::Relaxed) {
- return;
- }
- this.silence_scan = None;
- match landed {
- Ok(Some(levels)) => {
- this.silence_levels.insert(key.clone(), Arc::new(levels));
- this.scan_silences();
- }
- // A source with no audio track is not one long silence: it
- // is a clip this card has nothing to say about, named so the
- // user knows which one it meant.
- Ok(None) => {
- if let Some((lane, idx)) = this.silence_open {
- this.notify_user(unscannable(lane, idx, &key.0).into());
- }
- this.close_silence();
- }
- Err(e) => {
- this.close_silence();
- this.notify_user(format!("SCAN FAILED: {e}").into());
- }
- }
- cx.notify();
- })
- .ok();
- })
- .detach();
- cx.notify();
- }
-
- /// Keeps the scanning line's stall clock, for [`Player::poll_import`]'s
- /// reason: sampled once per frame rather than while drawing.
- fn poll_silence(&mut self) {
- if let Some(scan) = &mut self.silence_scan {
- scan.poll();
- }
- }
-
- /// Applies the settings to levels already in hand and replaces the preview
- /// -- never stacks on it. Arithmetic only: the decode is
- /// [`Player::start_silence_scan`]'s and happens once per source, so every
- /// run here is numbers already read, which is what makes moving a threshold
- /// feel like moving a slider. A source still being scanned has no marks yet
- /// and says so on the card.
- ///
- /// Changes nothing about the project: a preview is not an edit, and no undo
- /// step is spent until a button is pressed.
- fn scan_silences(&mut self) {
- let Some((lane, idx)) = self.silence_open else {
- return;
- };
- self.silence_marks.clear();
- // Copied out before anything is written back: the cache below lives on
- // the same struct the session does.
- let Some((clip, source)) = self.session.as_ref().and_then(|session| {
- let clip = *session.lane_clips(lane).get(idx)?;
- Some((clip, session.sources().get(clip.source)?.clone()))
- }) else {
- return;
- };
- // Nothing read yet: the worker is running and the card is drawing its
- // line. The marks arrive with the levels.
- let Some(levels) = self
- .silence_levels
- .get(&(
- source.path.clone(),
- source.audio_stream,
- clip.in_frame,
- clip.out_frame,
- ))
- .cloned()
- else {
- return;
- };
- self.silence_marks = engine::silence::timeline_regions(
- &clip,
- self.fps,
- &engine::silence::regions(&levels, self.silence),
- );
- }
-
- /// Moves the picked row by `steps` and re-runs the scan against it, so the
- /// marks on the lane are always what the numbers on the card say.
- fn nudge_silence(&mut self, steps: i32) {
- let secs = |at: f64| {
- (at + f64::from(steps) * SILENCE_SECS_STEP)
- .clamp(SILENCE_SECS_RANGE.0, SILENCE_SECS_RANGE.1)
- };
- match self.silence_field {
- // Round either way, like the fit policy's cycle: three choices are
- // a ring, not a range.
- 0 => {
- let at = SCOPES.iter().position(|&s| s == self.silence_scope);
- let step = steps.rem_euclid(SCOPES.len() as i32) as usize;
- self.silence_scope = SCOPES[(at.unwrap_or(0) + step) % SCOPES.len()];
- }
- 1 => {
- self.silence.threshold_db = (self.silence.threshold_db
- + steps as f32 * SILENCE_DB_STEP)
- .clamp(SILENCE_DB_RANGE.0, SILENCE_DB_RANGE.1)
- }
- // Two spellings of the same level, so either arrow flips it -- a
- // ring of two, like the scope row's.
- 2 => self.silence_dbfs = !self.silence_dbfs,
- 3 => self.silence.min_silence = secs(self.silence.min_silence),
- 4 => self.silence.padding = secs(self.silence.padding),
- 5 => self.silence.min_keep = secs(self.silence.min_keep),
- _ => {
- self.silence_factor =
- silence_rate(i32::from(self.silence_factor.permille()) + steps * SPEED_STEP)
- }
- }
- // Neither the scope nor the rate is part of the scan, but re-running is
- // cheap (the levels are cached) and one path is one place for the marks
- // to come from.
- self.scan_silences();
- }
-
- /// Which lanes an apply reaches, as the card's scope row says it: the
- /// lanes of the take the scanned clip belongs to, that clip's lane alone,
- /// or every lane there is.
- ///
- /// The take's lanes are the ones carrying its group id -- a link is one
- /// span on however many lanes, so "the take" is exactly the set of lanes
- /// that would otherwise be pulled apart. Nothing widens behind the user's
- /// back: [`Project::cut_regions`] refuses a scope that would split a take,
- /// and this row is how the user says the take instead.
- fn silence_lanes(&self) -> Vec {
- let (Some((lane, idx)), Some(session)) = (self.silence_open, self.session.as_ref()) else {
- return Vec::new();
- };
- match self.silence_scope {
- Scope::Track => vec![lane],
- Scope::Everything => session.lanes(),
- Scope::Take => match session.lane_clips(lane).get(idx).and_then(|c| c.link) {
- None => vec![lane],
- Some(id) => session
- .lanes()
- .into_iter()
- .filter(|&l| {
- l == lane || session.lane_clips(l).iter().any(|c| c.link == Some(id))
- })
- .collect(),
- },
- }
- }
-
- /// What an apply acts on: the previewed set and the lanes it reaches, or
- /// nothing at all with a notice saying so in the numbers that found
- /// nothing.
- fn previewed(&mut self) -> Option<(Vec<(u32, u32)>, Vec)> {
- if self.silence_marks.is_empty() {
- self.notify_user(
- format!(
- "no silence under {:.0} dBFS lasting {:.2} s — raise the threshold or forgive less",
- self.silence.threshold_db, self.silence.min_silence
- )
- .into(),
- );
- return None;
- }
- Some((self.silence_marks.clone(), self.silence_lanes()))
- }
-
- /// What an apply says afterwards: which tracks it reached, and -- when that
- /// was not all of them -- that the rest were left where they were. The
- /// scope is a choice, so the confirmation has to name the choice.
- fn silence_reach(&self, lanes: &[Lane]) -> String {
- let named = lanes
- .iter()
- .map(|l| l.label())
- .collect::>()
- .join("+");
- match self.silence_scope {
- Scope::Everything => "on every track".to_string(),
- _ => format!("on {named} — other tracks untouched"),
- }
- }
-
- /// Cuts every previewed silence out of the lanes the scope names, rippling
- /// each hole closed -- one edit and **one** undo press however many there
- /// were ([`engine::PlaybackSession::cut_regions`]). Tracks outside the
- /// scope do not move; a scope that would take half a take with it comes
- /// back refused in the engine's own words, naming both halves.
- fn cut_silences(&mut self, cx: &mut Context) {
- let Some((regions, lanes)) = self.previewed() else {
- cx.notify();
- return;
- };
- let saved = f64::from(regions.iter().map(|&(_, len)| len).sum::()) / self.fps;
- let (count, reach) = (regions.len(), self.silence_reach(&lanes));
- let Some(session) = self.session.as_mut() else {
- cx.notify();
- return;
- };
- match session.cut_regions(®ions, &lanes) {
- Ok(()) => {
- self.close_silence();
- // Every hole closed moves the clips after it up a place, so the
- // selection now names a different clip than the one that is
- // highlighted -- dropped here as after every other edit that
- // moves indexes (a delete, a paste, an undo).
- self.selected = None;
- self.reset_after_reseek();
- self.notify_user(
- format!(
- "{count} SILENCES CUT {reach} — {} shorter, {} takes it back",
- secs_label(saved),
- self.keymap.display(ActionId::Undo)
- )
- .into(),
- );
- }
- Err(e) => self.notify_user(e.to_string().into()),
- }
- cx.notify();
- }
-
- /// Plays them fast instead of cutting them, closing the room each one no
- /// longer needs. One undo press like the cut, and the same scope; the
- /// refusals (a clip lapping over a silence, a scope that would split a
- /// take) come back in the engine's own words and name the lane and frame,
- /// and the card stays up so the numbers that produced it are still on
- /// screen.
- fn speed_silences(&mut self, cx: &mut Context) {
- let Some((regions, lanes)) = self.previewed() else {
- cx.notify();
- return;
- };
- let (count, rate) = (regions.len(), self.silence_factor);
- let reach = self.silence_reach(&lanes);
- let Some(session) = self.session.as_mut() else {
- cx.notify();
- return;
- };
- match session.speed_regions(®ions, rate, &lanes) {
- Ok(()) => {
- self.close_silence();
- // Splitting each silence out and closing the room it no longer
- // needs moves indexes exactly as the cut does: the selection
- // goes with them.
- self.selected = None;
- self.reset_after_reseek();
- self.notify_user(
- format!(
- "{count} SILENCES AT {rate} {reach} — {} takes it back",
- self.keymap.display(ActionId::Undo)
- )
- .into(),
- );
- }
- Err(e) => self.notify_user(e.to_string().into()),
- }
- cx.notify();
- }
-
- /// Whether a card owns the window. While one does the timeline under it is
- /// out of reach, so a right-click there opens no menu -- the same rule the
- /// key handler and the drop target already follow.
- /// Whether anything at all is drawn over the window -- a card, a menu or an
- /// open list. What the hover labels stand aside for ([`OVERLAID`]): a
- /// tooltip belongs to the surface the pointer is on, and while one of these
- /// is up that surface is behind it.
- fn overlaid(&self) -> bool {
- self.modal()
- || self.context_menu.is_some()
- || self.library_menu.is_some()
- || self.picker.is_some()
- }
-
- fn modal(&self) -> bool {
- self.keys_open
- || self.export_open
- || self.eq_open.is_some()
- || self.color_open.is_some()
- || self.speed_open.is_some()
- || self.silence_open.is_some()
- || self.mix_open
- || self.exporting().is_some()
- }
-
- /// The pointer's way out of whatever card is up: what every scrim's press
- /// calls, so `esc` is *a* way out and never the only one. One list, and the
- /// same one [`Player::modal`] reads -- a card that can be counted there but
- /// not closed here is a card a hand alone cannot shut, which is what
- /// `every_card_closes_without_the_keyboard` fails on.
- ///
- /// Every card at once because only one is ever up (`export_open`): closing
- /// "the" card and closing all of them are the same act.
- fn close_card(&mut self) {
- self.keys_open = false;
- self.keys_search.clear();
- self.rebinding = None;
- self.export_open = false;
- // The two things typed *into* the export card go with it: a field left
- // open would take the next keystroke for a card that is gone.
- self.mbps_edit = None;
- self.picker = None;
- self.eq_open = None;
- self.eq_dragging = false;
- self.color_open = None;
- self.speed_open = None;
- // Marks and a running scan go with this one, which is why it is a call
- // and not an assignment ([`Player::close_silence`]).
- if self.silence_open.is_some() {
- self.close_silence();
- }
- self.mix_open = false;
- }
-
- /// Which of [`Repeat`]'s three the window is in, for the hold gate at the
- /// top of the key handler. Not [`Player::modal`]: that asks whether an
- /// overlay is up at all, and here the cards with sliders in them are
- /// exactly the ones that answer differently from the keys menu and the
- /// export card.
- fn repeat_scope(&self) -> Repeat {
- // A number being typed is a value under the arrows, exactly as a card's
- // slider is -- so a held arrow runs it. Asked before the export card
- // below, which otherwise repeats nothing.
- if self.mbps_edit.is_some() {
- Repeat::Card
- } else if self.rebinding.is_some()
- || self.keys_open
- || self.export_open
- || self.exporting().is_some()
- {
- Repeat::Nothing
- } else if self.eq_open.is_some()
- || self.color_open.is_some()
- || self.speed_open.is_some()
- || self.silence_open.is_some()
- || self.mix_open
- {
- Repeat::Card
- } else {
- Repeat::Keymap
- }
- }
-
- /// Opens the equalizer on the selected clip. Audio only, and it says so
- /// rather than opening a card of bands that would reach nothing: a video
- /// clip carries no sound of its own here (the sound is the audio lane's),
- /// and the model would take the setting without anything ever playing it.
- fn open_eq(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let refusal = match (self.selected, &self.session) {
- (_, None) => Some("NO TIMELINE — open a file first".to_string()),
- (None, _) => Some(format!(
- "NOTHING SELECTED — click an audio clip or press {}, then ask again",
- self.keymap.display(ActionId::Select)
- )),
- (Some((lane, _)), _) if lane.kind != LaneKind::Audio => Some(
- "NOT AN AUDIO CLIP — the equalizer works on the sound, so pick a clip in an audio lane".to_string(),
- ),
- _ => None,
- };
- if let Some(refusal) = refusal {
- self.notify_user(refusal.into());
- cx.notify();
- return;
- }
- let (lane, idx) = self.selected.expect("checked above");
- let session = self.session.as_ref().expect("checked above");
- // What the clip already plays through, or the flat default -- so the
- // card opens on the curve that is in force and a reopen shows the last
- // drag rather than a fresh set of zeroes.
- self.eq_params = session
- .eq_of(lane, idx)
- .cloned()
- .unwrap_or_else(EqParams::default_layout);
- self.eq_band = 0;
- self.eq_dragging = false;
- self.eq_open = Some((lane, idx));
- // One card at a time, the rule the other two already follow.
- self.keys_open = false;
- self.export_open = false;
- self.context_menu = None;
- cx.notify();
- }
-
- /// Writes what the card is showing at its clip: one undo step, one entry in
- /// the append-only equalizer table, so this is called once per *gesture* --
- /// the end of a drag, a keystroke -- and never per pointer sample.
- ///
- /// A curve that moves nothing is stored as *no* equalizer at all, which is
- /// what keeps a clip nobody has touched on the identity path through
- /// playback and export (`engine::eq::EqParams::is_identity`).
- fn commit_eq(&mut self, cx: &mut Context) {
- let Some((lane, idx)) = self.eq_open else {
- return;
- };
- let params = (!self.eq_params.is_identity()).then(|| self.eq_params.clone());
- if let Some(session) = &mut self.session {
- session.set_eq(lane, idx, params);
- }
- // `set_eq` reseeks inside the engine -- that is what makes the change
- // audible at once -- and a reseek is what these flags are about.
- self.reset_after_reseek();
- cx.notify();
- }
-
- /// Changes the picked band in place and says whether anything moved. Every
- /// edit of a band goes through here -- the drag, each key, each stepper
- /// button -- so the card has exactly one place that clamps a band into what
- /// the graph can draw, and no caller has to remember the limits.
- fn set_band(&mut self, change: impl FnOnce(&mut Band)) -> bool {
- let Some(band) = self.eq_params.bands.get_mut(self.eq_band) else {
- return false;
- };
- let was = *band;
- change(band);
- band.freq_hz = band.freq_hz.clamp(EQ_FREQ_LOW, EQ_FREQ_HIGH);
- band.gain_db = band.gain_db.clamp(-EQ_GAIN_LIMIT, EQ_GAIN_LIMIT);
- band.q = band.q.clamp(EQ_Q_LOW, EQ_Q_HIGH);
- *band != was
- }
-
- /// The keyboard's and the buttons' version of a drag: one step on the picked
- /// band, committed straight away -- neither has a release to wait for.
- fn nudge_band(&mut self, change: impl FnOnce(&mut Band), cx: &mut Context) {
- if self.set_band(change) {
- self.commit_eq(cx);
- }
- }
-
- /// Where the pointer sits in the graph, as the picked band's frequency and
- /// gain: across is the frequency axis and down is the gain one, so the
- /// handle follows the hand both ways rather than sliding up a rail. Called
- /// on every pointer sample, so the curve bends under it; the write is the
- /// release's ([`commit_eq`](Player::commit_eq)).
- fn drag_band(&mut self, at: Point, cx: &mut Context) {
- let bounds = self.eq_graph.get();
- let gain = (0.5 - frac_down(at.y, bounds)) * 2. * EQ_GAIN_LIMIT;
- let freq = eq_freq(frac_along(at.x, bounds));
- if self.set_band(|b| {
- b.gain_db = gain;
- b.freq_hz = freq;
- }) {
- cx.notify();
- }
- }
-
- /// A band added beside the picked one, at the frequency with the most room
- /// around it ([`inserted_band`]), and picked so the next keystroke moves the
- /// band that was just made. Refused rather than silently ignored at the cap.
- fn add_band(&mut self, cx: &mut Context) {
- if self.eq_params.bands.len() >= EQ_BANDS_MAX {
- self.notify_user(
- format!(
- "EQUALIZER FULL — {EQ_BANDS_MAX} bands is all this card holds; move one instead"
- )
- .into(),
- );
- cx.notify();
- return;
- }
- let band = inserted_band(&self.eq_params.bands, self.eq_band);
- self.eq_band = (self.eq_band + 1).min(self.eq_params.bands.len());
- self.eq_params.bands.insert(self.eq_band, band);
- self.commit_eq(cx);
- }
-
- /// Takes the picked band out. The last one stays: an equalizer of no bands
- /// is a card with nothing to edit, and flattening is what "off" means here.
- fn remove_band(&mut self, cx: &mut Context) {
- if self.eq_params.bands.len() <= 1 {
- self.notify_user("LAST BAND — flatten it instead (r), or close the card".into());
- cx.notify();
- return;
- }
- self.eq_params.bands.remove(self.eq_band);
- self.eq_band = self.eq_band.min(self.eq_params.bands.len() - 1);
- self.commit_eq(cx);
- }
-
- /// Which band a press on the graph grabs: the nearest one along the
- /// frequency axis, so the whole box is the handle rather than a 10 px dot
- /// -- and a press that misses every dot still moves the band it is under.
- fn nearest_band(&self, x: Pixels) -> usize {
- let at = frac_along(x, self.eq_graph.get());
- self.eq_params
- .bands
- .iter()
- .enumerate()
- .min_by(|(_, a), (_, b)| {
- (eq_x(a.freq_hz) - at)
- .abs()
- .total_cmp(&(eq_x(b.freq_hz) - at).abs())
- })
- .map_or(0, |(i, _)| i)
- }
-
- /// Jumps the timeline.
- fn seek(&mut self, t: f64, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let Some(session) = &mut self.session else {
- return;
- };
- session.seek(t);
- self.reset_after_reseek();
- cx.notify();
- }
-
- /// The keyboard's seek: whole frames along the timeline, through the same
- /// door a ruler click uses -- so a step while playing keeps playing, exactly
- /// as a click does. It starts from the frame the transport is showing, which
- /// past the end is the last one, and that is what lets a step back off EOS
- /// revive the picture (the engine's seek leaves [`Transport::Ended`]). Both
- /// ends clamp, so the two go-to actions are this same step asked for more
- /// frames than the timeline has. Selection is untouched: a seek is not an
- /// edit, and nothing it does moves a clip index.
- fn step(&mut self, frames: i64, cx: &mut Context) {
- let ended = self.transport() == Transport::Ended;
- let Some(session) = &self.session else {
- return;
- };
- let last = ((session.timeline_duration() * self.fps).round() as i64 - 1).max(0);
- let now = match ended {
- true => last,
- false => i64::from(frame_at(session.now(), self.fps)),
- };
- let target = now.saturating_add(frames).clamp(0, last);
- self.seek(target as f64 / self.fps, cx);
- }
-
- /// Splits the clip under the playhead. Metadata only: the timeline->source
- /// mapping is unchanged, so nothing reseeks and no flag is touched.
- fn cut(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- // Snapped to the source's own grid where one is within reach
- // ([`Player::cut_frame`]): a cut a third of a second off a sync point
- // looks identical on the bed and turns an export that copies its
- // picture in minutes into one that codes every frame of it for hours.
- // The playhead goes with it -- what was cut has to be where the line
- // is, or the next stroke acts a few frames from where it looks.
- let Some(session) = &self.session else {
- return;
- };
- let now = frame_at(session.now(), self.fps);
- let at = self.cut_frame(now);
- if at != now {
- self.seek(f64::from(at) / self.fps, cx);
- }
- if let Some(session) = &mut self.session {
- session.cut_at(f64::from(at) / self.fps);
- }
- self.selected = None;
- cx.notify();
- }
-
- /// Rejoins whatever meets under the playhead and puts it back in one group
- /// -- the inverse of [`Player::cut`], and metadata only like it. The engine
- /// decides what is joinable; a refusal is worded here, because `false` is
- /// all it says and a key that looks broken is worse than one that explains
- /// itself.
- fn regroup(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- if let Some(session) = &mut self.session {
- if session.regroup_at(session.now()) {
- self.selected = None;
- } else {
- self.notify_user(
- "NOTHING TO REGROUP — put the playhead where two clips meet, on frames that were cut apart"
- .into(),
- );
- }
- }
- cx.notify();
- }
-
- /// Takes the selected clip out of its group, so the picture and the sound
- /// under it are edited apart from here on: each half selects, moves, trims
- /// and is removed alone, and both draw outlined instead of tinted. The
- /// selection stays -- the half that was clicked is still the half in hand.
- fn detach(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- match (&mut self.session, self.selected) {
- (Some(session), Some((lane, idx))) => {
- if !session.ungroup(lane, idx) {
- self.notify_user(
- "NOTHING DETACHED — that clip is not grouped with another".into(),
- );
- }
- }
- (Some(_), None) => {
- self.notify_user("NOTHING DETACHED — click the take to take apart first".into())
- }
- (None, _) => {}
- }
- cx.notify();
- }
-
- /// Puts the selected clip back in a group with the clip covering exactly the
- /// same frames on another track -- the way back from [`Player::detach`], and
- /// the way to group a picture with sound it was never opened with. The
- /// partner is not clicked because there is nothing to choose: a group id
- /// names one span, so only a clip covering these very frames could join it,
- /// and the engine words what to do when none does.
- fn group(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let partner = match (&self.session, self.selected) {
- (Some(session), Some((lane, idx))) => span_partner(session, lane, idx),
- _ => None,
- };
- match (&mut self.session, self.selected, partner) {
- (Some(session), Some((lane, idx)), Some((other, o_idx))) => {
- if let Err(e) = session.group(lane, idx, other, o_idx) {
- self.notify_user(format!("NOT GROUPED — {e}").into());
- }
- }
- (Some(_), Some(_), None) => {
- self.notify_user(
- "NOTHING TO GROUP WITH — no clip on another track covers exactly these frames"
- .into(),
- )
- }
- (Some(_), None, _) => {
- self.notify_user("NOTHING GROUPED — click one of the halves first".into())
- }
- (None, ..) => {}
- }
- cx.notify();
- }
-
- /// Drops the selected clip and closes the hole: a whole take goes, both
- /// lanes of it, and everything after it moves up. A half with no take under
- /// it in the video lane -- what a lift leaves behind -- has nothing to
- /// ripple, so that one is lifted instead. The engine reseeks itself, so all
- /// this owes is the flag reset.
- fn delete_selected(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let selected = self.selected.take();
- // Whichever lane it was clicked in: the index is that lane's own, and
- // the ripple cuts the clip's span out of every lane -- a group covers
- // one span, so deleting a take by its audio half is the same edit as by
- // its picture. What is not a whole take is lifted instead, which is what
- // reaches a clip on an added track ([`whole_take`]).
- let deleted = match (&mut self.session, selected) {
- (Some(session), Some((lane, idx))) => match whole_take(session, lane, idx) {
- true => session.delete_clip(lane, idx),
- false => session.lift_clip(lane, idx),
- },
- _ => false,
- };
- if selected.is_some() && !deleted {
- self.notify_user("NOTHING DELETED — that clip is no longer there".into());
- }
- if deleted {
- self.reset_after_reseek();
- }
- cx.notify();
- }
-
- /// Lifts the selected half out and leaves the hole: black picture there if
- /// it was the video lane, silence if it was the audio one, and nothing else
- /// moves. What Delete is not.
- fn lift_selected(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- match (&mut self.session, self.selected.take()) {
- (Some(session), Some((lane, idx))) => {
- if session.lift_clip(lane, idx) {
- self.reset_after_reseek();
- } else {
- self.notify_user("NOTHING LIFTED — that half is no longer there".into());
- }
- }
- (Some(_), None) => {
- self.notify_user("NOTHING LIFTED — click the half to remove first".into())
- }
- (None, _) => {}
- }
- cx.notify();
- }
-
- /// Copies the selected clip. Nothing on screen changes, so no notify.
- fn copy_selected(&mut self) {
- let session = self.session.as_ref();
- // Out of the lane it was clicked in: the audio half of a group is a
- // different clip from the video one, and copying the wrong lane's
- // frames is a paste of the wrong thing.
- if let Some(clip) = self
- .selected
- .and_then(|(lane, idx)| session?.lane_clips(lane).get(idx).copied())
- {
- self.clipboard = Some(clip);
- }
- }
-
- /// Starts a peak decode -- and a stream probe -- for every source that has
- /// arrived since the last
- /// repaint. One call from the render rather than three at the doors,
- /// because argv, an import and a project load are all doors and only this
- /// one is guaranteed to run after each of them.
- ///
- /// The decode itself runs on a background thread, like the file chooser:
- /// whole-file audio decode is ~1 s for a half-hour source, and on the render
- /// path that is the window not painting for a second. The lane draws a bed
- /// meanwhile and the repaint comes with the peaks. The entry is written
- /// *before* the spawn, so the sixty repaints that happen while a decode runs
- /// start no further ones.
- fn cache_media(&mut self, cx: &mut Context) {
- let Some(session) = &self.session else {
- return;
- };
- // How big each still is, for the row that has to say so. Inline, unlike
- // the two below: an image header is a few bytes off the front of the
- // file, where a sample table is a parse and a decode is a second.
- for path in unseen_paths(session.sources(), &self.sizes) {
- let size = engine::is_image(&path)
- .then(|| engine::image_size(&path).ok())
- .flatten();
- self.sizes.insert(path, size);
- }
- // Which audio streams each file has, for the library's rows. Header
- // only, but a big file's sample tables are not free to parse, so it
- // goes off the render thread like the peaks do.
- for path in unseen_paths(session.sources(), &self.streams) {
- self.streams.insert(path.clone(), Vec::new());
- let probed = cx.background_executor().spawn({
- let path = path.clone();
- async move { engine::AudioSession::probe_streams(&path).unwrap_or_default() }
- });
- cx.spawn(async move |this, cx| {
- let probed = probed.await;
- this.update(cx, |this, cx| {
- this.streams.insert(path, probed);
- cx.notify();
- })
- .ok();
- })
- .detach();
- }
- // What each file is coded at, for the card that says so. Header and
- // sample table only, but a Matroska indexes no samples and its open
- // walks every cluster header -- 6.7 s on a 12.9 GB film -- so this of
- // all of them cannot be on the render thread.
- for path in unseen_paths(session.sources(), &self.bitrates) {
- self.bitrates.insert(path.clone(), None);
- let probed = cx.background_executor().spawn({
- let path = path.clone();
- async move { engine::probe_bitrate(&path) }
- });
- cx.spawn(async move |this, cx| {
- let probed = probed.await;
- this.update(cx, |this, cx| {
- this.bitrates.insert(path, Some(probed));
- cx.notify();
- })
- .ok();
- })
- .detach();
- }
- // Where this file's own groups of pictures begin, for the cut that
- // wants to land on one ([`Player::sync_frames`]). The heaviest probe
- // here -- a Matroska's whole cluster walk, seconds on a film -- and the
- // one nothing waits for: until it answers, the snap is the clip-edge
- // snap it always was.
- for path in unseen_paths(session.sources(), &self.syncs) {
- self.syncs.insert(path.clone(), Vec::new());
- let probed = cx.background_executor().spawn({
- let path = path.clone();
- async move { engine::demux::sync_points(&path) }
- });
- cx.spawn(async move |this, cx| {
- let probed = probed.await;
- this.update(cx, |this, cx| {
- this.syncs.insert(path, probed);
- cx.notify();
- })
- .ok();
- })
- .detach();
- }
- // Which decoder each file will run on, for the row that says so before
- // a frame of it plays. Off the render thread like the streams above: a
- // stream the plugin takes costs one VA-API init (~90 ms) to answer.
- for path in unseen_paths(session.sources(), &self.decoders) {
- self.decoders.insert(path.clone(), None);
- let probed = cx.background_executor().spawn({
- let path = path.clone();
- // A song and a source no decoder here takes are both `None`:
- // the row says nothing about them rather than guessing, and
- // import refused the second at the door anyway.
- async move { engine::decode::probe(&path).ok() }
- });
- cx.spawn(async move |this, cx| {
- let probed = probed.await;
- this.update(cx, |this, cx| {
- this.decoders.insert(path, probed);
- cx.notify();
- })
- .ok();
- })
- .detach();
- }
- for key in unseen_sources(session.sources(), &self.waves) {
- self.waves.insert(key.clone(), Wave::Loading);
- let decoded = cx.background_executor().spawn({
- let (path, stream) = key.clone();
- async move {
- engine::waveform::peaks(&path, stream, WAVE_BPS)
- .map(|peaks| peaks.map(|peaks| Arc::new(normalise(peaks))))
- .inspect_err(|e| eprintln!("waveform: {}: {e}", path.display()))
- }
- });
- cx.spawn(async move |this, cx| {
- let decoded = decoded.await;
- this.update(cx, |this, cx| {
- this.waves.insert(
- key,
- match decoded {
- Ok(Some(peaks)) => Wave::Peaks(peaks),
- // No audio track: an answer, and not worth asking
- // about again.
- Ok(None) => Wave::Silent,
- // A file whose sound we could not read is not a
- // silent one, and a lane that drew it as silent is
- // how a broken decode passes for a design choice.
- Err(_) => Wave::Failed,
- },
- );
- cx.notify();
- })
- .ok();
- })
- .detach();
- }
- }
-
- /// Probes what an export would open, once per (settings, resolution, cuts)
- /// and only while the export card is up -- it opens the very VA-API encoder
- /// the export would and asks [`engine::export::planned_seats`] the very
- /// question the export asks itself, which is what makes the card's line a
- /// measurement instead of a promise, and also what makes it too slow for
- /// the render thread. Written before the spawn, like the probes above, so
- /// the repaints during it start no second one.
- ///
- /// The cuts are in the key because they are in the answer: moving one onto
- /// a sync point is exactly what turns "SW encode" into "copy", and a card
- /// that kept the old line would be lying about the file it is about to
- /// write.
- fn cache_export_seat(&mut self, cx: &mut Context) {
- let Some(session) = &self.session else {
- return;
- };
- let settings =
- export_settings(self.quality, self.custom_mbps, self.format, self.audio_kbps);
- if !self.export_open {
- return;
- }
- // A format with no picture has no seat to probe -- and the *last*
- // format's is not its answer: cleared rather than left standing, or
- // picking MP3 after AV1 would read "SW encode (rav1e) · MP3 · SW
- // (rusty_mp3)", which names an encoder that will not run.
- if !settings.format.has_video() {
- self.export_seat = None;
- return;
- }
- // The timeline an export would be started with, owned so the probe can
- // run on a worker -- and the clips beside it, which are what tells this
- // that the question has changed.
- let (project, meta) = session.export_snapshot();
- let clips: Vec = session
- .lanes()
- .into_iter()
- .flat_map(|lane| session.lane_clips(lane).to_vec())
- .collect();
- // Cloned rather than copied: the settings carry the picked subtitle
- // rows, which is a `Vec` ([`engine::export::ExportSettings`]).
- let key = (settings.clone(), (meta.width, meta.height), clips);
- if self
- .export_seat
- .as_ref()
- .is_some_and(|(asked, size, cuts, _)| (asked, size, cuts) == (&key.0, &key.1, &key.2))
- {
- return;
- }
- self.export_seat = Some((key.0.clone(), key.1, key.2.clone(), None));
- let probed = cx.background_executor().spawn(async move {
- engine::export::planned_seats(&project, &meta, &settings)
- });
- cx.spawn(async move |this, cx| {
- let probed = probed.await;
- this.update(cx, |this, cx| {
- // Only if the card is still asking the same question: a format
- // changed while the plugin opened has a probe of its own.
- if let Some(seat) = this.export_seat.as_mut().filter(|(asked, size, cuts, _)| {
- (asked, size, cuts) == (&key.0, &key.1, &key.2)
- }) {
- seat.3 = Some(probed);
- }
- cx.notify();
- })
- .ok();
- })
- .detach();
- }
-
- /// Asks the plugin what this machine's GPU can do, once, the first time the
- /// export card is up to show it. Off the render thread for `cache_export_seat`'s
- /// reason: the plugin initialises VA-API to answer, and a driver that is
- /// slow to load must not be a frame the user waits for.
- fn cache_hw_caps(&mut self, cx: &mut Context) {
- if !self.export_open || self.hw_caps.is_some() {
- return;
- }
- // Written before the spawn, exactly as the probes above are, so the
- // repaints during it start no second one.
- self.hw_caps = Some("asking the driver…".into());
- let asked = cx
- .background_executor()
- .spawn(async move { engine::caps::hardware() });
- cx.spawn(async move |this, cx| {
- let line = asked.await;
- this.update(cx, |this, cx| {
- this.hw_caps = Some(line.into());
- cx.notify();
- })
- .ok();
- })
- .detach();
- }
-
- /// The group id of the clicked clip, which is what marks the other half.
- fn selected_link(&self) -> Option {
- let (lane, idx) = self.selected?;
- self.session.as_ref()?.lane_clips(lane).get(idx)?.link
- }
-
- /// Drops the copied clip in at the playhead. The engine reseeks itself, so
- /// like a delete this owes the flag reset -- and the selection, whose index
- /// the insert has just moved.
- fn paste(&mut self, cx: &mut Context) {
- let pasted = match (&mut self.session, self.clipboard) {
- (Some(session), Some(clip)) => session.paste_at(session.now(), clip),
- _ => false,
- };
- if pasted {
- self.selected = None;
- self.reset_after_reseek();
- }
- cx.notify();
- }
-
- /// A clip let go at window `x` over lane `to`: it lands with its head where
- /// the hand is carrying it ([`Player::drop_frame`]), on the track it was
- /// dropped on, taking its whole take with it -- one undo step for the
- /// gesture. Dropped back where it was picked up it is not an edit at all, so
- /// nothing is said about it. The engine reseeks, so all this owes is the
- /// flag reset -- and the selection, whose index was that lane's own and now
- /// names a different clip there.
- fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, x: Pixels, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let (Some((start, _)), Some(was)) = (
- self.drop_frame(from, idx, x),
- self.session
- .as_ref()
- .and_then(|session| session.lane_clips(from).get(idx).map(|c| c.start)),
- ) else {
- return;
- };
- let moved = self
- .session
- .as_mut()
- .is_some_and(|session| session.move_clip_to(from, idx, to, start));
- let (kind, lanes) = match from.kind {
- LaneKind::Video => ("picture", "video"),
- LaneKind::Audio => ("sound", "audio"),
- };
- match moved {
- true => {
- self.selected = None;
- self.reset_after_reseek();
- }
- // The three ways a drag is refused, told apart by what the
- // front-end already knows: a lane's kind, and where the clip was.
- // Everything else that could refuse (a clip that is not there)
- // cannot be dragged.
- false if from.kind != to.kind => {
- self.notify_user(
- format!(
- "NOT ON {} — that is a {kind} clip; drop it on a {lanes} lane",
- to.label()
- )
- .into(),
- )
- }
- // Picked up and put back down where it was: a click, and a click
- // says nothing.
- false if from == to && start == was => {}
- false => {
- self.notify_user(
- format!(
- "NOT MOVED — another clip already covers those frames on {}",
- to.label()
- )
- .into(),
- )
- }
- }
- cx.notify();
- }
-
- /// Where a clip let go at window `x` over lane `to` wants its head: the
- /// frame under the pointer, less however far into the box the hand grabbed
- /// it (so the clip does not jump under the pointer), pulled onto a
- /// neighbouring edge when it lands within [`SNAP_PX`] of one. `None` when
- /// there is no such clip to move. The engine has the last word on where it
- /// may actually go -- this is the ask, not the answer.
- ///
- /// corner-cut: the bed now runs past the last frame whenever the timeline is
- /// shorter than the view ([`Scale::time_at`] clamps at the head only), so a
- /// clip *can* be dragged out there. Zoomed in against the far end it cannot:
- /// the scroll clamp pins the bed's right edge to the duration, and the
- /// pointer has no pixel past it. The upgrade is to let the scroll clamp
- /// leave a screen of empty bed after the end, the way every NLE does.
- fn drop_frame(&self, from: Lane, idx: usize, x: Pixels) -> Option<(u32, Option)> {
- let clip = self.session.as_ref()?.lane_clips(from).get(idx).copied()?;
- let marks = self.snap_targets(Some((from, idx)));
- Some(landing(
- self.frame_under(x),
- self.grab,
- clip.frames(),
- self.snap,
- self.snap_frames(),
- &marks,
- ))
- }
-
- /// The same answer for a library row on its way down: nothing is in the hand
- /// yet, so there is no grab offset to take off and no length to snap by --
- /// the file's own is not known until the engine has placed it -- and only
- /// its head lands. Asked by the line, by the ghost and by the drop itself
- /// ([`Player::insert_source`]), so all three name one frame.
- fn place_frame(&self, x: Pixels) -> (u32, Option) {
- let marks = self.snap_targets(None);
- landing(
- self.frame_under(x),
- 0,
- 0,
- self.snap,
- self.snap_frames(),
- &marks,
- )
- }
-
- /// Which index the clip in the hand is at *now*: [`live_idx`] against the
- /// lane the drag named, since a stroke during the gesture moves the indices
- /// gpui froze into the payload. Both halves of a drag ask it -- the line
- /// drawn in flight and the drop that commits -- so the promise and the
- /// landing are made about one clip.
- fn dragged(&self, drag: &ClipDrag) -> Option {
- let session = self.session.as_ref()?;
- live_idx(session.lane_clips(drag.lane), drag.idx, drag.clip)
- }
-
- /// The line while the clip is still in the hand: the very answer
- /// [`Player::drop_frame`] will commit, worked out on every move of the drag,
- /// so what the eye was promised is where the release puts it. A pointer that
- /// has wandered off the bed promises nothing.
- fn preview_drop(&mut self, from: Lane, idx: usize, x: Pixels, cx: &mut Context) {
- let cue = self.drop_frame(from, idx, x).and_then(|(_, cue)| cue);
- self.set_cue(cue, x, cx);
- }
-
- /// The same line for a library row on its way to a lane: it goes down at
- /// the frame it is let go on ([`Player::place_frame`]), so that frame is
- /// what snaps and what is drawn.
- fn preview_place(&mut self, x: Pixels, cx: &mut Context) {
- let cue = self.place_frame(x).1;
- self.set_cue(cue, x, cx);
- }
-
- /// The shadow the clip in the hand would fill, on the lane the pointer is
- /// over: its head where [`Player::drop_frame`] says the release will put it
- /// -- the same call the drop makes, so the box drawn and the box committed
- /// are one answer -- and its own length at this zoom. A lane of the other
- /// kind refuses the drop ([`Project::move_clip`]), and the shadow says so
- /// before the release does.
- fn preview_ghost(&mut self, drag: &ClipDrag, to: Lane, x: Pixels, cx: &mut Context) {
- let ghost = self
- .dragged(drag)
- .and_then(|idx| self.drop_frame(drag.lane, idx, x))
- .map(|(start, _)| Ghost {
- lane: to,
- start,
- frames: drag.clip.frames(),
- tint: self.clip_tint(drag.clip.source),
- refused: drag.lane.kind != to.kind,
- });
- self.set_ghost(ghost, cx);
- }
-
- /// The line the track in the hand would drop into, on the row the pointer
- /// is over: at that row's top edge when the header is coming up from below
- /// and at its bottom edge when it is going down, which is the slot
- /// [`Player::reorder_lane`] commits to at the release. Nothing at all over
- /// its own row, where a release changes nothing.
- fn preview_lane_drop(&mut self, from: Lane, onto: Lane, cx: &mut Context) {
- let lanes = self
- .session
- .as_ref()
- .map_or_else(Vec::new, PlaybackSession::lanes);
- let at = |lane: Lane| lanes.iter().position(|&l| l == lane);
- let next = match (at(from), at(onto)) {
- (Some(i), Some(j)) if i != j => Some(LaneDrop {
- lane: onto,
- above: j < i,
- }),
- _ => None,
- };
- // Only when it has actually changed: a drag move fires on every painted
- // frame, and a redraw per frame that draws the same line is a redraw
- // for nothing.
- if self.lane_drop != next {
- self.lane_drop = next;
- cx.notify();
- }
- }
-
- /// The line taken back down again, by the row that drew it and by no other:
- /// the pointer has been carried off `lane`, so the slot it was promising is
- /// no longer the one a release would commit to.
- fn forget_lane_drop(&mut self, lane: Lane, cx: &mut Context) {
- if self.lane_drop.is_some_and(|d| d.lane == lane) {
- self.lane_drop = None;
- cx.notify();
- }
- }
-
- /// The same shadow for a library row: its head at [`Player::place_frame`],
- /// which is where the drop inserts it, and the file's own length for its
- /// width -- the length the library row already reports. A file this lane
- /// cannot hold ([`lane_refuses`]) is tinted as refused, which is the answer
- /// the release would give in words.
- fn preview_ghost_asset(&mut self, path: &Path, to: Lane, x: Pixels, cx: &mut Context) {
- let ghost = Ghost {
- lane: to,
- start: self.place_frame(x).0,
- frames: self
- .session
- .as_ref()
- .map_or(0, |session| session.file_frames(path)),
- // A path with no source entry has no colour of its own, and the
- // shadow wears the lane's own instead of borrowing another file's.
- tint: file_tint(self.sources(), path).unwrap_or(BG_RAISED()),
- refused: lane_refuses(path, to).is_some(),
- };
- self.set_ghost(Some(ghost), cx);
- }
-
- /// Sets the shadow, or takes it away, repainting only when it moved -- the
- /// listeners below run it on every pointer sample of a drag. Cleared by the
- /// root and set again by the lane under the pointer, in that order (gpui
- /// runs the capture phase parent-first), so a pointer over no lane at all
- /// leaves nothing drawn.
- fn set_ghost(&mut self, ghost: Option, cx: &mut Context) {
- if ghost != self.ghost {
- self.ghost = ghost;
- cx.notify();
- }
- }
-
- /// The swatch a clip from source `n` wears: [`source_tint`] over the first
- /// source entry naming that *file*, since two audio streams of one file are
- /// two sources and one colour. Every box on a lane and every ghost a drag
- /// draws asks this, so the shadow is recognisably the thing in the hand.
- fn clip_tint(&self, source: usize) -> u32 {
- self.sources()
- .get(source)
- .and_then(|entry| file_tint(self.sources(), &entry.path))
- .unwrap_or_else(|| source_tint(source))
- }
-
- fn sources(&self) -> &[Source] {
- self.session
- .as_ref()
- .map_or(&[][..], PlaybackSession::sources)
- }
-
- /// Sets the line, or takes it away, and repaints only when it moved: a
- /// pointer dragged off the bed (up to the library, say) is not promising a
- /// landing any more.
- fn set_cue(&mut self, cue: Option, x: Pixels, cx: &mut Context) {
- let bed = self.ruler.get();
- let cue = cue.filter(|_| x >= bed.left() && x <= bed.right());
- if cue != self.snap_cue {
- self.snap_cue = cue;
- cx.notify();
- }
- }
-
- /// Every edge this timeline offers a gesture: [`snap_marks`] over all of its
- /// lanes, so a clip meets a take one track over as readily as one beside it.
- fn snap_targets(&self, skip: Option<(Lane, usize)>) -> Vec {
- let Some(session) = &self.session else {
- return Vec::new();
- };
- let lanes = session.lanes();
- let clips: Vec<&[Clip]> = lanes.iter().map(|&lane| session.lane_clips(lane)).collect();
- let skip = skip.and_then(|(lane, idx)| Some((lanes.iter().position(|&l| l == lane)?, idx)));
- snap_marks(&clips, skip, frame_at(session.now(), self.fps))
- }
-
- /// Where a gesture at `raw` lands and the mark that pulled it there, with
- /// the switch honoured: snapping off, nothing moves and no line is drawn.
- fn snap_to(&self, raw: u32, len: u32, marks: &[u32]) -> (u32, Option) {
- snap_cue(self.snap, raw, len, self.snap_frames(), marks)
- }
-
- /// Every timeline frame that is a *source* sync point: each clip's own
- /// grid ([`Player::syncs`]), moved onto the frames the clip plays it at.
- /// Ascending, because the clips are and each grid is.
- ///
- /// This is the difference between an export that copies its picture and one
- /// that decodes and re-codes every frame of a feature film. A cut anywhere
- /// else leaves the copy path with a region that begins between two sync
- /// points -- pictures whose references are not in the file -- and the whole
- /// export falls back to the encoder ([`engine::export`] states the rule).
- ///
- /// Only clips at their own speed, and only video lanes: a re-timed clip is
- /// resampled pictures, which is not a copy at any cut, and a sound lane has
- /// no groups of pictures to begin with.
- fn sync_frames(&self) -> Vec {
- let Some(session) = &self.session else {
- return Vec::new();
- };
- let sources = session.sources();
- let mut marks = Vec::new();
- for lane in session.lanes() {
- if lane.kind != LaneKind::Video {
- continue;
- }
- for clip in session.lane_clips(lane) {
- let Some(keys) = sources
- .get(clip.source)
- .and_then(|entry| self.syncs.get(&entry.path))
- .filter(|_| clip.speed.is_normal())
- else {
- continue;
- };
- marks.extend(
- keys.iter()
- .filter(|&&key| key >= clip.in_frame && key < clip.out_frame)
- .map(|&key| clip.start + (key - clip.in_frame)),
- );
- }
- }
- marks.sort_unstable();
- marks
- }
-
- /// The frame a cut asked for at `raw` really lands on: the nearest source
- /// sync point within the snap's own tolerance, or `raw` itself where the
- /// magnet is off, where nothing is near enough, or where the source has no
- /// grid to offer (the walk has not answered yet, or the file is not one
- /// this project can copy at all).
- ///
- /// The same tolerance the clip-edge snap uses, so one switch and one
- /// distance govern every landing on this timeline.
- fn cut_frame(&self, raw: u32) -> u32 {
- if !self.snap {
- return raw;
- }
- let tol = self.snap_frames();
- self.sync_frames()
- .into_iter()
- .filter(|mark| mark.abs_diff(raw) <= tol)
- .min_by_key(|mark| mark.abs_diff(raw))
- .unwrap_or(raw)
- }
-
- /// Whether the playhead is standing exactly on one: what the timeline's own
- /// line says out loud, so "a cut here is copied" is on screen before the cut
- /// rather than discovered in the export card afterwards.
- ///
- /// Asked every repaint, so it walks the *playhead* into each clip's source
- /// and looks it up in that source's own sorted grid -- where
- /// [`Player::sync_frames`] builds the whole list, which is a film's worth of
- /// marks to allocate and sort sixty times a second.
- fn on_sync_point(&self) -> bool {
- let Some(session) = &self.session else {
- return false;
- };
- let now = frame_at(session.now(), self.fps);
- let sources = session.sources();
- session.lanes().into_iter().any(|lane| {
- lane.kind == LaneKind::Video
- && session.lane_clips(lane).iter().any(|clip| {
- clip.speed.is_normal()
- && (clip.start..clip.start + (clip.out_frame - clip.in_frame))
- .contains(&now)
- && sources
- .get(clip.source)
- .and_then(|entry| self.syncs.get(&entry.path))
- .is_some_and(|keys| {
- keys.binary_search(&(clip.in_frame + (now - clip.start))).is_ok()
- })
- })
- })
- }
-
- /// Puts the playhead on the sync point before or after it -- the keyboard's
- /// half of placing a cut where the export can copy it, and the only way to
- /// reach one exactly on a timeline zoomed out to a whole film, where one
- /// pixel is seconds.
- fn jump_sync(&mut self, forward: bool, cx: &mut Context) {
- let Some(session) = &self.session else {
- return;
- };
- let now = frame_at(session.now(), self.fps);
- let marks = self.sync_frames();
- let mark = match forward {
- true => marks.iter().find(|&&mark| mark > now).copied(),
- false => marks.iter().rev().find(|&&mark| mark < now).copied(),
- };
- match mark {
- Some(mark) => self.seek(f64::from(mark) / self.fps, cx),
- // Said rather than swallowed: the two most likely reasons are a walk
- // that has not answered yet and a source with no grid at all, and a
- // key that does nothing looks broken either way.
- None => self.notify_user(match marks.is_empty() {
- true => "NO SYNC POINTS — this source has no keyframe grid to jump by (or it is \
- still being read)"
- .into(),
- false => "NO SYNC POINT THAT WAY — the playhead is past the last one".into(),
- }),
- }
- }
-
- /// [`SNAP_PX`] in timeline frames at the scale the bed is drawn at: the bed's
- /// own width drops out of it, since a pixel is now worth the same stretch of
- /// timeline wherever the view sits.
- fn snap_frames(&self) -> u32 {
- self.scale.snap_frames(self.fps)
- }
-
- /// Opens the clip menu on the box under the pointer, from the right button
- /// wherever it was pressed on that box -- its middle or one of its edge
- /// strips, which cover the middle's own listener. Selecting first is part of
- /// it: every item acts on the clip the menu names.
- fn open_menu(&mut self, lane: Lane, idx: usize, at: Point, cx: &mut Context) {
- if self.modal() {
- return;
- }
- self.select((lane, idx), cx);
- self.context_menu = Some(ContextMenu {
- lane,
- idx,
- at,
- details: false,
- });
- cx.notify();
- }
-
- /// A press on a clip's edge: the start of the drag that changes how much of
- /// its source it plays. It selects the clip as a press anywhere else on the
- /// box does -- the edge strip covers the box's own listener (`occlude`), so
- /// this is the only one that fires there.
- fn start_trim(&mut self, lane: Lane, idx: usize, edge: Edge, cx: &mut Context) {
- if self.modal() || self.exporting().is_some() {
- return;
- }
- let Some(clip) = self
- .session
- .as_ref()
- .and_then(|session| session.lane_clips(lane).get(idx).copied())
- else {
- return;
- };
- self.select((lane, idx), cx);
- self.trim = Some(Trim {
- lane,
- idx,
- edge,
- // Where the edge already is: a press that never moves is not an
- // edit, and `Project::trim` refuses exactly that.
- to: match edge {
- Edge::Start => clip.start,
- Edge::End => clip.end(),
- },
- link: clip.link,
- });
- cx.notify();
- }
-
- /// Where the pointer has pulled the edge to, clamped to the room the engine
- /// says that edge has. Along the same bed the ruler is measured on and
- /// against the same duration the boxes are drawn to, so the edge tracks the
- /// pointer exactly.
- fn trim_to(&mut self, x: Pixels, cx: &mut Context) {
- let Some(trim) = self.trim else {
- return;
- };
- // The edge is pulled onto the same marks a whole clip is, by itself:
- // there is no other end travelling with it, so it snaps at length zero.
- let marks = self.snap_targets(Some((trim.lane, trim.idx)));
- let (at, cue) = self.snap_to(self.frame_under(x), 0, &marks);
- let Some((lo, hi)) = self
- .session
- .as_ref()
- .and_then(|session| session.trim_room(trim.lane, trim.idx, trim.edge))
- else {
- return;
- };
- let to = at.clamp(lo, hi);
- // The line only stands where the edge actually stopped: a mark the
- // engine's own room clamped away was never reached.
- self.set_cue(cue.filter(|_| to == at), x, cx);
- self.trim = Some(Trim { to, ..trim });
- cx.notify();
- }
-
- /// The timeline frame a pointer at window x is on: along the same bed the
- /// ruler is measured on, through the same [`Scale`] every box is drawn
- /// through, so a zoomed-in panel answers with the frame under the pointer
- /// and not with the one that would have been there unzoomed. The one
- /// question a trim, a grab and a drop all ask.
- fn frame_under(&self, x: Pixels) -> u32 {
- frame_at(
- self.scale.time_at(px_along(x, self.ruler.get())),
- self.fps,
- )
- }
-
- /// The release: the whole drag reaches the engine as one edit, so it is one
- /// undo step. The selection survives it -- a trim inserts and removes
- /// nothing, so every index a lane had still names the clip it named.
- fn commit_trim(&mut self, cx: &mut Context) {
- let Some(trim) = self.trim.take() else {
- return;
- };
- let trimmed = self
- .session
- .as_mut()
- .is_some_and(|session| session.trim_clip(trim.lane, trim.idx, trim.edge, trim.to));
- if trimmed {
- self.reset_after_reseek();
- }
- cx.notify();
- }
-
- /// The clip as the drag is showing it: an edge under the pointer moves its
- /// own box, and the boxes of the halves linked to it, before anything is
- /// committed. Display only -- the project is not touched until the release.
- fn trimmed(&self, lane: Lane, idx: usize, clip: Clip) -> Clip {
- let Some(trim) = self.trim.filter(|t| {
- (t.lane, t.idx) == (lane, idx) || (t.link.is_some() && t.link == clip.link)
- }) else {
- return clip;
- };
- let still = self.session.as_ref().is_some_and(|session| {
- session
- .sources()
- .get(clip.source)
- .is_some_and(|s| engine::is_image(&s.path))
- });
- trimmed_clip(clip, trim.edge, trim.to, still)
- }
-
- /// How long the timeline is *drawn* as: its own length, and while a tail is
- /// being dragged the furthest that tail may reach. A bed that ends exactly
- /// at the last frame has nowhere to put a pointer that means "longer", so
- /// without this the last clip on the timeline could be pulled in and never
- /// let back out.
- ///
- /// Scroll room only, now that a second is an absolute number of pixels
- /// ([`Scale`]): the extra length loosens [`View::settled`]'s clamp, which is
- /// where the pixels past the last frame come from, and moves no box by a
- /// pixel. It is still the *only* headroom at the tail -- zoomed in against
- /// the end, that clamp pins the bed's right edge to the duration and an
- /// End-trim of the last clip would have nowhere to be dragged to. What it
- /// must not do is be read as a length anyone is told: the timecode reads
- /// `PlaybackSession::timeline_duration` for exactly that reason.
- fn drawn_duration(&self) -> f64 {
- let Some(session) = &self.session else {
- return 0.;
- };
- let duration = session.timeline_duration();
- match self.trim {
- Some(trim) if trim.edge == Edge::End => {
- let (_, hi) = session
- .trim_room(trim.lane, trim.idx, trim.edge)
- .unwrap_or((0, 0));
- duration.max(f64::from(hi) / self.fps)
- }
- _ => duration,
- }
- }
-
- /// Where the playhead is, as the panel draws it: pinned to the out point
- /// once playback is done, and clamped to the drawn duration otherwise -- a
- /// tail being dragged draws past the timeline it is about to become.
- fn playhead(&self, duration: f64) -> f64 {
- if self.transport() == Transport::Ended {
- duration
- } else {
- self.session
- .as_ref()
- .map_or(0., PlaybackSession::now)
- .clamp(0., duration)
- }
- }
-
- /// The one way a library row reaches the timeline: the Add button and a row
- /// dragged onto a lane both come here, so there is a single answer to what
- /// "add this source" does. The whole source goes in as one grouped take at
- /// `at` -- the frame the pointer let it go on, or the playhead for the
- /// button, which names no place. It is the same insert a paste makes, so
- /// everything after it moves along rather than being painted over. Reseeks
- /// like every other edit, and drops the timeline's selection with it: the
- /// insert has just moved the indices it pointed at.
- fn insert_source(
- &mut self,
- path: &Path,
- stream: usize,
- onto: Option,
- at: Option,
- cx: &mut Context,
- ) {
- if self.exporting().is_some() {
- return;
- }
- // The lane the pointer named cannot hold this kind of file: refused by
- // name, in the same words the ghost was tinted by on the way down
- // ([`lane_refuses`]). The Add button names no lane and so is never
- // refused here -- where a file goes when nobody says is the engine's
- // choice, in `place_stream_at`, not one made twice here.
- if let Some(why) = onto.and_then(|lane| lane_refuses(path, lane)) {
- self.notify_user(why.into());
- cx.notify();
- return;
- }
- // The engine's own length for the file, noted when the import took it
- // in: a row that has never been on a lane is placeable at its full
- // length, which is the whole point of an import that only fills the
- // library.
- let fps = self.fps;
- let placed = match &mut self.session {
- // Seconds, because that is what the engine's own door takes: the
- // frame the pointer named goes back through the same rate every box
- // on the bed is drawn at, so it lands on the frame it was let go on
- // rather than a neighbouring one.
- Some(session) => {
- let at = at.map_or_else(|| session.now(), |frame| f64::from(frame) / fps);
- session.place_stream_at(at, path, stream, onto)
- }
- None => Ok(false),
- };
- match placed {
- Ok(true) => {
- self.selected = None;
- self.reset_after_reseek();
- }
- // The engine's own words: a stream that cannot join this timeline
- // says which property disagrees, exactly as a refused import does.
- Err(e) => self.notify_user(format!("NOTHING ADDED — {e}").into()),
- Ok(false) => {
- self.notify_user("NOTHING ADDED — that file could not be placed here".into())
- }
- }
- cx.notify();
- }
-
- /// Takes a library row's file out of the list, which is the one thing a row
- /// can lose. Refused in the engine's own words while clips still play from
- /// it -- and those words name the lanes holding them, so the refusal says
- /// what to delete first. The list itself is the report that it worked: the
- /// row is gone from it.
- fn remove_source(&mut self, path: &Path, stream: usize, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let removed = self
- .session
- .as_mut()
- .map(|session| session.remove_source(path, stream));
- let text = match removed {
- Some(Ok(idx)) => {
- // The picked row may be the one that just went, and the engine
- // reseeks, so this owes the flag reset like every other edit.
- if self.selected_asset.as_ref() == Some(&(path.to_path_buf(), stream)) {
- self.selected_asset = None;
- }
- // A copied clip names its source by *index*, and every index
- // past the one that went has just moved down: without this the
- // next paste puts some other file on the timeline.
- self.clipboard = clipboard_after_remove(self.clipboard, idx);
- self.reset_after_reseek();
- // The last row leaves a session naming no file: nothing to
- // play, nothing to save and nothing to show, which is the empty
- // window the editor launches as. The next import scaffolds a
- // fresh timeline from whatever file it is, at that file's own
- // rate -- which is why the session goes rather than lingering
- // on with the gone file's parameters.
- match self
- .session
- .as_ref()
- .is_some_and(|s| s.sources().is_empty())
- {
- true => {
- self.close_session();
- format!(
- "REMOVED {} — the library is empty; import a file to start again",
- file_name(path)
- )
- }
- // The undo stack goes with it (`Project::remove_source`):
- // said here, because a `z` that does nothing afterwards
- // would otherwise read as a bug.
- false => format!(
- "REMOVED {} — there is nothing left to undo",
- file_name(path)
- ),
- }
- }
- Some(Err(e)) => format!("NOT REMOVED — {e}"),
- None => "NO TIMELINE — open a file first".to_string(),
- };
- self.notify_user(text.into());
- cx.notify();
- }
-
- /// Back to the window the editor launches as: no timeline, no library, no
- /// picture -- and the hint that says to open a file. What removing the last
- /// library row leaves, since a session whose library is empty has nothing
- /// left to be ([`Player::remove_source`]).
- ///
- /// Everything a *loaded project* resets goes here for its reasons (an index
- /// into a timeline that is gone names nothing), plus the three per-file
- /// caches: they are keyed by path, and the next file to arrive fills them
- /// again.
- fn close_session(&mut self) {
- self.session = None;
- // The picture goes with it, or the empty window would keep showing the
- // last frame of a timeline that no longer exists.
- //
- // corner-cut: its atlas tile is not released -- `window.drop_image` wants
- // a `&mut Window` this door has no other reason to take. One tile per
- // emptied library, against one per displayed frame in `pump`; the
- // upgrade path is threading the window through `act_on_row`.
- self.image = None;
- // The drawn cue with it, and its tile for the same reason as above.
- self.sub_image = None;
- self.clipboard = None;
- self.selected = None;
- self.selected_asset = None;
- // The subtitle rows go with the timeline they were on.
- self.sub_track = 0;
- self.context_menu = None;
- self.library_menu = None;
- self.eq_open = None;
- self.color_open = None;
- self.speed_open = None;
- self.close_silence();
- self.waves.clear();
- self.streams.clear();
- self.bitrates.clear();
- self.sizes.clear();
- self.syncs.clear();
- // Scanned off sources that are not in the library any more.
- self.silence_levels.clear();
- // Every gesture in flight, dropped for `reset_after_reseek`'s reason
- // (it drops the trim below): a drag holds a bar, a clip or a band of a
- // timeline that has just stopped existing.
- self.scrubbing = false;
- self.volume_dragging = false;
- self.eq_dragging = false;
- self.speed_dragging = false;
- self.color_dragging = false;
- self.pending_color = None;
- self.pending_speed = None;
- self.displayed = 0;
- self.dropped = 0;
- self.started = None;
- // The empty window's own: no name in the titlebar, nowhere chosen to
- // export or save to yet, and a rate that only keeps the timecode
- // reading in frames until a file brings its own (`main`).
- self.name = NO_FILE.into();
- self.export_path = PathBuf::new();
- self.project_path = PathBuf::new();
- self.fps = 30.;
- // No decoder to wait for a frame from: the hint is what shows. The
- // transport reads `Stopped` from the session being gone, so there is no
- // end-of-stream state left to clear here.
- self.reset_after_reseek();
- self.seek_since = None;
- }
-
- /// One item of a library row's menu, done. Every one of them closes the
- /// menu first -- the list under it is about to be rebuilt -- except the one
- /// that turns the card over.
- fn act_on_row(&mut self, item: RowItem, cx: &mut Context) {
- let Some(menu) = self.library_menu.clone() else {
- return;
- };
- match item {
- RowItem::Properties => {
- if let Some(open) = &mut self.library_menu {
- open.details = true;
- }
- }
- RowItem::Add => {
- self.library_menu = None;
- self.insert_source(&menu.path, menu.stream, None, None, cx);
- }
- RowItem::Remove => {
- self.library_menu = None;
- self.remove_source(&menu.path, menu.stream, cx);
- }
- RowItem::Reveal => {
- self.library_menu = None;
- // Another process starting: off the UI thread, exactly as the
- // export notice's own click starts it.
- cx.background_executor()
- .spawn(async move { show_in_file_manager(&menu.path) })
- .detach();
- }
- }
- cx.notify();
- }
-
- /// The rate and layout the whole timeline's audio is, taken from the stream
- /// of the first source that could have one: what a library row has to match
- /// to be placeable. `None` until that file has been probed, and then nothing
- /// is greyed for it.
- ///
- /// The first source that is *not a still*, which is the rule the engine
- /// holds every import to (`playback::audio_source_of`) -- a picture at the
- /// head of the list (a removal moves indexes) has no stream to describe
- /// anything with.
- fn timeline_audio(&self) -> Option<(u32, u16)> {
- let first = self
- .session
- .as_ref()?
- .sources()
- .iter()
- .find(|s| !engine::is_image(&s.path))?;
- let info = self
- .streams
- .get(&first.path)?
- .iter()
- .find(|s| s.index == first.audio_stream)?;
- Some((info.sample_rate, info.channels))
- }
-
- /// Queues a file for the library. Nothing is read here: the reading is
- /// [`read_ahead`] on a worker, and [`Player::take_import`] is what finally
- /// touches the timeline, one repaint later and with the pages warm. A drop
- /// is not a key press, so the export guard on the key handler does not
- /// cover it and this checks for itself.
- ///
- /// One file at a time, in arrival order: a drop can carry six and argv can
- /// name more, and six header walks racing over one disk finish no sooner
- /// than six in a row -- while the line above the panel has exactly one file
- /// to name.
- fn import(&mut self, path: &std::path::Path, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- self.imports.push_back(path.to_path_buf());
- self.start_import(cx);
- }
-
- /// Starts the worker for the next queued file, if no worker is running.
- /// Called again as each import lands, which is what drains the queue.
- fn start_import(&mut self, cx: &mut Context) {
- if self.importing.is_some() {
- return;
- }
- let Some(path) = self.imports.pop_front() else {
- return;
- };
- let stage = Arc::new(std::sync::atomic::AtomicU8::new(ImportStage::Header as u8));
- let cancelled = Arc::new(std::sync::atomic::AtomicBool::new(false));
- // The fork is made here, once, and carried to the landing: an import is
- // *probed* on the worker and registered from what came back, while the
- // file argv named -- and a file arriving at a window with no timeline to
- // import into -- is *opened* on the worker and handed over whole. None
- // of the three leaves the UI thread anything to read: a cold 24 GB
- // header walk is twenty seconds, and the window keeps painting through
- // all of them.
- let what = arrival(self.opening.as_deref(), &path);
- // The timeline the file will be checked against, taken here because the
- // worker cannot reach the session: two clones and no disk
- // ([`PlaybackSession::import_gate`]). `None` is a window with nothing to
- // import into, which is the fork that opens the file outright.
- let gate = self.session.as_ref().map(PlaybackSession::import_gate);
- let read = cx.background_executor().spawn({
- let (path, stage) = (path.clone(), Arc::clone(&stage));
- async move { open_ahead(what, &path, &stage, gate) }
- });
- let now = Instant::now();
- self.importing = Some(Import {
- path: path.clone(),
- started: now,
- stage,
- seen: ImportStage::Header,
- since: now,
- cancelled: Arc::clone(&cancelled),
- });
- cx.spawn(async move |this, cx| {
- let landed = read.await;
- this.update(cx, |this, cx| {
- this.importing = None;
- // Cancelled while it read: the window was given back at the
- // click and said so then, so what the worker carried is dropped
- // without a second word ([`Player::cancel_import`]).
- if cancelled.load(std::sync::atomic::Ordering::Relaxed) {
- return;
- }
- this.take_import(&path, landed, cx);
- // The next one is started by the repaint this notified, which
- // is also what starts the files argv named ([`poll_import`]).
- })
- .ok();
- })
- .detach();
- cx.notify();
- }
-
- /// Keeps the import line's two clocks honest: the elapsed one runs from the
- /// worker, and the stall one from the last time the stage it is naming
- /// actually changed. Sampled here rather than while drawing, for
- /// [`Player::poll_export`]'s reason.
- ///
- /// ...and starts whatever is queued behind it, which is the one place the
- /// files argv named can begin: they are put in the queue before there is a
- /// context to spawn a worker from.
- fn poll_import(&mut self, cx: &mut Context) {
- match &mut self.importing {
- Some(import) => {
- import.poll();
- }
- None => self.start_import(cx),
- }
- }
-
- /// The Cancel beside the import line: the window is given back at once and
- /// the file does not land. Everything queued behind it goes too -- a person
- /// who has stopped an import of six dropped files has stopped the six, and
- /// leaving five to start themselves would be the same wait under another
- /// name.
- ///
- /// The read in flight is *not* stopped, for the reason [`Import::cancelled`]
- /// gives, and the notice says as much rather than promising the disk went
- /// quiet.
- fn cancel_import(&mut self, cx: &mut Context) {
- let Some(import) = self.importing.take() else {
- return;
- };
- import
- .cancelled
- .store(true, std::sync::atomic::Ordering::Relaxed);
- let waiting = self.imports.len();
- self.imports.clear();
- let tail = match waiting {
- 0 => String::new(),
- n => format!(" — {n} more dropped from the queue"),
- };
- let text = format!(
- "IMPORT CANCELLED: {}{tail} — the read already running finishes unheeded",
- file_name(&import.path)
- );
- eprintln!("{text}");
- self.notify_user(text.into());
- cx.notify();
- }
-
- /// Takes a read-ahead file into the library and nowhere else: the timeline
- /// is not touched, and the row is dragged onto a lane when it is wanted
- /// there. Nothing moves, so nothing reseeks; a refusal is shown as the
- /// engine worded it and changes nothing.
- ///
- /// The export guard again, and not for the caller's sake: an export can
- /// have started during the seconds the worker was reading, and a drop
- /// during an export has always been a silent no-op.
- fn take_import(&mut self, path: &std::path::Path, landed: Landed, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- // The file argv named is the one file in the queue that is not an
- // import: it *is* the timeline, and the worker has already opened it.
- // All that is left here is to hang everything derived from it off the
- // window -- the clock, the title, where an export and a save go -- and
- // that is arithmetic, not a read.
- let (subs, probe) = match landed {
- Landed::Read(subs, probe) => (subs, probe),
- what => {
- // Only when *this* is the file argv named: a project dropped
- // while that one is still being read must not make it land as
- // an import.
- if self.opening.as_deref() == Some(path) {
- self.opening = None;
- }
- match what {
- Landed::Project(opened) => self.install_project(path, opened, cx),
- Landed::Media(opened, place) => {
- let text = self.install_media(path, opened, place);
- eprintln!("{text}");
- self.notify_user(text.into());
- cx.notify();
- }
- Landed::Read(..) => unreachable!("matched above"),
- }
- // The line a launch has always printed, now printed when the
- // file actually arrives: it is the mark that says the timeline
- // is up, as the window's own appearance is the other one.
- if let Some(meta) = self.session.as_ref().map(PlaybackSession::meta) {
- println!(
- "{}: {}x{} @ {:.2} fps, {} samples",
- path.display(),
- meta.width,
- meta.height,
- meta.frame_rate,
- meta.frame_count
- );
- }
- return;
- }
- };
- // An empty window has no library to add to yet: the file opens one, and
- // the timeline under it stays empty, because an import is an import
- // whether or not a session was already up. A file *named at launch* is
- // the other fork -- that one is an open, and it does become the
- // timeline (`main`).
- // A subtitle file is not a source and lands on no lane: it joins the
- // timeline's own list of them, which is what the library's subtitle
- // section shows and what the overlay draws. With no timeline open there
- // is nothing for the cues to be timed against, and it says so.
- if is_subtitle(path) {
- self.take_subtitles(path, subs, cx);
- return;
- }
- // The container was read on the worker and what came back is registered
- // here ([`engine::PlaybackSession::import_probed`]): no header walk, no
- // decoder open, no probe of the timeline's own first source -- the three
- // reads that used to be spent on this thread. A song and a still fork
- // before the demuxer and pay their own small read
- // ([`engine::PlaybackSession::import`]); a window whose timeline went
- // away while the worker read falls to the slow door below, which is the
- // one that can still open one.
- let registered = match (self.session.as_mut(), probe) {
- (Some(session), Some(Ok(probe))) => Some(session.import_probed(path, probe)),
- (Some(_), Some(Err(refused))) => Some(Err(refused)),
- (Some(session), None) => Some(session.import(path)),
- (None, _) => None,
- };
- let text = match registered {
- Some(Ok(_)) => {
- // The file's own subtitle tracks with it, exactly as an open
- // takes them: an import is the other door the same file arrives
- // through. The cues were read on the worker
- // ([`read_ahead`]); what happens here is the push.
- let tail = self
- .session
- .as_mut()
- .and_then(|session| subtitle_tail(session, subs))
- .unwrap_or_default();
- format!(
- "IMPORTED {} to the library — drag it onto a lane to place it{tail}",
- file_name(path)
- )
- }
- // Named, because two files can fail in one launch and the queue now
- // shows both: "No such file or directory" twice over, with nothing
- // saying which file, is two messages that answer nothing.
- Some(Err(e)) => format!("IMPORT FAILED: {} — {e}", file_name(path)),
- None => self.open_media(path, false, subs),
- };
- eprintln!("{text}");
- self.notify_user(text.into());
- cx.notify();
- }
-
- /// Takes a file as the session an empty window is waiting for. Everything
- /// derived from the media -- the clock, the title, where an export and a
- /// save go -- is set here, exactly as a launch with a file argument sets
- /// it. Paused with its first frame showing, like every other way a timeline
- /// arrives.
- ///
- /// `place` is the difference between the two doors that come here: a file
- /// *opened* is the timeline, one *imported* into an empty window fills the
- /// library and leaves the lanes empty for a drag.
- fn open_media(&mut self, path: &std::path::Path, place: bool, subs: Subs) -> String {
- self.install_media(path, open_session(path, place, subs), place)
- }
-
- /// The second half of it: everything the window derives from a session that
- /// has already been opened. Split from the open itself because the file
- /// argv named is opened on a worker ([`open_ahead`]) -- the twelve seconds
- /// of a cold header walk are not the UI thread's to spend -- and lands
- /// here, where nothing is read and nothing blocks.
- fn install_media(
- &mut self,
- path: &std::path::Path,
- opened: Result<(PlaybackSession, String), String>,
- place: bool,
- ) -> String {
- match opened {
- Ok((session, subs)) => {
- self.fps = session.meta().frame_rate;
- // Read before the session moves: a file that plays silent says
- // so here or nowhere.
- let silent = audio_notice(&session);
- // A file replaces the one that was open, and track 3 of that one
- // is not track 3 of this.
- self.sub_track = 0;
- self.session = Some(session);
- // A fresh session comes up at full volume; the player's own
- // setting outlives the file, so it is pushed at every new one.
- self.apply_volume();
- // Beside the new file, but still the format the card is set to:
- // opening another clip is not a change of mind about that.
- self.export_path = retarget(&export_path(path), self.format);
- self.project_path = project_path(path);
- self.name = file_name(path).into();
- self.reset_after_reseek();
- let name = file_name(path);
- // The library is filled and the timeline is empty; the only
- // thing that says so is this line, so it says what to do next.
- let what = match place {
- true => format!("OPENED {name}"),
- false => {
- format!("IMPORTED {name} to the library — drag it onto a lane to place it")
- }
- };
- format!("{what}{}{subs}", silent.unwrap_or_default())
- }
- Err(e) => format!("OPEN FAILED: {e}"),
- }
- }
-
- /// The Import button: asks the desktop for a path and takes it the same way
- /// a drop would. The chooser is another process and the user may sit in it,
- /// so it runs on a background thread -- blocking here would freeze the
- /// window behind the dialog.
- fn pick_and_import(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let picked = cx
- .background_executor()
- .spawn(async { pick_file("edith — import") });
- cx.spawn(async move |this, cx| {
- let picked = picked.await;
- this.update(cx, |this, cx| match picked {
- // One queue, and the fork is made when its worker starts
- // ([`arrival`]): a project replaces the timeline, media joins
- // the library, and neither is read on this thread.
- Ok(Some(path)) => this.import(&path, cx),
- // Cancelled: the user already knows what happened.
- Ok(None) => {}
- Err(text) => {
- eprintln!("{text}");
- this.notify_user(text.into());
- cx.notify();
- }
- })
- .ok();
- })
- .detach();
- }
-
- /// The `+ S` button and its key: asks the desktop for a file and takes the
- /// subtitle tracks out of it -- a standalone `.srt`/`.vtt`/`.ass` is one of
- /// them, a Matroska however many are inside. Only the subtitles: the file
- /// itself does not join the library, which is what the Import button beside
- /// this one is for.
- ///
- /// The chooser is another process and the user may sit in it, so it runs on
- /// a background thread, exactly as [`Player::pick_and_import`] does.
- fn pick_and_add_subtitles(&mut self, cx: &mut Context) {
- // What dims the `+ S` button, asked here as well so the key answers the
- // same question -- and *before* the chooser rather than after it: a door
- // that opens a dialog, waits for a file and only then says the timeline
- // was never there is the second door disagreeing with the first.
- if let Some(why) = self.enable(ActionId::AddSubtitleTrack, None).why() {
- let text = format!("NO SUBTITLES ADDED — {why}");
- eprintln!("{text}");
- self.notify_user(text.into());
- cx.notify();
- return;
- }
- let picked = cx
- .background_executor()
- .spawn(async { pick_file("edith — subtitles to add") });
- cx.spawn(async move |this, cx| {
- let picked = picked.await;
- this.update(cx, |this, cx| match picked {
- Ok(Some(path)) => this.add_subtitles(&path, cx),
- // Cancelled: the user already knows what happened.
- Ok(None) => {}
- Err(text) => {
- eprintln!("{text}");
- this.notify_user(text.into());
- cx.notify();
- }
- })
- .ok();
- })
- .detach();
- }
-
- /// Takes a file's subtitle tracks onto the timeline, off the render thread.
- /// The walk reads the whole container for its cues
- /// (`engine::PlaybackSession::parse_subtitles`) -- ~200 ms on a two-hour 4K
- /// remux and 1.3 s on a cold 3 GB one -- and a button that costs the window
- /// that many frames is a button that freezes it. So the *parse* is the
- /// worker's, whole, and the UI thread only pushes what came back
- /// ([`PlaybackSession::add_subtitle_tracks`]): no borrow crosses the await,
- /// because the parse is an associated fn that owns nothing.
- fn add_subtitles(&mut self, path: &std::path::Path, cx: &mut Context) {
- // Nothing to time the cues against: said now rather than after a walk
- // of a 25 GB file that was never going to be kept.
- if self.session.is_none() {
- self.landed_subtitles(path, None, cx);
- return;
- }
- self.notify_user(format!("READING {} for subtitles…", file_name(path)).into());
- let parsed = cx.background_executor().spawn({
- let path = path.to_path_buf();
- async move { engine::PlaybackSession::parse_subtitles(&path) }
- });
- let path = path.to_path_buf();
- cx.spawn(async move |this, cx| {
- let parsed = parsed.await;
- this.update(cx, |this, cx| {
- // The dedupe lives inside the push, so a second `+ S` on the
- // same file still answers 0 and still says so below.
- let added = this
- .session
- .as_mut()
- .map(|session| parsed.and_then(|tracks| pushed(session, &path, tracks)));
- this.landed_subtitles(&path, added, cx);
- })
- .ok();
- })
- .detach();
- cx.notify();
- }
-
- /// Every subtitle track a `.srt`/`.vtt`/`.ass` carries, onto the timeline
- /// and nowhere else: they are not clips and land on no lane. The cues came
- /// off the worker that read the file ([`read_ahead`]), like every other
- /// door's do, and what is left here is the push. The engine dedupes by
- /// (file, track), so the same `.srt` twice is one row and says so.
- fn take_subtitles(&mut self, path: &std::path::Path, subs: Subs, cx: &mut Context) {
- let added = self
- .session
- .as_mut()
- .map(|session| subs.and_then(|tracks| pushed(session, path, tracks)));
- self.landed_subtitles(path, added, cx);
- }
-
- /// What the timeline says once the tracks are on it, whichever worker did
- /// the reading: the `+ S` button and its key ([`Self::add_subtitles`]), a
- /// dropped or imported subtitle file ([`Self::take_subtitles`]), and a
- /// window with nothing to time cues against all word the outcome here,
- /// once, so no two doors can drift apart.
- fn landed_subtitles(
- &mut self,
- path: &std::path::Path,
- added: Option>,
- cx: &mut Context,
- ) {
- let text = match added {
- Some(Ok(0)) => format!(
- "{}'s subtitles are on the timeline already",
- file_name(path)
- ),
- Some(Ok(n)) => format!(
- "SUBTITLES {} — {n} track(s), showing over the picture, {} hides them",
- file_name(path),
- self.keymap.display(ActionId::ToggleSubtitles)
- ),
- Some(Err(e)) => format!("SUBTITLE IMPORT FAILED: {e}"),
- None => "NO SUBTITLES ADDED — open a file for them to run against first".to_string(),
- };
- eprintln!("{text}");
- self.notify_user(text.into());
- cx.notify();
- }
-
- /// The × on a subtitle row, and the stroke that takes the picked one off:
- /// the track leaves the timeline and the pick moves with it. Every index
- /// past the one that went moves down
- /// ([`engine::Project::remove_subtitles`]), so a pick left where it was
- /// would name a *different* track -- and the pick is what an export writes
- /// into the file.
- ///
- /// Not an undo step: subtitles are not on the history's snapshots, so the
- /// way back is putting the file's subtitles on again -- which is a door of
- /// its own ([`Player::pick_and_add_subtitles`]) and reads the subtitles
- /// alone, never the media. The notice says that rather than promising a
- /// ctrl+z that would do nothing.
- fn remove_subtitle_track(&mut self, track: usize, cx: &mut Context) {
- // The one availability oracle, for the same reason the × on a row and
- // the stroke are one call: an empty list is not a failure, it is an
- // action with nothing to act on, and the engine's "there is no subtitle
- // track 0" is an index nobody typed. A real removal that fails still
- // says what the engine said, below.
- if let Some(why) = self.enable(ActionId::RemoveSubtitleTrack, None).why() {
- let text = format!("NO SUBTITLES REMOVED — {why}");
- eprintln!("{text}");
- self.notify_user(text.into());
- cx.notify();
- return;
- }
- // Read before it goes: a notice naming an index names nothing.
- let name = self
- .session
- .as_ref()
- .and_then(|session| sub_pick_name(session.subtitles(), track))
- .unwrap_or_else(|| format!("subtitle track {track}"));
- let text = match self
- .session
- .as_mut()
- .map(|session| session.remove_subtitles(track))
- {
- Some(Ok(())) => {
- let left = self
- .session
- .as_ref()
- .map_or(0, |session| session.subtitles().len());
- self.sub_track = sub_pick_after_removal(self.sub_track, track, left);
- // The drawn cue is keyed by that index ([`Player::sub_picture`])
- // and the index now stands for another track.
- //
- // corner-cut: its atlas tile is not released -- `close_session`'s
- // note, for its reason and with its upgrade path.
- self.sub_image = None;
- format!(
- "{name} REMOVED — {} puts a file's subtitles back on, the file itself stays off",
- self.keymap.display(ActionId::AddSubtitleTrack)
- )
- }
- Some(Err(e)) => format!("NO SUBTITLES REMOVED — {e}"),
- None => "NO SUBTITLES REMOVED — open a file first".to_string(),
- };
- eprintln!("{text}");
- self.notify_user(text.into());
- cx.notify();
- }
-
- /// Swaps the whole timeline for one restored from a `.edith`, for
- /// [`Player::install_media`]'s reason: the open is a worker's -- a project
- /// naming a 24 GB film opens that film, which is the same twenty seconds
- /// ([`arrival`] sends every `.edith` through the one queue) -- and this is
- /// what is left once it lands. Nothing is replaced until the new session is
- /// in hand, so a refusal is shown as the engine worded it and leaves what is
- /// playing alone.
- fn install_project(
- &mut self,
- path: &std::path::Path,
- opened: Result,
- cx: &mut Context,
- ) {
- if self.exporting().is_some() {
- return;
- }
- let text = match opened {
- Ok(session) => {
- self.fps = session.meta().frame_rate;
- let silent = audio_notice(&session);
- // A project is named after itself but still exports beside its
- // media: that is the only place an export has ever landed.
- self.export_path = retarget(&export_path(&session.sources()[0].path), self.format);
- self.session = Some(session);
- self.apply_volume();
- self.project_path = path.to_path_buf();
- self.name = file_name(path).into();
- // A copied clip names its source by index, which means a
- // different file -- or none -- in another project.
- self.clipboard = None;
- self.selected = None;
- // A menu can be up while a project is dropped on the window --
- // the scrim swallows clicks, never a drop -- and its index
- // would name some other timeline's clip. The two clip cards
- // hold a (lane, idx) of the old timeline for the same reason.
- self.context_menu = None;
- self.eq_open = None;
- self.color_open = None;
- // Marks are timeline frames of the timeline that was.
- self.close_silence();
- // A different set of sources: the row that was picked is not
- // the file that index names any more -- and neither is the
- // subtitle track that was showing.
- self.selected_asset = None;
- self.sub_track = 0;
- // The counters describe one timeline; the eof line must not
- // report the old one's frames against the new one.
- self.displayed = 0;
- self.dropped = 0;
- self.started = None;
- // Loaded paused at its saved playhead, so the still it owes
- // reaches the screen the way a seek's does. The old picture is
- // released by the swap in `pump`, as after any other seek.
- self.reset_after_reseek();
- format!("LOADED {}{}", file_name(path), silent.unwrap_or_default())
- }
- Err(e) => format!("OPEN FAILED: {e}"),
- };
- eprintln!("{text}");
- self.notify_user(text.into());
- cx.notify();
- }
-
- /// Writes the timeline back to its project file. Overwrites silently, like
- /// an export: the path was chosen once and the notice is the confirmation.
- fn save_project(&mut self, cx: &mut Context) {
- let saved = self
- .session
- .as_ref()
- .map(|session| session.save_project(&self.project_path));
- let text = match saved {
- Some(Ok(())) => format!("SAVED {}", file_name(&self.project_path)),
- Some(Err(e)) => format!("SAVE FAILED: {e}"),
- None => "NOTHING TO SAVE — open a file first".to_string(),
- };
- eprintln!("{text}");
- self.notify_user(text.into());
- cx.notify();
- }
-
- /// A new empty track under the ones already there. One undo step in the
- /// engine, so the stroke that takes back an edit takes back a track too, and
- /// no reseek: nothing plays differently until something is dropped on it.
- /// The selection stays -- the lanes it indexes into have not moved.
- fn add_lane(&mut self, kind: LaneKind, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- match &mut self.session {
- Some(session) => {
- let lane = session.add_lane(kind);
- self.notify_user(
- format!(
- "{} ADDED — drag a clip onto it, {} takes it back",
- lane.label(),
- self.keymap.display(ActionId::Undo)
- )
- .into(),
- );
- }
- None => self.notify_user("NO TRACK ADDED — open a file first".into()),
- }
- cx.notify();
- }
-
- /// The × in a track's header: the add taken back, one undo step, and the
- /// engine's own words when it refuses -- those name the clips still on the
- /// track, so the notice says what to delete first. A removal never deletes a
- /// clip.
- ///
- /// Everything holding a `(lane, idx)` is dropped, because the tracks below
- /// the one that went have just moved up an `ord`
- /// ([`engine::Project::remove_lane`]): a selection or an open card kept
- /// across it would be pointing at the *next* track's clip.
- fn remove_lane(&mut self, lane: Lane, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- let removed = self
- .session
- .as_mut()
- .map(|session| session.remove_lane(lane));
- let text = match removed {
- Some(Ok(())) => {
- self.selected = None;
- self.context_menu = None;
- self.eq_open = None;
- self.color_open = None;
- self.speed_open = None;
- self.close_silence();
- format!(
- "{} REMOVED — {} brings it back",
- lane.label(),
- self.keymap.display(ActionId::Undo)
- )
- }
- Some(Err(e)) => format!("NO TRACK REMOVED — {e}"),
- None => "NO TRACK REMOVED — open a file first".to_string(),
- };
- self.notify_user(text.into());
- cx.notify();
- }
-
- /// A header let go over another header: the track in the hand takes that
- /// one's place in the stack, clips and all
- /// ([`engine::Project::move_lane`]), one undo step. The gesture every
- /// editor reorders tracks with, and the only way the order is ever changed
- /// -- there is no second list of it to keep in step.
- ///
- /// Display order is the stack, so moving a video track past another video
- /// track changes which picture wins, here and in an export alike; audio is
- /// summed and does not care, which is what makes `A1` above `V1` a purely
- /// visual arrangement. A label is a position among the tracks of its kind,
- /// so a track that crossed one of its own kind comes back under a different
- /// name -- and everything holding a `(lane, idx)` is dropped exactly then,
- /// for [`Player::remove_lane`]'s reason: those handles now name another
- /// track's clip. A move that crossed only the other kind renames nothing
- /// and keeps the selection.
- fn reorder_lane(&mut self, lane: Lane, onto: Lane, cx: &mut Context) {
- self.lane_drop = None;
- if self.exporting().is_some() {
- return;
- }
- let Some(session) = self.session.as_mut() else {
- return;
- };
- let Some(to) = session.lanes().iter().position(|&l| l == onto) else {
- return;
- };
- // Picked up and put back down where it was is a click, and a click says
- // nothing -- `move_lane` refuses it and every other no-op.
- let Some(moved) = session.move_lane(lane, to) else {
- cx.notify();
- return;
- };
- if moved != lane {
- self.selected = None;
- self.context_menu = None;
- self.eq_open = None;
- self.color_open = None;
- self.speed_open = None;
- self.close_silence();
- }
- self.notify_user(
- format!(
- "{} IS TRACK {} NOW — {} puts it back",
- moved.label(),
- to + 1,
- self.keymap.display(ActionId::Undo)
- )
- .into(),
- );
- cx.notify();
- }
-
- /// What the remove keys act on: the last track of that kind, which is the
- /// one the matching add key appended. Nothing at all before a file is open,
- /// where the timeline drawn is a placeholder pair.
- fn remove_last_lane(&mut self, kind: LaneKind, cx: &mut Context) {
- let last = self.session.as_ref().and_then(|session| {
- session
- .lanes()
- .into_iter()
- .filter(|l| l.kind == kind)
- .next_back()
- });
- match last {
- Some(lane) => self.remove_lane(lane, cx),
- None => {
- self.notify_user("NO TRACK REMOVED — open a file first".into());
- cx.notify();
- }
- }
- }
-
- fn undo(&mut self, cx: &mut Context) {
- if self.session.as_mut().is_some_and(PlaybackSession::undo) {
- self.reset_after_reseek();
- }
- self.selected = None;
- cx.notify();
- }
-
- /// The stroke a waiting row was after: it becomes the whole of what reaches
- /// that action, which is what the row was showing. A chord another action
- /// already holds is refused by the keymap and the row keeps waiting, so the
- /// next stroke is another try rather than a lost one. A binding that took
- /// holds either way:
- /// what a failed write costs is only the next run, which is what the notice
- /// is for.
- fn capture(&mut self, action: ActionId, key: &str, ctrl: bool) {
- let chord = keymap::Chord {
- key: key.to_string(),
- ctrl,
- };
- // Only a stroke the file can spell and read back as itself: gpui reports
- // "+" for shift+=, which is the chord grammar's separator, so binding it
- // would write a line the next load would have to drop. Refused here, in
- // front of the user, rather than silently costing that binding later.
- // The row keeps waiting, as it does for a stroke already taken.
- if !chord.bindable() {
- let text = format!("THAT KEY CANNOT BE BOUND — {}", chord.pretty());
- eprintln!("{text}");
- self.notify_user(text.into());
- return;
- }
- let text = match self.keymap.rebind_action(action, chord.clone()) {
- Ok(()) => {
- self.rebinding = None;
- match self.keymap.save() {
- Ok(()) => return,
- Err(e) => format!(
- "KEYBINDINGS NOT SAVED — {}: {e}",
- Keymap::config_path().display()
- ),
- }
- }
- Err(holder) => format!("ALREADY BOUND — {} is {}", chord.pretty(), holder.label()),
- };
- eprintln!("{text}");
- self.notify_user(text.into());
- }
-
- /// Seeks to where the pointer sits along the ruler. `commit` is the press
- /// and the release, which must land exactly even when the throttle below
- /// would have skipped them.
- fn scrub_to(&mut self, x: Pixels, commit: bool, cx: &mut Context) {
- let Some(session) = &self.session else {
- return;
- };
- // Clamped to the timeline here rather than in the mapping: there is bed
- // past the last frame now, and a seek out there is a seek to the end.
- let t = self
- .scale
- .time_at(px_along(x, self.ruler.get()))
- .clamp(0., session.timeline_duration());
- let target = (t * self.fps) as u32;
- if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) {
- self.last_target = target;
- self.last_scrub = Instant::now();
- self.seek(t, cx);
- }
- }
-
- /// The play binding and the transport button share it: once the timeline is finished
- /// the only sensible "play" is from the top.
- /// Pushes the current volume at the session, which is the only place it is
- /// ever pushed: after a change here, and after a session arrives. A session
- /// starts at full volume, so a file opened while muted has to be told --
- /// that is the whole reason this is not just called from the key handler.
- /// Silent no-op with no timeline, or with a run that has no audio device.
- fn apply_volume(&self) {
- if let Some(session) = &self.session {
- session.set_gain(self.volume.gain());
- }
- }
-
- /// The mute key and the two volume keys, and the click on the button. The
- /// picture is not touched: silencing the output is not pausing it, so the
- /// clock -- which the device still drives -- runs straight through.
- fn set_volume(&mut self, change: impl FnOnce(&mut Volume), cx: &mut Context) {
- change(&mut self.volume);
- self.apply_volume();
- cx.notify();
- }
-
- /// Where the pointer sits along the slider, as a level. The press and every
- /// sample after it come here, so the sound follows the hand rather than the
- /// release -- there is nothing to undo about a monitoring level, which is
- /// why this writes live and keeps no gesture state beyond the flag.
- fn drag_volume(&mut self, x: Pixels, cx: &mut Context) {
- let along = frac_along(x, self.volume_bar.get());
- self.set_volume(|volume| volume.set_along(along), cx);
- }
-
- /// One sample of whatever drag is in the hand: the equalizer's handle, a
- /// clip's edge, a colour bar, the speed bar, the volume slider or the
- /// playhead. Each of those starts on a strip a few pixels wide that the
- /// pointer leaves immediately, so none of them can be tracked from the
- /// element it started on -- the gesture is followed here instead, on a
- /// hitbox that covers everything the hand can reach.
- ///
- /// Registered on the root *and* on the scrim of every card that holds a
- /// slider ([`Player::drag_scrim`]). An occluding sheet ends gpui's hit test
- /// where it sits (`Hitbox::is_hovered`, window.rs:788), so while a card is
- /// up the root is not hovered anywhere under it and hears none of this: the
- /// press set a value and the drag then froze on it.
- fn drag_move(&mut self, event: &MouseMoveEvent, _: &mut Window, cx: &mut Context) {
- // A handle is 10 px across and the pointer leaves it at once, so
- // the equalizer drag is tracked here for the ruler's reason.
- if self.eq_dragging {
- if event.pressed_button == Some(MouseButton::Left) {
- self.drag_band(event.position, cx);
- } else {
- // Released outside the window: the up below never came,
- // so this is where the gesture ends -- and it still owes
- // the one write the whole drag is worth.
- self.eq_dragging = false;
- self.commit_eq(cx);
- }
- return;
- }
- // A clip edge is 6 px wide and the pointer leaves it on the
- // first drag, so the gesture is tracked here for the same
- // reason -- and it ends here too when the button came up
- // outside the window, still owing its one edit.
- if self.trim.is_some() {
- match event.pressed_button {
- Some(MouseButton::Left) => self.trim_to(event.position.x, cx),
- _ => self.commit_trim(cx),
- }
- return;
- }
- // A colour slider is 4 px tall and the pointer leaves it just as
- // fast; every sample is live, so the release owes no write of
- // its own -- what the last sample set is what the clip carries.
- if self.color_dragging {
- if event.pressed_button == Some(MouseButton::Left) {
- self.drag_color(event.position.x, false, cx);
- } else {
- // The release happened outside the window, so this is
- // where the gesture ends -- and it may not end on a
- // sample the worker was too busy to take.
- self.color_dragging = false;
- self.flush_drag(cx);
- }
- return;
- }
- // The speed bar, the same 4 px and the same live writes: the
- // press took the undo step and every sample since is live.
- if self.speed_dragging {
- if event.pressed_button == Some(MouseButton::Left) {
- self.drag_speed(event.position.x, false, cx);
- } else {
- self.speed_dragging = false;
- self.flush_drag(cx);
- }
- return;
- }
- // The volume slider, the same live writes: what the hand is on
- // is what the speakers are doing, and there is nothing to undo.
- if self.volume_dragging {
- if event.pressed_button == Some(MouseButton::Left) {
- self.drag_volume(event.position.x, cx);
- } else {
- self.volume_dragging = false;
- }
- return;
- }
- if !self.scrubbing {
- return;
- }
- if event.pressed_button == Some(MouseButton::Left) {
- self.scrub_to(event.position.x, false, cx);
- } else {
- // A release outside the window never reaches the handler
- // below, so the first button-up move is when we learn the
- // drag is over. Without this the next hover would scrub.
- self.scrubbing = false;
- }
- }
-
- /// Where a drag ends: the release lands exactly, and whatever the gesture
- /// owes -- one undo step for the equalizer and the trim, a flush for the
- /// live-writing bars -- is paid here. On the root and on a card's scrim
- /// both, for [`Player::drag_move`]'s reason: a release over an open card
- /// never reaches the root.
- fn drag_release(&mut self, event: &MouseUpEvent, _: &mut Window, cx: &mut Context) {
- if std::mem::take(&mut self.eq_dragging) {
- // The release lands exactly, then the gesture is written
- // once -- the append-only table's whole reason.
- self.drag_band(event.position, cx);
- self.commit_eq(cx);
- return;
- }
- if self.trim.is_some() {
- // The release lands exactly, then the gesture is
- // written once -- one edit, one undo step.
- self.trim_to(event.position.x, cx);
- self.commit_trim(cx);
- return;
- }
- if std::mem::take(&mut self.color_dragging) {
- // The release lands exactly where the hand let go, and
- // it is a live write like every other sample: the undo
- // step the gesture rolls back to was the press's. The
- // flush is what makes "exactly" true while the worker is
- // still busy -- the sample above would only be held.
- self.drag_color(event.position.x, false, cx);
- self.flush_drag(cx);
- return;
- }
- if std::mem::take(&mut self.speed_dragging) {
- self.drag_speed(event.position.x, false, cx);
- self.flush_drag(cx);
- return;
- }
- if std::mem::take(&mut self.volume_dragging) {
- self.drag_volume(event.position.x, cx);
- return;
- }
- if std::mem::take(&mut self.scrubbing) {
- self.scrub_to(event.position.x, true, cx);
- }
- }
-
- fn toggle_or_restart(&mut self, cx: &mut Context) {
- if self.exporting().is_some() {
- return;
- }
- // Nothing to play is a message, not a transport state. An empty
- // timeline is [`Transport::Ended`] from its one black frame onward, so
- // the restart below would start a clock against a zero-length timeline
- // -- and it is Ended again by the next repaint, so no later press could
- // ever stop it: the button would read "Pause" and never pause. A delete
- // can empty the timeline mid-play, and that press must still stop it.
- if nothing_to_play(self.session.as_ref()) {
- match self.session.as_mut().filter(|s| s.is_playing()) {
- Some(session) => session.pause(),
- None => self.notify_user(NOTHING_TO_PLAY.into()),
- }
- cx.notify();
- return;
- }
- // Pressing play is asking to watch, so a view scrolled away while
- // paused comes back to the head with it -- as a seek's does.
- self.panned = false;
- match self.transport() {
- // Nothing open: the button is dimmed and the key says nothing.
- Transport::Stopped => {}
- // Back to the top and away, for the key and the button alike --
- // whichever asked, the transport was showing Play.
- state if state.restarts() => {
- self.seek(0., cx);
- if let Some(session) = &mut self.session {
- session.play();
- }
- }
- _ => {
- if let Some(session) = &mut self.session {
- session.toggle();
- // A paused timeline animates nothing; this is the repaint
- // that puts the new glyph up.
- cx.notify();
- }
- }
- }
- }
-
- /// The export that owns the UI, if any. A cancelled one does not: it has
- /// its own copy of the edit list and owes only its own cleanup.
- fn exporting(&self) -> Option<&ExportHandle> {
- self.export.as_ref().filter(|_| !self.cancelling)
- }
-
- /// What the export action does now: opens the card, which is where the
- /// quality, the destination and the decision to write at all are. Nothing
- /// is encoded until the button in it is pressed.
- fn open_export(&mut self, cx: &mut Context) {
- if self.export.is_some() {
- return;
- }
- // Nothing to write out, and a refusal rather than a card about it: the
- // window is empty and the export path is not even chosen yet.
- if self.session.is_none() {
- self.notify_user("NOTHING TO EXPORT — open a file first".into());
- cx.notify();
- return;
- }
- self.export_open = true;
- // One card at a time, and a waiting row must not outlive the card it
- // was waiting in. Nor may a half-typed number: the card opens on the
- // bitrate it will write, never on digits left behind by a closed one.
- self.keys_open = false;
- self.rebinding = None;
- self.mbps_edit = None;
- cx.notify();
- }
-
- /// A format row was clicked. The destination follows it at once -- a WAV
- /// written to a path ending in `.mp4` is a file every player will lie
- /// about -- keeping whatever stem the save dialog last left there.
- fn set_format(&mut self, format: Format) {
- // The one door both the row and its initial go through, so a format the
- // card greys out cannot be picked by keyboard either.
- if let Some(why) = self
- .session
- .as_ref()
- .and_then(|session| format_refusal(session, format))
- {
- self.notify_user(format!("NOT {} — {why}", format_label(format)).into());
- return;
- }
- self.format = format;
- self.export_path = retarget(&self.export_path, format);
- }
-
- /// The container row: the same codec in the other box, which retargets the
- /// destination exactly as picking a codec does -- and does nothing at all
- /// for a codec with only one box, so the stroke cannot invent a choice the
- /// card is not offering.
- fn cycle_container(&mut self) {
- self.set_format(next_container(self.format));
- }
-
- /// The quality rows by keyboard, wrapping. Refused by name where the format
- /// has no bitrate to pick: a key that silently does nothing is the card
- /// looking broken.
- fn cycle_quality(&mut self) {
- if let Some(why) = bitrate_refusal(self.format) {
- self.notify_user(why.into());
- return;
- }
- let at = Quality::ALL
- .iter()
- .position(|&q| q == self.quality)
- .unwrap_or(0);
- self.quality = Quality::ALL[(at + 1) % Quality::ALL.len()];
- }
-
- /// The sound's rate by keyboard, wrapping through the offered ones -- the
- /// picture's quality row for the other half of the file. Refused by name
- /// where this timeline in this format has no rate to pick, exactly as
- /// [`Player::cycle_quality`] is: a key that silently does nothing is the
- /// card looking broken.
- fn cycle_audio_kbps(&mut self) {
- if let Some(why) = self.audio_rate_refusal() {
- self.notify_user(why.into());
- return;
- }
- self.audio_kbps = next_audio_kbps(self.audio_kbps);
- }
-
- /// Why the sound row is not a choice right now, the engine answering about
- /// the very project it would export. No session is the same answer as no
- /// sound: there is nothing to write either way.
- fn audio_rate_refusal(&self) -> Option<&'static str> {
- match &self.session {
- Some(session) => session.audio_rate_refusal(self.format),
- None => Some("no sound to write"),
- }
- }
-
- /// The custom bitrate by pointer: the typed digits were the only control in
- /// this card a mouse could not reach. Clamped to the range the row states
- /// (the engine's own 1..50 Mbps), and picking the row is part of the step --
- /// a stepper that moves a number nobody is using would move nothing.
- fn nudge_mbps(&mut self, step: i32) {
- self.custom_mbps =
- (self.custom_mbps as i32 + step).clamp(MBPS_MIN as i32, MBPS_MAX as i32) as u32;
- self.quality = Quality::Custom;
- }
-
- /// The same number under the wheel, one step a notch, up for more: fifty
- /// presses of a stepper is not a way to reach the top of this range, and the
- /// wheel is what this editor already moves a value with (the timeline's
- /// zoom and scroll are the same gesture). Hold-to-run stays the keyboard's,
- /// as it is on every other card here -- a button that repeats while held is
- /// not a thing this program has.
- ///
- /// It moves the *field* while one is open, exactly as ↑↓ do, so the two
- /// ways in never disagree about which number is being changed.
- fn wheel_mbps(&mut self, event: &ScrollWheelEvent) {
- let by = wheel_delta(event);
- if by == 0. {
- return;
- }
- let by = by.signum() as i32;
- match &mut self.mbps_edit {
- Some(edit) => edit.step(by),
- None => self.nudge_mbps(by),
- }
- }
-
- /// Opens the custom bitrate's field on the number the row is carrying, and
- /// picks the row while it is at it: a field typed into is the row being
- /// chosen, and a number nobody is using would be a number typed at nothing.
- /// Nothing is committed here -- until enter, the card still exports at the
- /// bitrate it had.
- fn edit_mbps(&mut self) {
- self.quality = Quality::Custom;
- self.mbps_edit = Some(NumberEdit::new(self.custom_mbps));
- }
-
- /// The card's Destination row: the desktop's save dialog, on a background
- /// thread like the import chooser -- the user may sit in it and the window
- /// behind must not freeze. No chooser at all leaves the default path, which
- /// is what the refusal says.
- fn pick_destination(&mut self, cx: &mut Context) {
- let default = self.export_path.clone();
- let picked = cx
- .background_executor()
- .spawn(async move { pick_save(&default) });
- cx.spawn(async move |this, cx| {
- let picked = picked.await;
- this.update(cx, |this, cx| {
- // The dialog outlives the card: an export started meanwhile
- // took the old path and its notice must name what it wrote.
- if this.export.is_some() {
- return;
- }
- match picked {
- // The stem is the user's, the extension is the format's: a
- // FLAC named `.mp4` is a file every player lies about.
- Ok(Some(path)) => this.export_path = retarget(&path, this.format),
- // Cancelled: the default stands, as it did before.
- Ok(None) => {}
- Err(text) => {
- eprintln!("{text}");
- this.notify_user(text.into());
- }
- }
- cx.notify();
- })
- .ok();
- })
- .detach();
- }
-
- /// The subtitle tracks an export of this timeline carries: every one with a
- /// cue left in the exported range ([`PlaybackSession::timeline_cues`], the
- /// very map the file is written from), in the library's own order.
- ///
- /// Worked out from the cues each time rather than kept as a pick, which is
- /// what makes it impossible to desync: a row added or taken off shifts every
- /// index after it, and a stored list would then name tracks nobody chose.
- /// `Player::sub_track` stays what it always was -- which track the *overlay*
- /// draws -- and has no say here.
- ///
- /// The honest input and not the final answer: the engine filters it again
- /// per track (a track that could not be read, a picture one) and says so in
- /// the card's own words ([`engine::export::planned_subtitles`]).
- fn export_subs(&self) -> Vec {
- let Some(session) = self.session.as_ref() else {
- return Vec::new();
- };
- (0..session.subtitles().len())
- .filter(|&i| !session.timeline_cues(i).is_empty())
- .collect()
- }
-
- /// That list in the card's words ([`subtitle_plan`]): what travels, and the
- /// reason beside every track that does not -- including the ones
- /// [`Self::export_subs`] filtered out before the engine ever saw them.
- fn subtitle_line(&self) -> String {
- let Some(session) = self.session.as_ref() else {
- return "none".to_string();
- };
- let picks = self.export_subs();
- let plan = session.planned_subtitles(self.format, picks.iter().copied());
- match self.format.has_video() {
- true => subtitle_plan(plan, session.subtitles(), &picks),
- // A format that is the sound alone has nowhere to put any of them
- // and the engine says that once, about the file. Naming the cues of
- // each track under it answers a question the format already closed.
- false => plan,
- }
- }
-
- /// Writes the edit list out, at the settings the card was left at. Playback
- /// stops first: the exporter opens its own decoder -- and, on the hardware
- /// path, an encoder -- so a running player would only compete with it for
- /// the GPU. A cancelled export still winding down holds this off for the
- /// frame it takes to notice, which is what keeps its `remove_file` off the
- /// new output.
- fn start_export(&mut self, cx: &mut Context) {
- if self.export.is_some() {
- return;
- }
- let mut settings =
- export_settings(self.quality, self.custom_mbps, self.format, self.audio_kbps);
- // Whatever is on the timeline travels -- every track with a cue in the
- // exported range, not the one row the overlay happens to be drawing.
- // Set here rather than inside `export_settings`, which the card also
- // calls for the *estimate* and which nothing else needs a subtitle for.
- settings.subtitles = self.export_subs();
- let Some(session) = &mut self.session else {
- self.notify_user("NOTHING TO EXPORT — open a file first".into());
- cx.notify();
- return;
- };
- // An emptied timeline is a timeline; it is simply not a file. Refused by
- // name here rather than written as a project of no frames -- and the
- // engine refuses it again on the worker (`export::start`), so a caller
- // that is not this button cannot get past it either. Two fences on
- // purpose: this one is the one with a keystroke to blame.
- if session.is_empty() {
- self.notify_user("NOTHING TO EXPORT — the timeline is empty".into());
- cx.notify();
- return;
- }
- // The format row can be refused *after* it was picked -- mp4 is the
- // default and an audio-only timeline (or a second audio lane) is one
- // edit away -- so the button asks again rather than starting a worker
- // that will only settle with the same refusal minutes later.
- if let Some(why) = format_refusal(session, self.format) {
- self.notify_user(format!("NOT EXPORTED — {why}").into());
- cx.notify();
- return;
- }
- session.pause();
- self.export = Some(session.export_to_with(&self.export_path, &settings));
- // The clock starts with the worker, not with the first repaint that
- // happens to notice it.
- self.export_started = Some(Instant::now());
- self.export_marks.clear();
- // The card has been answered; the progress line takes the panel from
- // here, and it is the running export's escape that matters now.
- self.export_open = false;
- cx.notify();
- }
-
- /// Gives the editor back at once and leaves the worker to stop at its next
- /// frame and delete what it has written.
- fn cancel_export(&mut self) {
- if let Some(export) = &self.export {
- export.cancel();
- self.cancelling = true;
- }
- }
-
- /// Takes the export's verdict once it has one. The only place the app
- /// touches the handle's completion side.
- fn poll_export(&mut self) {
- // Sampled here rather than while drawing: a repaint stays a repaint,
- // and this runs once per repaint either way.
- if let (Some(progress), Some(started)) = (
- self.exporting().map(ExportHandle::progress),
- self.export_started,
- ) {
- note_progress(
- &mut self.export_marks,
- started.elapsed().as_secs_f32(),
- progress,
- );
- }
- let Some(result) = self.export.as_ref().and_then(ExportHandle::result) else {
- return;
- };
- self.export = None;
- self.export_started = None;
- // A cancellation is reported as an error, and the one who asked for it
- // has had the editor back since the keystroke. Nothing to say.
- if std::mem::take(&mut self.cancelling) {
- return;
- }
- let text = match result {
- Ok(()) => {
- // Written and still where it was written: the bar carries it
- // until some other notice takes the bar.
- self.exported = Some(self.export_path.clone());
- format!("{EXPORT_DONE}{}", file_name(&self.export_path))
- }
- Err(e) => format!("EXPORT FAILED: {e}"),
- };
- eprintln!("{text}");
- self.notify_user(text.into());
- }
-}
-
impl Render for Player {
fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement {
// A file that has just been opened sits on its first frame with the
diff --git a/crates/app/src/player/actions.rs b/crates/app/src/player/actions.rs
new file mode 100644
index 0000000..4032130
--- /dev/null
+++ b/crates/app/src/player/actions.rs
@@ -0,0 +1,382 @@
+//! Every action arrives here: the one dispatch, the notice queue, and the
+//! delegations to the oracle that say whether an action can be asked for.
+
+use crate::*;
+
+impl Player {
+ /// What an action does, wherever it was asked for -- a stroke, or the clip
+ /// menu item that names the same action. One table, so the two can never
+ /// come to mean different things.
+ pub(crate) fn act(&mut self, action: ActionId, cx: &mut Context) {
+ // Two doors, one oracle. This used to be the asymmetry the whole
+ // toolbar was built on: the buttons dimmed themselves off
+ // [`enable`] while the keyboard walked straight past it, so with no
+ // file open `s` toggled the snap and `v` added a track while the very
+ // same controls sat dim and *dead* to the pointer. Whatever refuses the
+ // button refuses the key, in the oracle's own words -- and a refusal
+ // that is silent from the keyboard is a bug the same size.
+ match self.enable(action, None) {
+ Enable::Yes => {}
+ // A state refusal is spoken: the thing exists and cannot happen
+ // *now*, which is exactly what a silent key press fails to say.
+ Enable::No(why) => {
+ self.notify_user(format!("{} — {why}", action.label()).into());
+ cx.notify();
+ return;
+ }
+ // A class refusal is not: the action does not exist for what is in
+ // front of the user, and `esc` with nothing exporting must not
+ // answer with a line about exports.
+ Enable::Hidden(_) => return,
+ }
+ match action {
+ ActionId::Play => self.toggle_or_restart(cx),
+ ActionId::StepBack => self.step(-1, cx),
+ ActionId::StepForward => self.step(1, cx),
+ // A second is however many frames this timeline runs at.
+ ActionId::JumpBack => self.step(-(self.fps.round() as i64), cx),
+ ActionId::JumpForward => self.step(self.fps.round() as i64, cx),
+ // The ends, as a step nothing can be far enough from.
+ ActionId::GoStart => self.step(i64::MIN, cx),
+ ActionId::GoEnd => self.step(i64::MAX, cx),
+ // Not a step at all: the grid these land on is the *source's*, and
+ // where the next one is depends on the file rather than on the rate.
+ ActionId::PrevSyncPoint => self.jump_sync(false, cx),
+ ActionId::NextSyncPoint => self.jump_sync(true, cx),
+ ActionId::Export => self.open_export(cx),
+ ActionId::Save => self.save_project(cx),
+ ActionId::Copy => self.copy_selected(),
+ ActionId::Paste => self.paste(cx),
+ ActionId::Cut => self.cut(cx),
+ ActionId::Regroup => self.regroup(cx),
+ ActionId::Detach => self.detach(cx),
+ ActionId::Group => self.group(cx),
+ ActionId::Select => self.select_under_playhead(cx),
+ ActionId::SelectNext => self.select_step(true, cx),
+ ActionId::SelectPrev => self.select_step(false, cx),
+ ActionId::Delete => self.delete_selected(cx),
+ ActionId::Lift => self.lift_selected(cx),
+ ActionId::Color => self.open_color(cx),
+ ActionId::Fit => self.cycle_fit(cx),
+ ActionId::Resolution => self.cycle_resolution(cx),
+ // The playhead is what a key zoom is aimed at: it is the one place
+ // on the timeline the user is certainly looking at, and keeping it
+ // still is what every editor does.
+ ActionId::ZoomIn => self.zoom(ZOOM_STEP, None, cx),
+ ActionId::ZoomOut => self.zoom(1. / ZOOM_STEP, None, cx),
+ ActionId::ZoomFit => self.zoom_fit(cx),
+ ActionId::Undo => self.undo(cx),
+ ActionId::AddVideoLane => self.add_lane(LaneKind::Video, cx),
+ ActionId::AddAudioLane => self.add_lane(LaneKind::Audio, cx),
+ // The last track of that kind: the one the add key put there, so the
+ // two strokes undo each other press for press. Any other track goes
+ // through the × in its own header.
+ ActionId::RemoveVideoLane => self.remove_last_lane(LaneKind::Video, cx),
+ ActionId::RemoveAudioLane => self.remove_last_lane(LaneKind::Audio, cx),
+ // The same chooser the + S button opens, and the picked row -- the
+ // one the panel draws highlighted -- for the removal: the × on any
+ // other row is that row's own door, and both doors are one call.
+ ActionId::AddSubtitleTrack => self.pick_and_add_subtitles(cx),
+ ActionId::RemoveSubtitleTrack => self.remove_subtitle_track(self.sub_track, cx),
+ ActionId::ToggleMute => self.set_volume(|volume| volume.muted = !volume.muted, cx),
+ ActionId::VolumeUp => self.set_volume(|volume| volume.step(true), cx),
+ ActionId::VolumeDown => self.set_volume(|volume| volume.step(false), cx),
+ ActionId::Equalizer => self.open_eq(cx),
+ ActionId::Speed => self.open_speed(cx),
+ ActionId::Silence => self.open_silence(cx),
+ ActionId::Mix => self.open_mix(None, cx),
+ ActionId::ToggleSnap => self.toggle_snap(cx),
+ ActionId::ToggleSubtitles => self.toggle_subtitles(cx),
+ // The keyboard's door to the same list the toolbar button opens.
+ // At the window's corner, since a stroke names no place -- and
+ // [`menu_at`] keeps it on screen from there.
+ ActionId::Theme => self.open_picker(Pick::Theme, Point::default(), cx),
+ // Nothing to cancel while nothing is exporting; the export guard in
+ // the key handler is what answers this one while there is.
+ ActionId::CancelExport => {}
+ ActionId::ShowActions => self.show_actions(cx),
+ }
+ }
+
+ /// Says something to the user. The one door: every message in this editor
+ /// comes through here, so "queued rather than overwritten" is a property of
+ /// the field and not of seventy call sites remembering to be polite.
+ ///
+ /// A repeat of what is already at the back is dropped -- holding a key that
+ /// refuses would otherwise fill the queue with one sentence, and the count
+ /// on the bar would be a count of how long the key was held.
+ pub(crate) fn notify_user(&mut self, message: SharedString) {
+ push_notice(&mut self.notices, message);
+ }
+
+ /// Answers the message on the bar and brings up the next one. Whether there
+ /// was one to answer, because a key that dismissed a notice owes a repaint
+ /// and a key that dismissed nothing does not.
+ pub(crate) fn dismiss_notice(&mut self) -> bool {
+ self.notices.pop_front().is_some()
+ }
+
+ /// The magnet off and on again, in words: a snap that stops working
+ /// silently reads as a bug, and one that starts working silently reads as
+ /// one too. The line goes with it -- nothing is being promised any more.
+ pub(crate) fn toggle_snap(&mut self, cx: &mut Context) {
+ self.snap = !self.snap;
+ self.snap_cue = None;
+ self.ghost = None;
+ self.notify_user(match self.snap {
+ true => "SNAP ON — drags land on clip edges, the playhead and the start".into(),
+ false => "SNAP OFF — drags land exactly where the hand leaves them".into(),
+ });
+ cx.notify();
+ }
+
+ /// The actions card, from its key, from the panel button, or from its own
+ /// row: open, with an empty search box -- a card that opens showing the
+ /// last search would hide most of the list for a reason nobody remembers.
+ pub(crate) fn show_actions(&mut self, cx: &mut Context) {
+ self.keys_open = true;
+ self.keys_search.clear();
+ self.scroll_keys(None);
+ self.rebinding = None;
+ // One card at a time, the rule the other cards follow.
+ self.export_open = false;
+ cx.notify();
+ }
+
+ /// Moves the actions card's row list by `by` pixels, or puts it back at the
+ /// top (`None`). Back to the top after every keystroke that changes the
+ /// search: a filtered list is shorter than the offset a scrolled one left
+ /// behind, and a card showing the empty space past its last row reads as a
+ /// search that found nothing.
+ ///
+ /// Clamped to what there is to scroll, so the list cannot be pushed off
+ /// either end -- `max_offset` is what the last paint measured, which is the
+ /// only place that number exists.
+ pub(crate) fn scroll_keys(&self, by: Option) {
+ let at = match by {
+ Some(by) => (f32::from(self.keys_scroll.offset().y) + by)
+ .clamp(-f32::from(self.keys_scroll.max_offset().height), 0.),
+ None => 0.,
+ };
+ self.keys_scroll.set_offset(point(px(0.), px(at)));
+ }
+
+ /// The cues over the picture, off and on. Says which it is now *and* what is
+ /// on screen while they are on: a toggle whose answer is invisible when the
+ /// playhead happens to sit between two cues would read as broken.
+ pub(crate) fn toggle_subtitles(&mut self, cx: &mut Context) {
+ self.subs_on = !self.subs_on;
+ // Named with its film here too: a notice saying "SUBTITLES ON — eng"
+ // over a timeline holding two films' eng tracks names neither.
+ let label = self
+ .session
+ .as_ref()
+ .and_then(|session| sub_pick_name(session.subtitles(), self.sub_track))
+ .unwrap_or_else(|| "nothing imported".to_string());
+ self.notify_user(
+ match self.subs_on {
+ true => format!("SUBTITLES ON — {label}"),
+ false => format!("SUBTITLES OFF — {label} is still on the timeline"),
+ }
+ .into(),
+ );
+ cx.notify();
+ }
+
+ /// The subtitle track the overlay and the strip are showing: the one a
+ /// library row picked, or the first there is. `None` with no timeline and
+ /// for an index left over from one that is gone.
+ pub(crate) fn subtitle_track(&self) -> Option<&engine::subtitle::SubtitleTrack> {
+ self.session.as_ref()?.subtitles().get(self.sub_track)
+ }
+
+ /// Whether the editor can be asked for `action` right now, and why not when
+ /// it cannot. `on` is the clip the question is about -- the one a clip menu
+ /// was opened on -- and `None` asks about the marked clip instead, which is
+ /// what a menu that hangs over no clip in particular means by "this one".
+ ///
+ /// The player's half of [`enable`]: it reads the state, the table decides.
+ pub(crate) fn enable(&self, action: ActionId, on: Option<(Lane, usize)>) -> Enable {
+ enable(action, self.ctx(on))
+ }
+
+ /// The state every one of those questions is asked against, read off the
+ /// player once: [`menu_items`] filters a whole menu with it, so the rows a
+ /// menu draws and the answers it dims them by come from the same reading.
+ pub(crate) fn ctx(&self, on: Option<(Lane, usize)>) -> Ctx {
+ let Some(session) = &self.session else {
+ return Ctx::default();
+ };
+ let clip = on
+ .or(self.selected)
+ .and_then(|(lane, idx)| session.lane_clips(lane).get(idx).map(|clip| (*clip, lane)));
+ Ctx {
+ clip,
+ image: clip.is_some_and(|(clip, _)| {
+ session
+ .sources()
+ .get(clip.source)
+ .is_some_and(|s| engine::is_image(&s.path))
+ }),
+ playhead: frame_at(session.now(), self.fps),
+ timeline: true,
+ clipboard: self.clipboard.is_some(),
+ subtitles: !session.subtitles().is_empty(),
+ playable: !nothing_to_play(Some(session)),
+ exporting: self.exporting().is_some(),
+ }
+ }
+
+ /// The same reading for a library row: whether this file can join this
+ /// timeline -- the very answer the list greys the row by, so the menu over a
+ /// row and the row under it cannot disagree -- and how many clips play it.
+ /// [`Player::ctx`] for the other panel.
+ pub(crate) fn row_ctx(&self, path: &Path, stream: usize) -> RowCtx {
+ let placed = self.session.as_ref().map_or(0, |session| {
+ let of_row = session
+ .sources()
+ .iter()
+ .position(|s| s.path == path && s.audio_stream == stream);
+ of_row.map_or(0, |idx| {
+ session
+ .lanes()
+ .into_iter()
+ .flat_map(|lane| session.lane_clips(lane))
+ .filter(|c| c.source == idx)
+ .count()
+ })
+ });
+ let sources = self
+ .session
+ .as_ref()
+ .map_or(&[][..], PlaybackSession::sources);
+ RowCtx {
+ timeline: self.session.is_some(),
+ exporting: self.exporting().is_some(),
+ usable: library_rows(
+ sources,
+ &self.streams,
+ &self.decoders,
+ self.timeline_audio(),
+ |path| {
+ self.session
+ .as_ref()
+ .map_or(0, |session| session.file_frames(path))
+ },
+ )
+ .iter()
+ .any(|row| row.path == path && row.stream == stream && row.unusable.is_none()),
+ placed,
+ }
+ }
+
+ /// Which of [`Repeat`]'s three the window is in, for the hold gate at the
+ /// top of the key handler. Not [`Player::modal`]: that asks whether an
+ /// overlay is up at all, and here the cards with sliders in them are
+ /// exactly the ones that answer differently from the keys menu and the
+ /// export card.
+ pub(crate) fn repeat_scope(&self) -> Repeat {
+ // A number being typed is a value under the arrows, exactly as a card's
+ // slider is -- so a held arrow runs it. Asked before the export card
+ // below, which otherwise repeats nothing.
+ if self.mbps_edit.is_some() {
+ Repeat::Card
+ } else if self.rebinding.is_some()
+ || self.keys_open
+ || self.export_open
+ || self.exporting().is_some()
+ {
+ Repeat::Nothing
+ } else if self.eq_open.is_some()
+ || self.color_open.is_some()
+ || self.speed_open.is_some()
+ || self.silence_open.is_some()
+ || self.mix_open
+ {
+ Repeat::Card
+ } else {
+ Repeat::Keymap
+ }
+ }
+
+ /// One item of a library row's menu, done. Every one of them closes the
+ /// menu first -- the list under it is about to be rebuilt -- except the one
+ /// that turns the card over.
+ pub(crate) fn act_on_row(&mut self, item: RowItem, cx: &mut Context) {
+ let Some(menu) = self.library_menu.clone() else {
+ return;
+ };
+ match item {
+ RowItem::Properties => {
+ if let Some(open) = &mut self.library_menu {
+ open.details = true;
+ }
+ }
+ RowItem::Add => {
+ self.library_menu = None;
+ self.insert_source(&menu.path, menu.stream, None, None, cx);
+ }
+ RowItem::Remove => {
+ self.library_menu = None;
+ self.remove_source(&menu.path, menu.stream, cx);
+ }
+ RowItem::Reveal => {
+ self.library_menu = None;
+ // Another process starting: off the UI thread, exactly as the
+ // export notice's own click starts it.
+ cx.background_executor()
+ .spawn(async move { show_in_file_manager(&menu.path) })
+ .detach();
+ }
+ }
+ cx.notify();
+ }
+
+ pub(crate) fn undo(&mut self, cx: &mut Context) {
+ if self.session.as_mut().is_some_and(PlaybackSession::undo) {
+ self.reset_after_reseek();
+ }
+ self.selected = None;
+ cx.notify();
+ }
+
+ /// The stroke a waiting row was after: it becomes the whole of what reaches
+ /// that action, which is what the row was showing. A chord another action
+ /// already holds is refused by the keymap and the row keeps waiting, so the
+ /// next stroke is another try rather than a lost one. A binding that took
+ /// holds either way:
+ /// what a failed write costs is only the next run, which is what the notice
+ /// is for.
+ pub(crate) fn capture(&mut self, action: ActionId, key: &str, ctrl: bool) {
+ let chord = keymap::Chord {
+ key: key.to_string(),
+ ctrl,
+ };
+ // Only a stroke the file can spell and read back as itself: gpui reports
+ // "+" for shift+=, which is the chord grammar's separator, so binding it
+ // would write a line the next load would have to drop. Refused here, in
+ // front of the user, rather than silently costing that binding later.
+ // The row keeps waiting, as it does for a stroke already taken.
+ if !chord.bindable() {
+ let text = format!("THAT KEY CANNOT BE BOUND — {}", chord.pretty());
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ return;
+ }
+ let text = match self.keymap.rebind_action(action, chord.clone()) {
+ Ok(()) => {
+ self.rebinding = None;
+ match self.keymap.save() {
+ Ok(()) => return,
+ Err(e) => format!(
+ "KEYBINDINGS NOT SAVED — {}: {e}",
+ Keymap::config_path().display()
+ ),
+ }
+ }
+ Err(holder) => format!("ALREADY BOUND — {} is {}", chord.pretty(), holder.label()),
+ };
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ }
+}
diff --git a/crates/app/src/player/cards.rs b/crates/app/src/player/cards.rs
new file mode 100644
index 0000000..7f429ba
--- /dev/null
+++ b/crates/app/src/player/cards.rs
@@ -0,0 +1,1102 @@
+//! The cards the window opens over itself: the pickers, colour, speed,
+//! silence, the mixer and the equalizer.
+
+use crate::*;
+
+impl Player {
+ /// Cycles the *project's* resolution through [`RESOLUTIONS`], starting from
+ /// the media's own -- the one size that must stay reachable, since a project
+ /// moved off it has no other way back (the resolution is not an undo step).
+ /// Every clip is recomposed onto it, so this is what makes "the project
+ /// resolution and the media's are different things" a thing a user can see.
+ pub(crate) fn cycle_resolution(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let Some(session) = &mut self.session else {
+ self.notify_user("no timeline to resize — open a file first".into());
+ cx.notify();
+ return;
+ };
+ let (width, height) = next_resolution(session.resolution(), session.native_resolution());
+ self.apply_resolution(width, height, cx);
+ }
+
+ /// The project resized, whichever asked: the stroke that steps to the next
+ /// size and the list that names one outright come through here.
+ pub(crate) fn apply_resolution(&mut self, width: u32, height: u32, cx: &mut Context) {
+ if let Some(session) = &mut self.session
+ && session.set_resolution(width, height)
+ {
+ self.notify_user(format!("PROJECT: {width}x{height}").into());
+ self.reset_after_reseek();
+ }
+ cx.notify();
+ }
+
+ /// The project cut at another rate: the list names one and this is where it
+ /// happens, the way [`apply_resolution`](Self::apply_resolution) is for a
+ /// size. The whole timeline is conformed to it by the engine
+ /// ([`PlaybackSession::set_frame_rate`]) -- same seconds, same footage --
+ /// and the rate the app itself counts frames in follows, since every
+ /// timecode, ruler mark and step key here is measured in it.
+ pub(crate) fn apply_frame_rate(&mut self, fps: f64, cx: &mut Context) {
+ if let Some(session) = &mut self.session
+ && session.set_frame_rate(fps)
+ {
+ self.fps = session.meta().frame_rate;
+ self.notify_user(format!("PROJECT: {} fps", fps_label(fps)).into());
+ self.reset_after_reseek();
+ }
+ cx.notify();
+ }
+
+ /// The project's HDR media shown another way: the list names a rendition and
+ /// this is where it happens, the way [`apply_resolution`](Self::apply_resolution)
+ /// is for a size. The engine remaps the frame under the playhead at once
+ /// ([`PlaybackSession::set_tone`]), so the picture on screen is the picked
+ /// one before the notice has faded -- and an SDR project is unmoved, which
+ /// is what the notice says rather than pretending something happened.
+ pub(crate) fn apply_tone(&mut self, preset: Preset, cx: &mut Context) {
+ if let Some(session) = &mut self.session
+ && session.set_tone(preset)
+ {
+ self.notify_user(format!("HDR: {} — affects HDR media", tone_label(preset)).into());
+ self.reset_after_reseek();
+ }
+ cx.notify();
+ }
+
+ /// Opens a choice list on a setting, where it was asked for. One floating
+ /// thing at a time: the click that opens it is the click that closes
+ /// whatever menu it was opened from.
+ pub(crate) fn open_picker(&mut self, of: Pick, at: Point, cx: &mut Context) {
+ // On the row that is in force, so the first ↑ or ↓ steps off the
+ // current value rather than off the top of the list.
+ let sel = self
+ .choices(of)
+ .iter()
+ .position(|(.., picked)| *picked)
+ .unwrap_or(0);
+ self.context_menu = None;
+ self.library_menu = None;
+ self.picker = Some(Picker { of, at, sel });
+ cx.notify();
+ }
+
+ /// A row of the open list was picked. Closes the list first -- the rule
+ /// every menu item here follows -- then does exactly what the stroke for
+ /// that setting does, through the same door.
+ pub(crate) fn choose(&mut self, choice: Choice, cx: &mut Context) {
+ self.picker = None;
+ match choice {
+ Choice::Size(w, h) => self.apply_resolution(w, h, cx),
+ Choice::Fps(fps) => self.apply_frame_rate(fps, cx),
+ Choice::Fit(lane, idx, fit) => self.apply_fit(lane, idx, fit, cx),
+ Choice::Tone(preset) => self.apply_tone(preset, cx),
+ // In force for the next paint -- every token is read through
+ // [`ui::theme::palette`], so one store repaints the whole window --
+ // and kept for the next launch. A file that could not be written is
+ // said out loud: the difference between "picked" and "picked for
+ // good" is the user's to know.
+ Choice::Theme(id) => {
+ ui::theme::set(id);
+ if let Err(e) = ui::theme::save(id) {
+ let path = ui::theme::config_path();
+ self.notify_user(
+ format!("THEME COULD NOT BE KEPT — {} — {e}", path.display()).into(),
+ );
+ }
+ cx.notify();
+ }
+ // The same field the row's key steps, set outright: a list picks a
+ // value, it does not step to one.
+ Choice::AudioRate(kbps) => {
+ self.audio_kbps = kbps;
+ cx.notify();
+ }
+ }
+ }
+
+ /// Every value the open list offers, in the order it lists them. Empty
+ /// without a timeline, which is the state where nothing here has a value to
+ /// offer -- and where the surfaces that open the list are dimmed anyway.
+ pub(crate) fn choices(&self, of: Pick) -> Vec {
+ // The palette is not the project's, so it is offered before the
+ // timeline is asked about: an empty window is painted too, and its
+ // Theme button is live there like the snap beside it.
+ if of == Pick::Theme {
+ return ui::theme::PaletteId::ALL
+ .into_iter()
+ .map(|id| {
+ (
+ Choice::Theme(id),
+ id.label().into(),
+ id.detail().into(),
+ id == ui::theme::active(),
+ )
+ })
+ .collect();
+ }
+ let Some(session) = &self.session else {
+ return Vec::new();
+ };
+ match of {
+ Pick::Resolution => {
+ resolution_choices(session.resolution(), session.native_resolution())
+ }
+ Pick::Fps => fps_choices(session.meta().frame_rate, session.native_frame_rate()),
+ Pick::Fit(lane, idx) => {
+ fit_choices(lane, idx, session.fit_of(lane, idx), session.resolution())
+ }
+ Pick::AudioRate => audio_rate_choices(self.audio_kbps),
+ Pick::Tone => tone_choices(session.tone()),
+ // Answered above, with or without a timeline.
+ Pick::Theme => Vec::new(),
+ }
+ }
+
+ /// Opens the colour card on the clip a grade would go on: the clip that was
+ /// clicked when it is a video one, and otherwise the clip the picture is
+ /// coming from -- the one the engine's own compositing rule picks, which is
+ /// what a person means by "this shot". The fallback stands even now that a
+ /// selection key exists: a grade asked for with nothing selected still means
+ /// the shot on screen.
+ pub(crate) fn open_color(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let Some(session) = &self.session else {
+ self.notify_user("no timeline to grade — open a file first".into());
+ cx.notify();
+ return;
+ };
+ let target = self
+ .selected
+ .filter(|(lane, _)| lane.kind == LaneKind::Video)
+ .or_else(|| session.video_clip_at(session.now()));
+ match target {
+ Some(clip) => {
+ self.color_open = Some(clip);
+ self.color_band = 0;
+ self.color_dragging = false;
+ // A sample the last card held back belongs to the clip it was
+ // dragged on, and this may be another one.
+ self.pending_color = None;
+ // One card at a time, the rule both the others already follow.
+ self.keys_open = false;
+ self.export_open = false;
+ self.context_menu = None;
+ }
+ None => self.notify_user("no clip under the playhead to grade".into()),
+ }
+ cx.notify();
+ }
+
+ /// What the card's clip is graded by right now -- the identity for one
+ /// nobody has graded, which is what the sliders start at. A sample a drag is
+ /// still holding wins over the clip's own: it is what the hand has asked
+ /// for, so it is what the sliders show and what the next sample builds on.
+ pub(crate) fn color_params(&self) -> ColorParams {
+ if let Some(params) = self.pending_color {
+ return params;
+ }
+ self.color_open
+ .zip(self.session.as_ref())
+ .and_then(|((lane, idx), session)| session.color_of(lane, idx).copied())
+ .unwrap_or_default()
+ }
+
+ /// Puts `params` on the card's clip, or takes the grade off when they are
+ /// the identity -- a slider walked back to the middle leaves the clip
+ /// ungraded rather than carrying a do-nothing entry, which is what keeps an
+ /// untouched project byte-identical. The engine reseeks on the edit, so the
+ /// frame on screen repaints through the new grade; this only owes the flags
+ /// that reseek clears.
+ pub(crate) fn set_color(&mut self, params: ColorParams, cx: &mut Context) {
+ self.write_color(params, false, cx);
+ }
+
+ /// Both writes: `live` is the one that takes no undo step, which is what
+ /// every sample *inside* a drag goes through
+ /// (`PlaybackSession::set_color_live`). Either way the engine reseeks, so
+ /// the picture -- and the histogram counted off it -- is regraded at once.
+ pub(crate) fn write_color(&mut self, params: ColorParams, live: bool, cx: &mut Context) {
+ // Any write supersedes a held sample, whichever way it arrived -- a key,
+ // a reset, or the flush that took this one out of the stash.
+ self.pending_color = None;
+ let Some((lane, idx)) = self.color_open else {
+ return;
+ };
+ let Some(session) = &mut self.session else {
+ return;
+ };
+ let grade = Some(params).filter(|p| !p.is_identity());
+ let took = match live {
+ true => session.set_color_live(lane, idx, grade),
+ false => session.set_color(lane, idx, grade),
+ };
+ if took {
+ self.reset_after_reseek();
+ }
+ cx.notify();
+ }
+
+ /// Moves the picked slider by `steps` of [`COLOR_STEP`], clamped to that
+ /// band's range. One edit, so one undo step per press.
+ pub(crate) fn nudge_color(&mut self, steps: f32, cx: &mut Context) {
+ let mut params = self.color_params();
+ let (_, low, high) = COLOR_BANDS[self.color_band];
+ let value = band_mut(&mut params, self.color_band);
+ *value = (*value + steps * COLOR_STEP).clamp(low, high);
+ self.set_color(params, cx);
+ }
+
+ /// Where the pointer sits along a slider, as that band's value: the left end
+ /// of the bar is the bottom of its range and the right end the top. Called
+ /// on every pointer sample, so the grade -- and the picture, and the
+ /// histogram over it -- moves under the hand.
+ ///
+ /// `first` is the press: it takes the undo step the whole gesture rolls back
+ /// to, and every sample after it is live. That is why it writes even when
+ /// the value did not change -- without that snapshot the rest of the drag
+ /// would be unundoable.
+ ///
+ /// Values land on the [`COLOR_STEP`] grid the keys use, which also bounds
+ /// one drag to forty-odd entries in the project's colour table.
+ ///
+ /// Samples crossed while the worker still owes a frame are held rather than
+ /// written ([`stash_or_write`]): a reopen costs half a second on a big film,
+ /// so a bar-wide sweep that wrote every step would queue forty opens, cancel
+ /// thirty-nine of them and freeze the window for the sum. What is written is
+ /// one grade per frame the worker actually delivers.
+ pub(crate) fn drag_color(&mut self, x: Pixels, first: bool, cx: &mut Context) {
+ let (_, low, high) = COLOR_BANDS[self.color_band];
+ let along = frac_along(x, self.color_bars[self.color_band].get());
+ let value = color_snap(low + along * (high - low)).clamp(low, high);
+ let mut params = self.color_params();
+ let at = band_mut(&mut params, self.color_band);
+ if *at == value && !first {
+ return;
+ }
+ *at = value;
+ let busy = self.seek_since.is_some();
+ match stash_or_write(&mut self.pending_color, params, first, busy) {
+ Some(params) => self.write_color(params, !first, cx),
+ // The sliders draw off the held sample, so the handle goes on
+ // following the hand while the picture catches up.
+ None => cx.notify(),
+ }
+ }
+
+ /// Opens the speed card on the clip whose rate is to change: the selected
+ /// one, or -- with nothing selected -- the clip the picture is coming from,
+ /// which is what a person means by "this shot". Either half of a take will
+ /// do: a rate applies to the whole group, so opening it on the sound and
+ /// opening it on the picture are the same card.
+ pub(crate) fn open_speed(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let Some(session) = &self.session else {
+ self.notify_user("no timeline to re-time — open a file first".into());
+ cx.notify();
+ return;
+ };
+ match self
+ .selected
+ .or_else(|| session.video_clip_at(session.now()))
+ {
+ Some(clip) => {
+ self.speed_open = Some(clip);
+ self.speed_dragging = false;
+ // The colour card's rule: a held sample is the last clip's.
+ self.pending_speed = None;
+ // One card at a time, the rule the other four follow.
+ self.keys_open = false;
+ self.export_open = false;
+ self.eq_open = None;
+ self.color_open = None;
+ self.close_silence();
+ self.context_menu = None;
+ }
+ None => self.notify_user("no clip under the playhead to re-time".into()),
+ }
+ cx.notify();
+ }
+
+ /// What the card's clip plays at right now -- real time for one nobody has
+ /// touched, which is where the bar starts.
+ pub(crate) fn card_speed(&self) -> Speed {
+ if let Some(speed) = self.pending_speed {
+ return speed;
+ }
+ self.speed_open
+ .zip(self.session.as_ref())
+ .map_or(Speed::NORMAL, |((lane, idx), session)| {
+ session.speed_of(lane, idx)
+ })
+ }
+
+ /// Writes a rate at the card's clip and its whole group -- one undo step for
+ /// the lot ([`engine::PlaybackSession::set_speed`]). The engine reseeks, so
+ /// the picture runs at the new rate and the sound is resampled from the next
+ /// chunk on; a refusal (a slower clip would run into its neighbour) comes
+ /// back in the engine's own words and *names* the clip in the way, because
+ /// "it did not fit" is not something a person can go and fix.
+ pub(crate) fn set_speed(&mut self, speed: Speed, cx: &mut Context) {
+ self.write_speed(speed, false, cx);
+ }
+
+ /// Both writes: `live` is the one that takes no undo step, which is what
+ /// every sample *inside* a drag goes through -- so a drag from 1.00x to
+ /// 2.00x is one undo press and lands back where the hand picked it up, and
+ /// the whole linked group comes back with it.
+ pub(crate) fn write_speed(&mut self, speed: Speed, live: bool, cx: &mut Context) {
+ // The colour card's rule: a write supersedes whatever a drag was holding.
+ self.pending_speed = None;
+ let Some((lane, idx)) = self.speed_open else {
+ return;
+ };
+ let Some(session) = &mut self.session else {
+ return;
+ };
+ if speed != session.speed_of(lane, idx) {
+ let wrote = match live {
+ true => session.set_speed_live(lane, idx, speed),
+ false => session.set_speed(lane, idx, speed),
+ };
+ match wrote {
+ Ok(()) => self.reset_after_reseek(),
+ Err(e) => self.notify_user(e.to_string().into()),
+ }
+ }
+ cx.notify();
+ }
+
+ /// One [`SPEED_STEP`] per keystroke, clamped to what a [`Speed`] can hold.
+ pub(crate) fn nudge_speed(&mut self, steps: i32, cx: &mut Context) {
+ let at = i32::from(self.card_speed().permille()) + steps * SPEED_STEP;
+ self.set_speed(speed_at(at), cx);
+ }
+
+ /// Where the pointer sits along the bar, as a rate: the left end is
+ /// [`Speed::MIN`] and the right end [`Speed::MAX`], on the same
+ /// [`SPEED_STEP`] grid the keys move on -- so a drag can land on exactly
+ /// 1.00x and the same drag twice is one entry, not forty.
+ /// `first` is the press: it takes the undo step the whole gesture rolls back
+ /// to, and every sample after it is live -- the colour card's rule, for the
+ /// colour card's reason.
+ pub(crate) fn drag_speed(&mut self, x: Pixels, first: bool, cx: &mut Context) {
+ let along = frac_along(x, self.speed_bar.get());
+ let lo = f32::from(Speed::MIN.permille());
+ let hi = f32::from(Speed::MAX.permille());
+ let raw = lo + along * (hi - lo);
+ // Snapped to the grid, then to real time itself when it is within half a
+ // step of it: 1.00x is the one value a hand must be able to hit, and
+ // nothing about the bar's geometry guarantees a pixel lands on it.
+ let stepped = (raw / SPEED_STEP as f32).round() as i32 * SPEED_STEP;
+ // Held back while the worker is busy, the colour card's way and for a
+ // sharper reason: a live rate also restarts the sound, so a sweep that
+ // wrote every step would restart it forty times.
+ let busy = self.seek_since.is_some();
+ match stash_or_write(&mut self.pending_speed, speed_at(stepped), first, busy) {
+ Some(speed) => self.write_speed(speed, !first, cx),
+ None => cx.notify(),
+ }
+ }
+
+ /// Writes what a slider drag held back, now that the worker has delivered.
+ /// The gate is the frame that landed and never a timer: a 100 ms tick
+ /// ([`SCRUB_GAP`]) says nothing about a reopen that costs half a second, and
+ /// a drag gated on one would still queue opens nobody sees.
+ ///
+ /// Called again by the release, where readiness is beside the point: the
+ /// value the hand let go on is owed whatever the worker is doing, and a
+ /// gesture may not end on a sample that was dropped.
+ pub(crate) fn flush_drag(&mut self, cx: &mut Context) {
+ if let Some(params) = self.pending_color.take() {
+ self.write_color(params, true, cx);
+ }
+ if let Some(speed) = self.pending_speed.take() {
+ self.write_speed(speed, true, cx);
+ }
+ }
+
+ /// Opens the silence card on the clip to be scanned: the selected one, or
+ /// -- with nothing selected -- the clip the picture is coming from, which is
+ /// the rule the speed card follows and what a person means by "this shot".
+ /// Either half of a take will do: both halves of an A/V take name the same
+ /// file and play the same source frames, which is the whole of what a scan
+ /// is of ([`ScanKey`]).
+ ///
+ /// The card is up on the next frame whatever the file is: a still is
+ /// refused by name here, where the answer costs a look at the path, and
+ /// everything the decoder has to open the file to know -- a track that is
+ /// not there, a read that fails -- is refused the same way when the scan
+ /// lands, because a fifty-second decode is not a thing to open a card
+ /// behind ([`Player::start_silence_scan`]).
+ pub(crate) fn open_silence(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let Some(session) = &self.session else {
+ self.notify_user("no timeline to scan — open a file first".into());
+ cx.notify();
+ return;
+ };
+ match self
+ .selected
+ .or_else(|| session.video_clip_at(session.now()))
+ .map(|clip| audio_half(session, clip))
+ {
+ Some((lane, idx)) => {
+ let found = self.session.as_ref().and_then(|session| {
+ let clip = *session.lane_clips(lane).get(idx)?;
+ Some((session.sources().get(clip.source)?.clone(), clip))
+ });
+ // A still is asked *before* the decoder is: handing a png to the
+ // mp4 demuxer answers "a box with a larger size than it", which
+ // is a true sentence about a container and nothing a person can
+ // act on. A picture has no sound for the same reason a silent
+ // video has none, so it is refused in the same words.
+ let Some((source, clip)) = found else {
+ cx.notify();
+ return;
+ };
+ if engine::is_image(&source.path) {
+ self.notify_user(unscannable(lane, idx, &source.path).into());
+ cx.notify();
+ return;
+ }
+ self.silence_open = Some((lane, idx));
+ self.silence_field = 0;
+ // One card at a time, the rule the other four follow.
+ self.keys_open = false;
+ self.export_open = false;
+ self.eq_open = None;
+ self.color_open = None;
+ self.speed_open = None;
+ self.context_menu = None;
+ // The clip's own range, not the file's: the scan reads what this
+ // clip plays and nothing else, so a take cut in half costs half
+ // the decode and finds only what is still on the timeline.
+ let key = (
+ source.path.clone(),
+ source.audio_stream,
+ clip.in_frame,
+ clip.out_frame,
+ );
+ match scan_plan(
+ self.silence_levels.contains_key(&key),
+ self.silence_scan.as_ref().map(|scan| &scan.key),
+ &key,
+ ) {
+ ScanPlan::Marks => self.scan_silences(),
+ ScanPlan::Start => self.start_silence_scan(key, cx),
+ ScanPlan::Wait => {}
+ }
+ }
+ None => self.notify_user("no clip under the playhead to scan".into()),
+ }
+ cx.notify();
+ }
+
+ /// Opens the mix card. `lane` is the row it lands on -- the track whose
+ /// header was clicked -- and `None` starts at the top, which is what the
+ /// stroke means.
+ ///
+ /// Nothing here is a clip's, so nothing is refused for want of a selection:
+ /// a timeline with no audio track at all still has a limiter to set, and a
+ /// fader on an empty track is the level the next take lands at.
+ pub(crate) fn open_mix(&mut self, lane: Option, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ self.mix_open = true;
+ self.mix_field = lane
+ .and_then(|lane| self.mix_lanes().iter().position(|&l| l == lane))
+ .unwrap_or(0);
+ // One card at a time, the rule the other five follow.
+ self.keys_open = false;
+ self.export_open = false;
+ self.eq_open = None;
+ self.color_open = None;
+ self.speed_open = None;
+ self.close_silence();
+ self.context_menu = None;
+ cx.notify();
+ }
+
+ /// The audio tracks the card shows a fader for, top to bottom: *every* one
+ /// of them, empty ones included -- what the timeline lays out, not what the
+ /// mixer happens to open (`Project::audio_lanes` leaves an empty track out,
+ /// and a fader that disappeared when a track was cleared would be a setting
+ /// nobody could reach).
+ pub(crate) fn mix_lanes(&self) -> Vec {
+ self.session.as_ref().map_or_else(Vec::new, |session| {
+ session
+ .lanes()
+ .into_iter()
+ .filter(|l| l.kind == LaneKind::Audio)
+ .collect()
+ })
+ }
+
+ /// Moves the row the card has picked: a fader by [`MIX_DB_STEP`], the
+ /// ceiling by the same, and the switch either way (a ring of two, like the
+ /// silence card's unit row).
+ ///
+ /// Every one of them goes through the session, which hands it straight to
+ /// the running mixer: what the ear hears while the arrow is held is the mix
+ /// that is being set, and nothing is rebuilt to make that true -- no reseek,
+ /// so no `reset_after_reseek` and no blink in the picture behind the card
+ /// ([`engine::PlaybackSession::set_lane_gain_db`]).
+ pub(crate) fn nudge_mix(&mut self, steps: i32, cx: &mut Context) {
+ let lanes = self.mix_lanes();
+ let Some(session) = self.session.as_mut() else {
+ return;
+ };
+ match lanes.get(self.mix_field) {
+ Some(&lane) => {
+ let at = session.lane_gain_db(lane) + steps as f32 * MIX_DB_STEP;
+ session.set_lane_gain_db(lane, at);
+ }
+ None => {
+ let limiter = session.limiter();
+ let at = match self.mix_field - lanes.len() {
+ 0 => Limiter {
+ on: limiter.on,
+ ..limiter
+ }
+ .with_ceiling(limiter.ceiling_db + steps as f32 * MIX_DB_STEP),
+ _ => Limiter {
+ on: !limiter.on,
+ ..limiter
+ },
+ };
+ session.set_limiter(at);
+ }
+ }
+ cx.notify();
+ }
+
+ /// Closes it and drops the preview with it: marks left on the lane after
+ /// the card is gone would name frames the next edit has already moved.
+ pub(crate) fn close_silence(&mut self) {
+ self.silence_open = None;
+ self.silence_marks.clear();
+ self.cancel_silence_scan();
+ }
+
+ /// Tells the worker nobody is waiting any more. It gives up at its next
+ /// chunk and the levels it had are dropped: half a track is not an answer,
+ /// and the flag stays set on the [`Arc`] the landing closure holds, which is
+ /// how that closure knows to keep its hands off the card.
+ pub(crate) fn cancel_silence_scan(&mut self) {
+ if let Some(scan) = self.silence_scan.take() {
+ scan.progress
+ .cancel
+ .store(true, std::sync::atomic::Ordering::Relaxed);
+ }
+ }
+
+ /// Hands the decode to a worker and returns at once -- the card is drawn by
+ /// the very next frame, saying it is scanning. Fifty-one seconds on a 25 GB
+ /// film is what this used to cost on the render thread, with the card
+ /// marked open and nothing on screen.
+ ///
+ /// Whatever was scanning is cancelled first: one card, one scan, and the
+ /// clip that has just been asked about is the one worth the disk.
+ ///
+ /// Only the clip's own `[in, out)` is read -- source frames over the
+ /// project's rate, the same seconds [`engine::Project`] hands the decoder
+ /// for playback -- so half a take is half a wait.
+ pub(crate) fn start_silence_scan(&mut self, key: ScanKey, cx: &mut Context) {
+ self.cancel_silence_scan();
+ self.silence_marks.clear();
+ let progress = Arc::new(engine::silence::Progress::default());
+ let range = source_secs(&key, self.fps);
+ let scan = cx.background_executor().spawn({
+ let (key, progress) = (key.clone(), Arc::clone(&progress));
+ async move { engine::silence::levels_with_progress(&key.0, key.1, range, &progress) }
+ });
+ let now = Instant::now();
+ self.silence_scan = Some(SilenceScan {
+ key: key.clone(),
+ started: now,
+ progress: Arc::clone(&progress),
+ seen: 0,
+ since: now,
+ });
+ cx.spawn(async move |this, cx| {
+ let landed = scan.await;
+ this.update(cx, |this, cx| {
+ // Cancelled means the card moved on or closed: the levels are a
+ // prefix of a track nobody asked about any more.
+ if progress.cancel.load(std::sync::atomic::Ordering::Relaxed) {
+ return;
+ }
+ this.silence_scan = None;
+ match landed {
+ Ok(Some(levels)) => {
+ this.silence_levels.insert(key.clone(), Arc::new(levels));
+ this.scan_silences();
+ }
+ // A source with no audio track is not one long silence: it
+ // is a clip this card has nothing to say about, named so the
+ // user knows which one it meant.
+ Ok(None) => {
+ if let Some((lane, idx)) = this.silence_open {
+ this.notify_user(unscannable(lane, idx, &key.0).into());
+ }
+ this.close_silence();
+ }
+ Err(e) => {
+ this.close_silence();
+ this.notify_user(format!("SCAN FAILED: {e}").into());
+ }
+ }
+ cx.notify();
+ })
+ .ok();
+ })
+ .detach();
+ cx.notify();
+ }
+
+ /// Keeps the scanning line's stall clock, for [`Player::poll_import`]'s
+ /// reason: sampled once per frame rather than while drawing.
+ pub(crate) fn poll_silence(&mut self) {
+ if let Some(scan) = &mut self.silence_scan {
+ scan.poll();
+ }
+ }
+
+ /// Applies the settings to levels already in hand and replaces the preview
+ /// -- never stacks on it. Arithmetic only: the decode is
+ /// [`Player::start_silence_scan`]'s and happens once per source, so every
+ /// run here is numbers already read, which is what makes moving a threshold
+ /// feel like moving a slider. A source still being scanned has no marks yet
+ /// and says so on the card.
+ ///
+ /// Changes nothing about the project: a preview is not an edit, and no undo
+ /// step is spent until a button is pressed.
+ pub(crate) fn scan_silences(&mut self) {
+ let Some((lane, idx)) = self.silence_open else {
+ return;
+ };
+ self.silence_marks.clear();
+ // Copied out before anything is written back: the cache below lives on
+ // the same struct the session does.
+ let Some((clip, source)) = self.session.as_ref().and_then(|session| {
+ let clip = *session.lane_clips(lane).get(idx)?;
+ Some((clip, session.sources().get(clip.source)?.clone()))
+ }) else {
+ return;
+ };
+ // Nothing read yet: the worker is running and the card is drawing its
+ // line. The marks arrive with the levels.
+ let Some(levels) = self
+ .silence_levels
+ .get(&(
+ source.path.clone(),
+ source.audio_stream,
+ clip.in_frame,
+ clip.out_frame,
+ ))
+ .cloned()
+ else {
+ return;
+ };
+ self.silence_marks = engine::silence::timeline_regions(
+ &clip,
+ self.fps,
+ &engine::silence::regions(&levels, self.silence),
+ );
+ }
+
+ /// Moves the picked row by `steps` and re-runs the scan against it, so the
+ /// marks on the lane are always what the numbers on the card say.
+ pub(crate) fn nudge_silence(&mut self, steps: i32) {
+ let secs = |at: f64| {
+ (at + f64::from(steps) * SILENCE_SECS_STEP)
+ .clamp(SILENCE_SECS_RANGE.0, SILENCE_SECS_RANGE.1)
+ };
+ match self.silence_field {
+ // Round either way, like the fit policy's cycle: three choices are
+ // a ring, not a range.
+ 0 => {
+ let at = SCOPES.iter().position(|&s| s == self.silence_scope);
+ let step = steps.rem_euclid(SCOPES.len() as i32) as usize;
+ self.silence_scope = SCOPES[(at.unwrap_or(0) + step) % SCOPES.len()];
+ }
+ 1 => {
+ self.silence.threshold_db = (self.silence.threshold_db
+ + steps as f32 * SILENCE_DB_STEP)
+ .clamp(SILENCE_DB_RANGE.0, SILENCE_DB_RANGE.1)
+ }
+ // Two spellings of the same level, so either arrow flips it -- a
+ // ring of two, like the scope row's.
+ 2 => self.silence_dbfs = !self.silence_dbfs,
+ 3 => self.silence.min_silence = secs(self.silence.min_silence),
+ 4 => self.silence.padding = secs(self.silence.padding),
+ 5 => self.silence.min_keep = secs(self.silence.min_keep),
+ _ => {
+ self.silence_factor =
+ silence_rate(i32::from(self.silence_factor.permille()) + steps * SPEED_STEP)
+ }
+ }
+ // Neither the scope nor the rate is part of the scan, but re-running is
+ // cheap (the levels are cached) and one path is one place for the marks
+ // to come from.
+ self.scan_silences();
+ }
+
+ /// Which lanes an apply reaches, as the card's scope row says it: the
+ /// lanes of the take the scanned clip belongs to, that clip's lane alone,
+ /// or every lane there is.
+ ///
+ /// The take's lanes are the ones carrying its group id -- a link is one
+ /// span on however many lanes, so "the take" is exactly the set of lanes
+ /// that would otherwise be pulled apart. Nothing widens behind the user's
+ /// back: [`Project::cut_regions`] refuses a scope that would split a take,
+ /// and this row is how the user says the take instead.
+ pub(crate) fn silence_lanes(&self) -> Vec {
+ let (Some((lane, idx)), Some(session)) = (self.silence_open, self.session.as_ref()) else {
+ return Vec::new();
+ };
+ match self.silence_scope {
+ Scope::Track => vec![lane],
+ Scope::Everything => session.lanes(),
+ Scope::Take => match session.lane_clips(lane).get(idx).and_then(|c| c.link) {
+ None => vec![lane],
+ Some(id) => session
+ .lanes()
+ .into_iter()
+ .filter(|&l| {
+ l == lane || session.lane_clips(l).iter().any(|c| c.link == Some(id))
+ })
+ .collect(),
+ },
+ }
+ }
+
+ /// What an apply acts on: the previewed set and the lanes it reaches, or
+ /// nothing at all with a notice saying so in the numbers that found
+ /// nothing.
+ pub(crate) fn previewed(&mut self) -> Option<(Vec<(u32, u32)>, Vec)> {
+ if self.silence_marks.is_empty() {
+ self.notify_user(
+ format!(
+ "no silence under {:.0} dBFS lasting {:.2} s — raise the threshold or forgive less",
+ self.silence.threshold_db, self.silence.min_silence
+ )
+ .into(),
+ );
+ return None;
+ }
+ Some((self.silence_marks.clone(), self.silence_lanes()))
+ }
+
+ /// What an apply says afterwards: which tracks it reached, and -- when that
+ /// was not all of them -- that the rest were left where they were. The
+ /// scope is a choice, so the confirmation has to name the choice.
+ pub(crate) fn silence_reach(&self, lanes: &[Lane]) -> String {
+ let named = lanes
+ .iter()
+ .map(|l| l.label())
+ .collect::>()
+ .join("+");
+ match self.silence_scope {
+ Scope::Everything => "on every track".to_string(),
+ _ => format!("on {named} — other tracks untouched"),
+ }
+ }
+
+ /// Cuts every previewed silence out of the lanes the scope names, rippling
+ /// each hole closed -- one edit and **one** undo press however many there
+ /// were ([`engine::PlaybackSession::cut_regions`]). Tracks outside the
+ /// scope do not move; a scope that would take half a take with it comes
+ /// back refused in the engine's own words, naming both halves.
+ pub(crate) fn cut_silences(&mut self, cx: &mut Context) {
+ let Some((regions, lanes)) = self.previewed() else {
+ cx.notify();
+ return;
+ };
+ let saved = f64::from(regions.iter().map(|&(_, len)| len).sum::()) / self.fps;
+ let (count, reach) = (regions.len(), self.silence_reach(&lanes));
+ let Some(session) = self.session.as_mut() else {
+ cx.notify();
+ return;
+ };
+ match session.cut_regions(®ions, &lanes) {
+ Ok(()) => {
+ self.close_silence();
+ // Every hole closed moves the clips after it up a place, so the
+ // selection now names a different clip than the one that is
+ // highlighted -- dropped here as after every other edit that
+ // moves indexes (a delete, a paste, an undo).
+ self.selected = None;
+ self.reset_after_reseek();
+ self.notify_user(
+ format!(
+ "{count} SILENCES CUT {reach} — {} shorter, {} takes it back",
+ secs_label(saved),
+ self.keymap.display(ActionId::Undo)
+ )
+ .into(),
+ );
+ }
+ Err(e) => self.notify_user(e.to_string().into()),
+ }
+ cx.notify();
+ }
+
+ /// Plays them fast instead of cutting them, closing the room each one no
+ /// longer needs. One undo press like the cut, and the same scope; the
+ /// refusals (a clip lapping over a silence, a scope that would split a
+ /// take) come back in the engine's own words and name the lane and frame,
+ /// and the card stays up so the numbers that produced it are still on
+ /// screen.
+ pub(crate) fn speed_silences(&mut self, cx: &mut Context) {
+ let Some((regions, lanes)) = self.previewed() else {
+ cx.notify();
+ return;
+ };
+ let (count, rate) = (regions.len(), self.silence_factor);
+ let reach = self.silence_reach(&lanes);
+ let Some(session) = self.session.as_mut() else {
+ cx.notify();
+ return;
+ };
+ match session.speed_regions(®ions, rate, &lanes) {
+ Ok(()) => {
+ self.close_silence();
+ // Splitting each silence out and closing the room it no longer
+ // needs moves indexes exactly as the cut does: the selection
+ // goes with them.
+ self.selected = None;
+ self.reset_after_reseek();
+ self.notify_user(
+ format!(
+ "{count} SILENCES AT {rate} {reach} — {} takes it back",
+ self.keymap.display(ActionId::Undo)
+ )
+ .into(),
+ );
+ }
+ Err(e) => self.notify_user(e.to_string().into()),
+ }
+ cx.notify();
+ }
+
+ /// Whether a card owns the window. While one does the timeline under it is
+ /// out of reach, so a right-click there opens no menu -- the same rule the
+ /// key handler and the drop target already follow.
+ /// Whether anything at all is drawn over the window -- a card, a menu or an
+ /// open list. What the hover labels stand aside for ([`OVERLAID`]): a
+ /// tooltip belongs to the surface the pointer is on, and while one of these
+ /// is up that surface is behind it.
+ pub(crate) fn overlaid(&self) -> bool {
+ self.modal()
+ || self.context_menu.is_some()
+ || self.library_menu.is_some()
+ || self.picker.is_some()
+ }
+
+ pub(crate) fn modal(&self) -> bool {
+ self.keys_open
+ || self.export_open
+ || self.eq_open.is_some()
+ || self.color_open.is_some()
+ || self.speed_open.is_some()
+ || self.silence_open.is_some()
+ || self.mix_open
+ || self.exporting().is_some()
+ }
+
+ /// The pointer's way out of whatever card is up: what every scrim's press
+ /// calls, so `esc` is *a* way out and never the only one. One list, and the
+ /// same one [`Player::modal`] reads -- a card that can be counted there but
+ /// not closed here is a card a hand alone cannot shut, which is what
+ /// `every_card_closes_without_the_keyboard` fails on.
+ ///
+ /// Every card at once because only one is ever up (`export_open`): closing
+ /// "the" card and closing all of them are the same act.
+ pub(crate) fn close_card(&mut self) {
+ self.keys_open = false;
+ self.keys_search.clear();
+ self.rebinding = None;
+ self.export_open = false;
+ // The two things typed *into* the export card go with it: a field left
+ // open would take the next keystroke for a card that is gone.
+ self.mbps_edit = None;
+ self.picker = None;
+ self.eq_open = None;
+ self.eq_dragging = false;
+ self.color_open = None;
+ self.speed_open = None;
+ // Marks and a running scan go with this one, which is why it is a call
+ // and not an assignment ([`Player::close_silence`]).
+ if self.silence_open.is_some() {
+ self.close_silence();
+ }
+ self.mix_open = false;
+ }
+
+ /// Opens the equalizer on the selected clip. Audio only, and it says so
+ /// rather than opening a card of bands that would reach nothing: a video
+ /// clip carries no sound of its own here (the sound is the audio lane's),
+ /// and the model would take the setting without anything ever playing it.
+ pub(crate) fn open_eq(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let refusal = match (self.selected, &self.session) {
+ (_, None) => Some("NO TIMELINE — open a file first".to_string()),
+ (None, _) => Some(format!(
+ "NOTHING SELECTED — click an audio clip or press {}, then ask again",
+ self.keymap.display(ActionId::Select)
+ )),
+ (Some((lane, _)), _) if lane.kind != LaneKind::Audio => Some(
+ "NOT AN AUDIO CLIP — the equalizer works on the sound, so pick a clip in an audio lane".to_string(),
+ ),
+ _ => None,
+ };
+ if let Some(refusal) = refusal {
+ self.notify_user(refusal.into());
+ cx.notify();
+ return;
+ }
+ let (lane, idx) = self.selected.expect("checked above");
+ let session = self.session.as_ref().expect("checked above");
+ // What the clip already plays through, or the flat default -- so the
+ // card opens on the curve that is in force and a reopen shows the last
+ // drag rather than a fresh set of zeroes.
+ self.eq_params = session
+ .eq_of(lane, idx)
+ .cloned()
+ .unwrap_or_else(EqParams::default_layout);
+ self.eq_band = 0;
+ self.eq_dragging = false;
+ self.eq_open = Some((lane, idx));
+ // One card at a time, the rule the other two already follow.
+ self.keys_open = false;
+ self.export_open = false;
+ self.context_menu = None;
+ cx.notify();
+ }
+
+ /// Writes what the card is showing at its clip: one undo step, one entry in
+ /// the append-only equalizer table, so this is called once per *gesture* --
+ /// the end of a drag, a keystroke -- and never per pointer sample.
+ ///
+ /// A curve that moves nothing is stored as *no* equalizer at all, which is
+ /// what keeps a clip nobody has touched on the identity path through
+ /// playback and export (`engine::eq::EqParams::is_identity`).
+ pub(crate) fn commit_eq(&mut self, cx: &mut Context) {
+ let Some((lane, idx)) = self.eq_open else {
+ return;
+ };
+ let params = (!self.eq_params.is_identity()).then(|| self.eq_params.clone());
+ if let Some(session) = &mut self.session {
+ session.set_eq(lane, idx, params);
+ }
+ // `set_eq` reseeks inside the engine -- that is what makes the change
+ // audible at once -- and a reseek is what these flags are about.
+ self.reset_after_reseek();
+ cx.notify();
+ }
+
+ /// Changes the picked band in place and says whether anything moved. Every
+ /// edit of a band goes through here -- the drag, each key, each stepper
+ /// button -- so the card has exactly one place that clamps a band into what
+ /// the graph can draw, and no caller has to remember the limits.
+ pub(crate) fn set_band(&mut self, change: impl FnOnce(&mut Band)) -> bool {
+ let Some(band) = self.eq_params.bands.get_mut(self.eq_band) else {
+ return false;
+ };
+ let was = *band;
+ change(band);
+ band.freq_hz = band.freq_hz.clamp(EQ_FREQ_LOW, EQ_FREQ_HIGH);
+ band.gain_db = band.gain_db.clamp(-EQ_GAIN_LIMIT, EQ_GAIN_LIMIT);
+ band.q = band.q.clamp(EQ_Q_LOW, EQ_Q_HIGH);
+ *band != was
+ }
+
+ /// The keyboard's and the buttons' version of a drag: one step on the picked
+ /// band, committed straight away -- neither has a release to wait for.
+ pub(crate) fn nudge_band(&mut self, change: impl FnOnce(&mut Band), cx: &mut Context) {
+ if self.set_band(change) {
+ self.commit_eq(cx);
+ }
+ }
+
+ /// Where the pointer sits in the graph, as the picked band's frequency and
+ /// gain: across is the frequency axis and down is the gain one, so the
+ /// handle follows the hand both ways rather than sliding up a rail. Called
+ /// on every pointer sample, so the curve bends under it; the write is the
+ /// release's ([`commit_eq`](Player::commit_eq)).
+ pub(crate) fn drag_band(&mut self, at: Point, cx: &mut Context) {
+ let bounds = self.eq_graph.get();
+ let gain = (0.5 - frac_down(at.y, bounds)) * 2. * EQ_GAIN_LIMIT;
+ let freq = eq_freq(frac_along(at.x, bounds));
+ if self.set_band(|b| {
+ b.gain_db = gain;
+ b.freq_hz = freq;
+ }) {
+ cx.notify();
+ }
+ }
+
+ /// A band added beside the picked one, at the frequency with the most room
+ /// around it ([`inserted_band`]), and picked so the next keystroke moves the
+ /// band that was just made. Refused rather than silently ignored at the cap.
+ pub(crate) fn add_band(&mut self, cx: &mut Context) {
+ if self.eq_params.bands.len() >= EQ_BANDS_MAX {
+ self.notify_user(
+ format!(
+ "EQUALIZER FULL — {EQ_BANDS_MAX} bands is all this card holds; move one instead"
+ )
+ .into(),
+ );
+ cx.notify();
+ return;
+ }
+ let band = inserted_band(&self.eq_params.bands, self.eq_band);
+ self.eq_band = (self.eq_band + 1).min(self.eq_params.bands.len());
+ self.eq_params.bands.insert(self.eq_band, band);
+ self.commit_eq(cx);
+ }
+
+ /// Takes the picked band out. The last one stays: an equalizer of no bands
+ /// is a card with nothing to edit, and flattening is what "off" means here.
+ pub(crate) fn remove_band(&mut self, cx: &mut Context) {
+ if self.eq_params.bands.len() <= 1 {
+ self.notify_user("LAST BAND — flatten it instead (r), or close the card".into());
+ cx.notify();
+ return;
+ }
+ self.eq_params.bands.remove(self.eq_band);
+ self.eq_band = self.eq_band.min(self.eq_params.bands.len() - 1);
+ self.commit_eq(cx);
+ }
+
+ /// Which band a press on the graph grabs: the nearest one along the
+ /// frequency axis, so the whole box is the handle rather than a 10 px dot
+ /// -- and a press that misses every dot still moves the band it is under.
+ pub(crate) fn nearest_band(&self, x: Pixels) -> usize {
+ let at = frac_along(x, self.eq_graph.get());
+ self.eq_params
+ .bands
+ .iter()
+ .enumerate()
+ .min_by(|(_, a), (_, b)| {
+ (eq_x(a.freq_hz) - at)
+ .abs()
+ .total_cmp(&(eq_x(b.freq_hz) - at).abs())
+ })
+ .map_or(0, |(i, _)| i)
+ }
+}
diff --git a/crates/app/src/player/export.rs b/crates/app/src/player/export.rs
new file mode 100644
index 0000000..278cc75
--- /dev/null
+++ b/crates/app/src/player/export.rs
@@ -0,0 +1,551 @@
+//! What is cached about the media and the machine, and the export the
+//! two of them decide.
+
+use crate::*;
+
+impl Player {
+ /// Starts a peak decode -- and a stream probe -- for every source that has
+ /// arrived since the last
+ /// repaint. One call from the render rather than three at the doors,
+ /// because argv, an import and a project load are all doors and only this
+ /// one is guaranteed to run after each of them.
+ ///
+ /// The decode itself runs on a background thread, like the file chooser:
+ /// whole-file audio decode is ~1 s for a half-hour source, and on the render
+ /// path that is the window not painting for a second. The lane draws a bed
+ /// meanwhile and the repaint comes with the peaks. The entry is written
+ /// *before* the spawn, so the sixty repaints that happen while a decode runs
+ /// start no further ones.
+ pub(crate) fn cache_media(&mut self, cx: &mut Context) {
+ let Some(session) = &self.session else {
+ return;
+ };
+ // How big each still is, for the row that has to say so. Inline, unlike
+ // the two below: an image header is a few bytes off the front of the
+ // file, where a sample table is a parse and a decode is a second.
+ for path in unseen_paths(session.sources(), &self.sizes) {
+ let size = engine::is_image(&path)
+ .then(|| engine::image_size(&path).ok())
+ .flatten();
+ self.sizes.insert(path, size);
+ }
+ // Which audio streams each file has, for the library's rows. Header
+ // only, but a big file's sample tables are not free to parse, so it
+ // goes off the render thread like the peaks do.
+ for path in unseen_paths(session.sources(), &self.streams) {
+ self.streams.insert(path.clone(), Vec::new());
+ let probed = cx.background_executor().spawn({
+ let path = path.clone();
+ async move { engine::AudioSession::probe_streams(&path).unwrap_or_default() }
+ });
+ cx.spawn(async move |this, cx| {
+ let probed = probed.await;
+ this.update(cx, |this, cx| {
+ this.streams.insert(path, probed);
+ cx.notify();
+ })
+ .ok();
+ })
+ .detach();
+ }
+ // What each file is coded at, for the card that says so. Header and
+ // sample table only, but a Matroska indexes no samples and its open
+ // walks every cluster header -- 6.7 s on a 12.9 GB film -- so this of
+ // all of them cannot be on the render thread.
+ for path in unseen_paths(session.sources(), &self.bitrates) {
+ self.bitrates.insert(path.clone(), None);
+ let probed = cx.background_executor().spawn({
+ let path = path.clone();
+ async move { engine::probe_bitrate(&path) }
+ });
+ cx.spawn(async move |this, cx| {
+ let probed = probed.await;
+ this.update(cx, |this, cx| {
+ this.bitrates.insert(path, Some(probed));
+ cx.notify();
+ })
+ .ok();
+ })
+ .detach();
+ }
+ // Where this file's own groups of pictures begin, for the cut that
+ // wants to land on one ([`Player::sync_frames`]). The heaviest probe
+ // here -- a Matroska's whole cluster walk, seconds on a film -- and the
+ // one nothing waits for: until it answers, the snap is the clip-edge
+ // snap it always was.
+ for path in unseen_paths(session.sources(), &self.syncs) {
+ self.syncs.insert(path.clone(), Vec::new());
+ let probed = cx.background_executor().spawn({
+ let path = path.clone();
+ async move { engine::demux::sync_points(&path) }
+ });
+ cx.spawn(async move |this, cx| {
+ let probed = probed.await;
+ this.update(cx, |this, cx| {
+ this.syncs.insert(path, probed);
+ cx.notify();
+ })
+ .ok();
+ })
+ .detach();
+ }
+ // Which decoder each file will run on, for the row that says so before
+ // a frame of it plays. Off the render thread like the streams above: a
+ // stream the plugin takes costs one VA-API init (~90 ms) to answer.
+ for path in unseen_paths(session.sources(), &self.decoders) {
+ self.decoders.insert(path.clone(), None);
+ let probed = cx.background_executor().spawn({
+ let path = path.clone();
+ // A song and a source no decoder here takes are both `None`:
+ // the row says nothing about them rather than guessing, and
+ // import refused the second at the door anyway.
+ async move { engine::decode::probe(&path).ok() }
+ });
+ cx.spawn(async move |this, cx| {
+ let probed = probed.await;
+ this.update(cx, |this, cx| {
+ this.decoders.insert(path, probed);
+ cx.notify();
+ })
+ .ok();
+ })
+ .detach();
+ }
+ for key in unseen_sources(session.sources(), &self.waves) {
+ self.waves.insert(key.clone(), Wave::Loading);
+ let decoded = cx.background_executor().spawn({
+ let (path, stream) = key.clone();
+ async move {
+ engine::waveform::peaks(&path, stream, WAVE_BPS)
+ .map(|peaks| peaks.map(|peaks| Arc::new(normalise(peaks))))
+ .inspect_err(|e| eprintln!("waveform: {}: {e}", path.display()))
+ }
+ });
+ cx.spawn(async move |this, cx| {
+ let decoded = decoded.await;
+ this.update(cx, |this, cx| {
+ this.waves.insert(
+ key,
+ match decoded {
+ Ok(Some(peaks)) => Wave::Peaks(peaks),
+ // No audio track: an answer, and not worth asking
+ // about again.
+ Ok(None) => Wave::Silent,
+ // A file whose sound we could not read is not a
+ // silent one, and a lane that drew it as silent is
+ // how a broken decode passes for a design choice.
+ Err(_) => Wave::Failed,
+ },
+ );
+ cx.notify();
+ })
+ .ok();
+ })
+ .detach();
+ }
+ }
+
+ /// Probes what an export would open, once per (settings, resolution, cuts)
+ /// and only while the export card is up -- it opens the very VA-API encoder
+ /// the export would and asks [`engine::export::planned_seats`] the very
+ /// question the export asks itself, which is what makes the card's line a
+ /// measurement instead of a promise, and also what makes it too slow for
+ /// the render thread. Written before the spawn, like the probes above, so
+ /// the repaints during it start no second one.
+ ///
+ /// The cuts are in the key because they are in the answer: moving one onto
+ /// a sync point is exactly what turns "SW encode" into "copy", and a card
+ /// that kept the old line would be lying about the file it is about to
+ /// write.
+ pub(crate) fn cache_export_seat(&mut self, cx: &mut Context) {
+ let Some(session) = &self.session else {
+ return;
+ };
+ let settings =
+ export_settings(self.quality, self.custom_mbps, self.format, self.audio_kbps);
+ if !self.export_open {
+ return;
+ }
+ // A format with no picture has no seat to probe -- and the *last*
+ // format's is not its answer: cleared rather than left standing, or
+ // picking MP3 after AV1 would read "SW encode (rav1e) · MP3 · SW
+ // (rusty_mp3)", which names an encoder that will not run.
+ if !settings.format.has_video() {
+ self.export_seat = None;
+ return;
+ }
+ // The timeline an export would be started with, owned so the probe can
+ // run on a worker -- and the clips beside it, which are what tells this
+ // that the question has changed.
+ let (project, meta) = session.export_snapshot();
+ let clips: Vec = session
+ .lanes()
+ .into_iter()
+ .flat_map(|lane| session.lane_clips(lane).to_vec())
+ .collect();
+ // Cloned rather than copied: the settings carry the picked subtitle
+ // rows, which is a `Vec` ([`engine::export::ExportSettings`]).
+ let key = (settings.clone(), (meta.width, meta.height), clips);
+ if self
+ .export_seat
+ .as_ref()
+ .is_some_and(|(asked, size, cuts, _)| (asked, size, cuts) == (&key.0, &key.1, &key.2))
+ {
+ return;
+ }
+ self.export_seat = Some((key.0.clone(), key.1, key.2.clone(), None));
+ let probed = cx.background_executor().spawn(async move {
+ engine::export::planned_seats(&project, &meta, &settings)
+ });
+ cx.spawn(async move |this, cx| {
+ let probed = probed.await;
+ this.update(cx, |this, cx| {
+ // Only if the card is still asking the same question: a format
+ // changed while the plugin opened has a probe of its own.
+ if let Some(seat) = this.export_seat.as_mut().filter(|(asked, size, cuts, _)| {
+ (asked, size, cuts) == (&key.0, &key.1, &key.2)
+ }) {
+ seat.3 = Some(probed);
+ }
+ cx.notify();
+ })
+ .ok();
+ })
+ .detach();
+ }
+
+ /// Asks the plugin what this machine's GPU can do, once, the first time the
+ /// export card is up to show it. Off the render thread for `cache_export_seat`'s
+ /// reason: the plugin initialises VA-API to answer, and a driver that is
+ /// slow to load must not be a frame the user waits for.
+ pub(crate) fn cache_hw_caps(&mut self, cx: &mut Context) {
+ if !self.export_open || self.hw_caps.is_some() {
+ return;
+ }
+ // Written before the spawn, exactly as the probes above are, so the
+ // repaints during it start no second one.
+ self.hw_caps = Some("asking the driver…".into());
+ let asked = cx
+ .background_executor()
+ .spawn(async move { engine::caps::hardware() });
+ cx.spawn(async move |this, cx| {
+ let line = asked.await;
+ this.update(cx, |this, cx| {
+ this.hw_caps = Some(line.into());
+ cx.notify();
+ })
+ .ok();
+ })
+ .detach();
+ }
+
+ /// The export that owns the UI, if any. A cancelled one does not: it has
+ /// its own copy of the edit list and owes only its own cleanup.
+ pub(crate) fn exporting(&self) -> Option<&ExportHandle> {
+ self.export.as_ref().filter(|_| !self.cancelling)
+ }
+
+ /// What the export action does now: opens the card, which is where the
+ /// quality, the destination and the decision to write at all are. Nothing
+ /// is encoded until the button in it is pressed.
+ pub(crate) fn open_export(&mut self, cx: &mut Context) {
+ if self.export.is_some() {
+ return;
+ }
+ // Nothing to write out, and a refusal rather than a card about it: the
+ // window is empty and the export path is not even chosen yet.
+ if self.session.is_none() {
+ self.notify_user("NOTHING TO EXPORT — open a file first".into());
+ cx.notify();
+ return;
+ }
+ self.export_open = true;
+ // One card at a time, and a waiting row must not outlive the card it
+ // was waiting in. Nor may a half-typed number: the card opens on the
+ // bitrate it will write, never on digits left behind by a closed one.
+ self.keys_open = false;
+ self.rebinding = None;
+ self.mbps_edit = None;
+ cx.notify();
+ }
+
+ /// A format row was clicked. The destination follows it at once -- a WAV
+ /// written to a path ending in `.mp4` is a file every player will lie
+ /// about -- keeping whatever stem the save dialog last left there.
+ pub(crate) fn set_format(&mut self, format: Format) {
+ // The one door both the row and its initial go through, so a format the
+ // card greys out cannot be picked by keyboard either.
+ if let Some(why) = self
+ .session
+ .as_ref()
+ .and_then(|session| format_refusal(session, format))
+ {
+ self.notify_user(format!("NOT {} — {why}", format_label(format)).into());
+ return;
+ }
+ self.format = format;
+ self.export_path = retarget(&self.export_path, format);
+ }
+
+ /// The container row: the same codec in the other box, which retargets the
+ /// destination exactly as picking a codec does -- and does nothing at all
+ /// for a codec with only one box, so the stroke cannot invent a choice the
+ /// card is not offering.
+ pub(crate) fn cycle_container(&mut self) {
+ self.set_format(next_container(self.format));
+ }
+
+ /// The quality rows by keyboard, wrapping. Refused by name where the format
+ /// has no bitrate to pick: a key that silently does nothing is the card
+ /// looking broken.
+ pub(crate) fn cycle_quality(&mut self) {
+ if let Some(why) = bitrate_refusal(self.format) {
+ self.notify_user(why.into());
+ return;
+ }
+ let at = Quality::ALL
+ .iter()
+ .position(|&q| q == self.quality)
+ .unwrap_or(0);
+ self.quality = Quality::ALL[(at + 1) % Quality::ALL.len()];
+ }
+
+ /// The sound's rate by keyboard, wrapping through the offered ones -- the
+ /// picture's quality row for the other half of the file. Refused by name
+ /// where this timeline in this format has no rate to pick, exactly as
+ /// [`Player::cycle_quality`] is: a key that silently does nothing is the
+ /// card looking broken.
+ pub(crate) fn cycle_audio_kbps(&mut self) {
+ if let Some(why) = self.audio_rate_refusal() {
+ self.notify_user(why.into());
+ return;
+ }
+ self.audio_kbps = next_audio_kbps(self.audio_kbps);
+ }
+
+ /// Why the sound row is not a choice right now, the engine answering about
+ /// the very project it would export. No session is the same answer as no
+ /// sound: there is nothing to write either way.
+ pub(crate) fn audio_rate_refusal(&self) -> Option<&'static str> {
+ match &self.session {
+ Some(session) => session.audio_rate_refusal(self.format),
+ None => Some("no sound to write"),
+ }
+ }
+
+ /// The custom bitrate by pointer: the typed digits were the only control in
+ /// this card a mouse could not reach. Clamped to the range the row states
+ /// (the engine's own 1..50 Mbps), and picking the row is part of the step --
+ /// a stepper that moves a number nobody is using would move nothing.
+ pub(crate) fn nudge_mbps(&mut self, step: i32) {
+ self.custom_mbps =
+ (self.custom_mbps as i32 + step).clamp(MBPS_MIN as i32, MBPS_MAX as i32) as u32;
+ self.quality = Quality::Custom;
+ }
+
+ /// The same number under the wheel, one step a notch, up for more: fifty
+ /// presses of a stepper is not a way to reach the top of this range, and the
+ /// wheel is what this editor already moves a value with (the timeline's
+ /// zoom and scroll are the same gesture). Hold-to-run stays the keyboard's,
+ /// as it is on every other card here -- a button that repeats while held is
+ /// not a thing this program has.
+ ///
+ /// It moves the *field* while one is open, exactly as ↑↓ do, so the two
+ /// ways in never disagree about which number is being changed.
+ pub(crate) fn wheel_mbps(&mut self, event: &ScrollWheelEvent) {
+ let by = wheel_delta(event);
+ if by == 0. {
+ return;
+ }
+ let by = by.signum() as i32;
+ match &mut self.mbps_edit {
+ Some(edit) => edit.step(by),
+ None => self.nudge_mbps(by),
+ }
+ }
+
+ /// Opens the custom bitrate's field on the number the row is carrying, and
+ /// picks the row while it is at it: a field typed into is the row being
+ /// chosen, and a number nobody is using would be a number typed at nothing.
+ /// Nothing is committed here -- until enter, the card still exports at the
+ /// bitrate it had.
+ pub(crate) fn edit_mbps(&mut self) {
+ self.quality = Quality::Custom;
+ self.mbps_edit = Some(NumberEdit::new(self.custom_mbps));
+ }
+
+ /// The card's Destination row: the desktop's save dialog, on a background
+ /// thread like the import chooser -- the user may sit in it and the window
+ /// behind must not freeze. No chooser at all leaves the default path, which
+ /// is what the refusal says.
+ pub(crate) fn pick_destination(&mut self, cx: &mut Context) {
+ let default = self.export_path.clone();
+ let picked = cx
+ .background_executor()
+ .spawn(async move { pick_save(&default) });
+ cx.spawn(async move |this, cx| {
+ let picked = picked.await;
+ this.update(cx, |this, cx| {
+ // The dialog outlives the card: an export started meanwhile
+ // took the old path and its notice must name what it wrote.
+ if this.export.is_some() {
+ return;
+ }
+ match picked {
+ // The stem is the user's, the extension is the format's: a
+ // FLAC named `.mp4` is a file every player lies about.
+ Ok(Some(path)) => this.export_path = retarget(&path, this.format),
+ // Cancelled: the default stands, as it did before.
+ Ok(None) => {}
+ Err(text) => {
+ eprintln!("{text}");
+ this.notify_user(text.into());
+ }
+ }
+ cx.notify();
+ })
+ .ok();
+ })
+ .detach();
+ }
+
+ /// The subtitle tracks an export of this timeline carries: every one with a
+ /// cue left in the exported range ([`PlaybackSession::timeline_cues`], the
+ /// very map the file is written from), in the library's own order.
+ ///
+ /// Worked out from the cues each time rather than kept as a pick, which is
+ /// what makes it impossible to desync: a row added or taken off shifts every
+ /// index after it, and a stored list would then name tracks nobody chose.
+ /// `Player::sub_track` stays what it always was -- which track the *overlay*
+ /// draws -- and has no say here.
+ ///
+ /// The honest input and not the final answer: the engine filters it again
+ /// per track (a track that could not be read, a picture one) and says so in
+ /// the card's own words ([`engine::export::planned_subtitles`]).
+ pub(crate) fn export_subs(&self) -> Vec {
+ let Some(session) = self.session.as_ref() else {
+ return Vec::new();
+ };
+ (0..session.subtitles().len())
+ .filter(|&i| !session.timeline_cues(i).is_empty())
+ .collect()
+ }
+
+ /// That list in the card's words ([`subtitle_plan`]): what travels, and the
+ /// reason beside every track that does not -- including the ones
+ /// [`Self::export_subs`] filtered out before the engine ever saw them.
+ pub(crate) fn subtitle_line(&self) -> String {
+ let Some(session) = self.session.as_ref() else {
+ return "none".to_string();
+ };
+ let picks = self.export_subs();
+ let plan = session.planned_subtitles(self.format, picks.iter().copied());
+ match self.format.has_video() {
+ true => subtitle_plan(plan, session.subtitles(), &picks),
+ // A format that is the sound alone has nowhere to put any of them
+ // and the engine says that once, about the file. Naming the cues of
+ // each track under it answers a question the format already closed.
+ false => plan,
+ }
+ }
+
+ /// Writes the edit list out, at the settings the card was left at. Playback
+ /// stops first: the exporter opens its own decoder -- and, on the hardware
+ /// path, an encoder -- so a running player would only compete with it for
+ /// the GPU. A cancelled export still winding down holds this off for the
+ /// frame it takes to notice, which is what keeps its `remove_file` off the
+ /// new output.
+ pub(crate) fn start_export(&mut self, cx: &mut Context) {
+ if self.export.is_some() {
+ return;
+ }
+ let mut settings =
+ export_settings(self.quality, self.custom_mbps, self.format, self.audio_kbps);
+ // Whatever is on the timeline travels -- every track with a cue in the
+ // exported range, not the one row the overlay happens to be drawing.
+ // Set here rather than inside `export_settings`, which the card also
+ // calls for the *estimate* and which nothing else needs a subtitle for.
+ settings.subtitles = self.export_subs();
+ let Some(session) = &mut self.session else {
+ self.notify_user("NOTHING TO EXPORT — open a file first".into());
+ cx.notify();
+ return;
+ };
+ // An emptied timeline is a timeline; it is simply not a file. Refused by
+ // name here rather than written as a project of no frames -- and the
+ // engine refuses it again on the worker (`export::start`), so a caller
+ // that is not this button cannot get past it either. Two fences on
+ // purpose: this one is the one with a keystroke to blame.
+ if session.is_empty() {
+ self.notify_user("NOTHING TO EXPORT — the timeline is empty".into());
+ cx.notify();
+ return;
+ }
+ // The format row can be refused *after* it was picked -- mp4 is the
+ // default and an audio-only timeline (or a second audio lane) is one
+ // edit away -- so the button asks again rather than starting a worker
+ // that will only settle with the same refusal minutes later.
+ if let Some(why) = format_refusal(session, self.format) {
+ self.notify_user(format!("NOT EXPORTED — {why}").into());
+ cx.notify();
+ return;
+ }
+ session.pause();
+ self.export = Some(session.export_to_with(&self.export_path, &settings));
+ // The clock starts with the worker, not with the first repaint that
+ // happens to notice it.
+ self.export_started = Some(Instant::now());
+ self.export_marks.clear();
+ // The card has been answered; the progress line takes the panel from
+ // here, and it is the running export's escape that matters now.
+ self.export_open = false;
+ cx.notify();
+ }
+
+ /// Gives the editor back at once and leaves the worker to stop at its next
+ /// frame and delete what it has written.
+ pub(crate) fn cancel_export(&mut self) {
+ if let Some(export) = &self.export {
+ export.cancel();
+ self.cancelling = true;
+ }
+ }
+
+ /// Takes the export's verdict once it has one. The only place the app
+ /// touches the handle's completion side.
+ pub(crate) fn poll_export(&mut self) {
+ // Sampled here rather than while drawing: a repaint stays a repaint,
+ // and this runs once per repaint either way.
+ if let (Some(progress), Some(started)) = (
+ self.exporting().map(ExportHandle::progress),
+ self.export_started,
+ ) {
+ note_progress(
+ &mut self.export_marks,
+ started.elapsed().as_secs_f32(),
+ progress,
+ );
+ }
+ let Some(result) = self.export.as_ref().and_then(ExportHandle::result) else {
+ return;
+ };
+ self.export = None;
+ self.export_started = None;
+ // A cancellation is reported as an error, and the one who asked for it
+ // has had the editor back since the keystroke. Nothing to say.
+ if std::mem::take(&mut self.cancelling) {
+ return;
+ }
+ let text = match result {
+ Ok(()) => {
+ // Written and still where it was written: the bar carries it
+ // until some other notice takes the bar.
+ self.exported = Some(self.export_path.clone());
+ format!("{EXPORT_DONE}{}", file_name(&self.export_path))
+ }
+ Err(e) => format!("EXPORT FAILED: {e}"),
+ };
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ }
+}
diff --git a/crates/app/src/player/library.rs b/crates/app/src/player/library.rs
new file mode 100644
index 0000000..c40c901
--- /dev/null
+++ b/crates/app/src/player/library.rs
@@ -0,0 +1,929 @@
+//! What comes in and what goes out: the sources, the imports, the
+//! subtitles, the project file and the lanes.
+
+use crate::*;
+
+impl Player {
+ /// The one way a library row reaches the timeline: the Add button and a row
+ /// dragged onto a lane both come here, so there is a single answer to what
+ /// "add this source" does. The whole source goes in as one grouped take at
+ /// `at` -- the frame the pointer let it go on, or the playhead for the
+ /// button, which names no place. It is the same insert a paste makes, so
+ /// everything after it moves along rather than being painted over. Reseeks
+ /// like every other edit, and drops the timeline's selection with it: the
+ /// insert has just moved the indices it pointed at.
+ pub(crate) fn insert_source(
+ &mut self,
+ path: &Path,
+ stream: usize,
+ onto: Option,
+ at: Option,
+ cx: &mut Context,
+ ) {
+ if self.exporting().is_some() {
+ return;
+ }
+ // The lane the pointer named cannot hold this kind of file: refused by
+ // name, in the same words the ghost was tinted by on the way down
+ // ([`lane_refuses`]). The Add button names no lane and so is never
+ // refused here -- where a file goes when nobody says is the engine's
+ // choice, in `place_stream_at`, not one made twice here.
+ if let Some(why) = onto.and_then(|lane| lane_refuses(path, lane)) {
+ self.notify_user(why.into());
+ cx.notify();
+ return;
+ }
+ // The engine's own length for the file, noted when the import took it
+ // in: a row that has never been on a lane is placeable at its full
+ // length, which is the whole point of an import that only fills the
+ // library.
+ let fps = self.fps;
+ let placed = match &mut self.session {
+ // Seconds, because that is what the engine's own door takes: the
+ // frame the pointer named goes back through the same rate every box
+ // on the bed is drawn at, so it lands on the frame it was let go on
+ // rather than a neighbouring one.
+ Some(session) => {
+ let at = at.map_or_else(|| session.now(), |frame| f64::from(frame) / fps);
+ session.place_stream_at(at, path, stream, onto)
+ }
+ None => Ok(false),
+ };
+ match placed {
+ Ok(true) => {
+ self.selected = None;
+ self.reset_after_reseek();
+ }
+ // The engine's own words: a stream that cannot join this timeline
+ // says which property disagrees, exactly as a refused import does.
+ Err(e) => self.notify_user(format!("NOTHING ADDED — {e}").into()),
+ Ok(false) => {
+ self.notify_user("NOTHING ADDED — that file could not be placed here".into())
+ }
+ }
+ cx.notify();
+ }
+
+ /// Takes a library row's file out of the list, which is the one thing a row
+ /// can lose. Refused in the engine's own words while clips still play from
+ /// it -- and those words name the lanes holding them, so the refusal says
+ /// what to delete first. The list itself is the report that it worked: the
+ /// row is gone from it.
+ pub(crate) fn remove_source(&mut self, path: &Path, stream: usize, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let removed = self
+ .session
+ .as_mut()
+ .map(|session| session.remove_source(path, stream));
+ let text = match removed {
+ Some(Ok(idx)) => {
+ // The picked row may be the one that just went, and the engine
+ // reseeks, so this owes the flag reset like every other edit.
+ if self.selected_asset.as_ref() == Some(&(path.to_path_buf(), stream)) {
+ self.selected_asset = None;
+ }
+ // A copied clip names its source by *index*, and every index
+ // past the one that went has just moved down: without this the
+ // next paste puts some other file on the timeline.
+ self.clipboard = clipboard_after_remove(self.clipboard, idx);
+ self.reset_after_reseek();
+ // The last row leaves a session naming no file: nothing to
+ // play, nothing to save and nothing to show, which is the empty
+ // window the editor launches as. The next import scaffolds a
+ // fresh timeline from whatever file it is, at that file's own
+ // rate -- which is why the session goes rather than lingering
+ // on with the gone file's parameters.
+ match self
+ .session
+ .as_ref()
+ .is_some_and(|s| s.sources().is_empty())
+ {
+ true => {
+ self.close_session();
+ format!(
+ "REMOVED {} — the library is empty; import a file to start again",
+ file_name(path)
+ )
+ }
+ // The undo stack goes with it (`Project::remove_source`):
+ // said here, because a `z` that does nothing afterwards
+ // would otherwise read as a bug.
+ false => format!(
+ "REMOVED {} — there is nothing left to undo",
+ file_name(path)
+ ),
+ }
+ }
+ Some(Err(e)) => format!("NOT REMOVED — {e}"),
+ None => "NO TIMELINE — open a file first".to_string(),
+ };
+ self.notify_user(text.into());
+ cx.notify();
+ }
+
+ /// Back to the window the editor launches as: no timeline, no library, no
+ /// picture -- and the hint that says to open a file. What removing the last
+ /// library row leaves, since a session whose library is empty has nothing
+ /// left to be ([`Player::remove_source`]).
+ ///
+ /// Everything a *loaded project* resets goes here for its reasons (an index
+ /// into a timeline that is gone names nothing), plus the three per-file
+ /// caches: they are keyed by path, and the next file to arrive fills them
+ /// again.
+ pub(crate) fn close_session(&mut self) {
+ self.session = None;
+ // The picture goes with it, or the empty window would keep showing the
+ // last frame of a timeline that no longer exists.
+ //
+ // corner-cut: its atlas tile is not released -- `window.drop_image` wants
+ // a `&mut Window` this door has no other reason to take. One tile per
+ // emptied library, against one per displayed frame in `pump`; the
+ // upgrade path is threading the window through `act_on_row`.
+ self.image = None;
+ // The drawn cue with it, and its tile for the same reason as above.
+ self.sub_image = None;
+ self.clipboard = None;
+ self.selected = None;
+ self.selected_asset = None;
+ // The subtitle rows go with the timeline they were on.
+ self.sub_track = 0;
+ self.context_menu = None;
+ self.library_menu = None;
+ self.eq_open = None;
+ self.color_open = None;
+ self.speed_open = None;
+ self.close_silence();
+ self.waves.clear();
+ self.streams.clear();
+ self.bitrates.clear();
+ self.sizes.clear();
+ self.syncs.clear();
+ // Scanned off sources that are not in the library any more.
+ self.silence_levels.clear();
+ // Every gesture in flight, dropped for `reset_after_reseek`'s reason
+ // (it drops the trim below): a drag holds a bar, a clip or a band of a
+ // timeline that has just stopped existing.
+ self.scrubbing = false;
+ self.volume_dragging = false;
+ self.eq_dragging = false;
+ self.speed_dragging = false;
+ self.color_dragging = false;
+ self.pending_color = None;
+ self.pending_speed = None;
+ self.displayed = 0;
+ self.dropped = 0;
+ self.started = None;
+ // The empty window's own: no name in the titlebar, nowhere chosen to
+ // export or save to yet, and a rate that only keeps the timecode
+ // reading in frames until a file brings its own (`main`).
+ self.name = NO_FILE.into();
+ self.export_path = PathBuf::new();
+ self.project_path = PathBuf::new();
+ self.fps = 30.;
+ // No decoder to wait for a frame from: the hint is what shows. The
+ // transport reads `Stopped` from the session being gone, so there is no
+ // end-of-stream state left to clear here.
+ self.reset_after_reseek();
+ self.seek_since = None;
+ }
+
+ /// The rate and layout the whole timeline's audio is, taken from the stream
+ /// of the first source that could have one: what a library row has to match
+ /// to be placeable. `None` until that file has been probed, and then nothing
+ /// is greyed for it.
+ ///
+ /// The first source that is *not a still*, which is the rule the engine
+ /// holds every import to (`playback::audio_source_of`) -- a picture at the
+ /// head of the list (a removal moves indexes) has no stream to describe
+ /// anything with.
+ pub(crate) fn timeline_audio(&self) -> Option<(u32, u16)> {
+ let first = self
+ .session
+ .as_ref()?
+ .sources()
+ .iter()
+ .find(|s| !engine::is_image(&s.path))?;
+ let info = self
+ .streams
+ .get(&first.path)?
+ .iter()
+ .find(|s| s.index == first.audio_stream)?;
+ Some((info.sample_rate, info.channels))
+ }
+
+ /// Queues a file for the library. Nothing is read here: the reading is
+ /// [`read_ahead`] on a worker, and [`Player::take_import`] is what finally
+ /// touches the timeline, one repaint later and with the pages warm. A drop
+ /// is not a key press, so the export guard on the key handler does not
+ /// cover it and this checks for itself.
+ ///
+ /// One file at a time, in arrival order: a drop can carry six and argv can
+ /// name more, and six header walks racing over one disk finish no sooner
+ /// than six in a row -- while the line above the panel has exactly one file
+ /// to name.
+ pub(crate) fn import(&mut self, path: &std::path::Path, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ self.imports.push_back(path.to_path_buf());
+ self.start_import(cx);
+ }
+
+ /// Starts the worker for the next queued file, if no worker is running.
+ /// Called again as each import lands, which is what drains the queue.
+ pub(crate) fn start_import(&mut self, cx: &mut Context) {
+ if self.importing.is_some() {
+ return;
+ }
+ let Some(path) = self.imports.pop_front() else {
+ return;
+ };
+ let stage = Arc::new(std::sync::atomic::AtomicU8::new(ImportStage::Header as u8));
+ let cancelled = Arc::new(std::sync::atomic::AtomicBool::new(false));
+ // The fork is made here, once, and carried to the landing: an import is
+ // *probed* on the worker and registered from what came back, while the
+ // file argv named -- and a file arriving at a window with no timeline to
+ // import into -- is *opened* on the worker and handed over whole. None
+ // of the three leaves the UI thread anything to read: a cold 24 GB
+ // header walk is twenty seconds, and the window keeps painting through
+ // all of them.
+ let what = arrival(self.opening.as_deref(), &path);
+ // The timeline the file will be checked against, taken here because the
+ // worker cannot reach the session: two clones and no disk
+ // ([`PlaybackSession::import_gate`]). `None` is a window with nothing to
+ // import into, which is the fork that opens the file outright.
+ let gate = self.session.as_ref().map(PlaybackSession::import_gate);
+ let read = cx.background_executor().spawn({
+ let (path, stage) = (path.clone(), Arc::clone(&stage));
+ async move { open_ahead(what, &path, &stage, gate) }
+ });
+ let now = Instant::now();
+ self.importing = Some(Import {
+ path: path.clone(),
+ started: now,
+ stage,
+ seen: ImportStage::Header,
+ since: now,
+ cancelled: Arc::clone(&cancelled),
+ });
+ cx.spawn(async move |this, cx| {
+ let landed = read.await;
+ this.update(cx, |this, cx| {
+ this.importing = None;
+ // Cancelled while it read: the window was given back at the
+ // click and said so then, so what the worker carried is dropped
+ // without a second word ([`Player::cancel_import`]).
+ if cancelled.load(std::sync::atomic::Ordering::Relaxed) {
+ return;
+ }
+ this.take_import(&path, landed, cx);
+ // The next one is started by the repaint this notified, which
+ // is also what starts the files argv named ([`poll_import`]).
+ })
+ .ok();
+ })
+ .detach();
+ cx.notify();
+ }
+
+ /// Keeps the import line's two clocks honest: the elapsed one runs from the
+ /// worker, and the stall one from the last time the stage it is naming
+ /// actually changed. Sampled here rather than while drawing, for
+ /// [`Player::poll_export`]'s reason.
+ ///
+ /// ...and starts whatever is queued behind it, which is the one place the
+ /// files argv named can begin: they are put in the queue before there is a
+ /// context to spawn a worker from.
+ pub(crate) fn poll_import(&mut self, cx: &mut Context) {
+ match &mut self.importing {
+ Some(import) => {
+ import.poll();
+ }
+ None => self.start_import(cx),
+ }
+ }
+
+ /// The Cancel beside the import line: the window is given back at once and
+ /// the file does not land. Everything queued behind it goes too -- a person
+ /// who has stopped an import of six dropped files has stopped the six, and
+ /// leaving five to start themselves would be the same wait under another
+ /// name.
+ ///
+ /// The read in flight is *not* stopped, for the reason [`Import::cancelled`]
+ /// gives, and the notice says as much rather than promising the disk went
+ /// quiet.
+ pub(crate) fn cancel_import(&mut self, cx: &mut Context) {
+ let Some(import) = self.importing.take() else {
+ return;
+ };
+ import
+ .cancelled
+ .store(true, std::sync::atomic::Ordering::Relaxed);
+ let waiting = self.imports.len();
+ self.imports.clear();
+ let tail = match waiting {
+ 0 => String::new(),
+ n => format!(" — {n} more dropped from the queue"),
+ };
+ let text = format!(
+ "IMPORT CANCELLED: {}{tail} — the read already running finishes unheeded",
+ file_name(&import.path)
+ );
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ cx.notify();
+ }
+
+ /// Takes a read-ahead file into the library and nowhere else: the timeline
+ /// is not touched, and the row is dragged onto a lane when it is wanted
+ /// there. Nothing moves, so nothing reseeks; a refusal is shown as the
+ /// engine worded it and changes nothing.
+ ///
+ /// The export guard again, and not for the caller's sake: an export can
+ /// have started during the seconds the worker was reading, and a drop
+ /// during an export has always been a silent no-op.
+ pub(crate) fn take_import(&mut self, path: &std::path::Path, landed: Landed, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ // The file argv named is the one file in the queue that is not an
+ // import: it *is* the timeline, and the worker has already opened it.
+ // All that is left here is to hang everything derived from it off the
+ // window -- the clock, the title, where an export and a save go -- and
+ // that is arithmetic, not a read.
+ let (subs, probe) = match landed {
+ Landed::Read(subs, probe) => (subs, probe),
+ what => {
+ // Only when *this* is the file argv named: a project dropped
+ // while that one is still being read must not make it land as
+ // an import.
+ if self.opening.as_deref() == Some(path) {
+ self.opening = None;
+ }
+ match what {
+ Landed::Project(opened) => self.install_project(path, opened, cx),
+ Landed::Media(opened, place) => {
+ let text = self.install_media(path, opened, place);
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ cx.notify();
+ }
+ Landed::Read(..) => unreachable!("matched above"),
+ }
+ // The line a launch has always printed, now printed when the
+ // file actually arrives: it is the mark that says the timeline
+ // is up, as the window's own appearance is the other one.
+ if let Some(meta) = self.session.as_ref().map(PlaybackSession::meta) {
+ println!(
+ "{}: {}x{} @ {:.2} fps, {} samples",
+ path.display(),
+ meta.width,
+ meta.height,
+ meta.frame_rate,
+ meta.frame_count
+ );
+ }
+ return;
+ }
+ };
+ // An empty window has no library to add to yet: the file opens one, and
+ // the timeline under it stays empty, because an import is an import
+ // whether or not a session was already up. A file *named at launch* is
+ // the other fork -- that one is an open, and it does become the
+ // timeline (`main`).
+ // A subtitle file is not a source and lands on no lane: it joins the
+ // timeline's own list of them, which is what the library's subtitle
+ // section shows and what the overlay draws. With no timeline open there
+ // is nothing for the cues to be timed against, and it says so.
+ if is_subtitle(path) {
+ self.take_subtitles(path, subs, cx);
+ return;
+ }
+ // The container was read on the worker and what came back is registered
+ // here ([`engine::PlaybackSession::import_probed`]): no header walk, no
+ // decoder open, no probe of the timeline's own first source -- the three
+ // reads that used to be spent on this thread. A song and a still fork
+ // before the demuxer and pay their own small read
+ // ([`engine::PlaybackSession::import`]); a window whose timeline went
+ // away while the worker read falls to the slow door below, which is the
+ // one that can still open one.
+ let registered = match (self.session.as_mut(), probe) {
+ (Some(session), Some(Ok(probe))) => Some(session.import_probed(path, probe)),
+ (Some(_), Some(Err(refused))) => Some(Err(refused)),
+ (Some(session), None) => Some(session.import(path)),
+ (None, _) => None,
+ };
+ let text = match registered {
+ Some(Ok(_)) => {
+ // The file's own subtitle tracks with it, exactly as an open
+ // takes them: an import is the other door the same file arrives
+ // through. The cues were read on the worker
+ // ([`read_ahead`]); what happens here is the push.
+ let tail = self
+ .session
+ .as_mut()
+ .and_then(|session| subtitle_tail(session, subs))
+ .unwrap_or_default();
+ format!(
+ "IMPORTED {} to the library — drag it onto a lane to place it{tail}",
+ file_name(path)
+ )
+ }
+ // Named, because two files can fail in one launch and the queue now
+ // shows both: "No such file or directory" twice over, with nothing
+ // saying which file, is two messages that answer nothing.
+ Some(Err(e)) => format!("IMPORT FAILED: {} — {e}", file_name(path)),
+ None => self.open_media(path, false, subs),
+ };
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ cx.notify();
+ }
+
+ /// Takes a file as the session an empty window is waiting for. Everything
+ /// derived from the media -- the clock, the title, where an export and a
+ /// save go -- is set here, exactly as a launch with a file argument sets
+ /// it. Paused with its first frame showing, like every other way a timeline
+ /// arrives.
+ ///
+ /// `place` is the difference between the two doors that come here: a file
+ /// *opened* is the timeline, one *imported* into an empty window fills the
+ /// library and leaves the lanes empty for a drag.
+ pub(crate) fn open_media(&mut self, path: &std::path::Path, place: bool, subs: Subs) -> String {
+ self.install_media(path, open_session(path, place, subs), place)
+ }
+
+ /// The second half of it: everything the window derives from a session that
+ /// has already been opened. Split from the open itself because the file
+ /// argv named is opened on a worker ([`open_ahead`]) -- the twelve seconds
+ /// of a cold header walk are not the UI thread's to spend -- and lands
+ /// here, where nothing is read and nothing blocks.
+ pub(crate) fn install_media(
+ &mut self,
+ path: &std::path::Path,
+ opened: Result<(PlaybackSession, String), String>,
+ place: bool,
+ ) -> String {
+ match opened {
+ Ok((session, subs)) => {
+ self.fps = session.meta().frame_rate;
+ // Read before the session moves: a file that plays silent says
+ // so here or nowhere.
+ let silent = audio_notice(&session);
+ // A file replaces the one that was open, and track 3 of that one
+ // is not track 3 of this.
+ self.sub_track = 0;
+ self.session = Some(session);
+ // A fresh session comes up at full volume; the player's own
+ // setting outlives the file, so it is pushed at every new one.
+ self.apply_volume();
+ // Beside the new file, but still the format the card is set to:
+ // opening another clip is not a change of mind about that.
+ self.export_path = retarget(&export_path(path), self.format);
+ self.project_path = project_path(path);
+ self.name = file_name(path).into();
+ self.reset_after_reseek();
+ let name = file_name(path);
+ // The library is filled and the timeline is empty; the only
+ // thing that says so is this line, so it says what to do next.
+ let what = match place {
+ true => format!("OPENED {name}"),
+ false => {
+ format!("IMPORTED {name} to the library — drag it onto a lane to place it")
+ }
+ };
+ format!("{what}{}{subs}", silent.unwrap_or_default())
+ }
+ Err(e) => format!("OPEN FAILED: {e}"),
+ }
+ }
+
+ /// The Import button: asks the desktop for a path and takes it the same way
+ /// a drop would. The chooser is another process and the user may sit in it,
+ /// so it runs on a background thread -- blocking here would freeze the
+ /// window behind the dialog.
+ pub(crate) fn pick_and_import(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let picked = cx
+ .background_executor()
+ .spawn(async { pick_file("edith — import") });
+ cx.spawn(async move |this, cx| {
+ let picked = picked.await;
+ this.update(cx, |this, cx| match picked {
+ // One queue, and the fork is made when its worker starts
+ // ([`arrival`]): a project replaces the timeline, media joins
+ // the library, and neither is read on this thread.
+ Ok(Some(path)) => this.import(&path, cx),
+ // Cancelled: the user already knows what happened.
+ Ok(None) => {}
+ Err(text) => {
+ eprintln!("{text}");
+ this.notify_user(text.into());
+ cx.notify();
+ }
+ })
+ .ok();
+ })
+ .detach();
+ }
+
+ /// The `+ S` button and its key: asks the desktop for a file and takes the
+ /// subtitle tracks out of it -- a standalone `.srt`/`.vtt`/`.ass` is one of
+ /// them, a Matroska however many are inside. Only the subtitles: the file
+ /// itself does not join the library, which is what the Import button beside
+ /// this one is for.
+ ///
+ /// The chooser is another process and the user may sit in it, so it runs on
+ /// a background thread, exactly as [`Player::pick_and_import`] does.
+ pub(crate) fn pick_and_add_subtitles(&mut self, cx: &mut Context) {
+ // What dims the `+ S` button, asked here as well so the key answers the
+ // same question -- and *before* the chooser rather than after it: a door
+ // that opens a dialog, waits for a file and only then says the timeline
+ // was never there is the second door disagreeing with the first.
+ if let Some(why) = self.enable(ActionId::AddSubtitleTrack, None).why() {
+ let text = format!("NO SUBTITLES ADDED — {why}");
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ cx.notify();
+ return;
+ }
+ let picked = cx
+ .background_executor()
+ .spawn(async { pick_file("edith — subtitles to add") });
+ cx.spawn(async move |this, cx| {
+ let picked = picked.await;
+ this.update(cx, |this, cx| match picked {
+ Ok(Some(path)) => this.add_subtitles(&path, cx),
+ // Cancelled: the user already knows what happened.
+ Ok(None) => {}
+ Err(text) => {
+ eprintln!("{text}");
+ this.notify_user(text.into());
+ cx.notify();
+ }
+ })
+ .ok();
+ })
+ .detach();
+ }
+
+ /// Takes a file's subtitle tracks onto the timeline, off the render thread.
+ /// The walk reads the whole container for its cues
+ /// (`engine::PlaybackSession::parse_subtitles`) -- ~200 ms on a two-hour 4K
+ /// remux and 1.3 s on a cold 3 GB one -- and a button that costs the window
+ /// that many frames is a button that freezes it. So the *parse* is the
+ /// worker's, whole, and the UI thread only pushes what came back
+ /// ([`PlaybackSession::add_subtitle_tracks`]): no borrow crosses the await,
+ /// because the parse is an associated fn that owns nothing.
+ pub(crate) fn add_subtitles(&mut self, path: &std::path::Path, cx: &mut Context) {
+ // Nothing to time the cues against: said now rather than after a walk
+ // of a 25 GB file that was never going to be kept.
+ if self.session.is_none() {
+ self.landed_subtitles(path, None, cx);
+ return;
+ }
+ self.notify_user(format!("READING {} for subtitles…", file_name(path)).into());
+ let parsed = cx.background_executor().spawn({
+ let path = path.to_path_buf();
+ async move { engine::PlaybackSession::parse_subtitles(&path) }
+ });
+ let path = path.to_path_buf();
+ cx.spawn(async move |this, cx| {
+ let parsed = parsed.await;
+ this.update(cx, |this, cx| {
+ // The dedupe lives inside the push, so a second `+ S` on the
+ // same file still answers 0 and still says so below.
+ let added = this
+ .session
+ .as_mut()
+ .map(|session| parsed.and_then(|tracks| pushed(session, &path, tracks)));
+ this.landed_subtitles(&path, added, cx);
+ })
+ .ok();
+ })
+ .detach();
+ cx.notify();
+ }
+
+ /// Every subtitle track a `.srt`/`.vtt`/`.ass` carries, onto the timeline
+ /// and nowhere else: they are not clips and land on no lane. The cues came
+ /// off the worker that read the file ([`read_ahead`]), like every other
+ /// door's do, and what is left here is the push. The engine dedupes by
+ /// (file, track), so the same `.srt` twice is one row and says so.
+ pub(crate) fn take_subtitles(&mut self, path: &std::path::Path, subs: Subs, cx: &mut Context) {
+ let added = self
+ .session
+ .as_mut()
+ .map(|session| subs.and_then(|tracks| pushed(session, path, tracks)));
+ self.landed_subtitles(path, added, cx);
+ }
+
+ /// What the timeline says once the tracks are on it, whichever worker did
+ /// the reading: the `+ S` button and its key ([`Self::add_subtitles`]), a
+ /// dropped or imported subtitle file ([`Self::take_subtitles`]), and a
+ /// window with nothing to time cues against all word the outcome here,
+ /// once, so no two doors can drift apart.
+ pub(crate) fn landed_subtitles(
+ &mut self,
+ path: &std::path::Path,
+ added: Option>,
+ cx: &mut Context,
+ ) {
+ let text = match added {
+ Some(Ok(0)) => format!(
+ "{}'s subtitles are on the timeline already",
+ file_name(path)
+ ),
+ Some(Ok(n)) => format!(
+ "SUBTITLES {} — {n} track(s), showing over the picture, {} hides them",
+ file_name(path),
+ self.keymap.display(ActionId::ToggleSubtitles)
+ ),
+ Some(Err(e)) => format!("SUBTITLE IMPORT FAILED: {e}"),
+ None => "NO SUBTITLES ADDED — open a file for them to run against first".to_string(),
+ };
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ cx.notify();
+ }
+
+ /// The × on a subtitle row, and the stroke that takes the picked one off:
+ /// the track leaves the timeline and the pick moves with it. Every index
+ /// past the one that went moves down
+ /// ([`engine::Project::remove_subtitles`]), so a pick left where it was
+ /// would name a *different* track -- and the pick is what an export writes
+ /// into the file.
+ ///
+ /// Not an undo step: subtitles are not on the history's snapshots, so the
+ /// way back is putting the file's subtitles on again -- which is a door of
+ /// its own ([`Player::pick_and_add_subtitles`]) and reads the subtitles
+ /// alone, never the media. The notice says that rather than promising a
+ /// ctrl+z that would do nothing.
+ pub(crate) fn remove_subtitle_track(&mut self, track: usize, cx: &mut Context) {
+ // The one availability oracle, for the same reason the × on a row and
+ // the stroke are one call: an empty list is not a failure, it is an
+ // action with nothing to act on, and the engine's "there is no subtitle
+ // track 0" is an index nobody typed. A real removal that fails still
+ // says what the engine said, below.
+ if let Some(why) = self.enable(ActionId::RemoveSubtitleTrack, None).why() {
+ let text = format!("NO SUBTITLES REMOVED — {why}");
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ cx.notify();
+ return;
+ }
+ // Read before it goes: a notice naming an index names nothing.
+ let name = self
+ .session
+ .as_ref()
+ .and_then(|session| sub_pick_name(session.subtitles(), track))
+ .unwrap_or_else(|| format!("subtitle track {track}"));
+ let text = match self
+ .session
+ .as_mut()
+ .map(|session| session.remove_subtitles(track))
+ {
+ Some(Ok(())) => {
+ let left = self
+ .session
+ .as_ref()
+ .map_or(0, |session| session.subtitles().len());
+ self.sub_track = sub_pick_after_removal(self.sub_track, track, left);
+ // The drawn cue is keyed by that index ([`Player::sub_picture`])
+ // and the index now stands for another track.
+ //
+ // corner-cut: its atlas tile is not released -- `close_session`'s
+ // note, for its reason and with its upgrade path.
+ self.sub_image = None;
+ format!(
+ "{name} REMOVED — {} puts a file's subtitles back on, the file itself stays off",
+ self.keymap.display(ActionId::AddSubtitleTrack)
+ )
+ }
+ Some(Err(e)) => format!("NO SUBTITLES REMOVED — {e}"),
+ None => "NO SUBTITLES REMOVED — open a file first".to_string(),
+ };
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ cx.notify();
+ }
+
+ /// Swaps the whole timeline for one restored from a `.edith`, for
+ /// [`Player::install_media`]'s reason: the open is a worker's -- a project
+ /// naming a 24 GB film opens that film, which is the same twenty seconds
+ /// ([`arrival`] sends every `.edith` through the one queue) -- and this is
+ /// what is left once it lands. Nothing is replaced until the new session is
+ /// in hand, so a refusal is shown as the engine worded it and leaves what is
+ /// playing alone.
+ pub(crate) fn install_project(
+ &mut self,
+ path: &std::path::Path,
+ opened: Result,
+ cx: &mut Context,
+ ) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let text = match opened {
+ Ok(session) => {
+ self.fps = session.meta().frame_rate;
+ let silent = audio_notice(&session);
+ // A project is named after itself but still exports beside its
+ // media: that is the only place an export has ever landed.
+ self.export_path = retarget(&export_path(&session.sources()[0].path), self.format);
+ self.session = Some(session);
+ self.apply_volume();
+ self.project_path = path.to_path_buf();
+ self.name = file_name(path).into();
+ // A copied clip names its source by index, which means a
+ // different file -- or none -- in another project.
+ self.clipboard = None;
+ self.selected = None;
+ // A menu can be up while a project is dropped on the window --
+ // the scrim swallows clicks, never a drop -- and its index
+ // would name some other timeline's clip. The two clip cards
+ // hold a (lane, idx) of the old timeline for the same reason.
+ self.context_menu = None;
+ self.eq_open = None;
+ self.color_open = None;
+ // Marks are timeline frames of the timeline that was.
+ self.close_silence();
+ // A different set of sources: the row that was picked is not
+ // the file that index names any more -- and neither is the
+ // subtitle track that was showing.
+ self.selected_asset = None;
+ self.sub_track = 0;
+ // The counters describe one timeline; the eof line must not
+ // report the old one's frames against the new one.
+ self.displayed = 0;
+ self.dropped = 0;
+ self.started = None;
+ // Loaded paused at its saved playhead, so the still it owes
+ // reaches the screen the way a seek's does. The old picture is
+ // released by the swap in `pump`, as after any other seek.
+ self.reset_after_reseek();
+ format!("LOADED {}{}", file_name(path), silent.unwrap_or_default())
+ }
+ Err(e) => format!("OPEN FAILED: {e}"),
+ };
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ cx.notify();
+ }
+
+ /// Writes the timeline back to its project file. Overwrites silently, like
+ /// an export: the path was chosen once and the notice is the confirmation.
+ pub(crate) fn save_project(&mut self, cx: &mut Context) {
+ let saved = self
+ .session
+ .as_ref()
+ .map(|session| session.save_project(&self.project_path));
+ let text = match saved {
+ Some(Ok(())) => format!("SAVED {}", file_name(&self.project_path)),
+ Some(Err(e)) => format!("SAVE FAILED: {e}"),
+ None => "NOTHING TO SAVE — open a file first".to_string(),
+ };
+ eprintln!("{text}");
+ self.notify_user(text.into());
+ cx.notify();
+ }
+
+ /// A new empty track under the ones already there. One undo step in the
+ /// engine, so the stroke that takes back an edit takes back a track too, and
+ /// no reseek: nothing plays differently until something is dropped on it.
+ /// The selection stays -- the lanes it indexes into have not moved.
+ pub(crate) fn add_lane(&mut self, kind: LaneKind, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ match &mut self.session {
+ Some(session) => {
+ let lane = session.add_lane(kind);
+ self.notify_user(
+ format!(
+ "{} ADDED — drag a clip onto it, {} takes it back",
+ lane.label(),
+ self.keymap.display(ActionId::Undo)
+ )
+ .into(),
+ );
+ }
+ None => self.notify_user("NO TRACK ADDED — open a file first".into()),
+ }
+ cx.notify();
+ }
+
+ /// The × in a track's header: the add taken back, one undo step, and the
+ /// engine's own words when it refuses -- those name the clips still on the
+ /// track, so the notice says what to delete first. A removal never deletes a
+ /// clip.
+ ///
+ /// Everything holding a `(lane, idx)` is dropped, because the tracks below
+ /// the one that went have just moved up an `ord`
+ /// ([`engine::Project::remove_lane`]): a selection or an open card kept
+ /// across it would be pointing at the *next* track's clip.
+ pub(crate) fn remove_lane(&mut self, lane: Lane, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let removed = self
+ .session
+ .as_mut()
+ .map(|session| session.remove_lane(lane));
+ let text = match removed {
+ Some(Ok(())) => {
+ self.selected = None;
+ self.context_menu = None;
+ self.eq_open = None;
+ self.color_open = None;
+ self.speed_open = None;
+ self.close_silence();
+ format!(
+ "{} REMOVED — {} brings it back",
+ lane.label(),
+ self.keymap.display(ActionId::Undo)
+ )
+ }
+ Some(Err(e)) => format!("NO TRACK REMOVED — {e}"),
+ None => "NO TRACK REMOVED — open a file first".to_string(),
+ };
+ self.notify_user(text.into());
+ cx.notify();
+ }
+
+ /// A header let go over another header: the track in the hand takes that
+ /// one's place in the stack, clips and all
+ /// ([`engine::Project::move_lane`]), one undo step. The gesture every
+ /// editor reorders tracks with, and the only way the order is ever changed
+ /// -- there is no second list of it to keep in step.
+ ///
+ /// Display order is the stack, so moving a video track past another video
+ /// track changes which picture wins, here and in an export alike; audio is
+ /// summed and does not care, which is what makes `A1` above `V1` a purely
+ /// visual arrangement. A label is a position among the tracks of its kind,
+ /// so a track that crossed one of its own kind comes back under a different
+ /// name -- and everything holding a `(lane, idx)` is dropped exactly then,
+ /// for [`Player::remove_lane`]'s reason: those handles now name another
+ /// track's clip. A move that crossed only the other kind renames nothing
+ /// and keeps the selection.
+ pub(crate) fn reorder_lane(&mut self, lane: Lane, onto: Lane, cx: &mut Context) {
+ self.lane_drop = None;
+ if self.exporting().is_some() {
+ return;
+ }
+ let Some(session) = self.session.as_mut() else {
+ return;
+ };
+ let Some(to) = session.lanes().iter().position(|&l| l == onto) else {
+ return;
+ };
+ // Picked up and put back down where it was is a click, and a click says
+ // nothing -- `move_lane` refuses it and every other no-op.
+ let Some(moved) = session.move_lane(lane, to) else {
+ cx.notify();
+ return;
+ };
+ if moved != lane {
+ self.selected = None;
+ self.context_menu = None;
+ self.eq_open = None;
+ self.color_open = None;
+ self.speed_open = None;
+ self.close_silence();
+ }
+ self.notify_user(
+ format!(
+ "{} IS TRACK {} NOW — {} puts it back",
+ moved.label(),
+ to + 1,
+ self.keymap.display(ActionId::Undo)
+ )
+ .into(),
+ );
+ cx.notify();
+ }
+
+ /// What the remove keys act on: the last track of that kind, which is the
+ /// one the matching add key appended. Nothing at all before a file is open,
+ /// where the timeline drawn is a placeholder pair.
+ pub(crate) fn remove_last_lane(&mut self, kind: LaneKind, cx: &mut Context) {
+ let last = self.session.as_ref().and_then(|session| {
+ session
+ .lanes()
+ .into_iter()
+ .filter(|l| l.kind == kind)
+ .next_back()
+ });
+ match last {
+ Some(lane) => self.remove_lane(lane, cx),
+ None => {
+ self.notify_user("NO TRACK REMOVED — open a file first".into());
+ cx.notify();
+ }
+ }
+ }
+}
diff --git a/crates/app/src/player/mod.rs b/crates/app/src/player/mod.rs
new file mode 100644
index 0000000..4e81827
--- /dev/null
+++ b/crates/app/src/player/mod.rs
@@ -0,0 +1,10 @@
+//! What the window does, one module per kind of doing. The state itself
+//! (`struct Player`) stays beside the render in `main.rs`; these are the
+//! detached halves of the one `impl` it used to carry.
+pub mod actions;
+pub mod cards;
+pub mod export;
+pub mod library;
+pub mod timeline_edit;
+pub mod transport;
+pub mod view;
diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs
new file mode 100644
index 0000000..fda07ab
--- /dev/null
+++ b/crates/app/src/player/timeline_edit.rs
@@ -0,0 +1,895 @@
+//! Editing the timeline itself: the cuts, the clipboard, the drags, the
+//! snapping and the trims.
+
+use crate::*;
+
+impl Player {
+ /// Splits the clip under the playhead. Metadata only: the timeline->source
+ /// mapping is unchanged, so nothing reseeks and no flag is touched.
+ pub(crate) fn cut(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ // Snapped to the source's own grid where one is within reach
+ // ([`Player::cut_frame`]): a cut a third of a second off a sync point
+ // looks identical on the bed and turns an export that copies its
+ // picture in minutes into one that codes every frame of it for hours.
+ // The playhead goes with it -- what was cut has to be where the line
+ // is, or the next stroke acts a few frames from where it looks.
+ let Some(session) = &self.session else {
+ return;
+ };
+ let now = frame_at(session.now(), self.fps);
+ let at = self.cut_frame(now);
+ if at != now {
+ self.seek(f64::from(at) / self.fps, cx);
+ }
+ if let Some(session) = &mut self.session {
+ session.cut_at(f64::from(at) / self.fps);
+ }
+ self.selected = None;
+ cx.notify();
+ }
+
+ /// Rejoins whatever meets under the playhead and puts it back in one group
+ /// -- the inverse of [`Player::cut`], and metadata only like it. The engine
+ /// decides what is joinable; a refusal is worded here, because `false` is
+ /// all it says and a key that looks broken is worse than one that explains
+ /// itself.
+ pub(crate) fn regroup(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ if let Some(session) = &mut self.session {
+ if session.regroup_at(session.now()) {
+ self.selected = None;
+ } else {
+ self.notify_user(
+ "NOTHING TO REGROUP — put the playhead where two clips meet, on frames that were cut apart"
+ .into(),
+ );
+ }
+ }
+ cx.notify();
+ }
+
+ /// Takes the selected clip out of its group, so the picture and the sound
+ /// under it are edited apart from here on: each half selects, moves, trims
+ /// and is removed alone, and both draw outlined instead of tinted. The
+ /// selection stays -- the half that was clicked is still the half in hand.
+ pub(crate) fn detach(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ match (&mut self.session, self.selected) {
+ (Some(session), Some((lane, idx))) => {
+ if !session.ungroup(lane, idx) {
+ self.notify_user(
+ "NOTHING DETACHED — that clip is not grouped with another".into(),
+ );
+ }
+ }
+ (Some(_), None) => {
+ self.notify_user("NOTHING DETACHED — click the take to take apart first".into())
+ }
+ (None, _) => {}
+ }
+ cx.notify();
+ }
+
+ /// Puts the selected clip back in a group with the clip covering exactly the
+ /// same frames on another track -- the way back from [`Player::detach`], and
+ /// the way to group a picture with sound it was never opened with. The
+ /// partner is not clicked because there is nothing to choose: a group id
+ /// names one span, so only a clip covering these very frames could join it,
+ /// and the engine words what to do when none does.
+ pub(crate) fn group(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let partner = match (&self.session, self.selected) {
+ (Some(session), Some((lane, idx))) => span_partner(session, lane, idx),
+ _ => None,
+ };
+ match (&mut self.session, self.selected, partner) {
+ (Some(session), Some((lane, idx)), Some((other, o_idx))) => {
+ if let Err(e) = session.group(lane, idx, other, o_idx) {
+ self.notify_user(format!("NOT GROUPED — {e}").into());
+ }
+ }
+ (Some(_), Some(_), None) => {
+ self.notify_user(
+ "NOTHING TO GROUP WITH — no clip on another track covers exactly these frames"
+ .into(),
+ )
+ }
+ (Some(_), None, _) => {
+ self.notify_user("NOTHING GROUPED — click one of the halves first".into())
+ }
+ (None, ..) => {}
+ }
+ cx.notify();
+ }
+
+ /// Drops the selected clip and closes the hole: a whole take goes, both
+ /// lanes of it, and everything after it moves up. A half with no take under
+ /// it in the video lane -- what a lift leaves behind -- has nothing to
+ /// ripple, so that one is lifted instead. The engine reseeks itself, so all
+ /// this owes is the flag reset.
+ pub(crate) fn delete_selected(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let selected = self.selected.take();
+ // Whichever lane it was clicked in: the index is that lane's own, and
+ // the ripple cuts the clip's span out of every lane -- a group covers
+ // one span, so deleting a take by its audio half is the same edit as by
+ // its picture. What is not a whole take is lifted instead, which is what
+ // reaches a clip on an added track ([`whole_take`]).
+ let deleted = match (&mut self.session, selected) {
+ (Some(session), Some((lane, idx))) => match whole_take(session, lane, idx) {
+ true => session.delete_clip(lane, idx),
+ false => session.lift_clip(lane, idx),
+ },
+ _ => false,
+ };
+ if selected.is_some() && !deleted {
+ self.notify_user("NOTHING DELETED — that clip is no longer there".into());
+ }
+ if deleted {
+ self.reset_after_reseek();
+ }
+ cx.notify();
+ }
+
+ /// Lifts the selected half out and leaves the hole: black picture there if
+ /// it was the video lane, silence if it was the audio one, and nothing else
+ /// moves. What Delete is not.
+ pub(crate) fn lift_selected(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ match (&mut self.session, self.selected.take()) {
+ (Some(session), Some((lane, idx))) => {
+ if session.lift_clip(lane, idx) {
+ self.reset_after_reseek();
+ } else {
+ self.notify_user("NOTHING LIFTED — that half is no longer there".into());
+ }
+ }
+ (Some(_), None) => {
+ self.notify_user("NOTHING LIFTED — click the half to remove first".into())
+ }
+ (None, _) => {}
+ }
+ cx.notify();
+ }
+
+ /// Copies the selected clip. Nothing on screen changes, so no notify.
+ pub(crate) fn copy_selected(&mut self) {
+ let session = self.session.as_ref();
+ // Out of the lane it was clicked in: the audio half of a group is a
+ // different clip from the video one, and copying the wrong lane's
+ // frames is a paste of the wrong thing.
+ if let Some(clip) = self
+ .selected
+ .and_then(|(lane, idx)| session?.lane_clips(lane).get(idx).copied())
+ {
+ self.clipboard = Some(clip);
+ }
+ }
+
+ /// The group id of the clicked clip, which is what marks the other half.
+ pub(crate) fn selected_link(&self) -> Option {
+ let (lane, idx) = self.selected?;
+ self.session.as_ref()?.lane_clips(lane).get(idx)?.link
+ }
+
+ /// Drops the copied clip in at the playhead. The engine reseeks itself, so
+ /// like a delete this owes the flag reset -- and the selection, whose index
+ /// the insert has just moved.
+ pub(crate) fn paste(&mut self, cx: &mut Context) {
+ let pasted = match (&mut self.session, self.clipboard) {
+ (Some(session), Some(clip)) => session.paste_at(session.now(), clip),
+ _ => false,
+ };
+ if pasted {
+ self.selected = None;
+ self.reset_after_reseek();
+ }
+ cx.notify();
+ }
+
+ /// A clip let go at window `x` over lane `to`: it lands with its head where
+ /// the hand is carrying it ([`Player::drop_frame`]), on the track it was
+ /// dropped on, taking its whole take with it -- one undo step for the
+ /// gesture. Dropped back where it was picked up it is not an edit at all, so
+ /// nothing is said about it. The engine reseeks, so all this owes is the
+ /// flag reset -- and the selection, whose index was that lane's own and now
+ /// names a different clip there.
+ pub(crate) fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, x: Pixels, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let (Some((start, _)), Some(was)) = (
+ self.drop_frame(from, idx, x),
+ self.session
+ .as_ref()
+ .and_then(|session| session.lane_clips(from).get(idx).map(|c| c.start)),
+ ) else {
+ return;
+ };
+ let moved = self
+ .session
+ .as_mut()
+ .is_some_and(|session| session.move_clip_to(from, idx, to, start));
+ let (kind, lanes) = match from.kind {
+ LaneKind::Video => ("picture", "video"),
+ LaneKind::Audio => ("sound", "audio"),
+ };
+ match moved {
+ true => {
+ self.selected = None;
+ self.reset_after_reseek();
+ }
+ // The three ways a drag is refused, told apart by what the
+ // front-end already knows: a lane's kind, and where the clip was.
+ // Everything else that could refuse (a clip that is not there)
+ // cannot be dragged.
+ false if from.kind != to.kind => {
+ self.notify_user(
+ format!(
+ "NOT ON {} — that is a {kind} clip; drop it on a {lanes} lane",
+ to.label()
+ )
+ .into(),
+ )
+ }
+ // Picked up and put back down where it was: a click, and a click
+ // says nothing.
+ false if from == to && start == was => {}
+ false => {
+ self.notify_user(
+ format!(
+ "NOT MOVED — another clip already covers those frames on {}",
+ to.label()
+ )
+ .into(),
+ )
+ }
+ }
+ cx.notify();
+ }
+
+ /// Where a clip let go at window `x` over lane `to` wants its head: the
+ /// frame under the pointer, less however far into the box the hand grabbed
+ /// it (so the clip does not jump under the pointer), pulled onto a
+ /// neighbouring edge when it lands within [`SNAP_PX`] of one. `None` when
+ /// there is no such clip to move. The engine has the last word on where it
+ /// may actually go -- this is the ask, not the answer.
+ ///
+ /// corner-cut: the bed now runs past the last frame whenever the timeline is
+ /// shorter than the view ([`Scale::time_at`] clamps at the head only), so a
+ /// clip *can* be dragged out there. Zoomed in against the far end it cannot:
+ /// the scroll clamp pins the bed's right edge to the duration, and the
+ /// pointer has no pixel past it. The upgrade is to let the scroll clamp
+ /// leave a screen of empty bed after the end, the way every NLE does.
+ pub(crate) fn drop_frame(&self, from: Lane, idx: usize, x: Pixels) -> Option<(u32, Option)> {
+ let clip = self.session.as_ref()?.lane_clips(from).get(idx).copied()?;
+ let marks = self.snap_targets(Some((from, idx)));
+ Some(landing(
+ self.frame_under(x),
+ self.grab,
+ clip.frames(),
+ self.snap,
+ self.snap_frames(),
+ &marks,
+ ))
+ }
+
+ /// The same answer for a library row on its way down: nothing is in the hand
+ /// yet, so there is no grab offset to take off and no length to snap by --
+ /// the file's own is not known until the engine has placed it -- and only
+ /// its head lands. Asked by the line, by the ghost and by the drop itself
+ /// ([`Player::insert_source`]), so all three name one frame.
+ pub(crate) fn place_frame(&self, x: Pixels) -> (u32, Option) {
+ let marks = self.snap_targets(None);
+ landing(
+ self.frame_under(x),
+ 0,
+ 0,
+ self.snap,
+ self.snap_frames(),
+ &marks,
+ )
+ }
+
+ /// Which index the clip in the hand is at *now*: [`live_idx`] against the
+ /// lane the drag named, since a stroke during the gesture moves the indices
+ /// gpui froze into the payload. Both halves of a drag ask it -- the line
+ /// drawn in flight and the drop that commits -- so the promise and the
+ /// landing are made about one clip.
+ pub(crate) fn dragged(&self, drag: &ClipDrag) -> Option {
+ let session = self.session.as_ref()?;
+ live_idx(session.lane_clips(drag.lane), drag.idx, drag.clip)
+ }
+
+ /// The line while the clip is still in the hand: the very answer
+ /// [`Player::drop_frame`] will commit, worked out on every move of the drag,
+ /// so what the eye was promised is where the release puts it. A pointer that
+ /// has wandered off the bed promises nothing.
+ pub(crate) fn preview_drop(&mut self, from: Lane, idx: usize, x: Pixels, cx: &mut Context) {
+ let cue = self.drop_frame(from, idx, x).and_then(|(_, cue)| cue);
+ self.set_cue(cue, x, cx);
+ }
+
+ /// The same line for a library row on its way to a lane: it goes down at
+ /// the frame it is let go on ([`Player::place_frame`]), so that frame is
+ /// what snaps and what is drawn.
+ pub(crate) fn preview_place(&mut self, x: Pixels, cx: &mut Context) {
+ let cue = self.place_frame(x).1;
+ self.set_cue(cue, x, cx);
+ }
+
+ /// The shadow the clip in the hand would fill, on the lane the pointer is
+ /// over: its head where [`Player::drop_frame`] says the release will put it
+ /// -- the same call the drop makes, so the box drawn and the box committed
+ /// are one answer -- and its own length at this zoom. A lane of the other
+ /// kind refuses the drop ([`Project::move_clip`]), and the shadow says so
+ /// before the release does.
+ pub(crate) fn preview_ghost(&mut self, drag: &ClipDrag, to: Lane, x: Pixels, cx: &mut Context) {
+ let ghost = self
+ .dragged(drag)
+ .and_then(|idx| self.drop_frame(drag.lane, idx, x))
+ .map(|(start, _)| Ghost {
+ lane: to,
+ start,
+ frames: drag.clip.frames(),
+ tint: self.clip_tint(drag.clip.source),
+ refused: drag.lane.kind != to.kind,
+ });
+ self.set_ghost(ghost, cx);
+ }
+
+ /// The line the track in the hand would drop into, on the row the pointer
+ /// is over: at that row's top edge when the header is coming up from below
+ /// and at its bottom edge when it is going down, which is the slot
+ /// [`Player::reorder_lane`] commits to at the release. Nothing at all over
+ /// its own row, where a release changes nothing.
+ pub(crate) fn preview_lane_drop(&mut self, from: Lane, onto: Lane, cx: &mut Context) {
+ let lanes = self
+ .session
+ .as_ref()
+ .map_or_else(Vec::new, PlaybackSession::lanes);
+ let at = |lane: Lane| lanes.iter().position(|&l| l == lane);
+ let next = match (at(from), at(onto)) {
+ (Some(i), Some(j)) if i != j => Some(LaneDrop {
+ lane: onto,
+ above: j < i,
+ }),
+ _ => None,
+ };
+ // Only when it has actually changed: a drag move fires on every painted
+ // frame, and a redraw per frame that draws the same line is a redraw
+ // for nothing.
+ if self.lane_drop != next {
+ self.lane_drop = next;
+ cx.notify();
+ }
+ }
+
+ /// The line taken back down again, by the row that drew it and by no other:
+ /// the pointer has been carried off `lane`, so the slot it was promising is
+ /// no longer the one a release would commit to.
+ pub(crate) fn forget_lane_drop(&mut self, lane: Lane, cx: &mut Context) {
+ if self.lane_drop.is_some_and(|d| d.lane == lane) {
+ self.lane_drop = None;
+ cx.notify();
+ }
+ }
+
+ /// The same shadow for a library row: its head at [`Player::place_frame`],
+ /// which is where the drop inserts it, and the file's own length for its
+ /// width -- the length the library row already reports. A file this lane
+ /// cannot hold ([`lane_refuses`]) is tinted as refused, which is the answer
+ /// the release would give in words.
+ pub(crate) fn preview_ghost_asset(&mut self, path: &Path, to: Lane, x: Pixels, cx: &mut Context) {
+ let ghost = Ghost {
+ lane: to,
+ start: self.place_frame(x).0,
+ frames: self
+ .session
+ .as_ref()
+ .map_or(0, |session| session.file_frames(path)),
+ // A path with no source entry has no colour of its own, and the
+ // shadow wears the lane's own instead of borrowing another file's.
+ tint: file_tint(self.sources(), path).unwrap_or(BG_RAISED()),
+ refused: lane_refuses(path, to).is_some(),
+ };
+ self.set_ghost(Some(ghost), cx);
+ }
+
+ /// Sets the shadow, or takes it away, repainting only when it moved -- the
+ /// listeners below run it on every pointer sample of a drag. Cleared by the
+ /// root and set again by the lane under the pointer, in that order (gpui
+ /// runs the capture phase parent-first), so a pointer over no lane at all
+ /// leaves nothing drawn.
+ pub(crate) fn set_ghost(&mut self, ghost: Option, cx: &mut Context) {
+ if ghost != self.ghost {
+ self.ghost = ghost;
+ cx.notify();
+ }
+ }
+
+ /// The swatch a clip from source `n` wears: [`source_tint`] over the first
+ /// source entry naming that *file*, since two audio streams of one file are
+ /// two sources and one colour. Every box on a lane and every ghost a drag
+ /// draws asks this, so the shadow is recognisably the thing in the hand.
+ pub(crate) fn clip_tint(&self, source: usize) -> u32 {
+ self.sources()
+ .get(source)
+ .and_then(|entry| file_tint(self.sources(), &entry.path))
+ .unwrap_or_else(|| source_tint(source))
+ }
+
+ pub(crate) fn sources(&self) -> &[Source] {
+ self.session
+ .as_ref()
+ .map_or(&[][..], PlaybackSession::sources)
+ }
+
+ /// Sets the line, or takes it away, and repaints only when it moved: a
+ /// pointer dragged off the bed (up to the library, say) is not promising a
+ /// landing any more.
+ pub(crate) fn set_cue(&mut self, cue: Option, x: Pixels, cx: &mut Context) {
+ let bed = self.ruler.get();
+ let cue = cue.filter(|_| x >= bed.left() && x <= bed.right());
+ if cue != self.snap_cue {
+ self.snap_cue = cue;
+ cx.notify();
+ }
+ }
+
+ /// Every edge this timeline offers a gesture: [`snap_marks`] over all of its
+ /// lanes, so a clip meets a take one track over as readily as one beside it.
+ pub(crate) fn snap_targets(&self, skip: Option<(Lane, usize)>) -> Vec {
+ let Some(session) = &self.session else {
+ return Vec::new();
+ };
+ let lanes = session.lanes();
+ let clips: Vec<&[Clip]> = lanes.iter().map(|&lane| session.lane_clips(lane)).collect();
+ let skip = skip.and_then(|(lane, idx)| Some((lanes.iter().position(|&l| l == lane)?, idx)));
+ snap_marks(&clips, skip, frame_at(session.now(), self.fps))
+ }
+
+ /// Where a gesture at `raw` lands and the mark that pulled it there, with
+ /// the switch honoured: snapping off, nothing moves and no line is drawn.
+ pub(crate) fn snap_to(&self, raw: u32, len: u32, marks: &[u32]) -> (u32, Option) {
+ snap_cue(self.snap, raw, len, self.snap_frames(), marks)
+ }
+
+ /// Every timeline frame that is a *source* sync point: each clip's own
+ /// grid ([`Player::syncs`]), moved onto the frames the clip plays it at.
+ /// Ascending, because the clips are and each grid is.
+ ///
+ /// This is the difference between an export that copies its picture and one
+ /// that decodes and re-codes every frame of a feature film. A cut anywhere
+ /// else leaves the copy path with a region that begins between two sync
+ /// points -- pictures whose references are not in the file -- and the whole
+ /// export falls back to the encoder ([`engine::export`] states the rule).
+ ///
+ /// Only clips at their own speed, and only video lanes: a re-timed clip is
+ /// resampled pictures, which is not a copy at any cut, and a sound lane has
+ /// no groups of pictures to begin with.
+ pub(crate) fn sync_frames(&self) -> Vec {
+ let Some(session) = &self.session else {
+ return Vec::new();
+ };
+ let sources = session.sources();
+ let mut marks = Vec::new();
+ for lane in session.lanes() {
+ if lane.kind != LaneKind::Video {
+ continue;
+ }
+ for clip in session.lane_clips(lane) {
+ let Some(keys) = sources
+ .get(clip.source)
+ .and_then(|entry| self.syncs.get(&entry.path))
+ .filter(|_| clip.speed.is_normal())
+ else {
+ continue;
+ };
+ marks.extend(
+ keys.iter()
+ .filter(|&&key| key >= clip.in_frame && key < clip.out_frame)
+ .map(|&key| clip.start + (key - clip.in_frame)),
+ );
+ }
+ }
+ marks.sort_unstable();
+ marks
+ }
+
+ /// The frame a cut asked for at `raw` really lands on: the nearest source
+ /// sync point within the snap's own tolerance, or `raw` itself where the
+ /// magnet is off, where nothing is near enough, or where the source has no
+ /// grid to offer (the walk has not answered yet, or the file is not one
+ /// this project can copy at all).
+ ///
+ /// The same tolerance the clip-edge snap uses, so one switch and one
+ /// distance govern every landing on this timeline.
+ pub(crate) fn cut_frame(&self, raw: u32) -> u32 {
+ if !self.snap {
+ return raw;
+ }
+ let tol = self.snap_frames();
+ self.sync_frames()
+ .into_iter()
+ .filter(|mark| mark.abs_diff(raw) <= tol)
+ .min_by_key(|mark| mark.abs_diff(raw))
+ .unwrap_or(raw)
+ }
+
+ /// Whether the playhead is standing exactly on one: what the timeline's own
+ /// line says out loud, so "a cut here is copied" is on screen before the cut
+ /// rather than discovered in the export card afterwards.
+ ///
+ /// Asked every repaint, so it walks the *playhead* into each clip's source
+ /// and looks it up in that source's own sorted grid -- where
+ /// [`Player::sync_frames`] builds the whole list, which is a film's worth of
+ /// marks to allocate and sort sixty times a second.
+ pub(crate) fn on_sync_point(&self) -> bool {
+ let Some(session) = &self.session else {
+ return false;
+ };
+ let now = frame_at(session.now(), self.fps);
+ let sources = session.sources();
+ session.lanes().into_iter().any(|lane| {
+ lane.kind == LaneKind::Video
+ && session.lane_clips(lane).iter().any(|clip| {
+ clip.speed.is_normal()
+ && (clip.start..clip.start + (clip.out_frame - clip.in_frame))
+ .contains(&now)
+ && sources
+ .get(clip.source)
+ .and_then(|entry| self.syncs.get(&entry.path))
+ .is_some_and(|keys| {
+ keys.binary_search(&(clip.in_frame + (now - clip.start))).is_ok()
+ })
+ })
+ })
+ }
+
+ /// Puts the playhead on the sync point before or after it -- the keyboard's
+ /// half of placing a cut where the export can copy it, and the only way to
+ /// reach one exactly on a timeline zoomed out to a whole film, where one
+ /// pixel is seconds.
+ pub(crate) fn jump_sync(&mut self, forward: bool, cx: &mut Context) {
+ let Some(session) = &self.session else {
+ return;
+ };
+ let now = frame_at(session.now(), self.fps);
+ let marks = self.sync_frames();
+ let mark = match forward {
+ true => marks.iter().find(|&&mark| mark > now).copied(),
+ false => marks.iter().rev().find(|&&mark| mark < now).copied(),
+ };
+ match mark {
+ Some(mark) => self.seek(f64::from(mark) / self.fps, cx),
+ // Said rather than swallowed: the two most likely reasons are a walk
+ // that has not answered yet and a source with no grid at all, and a
+ // key that does nothing looks broken either way.
+ None => self.notify_user(match marks.is_empty() {
+ true => "NO SYNC POINTS — this source has no keyframe grid to jump by (or it is \
+ still being read)"
+ .into(),
+ false => "NO SYNC POINT THAT WAY — the playhead is past the last one".into(),
+ }),
+ }
+ }
+
+ /// [`SNAP_PX`] in timeline frames at the scale the bed is drawn at: the bed's
+ /// own width drops out of it, since a pixel is now worth the same stretch of
+ /// timeline wherever the view sits.
+ pub(crate) fn snap_frames(&self) -> u32 {
+ self.scale.snap_frames(self.fps)
+ }
+
+ /// Opens the clip menu on the box under the pointer, from the right button
+ /// wherever it was pressed on that box -- its middle or one of its edge
+ /// strips, which cover the middle's own listener. Selecting first is part of
+ /// it: every item acts on the clip the menu names.
+ pub(crate) fn open_menu(&mut self, lane: Lane, idx: usize, at: Point, cx: &mut Context) {
+ if self.modal() {
+ return;
+ }
+ self.select((lane, idx), cx);
+ self.context_menu = Some(ContextMenu {
+ lane,
+ idx,
+ at,
+ details: false,
+ });
+ cx.notify();
+ }
+
+ /// A press on a clip's edge: the start of the drag that changes how much of
+ /// its source it plays. It selects the clip as a press anywhere else on the
+ /// box does -- the edge strip covers the box's own listener (`occlude`), so
+ /// this is the only one that fires there.
+ pub(crate) fn start_trim(&mut self, lane: Lane, idx: usize, edge: Edge, cx: &mut Context) {
+ if self.modal() || self.exporting().is_some() {
+ return;
+ }
+ let Some(clip) = self
+ .session
+ .as_ref()
+ .and_then(|session| session.lane_clips(lane).get(idx).copied())
+ else {
+ return;
+ };
+ self.select((lane, idx), cx);
+ self.trim = Some(Trim {
+ lane,
+ idx,
+ edge,
+ // Where the edge already is: a press that never moves is not an
+ // edit, and `Project::trim` refuses exactly that.
+ to: match edge {
+ Edge::Start => clip.start,
+ Edge::End => clip.end(),
+ },
+ link: clip.link,
+ });
+ cx.notify();
+ }
+
+ /// Where the pointer has pulled the edge to, clamped to the room the engine
+ /// says that edge has. Along the same bed the ruler is measured on and
+ /// against the same duration the boxes are drawn to, so the edge tracks the
+ /// pointer exactly.
+ pub(crate) fn trim_to(&mut self, x: Pixels, cx: &mut Context) {
+ let Some(trim) = self.trim else {
+ return;
+ };
+ // The edge is pulled onto the same marks a whole clip is, by itself:
+ // there is no other end travelling with it, so it snaps at length zero.
+ let marks = self.snap_targets(Some((trim.lane, trim.idx)));
+ let (at, cue) = self.snap_to(self.frame_under(x), 0, &marks);
+ let Some((lo, hi)) = self
+ .session
+ .as_ref()
+ .and_then(|session| session.trim_room(trim.lane, trim.idx, trim.edge))
+ else {
+ return;
+ };
+ let to = at.clamp(lo, hi);
+ // The line only stands where the edge actually stopped: a mark the
+ // engine's own room clamped away was never reached.
+ self.set_cue(cue.filter(|_| to == at), x, cx);
+ self.trim = Some(Trim { to, ..trim });
+ cx.notify();
+ }
+
+ /// The timeline frame a pointer at window x is on: along the same bed the
+ /// ruler is measured on, through the same [`Scale`] every box is drawn
+ /// through, so a zoomed-in panel answers with the frame under the pointer
+ /// and not with the one that would have been there unzoomed. The one
+ /// question a trim, a grab and a drop all ask.
+ pub(crate) fn frame_under(&self, x: Pixels) -> u32 {
+ frame_at(
+ self.scale.time_at(px_along(x, self.ruler.get())),
+ self.fps,
+ )
+ }
+
+ /// The release: the whole drag reaches the engine as one edit, so it is one
+ /// undo step. The selection survives it -- a trim inserts and removes
+ /// nothing, so every index a lane had still names the clip it named.
+ pub(crate) fn commit_trim(&mut self, cx: &mut Context) {
+ let Some(trim) = self.trim.take() else {
+ return;
+ };
+ let trimmed = self
+ .session
+ .as_mut()
+ .is_some_and(|session| session.trim_clip(trim.lane, trim.idx, trim.edge, trim.to));
+ if trimmed {
+ self.reset_after_reseek();
+ }
+ cx.notify();
+ }
+
+ /// The clip as the drag is showing it: an edge under the pointer moves its
+ /// own box, and the boxes of the halves linked to it, before anything is
+ /// committed. Display only -- the project is not touched until the release.
+ pub(crate) fn trimmed(&self, lane: Lane, idx: usize, clip: Clip) -> Clip {
+ let Some(trim) = self.trim.filter(|t| {
+ (t.lane, t.idx) == (lane, idx) || (t.link.is_some() && t.link == clip.link)
+ }) else {
+ return clip;
+ };
+ let still = self.session.as_ref().is_some_and(|session| {
+ session
+ .sources()
+ .get(clip.source)
+ .is_some_and(|s| engine::is_image(&s.path))
+ });
+ trimmed_clip(clip, trim.edge, trim.to, still)
+ }
+
+ /// How long the timeline is *drawn* as: its own length, and while a tail is
+ /// being dragged the furthest that tail may reach. A bed that ends exactly
+ /// at the last frame has nowhere to put a pointer that means "longer", so
+ /// without this the last clip on the timeline could be pulled in and never
+ /// let back out.
+ ///
+ /// Scroll room only, now that a second is an absolute number of pixels
+ /// ([`Scale`]): the extra length loosens [`View::settled`]'s clamp, which is
+ /// where the pixels past the last frame come from, and moves no box by a
+ /// pixel. It is still the *only* headroom at the tail -- zoomed in against
+ /// the end, that clamp pins the bed's right edge to the duration and an
+ /// End-trim of the last clip would have nowhere to be dragged to. What it
+ /// must not do is be read as a length anyone is told: the timecode reads
+ /// `PlaybackSession::timeline_duration` for exactly that reason.
+ pub(crate) fn drawn_duration(&self) -> f64 {
+ let Some(session) = &self.session else {
+ return 0.;
+ };
+ let duration = session.timeline_duration();
+ match self.trim {
+ Some(trim) if trim.edge == Edge::End => {
+ let (_, hi) = session
+ .trim_room(trim.lane, trim.idx, trim.edge)
+ .unwrap_or((0, 0));
+ duration.max(f64::from(hi) / self.fps)
+ }
+ _ => duration,
+ }
+ }
+
+ /// Where the playhead is, as the panel draws it: pinned to the out point
+ /// once playback is done, and clamped to the drawn duration otherwise -- a
+ /// tail being dragged draws past the timeline it is about to become.
+ pub(crate) fn playhead(&self, duration: f64) -> f64 {
+ if self.transport() == Transport::Ended {
+ duration
+ } else {
+ self.session
+ .as_ref()
+ .map_or(0., PlaybackSession::now)
+ .clamp(0., duration)
+ }
+ }
+
+ /// One sample of whatever drag is in the hand: the equalizer's handle, a
+ /// clip's edge, a colour bar, the speed bar, the volume slider or the
+ /// playhead. Each of those starts on a strip a few pixels wide that the
+ /// pointer leaves immediately, so none of them can be tracked from the
+ /// element it started on -- the gesture is followed here instead, on a
+ /// hitbox that covers everything the hand can reach.
+ ///
+ /// Registered on the root *and* on the scrim of every card that holds a
+ /// slider ([`Player::drag_scrim`]). An occluding sheet ends gpui's hit test
+ /// where it sits (`Hitbox::is_hovered`, window.rs:788), so while a card is
+ /// up the root is not hovered anywhere under it and hears none of this: the
+ /// press set a value and the drag then froze on it.
+ pub(crate) fn drag_move(&mut self, event: &MouseMoveEvent, _: &mut Window, cx: &mut Context) {
+ // A handle is 10 px across and the pointer leaves it at once, so
+ // the equalizer drag is tracked here for the ruler's reason.
+ if self.eq_dragging {
+ if event.pressed_button == Some(MouseButton::Left) {
+ self.drag_band(event.position, cx);
+ } else {
+ // Released outside the window: the up below never came,
+ // so this is where the gesture ends -- and it still owes
+ // the one write the whole drag is worth.
+ self.eq_dragging = false;
+ self.commit_eq(cx);
+ }
+ return;
+ }
+ // A clip edge is 6 px wide and the pointer leaves it on the
+ // first drag, so the gesture is tracked here for the same
+ // reason -- and it ends here too when the button came up
+ // outside the window, still owing its one edit.
+ if self.trim.is_some() {
+ match event.pressed_button {
+ Some(MouseButton::Left) => self.trim_to(event.position.x, cx),
+ _ => self.commit_trim(cx),
+ }
+ return;
+ }
+ // A colour slider is 4 px tall and the pointer leaves it just as
+ // fast; every sample is live, so the release owes no write of
+ // its own -- what the last sample set is what the clip carries.
+ if self.color_dragging {
+ if event.pressed_button == Some(MouseButton::Left) {
+ self.drag_color(event.position.x, false, cx);
+ } else {
+ // The release happened outside the window, so this is
+ // where the gesture ends -- and it may not end on a
+ // sample the worker was too busy to take.
+ self.color_dragging = false;
+ self.flush_drag(cx);
+ }
+ return;
+ }
+ // The speed bar, the same 4 px and the same live writes: the
+ // press took the undo step and every sample since is live.
+ if self.speed_dragging {
+ if event.pressed_button == Some(MouseButton::Left) {
+ self.drag_speed(event.position.x, false, cx);
+ } else {
+ self.speed_dragging = false;
+ self.flush_drag(cx);
+ }
+ return;
+ }
+ // The volume slider, the same live writes: what the hand is on
+ // is what the speakers are doing, and there is nothing to undo.
+ if self.volume_dragging {
+ if event.pressed_button == Some(MouseButton::Left) {
+ self.drag_volume(event.position.x, cx);
+ } else {
+ self.volume_dragging = false;
+ }
+ return;
+ }
+ if !self.scrubbing {
+ return;
+ }
+ if event.pressed_button == Some(MouseButton::Left) {
+ self.scrub_to(event.position.x, false, cx);
+ } else {
+ // A release outside the window never reaches the handler
+ // below, so the first button-up move is when we learn the
+ // drag is over. Without this the next hover would scrub.
+ self.scrubbing = false;
+ }
+ }
+
+ /// Where a drag ends: the release lands exactly, and whatever the gesture
+ /// owes -- one undo step for the equalizer and the trim, a flush for the
+ /// live-writing bars -- is paid here. On the root and on a card's scrim
+ /// both, for [`Player::drag_move`]'s reason: a release over an open card
+ /// never reaches the root.
+ pub(crate) fn drag_release(&mut self, event: &MouseUpEvent, _: &mut Window, cx: &mut Context) {
+ if std::mem::take(&mut self.eq_dragging) {
+ // The release lands exactly, then the gesture is written
+ // once -- the append-only table's whole reason.
+ self.drag_band(event.position, cx);
+ self.commit_eq(cx);
+ return;
+ }
+ if self.trim.is_some() {
+ // The release lands exactly, then the gesture is
+ // written once -- one edit, one undo step.
+ self.trim_to(event.position.x, cx);
+ self.commit_trim(cx);
+ return;
+ }
+ if std::mem::take(&mut self.color_dragging) {
+ // The release lands exactly where the hand let go, and
+ // it is a live write like every other sample: the undo
+ // step the gesture rolls back to was the press's. The
+ // flush is what makes "exactly" true while the worker is
+ // still busy -- the sample above would only be held.
+ self.drag_color(event.position.x, false, cx);
+ self.flush_drag(cx);
+ return;
+ }
+ if std::mem::take(&mut self.speed_dragging) {
+ self.drag_speed(event.position.x, false, cx);
+ self.flush_drag(cx);
+ return;
+ }
+ if std::mem::take(&mut self.volume_dragging) {
+ self.drag_volume(event.position.x, cx);
+ return;
+ }
+ if std::mem::take(&mut self.scrubbing) {
+ self.scrub_to(event.position.x, true, cx);
+ }
+ }
+}
diff --git a/crates/app/src/player/transport.rs b/crates/app/src/player/transport.rs
new file mode 100644
index 0000000..acbca67
--- /dev/null
+++ b/crates/app/src/player/transport.rs
@@ -0,0 +1,308 @@
+//! Where the playhead is and where it is going: the pump, the seeks, the
+//! volume and the one button that starts it.
+
+use crate::*;
+
+impl Player {
+ /// Catches the display up to the clock: everything already due is taken off
+ /// the channel and only the last of them is shown, which *is* the
+ /// drop-when-behind policy. A frame that is not due yet waits in `held`, and
+ /// while the clock is paused *nothing* is due -- a repaint re-presents the
+ /// frame already on screen, whatever asked for the repaint.
+ pub(crate) fn pump(&mut self, window: &mut Window) {
+ // Where the transport was before this drain, so the crossing into
+ // `Ended` can be recognised as the one transition it is.
+ let was = self.transport();
+ // No timeline, nothing to catch up to: the window is showing its empty
+ // state and there is no decoder to drain.
+ let Some(session) = &mut self.session else {
+ return;
+ };
+ let target = session.now() * self.fps;
+ let mut newest: Option = None;
+ // A frame the screen is owed: a seek's landing, and the one readiness
+ // signal there is ([`Player::reset_after_reseek`]).
+ let owed = self.seek_since.is_some();
+ // Paused, the clock is frozen and *nothing new is due*. Whatever the
+ // decoder is still handing over is the backlog it was behind by when
+ // the pause landed -- frames at a position the transport has already
+ // left -- and taking one per repaint is what walked the picture on
+ // after the sound had stopped, at exactly the rate the pointer was
+ // moved over the timeline. Gated here, at the one place a frame ever
+ // reaches the screen, rather than in the handlers that repaint: a
+ // hover, a notice, a resize and a vsync are all the same event to this.
+ // An owed frame is still taken, playing or not -- a scrub is paused by
+ // definition, and its landing is the whole point of it.
+ while session.is_playing() || owed {
+ let frame = match self.held.take() {
+ Some(frame) => frame,
+ // Nothing waiting means either a clip boundary being rebuilt or
+ // the real end of the timeline, and only the engine can tell
+ // them apart -- `frame.index` is already a timeline index.
+ None => match session.try_frame() {
+ Some(frame) => frame,
+ None => break,
+ },
+ };
+ if f64::from(frame.index) <= target {
+ self.dropped += u32::from(newest.is_some());
+ newest = Some(frame);
+ } else {
+ self.held = Some(frame);
+ break;
+ }
+ }
+
+ // How far behind the master clock the picture just handed over is, in
+ // seconds. Measured off a frame that really arrived and nothing else: a
+ // clip boundary being reopened delivers nothing at all for hundreds of
+ // milliseconds, and restarting *that* would only cancel the open it is
+ // waiting on.
+ let late = newest
+ .as_ref()
+ .map_or(0., |f| (target - f64::from(f.index)) / self.fps);
+
+ if let Some(frame) = newest {
+ self.displayed += 1;
+ self.seek_since = None;
+ self.started.get_or_insert_with(|| {
+ eprintln!("first frame displayed (index {})", frame.index);
+ Instant::now()
+ });
+ let buf = image::RgbaImage::from_raw(frame.width, frame.height, frame.bgra)
+ .expect("frame buffer sized width*height*4");
+ // Counted here rather than under `color_open`, because the card
+ // opens on a frame that was pumped before it: gating this on the
+ // card would leave its graph flat until something reseeked. A
+ // thousandth of the pixels, against a conversion that just touched
+ // all of them.
+ self.histogram = histogram(buf.as_raw());
+ let next = Arc::new(RenderImage::new(vec![image::Frame::new(buf)]));
+ if let Some(old) = self.image.replace(next) {
+ // Every RenderImage gets a fresh id and its own atlas tile:
+ // without this the sprite atlas grows for the whole video.
+ let _ = window.drop_image(old);
+ }
+ }
+
+ // Audio is the master clock and a decoder that cannot keep up with it
+ // never gets back on its own: it hands over every frame in order,
+ // whether or not its moment has passed, so what it is behind by only
+ // grows -- a minute in, the picture is seconds behind what is being
+ // heard, and that is the whole of "the video can't catch the audio".
+ // Past `LATE_RESYNC` the backlog is abandoned and the picture restarted
+ // at the clock, which touches neither the sound nor the clock
+ // (`PlaybackSession::resync_picture`), so nothing the ear is following
+ // moves. Never on a frame a seek owed: that one is late by however long
+ // its own reopen took, and answering it with another reopen is a loop.
+ //
+ // corner-cut: on a machine that cannot decode the file in real time at
+ // all this settles into one restart per `RESYNC_GAP` -- in sync, and
+ // stuttering, which is the honest picture of what that machine can do.
+ // The upgrade path is dropping late frames *inside* the worker (skip
+ // the convert and the send for anything already past due), which needs
+ // the deadline shared with it.
+ if !owed && session.is_playing() && should_resync(late, self.resynced) {
+ eprintln!("picture {late:.3}s behind the clock: restarting it there");
+ session.resync_picture();
+ self.held = None;
+ self.resynced = Some(Instant::now());
+ }
+
+ if self.transport() == Transport::Ended {
+ // A seek whose worker never produced a frame (vanished file) would
+ // otherwise repaint at vsync forever. Held clear for as long as the
+ // state does, not just on the crossing: nothing else is coming.
+ self.seek_since = None;
+ if was != Transport::Ended {
+ // Ended is a *stopped* transport, so the clock stops with it,
+ // on the out point the timecode and the playhead have been
+ // showing all along. Nothing else ever stopped it: past the
+ // last frame wall time takes over and `now()` walks off the end
+ // of the timeline for as long as the window is left open -- and
+ // the playhead is what a cut, a paste, an insert and the
+ // analyser all act at, so every one of them was aiming into
+ // empty space (measured: a 5 s timeline recognised its end at
+ // clock 17.5 s under a slow renderer). End of stream is left
+ // set, so this is still `Ended` and the next press restarts.
+ if let Some(session) = &mut self.session {
+ session.halt_at_end();
+ }
+ let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64());
+ eprintln!(
+ "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s",
+ self.displayed,
+ self.dropped,
+ self.session.as_ref().map_or(0., PlaybackSession::now)
+ );
+ }
+ }
+ }
+
+ /// Where the transport is, asked of the session rather than remembered:
+ /// end of stream is the engine's own flag (any seek clears it, which is why
+ /// an edit past the end revives the picture) and so is the clock. A held
+ /// frame is one still owed to the screen, so the end is not the end yet.
+ pub(crate) fn transport(&self) -> Transport {
+ let Some(session) = &self.session else {
+ return Transport::Stopped;
+ };
+ transport(
+ session.is_playing(),
+ session.is_eos() && self.held.is_none(),
+ )
+ }
+
+ /// A frame owed to the screen after a reseek, and the buffered one dropped:
+ /// what stops the picture from staying frozen on the old last frame. The
+ /// end-of-stream flag itself is the engine's and its own seek clears it --
+ /// edits reseek inside the engine and still owe this.
+ pub(crate) fn reset_after_reseek(&mut self) {
+ self.held = None;
+ // Restarted on every reseek, not only on the first: what it measures is
+ // the open now standing, which is what a person is waiting on.
+ self.seek_since = Some(Instant::now());
+ // A seek is a person saying where to look, so it takes the view back
+ // from an earlier scroll: the frame asked for is the one to be shown.
+ self.panned = false;
+ // An edit moves the indices a drag in flight is holding -- a stroke
+ // during one is exactly that -- and an edge committed against a moved
+ // index would trim a clip nobody grabbed. Dropping it is the whole fix:
+ // nothing has been written yet.
+ self.trim = None;
+ // ...and the shadow a drag is drawn under promises a landing on a lane
+ // this edit has just reshaped. The next move of the drag draws it
+ // again; until then it says nothing.
+ self.ghost = None;
+ }
+
+ /// Jumps the timeline.
+ pub(crate) fn seek(&mut self, t: f64, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let Some(session) = &mut self.session else {
+ return;
+ };
+ session.seek(t);
+ self.reset_after_reseek();
+ cx.notify();
+ }
+
+ /// The keyboard's seek: whole frames along the timeline, through the same
+ /// door a ruler click uses -- so a step while playing keeps playing, exactly
+ /// as a click does. It starts from the frame the transport is showing, which
+ /// past the end is the last one, and that is what lets a step back off EOS
+ /// revive the picture (the engine's seek leaves [`Transport::Ended`]). Both
+ /// ends clamp, so the two go-to actions are this same step asked for more
+ /// frames than the timeline has. Selection is untouched: a seek is not an
+ /// edit, and nothing it does moves a clip index.
+ pub(crate) fn step(&mut self, frames: i64, cx: &mut Context) {
+ let ended = self.transport() == Transport::Ended;
+ let Some(session) = &self.session else {
+ return;
+ };
+ let last = ((session.timeline_duration() * self.fps).round() as i64 - 1).max(0);
+ let now = match ended {
+ true => last,
+ false => i64::from(frame_at(session.now(), self.fps)),
+ };
+ let target = now.saturating_add(frames).clamp(0, last);
+ self.seek(target as f64 / self.fps, cx);
+ }
+
+ /// Seeks to where the pointer sits along the ruler. `commit` is the press
+ /// and the release, which must land exactly even when the throttle below
+ /// would have skipped them.
+ pub(crate) fn scrub_to(&mut self, x: Pixels, commit: bool, cx: &mut Context) {
+ let Some(session) = &self.session else {
+ return;
+ };
+ // Clamped to the timeline here rather than in the mapping: there is bed
+ // past the last frame now, and a seek out there is a seek to the end.
+ let t = self
+ .scale
+ .time_at(px_along(x, self.ruler.get()))
+ .clamp(0., session.timeline_duration());
+ let target = (t * self.fps) as u32;
+ if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) {
+ self.last_target = target;
+ self.last_scrub = Instant::now();
+ self.seek(t, cx);
+ }
+ }
+
+ /// The play binding and the transport button share it: once the timeline is finished
+ /// the only sensible "play" is from the top.
+ /// Pushes the current volume at the session, which is the only place it is
+ /// ever pushed: after a change here, and after a session arrives. A session
+ /// starts at full volume, so a file opened while muted has to be told --
+ /// that is the whole reason this is not just called from the key handler.
+ /// Silent no-op with no timeline, or with a run that has no audio device.
+ pub(crate) fn apply_volume(&self) {
+ if let Some(session) = &self.session {
+ session.set_gain(self.volume.gain());
+ }
+ }
+
+ /// The mute key and the two volume keys, and the click on the button. The
+ /// picture is not touched: silencing the output is not pausing it, so the
+ /// clock -- which the device still drives -- runs straight through.
+ pub(crate) fn set_volume(&mut self, change: impl FnOnce(&mut Volume), cx: &mut Context) {
+ change(&mut self.volume);
+ self.apply_volume();
+ cx.notify();
+ }
+
+ /// Where the pointer sits along the slider, as a level. The press and every
+ /// sample after it come here, so the sound follows the hand rather than the
+ /// release -- there is nothing to undo about a monitoring level, which is
+ /// why this writes live and keeps no gesture state beyond the flag.
+ pub(crate) fn drag_volume(&mut self, x: Pixels, cx: &mut Context) {
+ let along = frac_along(x, self.volume_bar.get());
+ self.set_volume(|volume| volume.set_along(along), cx);
+ }
+
+ pub(crate) fn toggle_or_restart(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ // Nothing to play is a message, not a transport state. An empty
+ // timeline is [`Transport::Ended`] from its one black frame onward, so
+ // the restart below would start a clock against a zero-length timeline
+ // -- and it is Ended again by the next repaint, so no later press could
+ // ever stop it: the button would read "Pause" and never pause. A delete
+ // can empty the timeline mid-play, and that press must still stop it.
+ if nothing_to_play(self.session.as_ref()) {
+ match self.session.as_mut().filter(|s| s.is_playing()) {
+ Some(session) => session.pause(),
+ None => self.notify_user(NOTHING_TO_PLAY.into()),
+ }
+ cx.notify();
+ return;
+ }
+ // Pressing play is asking to watch, so a view scrolled away while
+ // paused comes back to the head with it -- as a seek's does.
+ self.panned = false;
+ match self.transport() {
+ // Nothing open: the button is dimmed and the key says nothing.
+ Transport::Stopped => {}
+ // Back to the top and away, for the key and the button alike --
+ // whichever asked, the transport was showing Play.
+ state if state.restarts() => {
+ self.seek(0., cx);
+ if let Some(session) = &mut self.session {
+ session.play();
+ }
+ }
+ _ => {
+ if let Some(session) = &mut self.session {
+ session.toggle();
+ // A paused timeline animates nothing; this is the repaint
+ // that puts the new glyph up.
+ cx.notify();
+ }
+ }
+ }
+ }
+}
diff --git a/crates/app/src/player/view.rs b/crates/app/src/player/view.rs
new file mode 100644
index 0000000..086e259
--- /dev/null
+++ b/crates/app/src/player/view.rs
@@ -0,0 +1,204 @@
+//! What of the timeline is on the bed and what of it is picked: the zoom,
+//! the scroll and the selection.
+
+use crate::*;
+
+impl Player {
+ /// The one place a clip becomes *the* selected one: a click, a right-click
+ /// that opens the menu, and every selection key go through here, so what a
+ /// keyboard marks and what a pointer marks are the same state marked the
+ /// same way (group and all -- see [`marked`]).
+ pub(crate) fn select(&mut self, target: (Lane, usize), cx: &mut Context) {
+ self.selected = Some(target);
+ cx.notify();
+ }
+
+ /// Every clip the playhead is over, one per lane, in the order the lanes are
+ /// drawn -- video first, which is the order [`PlaybackSession::lanes`] comes
+ /// in. What the select key walks.
+ pub(crate) fn under_playhead(&self) -> Vec<(Lane, usize)> {
+ let Some(session) = &self.session else {
+ return Vec::new();
+ };
+ let now = session.now();
+ session
+ .lanes()
+ .into_iter()
+ .filter_map(|lane| Some((lane, session.lane_clip_at(lane, now)?)))
+ .collect()
+ }
+
+ /// Selects the clip under the playhead, and on a repeat press the next
+ /// lane's -- so one key reaches every clip the playhead is over, which is
+ /// what makes selection (and everything that acts on a selection: delete,
+ /// lift, the equalizer, the grade) reachable with no pointer at all.
+ pub(crate) fn select_under_playhead(&mut self, cx: &mut Context) {
+ let under = self.under_playhead();
+ let Some(&first) = under.first() else {
+ self.notify_user("NOTHING UNDER THE PLAYHEAD — move it onto a clip first".into());
+ cx.notify();
+ return;
+ };
+ // Where the current selection sits in that walk decides what "again"
+ // means; a selection off the playhead starts the walk over.
+ let next = self
+ .selected
+ .and_then(|sel| under.iter().position(|&clip| clip == sel))
+ .map_or(first, |at| under[(at + 1) % under.len()]);
+ self.select(next, cx);
+ }
+
+ /// Walks the selection along its own lane, wrapping at either end. Nothing
+ /// selected means nothing to walk from, so it selects under the playhead
+ /// exactly as the select key does: either key can start as well as continue.
+ pub(crate) fn select_step(&mut self, forward: bool, cx: &mut Context) {
+ let clips = self
+ .selected
+ .zip(self.session.as_ref())
+ .map_or(0, |((lane, _), session)| session.lane_clips(lane).len());
+ match (self.selected, clips) {
+ // An empty lane is a selection nothing can be stepped from -- as is
+ // no selection at all, and the playhead answers both.
+ (Some((lane, idx)), len) if len > 0 => {
+ let next = if forward {
+ (idx + 1) % len
+ } else {
+ (idx + len - 1) % len
+ };
+ self.select((lane, next), cx);
+ }
+ _ => self.select_under_playhead(cx),
+ }
+ }
+
+ /// Cycles the fit policy of the clip the picture is coming from -- the
+ /// clicked one when it is a video clip, else the composite's own, exactly as
+ /// the colour card picks its target. A whole card for one four-valued
+ /// setting would be a card to close; a stroke that cycles it and says what
+ /// it landed on is the same setting with nothing to dismiss.
+ ///
+ /// Only means anything when the clip is not the project's size -- a clip
+ /// that already fills the canvas looks the same under all four -- so the
+ /// notice says the size it is placing, not just the word.
+ pub(crate) fn cycle_fit(&mut self, cx: &mut Context) {
+ if self.exporting().is_some() {
+ return;
+ }
+ let Some(session) = &mut self.session else {
+ self.notify_user("no timeline to fit — open a file first".into());
+ cx.notify();
+ return;
+ };
+ let target = self
+ .selected
+ .filter(|(lane, _)| lane.kind == LaneKind::Video)
+ .or_else(|| session.video_clip_at(session.now()));
+ let Some((lane, idx)) = target else {
+ self.notify_user("no clip under the playhead to fit".into());
+ cx.notify();
+ return;
+ };
+ let next = next_fit(session.fit_of(lane, idx));
+ self.apply_fit(lane, idx, next, cx);
+ }
+
+ /// One clip's fit policy set, whichever asked: the stroke that steps to the
+ /// next one and the list that names one outright come through here, so they
+ /// cannot differ in what they do or in what they say they did.
+ pub(crate) fn apply_fit(&mut self, lane: Lane, idx: usize, fit: FitPolicy, cx: &mut Context) {
+ if let Some(session) = &mut self.session
+ && session.set_fit(lane, idx, fit)
+ {
+ let (w, h) = session.resolution();
+ self.notify_user(format!("FIT POLICY: {} on {w}x{h}", fit_label(fit)).into());
+ self.reset_after_reseek();
+ }
+ cx.notify();
+ }
+
+ /// The scale against the bed it is drawn on and the timeline it is drawn
+ /// from: every clamp, zoom and scroll is worked out through this, and the
+ /// bed is measured off the ruler's probe rather than remembered, so a
+ /// resized window is a resized view on the very next answer.
+ pub(crate) fn view(&self) -> View {
+ View {
+ scale: self.scale,
+ bed: f32::from(self.ruler.get().size.width),
+ duration: self.drawn_duration(),
+ fps: self.fps,
+ }
+ }
+
+ /// Magnifies the timeline about a point that stays put: `anchor` is how many
+ /// pixels along the bed to hold still (a ctrl+wheel holds the pointer), and
+ /// with none it is the playhead -- so the frame being worked on is still the
+ /// frame on screen after the zoom. Clamped at both ends by [`View`]: out
+ /// stops at the whole timeline on the bed, in at a handful of frames.
+ pub(crate) fn zoom(&mut self, factor: f32, anchor: Option, cx: &mut Context) {
+ let view = self.view();
+ let at = self.playhead(view.duration);
+ let anchor = anchor.unwrap_or_else(|| self.scale.px_at(at).clamp(0., view.bed));
+ self.scale = view.zoomed(factor, anchor);
+ // The view a hand chose. A zoom about the playhead leaves it on the
+ // bed, so this is given back on the very next frame and only a zoom
+ // that took the head off screen -- ctrl+wheel away from it -- holds.
+ self.panned = true;
+ cx.notify();
+ }
+
+ /// All the way back out: the whole timeline across the bed, and the one
+ /// thing that reads the timeline's own length to decide how wide a second
+ /// is drawn.
+ pub(crate) fn zoom_fit(&mut self, cx: &mut Context) {
+ self.scale = self.view().fit();
+ cx.notify();
+ }
+
+ /// Slides the view along the timeline by `notches` of the wheel, later in
+ /// time for a positive one and [`SCROLL_NOTCH_SHARE`] of the bed each. The
+ /// scale is untouched: this is the timeline's scrollbar, and the only thing
+ /// on the panel that moves what is on screen without magnifying it.
+ pub(crate) fn scroll_view(&mut self, notches: f32, cx: &mut Context) {
+ let view = self.view();
+ // Nothing painted yet: there is no bed to measure a notch against, and
+ // a start moved against a zero width would be a jump to the head.
+ if view.bed <= 0. {
+ return;
+ }
+ self.scale = view.scrolled(notches * view.bed * SCROLL_NOTCH_SHARE);
+ // The one gesture whose whole purpose is to look away from the
+ // playhead: while playing it wins over the follow, which is what every
+ // editor does with a scroll during playback.
+ self.panned = true;
+ cx.notify();
+ }
+
+ /// One notch of the wheel anywhere over the timeline -- the ruler or a
+ /// lane's bed alike, since a hand aims at the clip it is working on and not
+ /// at the strip above it. Ctrl zooms about the pointer, bare scrolls the
+ /// view along: the mapping Premiere, Movavi and CapCut share, and the one
+ /// the user named.
+ ///
+ /// The anchor is measured off the ruler's probe wherever the pointer is,
+ /// because that probe *is* the bed's x-to-time mapping ([`HEADER_W`]) and
+ /// every lane is drawn through the same one.
+ pub(crate) fn timeline_wheel(&mut self, event: &ScrollWheelEvent, cx: &mut Context) {
+ let d = wheel_delta(event);
+ if d == 0. {
+ return;
+ }
+ let factor = match d > 0. {
+ true => ZOOM_STEP,
+ false => 1. / ZOOM_STEP,
+ };
+ match event.modifiers.control {
+ true => {
+ let anchor = px_along(event.position.x, self.ruler.get());
+ self.zoom(factor, Some(anchor), cx);
+ }
+ // Up is back towards the head of the timeline, the way a wheel up
+ // is back towards the top of a page.
+ false => self.scroll_view(-d.signum(), cx),
+ }
+ }
+}