gui: the scrubber and speed slider are one slider, dragged through gpui's drag
render_scrubber and render_speed_slider drew the same groove, fill and ringed thumb and did the same ratio-from-cursor math (seek_to_cursor, drag_speed_slider) (audit M1). They are one slider() with its proportions in a SliderLook, ratio_at, and an on_slide that sets the value on a press and on every move of a drag begun there. The drag now goes through gpui's on_drag/on_drag_move, which follows the pointer window-wide, so a scrub or a speed drag no longer stops dead when the pointer leaves the 24px strip (B6). The speed thumb sits half a pixel higher, now centered on its groove. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
92dac4bb67
commit
7b3c0e6475
1 file changed
+163
-156
@@ -39,9 +39,10 @@ use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use gpui::{
|
||||
AnyElement, Context, Entity, EventEmitter, FontWeight, ImageSource, IntoElement, MouseButton,
|
||||
MouseDownEvent, MouseMoveEvent, ObjectFit, Pixels, Render, RenderImage, ScrollHandle,
|
||||
SharedString, Window, div, img, prelude::*, px, relative, svg,
|
||||
AnyElement, Context, Div, DragMoveEvent, EmptyView, Entity, EventEmitter, FontWeight,
|
||||
ImageSource, IntoElement, MouseButton, MouseDownEvent, ObjectFit, Pixels, Rems, Render,
|
||||
RenderImage, Rgba, ScrollHandle, SharedString, Stateful, Window, div, img, prelude::*, px,
|
||||
relative, svg,
|
||||
};
|
||||
use stream_download::storage::temp::TempStorageProvider;
|
||||
use stream_download::{Settings, StreamDownload};
|
||||
@@ -857,17 +858,6 @@ impl PlayerControls {
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Drag handler for the slider: maps a cursor x to a value in
|
||||
/// `[MIN_SPEED, MAX_SPEED]`, rounds it to [`SPEED_STEP`], and applies it live.
|
||||
fn drag_speed_slider(&mut self, x: Pixels, cx: &mut Context<Self>) {
|
||||
let bounds = self.speed_slider.bounds();
|
||||
if bounds.size.width <= px(0.) {
|
||||
return;
|
||||
}
|
||||
let ratio = ((x - bounds.origin.x) / bounds.size.width).clamp(0., 1.);
|
||||
self.set_speed_dialog_value(MIN_SPEED + ratio * (MAX_SPEED - MIN_SPEED), cx);
|
||||
}
|
||||
|
||||
/// The +/- steppers: nudge the dialog's value by one [`SPEED_STEP`],
|
||||
/// clamped to `[MIN_SPEED, MAX_SPEED]`, and apply it live.
|
||||
fn nudge_speed_dialog(&mut self, steps: i32, cx: &mut Context<Self>) {
|
||||
@@ -940,17 +930,6 @@ impl PlayerControls {
|
||||
self.seek_secs(self.position_secs + delta, cx);
|
||||
}
|
||||
|
||||
/// Map a cursor x position over the scrubber to a seek ratio, using the bar's
|
||||
/// last laid-out bounds.
|
||||
fn seek_to_cursor(&mut self, x: gpui::Pixels, cx: &mut Context<Self>) {
|
||||
let bounds = self.scrubber.bounds();
|
||||
if bounds.size.width <= px(0.) {
|
||||
return;
|
||||
}
|
||||
let ratio = ((x - bounds.origin.x) / bounds.size.width).clamp(0., 1.);
|
||||
self.seek(ratio, cx);
|
||||
}
|
||||
|
||||
/// Keep `position_secs` in step with playback, for as long as something is
|
||||
/// playing — the ticker starts with playback and stops at the first tick
|
||||
/// that finds it paused or stopped, so an idle bar wakes nobody. When real
|
||||
@@ -1338,81 +1317,20 @@ impl PlayerControls {
|
||||
cx: &mut Context<Self>,
|
||||
) -> impl IntoElement + use<> {
|
||||
let seekable = enabled && self.duration_secs() > 0.;
|
||||
let fill = ratio.clamp(0., 1.);
|
||||
|
||||
// The strip is much taller than the line drawn in it — the hit area is
|
||||
// the control, and a 4px groove is a 4px target. The groove and thumb
|
||||
// are positioned from the strip's own height so widening the target
|
||||
// later can't leave the line off-center inside it.
|
||||
let strip = scaled(SCRUBBER_HIT_HEIGHT);
|
||||
let groove_top = (strip - control::TRACK_HEIGHT) / 2.;
|
||||
let thumb_top = (strip - scaled(SCRUBBER_THUMB_SIZE)) / 2.;
|
||||
|
||||
let mut bar = div()
|
||||
.id("player-scrubber")
|
||||
.track_scroll(&self.scrubber)
|
||||
.flex_1()
|
||||
.relative()
|
||||
.h(scaled(SCRUBBER_HIT_HEIGHT))
|
||||
// Track groove (centered vertically).
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.top(groove_top)
|
||||
.left_0()
|
||||
.w_full()
|
||||
.h(control::TRACK_HEIGHT)
|
||||
.rounded(shape::PILL)
|
||||
.bg(theme.border_default),
|
||||
)
|
||||
// Filled portion up to the current position.
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.top(groove_top)
|
||||
.left_0()
|
||||
.w(relative(fill))
|
||||
.h(control::TRACK_HEIGHT)
|
||||
.rounded(shape::PILL)
|
||||
.bg(theme.accent),
|
||||
)
|
||||
// Thumb: a full-width layer positions its left edge at the fraction,
|
||||
// then a negative margin pulls it left by its radius to center it.
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.top(thumb_top)
|
||||
.left_0()
|
||||
.w_full()
|
||||
.h(scaled(SCRUBBER_THUMB_SIZE))
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.left(relative(fill))
|
||||
.ml(scaled(-SCRUBBER_THUMB_SIZE / 2.))
|
||||
.size(scaled(SCRUBBER_THUMB_SIZE))
|
||||
.rounded(shape::PILL)
|
||||
.bg(theme.accent)
|
||||
.border_2()
|
||||
.border_color(theme.bg_sidebar),
|
||||
),
|
||||
);
|
||||
if seekable {
|
||||
bar = bar
|
||||
.cursor_pointer()
|
||||
.on_mouse_down(
|
||||
MouseButton::Left,
|
||||
cx.listener(|this, ev: &MouseDownEvent, _, cx| {
|
||||
this.seek_to_cursor(ev.position.x, cx);
|
||||
}),
|
||||
)
|
||||
.on_mouse_move(cx.listener(|this, ev: &MouseMoveEvent, _, cx| {
|
||||
if ev.dragging() {
|
||||
this.seek_to_cursor(ev.position.x, cx);
|
||||
}
|
||||
}));
|
||||
}
|
||||
bar
|
||||
// the control, and a 4px groove is a 4px target.
|
||||
let look = SliderLook {
|
||||
strip: scaled(SCRUBBER_HIT_HEIGHT),
|
||||
thumb: scaled(SCRUBBER_THUMB_SIZE),
|
||||
ring: theme.bg_sidebar,
|
||||
};
|
||||
slider(theme, SliderDrag::Scrubber, &self.scrubber, ratio, look).flex_1().when(
|
||||
seekable,
|
||||
|bar| {
|
||||
bar.cursor_pointer()
|
||||
.on_slide(SliderDrag::Scrubber, cx, |this, ratio, cx| this.seek(ratio, cx))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// A pill showing the current rate that opens a menu of preset speeds, this
|
||||
@@ -1554,63 +1472,29 @@ impl PlayerControls {
|
||||
cx.listener(|this, _, _, cx| this.nudge_speed_dialog(-1, cx)),
|
||||
))
|
||||
.child(
|
||||
div()
|
||||
.id("speed-dialog-slider")
|
||||
.track_scroll(&self.speed_slider)
|
||||
.cursor_pointer()
|
||||
.flex_1()
|
||||
.relative()
|
||||
.h(scaled(20.))
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.top(scaled(8.))
|
||||
.left_0()
|
||||
.w_full()
|
||||
.h(control::TRACK_HEIGHT)
|
||||
.rounded(shape::PILL)
|
||||
.bg(theme.border_default),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.top(scaled(8.))
|
||||
.left_0()
|
||||
.w(relative(ratio))
|
||||
.h(control::TRACK_HEIGHT)
|
||||
.rounded(shape::PILL)
|
||||
.bg(theme.accent),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.top(scaled(3.))
|
||||
.left_0()
|
||||
.w_full()
|
||||
.h(scaled(16.))
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.left(relative(ratio))
|
||||
.ml(-(icon::SM / 2.))
|
||||
.size(icon::SM)
|
||||
.rounded(shape::PILL)
|
||||
.bg(theme.accent)
|
||||
.border_2()
|
||||
.border_color(theme.bg_window),
|
||||
),
|
||||
)
|
||||
.on_mouse_down(
|
||||
MouseButton::Left,
|
||||
cx.listener(|this, ev: &MouseDownEvent, _, cx| {
|
||||
this.drag_speed_slider(ev.position.x, cx);
|
||||
}),
|
||||
)
|
||||
.on_mouse_move(cx.listener(|this, ev: &MouseMoveEvent, _, cx| {
|
||||
if ev.dragging() {
|
||||
this.drag_speed_slider(ev.position.x, cx);
|
||||
}
|
||||
})),
|
||||
slider(
|
||||
theme,
|
||||
SliderDrag::Speed,
|
||||
&self.speed_slider,
|
||||
ratio,
|
||||
SliderLook {
|
||||
strip: scaled(20.),
|
||||
thumb: icon::SM,
|
||||
ring: theme.bg_window,
|
||||
},
|
||||
)
|
||||
.flex_1()
|
||||
.cursor_pointer()
|
||||
.on_slide(
|
||||
SliderDrag::Speed,
|
||||
cx,
|
||||
|this, ratio, cx| {
|
||||
this.set_speed_dialog_value(
|
||||
MIN_SPEED + ratio * (MAX_SPEED - MIN_SPEED),
|
||||
cx,
|
||||
)
|
||||
},
|
||||
),
|
||||
)
|
||||
.child(speed_step_button(
|
||||
theme,
|
||||
@@ -1709,6 +1593,129 @@ fn format_speed(speed: f32) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// Which of the bar's two sliders a drag began on. gpui hands every move of a
|
||||
/// drag to every element listening for its type, so each slider checks the
|
||||
/// value is its own.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum SliderDrag {
|
||||
Scrubber,
|
||||
Speed,
|
||||
}
|
||||
|
||||
impl SliderDrag {
|
||||
fn id(self) -> &'static str {
|
||||
match self {
|
||||
SliderDrag::Scrubber => "player-scrubber",
|
||||
SliderDrag::Speed => "speed-dialog-slider",
|
||||
}
|
||||
}
|
||||
|
||||
/// The handle recording this slider's laid-out bounds, for [`ratio_at`].
|
||||
fn handle(self, player: &PlayerControls) -> &ScrollHandle {
|
||||
match self {
|
||||
SliderDrag::Scrubber => &player.scrubber,
|
||||
SliderDrag::Speed => &player.speed_slider,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A slider's proportions: the hit strip's height, the thumb's diameter, and
|
||||
/// the ring its thumb is cut out of the background with.
|
||||
struct SliderLook {
|
||||
strip: Rems,
|
||||
thumb: Rems,
|
||||
ring: Rgba,
|
||||
}
|
||||
|
||||
/// A horizontal slider: a groove, the fill up to `ratio`, and a round thumb
|
||||
/// centered on the fill's end, all positioned from the strip's own height so
|
||||
/// widening the target can't leave the line off-center inside it. Inert until
|
||||
/// the caller adds [`OnSlide::on_slide`].
|
||||
fn slider(
|
||||
theme: &Theme,
|
||||
which: SliderDrag,
|
||||
handle: &ScrollHandle,
|
||||
ratio: f32,
|
||||
look: SliderLook,
|
||||
) -> Stateful<Div> {
|
||||
let ratio = ratio.clamp(0., 1.);
|
||||
let groove_top = (look.strip - control::TRACK_HEIGHT) / 2.;
|
||||
let groove =
|
||||
|| div().absolute().top(groove_top).left_0().h(control::TRACK_HEIGHT).rounded(shape::PILL);
|
||||
div()
|
||||
.id(which.id())
|
||||
.track_scroll(handle)
|
||||
.relative()
|
||||
.h(look.strip)
|
||||
.child(groove().w_full().bg(theme.border_default))
|
||||
.child(groove().w(relative(ratio)).bg(theme.accent))
|
||||
// A full-width layer puts the thumb's left edge at the fraction, then a
|
||||
// negative margin pulls it left by its radius to center it.
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.top((look.strip - look.thumb) / 2.)
|
||||
.left_0()
|
||||
.w_full()
|
||||
.h(look.thumb)
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.left(relative(ratio))
|
||||
.ml(-(look.thumb / 2.))
|
||||
.size(look.thumb)
|
||||
.rounded(shape::PILL)
|
||||
.bg(theme.accent)
|
||||
.border_2()
|
||||
.border_color(look.ring),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Where along the slider `x` falls, 0..=1, from its last laid-out bounds.
|
||||
fn ratio_at(handle: &ScrollHandle, x: Pixels) -> Option<f32> {
|
||||
let bounds = handle.bounds();
|
||||
(bounds.size.width > px(0.)).then(|| ((x - bounds.origin.x) / bounds.size.width).clamp(0., 1.))
|
||||
}
|
||||
|
||||
trait OnSlide {
|
||||
/// Call `set` with the slider's ratio under the pointer on a press, and on
|
||||
/// every move of a drag that began here — wherever the pointer has gone, so
|
||||
/// a drag doesn't stop at the strip's edge.
|
||||
fn on_slide(
|
||||
self,
|
||||
which: SliderDrag,
|
||||
cx: &mut Context<PlayerControls>,
|
||||
set: fn(&mut PlayerControls, f32, &mut Context<PlayerControls>),
|
||||
) -> Self;
|
||||
}
|
||||
|
||||
impl OnSlide for Stateful<Div> {
|
||||
fn on_slide(
|
||||
self,
|
||||
which: SliderDrag,
|
||||
cx: &mut Context<PlayerControls>,
|
||||
set: fn(&mut PlayerControls, f32, &mut Context<PlayerControls>),
|
||||
) -> Self {
|
||||
self.on_mouse_down(
|
||||
MouseButton::Left,
|
||||
cx.listener(move |this, ev: &MouseDownEvent, _, cx| {
|
||||
if let Some(ratio) = ratio_at(which.handle(this), ev.position.x) {
|
||||
set(this, ratio, cx);
|
||||
}
|
||||
}),
|
||||
)
|
||||
.on_drag(which, |_, _, _, cx| cx.new(|_| EmptyView))
|
||||
.on_drag_move(cx.listener(move |this, ev: &DragMoveEvent<SliderDrag>, _, cx| {
|
||||
if *ev.drag(cx) == which
|
||||
&& let Some(ratio) = ratio_at(which.handle(this), ev.event.position.x)
|
||||
{
|
||||
set(this, ratio, cx);
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
/// The square cover thumbnail, with a neutral fallback glyph. Always drawn at
|
||||
/// the same size whether or not the track has art, so the player's regions
|
||||
/// don't shift sideways between a track with a cover and one without.
|
||||
|
||||
Reference in new issue
Block a user