Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Value fields: leave the view its whole wheel, and sideways swipes (#5473)

Khawar Zamman Wani87c53e06abef
diff --git a/crates/ui-egui/src/field_wheel.rs b/crates/ui-egui/src/field_wheel.rs
index d8924b0d8..c2492e04f 100644
--- a/crates/ui-egui/src/field_wheel.rs
+++ b/crates/ui-egui/src/field_wheel.rs
@@ -40,9 +40,11 @@ fn taken_id() -> Id {
Id::new("pc-field-wheel-taken")
}
-/// A wheel event the focused field takes: plain or Shift, not the view's Alt or Ctrl/Cmd zoom.
+/// A wheel event a field takes: plain or Shift, not the view's Alt or Ctrl/Cmd zoom, nor a plain
+/// sideways scroll (a trackpad swipe pans the view; Shift+wheel can arrive sideways).
fn for_field(e: &Event) -> bool {
- matches!(e, Event::MouseWheel { modifiers, .. } if !modifiers.alt && !modifiers.ctrl && !modifiers.command && !modifiers.mac_cmd)
+ matches!(e, Event::MouseWheel { delta, modifiers, .. }
+ if (delta.y != 0.0 || modifiers.shift) && !modifiers.alt && !modifiers.ctrl && !modifiers.command && !modifiers.mac_cmd)
}
/// Before this frame's UI is laid out: hand the wheel to the focused value field, if any, and
@@ -53,20 +55,23 @@ pub fn begin_frame(ctx: &Context) {
let focused = ctx.data(|d| d.get_temp::<(Id, Id)>(focused_id())).filter(|(id, _)| ctx.memory(|m| m.has_focus(*id)));
let (now, wheel) = ctx.input(|i| (i.time, i.events.iter().any(|e| matches!(e, Event::MouseWheel { .. }))));
if let Some((_, field)) = focused {
- let taken: Vec<Event> = ctx.input_mut(|i| {
+ let (taken, view_wheel): (Vec<Event>, bool) = ctx.input_mut(|i| {
let (taken, rest) = std::mem::take(&mut i.events).into_iter().partition(for_field);
i.events = rest;
- // egui spreads each notch over a few frames: none of it scrolls anything meanwhile.
- if !i.events.iter().any(|e| matches!(e, Event::MouseWheel { .. })) {
- i.smooth_scroll_delta = Vec2::ZERO;
- }
- taken
+ (taken, i.events.iter().any(|e| matches!(e, Event::MouseWheel { .. })))
});
+ if wheel {
+ // The latest wheel is the view's when any of it was left to the view (Alt, Ctrl/Cmd).
+ let owner = if view_wheel { Owner::Elsewhere } else { Owner::Field(field) };
+ ctx.data_mut(|d| d.insert_temp(gesture_id(), Gesture { last: now, owner }));
+ }
if !taken.is_empty() {
- ctx.data_mut(|d| {
- d.insert_temp(taken_id(), taken);
- d.insert_temp(gesture_id(), Gesture { last: now, owner: Owner::Field(field) });
- });
+ ctx.data_mut(|d| d.insert_temp(taken_id(), taken));
+ }
+ // egui spreads each notch over a few frames: the tail of the field's own notches scrolls
+ // nothing, while the view's keeps all of its own.
+ if ctx.data(|d| d.get_temp::<Gesture>(gesture_id())).is_some_and(|g| g.owner == Owner::Field(field)) {
+ ctx.input_mut(|i| i.smooth_scroll_delta = Vec2::ZERO);
}
return;
}
@@ -209,6 +214,27 @@ mod tests {
assert!(h.state().1 > 0.0, "the panel scrolled");
}
+ /// A plain sideways scroll (a trackpad swipe) is the view's, focused or hovered; Shift+wheel
+ /// that the system turned sideways still steps by ten.
+ #[test]
+ fn a_sideways_scroll_steps_a_field_only_with_shift() {
+ for focus in [false, true] {
+ for (m, want) in [(Modifiers::NONE, 100.0), (Modifiers::SHIFT, 110.0)] {
+ let mut h = harness(100.0);
+ if focus {
+ h.get_by_role(Role::SpinButton).click();
+ h.run();
+ }
+ let at = field(&h).center();
+ h.hover_at(at);
+ h.run_steps(1);
+ h.event(egui::Event::MouseWheel { unit: egui::MouseWheelUnit::Line, delta: vec2(1.0, 0.0), phase: egui::TouchPhase::Move, modifiers: m });
+ h.run_steps(4);
+ assert_eq!(h.state().0, want, "focus {focus}, {m:?}");
+ }
+ }
+ }
+
#[test]
fn a_disabled_field_ignores_the_wheel() {
let mut h = harness(100.0);
diff --git a/crates/ui-egui/src/field_wheel_app_tests.rs b/crates/ui-egui/src/field_wheel_app_tests.rs
index ba902da50..6d4f56db3 100644
--- a/crates/ui-egui/src/field_wheel_app_tests.rs
+++ b/crates/ui-egui/src/field_wheel_app_tests.rs
@@ -71,3 +71,35 @@ fn the_wheel_over_a_field_steps_it_without_a_click() {
assert!(!field(&h, "21").is_focused(), "hovering doesn't put the cursor in the field");
assert_eq!(view(&h), before, "the canvas didn't move");
}
+
+/// How far a wheel of `delta` lines with `modifiers` over the canvas moves the view, a Smoothing
+/// field focused or not, once egui's smoothed scroll has run out. The field isn't stepped.
+fn canvas_pan(focus: bool, delta: egui::Vec2, modifiers: egui::Modifiers) -> (f32, [f32; 2]) {
+ let mut h = harness();
+ if focus {
+ field(&h, "10").click();
+ h.run();
+ }
+ let canvas = egui::pos2(640.0, 500.0);
+ h.hover_at(canvas);
+ h.run_steps(1);
+ let before = view(&h);
+ h.event(egui::Event::MouseWheel { unit: egui::MouseWheelUnit::Line, delta, phase: egui::TouchPhase::Move, modifiers });
+ h.run_steps(40);
+ assert!(h.query_all_by_role(Role::SpinButton).any(|n| n.value().as_deref() == Some("10")), "focus {focus}: the field wasn't stepped");
+ let after = view(&h);
+ (after.0 / before.0, [after.1[0] - before.1[0], after.1[1] - before.1[1]])
+}
+
+/// The wheel a focused field leaves to the view moves it as far as with no field focused: its
+/// smoothed tail isn't cut off (⌘/Ctrl + wheel, and a plain sideways trackpad swipe).
+#[test]
+fn the_view_keeps_its_whole_wheel_while_a_field_is_focused() {
+ for (delta, m) in [(vec2(0.0, -1.0), egui::Modifiers::COMMAND), (vec2(0.0, -1.0), egui::Modifiers::CTRL), (vec2(-1.0, 0.0), egui::Modifiers::NONE)] {
+ let free = canvas_pan(false, delta, m);
+ assert!(free != (1.0, [0.0, 0.0]), "{delta:?} {m:?}: the wheel moves the view");
+ let focused = canvas_pan(true, delta, m);
+ let close = (focused.0 - free.0).abs() < 1e-3 && (0..2).all(|k| (focused.1[k] - free.1[k]).abs() < 0.5);
+ assert!(close, "{delta:?} {m:?}: {focused:?} with a field focused, {free:?} without");
+ }
+}