From d0b24e1abdad2e04eef2d7fe7ad413e9a70bccb8 Mon Sep 17 00:00:00 2001 From: kirubhakaran12 <27127513+kirubhakaran12@users.noreply.github.com> Date: Sat, 7 Nov 2026 06:54:23 +0530 Subject: [PATCH 1/4] Round the Magic Wand's Tolerance field to whole numbers The wand's Tolerance took a plain value_field over an f32 option, so typing 12.5 stored 12.5 while the wand floored it to 12: the field showed a number the tool did not use. A decimal written with the interface language's comma (12,5) was rejected outright, and the old value stayed. Adds FieldOpts::integer and widgets::value_field_int: the field rounds the number its parser reads, so the value it holds while the number is typed is the one it keeps and the DragValue keeps the typed text, and it rounds whatever value another writer leaves behind (a drag, a preference file). read() takes the decimal comma as a decimal point, as the zoom fields do. The wand's Tolerance uses it. The Paint Bucket's field on the same option is untouched, and the drag and arrow-key step stays 1. Fixes #11786 Co-Authored-By: Claude Code --- crates/ui-egui/src/lib.rs | 2 + crates/ui-egui/src/panels/options_bar.rs | 4 +- .../ui-egui/src/wand_tolerance_field_tests.rs | 166 ++++++++++++++++++ crates/ui-egui/src/widgets.rs | 4 +- crates/ui-egui/src/widgets/fields.rs | 24 ++- crates/ui-egui/src/widgets/units.rs | 8 + 6 files changed, 204 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/wand_tolerance_field_tests.rs diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 7fd35442cc..9eff0f1e72 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -969,6 +969,8 @@ mod export_quality_tests; mod options_hit_area_tests; #[cfg(test)] mod select_and_mask_button_tests; +#[cfg(test)] +mod wand_tolerance_field_tests; #[cfg(test)] mod saved_path_edit_tests; diff --git a/crates/ui-egui/src/panels/options_bar.rs b/crates/ui-egui/src/panels/options_bar.rs index 15c602ae99..43627e54bf 100644 --- a/crates/ui-egui/src/panels/options_bar.rs +++ b/crates/ui-egui/src/panels/options_bar.rs @@ -382,7 +382,9 @@ fn selection_specific_options(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: selection_mode_buttons(app, ui); widgets::vline(ui, 22.0); opt_label(ui, tl!("Tolerance")); - widgets::value_field(ui, &mut app.ui.tool_options.tolerance, 0.0..=255.0, "", 58.0); + // The wand reads a whole-number tolerance (#11786): the field rounds a typed decimal + // and never keeps a fraction, so it shows the number the wand uses. + widgets::value_field_int(ui, &mut app.ui.tool_options.tolerance, 0.0..=255.0, "", 58.0); widgets::checkbox(ui, &mut app.ui.tool_options.anti_alias, tl!("Anti-alias")); widgets::checkbox(ui, &mut app.ui.tool_options.contiguous, tl!("Contiguous")); widgets::checkbox(ui, &mut app.ui.tool_options.sample_all.magic_wand, tl!("Sample All Layers")); diff --git a/crates/ui-egui/src/wand_tolerance_field_tests.rs b/crates/ui-egui/src/wand_tolerance_field_tests.rs new file mode 100644 index 0000000000..5727400e1a --- /dev/null +++ b/crates/ui-egui/src/wand_tolerance_field_tests.rs @@ -0,0 +1,166 @@ +//! #11786: the Magic Wand's Tolerance is a whole number 0–255, and the wand floors what it is +//! given (`wand_region`: `(tolerance + 1e-3).floor()`). A field that keeps a fraction therefore +//! shows a number the tool does not use, and a decimal typed with the decimal comma (the UI +//! language's separator) is rejected outright. Typing `12.5` or `12,5` must leave 13 behind, so the +//! field and the wand agree; 300 and -5 clamp, as today. + +use egui::accesskit::Role; +use egui::{PointerButton, Pos2, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Document, Layer, LayerContent}; +use photocraft_geom::{Rect, Size}; +use photocraft_raster::Surface; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::Tool; + +/// The Magic Wand's options bar, with a document open, after a few settled frames. +fn wand_bar() -> Harness<'static, PhotocraftApp> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::MagicWand; + let mut h = Harness::builder().with_size(vec2(1000.0, 60.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + crate::panels::options_bar(app, ui); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.run_steps(4); + h +} + +/// The number the Tolerance field shows, as a person reads it off the screen. +fn shown_tolerance(h: &Harness<'_, PhotocraftApp>) -> f32 { + let node = h.get_by_role(Role::SpinButton); + node.accesskit_node().value().unwrap_or_default().parse().unwrap_or(f32::NAN) +} + +/// Types `text` over the field's number and presses Enter, as a person would, and returns the +/// option the edit left behind. +fn type_tolerance(h: &mut Harness<'_, PhotocraftApp>, text: &str) -> f32 { + h.get_by_role(Role::SpinButton).click(); + h.run(); + h.event(egui::Event::Text(text.into())); + h.run_steps(2); + h.key_press(egui::Key::Enter); + h.run_steps(2); + h.state().ui.tool_options.tolerance +} + +/// A typed decimal rounds to the nearest whole number, whichever separator it was typed with: the +/// wand's option is never a fraction for it to floor differently from what the field shows. +#[test] +fn typed_decimals_round_to_the_nearest_whole_number() { + for (text, want) in [("12.5", 13.0), ("12,5", 13.0), ("12.4", 12.0), ("12.6", 13.0)] { + let mut h = wand_bar(); + assert_eq!(type_tolerance(&mut h, text), want, "typing {text}"); + assert_eq!(shown_tolerance(&h), want, "the field shows the number it kept ({text})"); + } +} + +/// A typed number outside the range clamps, as in any value field. +#[test] +fn typed_values_outside_the_range_clamp() { + for (text, want) in [("300", 255.0), ("-5", 0.0)] { + let mut h = wand_bar(); + assert_eq!(type_tolerance(&mut h, text), want, "typing {text}"); + assert_eq!(shown_tolerance(&h), want, "the field shows the clamped number ({text})"); + } +} + +/// The Paint Bucket's plain field shares this option and keeps today's behaviour, and a restored +/// preference file can hold a fraction: the wand's field takes whatever it finds as the whole +/// number it shows, so the wand can never floor a value the field is displaying. +#[test] +fn a_fractional_option_becomes_the_whole_number_the_field_shows() { + let mut h = wand_bar(); + h.state_mut().ui.tool_options.tolerance = 12.5; + h.run_steps(2); + assert_eq!(h.state().ui.tool_options.tolerance, 13.0); + assert_eq!(shown_tolerance(&h), 13.0); +} + +/// Six white pixels whose second one is 13 levels darker: inside the wand's reach at Tolerance 13, +/// outside it at 12, so one click tells the two numbers apart. +fn wand_canvas() -> Harness<'static, PhotocraftApp> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + let mut doc = Document::new("Wand", Size::new(6, 1), ColorMode::Rgb, SampleType::U8); + let mut pixels = Surface::with_default(doc.pixel_format(), &[1.0, 1.0, 1.0, 1.0]); + pixels.write_pixel(1, 0, &[242.0 / 255.0, 242.0 / 255.0, 242.0 / 255.0, 1.0]); + doc.layers.push(Layer::new("Layer 1", LayerContent::Raster(pixels))); + let layer = doc.layers[0].id; + app.session.add_document(doc, None); + app.session.select_layer(layer).unwrap(); + app.ui.tool = Tool::MagicWand; + app.ui.extras.rulers = false; + app.sync_views(); + let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let ctx = ui.ctx().clone(); + if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + // The options bar is a panel of its own; the canvas takes the height that is left. + crate::panels::options_bar(app, ui); + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.run_steps(4); + // 40× zoom on a one-pixel-tall document: the pixels are 40 pt apart and easy to hit. + let v = &mut h.state_mut().ui.views[0]; + v.zoom = 40.0; + v.center = [3.0, 0.5]; + v.fit_pending = false; + h.run_steps(2); + h +} + +/// Where document point `(x, y)` lands on screen, as the canvas' own transform maps it. +fn screen(h: &Harness<'static, PhotocraftApp>, x: f32, y: f32) -> Pos2 { + let app = h.state(); + let v = &app.ui.views[0]; + crate::canvas::ViewXform { + rect: crate::rulers::content_rect(app, app.last_canvas_rect), + zoom: v.zoom, + center: v.center, + flip: app.ui.view.flip_horizontal, + rotation: v.rotation, + aspect: app.ui.view.display_aspect(), + } + .to_screen(x, y) +} + +/// Starts from `option` in the shared tool state, clicks the first pixel with the Magic Wand, and +/// returns the area selected. +fn wand_click(option: f32) -> Rect { + let mut h = wand_canvas(); + h.state_mut().ui.tool_options.tolerance = option; + h.run_steps(2); + let p = screen(&h, 0.5, 0.5); + h.event(egui::Event::PointerMoved(p)); + h.run(); + for pressed in [true, false] { + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }); + h.run_steps(2); + } + h.state().session.active().unwrap().doc.selection.as_ref().map_or(Rect::EMPTY, |s| s.content_bounds()) +} + +/// The wand selects by the whole number the field holds, so a typed `12.5` selects what 13 selects, +/// not what the 12 it would have floored to selects. +#[test] +fn the_wand_selects_by_the_number_the_field_shows() { + let (at_12, at_13) = (wand_click(12.0), wand_click(13.0)); + assert_ne!(at_12, at_13, "Tolerance 12 and 13 must select different areas for this test to mean anything"); + assert_eq!(wand_click(12.5), at_13, "a typed 12.5 gives the 13 the field shows"); +} diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 3b4320dd0d..f95db27020 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -68,8 +68,8 @@ pub use text_field::{ INPUT_EVENT_LIMIT, TEXT_FIELD_H, TEXT_FIELD_LIMIT, cap_text_events, focus_first_field, text_field, text_field_ex, text_field_with_trailing, }; pub use units::{ - FieldUnits, fmt_num, fmt_num_prec, fmt_num2, parse_num, set_field_resolution, unit_decimals, unit_of_key, value_field_extent, value_field_len, - value_field_prec, value_field_units, + FieldUnits, fmt_num, fmt_num_prec, fmt_num2, parse_num, set_field_resolution, unit_decimals, unit_of_key, value_field_extent, value_field_int, + value_field_len, value_field_prec, value_field_units, }; #[cfg(test)] diff --git a/crates/ui-egui/src/widgets/fields.rs b/crates/ui-egui/src/widgets/fields.rs index c0030bd9c8..7be30f442c 100644 --- a/crates/ui-egui/src/widgets/fields.rs +++ b/crates/ui-egui/src/widgets/fields.rs @@ -71,6 +71,11 @@ pub(super) struct FieldOpts { /// keystroke (the zoom fields: typing `150` over `50` must not zoom to 1 % on the way). Esc, /// Tab and Alt do not apply it (`zoom_field`). pub(super) hold_typed: bool, + /// Whole numbers only ([`super::value_field_int`], #11786): a typed decimal rounds to the + /// nearest one, `12.5` and `12,5` both giving `13`, and the value the field holds is never a + /// fraction, whichever path wrote it (a drag, another field on the same option, a preference + /// file). The number the field shows is therefore the number its caller reads. + pub(super) integer: bool, } /// Arrow-key and drag step for a value field: [`FieldOpts::step_from_decimals`] uses `decimals`; @@ -400,7 +405,7 @@ fn number_edit( units: crate::numeric_expression::Units, opts: FieldOpts, ) -> Response { - let FieldOpts { decimals, wrap_drag, hold_typed, .. } = opts; + let FieldOpts { decimals, wrap_drag, hold_typed, integer, .. } = opts; let (id, ctx) = (ui.next_auto_id(), ui.ctx().clone()); // Focused before the DragValue is drawn, so it gains focus this frame and selects its text. if ui.data_mut(|d| d.remove_temp::(first_field_id())).unwrap_or(false) { @@ -487,11 +492,22 @@ fn number_edit( return lead.and_then(|lead| zoom_number(&lead, units)); } } + } else if integer { + // A whole-number field takes the decimal comma too (`12,5` is `12.5`); two + // separators (`1,5,2`) are not a number, as in any other field. + match crate::zoom_field::read(s) { + crate::zoom_field::Entry::Number(ready) => ready, + crate::zoom_field::Entry::Invalid(_) => return None, + } } else { s.to_owned() }; let s = ready.as_str(); let v = if hold_typed { zoom_number(s, units) } else { parse_num_in(s, units) }; + // A whole-number field already rounds what it reads here, in the parser: the value + // it holds while the number is being typed is then the one it keeps, where rounding + // it after the DragValue wrote it down would rewrite the text under the typist. + let v = if integer { v.map(|v| v.round()) } else { v }; if ctx.memory(|m| m.has_focus(id)) && plain(s).is_none() { // Still typing arithmetic: hold it, and stop applying keystrokes once it parses. ctx.data_mut(|d| d.insert_temp(held, v.is_some())); @@ -520,6 +536,12 @@ fn number_edit( if *value != before && lo <= hi { *value = value.clamp(lo, hi); } + // A whole-number field keeps whole numbers whatever wrote one: a drag between two values, a + // plain field on the same option, a preference file. The number it shows is then the number + // its caller reads, and rounding a whole number changes nothing. + if integer && lo <= hi && value.is_finite() && *value != value.round() { + *value = value.round().clamp(lo, hi); + } resp.flags.set(egui::response::Flags::CHANGED, *value != before); resp } diff --git a/crates/ui-egui/src/widgets/units.rs b/crates/ui-egui/src/widgets/units.rs index d8da90b5fb..28a7c1691f 100644 --- a/crates/ui-egui/src/widgets/units.rs +++ b/crates/ui-egui/src/widgets/units.rs @@ -28,6 +28,14 @@ pub fn value_field_prec(ui: &mut Ui, value: &mut f32, range: std::ops::RangeIncl value_field_in(ui, value, range, suffix, width, 0.0, FieldOpts { decimals: Some(decimals), ..Default::default() }, None).0 } +/// A [`value_field`] of whole numbers with no decimal places: the Magic Wand's Tolerance, an +/// integer 0–255 that its tool floors (#11786). A typed decimal rounds to the nearest whole number, +/// `12.5` and `12,5` both giving `13`, and the value the field holds is never a fraction, so the +/// number it shows is the number the tool uses. Out-of-range entries clamp, as in any value field. +pub fn value_field_int(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive, suffix: &str, width: f32) -> Response { + value_field_in(ui, value, range, suffix, width, 0.0, FieldOpts { decimals: Some(0), integer: true, ..Default::default() }, None).0 +} + /// A field's unit and the resolution a typed unit converts at (`1in` in a px field). #[derive(Clone, Copy, Debug, PartialEq)] pub struct FieldUnits { From 26401a90df65cbf84ddf0bdc332410d4ebe4ecbb Mon Sep 17 00:00:00 2001 From: kirubhakaran12 <27127513+kirubhakaran12@users.noreply.github.com> Date: Sat, 7 Nov 2026 07:13:05 +0530 Subject: [PATCH 2/4] Credit kirubhakaran12 by their GitHub profile name Adds their own contributors/people.toml entry, asking About to use the name on their public GitHub profile (sync_github_name = true) rather than the username alone. --- contributors/people.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contributors/people.toml b/contributors/people.toml index a2ead57598..20b8aabd8c 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -315,3 +315,6 @@ sync_github_name = false [people.davidvhk] real_name = "David Vanhoucke" sync_github_name = true + +[people.kirubhakaran12] +sync_github_name = true From 1427d15e7cadce4c139b4d58ffe01979300481d2 Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 11 Nov 2026 13:57:34 +0100 Subject: [PATCH 3/4] PR #11825: decimal operands in the Tolerance field's arithmetic The whole-number field checks each operand for a second decimal separator instead of the whole entry, so 1.5+2.5 adds up to 4 while 1,5,2 is still rejected. Co-Authored-By: kirubhakaran12 Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/wand_tolerance_field_tests.rs | 11 +++++++++++ crates/ui-egui/src/widgets/fields.rs | 12 +++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/wand_tolerance_field_tests.rs b/crates/ui-egui/src/wand_tolerance_field_tests.rs index 5727400e1a..1681f7952c 100644 --- a/crates/ui-egui/src/wand_tolerance_field_tests.rs +++ b/crates/ui-egui/src/wand_tolerance_field_tests.rs @@ -66,6 +66,17 @@ fn typed_decimals_round_to_the_nearest_whole_number() { } } +/// Each operand of a typed expression is its own number: `1.5+2.5` adds up to 4, while a number +/// with two separators (`1,5,2`) is not one and leaves the option as it was. +#[test] +fn typed_expressions_take_decimal_operands() { + let mut h = wand_bar(); + assert_eq!(type_tolerance(&mut h, "1.5+2.5"), 4.0, "typing 1.5+2.5"); + let mut h = wand_bar(); + let before = h.state().ui.tool_options.tolerance; + assert_eq!(type_tolerance(&mut h, "1,5,2"), before, "typing 1,5,2"); +} + /// A typed number outside the range clamps, as in any value field. #[test] fn typed_values_outside_the_range_clamp() { diff --git a/crates/ui-egui/src/widgets/fields.rs b/crates/ui-egui/src/widgets/fields.rs index c32fd8fe56..cca8c9bc8c 100644 --- a/crates/ui-egui/src/widgets/fields.rs +++ b/crates/ui-egui/src/widgets/fields.rs @@ -558,12 +558,14 @@ fn number_edit( } } } else if integer { - // A whole-number field takes the decimal comma too (`12,5` is `12.5`); two - // separators (`1,5,2`) are not a number, as in any other field. - match crate::zoom_field::read(s) { - crate::zoom_field::Entry::Number(ready) => ready, - crate::zoom_field::Entry::Invalid(_) => return None, + // A whole-number field takes the decimal comma too (`12,5` is `12.5`); a number + // with two separators (`1,5,2`) is not one, as in any other field. Each operand + // of an expression is its own number, so `1.5+2.5` still adds up. + let is_sep = |c: char| matches!(c, '.' | ','); + if s.split(|c: char| !(c.is_ascii_digit() || is_sep(c))).any(|n| n.chars().filter(|&c| is_sep(c)).count() > 1) { + return None; } + s.replace(',', ".") } else { s.to_owned() }; From 36f3e7ec68cb614e1608f11a093ba1ff91012f26 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sat, 14 Nov 2026 00:13:26 +0100 Subject: [PATCH 4/4] PR #11825: Command+arrow leaves the whole-number Tolerance as it is Ctrl/Cmd+Up/Down stepped the integer field by 0.1, writing 12.9 into the option for one pass before the field rounded it back to 13. The reference application's Magic Wand Tolerance keeps its value on Ctrl+arrow, so a whole-number field now has no fine step; plain and Shift arrows are unchanged. Resolves the CodeRabbit thread on widgets/fields.rs. Co-Authored-By: kirubhakaran12 Co-Authored-By: Claude Opus 5.5 --- .../ui-egui/src/wand_tolerance_field_tests.rs | 28 +++++++++++++++++++ crates/ui-egui/src/widgets.rs | 4 +-- crates/ui-egui/src/widgets/fields.rs | 13 ++++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/wand_tolerance_field_tests.rs b/crates/ui-egui/src/wand_tolerance_field_tests.rs index 1681f7952c..32ad9917e0 100644 --- a/crates/ui-egui/src/wand_tolerance_field_tests.rs +++ b/crates/ui-egui/src/wand_tolerance_field_tests.rs @@ -87,6 +87,34 @@ fn typed_values_outside_the_range_clamp() { } } +/// The reference application has no 0.1 step for this whole number: Ctrl/Cmd+arrow leaves 13 as +/// 13 on every pass, instead of writing 12.9 for the next pass to round back. A plain arrow still +/// steps by one. +#[test] +fn command_arrows_leave_the_whole_number_as_it_is() { + // The field's value after every pass, so a fraction that only lives until the next pass shows. + // A bare field has no caption to name it; the options bar gives it one. + let mut h = Harness::builder().with_size(vec2(200.0, 40.0)).with_accessibility_check(false).build_ui_state( + |ui, (value, seen): &mut (f32, Vec)| { + crate::widgets::value_field_int(ui, value, 0.0..=255.0, "", 58.0); + seen.push(*value); + }, + (13.0_f32, Vec::new()), + ); + h.run(); + h.get_by_role(Role::SpinButton).click(); + h.run(); + for key in [egui::Key::ArrowDown, egui::Key::ArrowDown, egui::Key::ArrowUp] { + h.state_mut().1.clear(); + h.key_press_modifiers(egui::Modifiers::COMMAND, key); + h.run_steps(2); + assert!(h.state().1.iter().all(|&v| v == 13.0), "after Command+{key:?}: {:?}", h.state().1); + } + h.key_press(egui::Key::ArrowUp); + h.run_steps(2); + assert_eq!(h.state().0, 14.0, "a plain arrow still steps by one"); +} + /// The Paint Bucket's plain field shares this option and keeps today's behaviour, and a restored /// preference file can hold a fraction: the wand's field takes whatever it finds as the whole /// number it shows, so the wand can never floor a value the field is displaying. diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index de83de1931..d678d5ce15 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -74,8 +74,8 @@ pub use text_field::{ INPUT_EVENT_LIMIT, TEXT_FIELD_H, TEXT_FIELD_LIMIT, cap_text_events, focus_first_field, text_field, text_field_ex, text_field_with_trailing, }; pub use units::{ - FieldUnits, fmt_num, fmt_num_prec, fmt_num2, parse_num, set_field_resolution, unit_decimals, unit_of_key, value_field_extent, value_field_fixed, value_field_int, - value_field_len, value_field_prec, value_field_prec_blank, value_field_units, + FieldUnits, fmt_num, fmt_num_prec, fmt_num2, parse_num, set_field_resolution, unit_decimals, unit_of_key, value_field_extent, value_field_fixed, + value_field_int, value_field_len, value_field_prec, value_field_prec_blank, value_field_units, }; #[cfg(test)] diff --git a/crates/ui-egui/src/widgets/fields.rs b/crates/ui-egui/src/widgets/fields.rs index 443d909649..2014185077 100644 --- a/crates/ui-egui/src/widgets/fields.rs +++ b/crates/ui-egui/src/widgets/fields.rs @@ -210,7 +210,7 @@ fn value_field_box( opts: FieldOpts, name: Option<&str>, ) -> (Response, Rect) { - let FieldOpts { decimals, step_from_decimals, units, extent, .. } = opts; + let FieldOpts { decimals, step_from_decimals, units, extent, integer, .. } = opts; let t = Tokens::get(ui.ctx()); let (rect, slot) = ui.allocate_exact_size(size, Sense::hover()); let last_editor = ui.data(|d| d.get_temp::(slot.id)); @@ -261,7 +261,7 @@ fn value_field_box( let (lo, hi) = (*range.start(), *range.end()); // 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::(slot.id)).filter(|id| ui.memory(|m| m.has_focus(*id))); - let (step, grid) = arrow_step(ui, slot.id, unit); + let (step, grid) = arrow_step(ui, slot.id, unit, integer); // 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 @@ -693,8 +693,10 @@ fn zoom_number(text: &str, units: crate::numeric_expression::Units) -> Option (f32, f32) { +/// Returns the amount to add, and what to round the value to first. A whole-number field has no +/// 0.1 step: Ctrl/Cmd+arrow leaves its value as it is, as the reference application's Magic Wand +/// Tolerance does, instead of writing a fraction the field rounds back next frame (#11786). +fn arrow_step(ui: &mut Ui, slot: egui::Id, step: f32, integer: bool) -> (f32, f32) { use egui::{Key, Modifiers}; // The field's id is only known once it's drawn, so `value_field` saves it under `slot` for the next frame. let Some(id) = ui.data(|d| d.get_temp::(slot)).filter(|id| ui.memory(|m| m.has_focus(*id))) else { @@ -706,6 +708,9 @@ fn arrow_step(ui: &mut Ui, slot: egui::Id, step: f32) -> (f32, f32) { for (mods, k) in [(Modifiers::COMMAND, (step / 10.0).max(0.01)), (Modifiers::SHIFT, 10.0 * step), (Modifiers::NONE, step)] { let presses = i.count_and_consume_key(mods, Key::ArrowUp) as f32 - i.count_and_consume_key(mods, Key::ArrowDown) as f32; if presses != 0.0 && mods == Modifiers::COMMAND { + if integer { + continue; + } grid = k; } n += k * presses;