FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
monitors: ruler ticks follow the time zoom
ReReddo5a755aecd07b
diff --git a/crates/ui-egui/src/panels/monitor.rs b/crates/ui-egui/src/panels/monitor.rsindex 992677c20..83f553a23 100644--- a/crates/ui-egui/src/panels/monitor.rs+++ b/crates/ui-egui/src/panels/monitor.rs@@ -760,17 +760,27 @@ fn mini_timeline(app.auto.add(if which == Which::Program { "program.scrubBar" } else { "source.scrubBar" }, bar, "scrub bar");let interaction = crate::panels::source_range::interact(app, ui, bar, which, shown);let p = ui.painter();- // ticks: minor 4 pt, major 10 pt, ~20 pt spacing, at fixed times so they move with a scroll- let n = ((bar.width() / 20.0) as i32).max(2);- let step = dur / f64::from(n);- let first = ((shown.0 - origin).0 as f64 / step).ceil() as i64;- for i in first..=first.saturating_add(i64::from(n)) {- let x = bar.min.x + ((origin.0 as f64 + i as f64 * step - shown.0.0 as f64) / dur) as f32 * bar.width();+ // ticks: minor 3.5 pt high, major 8 pt, on whole frames chosen as on the Timeline's ruler (minor+ // at least 12 pt apart, major at least 110 pt), so they spread out and fill in as the ruler zooms+ let frame_px = rate.frame_duration().0 as f64 / dur * f64::from(bar.width());+ let base = rate.timecode_base();+ let steps: Vec<i64> =+ [1, 2, 5, 10, base / 2].into_iter().chain([1, 2, 5, 10, 15, 30, 60, 120, 300, 600, 1800, 3600].map(|k| base.saturating_mul(k))).collect();+ let major = steps.iter().copied().find(|s| *s > 0 && *s as f64 * frame_px >= 110.0).unwrap_or(base.saturating_mul(3600)).max(1);+ // a step that divides the major one, so every major tick is on a minor one+ let minor = steps.iter().copied().find(|s| *s > 0 && major % s == 0 && *s as f64 * frame_px >= 12.0).unwrap_or(major);+ let mut f = rate.frame_at(shown.0) / minor * minor;+ // (bounded: a ruler too short for its ticks shows the first few hundred)+ for _ in 0..512 {+ let x = bar.min.x + ((rate.tick_of(f).0 as f64 - shown.0.0 as f64) / dur) as f32 * bar.width();if x > bar.max.x + 0.5 {break;}- let h = if i % 5 == 0 { 8.0 } else { 3.5 };- p.line_segment([pos2(x, bar.max.y - h), pos2(x, bar.max.y)], Stroke::new(1.0, t.text_faint));+ if x >= bar.min.x - 0.5 {+ let h = if f % major == 0 { 8.0 } else { 3.5 };+ p.line_segment([pos2(x, bar.max.y - h), pos2(x, bar.max.y)], Stroke::new(1.0, t.text_faint));+ }+ f = f.saturating_add(minor);}let a_t = interaction.range.map(|r| r.start).unwrap_or(mark_in.unwrap_or(origin));// Out includes its frame; both the shading and its bracket end at the following boundary.diff --git a/crates/ui-egui/tests/view_ui.rs b/crates/ui-egui/tests/view_ui.rsindex fd0212c07..f8d40e6f6 100644--- a/crates/ui-egui/tests/view_ui.rs+++ b/crates/ui-egui/tests/view_ui.rs@@ -250,14 +250,58 @@ fn wheel_zooms_about_the_pointer_and_the_middle_button_pans() {assert!(source["zoom"].is_null() && source["pan"] == json!([0.0, 0.0]), "{source}");}+/// The x positions of the tick marks drawn on the time ruler `bar`.+fn ruler_ticks(d: &Driver, bar: [f64; 4]) -> Vec<f64> {+ let bottom = (bar[1] + bar[3]) as f32;+ let (left, right) = (bar[0] as f32 - 1.0, (bar[0] + bar[2]) as f32 + 1.0);+ let mut xs: Vec<f64> = d+ .harness+ .output()+ .shapes+ .iter()+ .filter_map(|s| match &s.shape {+ egui::Shape::LineSegment { points: [a, b], .. }+ if a.x == b.x && (b.y - bottom).abs() < 0.01 && [3.5, 8.0].iter().any(|h| (b.y - a.y - h).abs() < 0.01) && (left..=right).contains(&a.x) =>+ {+ Some(f64::from(a.x))+ }+ _ => None,+ })+ .collect();+ xs.sort_by(f64::total_cmp);+ xs+}++/// The frames the ticks `xs` of the ruler `bar` stand for while it shows `zoom` (fractions) of a+/// sequence `frames` long; and that each is a whole frame.+fn tick_frames(xs: &[f64], bar: [f64; 4], zoom: (f64, f64), frames: f64) -> Vec<f64> {+ let f: Vec<f64> = xs.iter().map(|x| (zoom.0 + (x - bar[0]) / bar[2] * (zoom.1 - zoom.0)) * frames).collect();+ assert!(f.iter().all(|f| (f - f.round()).abs() < 0.05), "ticks off whole frames: {f:?}");+ f+}++/// The minor tick step (frames): the commonest gap between neighbouring ticks.+fn tick_step(frames: &[f64]) -> i64 {+ let mut counts = std::collections::BTreeMap::new();+ for w in frames.windows(2) {+ *counts.entry((w[1] - w[0]).round() as i64).or_insert(0) += 1;+ }+ counts.into_iter().max_by_key(|(_, n)| *n).map(|(gap, _)| gap).expect("ticks")+}+/// The zoom bar under a monitor's time ruler: a handle zooms the ruler in, the middle scrolls it,/// the ruler maps clicks and the playhead to the part it shows, and each monitor keeps its own.#[test]fn time_zoom_bar_zooms_and_scrolls_the_monitor_ruler() {let mut d = Driver::demo();- let duration = num(&d.exec("sequence.inspect", json!({}))["duration"]);+ let seq = d.exec("sequence.inspect", json!({}));+ let duration = num(&seq["duration"]);+ let rate = &seq["settings"]["frame_rate"];+ let frames = duration * num(&rate["num"]) / num(&rate["den"]) / 254_016_000_000.0;let track = d.rect("program.zoomBar.track");assert!((d.rect("program.zoomBar")[2] - track[2]).abs() < 0.5, "the whole sequence at first");+ let bar = d.rect("program.scrubBar");+ let full = tick_frames(&ruler_ticks(&d, bar), bar, (0.0, 1.0), frames);// the right handle dragged to the middle: the ruler shows the first halflet y = track[1] + track[3] / 2.0;d.ok("ui.drag", json!({"from": {"x": track[0] + track[2] - 4.0, "y": y}, "to": {"x": track[0] + track[2] / 2.0, "y": y}, "steps": 8}));@@ -266,9 +310,18 @@ fn time_zoom_bar_zooms_and_scrolls_the_monitor_ruler() {assert!(num(&z[0]) == 0.0 && (num(&z[1]) - 0.5).abs() < 0.02, "{z}");let thumb = d.rect("program.zoomBar");assert!((thumb[2] - track[2] * num(&z[1])).abs() < 1.0, "thumb {thumb:?} for {z}");+ // the ticks follow the zoom: still on whole frames, no further apart in time, and spread+ // further apart on screen than the same step was before+ let zoomed = tick_frames(&ruler_ticks(&d, bar), bar, (0.0, num(&z[1])), frames);+ let (full_step, zoomed_step) = (tick_step(&full), tick_step(&zoomed));+ assert!(zoomed_step <= full_step, "tick steps {full_step} → {zoomed_step} frames");+ let (before, after) = (full_step as f64 / frames * bar[2], zoomed_step as f64 / (frames * num(&z[1])) * bar[2]);+ assert!(after >= 12.0 - 0.01, "minor ticks at least 12 pt apart: {after}");+ if zoomed_step == full_step {+ assert!(after > before * 1.5, "same step, spread out: {before} → {after} pt");+ }// a click halfway along the ruler is halfway through the half shown, and the playhead is drawn there- let bar = d.rect("program.scrubBar");d.ok("ui.click", json!({"x": bar[0] + bar[2] / 2.0, "y": bar[1] + bar[3] / 2.0}));d.frames(2);let playhead = num(&d.exec("sequence.inspect", json!({}))["playhead"]);