From c377b3dabac75b79cca0b8cd42ccb111060d623b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 11 Aug 2026 01:18:51 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(panel):=20the=20volume=20is=20?= =?UTF-8?q?a=20slider,=20and=20an=20empty=20timeline=20refuses=20to=20play?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.rs | 283 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 270 insertions(+), 13 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 30482e8..109d73b 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -95,6 +95,9 @@ const ROW_H: f32 = 32.; /// wear in the lanes, which is the whole of the panel<->timeline association. const SWATCH_W: f32 = 4.; const CONTROL_H: f32 = 28.; +/// The volume slider beside its button: a hundred steps across it, so a pixel +/// is finer than a step and the drag reads as continuous. +const VOLUME_W: f32 = 110.; const RULER_HIT_H: f32 = HIT_MIN; /// Wide enough for `HH:MM:SS:FF / HH:MM:SS:FF`, and fixed so changing digits /// cannot push the layout around. @@ -128,6 +131,18 @@ const ESCAPE: &str = "escape"; /// as a program name rather than as a file name. const NO_FILE: &str = "no file open"; +/// What a press of play says when there is nothing to play: no timeline at all +/// and an emptied one are the same answer to the user, so they are one line. +const NOTHING_TO_PLAY: &str = "NOTHING TO PLAY — put a clip on the timeline first"; + +/// Whether a press of play would have anything to play. No timeline at all and +/// one every clip has been taken off are the same state to a transport, and the +/// button and the key both have to give the same answer to it -- so there is +/// one of them, and it is free of the window so it can be checked without one. +fn nothing_to_play(session: Option<&PlaybackSession>) -> bool { + session.is_none_or(PlaybackSession::is_empty) +} + /// What the monitoring output is set to. Two things, not one: the level the /// user picked, and whether it is being held silent -- so unmuting comes back /// to the level rather than to a guess. @@ -146,29 +161,45 @@ struct Volume { } impl Volume { - /// 5% a press: twenty of them across the range. - const MAX_STEPS: u8 = 20; + /// One step is one percent: fine enough that a drag along the slider reads + /// as continuous, and still a count rather than a float. + const MAX_STEPS: u8 = 100; + + /// 5% a press: twenty presses across the range, which is what the keys have + /// always moved. The slider is what the finer grid is for. + const KEY_STEP: u8 = 5; /// What the device is set to: mute wins, and the level is what it returns /// to. `0.0..=1.0`, which is the range the plugin's ABI accepts. fn gain(self) -> f32 { - if self.muted { - 0. - } else { - f32::from(self.steps) / f32::from(Self::MAX_STEPS) - } + if self.muted { 0. } else { self.along() } } /// One press up or down, clamped at both ends -- saturating, so the count /// cannot wrap past silence into full volume. fn step(&mut self, up: bool) { self.steps = if up { - self.steps.saturating_add(1).min(Self::MAX_STEPS) + self.steps.saturating_add(Self::KEY_STEP).min(Self::MAX_STEPS) } else { - self.steps.saturating_sub(1) + self.steps.saturating_sub(Self::KEY_STEP) }; } + /// Where the hand let go along the slider, 0..1 from silence to full. The + /// grid is the same one the keys land on, so a drag to the top and a key + /// held up reach the very same number -- and a drag never touches mute: + /// asking for a level while muted is not asking for sound. + fn set_along(&mut self, frac: f32) { + self.steps = (frac.clamp(0., 1.) * f32::from(Self::MAX_STEPS)).round() as u8; + } + + /// How full the slider is drawn, 0..1. The level and not the gain: a muted + /// slider still shows what unmuting comes back to, exactly as the label + /// does. + fn along(self) -> f32 { + f32::from(self.steps) / f32::from(Self::MAX_STEPS) + } + /// What the button reads. The level shows while muted too: it is what the /// next press of the mute key brings back. fn label(self) -> String { @@ -584,6 +615,11 @@ struct Player { /// also why it is not written to the project file and cannot reach an /// export. [`Player::apply_volume`] is what pushes it at a session. volume: Volume, + /// Where the volume slider was last painted, and whether a hand is on it -- + /// the speed bar's pair, for the speed bar's reason: the pointer moves + /// arrive at the root, so the bar's own geometry has to be readable there. + volume_bar: Rc>>, + volume_dragging: bool, /// The keybindings overlay is up. While it is, it owns the keyboard and the /// pointer: a stroke or a click meant for a row must not also cut the /// timeline. @@ -2483,10 +2519,33 @@ impl Player { 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); + } + 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 `done` from its one black frame onward, so the restart + // below would start a clock against a zero-length timeline -- and it is + // `done` 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.notice = Some(NOTHING_TO_PLAY.into()), + } + cx.notify(); + return; + } if self.done { self.seek(0., cx); if let Some(session) = &mut self.session { @@ -2991,6 +3050,16 @@ impl Render for Player { } 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 this.volume_dragging { + if event.pressed_button == Some(MouseButton::Left) { + this.drag_volume(event.position.x, cx); + } else { + this.volume_dragging = false; + } + return; + } if !this.scrubbing { return; } @@ -3031,6 +3100,10 @@ impl Render for Player { this.drag_speed(event.position.x, false, cx); return; } + if std::mem::take(&mut this.volume_dragging) { + this.drag_volume(event.position.x, cx); + return; + } if std::mem::take(&mut this.scrubbing) { this.scrub_to(event.position.x, true, cx); } @@ -3453,8 +3526,17 @@ impl Player { "transport", Some(transport_glyph(playing).into_any_element()), if playing { "Pause" } else { "Play" }, - key(ActionId::Play), - live, + if nothing_to_play(self.session.as_ref()) && !playing { + format!("{} — put a clip on a lane first", key(ActionId::Play)) + } else { + key(ActionId::Play) + }, + // An empty timeline has nothing to play, so the button + // says so by being dim rather than by starting a clock + // against nothing (the key press answers with the + // notice). Still live while it *is* playing: a delete + // can empty the timeline mid-play and that has to stop. + live && (playing || !nothing_to_play(self.session.as_ref())), cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)), )) .child(separator()) @@ -3529,6 +3611,15 @@ impl Player { this.set_volume(|volume| volume.muted = !volume.muted, cx) }), )) + // The level itself, to drag. The button beside it stays the + // mute -- one gesture each -- and the label above is what + // this writes, live, so the number follows the hand. + .child(volume_slider( + self.volume, + self.volume_bar.clone(), + live, + cx, + )) .child(separator()) // Import is not here: it belongs to the media list it adds // to, and two doors into one action is a question about @@ -6440,6 +6531,59 @@ fn control( }) } +/// The monitoring level as something to drag: 4 px of bar to look at and the +/// whole control's height to hit (WCAG 2.5.8), the split the speed bar and the +/// colour sliders both make. Only the level -- mute is the button beside it, so +/// a muted slider still shows what unmuting comes back to, drawn dim. +/// +/// Dimmed and inert without a timeline, like every other control that would +/// have nothing to act on. +fn volume_slider( + volume: Volume, + bar: Rc>>, + enabled: bool, + cx: &mut Context, +) -> impl IntoElement { + div() + .id("volume-bar") + .relative() + .flex_none() + .w(px(VOLUME_W)) + .h(px(CONTROL_H)) + .flex() + .items_center() + .tooltip(|_, cx| { + cx.new(|_| Tip("Volume — drag to set the level; the button mutes".into())) + .into() + }) + .when(!enabled, |d| d.opacity(0.4).cursor_not_allowed()) + .when(enabled, |d| { + d.cursor_pointer() + .on_mouse_down( + MouseButton::Left, + cx.listener(|this, event: &MouseDownEvent, _, cx| { + this.volume_dragging = true; + this.drag_volume(event.position.x, cx); + }), + ) + .child(bounds_probe(bar)) + }) + .child( + div() + .w_full() + .h(px(4.)) + .rounded(px(2.)) + .bg(rgb(SURFACE)) + .child( + div() + .h_full() + .w(relative(volume.along())) + .rounded(px(2.)) + .bg(rgb(if volume.muted { INK_DIM } else { ACCENT })), + ), + ) +} + /// The line between two groups of buttons. fn separator() -> impl IntoElement { div() @@ -6710,13 +6854,14 @@ mod tests { INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX, LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_W, NO_FILE, PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, SOURCE_TINTS, SPEED_PRESETS, - SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, Volume, WAVE_BPS, WAVE_COL, Wave, + SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, VOLUME_W, Volume, WAVE_BPS, WAVE_COL, + Wave, applicable, band_label, can_add, cancels_export, color_snap, envelope, eq_x, eq_y, export_path, export_settings, format_key, format_line, format_refusal, frac_along, frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked, menu_at, normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label, silence_rate, source_tint, span_partner, speed_at, start_frac, timecode, unseen_paths, - unseen_sources, whole_take, width_frac, window_title, + nothing_to_play, unseen_sources, whole_take, width_frac, window_title, }; use super::{LaneKind, file_name, file_uri, library_rows, unscannable}; @@ -8561,6 +8706,116 @@ mod tests { assert_eq!(source_tint(9), source_tint(1)); assert_eq!(source_tint(usize::MAX), SOURCE_TINTS[usize::MAX % 4]); } + + /// The bug: an empty timeline is end-of-stream from its one black frame + /// onward, so the pump had `done` set before anything was ever pressed -- + /// and the transport's restart branch read that as "played out, start from + /// the top". It started a clock against a zero-length timeline, which was + /// `done` again by the next repaint, so every further press restarted it + /// too: the button read "Pause" and no press of it ever paused. + /// + /// What holds it now is one predicate, checked here against real sessions + /// on both sides -- the emptied one refuses, a timeline with clips on it + /// does not. + #[test] + fn an_empty_timeline_has_nothing_to_play() { + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + // Silent like the engine suite: this opens the real device. + session.set_gain(0.0); + // A timeline with clips on it plays, and always did: the guard must not + // touch that side. + assert!(!session.is_empty()); + assert!(!nothing_to_play(Some(&session))); + + // Every clip taken off, which is a state and not a failure. + while session.delete_clip(Lane::V1, 0) {} + while session.delete_clip(Lane::A1, 0) {} + assert!(session.is_empty(), "the timeline is empty"); + assert_eq!(session.timeline_duration(), 0.0); + + // What the pump does every render, and what set `done` before the fix: + // the black frame goes by and the session is at its end at once. + for _ in 0..40 { + while session.try_frame().is_some() {} + std::thread::sleep(Duration::from_millis(10)); + } + assert!(session.is_eos(), "an empty timeline is done before it starts"); + + // So the press is refused rather than sent down the restart branch -- + // and with no session at all it is the same refusal. + assert!(nothing_to_play(Some(&session))); + assert!(nothing_to_play(None)); + assert!(!session.is_playing(), "and nothing was started"); + } + + /// The slider writes the same numbers the keys do, and mute is not one of + /// them: dragging while muted picks the level unmuting comes back to. + #[test] + fn the_slider_lands_on_the_grid_the_keys_move_on() { + let mut volume = Volume::default(); + // Both ends exactly, and clamped past them. + volume.set_along(0.); + assert_eq!(volume.gain(), 0.0); + assert_eq!(volume.label(), "Vol 0%"); + volume.set_along(1.5); + assert_eq!(volume.gain(), 1.0); + assert_eq!(volume.along(), 1.0); + + // Halfway is 50%, and a key press from there is 5% -- the same step + // count as before, on a finer grid. + volume.set_along(0.5); + assert_eq!(volume.label(), "Vol 50%"); + volume.step(true); + assert_eq!(volume.label(), "Vol 55%"); + volume.step(false); + assert_eq!(volume.gain(), 0.5); + + // A number no step lands on comes back as the nearest one, so the label + // and the fill are the same value the device was handed. + volume.set_along(0.333); + assert_eq!(volume.label(), "Vol 33%"); + assert_eq!(volume.along(), 0.33); + + // Muted, the drag moves the level and nothing comes out. + volume.muted = true; + volume.set_along(0.8); + assert_eq!(volume.gain(), 0.0); + assert_eq!(volume.label(), "Muted 80%"); + volume.muted = false; + assert_eq!(volume.gain(), 0.8); + } + + /// The slider lands where it paints: the arithmetic `Player::drag_volume` + /// runs over the bar's own painted width, which is the one thing a test of + /// it can share without re-deriving it. + #[test] + fn the_volume_slider_lands_where_it_paints() { + let bar = Bounds { + origin: point(px(420.), px(508.)), + size: size(px(VOLUME_W), px(CONTROL_H)), + }; + let at = |x: f32| { + let mut volume = Volume::default(); + volume.set_along(frac_along(px(x), bar)); + volume + }; + assert_eq!(at(420.).gain(), 0.0, "the left end is silence"); + assert_eq!(at(420. + VOLUME_W).gain(), 1.0, "the right end is full"); + assert_eq!(at(-4000.).gain(), 0.0, "off the left clamps"); + assert_eq!(at(9999.).gain(), 1.0, "off the right clamps"); + // Every pixel along it: a level the keys could also reach, painted back + // where the hand pressed to within the half step the rounding costs. + for step in 0..=(VOLUME_W as u32) { + let along = step as f32 / VOLUME_W; + let volume = at(420. + along * VOLUME_W); + let painted = volume.along(); + let slack = 0.5 / f32::from(Volume::MAX_STEPS) + 1e-4; + assert!( + (painted - along).abs() <= slack, + "pressed at {along}, paints at {painted}" + ); + } + } } fn main() { @@ -8681,6 +8936,8 @@ fn main() { // Full and unmuted, which is what the session it was just // handed is already set to: nothing to push at startup. volume: Volume::default(), + volume_bar: Rc::default(), + volume_dragging: false, // Only ever used with a timeline; 30 keeps the empty // timecode reading in frames rather than in NaN. fps: meta.map_or(30., |meta| meta.frame_rate),