Value fields: the mouse wheel steps them, as in Photoshop (#5473)

A field that has been clicked into takes the wheel wherever the pointer
is: field_wheel::begin_frame takes plain and Shift wheel events out of
the frame's input before anything is laid out, so the canvas and panels
neither scroll nor pan. Alt and Ctrl/Cmd + wheel stay the view's.

Over a field that hasn't been clicked, the wheel steps it too (as in
Photopea, #4061), but only for a gesture that starts over the field. A
panel being scrolled keeps scrolling when a field passes under the
pointer.

One step a notch, as the Up/Down keys: 1, or 0.01 for small ranges,
x10 with Shift. A typed sum is worked out first. Hovering never takes
focus, and a non-finite step is dropped. The slider wheel and the
fields share widgets::count_notches.

Closes #5473

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Khawar Zamman WaniandClaude Opus 5.5 committed 2026-10-14 17:06:45 +05:30
1 parent ed7df56a24
commit 20735ab521
5 files changed
+378 -12

No files matched your search

+257
View File
@@ -0,0 +1,257 @@
//! The mouse wheel on value fields (#5473, #4061): one step a notch, as the Up/Down keys (×10
//! with Shift).
//!
//! - While a field has keyboard focus, the wheel steps it wherever the pointer is. [`begin_frame`]
//! takes the wheel out of the frame's input before anything is laid out, so neither the canvas
//! nor a panel scrolls or zooms with it. ⌥/Alt and ⌘/Ctrl + wheel stay the view's.
//! - Over a field without focus, the wheel steps it too, but only a gesture that starts over it:
//! a panel being scrolled keeps scrolling when a field passes under the pointer.
use egui::{Context, Event, Id, MouseWheelUnit, Rect, Ui, Vec2};
/// Wheel events this close together (seconds) are one gesture.
const GESTURE_GAP: f64 = 0.3;
/// Who a wheel gesture belongs to.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Owner {
/// It started this frame and no field has taken it yet.
New,
Field(Id),
/// It started where no field took it (a panel, the canvas).
Elsewhere,
}
#[derive(Clone, Copy, Debug)]
struct Gesture {
last: f64,
owner: Owner,
}
fn gesture_id() -> Id {
Id::new("pc-field-wheel-gesture")
}
fn focused_id() -> Id {
Id::new("pc-field-wheel-focused")
}
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.
fn for_field(e: &Event) -> bool {
matches!(e, Event::MouseWheel { modifiers, .. } if !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
/// follow which gesture the wheel is in.
pub fn begin_frame(ctx: &Context) {
ctx.data_mut(|d| d.remove::<Vec<Event>>(taken_id()));
// (the field's widget id, its slot id): focus is the widget's, a gesture the slot's.
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, 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
});
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) });
});
}
return;
}
if !wheel {
return;
}
let gesture = ctx.data(|d| d.get_temp::<Gesture>(gesture_id()));
let owner = match gesture {
Some(g) if now - g.last <= GESTURE_GAP => match g.owner {
// No field took it when it started.
Owner::New => Owner::Elsewhere,
owner => owner,
},
_ => Owner::New,
};
ctx.data_mut(|d| d.insert_temp(gesture_id(), Gesture { last: now, owner }));
}
/// Record that value field `id` (whose slot is `field`, as given to [`notches`]) has keyboard
/// focus this frame, so the wheel is its next frame.
pub fn set_focused(ctx: &Context, id: Id, field: Id) {
ctx.data_mut(|d| d.insert_temp(focused_id(), (id, field)));
}
/// Wheel notches for the value field `field` in `rect` this frame (up positive, Shift ×10):
/// all of the wheel while it is `focused`, else a gesture that started with the pointer over it.
pub fn notches(ui: &Ui, field: Id, focused: bool, rect: Rect) -> f32 {
let events: Vec<Event> = if focused {
ui.data_mut(|d| d.remove_temp::<Vec<Event>>(taken_id())).unwrap_or_default()
} else {
if !ui.is_enabled() || !ui.rect_contains_pointer(rect) {
return 0.0;
}
let Some(mut g) = ui.data(|d| d.get_temp::<Gesture>(gesture_id())) else { return 0.0 };
let events: Vec<Event> = ui.input(|i| i.events.iter().filter(|e| for_field(e)).cloned().collect());
match g.owner {
Owner::New if !events.is_empty() => {
g.owner = Owner::Field(field);
ui.data_mut(|d| d.insert_temp(gesture_id(), g));
}
Owner::Field(id) if id == field => {}
_ => return 0.0,
}
// Nothing under the field scrolls with the wheel that steps it, smoothed tail included.
ui.ctx().input_mut(|i| i.smooth_scroll_delta = Vec2::ZERO);
events
};
let wheel: Vec<(MouseWheelUnit, Vec2, bool)> = events
.iter()
.filter_map(|e| match e {
Event::MouseWheel { unit, delta, modifiers, .. } if for_field(e) => Some((*unit, *delta, modifiers.shift)),
_ => None,
})
.collect();
crate::widgets::count_notches(ui, field.with("wheel-acc"), &wheel, 10.0)
}
#[cfg(test)]
mod tests {
use egui::accesskit::Role;
use egui::{Modifiers, Pos2, vec2};
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
/// The value, the scroll area's offset, and whether the field is enabled.
type State = (f32, f32, bool);
/// A value field (0–1000) at the top of a scroll area 100 pt tall, above a tall filler.
fn harness(value: f32) -> Harness<'static, State> {
let mut h = Harness::builder().with_size(vec2(300.0, 200.0)).build_ui_state(
|ui, s: &mut State| {
super::begin_frame(ui.ctx());
let out = egui::ScrollArea::vertical().max_height(100.0).show(ui, |ui| {
ui.add_enabled_ui(s.2, |ui| crate::widgets::value_field(ui, &mut s.0, 0.0..=1000.0, "px", 90.0));
ui.allocate_space(vec2(200.0, 1000.0));
});
s.1 = out.state.offset.y;
},
(value, 0.0, true),
);
h.run();
h
}
fn field(h: &Harness<'static, State>) -> egui::Rect {
h.get_by_role(Role::SpinButton).rect()
}
/// `lines` wheel lines (up positive) at `at`, then let the gesture end.
fn wheel(h: &mut Harness<'static, State>, at: Pos2, lines: f32, modifiers: Modifiers) {
h.hover_at(at);
h.run_steps(1);
h.event(egui::Event::MouseWheel { unit: egui::MouseWheelUnit::Line, delta: vec2(0.0, lines), phase: egui::TouchPhase::Move, modifiers });
h.run_steps(4);
}
#[test]
fn the_wheel_over_a_field_steps_it_and_scrolls_nothing() {
let mut h = harness(100.0);
let at = field(&h).center();
wheel(&mut h, at, 1.0, Modifiers::NONE);
assert_eq!(h.state().0, 101.0);
wheel(&mut h, at, -3.0, Modifiers::NONE);
assert_eq!(h.state().0, 98.0);
wheel(&mut h, at, 1.0, Modifiers::SHIFT);
assert_eq!(h.state().0, 108.0, "Shift: ten a notch");
assert_eq!(h.state().1, 0.0, "the panel under the field didn't scroll");
}
#[test]
fn a_focused_field_takes_the_wheel_wherever_the_pointer_is() {
let mut h = harness(100.0);
h.get_by_role(Role::SpinButton).click();
h.run();
let away = field(&h).center() + vec2(0.0, 60.0);
wheel(&mut h, away, -2.0, Modifiers::NONE);
assert_eq!(h.state().0, 98.0);
assert_eq!(h.state().1, 0.0, "the panel under the pointer didn't scroll");
assert!(h.get_by_role(Role::SpinButton).is_focused(), "the field keeps focus");
// Alt + wheel stays the view's (zoom), not the field's.
wheel(&mut h, away, 1.0, Modifiers::ALT);
assert_eq!(h.state().0, 98.0);
}
#[test]
fn a_gesture_that_starts_over_the_panel_keeps_scrolling_it_over_a_field() {
let mut h = harness(100.0);
let below = field(&h).center() + vec2(0.0, 40.0);
h.hover_at(below);
h.run_steps(1);
let down =
|| egui::Event::MouseWheel { unit: egui::MouseWheelUnit::Line, delta: vec2(0.0, -1.0), phase: egui::TouchPhase::Move, modifiers: Modifiers::NONE };
h.event(down());
h.run_steps(1);
// Still the same gesture as the pointer reaches a field.
h.hover_at(field(&h).center());
h.event(down());
h.run_steps(1);
assert_eq!(h.state().0, 100.0, "the field passing under the pointer is not stepped");
assert!(h.state().1 > 0.0, "the panel scrolled");
}
#[test]
fn a_disabled_field_ignores_the_wheel() {
let mut h = harness(100.0);
h.state_mut().2 = false;
h.run();
let at = field(&h).center();
wheel(&mut h, at, 1.0, Modifiers::NONE);
assert_eq!(h.state().0, 100.0);
}
#[test]
fn wild_wheel_deltas_leave_a_finite_value_in_range() {
// egui itself asserts on a non-finite wheel event in debug builds: `count_notches` is checked
// with those below.
for lines in [1e30, -1e30, 1e6, -1e6] {
for focus in [false, true] {
let mut h = harness(100.0);
if focus {
h.get_by_role(Role::SpinButton).click();
h.run();
}
let at = field(&h).center();
wheel(&mut h, at, lines, Modifiers::SHIFT);
let v = h.state().0;
assert!(v.is_finite() && (0.0..=1000.0).contains(&v), "{lines} (focus {focus}): {v}");
}
}
}
#[test]
fn notches_of_nan_or_infinite_deltas_are_none() {
use egui::MouseWheelUnit::{Line, Point};
let ctx = egui::Context::default();
let mut out = ctx.run_ui(Default::default(), |ui| {
let id = egui::Id::new("acc");
for d in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
for unit in [Line, Point] {
assert_eq!(crate::widgets::count_notches(ui, id, &[(unit, vec2(0.0, d), false)], 10.0), 0.0, "{d} {unit:?}");
// Nothing poisoned is carried over to the next notch.
assert_eq!(crate::widgets::count_notches(ui, id, &[(Line, vec2(0.0, 1.0), true)], 10.0), 10.0);
}
}
});
out.textures_delta.clear();
}
}
@@ -0,0 +1,73 @@
//! #5473 in the app: a focused value field takes the wheel over the canvas, which neither pans
//! nor zooms with it.
use egui::accesskit::Role;
use egui::vec2;
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use serde_json::json;
use crate::PhotocraftApp;
use crate::state::Tool;
fn harness() -> Harness<'static, PhotocraftApp> {
let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(move |cc| {
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
app.ui.tool = Tool::Brush;
app
});
h.run_steps(8);
h
}
/// The value field showing `value`: the options bar's Smoothing is the brush's only one at 10.
fn field<'a>(h: &'a Harness<'_, PhotocraftApp>, value: &str) -> egui_kittest::Node<'a> {
h.query_all_by_role(Role::SpinButton).find(|n| n.value().as_deref() == Some(value)).unwrap()
}
fn wheel(h: &mut Harness<'_, PhotocraftApp>, at: egui::Pos2, lines: f32, modifiers: egui::Modifiers) {
h.hover_at(at);
h.run_steps(1);
h.event(egui::Event::MouseWheel { unit: egui::MouseWheelUnit::Line, delta: vec2(0.0, lines), phase: egui::TouchPhase::Move, modifiers });
h.run_steps(6);
}
/// (zoom, centre) of the document's view.
fn view(h: &Harness<'_, PhotocraftApp>) -> (f32, [f32; 2]) {
let v = &h.state().ui.views[0];
(v.zoom, v.center)
}
#[test]
fn a_focused_field_takes_the_wheel_over_the_canvas() {
let mut h = harness();
let canvas = egui::pos2(640.0, 500.0);
let before = view(&h);
wheel(&mut h, canvas, -1.0, egui::Modifiers::NONE);
assert_ne!(view(&h), before, "without a focused field the wheel pans the canvas");
let before = view(&h);
field(&h, "10").click();
h.run();
wheel(&mut h, canvas, -2.0, egui::Modifiers::NONE);
assert_eq!(view(&h), before, "the canvas neither pans nor zooms");
assert!(h.query_all_by_role(Role::SpinButton).any(|n| n.value().as_deref() == Some("8")), "Smoothing stepped from 10 to 8");
// ⌥/Alt + wheel is still the view's zoom.
wheel(&mut h, canvas, 1.0, egui::Modifiers::ALT);
assert_ne!(view(&h).0, before.0, "Alt + wheel zooms");
assert!(h.query_all_by_role(Role::SpinButton).any(|n| n.value().as_deref() == Some("8")));
}
#[test]
fn the_wheel_over_a_field_steps_it_without_a_click() {
let mut h = harness();
let before = view(&h);
let at = field(&h, "10").rect().center();
wheel(&mut h, at, 1.0, egui::Modifiers::NONE);
assert!(h.query_all_by_role(Role::SpinButton).any(|n| n.value().as_deref() == Some("11")), "Smoothing stepped from 10 to 11");
wheel(&mut h, at, 1.0, egui::Modifiers::SHIFT);
assert!(h.query_all_by_role(Role::SpinButton).any(|n| n.value().as_deref() == Some("21")), "Shift: ten a notch");
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");
}
+6
View File
@@ -84,6 +84,7 @@ pub mod eraser_ui;
pub mod export_dialog;
pub mod eyedropper_ui;
pub mod field_tab;
pub mod field_wheel;
pub mod file_dialog;
pub mod file_open;
pub mod file_ui;
@@ -1806,6 +1807,8 @@ impl eframe::App for PhotocraftApp {
self.stroke_trace.cpu_prev(frame.info().cpu_usage);
// A text field's right-click menu, before any field is drawn (text_field_menu.rs).
text_field_menu::show(&ctx);
// The wheel goes to a focused value field before the canvas or a panel sees it (#5473).
field_wheel::begin_frame(&ctx);
// Each painting tool keeps its own brush (#218), so switch the active tool's brush in
// before anything this frame reads it (the cursor, the options bar, a stroke).
paint_mouse::sync_tool_brush(self);
@@ -2566,6 +2569,9 @@ mod blend_dropdown_wheel_tests;
#[cfg(test)]
mod options_bar_mode_wheel_tests;
#[cfg(test)]
mod field_wheel_app_tests;
#[cfg(test)]
mod dropdown_press_tests;
+39 -11
View File
@@ -529,7 +529,20 @@ fn value_field_box(
// Small ranges (gamma 0.01–9.99, 0–1 centres) need two decimals and a finer drag, like Photoshop.
let fine = range.end() - range.start() <= 10.0;
let (lo, hi) = (*range.start(), *range.end());
let (step, grid) = arrow_step(ui, slot.id, if fine { 0.01 } else { 1.0 });
// The field's id is only known once it's drawn: last frame's, saved under `slot` below.
let focused = ui.data(|d| d.get_temp::<egui::Id>(slot.id)).filter(|id| ui.memory(|m| m.has_focus(*id)));
let unit = if fine { 0.01 } else { 1.0 };
let (step, grid) = arrow_step(ui, slot.id, unit);
// The wheel steps it as the arrow keys do (#5473), focused or under the pointer.
let notches = crate::field_wheel::notches(ui, slot.id, focused.is_some(), rect);
if notches != 0.0
&& let Some(id) = focused
&& ui.data(|d| d.get_temp(id.with("arithmetic"))).unwrap_or(false)
{
// A typed sum works itself out first, as before an arrow step.
ui.memory_mut(|m| m.surrender_focus(id));
}
let step = step + notches * unit;
// new_child (not scope_builder): a scope would move the parent cursor back to the child rect.
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(field));
let mut resp = {
@@ -585,12 +598,22 @@ fn value_field_box(
// Round to 4 decimal places, so repeated 0.1 steps don't leave float errors.
let v = (v * 1e4).round() / 1e4;
// `f32::clamp` panics on a reversed or NaN range, which `DragValue::range` accepts.
*value = if lo <= hi { v.clamp(lo, hi) } else { v };
ui.memory_mut(|m| m.request_focus(resp.id));
// The text stays selected, as in Photoshop: typing after a step replaces the value.
crate::field_tab::select_all(ui.ctx(), resp.id, &if fine { fmt_num2(f64::from(*value)) } else { fmt_num(f64::from(*value)) });
let v = if lo <= hi { v.clamp(lo, hi) } else { v };
// A wild wheel delta must not leave infinity in the field.
if v.is_finite() {
*value = v;
}
// A wheel step over a field without focus leaves the focus where it is.
if focused.is_some() {
ui.memory_mut(|m| m.request_focus(resp.id));
// The text stays selected, as in Photoshop: typing after a step replaces the value.
crate::field_tab::select_all(ui.ctx(), resp.id, &if fine { fmt_num2(f64::from(*value)) } else { fmt_num(f64::from(*value)) });
}
resp.mark_changed();
}
if ui.memory(|m| m.has_focus(resp.id)) {
crate::field_wheel::set_focused(ui.ctx(), resp.id, slot.id);
}
(resp, rect)
}
@@ -860,11 +883,6 @@ pub(crate) fn wheel_notches_of(ui: &Ui, id: egui::Id, hovered: bool, shift_notch
if !hovered {
return 0.0;
}
let acc_id = id.with("wheel-acc");
let mut acc: f32 = ui.data(|d| d.get_temp(acc_id)).unwrap_or(0.0);
let per_line = ui.ctx().options(|o| o.input_options.line_scroll_speed);
let per_line = if per_line.is_finite() && per_line > 0.0 { per_line } else { 40.0 };
let mut notches = 0.0;
let wheel: Vec<(egui::MouseWheelUnit, Vec2, bool)> = ui.input(|i| {
i.events
.iter()
@@ -874,7 +892,17 @@ pub(crate) fn wheel_notches_of(ui: &Ui, id: egui::Id, hovered: bool, shift_notch
})
.collect()
});
for (unit, delta, shift) in wheel {
count_notches(ui, id.with("wheel-acc"), &wheel, shift_notches)
}
/// Whole notches in `wheel` (unit, delta, Shift held), a notch with Shift counting
/// `shift_notches`; fractions carry over in egui memory under `acc_id`.
pub(crate) fn count_notches(ui: &Ui, acc_id: egui::Id, wheel: &[(egui::MouseWheelUnit, Vec2, bool)], shift_notches: f32) -> f32 {
let mut acc: f32 = ui.data(|d| d.get_temp(acc_id)).unwrap_or(0.0);
let per_line = ui.ctx().options(|o| o.input_options.line_scroll_speed);
let per_line = if per_line.is_finite() && per_line > 0.0 { per_line } else { 40.0 };
let mut notches = 0.0;
for &(unit, delta, shift) in wheel {
// Some systems turn Shift+wheel into a horizontal scroll.
let d = if delta.y != 0.0 { delta.y } else { delta.x };
acc += match unit {
+3 -1
View File
@@ -1,6 +1,6 @@
# UI parity: tools, handles, modifiers and feel
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (smart filter reordering by drag; smart object perspective row corrected to done, #426; Free Transform H/V skew fields; toolbar tool shortcuts can be reassigned; ⇧-dragged guides land on ruler ticks; layer and selection keys; tool keys; spring-loaded tool letters; type formatting keys; Pen Auto Add/Delete, #4144; smart filter rows; stale missing-tool rows corrected: Color Replacement, Red Eye and the Add/Delete Anchor Point pair are live toolbar tools, Frame landed; no percentage changed) · **Target:** Adobe Photoshop 2026
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-14 · **Change:** minor (the wheel steps value fields, #5473; smart filter reordering by drag; smart object perspective row corrected to done, #426; Free Transform H/V skew fields; toolbar tool shortcuts can be reassigned; ⇧-dragged guides land on ruler ticks; layer and selection keys; tool keys; spring-loaded tool letters; type formatting keys; Pen Auto Add/Delete, #4144; smart filter rows; stale missing-tool rows corrected: Color Replacement, Red Eye and the Add/Delete Anchor Point pair are live toolbar tools, Frame landed; no percentage changed) · **Target:** Adobe Photoshop 2026
How PhotoCraft's direct manipulation compares with Photoshop 2026 (27.11.0): the toolbox, the
handles and modifier keys of each gesture, nudging, snapping, numeric precision, navigation,
@@ -156,6 +156,7 @@ Triangle and Curvature Pen (minus legacy Rounded Rectangle and Targeted Adjustme
| Guides stored as f64 | missing | `Vec<f32>` (UI-217-20) |
| Geometry precision | partial | `geom::Point` f64, paths f64; paint internals mostly f32 |
| Numeric fields: arithmetic, scrubby labels | partial | units in expressions and double-click reset missing (UI-217-13) |
| Wheel steps a value field, ⇧ ×10: clicked into, wherever the pointer is; not clicked, a gesture that starts over it | partial | `field_wheel.rs` (#5473, #4061). Photoshop: a field takes the wheel once clicked into (shortcut guides, user reports); whether the pointer must stay over it is unconfirmed. Stepping on hover alone is Photopea's, kept deliberately |
## Navigation
@@ -217,6 +218,7 @@ Paragraph; the rest open as in-app floating windows.
| Date | Change | Summary |
|---|---|---|
| 2026-10-14 | minor | The wheel steps value fields: a focused field takes it wherever the pointer is (the canvas and panels don't scroll), an unfocused one only a gesture that starts over it (#5473, #4061); no percentage changed |
| 2026-10-13 | minor | Stale "missing tool" rows corrected against main: Color Replacement (`paint.colorReplacement`), Red Eye (`paint.redEye`) and Add/Delete Anchor Point (the P flyout cycle) are live toolbar tools, so only Art History Brush is missing; no percentage or estimate changed |
| 2026-10-13 | minor | Smart object perspective (non-affine) corrected to done: #426 landed four-corner placements with PSD round trip; scorecard FILE-215-6 flipped |
| 2026-10-10 | major | First version, from a source audit against Photoshop 2026 27.11.0's shortcuts file and the open issues |