diff --git a/crates/behavior/src/sense.rs b/crates/behavior/src/sense.rs index 062bf0640..eb396da58 100644 --- a/crates/behavior/src/sense.rs +++ b/crates/behavior/src/sense.rs @@ -42,6 +42,9 @@ bitflags! { /// is a one-value gesture, not a click. Declared with `DRAG`: a drag moves the value with the pointer. /// `Cx::follow_press` runs such a part's gesture. const SETS_ON_PRESS = 16384; + /// A zoom zone: a vertical wheel over the shape zooms the view of the innermost enclosing taker of the zoom + /// share (a ruler over its time area). The shape takes no wheel share itself. + const WHEEL_ZOOMS_VIEW = 32768; } } @@ -49,6 +52,9 @@ impl Sense { /// The bits that make a shape a press candidate. pub const PRESS: Sense = Sense::CLICK.union(Sense::DRAG); + /// The bits that make a shape register a wheel taker. + pub const WHEELS: Sense = Sense::WHEEL.union(Sense::WHEEL_X).union(Sense::WHEEL_Y).union(Sense::WHEEL_ZOOM).union(Sense::WHEEL_ZOOMS_VIEW); + /// Whether a press can start on a shape with this sense. pub const fn takes_press(self) -> bool { self.intersects(Sense::PRESS) diff --git a/crates/behavior/src/wheel.rs b/crates/behavior/src/wheel.rs index 3b3b21741..39016592a 100644 --- a/crates/behavior/src/wheel.rs +++ b/crates/behavior/src/wheel.rs @@ -48,12 +48,15 @@ pub struct Wheel { pub phase: WheelPhase, /// Where the pointer was, in screen points; a zoom is about this point. pub at: Option, + /// The vertical scroll came from over a zoom zone (`Sense::WHEEL_ZOOMS_VIEW`): the taker zooms its view by it, + /// about `at`, instead of scrolling. + pub zooms: bool, } impl Wheel { /// A scroll of `scroll` points with no zoom. pub const fn new(scroll: Vec2, mods: Mods, unit: WheelUnit, phase: WheelPhase) -> Self { - Wheel { scroll, zoom: 1.0, mods, unit, phase, at: None } + Wheel { scroll, zoom: 1.0, mods, unit, phase, at: None, zooms: false } } /// The same gesture with zoom factor `zoom`. @@ -92,6 +95,9 @@ pub struct WheelTaker { pub notch: WheelNotch, /// How far a scroll region can still move each way; `None` for a taker without an end (a value control, a canvas). pub room: Option, + /// A zoom zone: a vertical wheel over it goes to the innermost enclosing open taker of the zoom share, with + /// `Wheel::zooms` set. A zone that takes no axis itself never takes a share. + pub zooms_view: bool, } /// The scroll left in a region, in points of wheel scroll: `positive` is what a wheel with positive `scroll` can @@ -148,18 +154,28 @@ impl WheelTaker { scrolls: false, notch: WheelNotch::DEFAULT, room: None, + zooms_view: false, } } /// The taker a shape with `sense` registers, `None` when `sense` has no wheel bit: `WHEEL` takes both scroll - /// axes and zoom; `WHEEL_X`, `WHEEL_Y` and `WHEEL_ZOOM` take one share each and combine. + /// axes and zoom; `WHEEL_X`, `WHEEL_Y` and `WHEEL_ZOOM` take one share each and combine; `WHEEL_ZOOMS_VIEW` + /// makes a zoom zone. pub fn for_sense(owner: O, rect: Rect, sense: Sense) -> Option { let all = sense.contains(Sense::WHEEL); let taker = WheelTaker::new(owner, rect) .horizontal(all || sense.contains(Sense::WHEEL_X)) .vertical(all || sense.contains(Sense::WHEEL_Y)) - .zoom(all || sense.contains(Sense::WHEEL_ZOOM)); - (taker.horizontal || taker.vertical || taker.zoom).then_some(taker) + .zoom(all || sense.contains(Sense::WHEEL_ZOOM)) + .zooms_view(sense.contains(Sense::WHEEL_ZOOMS_VIEW)); + (taker.horizontal || taker.vertical || taker.zoom || taker.zooms_view).then_some(taker) + } + + /// Whether it is a zoom zone (`WheelTaker::zooms_view`). + #[must_use] + pub const fn zooms_view(mut self, on: bool) -> Self { + self.zooms_view = on; + self } /// Whether it takes horizontal scroll. @@ -227,37 +243,48 @@ impl WheelTaker { /// The split rule: each axis of `wheel` goes to the innermost taker under `at` that takes it; a share no taker /// takes, or that a scroll region at its end in that direction cannot move, passes to the next enclosing one. A gated taker takes no axis unless it is `focused` or -/// `armed`. Shift turns a vertical wheel sideways when its vertical owner is a scroll region. Shares of one owner merge. +/// `armed`. Shift turns a vertical wheel sideways when its vertical owner is a scroll region. When the innermost open +/// taker under `at` for the vertical share is a zoom zone, the share goes to the innermost open zoom taker enclosing it, +/// with `Wheel::zooms` set; without one the zone is passed over. Shares of one owner merge. pub fn split(takers: &[WheelTaker], at: Pos2, wheel: Wheel, focused: Option, armed: Option) -> Vec<(O, Wheel)> { // Takers register outer first, so among equal areas the later one is the deeper (or the upper) and goes first. let mut under: Vec<&WheelTaker> = takers.iter().rev().filter(|taker| taker.rect.contains(at)).collect(); under.sort_by(|a, b| a.rect.area().total_cmp(&b.rect.area())); let open = |taker: &&WheelTaker| !taker.gated || armed == Some(taker.owner) || focused == Some(taker.owner); + let zone = (wheel.scroll.y != 0.0).then(|| under.iter().position(|t| (t.vertical || t.zooms_view) && open(t))).flatten(); + let zone_view = zone.filter(|&at| !under[at].vertical).and_then(|at| under[at + 1..].iter().find(|t| t.zoom && open(t))).map(|t| t.owner); let vertical_owner = (wheel.scroll.y != 0.0).then(|| under.iter().find(|t| t.vertical && open(t))).flatten(); // Shift turns the vertical wheel sideways over a scroll region (Photoshop); a value control that takes the // vertical wheel keeps Shift as its modifier, and a device that already sends a sideways axis keeps it. - let wheel = if wheel.mods.shift && wheel.scroll.x == 0.0 && vertical_owner.is_some_and(|t| t.scrolls) { + let wheel = if zone_view.is_none() && wheel.mods.shift && wheel.scroll.x == 0.0 && vertical_owner.is_some_and(|t| t.scrolls) { Wheel { scroll: Vec2::new(wheel.scroll.y, 0.0), ..wheel } } else { wheel }; - let vertical = - (wheel.scroll.y != 0.0).then(|| under.iter().find(|t| t.vertical && open(t) && t.has_room(true, wheel.scroll.y))).flatten().map(|t| t.owner); + let vertical = zone_view.or_else(|| { + (wheel.scroll.y != 0.0).then(|| under.iter().find(|t| t.vertical && open(t) && t.has_room(true, wheel.scroll.y))).flatten().map(|t| t.owner) + }); let horizontal = (wheel.scroll.x != 0.0) .then(|| under.iter().find(|t| t.horizontal && open(t) && t.has_room(false, wheel.scroll.x))) .flatten() .map(|t| t.owner); let zoom = (wheel.zoom != 1.0).then(|| under.iter().find(|t| t.zoom && open(t))).flatten().map(|t| t.owner); let mut shares: Vec<(O, Wheel)> = Vec::new(); - let parts = [(vertical, Vec2::new(0.0, wheel.scroll.y), 1.0), (horizontal, Vec2::new(wheel.scroll.x, 0.0), 1.0), (zoom, Vec2::ZERO, wheel.zoom)]; - for (owner, scroll, zoom) in parts { + let zooms = zone_view.is_some(); + let parts = [ + (vertical, Vec2::new(0.0, wheel.scroll.y), 1.0, zooms), + (horizontal, Vec2::new(wheel.scroll.x, 0.0), 1.0, false), + (zoom, Vec2::ZERO, wheel.zoom, false), + ]; + for (owner, scroll, zoom, zooms) in parts { let Some(owner) = owner else { continue }; match shares.iter_mut().find(|(o, _)| *o == owner) { Some((_, share)) => { share.scroll += scroll; share.zoom *= zoom; + share.zooms |= zooms; } - None => shares.push((owner, Wheel { scroll, zoom, ..wheel })), + None => shares.push((owner, Wheel { scroll, zoom, zooms, ..wheel })), } } shares @@ -285,4 +312,19 @@ mod tests { assert_eq!(owner(7.0), Some(1), "no room toward positive: the outer takes it"); assert_eq!(owner(-7.0), Some(2), "room toward negative: the inner keeps it"); } + + #[test] + fn a_vertical_wheel_over_a_zoom_zone_zooms_the_enclosing_view() { + let region = WheelTaker::new(1u8, rect(300.0)).vertical(true).scrolls(true); + let view = WheelTaker::new(2u8, rect(200.0)).horizontal(true).zoom(true); + let zone = WheelTaker::new(3u8, rect(100.0)).zooms_view(true); + let at = pos2(10.0, 10.0); + let shares = split(&[region, view, zone], at, wheel(0.0, 5.0), None, None); + assert_eq!(shares.len(), 1); + assert_eq!((shares[0].0, shares[0].1.scroll.y, shares[0].1.zooms), (2, 5.0, true), "the view zooms by the vertical share"); + let shares = split(&[region, zone], at, wheel(0.0, 5.0), None, None); + assert_eq!(shares.iter().map(|(o, w)| (*o, w.zooms)).collect::>(), [(1, false)], "no zoom taker: the zone is passed over"); + let shares = split(&[region, view, zone], at, wheel(4.0, 0.0), None, None); + assert_eq!(shares.iter().map(|(o, w)| (*o, w.zooms)).collect::>(), [(2, false)], "the horizontal share passes the zone"); + } } diff --git a/crates/timeline/src/ui/ruler.rs b/crates/timeline/src/ui/ruler.rs index af167fb82..402b79534 100644 --- a/crates/timeline/src/ui/ruler.rs +++ b/crates/timeline/src/ui/ruler.rs @@ -271,6 +271,7 @@ fn rows(rect: Rect, style: &TimelineRulerStyle) -> (Rect, Rect, Rect) { /// - Keyboard: one Tab stop. Left and Right step the playhead one frame (Shift: five), Home and End go to the start /// and the end; each a `Set`. /// - A secondary click reports the time under it in [`TimelineRulerResponse::context`]. +/// - A vertical wheel over the ruler zooms the enclosing `TimelineView` about the pointer, focused or not. /// - AccessKit: a slider named by the label, value the playhead's text, numeric value the playhead in seconds over /// `0..=end`. #[derive(Component, Props)] @@ -401,7 +402,7 @@ impl Component for TimelineRuler<'_> { } fn sense(&self) -> Sense { - Sense::FOCUS + Sense::FOCUS | Sense::WHEEL_ZOOMS_VIEW } fn bind(&mut self) -> Option> { diff --git a/crates/timeline/src/ui/view.rs b/crates/timeline/src/ui/view.rs index 6be16f1a5..16ca590f4 100644 --- a/crates/timeline/src/ui/view.rs +++ b/crates/timeline/src/ui/view.rs @@ -237,7 +237,8 @@ impl Overlay { /// step), publishes the [`TimeAxis`] to its body and shows the horizontal bar over the time area. The time area is one /// Tab stop and an F6 pane; its keys are Premiere's, each an exact chord: Left and Right step the playhead a frame /// (Shift: five), Home and End go to the start and the end, Up and Down to the previous and next edit point, and the -/// [`TimeCommand`]s zoom and fit. The Menu key and Shift+F10 report [`TimelineViewOutput::context_menu`]. +/// [`TimeCommand`]s zoom and fit. The Menu key and Shift+F10 report [`TimelineViewOutput::context_menu`]. Alt+wheel, +/// a pinch, and a plain vertical wheel over a zoom zone (the ruler) zoom about the pointer. #[derive(Component, Props)] #[component(container)] pub struct TimelineView<'a> { @@ -367,7 +368,7 @@ impl<'a> TimelineView<'a> { /// Reads this pass's wheel into `next`. fn wheel(&mut self, wheel: Wheel, next: &mut TimeView, at: Tick, width: f32) { let pinched = (f64::from(wheel.zoom) - 1.0).abs() > PINCH_EPSILON; - if pinched || (wheel.mods.alt && wheel.scroll.y != 0.0) { + if pinched || ((wheel.zooms || wheel.mods.alt) && wheel.scroll.y != 0.0) { let factor = if pinched { f64::from(wheel.zoom) } else { wheel_zoom_factor(wheel.scroll.y) }; if factor.is_finite() { self.zoom(next, factor, at, width); @@ -495,6 +496,8 @@ impl Component for TimelineView<'_> { let pointer = cx.response.hover_pos().unwrap_or_else(|| cx.rect.center()); let left = cx.flow.span(self.inset(cx.style), width).left(); if let Some(wheel) = cx.take_wheel() { + // The wheel's own point: over a child (the ruler) the view itself has no hover. + let pointer = wheel.at.unwrap_or(pointer); let at = next.time_at(pointer.x.max(left), left); self.wheel(wheel, &mut next, at, width); } diff --git a/crates/timeline/tests/it/main.rs b/crates/timeline/tests/it/main.rs index 4b188e606..9efc93f14 100644 --- a/crates/timeline/tests/it/main.rs +++ b/crates/timeline/tests/it/main.rs @@ -3,4 +3,5 @@ mod features; mod lanes; mod panel; +mod ruler; mod timecode; diff --git a/crates/timeline/tests/it/ruler.rs b/crates/timeline/tests/it/ruler.rs new file mode 100644 index 000000000..fc04054a9 --- /dev/null +++ b/crates/timeline/tests/it/ruler.rs @@ -0,0 +1,55 @@ +//! The ruler: a plain wheel over it zooms the timeline about the pointer, whether or not the ruler has focus. + +use std::cell::RefCell; +use std::rc::Rc; + +use craft_timeline::ui::{TimelineRuler, TimelineView}; +use craft_timeline::{FrameRate, Tick, TimeFormat, TimeView}; +use craft_ui::contract::{Bind, Show as _, ShowContainer as _}; +use craft_ui::testing::{Fixture, pointer, trial}; +use egui::{Pos2, vec2}; + +const POINTS_PER_SECOND: f64 = 40.0; + +/// A ruler inside a strip view over 60 s, its playhead and view held between passes. +fn ruler() -> (Fixture, Rc>) { + let view = Rc::new(RefCell::new(TimeView::new(Tick::ZERO, POINTS_PER_SECOND))); + let shown = view.clone(); + let playhead = Rc::new(RefCell::new(Tick::ZERO)); + let mut fixture = Fixture::new(move |ui| { + let (mut view, mut playhead) = (shown.borrow_mut(), playhead.borrow_mut()); + let format = TimeFormat::timecode(FrameRate::FPS_30); + TimelineView::new("Time", Bind::bound(&mut *view), format, Tick::from_seconds_f64(60.0)).strip().show(ui, |ui, _| { + TimelineRuler::new("Time ruler", Bind::bound(&mut *playhead)).show(ui); + }); + }); + fixture.room(vec2(600.0, 120.0)); + fixture.settle(); + (fixture, view) +} + +/// Turns the wheel up over `at` and lets the zoom ease to its end. +fn wheel_at(fixture: &mut Fixture, at: Pos2) { + fixture.frame(vec![pointer::moved(at)]); + fixture.frame(vec![pointer::wheel(20.0)]); + fixture.idle(60); +} + +#[trial] +fn a_wheel_over_the_ruler_zooms_the_timeline_about_the_pointer() { + let (mut fixture, view) = ruler(); + let rect = fixture.by_label("Time ruler").expect("the ruler").rect; + let at = rect.left_center() + vec2(rect.width() / 3.0, 0.0); + let under = |view: &TimeView| view.time_at(at.x, rect.left()).seconds(); + let before = under(&view.borrow()); + wheel_at(&mut fixture, at); + let zoomed = *view.borrow(); + assert!(zoomed.points_per_second > POINTS_PER_SECOND, "the wheel zoomed in: {} points a second", zoomed.points_per_second); + assert!((under(&zoomed) - before).abs() < 0.05, "the time under the pointer stayed: {before} s, then {} s", under(&zoomed)); + + // A click focuses the ruler (and scrubs); the wheel still zooms. + fixture.click_at(at); + let focused = view.borrow().points_per_second; + wheel_at(&mut fixture, at); + assert!(view.borrow().points_per_second > focused, "the focused ruler passed the wheel to the view"); +} diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index c641bfa08..a75453463 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -85,7 +85,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { ); // A capped collection is bounded by its rows, so it scrolls them inside a scrolling body too. self.scrolls = self.capped || (!natural && !shared); - self.own_wheel = crate::contract::Component::sense(comp).intersects(Sense::WHEEL | Sense::WHEEL_X | Sense::WHEEL_Y | Sense::WHEEL_ZOOM); + self.own_wheel = crate::contract::Component::sense(comp).intersects(Sense::WHEELS); self.overflow = overflow; self.selection = match comp.selection() { Some(bind) => match bind.get() { diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 683d18ced..0bdc67ff9 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -855,7 +855,7 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, h let registers = match e.shape() { ShapeKind::Leaf => true, ShapeKind::Body => own.intersects(Sense::WHEEL | Sense::WHEEL_X), - ShapeKind::Item => !own.intersects(Sense::WHEEL | Sense::WHEEL_X | Sense::WHEEL_Y | Sense::WHEEL_ZOOM), + ShapeKind::Item => !own.intersects(Sense::WHEELS), }; if !inert && registers { steps::register_wheel(&ctx, id, rect, own, e.bound().0 && !facts.binds_view, env.wheel); diff --git a/crates/ui-contract/src/contract/steps/sense.rs b/crates/ui-contract/src/contract/steps/sense.rs index e28579b7f..513d0eba5 100644 --- a/crates/ui-contract/src/contract/steps/sense.rs +++ b/crates/ui-contract/src/contract/steps/sense.rs @@ -10,13 +10,15 @@ use egui::{Context, Id, Rect}; /// Registers a wheel taker for next pass's split (8.6) from the wheel bits of `sense` (`WheelTaker::for_sense`). A /// value control takes the wheel only while focused or armed, on the axes it senses (the vertical one when it senses /// none); inside a `WheelScope::Free` scope one that senses the wheel takes it without focus. Inside a -/// `WheelScope::Gated` scope every taker waits for focus or arming. +/// `WheelScope::Gated` scope every taker waits for focus or arming. A zoom zone that takes no axis itself waits only +/// for a gated scope: its wheel goes to the view around it. pub(crate) fn register_wheel(ctx: &Context, id: Id, rect: Rect, sense: Sense, value_control: bool, scope: WheelScope) { let sensed = craft_behavior::WheelTaker::for_sense(id, rect, sense); let free = sensed.is_some() && scope == WheelScope::Free; + let zone = sensed.is_some_and(|taker| taker.zooms_view && !(taker.horizontal || taker.vertical || taker.zoom)); let taker = sensed.or_else(|| value_control.then(|| craft_behavior::WheelTaker::new(id, rect).vertical(true))); if let Some(taker) = taker { - wheel::register(ctx, taker.gated(scope == WheelScope::Gated || (value_control && !free))); + wheel::register(ctx, taker.gated(scope == WheelScope::Gated || (value_control && !free && !zone))); } }