From 1b3312fd8c7912873b070610e00d842039649c87 Mon Sep 17 00:00:00 2001 From: DuBearte <130190089+DuBearte@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:12:06 -0500 Subject: [PATCH 1/4] Brush Preset picker: Size and Hardness take whole numbers and alert when out of range Fixes #11817. Co-Authored-By: Claude Opus 5.5 --- .../src/brush_picker_range_alert_tests.rs | 88 +++++++++++++++++++ crates/ui-egui/src/lib.rs | 2 + crates/ui-egui/src/tools/brush_picker.rs | 56 ++++++++++-- 3 files changed, 139 insertions(+), 7 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 00000000000..80fdabe2ae6 --- /dev/null +++ b/crates/ui-egui/src/brush_picker_range_alert_tests.rs @@ -0,0 +1,88 @@ +//! #11817: the Brush Preset picker's Size and Hardness take whole numbers; a value typed past the +//! range becomes the closest one and, once the field is left, an alert with OK says so. The +//! picker stays open after OK. + +use egui::vec2; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use crate::brush_picker::{HARDNESS_RANGE_ALERT, SIZE_RANGE_ALERT}; +use crate::state::DialogKind; +use crate::{PhotocraftApp, Services, Tool}; + +fn harness() -> Harness<'static, PhotocraftApp> { + let mut h = Harness::builder().with_size(vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_eframe(|cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), 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 + }); + h.run_steps(5); + let ctx = h.ctx.clone(); + let c = h.state().last_canvas_rect.center(); + crate::brush_picker::open(&mut h.state_mut().ui, &ctx, [c.x, c.y]); + h.run_steps(3); + h +} + +/// Type `text` into the picker field named `name`, then Tab out of it. +fn type_into(h: &mut Harness<'static, PhotocraftApp>, name: &str, text: &str) { + h.query_all_by_role(egui::accesskit::Role::SpinButton).find(|n| n.accesskit_node().label().as_deref() == Some(name)).expect("the field").click(); + h.run_steps(1); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.event(egui::Event::Text(text.into())); + h.run_steps(1); + h.key_press(egui::Key::Tab); + h.run_steps(3); +} + +fn alert(h: &Harness<'static, PhotocraftApp>) -> Option { + h.state().ui.dialogs.iter().find(|d| d.kind == DialogKind::Error).and_then(|d| d.fields.get("message")).and_then(|m| m.as_str()).map(str::to_owned) +} + +fn size(h: &Harness<'static, PhotocraftApp>) -> f32 { + h.state().session.tools.brush.size +} + +#[test] +fn size_past_the_range_takes_the_closest_value_and_alerts() { + for (typed, want) in [("9999", 5000.0), ("0", 1.0), ("-5", 1.0)] { + let mut h = harness(); + type_into(&mut h, "Size", typed); + assert_eq!(size(&h), want, "{typed}"); + assert_eq!(alert(&h).as_deref(), Some(SIZE_RANGE_ALERT), "{typed}"); + 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!(alert(&h), None, "{typed}: OK closes the alert"); + assert!(h.state().ui.brush_picker.is_some(), "{typed}: the picker stays open after OK"); + } +} + +#[test] +fn size_in_range_cuts_fractions_without_an_alert() { + let mut h = harness(); + type_into(&mut h, "Size", "2.5"); + assert_eq!(size(&h), 2.0); + assert_eq!(alert(&h), None); + type_into(&mut h, "Size", "abc"); + assert_eq!(size(&h), 2.0, "not a number: the value stays"); + assert_eq!(alert(&h), None); +} + +#[test] +fn hardness_past_the_range_takes_100_and_alerts() { + let mut h = harness(); + type_into(&mut h, "Hardness", "150"); + assert_eq!(h.state().session.tools.brush.hardness, 1.0); + assert_eq!(alert(&h).as_deref(), Some(HARDNESS_RANGE_ALERT)); + h.get_by_label("OK").click(); + h.run_steps(4); + assert!(h.state().ui.brush_picker.is_some()); + type_into(&mut h, "Hardness", "33.7"); + assert_eq!(h.state().session.tools.brush.hardness, 0.33, "a whole percent"); + assert_eq!(alert(&h), None); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index f19ec41da94..1853c7fab37 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -997,6 +997,8 @@ mod pen_path_ownership_tests; #[cfg(test)] mod artboard_options_bar_tests; #[cfg(test)] +mod brush_picker_range_alert_tests; +#[cfg(test)] mod doc_tab_menu_tests; #[cfg(test)] mod layers_empty_area_menu_tests; diff --git a/crates/ui-egui/src/tools/brush_picker.rs b/crates/ui-egui/src/tools/brush_picker.rs index 3e59a904443..01e0f2404c5 100644 --- a/crates/ui-egui/src/tools/brush_picker.rs +++ b/crates/ui-egui/src/tools/brush_picker.rs @@ -65,8 +65,15 @@ pub enum Pick { NewPreset, /// Import brushes (.abr) with the file dialog. Import, + /// A value typed out of range was replaced by the closest one: say so in an alert with OK, + /// as Photoshop does (#11817). The English message, translated when shown. + RangeAlert(&'static str), } +/// The alerts for Size and Hardness typed out of range (#11817). +pub const SIZE_RANGE_ALERT: &str = "An integer between 1 pixel and 5000 Pixels is required. Closest value inserted."; +pub const HARDNESS_RANGE_ALERT: &str = "An integer between 0% and 100% is required. Closest value inserted."; + impl Pick { /// Does this pick dismiss the picker? A double-click on a preset does (Photoshop), and so does /// anything that opens another panel or dialog. @@ -204,6 +211,17 @@ pub fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, picks: Vec) { Pick::Import => { let _ = crate::preset_files_ui::import_brushes(app); } + Pick::RangeAlert(message) => { + let message = tl!(message).to_owned(); + let shown = app + .ui + .dialogs + .iter() + .any(|d| d.kind == crate::state::DialogKind::Error && d.fields.get("message").and_then(|m| m.as_str()) == Some(&message)); + if !shown { + app.ui.open_dialog(crate::state::DialogKind::Error, json!({ "message": message }).as_object().cloned().unwrap_or_default()); + } + } } } } @@ -231,6 +249,24 @@ pub fn settings_toggle(app: &mut PhotocraftApp, ui: &mut egui::Ui) { } } +/// A picker field of whole numbers in `lo..=hi` (#11817): a typed fraction is cut off (2.5 → 2), +/// text that is no number leaves the value, and a value past the range becomes the closest one, +/// with `alert` once the field is left. +fn integer_field(ui: &mut egui::Ui, value: &mut f32, (lo, hi): (f32, f32), suffix: &str, name: &str, alert: &'static str, picks: &mut Vec) -> bool { + // The field's own range is wide, so the typed value reaches here unclamped. + let mut typed = *value; + let r = widgets::value_field(ui, &mut typed, -1.0e9..=1.0e9, suffix, 72.0).accessible_name(tl!(name)); + let out_id = r.id.with("out-of-range"); + if r.changed() && typed.is_finite() { + *value = typed.trunc().clamp(lo, hi); + ui.ctx().data_mut(|d| d.insert_temp(out_id, typed < lo || typed > hi)); + } + if r.lost_focus() && ui.ctx().data_mut(|d| d.remove_temp::(out_id)).unwrap_or(false) { + picks.push(Pick::RangeAlert(alert)); + } + r.changed() && typed.is_finite() +} + /// An icon button's accessible name (icon buttons have only a tooltip). pub(crate) fn named(resp: egui::Response, label: &str) -> egui::Response { resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!(label))); @@ -269,10 +305,7 @@ pub fn body( ui.horizontal(|ui| { ui.label(RichText::new(tl!("Size")).color(t.text_dim)); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - let mut size = b.size; - if widgets::value_field(ui, &mut size, 1.0..=MAX_BRUSH_SIZE, "px", 72.0).accessible_name(tl!("Size")).changed() { - b.size = size.round().clamp(1.0, MAX_BRUSH_SIZE); - } + integer_field(ui, &mut b.size, (1.0, MAX_BRUSH_SIZE), "px", "Size", SIZE_RANGE_ALERT, &mut picks); }); }); let mut lv = b.size.max(1.0).ln(); @@ -282,10 +315,19 @@ pub fn body( } dismiss_popup(&size_slider); if b.tip == TipShape::Round { + // Like Size: a caption and a whole-number field, then the slider. let mut hard = b.hardness * 100.0; - let hardness = widgets::slider_row(ui, "Hardness", &mut hard, 0.0..=100.0, "%", None); - if hardness.changed() { - b.hardness = (hard / 100.0).clamp(0.0, 1.0); + let mut typed = false; + ui.horizontal(|ui| { + ui.label(RichText::new(tl!("Hardness")).color(t.text_dim)); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + typed = integer_field(ui, &mut hard, (0.0, 100.0), "%", "Hardness", HARDNESS_RANGE_ALERT, &mut picks); + }); + }); + let hardness = widgets::slider(ui, &mut hard, 0.0..=100.0, None); + hardness.widget_info(|| egui::WidgetInfo::slider(true, f64::from(hard), tl!("Hardness"))); + if typed || hardness.changed() { + b.hardness = (hard.round() / 100.0).clamp(0.0, 1.0); } dismiss_popup(&hardness); } From 2a32a22624a2f24623e1289b3fae485debfd2fed Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 29 Nov 2026 15:52:21 +0100 Subject: [PATCH 2/4] 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 00000000000..7232f03ba4d --- /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 16abf9696e2..a0fc3024d88 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; From 768592563375efe1ccd71c9fcfbf7f6e9c041877 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 29 Nov 2026 21:57:23 +0100 Subject: [PATCH 3/4] Brush Preset picker range alert test: show the dialogs, so OK can be pressed Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/brush_picker_range_alert_tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ui-egui/src/brush_picker_range_alert_tests.rs b/crates/ui-egui/src/brush_picker_range_alert_tests.rs index 7232f03ba4d..4ebdea739d8 100644 --- a/crates/ui-egui/src/brush_picker_range_alert_tests.rs +++ b/crates/ui-egui/src/brush_picker_range_alert_tests.rs @@ -23,6 +23,7 @@ fn harness() -> Harness<'static, Box> { } // The canvas shows the picker itself (`canvas_tool_menu::run`). egui::CentralPanel::default().show(ui, |ui| crate::canvas::testing::document_area(app, ui)); + crate::dialogs::run(app, ui); }, app, ); From 93a120630d162b8ad889318b81c3a81d30a16678 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 19:08:58 +0100 Subject: [PATCH 4/4] Brush Preset picker range alert test: a typed Size fraction is cut off Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/brush_picker_range_alert_tests.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/brush_picker_range_alert_tests.rs b/crates/ui-egui/src/brush_picker_range_alert_tests.rs index 4ebdea739d8..981358c5c04 100644 --- a/crates/ui-egui/src/brush_picker_range_alert_tests.rs +++ b/crates/ui-egui/src/brush_picker_range_alert_tests.rs @@ -1,5 +1,6 @@ //! #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. +//! range alert; the picker stays open under it and after OK. A value in range (a typed fraction cut off) and a slider +//! press open none. use crate::app::framework::BuildApp as _; use egui::{Event, Modifiers, vec2}; @@ -74,6 +75,8 @@ fn size_and_hardness_typed_out_of_range_alert_and_the_picker_stays_open() { #[test] fn a_value_in_range_and_a_slider_press_open_no_alert() { let mut h = harness(); + type_into(&mut h, 0, "2.5"); + assert_eq!(size_and_hardness(&h).0, 2.0, "a typed fraction is cut off, not rounded"); type_into(&mut h, 0, "40"); assert_eq!(size_and_hardness(&h).0, 40.0); let slider = h