forge
Explore
Log in

Don't you dare go Hollow

The volume is a slider, and an empty timeline refuses to play

Browse files at this commit

c377b3dabac75b79cca0b8cd42ccb111060d623bAhmet Kaan GümüşParent: 88fbed0
  • feat
    • the volume is a slider, and an empty timeline refuses to play
Show full message
✨ feat(panel): the volume is a slider, and an empty timeline refuses to play

The level is a hundred steps now rather than twenty, so a drag along the
bar reads as continuous; the keys still move five percent a press and
mute is still the button beside it.

And a timeline with nothing on it is end-of-stream from its one black
frame onward, so the transport read it as "played out" and restarted a
clock that had nothing to run against: the button said Pause and no
press of it ever paused. Nothing to play is a message now, and the
button dims -- unless it is playing, which a delete can leave it doing.
crates/app/src/main.rsmodified+270 -13
@@ -95,6 +95,9 @@ const ROW_H: f32 = 32.;
95 /// wear in the lanes, which is the whole of the panel<->timeline association.95 /// wear in the lanes, which is the whole of the panel<->timeline association.
96 const SWATCH_W: f32 = 4.;96 const SWATCH_W: f32 = 4.;
97 const CONTROL_H: f32 = 28.;97 const CONTROL_H: f32 = 28.;
98+/// The volume slider beside its button: a hundred steps across it, so a pixel
99+/// is finer than a step and the drag reads as continuous.
100+const VOLUME_W: f32 = 110.;
98 const RULER_HIT_H: f32 = HIT_MIN;101 const RULER_HIT_H: f32 = HIT_MIN;
99 /// Wide enough for `HH:MM:SS:FF / HH:MM:SS:FF`, and fixed so changing digits102 /// Wide enough for `HH:MM:SS:FF / HH:MM:SS:FF`, and fixed so changing digits
100 /// cannot push the layout around.103 /// cannot push the layout around.
@@ -128,6 +131,18 @@ const ESCAPE: &str = "escape";
128 /// as a program name rather than as a file name.131 /// as a program name rather than as a file name.
129 const NO_FILE: &str = "no file open";132 const NO_FILE: &str = "no file open";
130 133
134+/// What a press of play says when there is nothing to play: no timeline at all
135+/// and an emptied one are the same answer to the user, so they are one line.
136+const NOTHING_TO_PLAY: &str = "NOTHING TO PLAY — put a clip on the timeline first";
137+
138+/// Whether a press of play would have anything to play. No timeline at all and
139+/// one every clip has been taken off are the same state to a transport, and the
140+/// button and the key both have to give the same answer to it -- so there is
141+/// one of them, and it is free of the window so it can be checked without one.
142+fn nothing_to_play(session: Option<&PlaybackSession>) -> bool {
143+ session.is_none_or(PlaybackSession::is_empty)
144+}
145+
131 /// What the monitoring output is set to. Two things, not one: the level the146 /// What the monitoring output is set to. Two things, not one: the level the
132 /// user picked, and whether it is being held silent -- so unmuting comes back147 /// user picked, and whether it is being held silent -- so unmuting comes back
133 /// to the level rather than to a guess.148 /// to the level rather than to a guess.
@@ -146,29 +161,45 @@ struct Volume {
146 }161 }
147 162
148 impl Volume {163 impl Volume {
149- /// 5% a press: twenty of them across the range.164+ /// One step is one percent: fine enough that a drag along the slider reads
150- const MAX_STEPS: u8 = 20;165+ /// as continuous, and still a count rather than a float.
166+ const MAX_STEPS: u8 = 100;
167+
168+ /// 5% a press: twenty presses across the range, which is what the keys have
169+ /// always moved. The slider is what the finer grid is for.
170+ const KEY_STEP: u8 = 5;
151 171
152 /// What the device is set to: mute wins, and the level is what it returns172 /// What the device is set to: mute wins, and the level is what it returns
153 /// to. `0.0..=1.0`, which is the range the plugin's ABI accepts.173 /// to. `0.0..=1.0`, which is the range the plugin's ABI accepts.
154 fn gain(self) -> f32 {174 fn gain(self) -> f32 {
155- if self.muted {175+ if self.muted { 0. } else { self.along() }
156- 0.
157- } else {
158- f32::from(self.steps) / f32::from(Self::MAX_STEPS)
159- }
160 }176 }
161 177
162 /// One press up or down, clamped at both ends -- saturating, so the count178 /// One press up or down, clamped at both ends -- saturating, so the count
163 /// cannot wrap past silence into full volume.179 /// cannot wrap past silence into full volume.
164 fn step(&mut self, up: bool) {180 fn step(&mut self, up: bool) {
165 self.steps = if up {181 self.steps = if up {
166- self.steps.saturating_add(1).min(Self::MAX_STEPS)182+ self.steps.saturating_add(Self::KEY_STEP).min(Self::MAX_STEPS)
167 } else {183 } else {
168- self.steps.saturating_sub(1)184+ self.steps.saturating_sub(Self::KEY_STEP)
169 };185 };
170 }186 }
171 187
188+ /// Where the hand let go along the slider, 0..1 from silence to full. The
189+ /// grid is the same one the keys land on, so a drag to the top and a key
190+ /// held up reach the very same number -- and a drag never touches mute:
191+ /// asking for a level while muted is not asking for sound.
192+ fn set_along(&mut self, frac: f32) {
193+ self.steps = (frac.clamp(0., 1.) * f32::from(Self::MAX_STEPS)).round() as u8;
194+ }
195+
196+ /// How full the slider is drawn, 0..1. The level and not the gain: a muted
197+ /// slider still shows what unmuting comes back to, exactly as the label
198+ /// does.
199+ fn along(self) -> f32 {
200+ f32::from(self.steps) / f32::from(Self::MAX_STEPS)
201+ }
202+
172 /// What the button reads. The level shows while muted too: it is what the203 /// What the button reads. The level shows while muted too: it is what the
173 /// next press of the mute key brings back.204 /// next press of the mute key brings back.
174 fn label(self) -> String {205 fn label(self) -> String {
@@ -584,6 +615,11 @@ struct Player {
584 /// also why it is not written to the project file and cannot reach an615 /// also why it is not written to the project file and cannot reach an
585 /// export. [`Player::apply_volume`] is what pushes it at a session.616 /// export. [`Player::apply_volume`] is what pushes it at a session.
586 volume: Volume,617 volume: Volume,
618+ /// Where the volume slider was last painted, and whether a hand is on it --
619+ /// the speed bar's pair, for the speed bar's reason: the pointer moves
620+ /// arrive at the root, so the bar's own geometry has to be readable there.
621+ volume_bar: Rc<Cell<Bounds<Pixels>>>,
622+ volume_dragging: bool,
587 /// The keybindings overlay is up. While it is, it owns the keyboard and the623 /// The keybindings overlay is up. While it is, it owns the keyboard and the
588 /// pointer: a stroke or a click meant for a row must not also cut the624 /// pointer: a stroke or a click meant for a row must not also cut the
589 /// timeline.625 /// timeline.
@@ -2483,10 +2519,33 @@ impl Player {
2483 cx.notify();2519 cx.notify();
2484 }2520 }
2485 2521
2522+ /// Where the pointer sits along the slider, as a level. The press and every
2523+ /// sample after it come here, so the sound follows the hand rather than the
2524+ /// release -- there is nothing to undo about a monitoring level, which is
2525+ /// why this writes live and keeps no gesture state beyond the flag.
2526+ fn drag_volume(&mut self, x: Pixels, cx: &mut Context<Self>) {
2527+ let along = frac_along(x, self.volume_bar.get());
2528+ self.set_volume(|volume| volume.set_along(along), cx);
2529+ }
2530+
2486 fn toggle_or_restart(&mut self, cx: &mut Context<Self>) {2531 fn toggle_or_restart(&mut self, cx: &mut Context<Self>) {
2487 if self.exporting().is_some() {2532 if self.exporting().is_some() {
2488 return;2533 return;
2489 }2534 }
2535+ // Nothing to play is a message, not a transport state. An empty
2536+ // timeline is `done` from its one black frame onward, so the restart
2537+ // below would start a clock against a zero-length timeline -- and it is
2538+ // `done` again by the next repaint, so no later press could ever stop
2539+ // it: the button would read "Pause" and never pause. A delete can empty
2540+ // the timeline mid-play, and that press must still stop it.
2541+ if nothing_to_play(self.session.as_ref()) {
2542+ match self.session.as_mut().filter(|s| s.is_playing()) {
2543+ Some(session) => session.pause(),
2544+ None => self.notice = Some(NOTHING_TO_PLAY.into()),
2545+ }
2546+ cx.notify();
2547+ return;
2548+ }
2490 if self.done {2549 if self.done {
2491 self.seek(0., cx);2550 self.seek(0., cx);
2492 if let Some(session) = &mut self.session {2551 if let Some(session) = &mut self.session {
@@ -2991,6 +3050,16 @@ impl Render for Player {
2991 }3050 }
2992 return;3051 return;
2993 }3052 }
3053+ // The volume slider, the same live writes: what the hand is on
3054+ // is what the speakers are doing, and there is nothing to undo.
3055+ if this.volume_dragging {
3056+ if event.pressed_button == Some(MouseButton::Left) {
3057+ this.drag_volume(event.position.x, cx);
3058+ } else {
3059+ this.volume_dragging = false;
3060+ }
3061+ return;
3062+ }
2994 if !this.scrubbing {3063 if !this.scrubbing {
2995 return;3064 return;
2996 }3065 }
@@ -3031,6 +3100,10 @@ impl Render for Player {
3031 this.drag_speed(event.position.x, false, cx);3100 this.drag_speed(event.position.x, false, cx);
3032 return;3101 return;
3033 }3102 }
3103+ if std::mem::take(&mut this.volume_dragging) {
3104+ this.drag_volume(event.position.x, cx);
3105+ return;
3106+ }
3034 if std::mem::take(&mut this.scrubbing) {3107 if std::mem::take(&mut this.scrubbing) {
3035 this.scrub_to(event.position.x, true, cx);3108 this.scrub_to(event.position.x, true, cx);
3036 }3109 }
@@ -3453,8 +3526,17 @@ impl Player {
3453 "transport",3526 "transport",
3454 Some(transport_glyph(playing).into_any_element()),3527 Some(transport_glyph(playing).into_any_element()),
3455 if playing { "Pause" } else { "Play" },3528 if playing { "Pause" } else { "Play" },
3456- key(ActionId::Play),3529+ if nothing_to_play(self.session.as_ref()) && !playing {
3457- live,3530+ format!("{} — put a clip on a lane first", key(ActionId::Play))
3531+ } else {
3532+ key(ActionId::Play)
3533+ },
3534+ // An empty timeline has nothing to play, so the button
3535+ // says so by being dim rather than by starting a clock
3536+ // against nothing (the key press answers with the
3537+ // notice). Still live while it *is* playing: a delete
3538+ // can empty the timeline mid-play and that has to stop.
3539+ live && (playing || !nothing_to_play(self.session.as_ref())),
3458 cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)),3540 cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)),
3459 ))3541 ))
3460 .child(separator())3542 .child(separator())
@@ -3529,6 +3611,15 @@ impl Player {
3529 this.set_volume(|volume| volume.muted = !volume.muted, cx)3611 this.set_volume(|volume| volume.muted = !volume.muted, cx)
3530 }),3612 }),
3531 ))3613 ))
3614+ // The level itself, to drag. The button beside it stays the
3615+ // mute -- one gesture each -- and the label above is what
3616+ // this writes, live, so the number follows the hand.
3617+ .child(volume_slider(
3618+ self.volume,
3619+ self.volume_bar.clone(),
3620+ live,
3621+ cx,
3622+ ))
3532 .child(separator())3623 .child(separator())
3533 // Import is not here: it belongs to the media list it adds3624 // Import is not here: it belongs to the media list it adds
3534 // to, and two doors into one action is a question about3625 // to, and two doors into one action is a question about
@@ -6440,6 +6531,59 @@ fn control(
6440 })6531 })
6441 }6532 }
6442 6533
6534+/// The monitoring level as something to drag: 4 px of bar to look at and the
6535+/// whole control's height to hit (WCAG 2.5.8), the split the speed bar and the
6536+/// colour sliders both make. Only the level -- mute is the button beside it, so
6537+/// a muted slider still shows what unmuting comes back to, drawn dim.
6538+///
6539+/// Dimmed and inert without a timeline, like every other control that would
6540+/// have nothing to act on.
6541+fn volume_slider(
6542+ volume: Volume,
6543+ bar: Rc<Cell<Bounds<Pixels>>>,
6544+ enabled: bool,
6545+ cx: &mut Context<Player>,
6546+) -> impl IntoElement {
6547+ div()
6548+ .id("volume-bar")
6549+ .relative()
6550+ .flex_none()
6551+ .w(px(VOLUME_W))
6552+ .h(px(CONTROL_H))
6553+ .flex()
6554+ .items_center()
6555+ .tooltip(|_, cx| {
6556+ cx.new(|_| Tip("Volume — drag to set the level; the button mutes".into()))
6557+ .into()
6558+ })
6559+ .when(!enabled, |d| d.opacity(0.4).cursor_not_allowed())
6560+ .when(enabled, |d| {
6561+ d.cursor_pointer()
6562+ .on_mouse_down(
6563+ MouseButton::Left,
6564+ cx.listener(|this, event: &MouseDownEvent, _, cx| {
6565+ this.volume_dragging = true;
6566+ this.drag_volume(event.position.x, cx);
6567+ }),
6568+ )
6569+ .child(bounds_probe(bar))
6570+ })
6571+ .child(
6572+ div()
6573+ .w_full()
6574+ .h(px(4.))
6575+ .rounded(px(2.))
6576+ .bg(rgb(SURFACE))
6577+ .child(
6578+ div()
6579+ .h_full()
6580+ .w(relative(volume.along()))
6581+ .rounded(px(2.))
6582+ .bg(rgb(if volume.muted { INK_DIM } else { ACCENT })),
6583+ ),
6584+ )
6585+}
6586+
6443 /// The line between two groups of buttons.6587 /// The line between two groups of buttons.
6444 fn separator() -> impl IntoElement {6588 fn separator() -> impl IntoElement {
6445 div()6589 div()
@@ -6710,13 +6854,14 @@ mod tests {
6710 INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX,6854 INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX,
6711 LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_W, NO_FILE, PANEL_H,6855 LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_W, NO_FILE, PANEL_H,
6712 Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, SOURCE_TINTS, SPEED_PRESETS,6856 Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, SOURCE_TINTS, SPEED_PRESETS,
6713- SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, Volume, WAVE_BPS, WAVE_COL, Wave,6857+ SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, VOLUME_W, Volume, WAVE_BPS, WAVE_COL,
6858+ Wave,
6714 applicable, band_label, can_add, cancels_export, color_snap, envelope, eq_x, eq_y,6859 applicable, band_label, can_add, cancels_export, color_snap, envelope, eq_x, eq_y,
6715 export_path, export_settings, format_key, format_line, format_refusal, frac_along,6860 export_path, export_settings, format_key, format_line, format_refusal, frac_along,
6716 frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked,6861 frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked,
6717 menu_at, normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label,6862 menu_at, normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label,
6718 silence_rate, source_tint, span_partner, speed_at, start_frac, timecode, unseen_paths,6863 silence_rate, source_tint, span_partner, speed_at, start_frac, timecode, unseen_paths,
6719- unseen_sources, whole_take, width_frac, window_title,6864+ nothing_to_play, unseen_sources, whole_take, width_frac, window_title,
6720 };6865 };
6721 use super::{LaneKind, file_name, file_uri, library_rows, unscannable};6866 use super::{LaneKind, file_name, file_uri, library_rows, unscannable};
6722 6867
@@ -8561,6 +8706,116 @@ mod tests {
8561 assert_eq!(source_tint(9), source_tint(1));8706 assert_eq!(source_tint(9), source_tint(1));
8562 assert_eq!(source_tint(usize::MAX), SOURCE_TINTS[usize::MAX % 4]);8707 assert_eq!(source_tint(usize::MAX), SOURCE_TINTS[usize::MAX % 4]);
8563 }8708 }
8709+
8710+ /// The bug: an empty timeline is end-of-stream from its one black frame
8711+ /// onward, so the pump had `done` set before anything was ever pressed --
8712+ /// and the transport's restart branch read that as "played out, start from
8713+ /// the top". It started a clock against a zero-length timeline, which was
8714+ /// `done` again by the next repaint, so every further press restarted it
8715+ /// too: the button read "Pause" and no press of it ever paused.
8716+ ///
8717+ /// What holds it now is one predicate, checked here against real sessions
8718+ /// on both sides -- the emptied one refuses, a timeline with clips on it
8719+ /// does not.
8720+ #[test]
8721+ fn an_empty_timeline_has_nothing_to_play() {
8722+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
8723+ // Silent like the engine suite: this opens the real device.
8724+ session.set_gain(0.0);
8725+ // A timeline with clips on it plays, and always did: the guard must not
8726+ // touch that side.
8727+ assert!(!session.is_empty());
8728+ assert!(!nothing_to_play(Some(&session)));
8729+
8730+ // Every clip taken off, which is a state and not a failure.
8731+ while session.delete_clip(Lane::V1, 0) {}
8732+ while session.delete_clip(Lane::A1, 0) {}
8733+ assert!(session.is_empty(), "the timeline is empty");
8734+ assert_eq!(session.timeline_duration(), 0.0);
8735+
8736+ // What the pump does every render, and what set `done` before the fix:
8737+ // the black frame goes by and the session is at its end at once.
8738+ for _ in 0..40 {
8739+ while session.try_frame().is_some() {}
8740+ std::thread::sleep(Duration::from_millis(10));
8741+ }
8742+ assert!(session.is_eos(), "an empty timeline is done before it starts");
8743+
8744+ // So the press is refused rather than sent down the restart branch --
8745+ // and with no session at all it is the same refusal.
8746+ assert!(nothing_to_play(Some(&session)));
8747+ assert!(nothing_to_play(None));
8748+ assert!(!session.is_playing(), "and nothing was started");
8749+ }
8750+
8751+ /// The slider writes the same numbers the keys do, and mute is not one of
8752+ /// them: dragging while muted picks the level unmuting comes back to.
8753+ #[test]
8754+ fn the_slider_lands_on_the_grid_the_keys_move_on() {
8755+ let mut volume = Volume::default();
8756+ // Both ends exactly, and clamped past them.
8757+ volume.set_along(0.);
8758+ assert_eq!(volume.gain(), 0.0);
8759+ assert_eq!(volume.label(), "Vol 0%");
8760+ volume.set_along(1.5);
8761+ assert_eq!(volume.gain(), 1.0);
8762+ assert_eq!(volume.along(), 1.0);
8763+
8764+ // Halfway is 50%, and a key press from there is 5% -- the same step
8765+ // count as before, on a finer grid.
8766+ volume.set_along(0.5);
8767+ assert_eq!(volume.label(), "Vol 50%");
8768+ volume.step(true);
8769+ assert_eq!(volume.label(), "Vol 55%");
8770+ volume.step(false);
8771+ assert_eq!(volume.gain(), 0.5);
8772+
8773+ // A number no step lands on comes back as the nearest one, so the label
8774+ // and the fill are the same value the device was handed.
8775+ volume.set_along(0.333);
8776+ assert_eq!(volume.label(), "Vol 33%");
8777+ assert_eq!(volume.along(), 0.33);
8778+
8779+ // Muted, the drag moves the level and nothing comes out.
8780+ volume.muted = true;
8781+ volume.set_along(0.8);
8782+ assert_eq!(volume.gain(), 0.0);
8783+ assert_eq!(volume.label(), "Muted 80%");
8784+ volume.muted = false;
8785+ assert_eq!(volume.gain(), 0.8);
8786+ }
8787+
8788+ /// The slider lands where it paints: the arithmetic `Player::drag_volume`
8789+ /// runs over the bar's own painted width, which is the one thing a test of
8790+ /// it can share without re-deriving it.
8791+ #[test]
8792+ fn the_volume_slider_lands_where_it_paints() {
8793+ let bar = Bounds {
8794+ origin: point(px(420.), px(508.)),
8795+ size: size(px(VOLUME_W), px(CONTROL_H)),
8796+ };
8797+ let at = |x: f32| {
8798+ let mut volume = Volume::default();
8799+ volume.set_along(frac_along(px(x), bar));
8800+ volume
8801+ };
8802+ assert_eq!(at(420.).gain(), 0.0, "the left end is silence");
8803+ assert_eq!(at(420. + VOLUME_W).gain(), 1.0, "the right end is full");
8804+ assert_eq!(at(-4000.).gain(), 0.0, "off the left clamps");
8805+ assert_eq!(at(9999.).gain(), 1.0, "off the right clamps");
8806+ // Every pixel along it: a level the keys could also reach, painted back
8807+ // where the hand pressed to within the half step the rounding costs.
8808+ for step in 0..=(VOLUME_W as u32) {
8809+ let along = step as f32 / VOLUME_W;
8810+ let volume = at(420. + along * VOLUME_W);
8811+ let painted = volume.along();
8812+ let slack = 0.5 / f32::from(Volume::MAX_STEPS) + 1e-4;
8813+ assert!(
8814+ (painted - along).abs() <= slack,
8815+ "pressed at {along}, paints at {painted}"
8816+ );
8817+ }
8818+ }
8564 }8819 }
8565 8820
8566 fn main() {8821 fn main() {
@@ -8681,6 +8936,8 @@ fn main() {
8681 // Full and unmuted, which is what the session it was just8936 // Full and unmuted, which is what the session it was just
8682 // handed is already set to: nothing to push at startup.8937 // handed is already set to: nothing to push at startup.
8683 volume: Volume::default(),8938 volume: Volume::default(),
8939+ volume_bar: Rc::default(),
8940+ volume_dragging: false,
8684 // Only ever used with a timeline; 30 keeps the empty8941 // Only ever used with a timeline; 30 keeps the empty
8685 // timecode reading in frames rather than in NaN.8942 // timecode reading in frames rather than in NaN.
8686 fps: meta.map_or(30., |meta| meta.frame_rate),8943 fps: meta.map_or(30., |meta| meta.frame_rate),