🔀 merge(T11): volume slider + empty-timeline play refusal
Browse files at this commit1 changed file +270 -13
- crates/app/src/main.rs +270 -13
crates/app/src/main.rsmodified+270 -13
| @@ -95,6 +95,9 @@ const ROW_H: f32 = 32.; | ||
| 95 | 95 | /// wear in the lanes, which is the whole of the panel<->timeline association. |
| 96 | 96 | const SWATCH_W: f32 = 4.; |
| 97 | 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 | 101 | const RULER_HIT_H: f32 = HIT_MIN; |
| 99 | 102 | /// Wide enough for `HH:MM:SS:FF / HH:MM:SS:FF`, and fixed so changing digits |
| 100 | 103 | /// cannot push the layout around. |
| @@ -128,6 +131,18 @@ const ESCAPE: &str = "escape"; | ||
| 128 | 131 | /// as a program name rather than as a file name. |
| 129 | 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 | 146 | /// What the monitoring output is set to. Two things, not one: the level the |
| 132 | 147 | /// user picked, and whether it is being held silent -- so unmuting comes back |
| 133 | 148 | /// to the level rather than to a guess. |
| @@ -146,29 +161,45 @@ struct Volume { | ||
| 146 | 161 | } |
| 147 | 162 | |
| 148 | 163 | impl Volume { |
| 149 | - /// 5% a press: twenty of them across the range. | |
| 150 | - const MAX_STEPS: u8 = 20; | |
| 164 | + /// One step is one percent: fine enough that a drag along the slider reads | |
| 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 | 172 | /// What the device is set to: mute wins, and the level is what it returns |
| 153 | 173 | /// to. `0.0..=1.0`, which is the range the plugin's ABI accepts. |
| 154 | 174 | fn gain(self) -> f32 { |
| 155 | - if self.muted { | |
| 156 | - 0. | |
| 157 | - } else { | |
| 158 | - f32::from(self.steps) / f32::from(Self::MAX_STEPS) | |
| 159 | - } | |
| 175 | + if self.muted { 0. } else { self.along() } | |
| 160 | 176 | } |
| 161 | 177 | |
| 162 | 178 | /// One press up or down, clamped at both ends -- saturating, so the count |
| 163 | 179 | /// cannot wrap past silence into full volume. |
| 164 | 180 | fn step(&mut self, up: bool) { |
| 165 | 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 | 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 | 203 | /// What the button reads. The level shows while muted too: it is what the |
| 173 | 204 | /// next press of the mute key brings back. |
| 174 | 205 | fn label(self) -> String { |
| @@ -584,6 +615,11 @@ struct Player { | ||
| 584 | 615 | /// also why it is not written to the project file and cannot reach an |
| 585 | 616 | /// export. [`Player::apply_volume`] is what pushes it at a session. |
| 586 | 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 | 623 | /// The keybindings overlay is up. While it is, it owns the keyboard and the |
| 588 | 624 | /// pointer: a stroke or a click meant for a row must not also cut the |
| 589 | 625 | /// timeline. |
| @@ -2502,10 +2538,33 @@ impl Player { | ||
| 2502 | 2538 | cx.notify(); |
| 2503 | 2539 | } |
| 2504 | 2540 | |
| 2541 | + /// Where the pointer sits along the slider, as a level. The press and every | |
| 2542 | + /// sample after it come here, so the sound follows the hand rather than the | |
| 2543 | + /// release -- there is nothing to undo about a monitoring level, which is | |
| 2544 | + /// why this writes live and keeps no gesture state beyond the flag. | |
| 2545 | + fn drag_volume(&mut self, x: Pixels, cx: &mut Context<Self>) { | |
| 2546 | + let along = frac_along(x, self.volume_bar.get()); | |
| 2547 | + self.set_volume(|volume| volume.set_along(along), cx); | |
| 2548 | + } | |
| 2549 | + | |
| 2505 | 2550 | fn toggle_or_restart(&mut self, cx: &mut Context<Self>) { |
| 2506 | 2551 | if self.exporting().is_some() { |
| 2507 | 2552 | return; |
| 2508 | 2553 | } |
| 2554 | + // Nothing to play is a message, not a transport state. An empty | |
| 2555 | + // timeline is `done` from its one black frame onward, so the restart | |
| 2556 | + // below would start a clock against a zero-length timeline -- and it is | |
| 2557 | + // `done` again by the next repaint, so no later press could ever stop | |
| 2558 | + // it: the button would read "Pause" and never pause. A delete can empty | |
| 2559 | + // the timeline mid-play, and that press must still stop it. | |
| 2560 | + if nothing_to_play(self.session.as_ref()) { | |
| 2561 | + match self.session.as_mut().filter(|s| s.is_playing()) { | |
| 2562 | + Some(session) => session.pause(), | |
| 2563 | + None => self.notice = Some(NOTHING_TO_PLAY.into()), | |
| 2564 | + } | |
| 2565 | + cx.notify(); | |
| 2566 | + return; | |
| 2567 | + } | |
| 2509 | 2568 | if self.done { |
| 2510 | 2569 | self.seek(0., cx); |
| 2511 | 2570 | if let Some(session) = &mut self.session { |
| @@ -3010,6 +3069,16 @@ impl Render for Player { | ||
| 3010 | 3069 | } |
| 3011 | 3070 | return; |
| 3012 | 3071 | } |
| 3072 | + // The volume slider, the same live writes: what the hand is on | |
| 3073 | + // is what the speakers are doing, and there is nothing to undo. | |
| 3074 | + if this.volume_dragging { | |
| 3075 | + if event.pressed_button == Some(MouseButton::Left) { | |
| 3076 | + this.drag_volume(event.position.x, cx); | |
| 3077 | + } else { | |
| 3078 | + this.volume_dragging = false; | |
| 3079 | + } | |
| 3080 | + return; | |
| 3081 | + } | |
| 3013 | 3082 | if !this.scrubbing { |
| 3014 | 3083 | return; |
| 3015 | 3084 | } |
| @@ -3050,6 +3119,10 @@ impl Render for Player { | ||
| 3050 | 3119 | this.drag_speed(event.position.x, false, cx); |
| 3051 | 3120 | return; |
| 3052 | 3121 | } |
| 3122 | + if std::mem::take(&mut this.volume_dragging) { | |
| 3123 | + this.drag_volume(event.position.x, cx); | |
| 3124 | + return; | |
| 3125 | + } | |
| 3053 | 3126 | if std::mem::take(&mut this.scrubbing) { |
| 3054 | 3127 | this.scrub_to(event.position.x, true, cx); |
| 3055 | 3128 | } |
| @@ -3472,8 +3545,17 @@ impl Player { | ||
| 3472 | 3545 | "transport", |
| 3473 | 3546 | Some(transport_glyph(playing).into_any_element()), |
| 3474 | 3547 | if playing { "Pause" } else { "Play" }, |
| 3475 | - key(ActionId::Play), | |
| 3476 | - live, | |
| 3548 | + if nothing_to_play(self.session.as_ref()) && !playing { | |
| 3549 | + format!("{} — put a clip on a lane first", key(ActionId::Play)) | |
| 3550 | + } else { | |
| 3551 | + key(ActionId::Play) | |
| 3552 | + }, | |
| 3553 | + // An empty timeline has nothing to play, so the button | |
| 3554 | + // says so by being dim rather than by starting a clock | |
| 3555 | + // against nothing (the key press answers with the | |
| 3556 | + // notice). Still live while it *is* playing: a delete | |
| 3557 | + // can empty the timeline mid-play and that has to stop. | |
| 3558 | + live && (playing || !nothing_to_play(self.session.as_ref())), | |
| 3477 | 3559 | cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)), |
| 3478 | 3560 | )) |
| 3479 | 3561 | .child(separator()) |
| @@ -3548,6 +3630,15 @@ impl Player { | ||
| 3548 | 3630 | this.set_volume(|volume| volume.muted = !volume.muted, cx) |
| 3549 | 3631 | }), |
| 3550 | 3632 | )) |
| 3633 | + // The level itself, to drag. The button beside it stays the | |
| 3634 | + // mute -- one gesture each -- and the label above is what | |
| 3635 | + // this writes, live, so the number follows the hand. | |
| 3636 | + .child(volume_slider( | |
| 3637 | + self.volume, | |
| 3638 | + self.volume_bar.clone(), | |
| 3639 | + live, | |
| 3640 | + cx, | |
| 3641 | + )) | |
| 3551 | 3642 | .child(separator()) |
| 3552 | 3643 | // Import is not here: it belongs to the media list it adds |
| 3553 | 3644 | // to, and two doors into one action is a question about |
| @@ -6459,6 +6550,59 @@ fn control( | ||
| 6459 | 6550 | }) |
| 6460 | 6551 | } |
| 6461 | 6552 | |
| 6553 | +/// The monitoring level as something to drag: 4 px of bar to look at and the | |
| 6554 | +/// whole control's height to hit (WCAG 2.5.8), the split the speed bar and the | |
| 6555 | +/// colour sliders both make. Only the level -- mute is the button beside it, so | |
| 6556 | +/// a muted slider still shows what unmuting comes back to, drawn dim. | |
| 6557 | +/// | |
| 6558 | +/// Dimmed and inert without a timeline, like every other control that would | |
| 6559 | +/// have nothing to act on. | |
| 6560 | +fn volume_slider( | |
| 6561 | + volume: Volume, | |
| 6562 | + bar: Rc<Cell<Bounds<Pixels>>>, | |
| 6563 | + enabled: bool, | |
| 6564 | + cx: &mut Context<Player>, | |
| 6565 | +) -> impl IntoElement { | |
| 6566 | + div() | |
| 6567 | + .id("volume-bar") | |
| 6568 | + .relative() | |
| 6569 | + .flex_none() | |
| 6570 | + .w(px(VOLUME_W)) | |
| 6571 | + .h(px(CONTROL_H)) | |
| 6572 | + .flex() | |
| 6573 | + .items_center() | |
| 6574 | + .tooltip(|_, cx| { | |
| 6575 | + cx.new(|_| Tip("Volume — drag to set the level; the button mutes".into())) | |
| 6576 | + .into() | |
| 6577 | + }) | |
| 6578 | + .when(!enabled, |d| d.opacity(0.4).cursor_not_allowed()) | |
| 6579 | + .when(enabled, |d| { | |
| 6580 | + d.cursor_pointer() | |
| 6581 | + .on_mouse_down( | |
| 6582 | + MouseButton::Left, | |
| 6583 | + cx.listener(|this, event: &MouseDownEvent, _, cx| { | |
| 6584 | + this.volume_dragging = true; | |
| 6585 | + this.drag_volume(event.position.x, cx); | |
| 6586 | + }), | |
| 6587 | + ) | |
| 6588 | + .child(bounds_probe(bar)) | |
| 6589 | + }) | |
| 6590 | + .child( | |
| 6591 | + div() | |
| 6592 | + .w_full() | |
| 6593 | + .h(px(4.)) | |
| 6594 | + .rounded(px(2.)) | |
| 6595 | + .bg(rgb(SURFACE)) | |
| 6596 | + .child( | |
| 6597 | + div() | |
| 6598 | + .h_full() | |
| 6599 | + .w(relative(volume.along())) | |
| 6600 | + .rounded(px(2.)) | |
| 6601 | + .bg(rgb(if volume.muted { INK_DIM } else { ACCENT })), | |
| 6602 | + ), | |
| 6603 | + ) | |
| 6604 | +} | |
| 6605 | + | |
| 6462 | 6606 | /// The line between two groups of buttons. |
| 6463 | 6607 | fn separator() -> impl IntoElement { |
| 6464 | 6608 | div() |
| @@ -6729,13 +6873,14 @@ mod tests { | ||
| 6729 | 6873 | INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX, |
| 6730 | 6874 | LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_W, NO_FILE, PANEL_H, |
| 6731 | 6875 | Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, SOURCE_TINTS, SPEED_PRESETS, |
| 6732 | - SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, Volume, WAVE_BPS, WAVE_COL, Wave, | |
| 6876 | + SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, VOLUME_W, Volume, WAVE_BPS, WAVE_COL, | |
| 6877 | + Wave, | |
| 6733 | 6878 | applicable, band_label, can_add, cancels_export, color_snap, envelope, eq_x, eq_y, |
| 6734 | 6879 | export_path, export_settings, format_key, format_line, format_refusal, frac_along, |
| 6735 | 6880 | frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked, |
| 6736 | 6881 | menu_at, normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label, |
| 6737 | 6882 | silence_rate, source_tint, span_partner, speed_at, start_frac, timecode, unseen_paths, |
| 6738 | - unseen_sources, whole_take, width_frac, window_title, | |
| 6883 | + nothing_to_play, unseen_sources, whole_take, width_frac, window_title, | |
| 6739 | 6884 | }; |
| 6740 | 6885 | use super::{LaneKind, file_name, file_uri, library_rows, unscannable}; |
| 6741 | 6886 | |
| @@ -8580,6 +8725,116 @@ mod tests { | ||
| 8580 | 8725 | assert_eq!(source_tint(9), source_tint(1)); |
| 8581 | 8726 | assert_eq!(source_tint(usize::MAX), SOURCE_TINTS[usize::MAX % 4]); |
| 8582 | 8727 | } |
| 8728 | + | |
| 8729 | + /// The bug: an empty timeline is end-of-stream from its one black frame | |
| 8730 | + /// onward, so the pump had `done` set before anything was ever pressed -- | |
| 8731 | + /// and the transport's restart branch read that as "played out, start from | |
| 8732 | + /// the top". It started a clock against a zero-length timeline, which was | |
| 8733 | + /// `done` again by the next repaint, so every further press restarted it | |
| 8734 | + /// too: the button read "Pause" and no press of it ever paused. | |
| 8735 | + /// | |
| 8736 | + /// What holds it now is one predicate, checked here against real sessions | |
| 8737 | + /// on both sides -- the emptied one refuses, a timeline with clips on it | |
| 8738 | + /// does not. | |
| 8739 | + #[test] | |
| 8740 | + fn an_empty_timeline_has_nothing_to_play() { | |
| 8741 | + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); | |
| 8742 | + // Silent like the engine suite: this opens the real device. | |
| 8743 | + session.set_gain(0.0); | |
| 8744 | + // A timeline with clips on it plays, and always did: the guard must not | |
| 8745 | + // touch that side. | |
| 8746 | + assert!(!session.is_empty()); | |
| 8747 | + assert!(!nothing_to_play(Some(&session))); | |
| 8748 | + | |
| 8749 | + // Every clip taken off, which is a state and not a failure. | |
| 8750 | + while session.delete_clip(Lane::V1, 0) {} | |
| 8751 | + while session.delete_clip(Lane::A1, 0) {} | |
| 8752 | + assert!(session.is_empty(), "the timeline is empty"); | |
| 8753 | + assert_eq!(session.timeline_duration(), 0.0); | |
| 8754 | + | |
| 8755 | + // What the pump does every render, and what set `done` before the fix: | |
| 8756 | + // the black frame goes by and the session is at its end at once. | |
| 8757 | + for _ in 0..40 { | |
| 8758 | + while session.try_frame().is_some() {} | |
| 8759 | + std::thread::sleep(Duration::from_millis(10)); | |
| 8760 | + } | |
| 8761 | + assert!(session.is_eos(), "an empty timeline is done before it starts"); | |
| 8762 | + | |
| 8763 | + // So the press is refused rather than sent down the restart branch -- | |
| 8764 | + // and with no session at all it is the same refusal. | |
| 8765 | + assert!(nothing_to_play(Some(&session))); | |
| 8766 | + assert!(nothing_to_play(None)); | |
| 8767 | + assert!(!session.is_playing(), "and nothing was started"); | |
| 8768 | + } | |
| 8769 | + | |
| 8770 | + /// The slider writes the same numbers the keys do, and mute is not one of | |
| 8771 | + /// them: dragging while muted picks the level unmuting comes back to. | |
| 8772 | + #[test] | |
| 8773 | + fn the_slider_lands_on_the_grid_the_keys_move_on() { | |
| 8774 | + let mut volume = Volume::default(); | |
| 8775 | + // Both ends exactly, and clamped past them. | |
| 8776 | + volume.set_along(0.); | |
| 8777 | + assert_eq!(volume.gain(), 0.0); | |
| 8778 | + assert_eq!(volume.label(), "Vol 0%"); | |
| 8779 | + volume.set_along(1.5); | |
| 8780 | + assert_eq!(volume.gain(), 1.0); | |
| 8781 | + assert_eq!(volume.along(), 1.0); | |
| 8782 | + | |
| 8783 | + // Halfway is 50%, and a key press from there is 5% -- the same step | |
| 8784 | + // count as before, on a finer grid. | |
| 8785 | + volume.set_along(0.5); | |
| 8786 | + assert_eq!(volume.label(), "Vol 50%"); | |
| 8787 | + volume.step(true); | |
| 8788 | + assert_eq!(volume.label(), "Vol 55%"); | |
| 8789 | + volume.step(false); | |
| 8790 | + assert_eq!(volume.gain(), 0.5); | |
| 8791 | + | |
| 8792 | + // A number no step lands on comes back as the nearest one, so the label | |
| 8793 | + // and the fill are the same value the device was handed. | |
| 8794 | + volume.set_along(0.333); | |
| 8795 | + assert_eq!(volume.label(), "Vol 33%"); | |
| 8796 | + assert_eq!(volume.along(), 0.33); | |
| 8797 | + | |
| 8798 | + // Muted, the drag moves the level and nothing comes out. | |
| 8799 | + volume.muted = true; | |
| 8800 | + volume.set_along(0.8); | |
| 8801 | + assert_eq!(volume.gain(), 0.0); | |
| 8802 | + assert_eq!(volume.label(), "Muted 80%"); | |
| 8803 | + volume.muted = false; | |
| 8804 | + assert_eq!(volume.gain(), 0.8); | |
| 8805 | + } | |
| 8806 | + | |
| 8807 | + /// The slider lands where it paints: the arithmetic `Player::drag_volume` | |
| 8808 | + /// runs over the bar's own painted width, which is the one thing a test of | |
| 8809 | + /// it can share without re-deriving it. | |
| 8810 | + #[test] | |
| 8811 | + fn the_volume_slider_lands_where_it_paints() { | |
| 8812 | + let bar = Bounds { | |
| 8813 | + origin: point(px(420.), px(508.)), | |
| 8814 | + size: size(px(VOLUME_W), px(CONTROL_H)), | |
| 8815 | + }; | |
| 8816 | + let at = |x: f32| { | |
| 8817 | + let mut volume = Volume::default(); | |
| 8818 | + volume.set_along(frac_along(px(x), bar)); | |
| 8819 | + volume | |
| 8820 | + }; | |
| 8821 | + assert_eq!(at(420.).gain(), 0.0, "the left end is silence"); | |
| 8822 | + assert_eq!(at(420. + VOLUME_W).gain(), 1.0, "the right end is full"); | |
| 8823 | + assert_eq!(at(-4000.).gain(), 0.0, "off the left clamps"); | |
| 8824 | + assert_eq!(at(9999.).gain(), 1.0, "off the right clamps"); | |
| 8825 | + // Every pixel along it: a level the keys could also reach, painted back | |
| 8826 | + // where the hand pressed to within the half step the rounding costs. | |
| 8827 | + for step in 0..=(VOLUME_W as u32) { | |
| 8828 | + let along = step as f32 / VOLUME_W; | |
| 8829 | + let volume = at(420. + along * VOLUME_W); | |
| 8830 | + let painted = volume.along(); | |
| 8831 | + let slack = 0.5 / f32::from(Volume::MAX_STEPS) + 1e-4; | |
| 8832 | + assert!( | |
| 8833 | + (painted - along).abs() <= slack, | |
| 8834 | + "pressed at {along}, paints at {painted}" | |
| 8835 | + ); | |
| 8836 | + } | |
| 8837 | + } | |
| 8583 | 8838 | } |
| 8584 | 8839 | |
| 8585 | 8840 | fn main() { |
| @@ -8700,6 +8955,8 @@ fn main() { | ||
| 8700 | 8955 | // Full and unmuted, which is what the session it was just |
| 8701 | 8956 | // handed is already set to: nothing to push at startup. |
| 8702 | 8957 | volume: Volume::default(), |
| 8958 | + volume_bar: Rc::default(), | |
| 8959 | + volume_dragging: false, | |
| 8703 | 8960 | // Only ever used with a timeline; 30 keeps the empty |
| 8704 | 8961 | // timecode reading in frames rather than in NaN. |
| 8705 | 8962 | fps: meta.map_or(30., |meta| meta.frame_rate), |