From 2a32a22624a2f24623e1289b3fae485debfd2fed Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 29 Nov 2026 15:52:21 +0100 Subject: [PATCH] Brush Preset picker: a Size or Hardness typed out of range opens the range alert (#11817) The picker's head now reports a value typed past its range (storytold/craft-libs#4273); apply opens the shared range alert for it, as the options bar does, and the picker stays open under it. Co-Authored-By: Claude Opus 5.5 --- .../src/brush_picker_range_alert_tests.rs | 92 +++++++++++++++++++ crates/ui-egui/src/tools/brush_picker.rs | 20 ++-- 2 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 crates/ui-egui/src/brush_picker_range_alert_tests.rs diff --git a/crates/ui-egui/src/brush_picker_range_alert_tests.rs b/crates/ui-egui/src/brush_picker_range_alert_tests.rs new file mode 100644 index 0000000000..7232f03ba4 --- /dev/null +++ b/crates/ui-egui/src/brush_picker_range_alert_tests.rs @@ -0,0 +1,92 @@ +//! #11817: a Size or Hardness typed past its range in the Brush Preset picker takes the closest value and opens the +//! range alert; the picker stays open under it and after OK. A value in range and a slider press open none. + +use crate::app::framework::BuildApp as _; +use egui::{Event, Modifiers, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::Tool; + +fn harness() -> Harness<'static, Box> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 20, "hardness": 0.5}})).unwrap(); + app.ui.tool = Tool::Brush; + app.sync_views(); + let mut h = Harness::builder().with_size(vec2(1200.0, 800.0)).with_step_dt(1.0 / 60.0).with_context(app.ctx().clone()).build_app( + |ui, app: &mut PhotocraftApp| { + if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + // The canvas shows the picker itself (`canvas_tool_menu::run`). + egui::CentralPanel::default().show(ui, |ui| crate::canvas::testing::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::AppTheme::pro()); + h.run_steps(4); + let c = h.state().last_canvas_rect.center(); + crate::brush_picker::open(h.state_mut(), [c.x, c.y]); + h.run_steps(3); + h +} + +/// Type `text` into the picker's `n`th spin field (0 Size, 1 Hardness), then Tab. +fn type_into(h: &mut Harness<'static, Box>, n: usize, text: &str) { + h.get_all_by_role(egui::accesskit::Role::SpinButton).nth(n).expect("the field").click(); + h.run_steps(1); + h.key_press_modifiers(Modifiers::COMMAND, egui::Key::A); + h.event(Event::Text(text.into())); + h.run_steps(1); + h.key_press(egui::Key::Tab); + h.run_steps(3); +} + +fn alerts(h: &Harness<'static, Box>) -> usize { + h.state().framework.dialogs.open_dialogs().len() +} + +fn size_and_hardness(h: &Harness<'static, Box>) -> (f32, f32) { + let b = &h.state().session.tools.brush; + (b.size, b.hardness) +} + +#[test] +fn size_and_hardness_typed_out_of_range_alert_and_the_picker_stays_open() { + for (n, typed, want) in [(0, "9999", (5000.0, 0.5)), (0, "0", (1.0, 0.5)), (1, "150", (20.0, 1.0))] { + let mut h = harness(); + type_into(&mut h, n, typed); + assert_eq!(size_and_hardness(&h), want, "{typed}: the closest value"); + assert_eq!(alerts(&h), 1, "{typed}: one range alert"); + assert!(h.state().ui.brush_picker.is_some(), "{typed}: the picker stays open under the alert"); + h.get_by_label("OK").click(); + h.run_steps(4); + assert_eq!(alerts(&h), 0, "{typed}: OK closes the alert"); + assert_eq!(size_and_hardness(&h), want, "{typed}: OK keeps the closest value"); + assert!(h.state().ui.brush_picker.is_some(), "{typed}: the picker stays open after OK"); + } +} + +#[test] +fn a_value_in_range_and_a_slider_press_open_no_alert() { + let mut h = harness(); + type_into(&mut h, 0, "40"); + assert_eq!(size_and_hardness(&h).0, 40.0); + let slider = h + .get_all_by_role(egui::accesskit::Role::Slider) + .find(|n| n.accesskit_node().label().as_deref() == Some("Hardness")) + .expect("the Hardness slider") + .rect(); + let at = slider.left_center() + vec2(slider.width() * 0.8, 0.0); + h.event(Event::PointerMoved(at)); + h.run_steps(1); + h.event(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.run_steps(1); + h.event(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); + assert!(size_and_hardness(&h).1 > 0.5, "the press moved Hardness"); + assert_eq!(alerts(&h), 0, "nothing was typed out of range"); +} diff --git a/crates/ui-egui/src/tools/brush_picker.rs b/crates/ui-egui/src/tools/brush_picker.rs index 16abf9696e..a0fc3024d8 100644 --- a/crates/ui-egui/src/tools/brush_picker.rs +++ b/crates/ui-egui/src/tools/brush_picker.rs @@ -98,9 +98,10 @@ fn commit_edits(app: &mut PhotocraftApp, events: &EditEvents, set: impl Fn } } -/// Runs what the picker reported: the head's edits, Escape's revert, the saved view and the picks. -/// `keys` are every row's key in list order (a reorder pick is placed by them). -pub(crate) fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, keys: &[BrushKey], out: BrushPresetPickerOutput) { +/// Runs what the picker reported: the head's edits, Escape's revert, the range alert for a Size or Hardness typed out +/// of range (the picker stays open under it, #11817), the saved view and the picks. `keys` are every row's key in list +/// order (a reorder pick is placed by them); `ui` words the alert's range. +pub(crate) fn apply(app: &mut PhotocraftApp, ui: &egui::Ui, keys: &[BrushKey], out: BrushPresetPickerOutput) { commit_edits(app, &out.diameter, |b, v| b.size = *v as f32); commit_edits(app, &out.hardness, |b, v| b.hardness = *v as f32); commit_edits(app, &out.tip, |b, v| { @@ -121,12 +122,15 @@ pub(crate) fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, keys: &[BrushK // One command and no history step: `tools.setBrush` without a coalesce key is a tool setting. commit_keyed(app, &now, &after, None); } + if let Some(alert) = &out.out_of_range { + crate::tools::option_cells::range_alert(app, ui, alert); + } if let Some(settings) = &out.settings { save(app, settings); } for pick in out.picks { match pick { - BrushPick::OpenSettings => open_settings(app, ctx), + BrushPick::OpenSettings => open_settings(app, ui.ctx()), other => run_picks(app, vec![other], keys), } } @@ -157,7 +161,7 @@ pub(crate) fn show_at(ui: &mut egui::Ui, app: &mut PhotocraftApp) { }; let _ = MemoView::new("brush-preset-canvas", props, canvas_view).show(ui); for CanvasEvent { out, keys, eraser, closed } in sent { - apply(app, ui.ctx(), &keys, out); + apply(app, ui, &keys, out); if let Some(rows) = eraser { commit_keyed(app, &rows.before, &rows.brush, rows.key); if app.ui.tool_options.bg_tolerance_control != rows.tolerance { @@ -249,7 +253,7 @@ pub(crate) fn show_field(ui: &mut egui::Ui, app: &mut PhotocraftApp, enabled: bo }; let _ = MemoView::new("brush-preset-field", props, field_view).show(ui); for FieldEvent(out, keys) in sent { - apply(app, ui.ctx(), &keys, out); + apply(app, ui, &keys, out); } } @@ -393,3 +397,7 @@ fn toggle_view(props: &mut ToggleProps<'_>, ui: &mut egui::Ui) { #[cfg(test)] #[path = "../brush_picker_gear_menu_tests.rs"] mod gear_menu_tests; + +#[cfg(test)] +#[path = "../brush_picker_range_alert_tests.rs"] +mod range_alert_tests;