diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index d159f8f217..7e6cc44d0c 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1017,6 +1017,9 @@ mod warp_text_dialog_tests; #[cfg(test)] mod options_bar_mode_wheel_tests; +#[cfg(test)] +mod options_bar_percent_arrow_repeat_tests; + #[cfg(test)] mod field_wheel_app_tests; diff --git a/crates/ui-egui/src/options_bar_percent_arrow_repeat_tests.rs b/crates/ui-egui/src/options_bar_percent_arrow_repeat_tests.rs new file mode 100644 index 0000000000..5695caa309 --- /dev/null +++ b/crates/ui-egui/src/options_bar_percent_arrow_repeat_tests.rs @@ -0,0 +1,123 @@ +//! #16806: Up and Down in the painting tools' % fields (Opacity, Flow, Smoothing; the Mixer Brush's +//! Wet, Load, Mix and Flow) step on every press. The brush keeps these as `f32` fractions, so 0.51 +//! reads back as 50.999999..., which the field took for a value off its grid and stepped to 51 again. + +use egui::accesskit::Role; +use egui::{Key, Modifiers, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::Tool; + +/// The options bar of a new document with `tool`. +fn bar(tool: Tool) -> Harness<'static, Box> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 80, "height": 80})).unwrap(); + app.ui.tool = tool; + crate::paint_mouse::sync_tool_brush(&mut app); + crate::theme::install_fonts(app.ctx()); + crate::theme::apply(app.ctx(), &crate::theme::AppTheme::pro()); + let mut h = Harness::builder().with_context(app.ctx().clone()).with_size(vec2(2400.0, 300.0)).with_step_dt(1.0 / 60.0).build_ui_state( + move |ui, app: &mut Box| { + crate::panels::options_bar(app, ui); + }, + app, + ); + h.run_steps(4); + h +} + +/// The field named `label`, with or without its caption's colon. +fn field<'a>(h: &'a Harness<'static, Box>, label: &str) -> egui_kittest::Node<'a> { + h.query_all_by_role(Role::SpinButton) + .find(|n| n.accesskit_node().label().is_some_and(|l| l.trim_end_matches(':') == label)) + .unwrap_or_else(|| panic!("the {label} field")) +} + +/// The value the field named `label` holds, to a billionth (the field's own grid tolerance), so +/// 50.999999 (an `f32` read as it is) stays apart from 51. +fn shown(h: &Harness<'static, Box>, label: &str) -> f64 { + let value = field(h, label).accesskit_node().numeric_value().unwrap_or_else(|| panic!("the {label} field's value")); + (value * 1e9).round() / 1e9 +} + +/// Clicks into the field named `label`, types `text` over it and presses Enter; the focus stays in it. +fn type_into(h: &mut Harness<'static, Box>, label: &str, text: &str) { + let at = field(h, label).rect().center(); + h.hover_at(at); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(1); + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.event(egui::Event::Text(text.into())); + h.run_steps(1); + h.key_press(Key::Enter); + h.run_steps(3); +} + +/// Presses `key` with `mods` `times` times, letting the bar redraw from the stored value in between. +fn press(h: &mut Harness<'static, Box>, key: Key, mods: Modifiers, times: usize) { + for _ in 0..times { + h.key_press_modifiers(mods, key); + h.run_steps(3); + } +} + +/// From 50, Up x3 shows 53 and Down x3 (from 50 again) shows 47 in the field named `label`. +fn steps_every_press(tool: Tool, label: &str) { + let mut h = bar(tool); + type_into(&mut h, label, "50"); + assert_eq!(shown(&h, label), 50.0, "{tool:?} {label} typed 50"); + press(&mut h, Key::ArrowUp, Modifiers::NONE, 3); + assert_eq!(shown(&h, label), 53.0, "{tool:?} {label}: 50, Up x3"); + type_into(&mut h, label, "50"); + press(&mut h, Key::ArrowDown, Modifiers::NONE, 3); + assert_eq!(shown(&h, label), 47.0, "{tool:?} {label}: 50, Down x3"); +} + +#[test] +fn brush_opacity_flow_and_smoothing_step_on_every_press() { + for label in ["Opacity", "Flow", "Smoothing"] { + steps_every_press(Tool::Brush, label); + } +} + +#[test] +fn pencil_and_eraser_percent_fields_step_on_every_press() { + steps_every_press(Tool::Pencil, "Opacity"); + steps_every_press(Tool::Pencil, "Smoothing"); + steps_every_press(Tool::Eraser, "Opacity"); + steps_every_press(Tool::Eraser, "Flow"); +} + +#[test] +fn mixer_brush_percent_fields_step_on_every_press() { + for label in ["Wet", "Load", "Mix", "Flow"] { + steps_every_press(Tool::MixerBrush, label); + } +} + +#[test] +fn shift_up_steps_ten_on_every_press() { + let mut h = bar(Tool::Brush); + type_into(&mut h, "Opacity", "50"); + press(&mut h, Key::ArrowUp, Modifiers::SHIFT, 2); + assert_eq!(shown(&h, "Opacity"), 70.0, "50, Shift+Up x2"); + assert_eq!((h.state().session.tools.brush.opacity * 100.0).round(), 70.0, "the brush holds 70 %"); +} + +#[test] +fn an_f32_opacity_of_0_51_steps_up_twice_to_53() { + let mut h = bar(Tool::Brush); + type_into(&mut h, "Opacity", "40"); + h.state_mut().session.tools.brush.opacity = 0.51_f32; + h.run_steps(3); + assert_eq!(shown(&h, "Opacity"), 51.0); + press(&mut h, Key::ArrowUp, Modifiers::NONE, 2); + assert_eq!(shown(&h, "Opacity"), 53.0, "0.51 (f32), Up x2"); + assert_eq!((h.state().session.tools.brush.opacity * 100.0).round(), 53.0, "the brush holds 53 %"); +} diff --git a/crates/ui-egui/src/panels/options_bar/paint.rs b/crates/ui-egui/src/panels/options_bar/paint.rs index 765873a3df..43ec7d3b29 100644 --- a/crates/ui-egui/src/panels/options_bar/paint.rs +++ b/crates/ui-egui/src/panels/options_bar/paint.rs @@ -113,10 +113,10 @@ fn paint_values(app: &PhotocraftApp, tool: Tool, b: &BrushSettings) -> PaintValu PaintMode::Blend(b.mode) }; let mut v = PaintValues::new(mode); - v.opacity = f64::from(b.opacity); - v.flow = f64::from(b.flow); + v.opacity = fraction(b.opacity); + v.flow = fraction(b.flow); let mut smoothing = Smoothing::default(); - smoothing.amount = f64::from(b.smoothing.amount); + smoothing.amount = fraction(b.smoothing.amount); smoothing.pulled_string = b.smoothing.pulled_string; smoothing.catch_up = b.smoothing.catch_up; smoothing.catch_up_on_end = b.smoothing.catch_up_on_end; @@ -130,10 +130,10 @@ fn paint_values(app: &PhotocraftApp, tool: Tool, b: &BrushSettings) -> PaintValu let mut mixer = MixerValues::default(); mixer.load_after_stroke = b.mixer.load_after_stroke; mixer.clean_after_stroke = b.mixer.clean_after_stroke; - mixer.wet = f64::from(b.mixer.wet); - mixer.load = f64::from(b.mixer.load); - mixer.mix = f64::from(b.mixer.mix); - mixer.flow = f64::from(b.mixer.flow); + mixer.wet = fraction(b.mixer.wet); + mixer.load = fraction(b.mixer.load); + mixer.mix = fraction(b.mixer.mix); + mixer.flow = fraction(b.mixer.flow); mixer.sample_all_layers = b.mixer.sample_all_layers; mixer.combination = o.mixer_combination; mixer.current_load = mixer_load(app); @@ -142,6 +142,14 @@ fn paint_values(app: &PhotocraftApp, tool: Tool, b: &BrushSettings) -> PaintValu v } +/// A fraction the brush keeps as `f32`, read without its float noise. 0.51 as `f32` reads back as +/// 0.50999999..., which the bar's whole-percent fields take for a value off their step grid, so Up +/// stepped it to 51 again and only the first press moved the value (#16806). Six decimals keep every +/// value the fields can set (a tenth of a percent at the finest). +fn fraction(x: f32) -> f64 { + (f64::from(x) * 1e6).round() / 1e6 +} + /// What the Mixer Brush holds, as the Current Brush Load swatch shows it: the sampled tip image of an Alt-click while /// the foreground stays, else the loaded colour, else nothing (a clean brush). fn mixer_load(app: &PhotocraftApp) -> MixerLoad {