MediaControls: trim ends across a seek track that fills the bar

The trim ends were Slider marks: small ticks under the track that the seek snapped to. Slider::trim draws the ends of a kept span as lines across the track, a thumb high, in the warning colour, without snapping; Slider::fill_width lets a track take the rest of its row. The media controls use both, as the old did.
This commit is contained in:
aiongg
2026-11-12 14:32:48 +00:00
parent 56857cb223
commit e54fed5fa3
4 changed files with 64 additions and 8 deletions
+8 -5
View File
@@ -333,9 +333,9 @@ impl Outcome for MediaControlsOutput {
}
/// PowerPoint's media controls under a video or sound on a slide: play or pause, back and forward a quarter second, a
/// seek track with the trim marks, and the time (`1:05.25` style in the mono role, digits through the units). The
/// controls are a named toolbar `Bar`, so a press on them never reaches the canvas under them; each button is its own
/// Tab stop, in reading order. The seek track is a `Slider` (one Tab stop; Left and Right a quarter second, Home and
/// seek track along the rest of the bar with the trim ends across it, and the time (`1:05.25` style in the mono
/// role, digits through the units). The controls are a named toolbar `Bar`, so a press on them never reaches the
/// canvas under them; each button is its own Tab stop, in reading order. The seek track is a `Slider` (one Tab stop; Left and Right a quarter second, Home and
/// End) reporting an `EditEvent<f64>` gesture in seconds.
#[derive(Pattern, Props)]
pub struct MediaControls {
@@ -375,8 +375,11 @@ impl MediaControls {
nudge = Some(NUDGE);
}
let range = if playable { 0.0..=duration } else { 0.0..=1.0 };
let marks: Vec<f64> = trim.filter(|_| playable).map(|(start, end)| [start, end]).into_iter().flatten().collect();
let edit = Slider::new(Bind::given(position), range).step(NUDGE).marks(&marks).enabled(playable).label(words.seek.clone()).show(ui);
let mut slider = Slider::new(Bind::given(position), range).step(NUDGE).fill_width(true).enabled(playable).label(words.seek.clone());
if let Some((start, end)) = trim.filter(|_| playable) {
slider = slider.trim(start, end);
}
let edit = slider.show(ui);
seek = edit.events;
Text::new(clock(&units, position)).role(TypeRole::Mono).show(ui);
});
+26 -1
View File
@@ -11,7 +11,7 @@ use crate::contract::{
};
use crate::runtime::env::Label;
use crate::runtime::focus::{ActivateKeys, KeepKeys, KeyUse};
use crate::style::{ComponentStyle, FocusLook, StyleCx, SurfaceStyle, TextLook, TrackLook};
use crate::style::{ComponentStyle, FocusLook, StyleColor, StyleCx, SurfaceStyle, TextLook, TrackLook};
use craft_behavior::{Flow, HitLevel, HitPriority, Metrics, PartKey, PressPhase, Proposal, Sense};
use craft_theme::Stateful;
use egui::{Pos2, Rect, Vec2};
@@ -46,6 +46,8 @@ pub struct SliderStyle {
thumb: Vec2,
/// The length a slider asks for along its axis.
length: f32,
/// The lines at the ends of a trimmed span.
trim: StyleColor,
}
/// The running drag: where the thumb was along the track when it began, and what the press moves.
@@ -93,6 +95,11 @@ pub struct Slider<'a> {
/// drawn over it.
#[prop]
track_face: Option<TrackFace<'a>>,
/// The kept part of the range, its ends drawn across the track ([`Slider::trim`]).
trim: Option<(f64, f64)>,
/// Takes the width its row offers past its length: a seek track along the rest of a bar.
#[prop]
fill_width: bool,
}
/// The shape of a slider's thumb.
@@ -125,6 +132,8 @@ impl<'a> Slider<'a> {
wheel_step: None,
value_text: None,
track_face: None,
trim: None,
fill_width: false,
}
}
@@ -142,6 +151,14 @@ impl<'a> Slider<'a> {
self
}
/// The kept part of the range, from `start` to `end`: a trimmed clip's In and Out on a seek track. Each end is a
/// line across the track, a thumb high, in the warning colour; the thumb does not snap to them. An end outside
/// the range is not drawn.
pub fn trim(mut self, start: f64, end: f64) -> Self {
self.trim = Some((start, end));
self
}
fn track_scale(&self) -> Scale {
Scale::new(&self.range, self.step, self.scale)
}
@@ -173,9 +190,14 @@ impl Component for Slider<'_> {
ring: ThumbRing::new(cx),
thumb: track::knob_size(cx),
length,
trim: cx.color(cx.tokens.color.status.warn.fg),
}
}
fn fills(&self) -> Option<craft_behavior::Axis> {
self.fill_width.then_some(craft_behavior::Axis::X)
}
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
self.marks.lay_out(&cx.style.mark_text, cx.style.space, |label| cx.env.resolve(label), |run| cx.layout(run).size());
let along = if cx.proposal == Proposal::Min { 0.0 } else { cx.style.length };
@@ -316,6 +338,9 @@ impl Component for Slider<'_> {
}
let ink = MarkInk { marks: cx.resolve(&style.track.mark_color), labels: cx.resolve(&style.mark_text.color), role: style.mark_text.role };
track::paint_marks(&draw, &style.track, &self.marks, &scale, along, cx.rect, &ink);
if let Some(span) = self.trim {
track::paint_trim(&draw, span, &scale, along, style.thumb.y, style.trim);
}
// A press anywhere on the slider moves the thumb, so hovering anywhere on it lights the thumb.
let running = cx.gesture().and_then(|_| cx.recall::<Held>().press);
let hovered = [Self::THUMB, TRACK].into_iter().find_map(|part| cx.hover_pos(part)).and_then(|at| along.pick(false).press(&[centre.x], at.x));
+10
View File
@@ -255,6 +255,16 @@ pub(crate) fn paint_marks(draw: &Draw<'_>, look: &TrackLook, marks: &Marks, scal
}
}
/// The two ends of a trimmed span (`start`, `end`), each a line `height` high across the middle of the track in
/// `color`. An end outside the scale's range is not drawn.
pub(crate) fn paint_trim(draw: &Draw<'_>, (start, end): (f64, f64), scale: &Scale, along: Along, height: f32, color: StyleColor) {
let middle = along.area.center().y;
for value in [start, end].into_iter().filter(|value| (scale.min..=scale.max).contains(value)) {
let x = along.x(scale.position(value));
draw.stroke(&PaintShape::Path(vec![Pos2::new(x, middle - height / 2.0), Pos2::new(x, middle + height / 2.0)]), StrokeWidth::Thin, color);
}
}
/// The part of the groove painted in `color`, from `from` to `to` along the reading direction, measured from the start edge.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Fill {
+20 -2
View File
@@ -24,12 +24,13 @@ struct Slide {
range: std::ops::RangeInclusive<f64>,
step: Option<f64>,
wheel_step: Option<f64>,
trim: Option<(f64, f64)>,
events: RefCell<Vec<EditEvent<f64>>>,
}
impl Slide {
fn new(value: f64) -> Self {
Slide { value: RefCell::new(value), range: 0.0..=100.0, step: None, wheel_step: None, events: RefCell::default() }
Slide { value: RefCell::new(value), range: 0.0..=100.0, step: None, wheel_step: None, trim: None, events: RefCell::default() }
}
fn show(&self, ui: &mut Ui) {
@@ -41,6 +42,9 @@ impl Slide {
if let Some(step) = self.wheel_step {
slider = slider.wheel_step(step);
}
if let Some((start, end)) = self.trim {
slider = slider.trim(start, end);
}
self.events.borrow_mut().extend(slider.show_at(ui, FIELD).events);
}
@@ -65,7 +69,11 @@ fn drag(frames: &mut Frames, from: Pos2, travel: f32, show: &mut impl FnMut(&mut
/// The value after grabbing the slider's thumb `offset` from its centre and dragging `travel`.
fn dragged_from(offset: f32, travel: f32) -> f64 {
let slide = Slide::new(START);
dragged(&Slide::new(START), offset, travel)
}
/// `slide`'s value after grabbing its thumb `offset` from its centre and dragging `travel`.
fn dragged(slide: &Slide, offset: f32, travel: f32) -> f64 {
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| slide.show(ui);
frames.idle(3, &mut show);
@@ -90,6 +98,16 @@ fn a_grabbed_slider_thumb_moves_by_the_pointer_travel_and_a_click_on_it_changes_
assert_eq!(slide.value(), START, "a click on the thumb leaves the value");
}
/// A trimmed span's ends are drawn across the track, not stops: a drag that ends beside one lands where the drag
/// puts it.
#[trial]
fn a_drag_that_ends_beside_a_trim_end_does_not_snap_to_it() {
let free = dragged_from(0.0, 10.0);
let trimmed = Slide { trim: Some((0.0, free + 0.5)), ..Slide::new(START) };
let landed = dragged(&trimmed, 0.0, 10.0);
assert!((landed - free).abs() < 1e-3, "the drag landed at {landed}, as without the trim ({free}), not on its end");
}
/// A leaf with parts shown at a rect that is empty, not a number, infinite, over the extent cap, or one point wide
/// does not panic and answers presses and keys with nothing: its value stays as it was. It runs on a slider, because
/// only shared code can break it.