From 8d8d8808a2dd2437eed1ab3601a66369948e2ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 10 Aug 2026 15:33:23 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(colour):=20sliders=20are=20dra?= =?UTF-8?q?gged=20and=20the=20graded=20frame=20is=20drawn=20over=20them?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The colour card loses its stepper buttons: each row's bar is now pressed and pulled straight to a value, which is what a slider is for. The press takes the undo step and every sample after it is a live write (`set_color_live`), so a whole drag is one `z` rather than one per pixel -- and because a live write reseeks like any other edit, the picture regrades under the hand. Above the rows, an RGB histogram of the frame on screen: counted off the BGRA the decoder handed over, which is already through the grade, so pulling brightness tilts the graph as it moves. Every 253rd pixel of a 1080p frame (HIST_SAMPLES), 64 bins a channel, square-rooted so a flat black area does not draw the rest as a line. The arrow keys still move the same values, on the same 0.05 grid a drag snaps to, and the bar is 4 px to look at but a whole row to hit. --- crates/app/src/main.rs | 429 +++++++++++++++++++++++++++++----- crates/engine/src/playback.rs | 8 + crates/engine/src/project.rs | 58 ++++- 3 files changed, 440 insertions(+), 55 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 59a5b02..773097e 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -296,16 +296,37 @@ const COLOR_BANDS: [(&str, f32, f32); 4] = [ ("Tint (cool–warm)", -1., 1.), ]; -/// A press of a nudge key or button: a fortieth of a band's range, so a slider -/// crosses it in forty presses and every stop is a number the file can write. +/// A press of a nudge key: a fortieth of a band's range, so a slider crosses it +/// in forty presses and every stop is a number the file can write. A drag lands +/// on the same grid ([`Player::drag_color`]), so the pointer and the keyboard +/// cannot reach two different sets of values. const COLOR_STEP: f32 = 0.05; -/// The card's width: a slider row is a label, two buttons, the bar and the -/// value, and the longest label has to fit beside all four without truncating -/// (measured against "Tint (cool–warm)" at 460). +/// The card's width: a slider row is a label, the bar and the value, and the +/// longest label has to fit beside all three without truncating (measured +/// against "Tint (cool–warm)"). const COLOR_W: f32 = 460.; -/// How much of a slider row the bar itself gets. -const COLOR_BAR_W: f32 = 140.; +/// How much of a slider row the bar itself gets -- what a drag is read against, +/// so it takes the width the two nudge buttons used to. +const COLOR_BAR_W: f32 = 240.; + +/// The histogram's bins per channel. 64 is four codes of an 8-bit ramp per bin: +/// fine enough to see a grade tilt, coarse enough that a subsampled count is +/// not noise. +const HIST_BINS: usize = 64; + +/// How many pixels of a frame the histogram reads. The stride is +/// `pixels / HIST_SAMPLES` (1920x1080 -> every 253rd pixel), which is a +/// thousandth of the frame and walks across columns rather than down one. +const HIST_SAMPLES: usize = 8_192; + +/// The histogram box. Shorter than the equalizer's curve because four slider +/// rows stand under it and the card still has to fit a 360 px window. +const HIST_H: f32 = 96.; + +/// What each channel's line is drawn in, in `[r, g, b]` order -- the channel it +/// counts, lightened enough to read on the dark box. +const HIST_INK: [u32; 3] = [0xE0_5A_5A, 0x5A_D0_7A, 0x5A_9A_E0]; struct Player { /// The timeline, once there is one. A run with no file opens without it and @@ -426,9 +447,24 @@ struct Player { /// there. `None` when it is closed, which is the only place that state /// lives: the grade itself is the project's. color_open: Option<(Lane, usize)>, - /// Which of the card's four sliders the arrow keys and the buttons move. - /// The card's own focus, since nothing in it takes gpui's (ledger:182). + /// Which of the card's four sliders the arrow keys and a drag move. The + /// card's own focus, since nothing in it takes gpui's (ledger:182). color_band: usize, + /// A slider is being dragged. Tracked on the root like `scrubbing` and the + /// equalizer's drag, for the same reason: a 4 px bar is left by the pointer + /// on the first move and its own listeners then stop firing. + color_dragging: bool, + /// Each slider's box, recorded at prepaint: a mouse listener is handed the + /// window position only, so this is what a press and a drag are read against + /// ([`frac_along`]). One per band, because the press picks the row it landed + /// on and the drag then belongs to that row's range. + color_bars: [Rc>>; COLOR_BANDS.len()], + /// The frame on screen counted into `HIST_BINS` bins per channel -- the + /// *graded* frame, because the grade is applied in the decode worker and + /// what arrives here is already through it. Refilled by every pumped frame, + /// which is what makes the colour card's graph move as a slider is dragged: + /// each live write reseeks, and the reseek's frame is the next count. + histogram: [[u32; HIST_BINS]; 3], /// The action whose row is waiting for a stroke. The next key that is /// neither escape nor a lone modifier becomes the whole of what reaches it. rebinding: Option, @@ -495,6 +531,12 @@ impl Player { }); let buf = image::RgbaImage::from_raw(frame.width, frame.height, frame.bgra) .expect("frame buffer sized width*height*4"); + // Counted here rather than under `color_open`, because the card + // opens on a frame that was pumped before it: gating this on the + // card would leave its graph flat until something reseeked. A + // thousandth of the pixels, against a conversion that just touched + // all of them. + self.histogram = histogram(buf.as_raw()); let next = Arc::new(RenderImage::new(vec![image::Frame::new(buf)])); if let Some(old) = self.image.replace(next) { // Every RenderImage gets a fresh id and its own atlas tile: @@ -718,6 +760,7 @@ impl Player { Some(clip) => { self.color_open = Some(clip); self.color_band = 0; + self.color_dragging = false; // One card at a time, the rule both the others already follow. self.keys_open = false; self.export_open = false; @@ -744,6 +787,14 @@ impl Player { /// frame on screen repaints through the new grade; this only owes the flags /// that reseek clears. fn set_color(&mut self, params: ColorParams, cx: &mut Context) { + self.write_color(params, false, cx); + } + + /// Both writes: `live` is the one that takes no undo step, which is what + /// every sample *inside* a drag goes through + /// (`PlaybackSession::set_color_live`). Either way the engine reseeks, so + /// the picture -- and the histogram counted off it -- is regraded at once. + fn write_color(&mut self, params: ColorParams, live: bool, cx: &mut Context) { let Some((lane, idx)) = self.color_open else { return; }; @@ -751,7 +802,11 @@ impl Player { return; }; let grade = Some(params).filter(|p| !p.is_identity()); - if session.set_color(lane, idx, grade) { + let took = match live { + true => session.set_color_live(lane, idx, grade), + false => session.set_color(lane, idx, grade), + }; + if took { self.reset_after_reseek(); } cx.notify(); @@ -767,6 +822,35 @@ impl Player { self.set_color(params, cx); } + /// Where the pointer sits along a slider, as that band's value: the left end + /// of the bar is the bottom of its range and the right end the top. Called + /// on every pointer sample, so the grade -- and the picture, and the + /// histogram over it -- moves under the hand. + /// + /// `first` is the press: it takes the undo step the whole gesture rolls back + /// to, and every sample after it is live. That is why it writes even when + /// the value did not change -- without that snapshot the rest of the drag + /// would be unundoable. + /// + /// Values land on the [`COLOR_STEP`] grid the keys use, which also bounds + /// one drag to forty-odd entries in the project's colour table. + /// + /// ponytail: one reseek per step crossed, up to ~40 for a bar-wide sweep. If + /// that ever stutters, the throttle is [`scrub_due`]'s, which the ruler drag + /// already uses for the same cost. + fn drag_color(&mut self, x: Pixels, first: bool, cx: &mut Context) { + let (_, low, high) = COLOR_BANDS[self.color_band]; + let along = frac_along(x, self.color_bars[self.color_band].get()); + let value = color_snap(low + along * (high - low)).clamp(low, high); + let mut params = self.color_params(); + let at = band_mut(&mut params, self.color_band); + if *at == value && !first { + return; + } + *at = value; + self.write_color(params, !first, cx); + } + /// Whether a card owns the window. While one does the timeline under it is /// out of reach, so a right-click there opens no menu -- the same rule the /// key handler and the drop target already follow. @@ -1831,7 +1915,10 @@ impl Render for Player { // the keys menu still lists them. if this.color_open.is_some() { match color_key(key) { - Some(ColorKey::Close) => this.color_open = None, + Some(ColorKey::Close) => { + this.color_open = None; + this.color_dragging = false; + } Some(ColorKey::Band(step)) => { this.color_band = (this.color_band + step) % COLOR_BANDS.len(); } @@ -1899,6 +1986,17 @@ impl Render for Player { } return; } + // A colour slider is 4 px tall and the pointer leaves it just as + // fast; every sample is live, so the release owes no write of + // its own -- what the last sample set is what the clip carries. + if this.color_dragging { + if event.pressed_button == Some(MouseButton::Left) { + this.drag_color(event.position.x, false, cx); + } else { + this.color_dragging = false; + } + return; + } if !this.scrubbing { return; } @@ -1921,6 +2019,13 @@ impl Render for Player { this.commit_eq(cx); return; } + if std::mem::take(&mut this.color_dragging) { + // The release lands exactly where the hand let go, and + // it is a live write like every other sample: the undo + // step the gesture rolls back to was the press's. + this.drag_color(event.position.x, false, cx); + return; + } if std::mem::take(&mut this.scrubbing) { this.scrub_to(event.position.x, true, cx); } @@ -3076,14 +3181,17 @@ impl Player { ) } - /// The colour card: a row per control, each showing where in its range it - /// stands and carrying the pair of buttons that move it -- the same move the - /// arrow keys make, because a control only the pointer can reach is not one - /// everyone can reach. Same scrim, surface and row shape as the other two - /// cards, and the same plain divs, so the root keeps the keyboard. + /// The colour card: the graded frame's histogram over a row per control, + /// each row a bar the pointer drags straight to a value -- no stepper + /// buttons, because a slider is a thing to pull, and the arrow keys still + /// move the same value for anyone not using a pointer. Same scrim, surface + /// and row shape as the other two cards, and the same plain divs, so the + /// root keeps the keyboard. /// /// The values are read from the project every render: what is drawn is what - /// the decoder is grading with, never a copy that could drift from it. + /// the decoder is grading with, never a copy that could drift from it. The + /// graph above them is counted off the frame that came *back* through that + /// grade ([`histogram`]), so pulling exposure tilts it while the hand moves. fn color_card(&self, cx: &mut Context) -> Option { let (lane, idx) = self.color_open?; let params = self.color_params(); @@ -3095,26 +3203,6 @@ impl Player { let value = *band_mut(&mut read, i); let frac = ((value - low) / (high - low)).clamp(0., 1.); let picked = i == self.color_band; - // Both buttons of a row do what the arrows do, so there is one - // rule for what a press is worth and one place it is clamped. - let step = |name: &'static str, steps: f32| { - div() - .id((name, i)) - .w(px(CONTROL_H)) - .h(px(CONTROL_H)) - .flex() - .items_center() - .justify_center() - .rounded(px(3.)) - .bg(rgb(CHROME)) - .cursor_pointer() - .hover(|s| s.bg(rgb(HOVER))) - .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { - this.color_band = i; - this.nudge_color(steps, cx); - })) - .child(if steps < 0. { "–" } else { "+" }) - }; div() .id(("color-row", i)) .flex() @@ -3131,22 +3219,43 @@ impl Player { cx.notify(); })) .child(div().flex_1().min_w(px(0.)).truncate().child(label)) - .child(step("color-down", -1.)) .child( + // The bar is 4 px to look at and a whole row to hit + // (WCAG 2.5.8), the same split the ruler makes between + // what is drawn and what is grabbed. The press is + // already the first sample of the drag, so a plain click + // sets the value it landed on. div() + .id(("color-bar", i)) + .relative() .w(px(COLOR_BAR_W)) - .h(px(4.)) - .rounded(px(2.)) - .bg(rgb(CHROME)) + .h(px(KEYS_ROW_H)) + .flex() + .items_center() + .on_mouse_down( + MouseButton::Left, + cx.listener(move |this, event: &MouseDownEvent, _, cx| { + this.color_band = i; + this.color_dragging = true; + this.drag_color(event.position.x, true, cx); + }), + ) + .child(bounds_probe(self.color_bars[i].clone())) .child( div() - .h_full() - .w(relative(frac)) + .w_full() + .h(px(4.)) .rounded(px(2.)) - .bg(rgb(ACCENT)), + .bg(rgb(CHROME)) + .child( + div() + .h_full() + .w(relative(frac)) + .rounded(px(2.)) + .bg(rgb(ACCENT)), + ), ), ) - .child(step("color-up", 1.)) .child( div() .w(px(44.)) @@ -3187,7 +3296,22 @@ impl Player { .px(px(6.)) .text_size(px(11.)) .text_color(rgb(INK_DIM)) - .child("↑↓ picks a slider, ←→ moves it, r resets — esc closes"), + .child( + "drag a bar, or ↑↓ picks one and ←→ moves it, r resets — esc closes", + ), + ) + // The frame as it is being graded, over the controls + // grading it: the three lines are what the picture is + // made of, and every sample of a drag reseeks, so they + // move with the bar under the hand. + .child( + div() + .flex_none() + .h(px(HIST_H)) + .rounded(px(3.)) + .bg(rgb(HOVER_DIM)) + .relative() + .child(hist_curves(self.histogram)), ) .children(rows) .child( @@ -4683,6 +4807,73 @@ fn frac_down(y: Pixels, bounds: Bounds) -> f32 { ((y - bounds.top()) / bounds.size.height).clamp(0., 1.) } +/// A slider value on the [`COLOR_STEP`] grid: what a drag rounds to, so the +/// pointer stops where the arrow keys do and "0.35" on screen is the number the +/// file writes rather than a rounding of one. +fn color_snap(value: f32) -> f32 { + (value / COLOR_STEP).round() * COLOR_STEP +} + +/// How the frame on screen is spread across the tone range: `HIST_BINS` counts +/// per channel, read off the BGRA the decoder handed over -- which is the +/// *graded* picture, because the grade is folded into the conversion +/// (`engine::convert::i420_to_bgra_with`). So what this counts is what the eye +/// is looking at, and moving a slider moves it. +/// +/// Every [`HIST_SAMPLES`]th-of-a-frame pixel, not every pixel: a shape drawn +/// from eight thousand samples is the same shape, at a thousandth of the reads. +fn histogram(bgra: &[u8]) -> [[u32; HIST_BINS]; 3] { + let pixels = bgra.len() / 4; + let stride = (pixels / HIST_SAMPLES).max(1); + let mut bins = [[0u32; HIST_BINS]; 3]; + for p in (0..pixels).step_by(stride) { + let px = &bgra[p * 4..]; + // BGRA on the wire, `[r, g, b]` in the bins: the graph names channels + // the way a person does. + for (channel, value) in [px[2], px[1], px[0]].into_iter().enumerate() { + bins[channel][usize::from(value) * HIST_BINS / 256] += 1; + } + } + bins +} + +/// The three counts drawn as three lines across the box, tallest bin to the top. +/// +/// Square root, not linear: a shot with a big flat area (a night sky, a title +/// card) puts one bin so far above the rest that a linear graph is a single +/// spike beside a flat line, and the tilt a grade puts in the rest is exactly +/// what the card is for. +fn hist_curves(bins: [[u32; HIST_BINS]; 3]) -> impl IntoElement { + canvas( + |_, _, _| (), + move |bounds, _, window, _| { + let (o, s) = (bounds.origin, bounds.size); + // Shared across the channels, so their relative weight is readable; + // never zero, so an unpumped (all-zero) histogram is a flat line + // rather than a division by nothing. + let top = bins.iter().flatten().copied().max().unwrap_or(0).max(1) as f32; + for (channel, counts) in bins.iter().enumerate() { + let mut path = PathBuilder::stroke(px(1.5)); + for (bin, &count) in counts.iter().enumerate() { + let at = point( + o.x + s.width * (bin as f32 / (HIST_BINS - 1) as f32), + o.y + s.height * (1. - (count as f32 / top).sqrt()), + ); + match bin { + 0 => path.move_to(at), + _ => path.line_to(at), + } + } + if let Ok(path) = path.build() { + window.paint_path(path, rgb(HIST_INK[channel])); + } + } + }, + ) + .absolute() + .size_full() +} + /// The cascade's frequency response, drawn as one line across the graph. /// /// Every point comes from `EqParams::response_db`, which reads the very @@ -4788,17 +4979,18 @@ fn timecode(t: f64, fps: f64) -> String { #[cfg(test)] mod tests { use super::{ - ACCENT, CONTROL_H, Clip, EQ_FREQ_HIGH, EQ_FREQ_LOW, EQ_GAIN_LIMIT, EQ_GRAPH_H, EQ_HANDLE, - EQ_TICKS, EXPORT_ROWS_H, FORMATS, Format, HEADER_GAP, HEADER_W, HIT_MIN, INK, INK_DIM, + ACCENT, COLOR_BANDS, COLOR_BAR_W, COLOR_STEP, COLOR_W, CONTROL_H, Clip, EQ_FREQ_HIGH, + EQ_FREQ_LOW, EQ_GAIN_LIMIT, EQ_GRAPH_H, EQ_HANDLE, EQ_TICKS, EXPORT_ROWS_H, FORMATS, + Format, HEADER_GAP, HEADER_W, HIST_BINS, HIST_H, HIST_SAMPLES, HIT_MIN, INK, 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, SOURCE_TINTS, SURFACE, SWATCH_W, Source, StreamInfo, Volume, - WAVE_BPS, WAVE_COL, Wave, applicable, band_label, can_add, cancels_export, envelope, eq_x, - eq_y, export_path, export_settings, format_line, frac_along, frac_down, frame_at, - is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked, menu_at, mp4_refusal, - normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label, - source_frames, source_tint, start_frac, timecode, unseen_paths, unseen_sources, whole_take, - width_frac, window_title, + WAVE_BPS, WAVE_COL, Wave, applicable, band_label, can_add, cancels_export, color_snap, + envelope, eq_x, eq_y, export_path, export_settings, format_line, frac_along, frac_down, + frame_at, histogram, is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked, + menu_at, mp4_refusal, normalise, panel_h, project_path, push_digit, retarget, scrub_due, + show_label, source_frames, source_tint, start_frac, timecode, unseen_paths, unseen_sources, + whole_take, width_frac, window_title, }; use super::{file_name, file_uri, library_rows}; @@ -5712,6 +5904,130 @@ mod tests { assert!(KEYS_ROW_H >= HIT_MIN); } + /// A colour slider is dragged straight to a value, so where the pointer + /// lands and where the bar then paints have to be the same place: this is + /// the round trip [`Player::drag_color`] makes, pixels -> value -> fill. + #[test] + fn a_colour_drag_lands_where_it_paints_and_the_card_fits_the_smallest_window() { + // A bar as laid out, somewhere that is not the window's origin -- a + // mapping that forgot the offset would pass at zero. + let bar = Bounds { + origin: point(px(180.), px(240.)), + size: size(px(COLOR_BAR_W), px(KEYS_ROW_H)), + }; + for &(label, low, high) in &COLOR_BANDS { + // The ends are the ends: the left of the bar is the bottom of the + // range and the right is the top, so a slider can be pulled to + // either without hunting for the last pixel. + let at = |x: f32| color_snap(low + frac_along(px(x), bar) * (high - low)); + assert_eq!(at(180.), low, "{label} left end"); + assert_eq!(at(180. + COLOR_BAR_W), high, "{label} right end"); + // Off either end clamps rather than running past the range. + assert_eq!(at(-4000.), low, "{label} off the left"); + assert_eq!(at(9999.), high, "{label} off the right"); + + for step in 0..=48 { + let along = step as f32 / 48.; + let value = at(180. + along * COLOR_BAR_W); + // Every stop is one the keyboard can also reach, which is what + // keeps "0.35" the number the file writes. + let steps = value / COLOR_STEP; + assert!( + (steps - steps.round()).abs() < 1e-3, + "{label}: {value} is off the {COLOR_STEP} grid" + ); + assert!( + (low..=high).contains(&value), + "{label}: {value} outside {low}..{high}" + ); + // What the row paints from that value is where the pointer was, + // to within the half step the snap costs. + let painted = (value - low) / (high - low); + let slack = COLOR_STEP / (high - low) / 2. + 1e-4; + assert!( + (painted - along).abs() <= slack, + "{label}: pressed at {along}, paints at {painted}" + ); + } + } + + // The same shape as the other two cards, so it fits where they do: the + // graph, four rows and the reset button inside a 360 px window. + let (title, status, gaps, padding) = (17., 17., 6. * 2., 24.); + let rows = COLOR_BANDS.len() as f32 * KEYS_ROW_H; + assert!( + title + status + HIST_H + rows + gaps + padding + CONTROL_H + 4. <= 360., + "card too tall" + ); + assert!(COLOR_W <= 640., "card too wide"); + // The label still has room beside the bar and the readout, which is + // what the buttons coming off the row bought. + let row = COLOR_W - padding - 12. - 2. * 8. - COLOR_BAR_W - 44.; + assert!(row >= LABEL_MIN_W, "no room left for a label: {row}px"); + // What is dragged is the whole row's height, not the 4 px the bar is + // drawn as (WCAG 2.5.8) -- the same split the ruler makes. + assert!(KEYS_ROW_H >= HIT_MIN); + } + + /// The graph over the sliders is the frame the grade already went through, + /// so it has to count what is actually in those bytes -- BGRA on the wire, + /// red-green-blue in the bins. + #[test] + fn the_histogram_counts_the_frame_it_is_handed() { + // Half pure red, half mid grey: two known values, in two known bins. + let (w, h) = (64usize, 64usize); + let mut frame = Vec::with_capacity(w * h * 4); + for _ in 0..h { + for col in 0..w { + match col < w / 2 { + true => frame.extend_from_slice(&[0, 0, 255, 255]), + false => frame.extend_from_slice(&[128, 128, 128, 255]), + } + } + } + let bins = histogram(&frame); + let half = (w * h / 2) as u32; + // 64 bins over 256 codes: 255 is the last bin, 128 the middle one, 0 the + // first. + assert_eq!(bins[0][63], half, "the red half tops the red channel"); + assert_eq!(bins[0][32], half, "and the grey half sits mid red"); + for channel in [1, 2] { + assert_eq!(bins[channel][0], half, "no green or blue in the red half"); + assert_eq!(bins[channel][32], half); + assert_eq!(bins[channel][63], 0); + } + // Nothing is counted twice and nothing is dropped: this frame is small + // enough to be read whole. + for channel in bins { + assert_eq!(channel.iter().sum::(), (w * h) as u32); + } + + // A grade shifts it, which is the whole point of drawing it: the same + // frame darkened lands in lower bins. + let darker: Vec = frame.iter().map(|b| b / 2).collect(); + let bins = histogram(&darker); + assert_eq!(bins[0][31], half, "255 -> 127"); + assert_eq!(bins[0][16], half, "128 -> 64"); + + // A real frame is subsampled: a 1080p one is read every 253rd pixel, so + // the shape costs a thousandth of the reads and still counts thousands. + let big = vec![200u8; 1920 * 1080 * 4]; + let bins = histogram(&big); + let counted = bins[0].iter().sum::(); + let pixels = 1920 * 1080usize; + let expected = pixels.div_ceil(pixels / HIST_SAMPLES) as u32; + assert_eq!(counted, expected, "every strided pixel counted, once"); + assert!( + (HIST_SAMPLES as u32..=HIST_SAMPLES as u32 + 64).contains(&counted), + "{counted} samples is not the budget" + ); + assert_eq!(bins[0][200 * HIST_BINS / 256], counted, "all in one bin"); + + // An empty buffer is a flat graph rather than a panic: the card is open + // before the first frame is pumped. + assert_eq!(histogram(&[]), [[0; HIST_BINS]; 3]); + } + /// Mute and level are one control with two states, and the whole point is /// that mute keeps the level: the user gets back what they had, not 100%. #[test] @@ -6244,6 +6560,11 @@ fn main() { eq_graph: Rc::default(), color_open: None, color_band: 0, + color_dragging: false, + color_bars: std::array::from_fn(|_| Rc::default()), + // Empty until the first frame is pumped, which draws as a + // flat line rather than as a shape nothing measured. + histogram: [[0; HIST_BINS]; 3], // What an export is until someone says otherwise: the // bitrate the picture asks for. quality: Quality::Auto, diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index e0925df..ff0ec66 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -624,6 +624,14 @@ impl PlaybackSession { self.edit(|p| p.set_color(lane, idx, params)) } + /// The same grade and the same reseek without the undo step + /// ([`Project::set_color_live`]): what the samples inside one slider drag go + /// through, so the frame regrades under the hand and the whole gesture is + /// still a single `z`. + pub fn set_color_live(&mut self, lane: Lane, idx: usize, params: Option) -> bool { + self.edit(|p| p.set_color_live(lane, idx, params)) + } + /// The project's resolution: what every clip is composed onto, what the /// window is sized to and what an export writes -- *not* any one file's. /// It starts as source 0's picture, which is what a project meant before it diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 011f4ac..5fb2ce9 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -633,6 +633,24 @@ impl Project { /// (and no history) for an index that is not there or a value that is not /// finite, and equal settings share a table entry. pub fn set_color(&mut self, lane: Lane, idx: usize, params: Option) -> bool { + self.write_color(lane, idx, params, true) + } + + /// [`set_color`](Self::set_color) without the undo step: the samples *inside* + /// one pointer drag, whose first write (a plain `set_color`) already took the + /// snapshot the gesture rolls back to. A drag across a slider is one undo, + /// not one per pixel -- and undoing it lands where the hand picked it up. + pub fn set_color_live(&mut self, lane: Lane, idx: usize, params: Option) -> bool { + self.write_color(lane, idx, params, false) + } + + fn write_color( + &mut self, + lane: Lane, + idx: usize, + params: Option, + snapshot: bool, + ) -> bool { if idx >= self.lane(lane).len() { return false; } @@ -655,7 +673,9 @@ impl Project { }) } }; - self.snapshot(); + if snapshot { + self.snapshot(); + } self.lane_mut(lane).expect("checked above")[idx].color = slot; true } @@ -2514,6 +2534,42 @@ mod tests { ); } + /// A slider drag is one gesture: the press snapshots, every sample after it + /// only regrades, and the single undo lands on what the clip was *before* + /// the hand touched it -- not one step back down the drag. + #[test] + fn a_whole_colour_drag_undoes_in_one_step() { + let mut p = three(); + assert!(p.set_color(Lane::V1, 0, Some(grade_at(1))), "the press"); + for n in 2..=8 { + assert!(p.set_color_live(Lane::V1, 0, Some(grade_at(n))), "a sample"); + } + assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(8))); + assert!(p.undo()); + assert!( + p.color_of(Lane::V1, 0).is_none(), + "one undo is the whole gesture, back to ungraded" + ); + + // The live write refuses what the snapshotting one refuses, and a + // refusal still costs no history either way. + assert!(!p.set_color_live(Lane::V1, 99, Some(grade_at(1)))); + assert!(!p.set_color_live( + Lane::V1, + 0, + Some(ColorParams { + contrast: f32::NAN, + ..grade_at(1) + }) + )); + assert!(p.undo()); + assert_eq!( + p.lane(Lane::V1).len(), + 2, + "the next step back is the split before the drag: no sample pushed one" + ); + } + /// The colour table is append-only within a session, exactly as the eq one /// is -- and a save is where an undone grade goes. #[test]