Merge pull request #12277 from hectorviov/fix/eraser-range-alerts

Eraser: Opacity, Flow and Smoothing out of range show the Closest value inserted alert (#11938)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-08 23:29:40 +01:00
commit 3ef7bd1183
5 files changed
+364 -5

No files matched your search

@@ -0,0 +1,258 @@
//! #11938: on the Eraser's bar, Opacity, Flow and Smoothing typed out of range keep the closest
//! value and show the reference's "Closest value inserted" alert once, with OK; values in range
//! show none. The Brush's bar (not measured) keeps today's silent clamp.
use egui::accesskit::Role;
use egui::vec2;
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};
use serde_json::json;
use super::{ONE_TO_100, ZERO_TO_100};
use crate::PhotocraftApp;
use crate::state::{DialogKind, Tool};
fn bar(tool: Tool) -> Harness<'static, PhotocraftApp> {
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;
let mut h = Harness::builder().with_size(vec2(1800.0, 300.0)).build_ui_state(|ui, app| crate::panels::options_bar(app, ui), app);
PhotocraftApp::setup_context(&h.ctx, Default::default());
h.run_steps(4);
h
}
/// Types `text` into the field named `label`, then Tabs out of it.
fn type_into(h: &mut Harness<'static, PhotocraftApp>, label: &str, text: &str) {
// The fields take their captions' names ("Opacity:").
let label = &format!("{label}:");
h.query_all_by_role(Role::SpinButton)
.find(|n| n.accesskit_node().label().as_deref() == Some(label.as_str()))
.unwrap_or_else(|| panic!("the {label} 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 alerts(h: &Harness<'static, PhotocraftApp>) -> Vec<String> {
h.state().ui.dialogs.iter().filter(|d| d.kind == DialogKind::Error).filter_map(|d| d.fields.get("message")?.as_str().map(str::to_owned)).collect()
}
/// OK on the alert, as `ui.dialog.confirm` presses it (this harness draws only the bar).
fn ok(h: &mut Harness<'static, PhotocraftApp>) {
let id = h.state().ui.dialogs.iter().find(|d| d.kind == DialogKind::Error).map(|d| d.id).expect("an alert");
crate::dialogs::confirm(h.state_mut(), id).unwrap();
h.run_steps(3);
}
fn brush(h: &Harness<'static, PhotocraftApp>) -> (f32, f32, f32) {
let b = &h.state().session.tools.brush;
((b.opacity * 100.0).round(), (b.flow * 100.0).round(), (b.smoothing.amount * 100.0).round())
}
#[test]
fn eraser_opacity_flow_and_smoothing_clamp_with_the_alert() {
for (label, typed, want, alert) in [
("Opacity", "150", 100.0, ONE_TO_100),
("Opacity", "0", 1.0, ONE_TO_100),
("Flow", "150", 100.0, ONE_TO_100),
("Flow", "0", 1.0, ONE_TO_100),
("Smoothing", "150", 100.0, ZERO_TO_100),
("Smoothing", "-5", 0.0, ZERO_TO_100),
] {
let mut h = bar(Tool::Eraser);
type_into(&mut h, label, typed);
let (o, f, s) = brush(&h);
let got = match label {
"Opacity" => o,
"Flow" => f,
_ => s,
};
assert_eq!(got, want, "{label} {typed}");
assert_eq!(alerts(&h), [alert], "{label} {typed}: the alert, once");
ok(&mut h);
assert!(alerts(&h).is_empty(), "{label} {typed}: OK closes it");
let (o, f, s) = brush(&h);
assert_eq!(
match label {
"Opacity" => o,
"Flow" => f,
_ => s,
},
want,
"{label}: the clamped value is kept"
);
}
}
#[test]
fn values_in_range_show_no_alert_and_the_field_keeps_working() {
let mut h = bar(Tool::Eraser);
type_into(&mut h, "Opacity", "40");
assert_eq!(brush(&h).0, 40.0);
assert!(alerts(&h).is_empty());
// After an alert the field still takes the keyboard.
type_into(&mut h, "Flow", "150");
ok(&mut h);
type_into(&mut h, "Flow", "55");
assert_eq!(brush(&h).1, 55.0);
assert!(alerts(&h).is_empty());
}
#[test]
fn the_alert_is_a_dialog_the_control_channel_lists() {
let mut h = bar(Tool::Eraser);
type_into(&mut h, "Smoothing", "150");
let dialogs = serde_json::to_value(&h.state().ui.dialogs).unwrap();
assert!(dialogs.to_string().contains(ZERO_TO_100), "{dialogs}");
}
#[test]
fn the_brush_bar_keeps_todays_silent_clamp() {
let mut h = bar(Tool::Brush);
type_into(&mut h, "Opacity", "150");
assert_eq!(brush(&h).0, 100.0);
assert!(alerts(&h).is_empty(), "not measured for the Brush: no alert");
}
/// A value typed past the range and left while the bar is not drawn (the tool is set from outside
/// the bar, as the control channel does, and the field loses the keyboard meanwhile) shows the
/// stored value when the Eraser comes back, not the typed one, and opens no alert on the way. A
/// click on the toolbar leaves the field while the bar is drawn: see
/// `a_toolbar_click_after_a_typed_value_past_the_range_alerts_once`.
#[test]
fn a_typed_value_left_by_a_tool_switch_shows_the_stored_value() {
let mut h = bar(Tool::Eraser);
h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().as_deref() == Some("Opacity:")).expect("the Opacity field").click();
h.run_steps(1);
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
h.event(egui::Event::Text("150".into()));
h.run_steps(1);
let switch = |h: &mut Harness<'static, PhotocraftApp>, tool: Tool| {
h.state_mut().ui.tool = tool;
crate::paint_mouse::sync_tool_brush(h.state_mut());
};
switch(&mut h, Tool::Brush);
h.ctx.memory_mut(|m| {
if let Some(id) = m.focused() {
m.surrender_focus(id);
}
});
h.run_steps(3);
assert!(alerts(&h).is_empty(), "leaving by a tool switch: no alert");
switch(&mut h, Tool::Eraser);
h.run_steps(1);
assert!(alerts(&h).is_empty(), "back on the Eraser: no alert");
let shown = h
.query_all_by_role(Role::SpinButton)
.find(|n| n.accesskit_node().label().as_deref() == Some("Opacity:"))
.and_then(|n| n.accesskit_node().numeric_value());
assert_eq!(brush(&h).0, 100.0, "the stored value");
assert_eq!(shown, Some(100.0), "the field shows the stored value");
}
#[test]
fn a_leading_zero_typed_key_by_key_keeps_the_text() {
// While the field has the keyboard it keeps what was typed: 0 (past 1..100) on the way to 05
// stays 0 rather than turning into 1, so 0 then 5 is 5, not 15, and in range.
let mut h = bar(Tool::Eraser);
h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().as_deref() == Some("Opacity:")).expect("the Opacity field").click();
h.run_steps(1);
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
for c in ["0", "5"] {
h.event(egui::Event::Text(c.into()));
h.run_steps(2);
}
h.key_press(egui::Key::Tab);
h.run_steps(3);
assert_eq!(brush(&h).0, 5.0, "0 then 5 is 05");
assert!(alerts(&h).is_empty(), "05 is in range");
}
#[test]
fn a_caption_scrub_past_100_does_not_alert() {
let mut h = bar(Tool::Eraser);
type_into(&mut h, "Opacity", "90");
// The field keeps the keyboard while its caption is scrubbed from 90 past the end.
let field = h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().as_deref() == Some("Opacity:")).expect("the Opacity field");
let rect = field.rect();
field.click();
h.run_steps(1);
// The caption sits just left of the field.
let from = egui::pos2(rect.left() - 25.0, rect.center().y);
h.event(egui::Event::PointerMoved(from));
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
h.run_steps(1);
for k in 1..=6 {
h.event(egui::Event::PointerMoved(from + vec2(50.0 * k as f32, 0.0)));
h.run_steps(1);
}
let to = from + vec2(300.0, 0.0);
h.event(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
h.run_steps(3);
h.key_press(egui::Key::Tab);
h.run_steps(3);
assert_eq!(brush(&h).0, 100.0, "the scrub reached the end");
assert!(alerts(&h).is_empty(), "a scrub stops at 100 without an alert");
}
/// 150 typed into the Eraser's Opacity, then a click on the toolbar's Brush Tool: the click leaves
/// the field while the Eraser's bar is still drawn, so the value is clamped and its alert shows
/// once, as when the field is left any other way. The tool switches, and the Eraser shows the
/// stored value when it comes back.
#[test]
fn a_toolbar_click_after_a_typed_value_past_the_range_alerts_once() {
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::Eraser;
let mut h = Harness::builder().with_size(vec2(1800.0, 900.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
crate::panels::options_bar(app, ui);
crate::panels::toolbar(app, ui);
},
app,
);
PhotocraftApp::setup_context(&h.ctx, Default::default());
h.run_steps(4);
let field = |h: &Harness<'static, PhotocraftApp>| {
h.query_all_by_role(Role::SpinButton)
.find(|n| n.accesskit_node().label().as_deref() == Some("Opacity:"))
.map(|n| (n.rect(), n.accesskit_node().numeric_value()))
};
let click = |h: &mut Harness<'static, PhotocraftApp>, at: egui::Pos2| {
h.event(egui::Event::PointerMoved(at));
h.run_steps(1);
for pressed in [true, false] {
h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
h.run_steps(1);
}
};
let (rect, _) = field(&h).expect("the Opacity field");
click(&mut h, rect.center());
h.run_steps(1);
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
h.event(egui::Event::Text("150".into()));
h.run_steps(1);
let tool = |h: &Harness<'static, PhotocraftApp>, name: &str| {
h.query_all_by_role(Role::Button).find(|n| n.accesskit_node().label().is_some_and(|l| l.starts_with(name))).map(|n| n.rect().center())
};
let at = tool(&h, "Brush Tool").expect("the toolbar's Brush Tool");
click(&mut h, at);
h.run_steps(3);
assert_eq!(h.state().ui.tool, Tool::Brush, "the click switches tools");
assert_eq!(alerts(&h), [ONE_TO_100], "the typed value's alert, once");
assert_eq!(brush(&h).0, 100.0, "the closest value is stored");
ok(&mut h);
assert!(alerts(&h).is_empty(), "OK closes it");
let at = tool(&h, "Eraser Tool").expect("the toolbar's Eraser Tool");
click(&mut h, at);
h.run_steps(3);
assert_eq!(h.state().ui.tool, Tool::Eraser);
assert!(alerts(&h).is_empty(), "back on the Eraser: no alert");
assert_eq!(field(&h).and_then(|(_, v)| v), Some(100.0), "the field shows the stored value");
}
@@ -35,6 +35,18 @@ pub(super) fn paint_mode_dropdown(ui: &mut egui::Ui, id: &str, b: &mut photocraf
} }
} }
/// The Eraser's Smoothing %: whole numbers 0..100 that say when a typed value was past them, for
/// the reference's alert (#11938). Returns the field and whether to show the alert.
pub(super) fn smoothing_field_alerting(ui: &mut egui::Ui, b: &mut photocraft_engine::BrushSettings, width: f32) -> (egui::Response, bool) {
let mut sm = b.smoothing.amount * 100.0;
let (r, alert) = widgets::value_field_whole_alerting(ui, tl!("Smoothing"), &mut sm, 0.0..=100.0, "%", width);
if r.changed() {
b.smoothing.enabled = true;
b.smoothing.amount = (sm / 100.0).clamp(0.0, 1.0);
}
(r, alert)
}
/// Options-bar Smoothing % (the brush's stroke smoothing; the live stroke and the commit use it). /// Options-bar Smoothing % (the brush's stroke smoothing; the live stroke and the commit use it).
pub(super) fn smoothing_field(ui: &mut egui::Ui, b: &mut photocraft_engine::BrushSettings, width: f32) -> egui::Response { pub(super) fn smoothing_field(ui: &mut egui::Ui, b: &mut photocraft_engine::BrushSettings, width: f32) -> egui::Response {
let mut sm = (b.smoothing.amount * 100.0).round(); let mut sm = (b.smoothing.amount * 100.0).round();
+46 -4
View File
@@ -181,8 +181,13 @@ fn brush_specific_options(app: &mut PhotocraftApp, ui: &mut egui::Ui, t: Tokens,
} }
} }
ui.add_enabled_ui(!block, |ui| { ui.add_enabled_ui(!block, |ui| {
// A whole number from 1 to 100, as in Photoshop: 0 paints nothing (#11812). // A whole number from 1 to 100, as in Photoshop: 0 paints nothing (#11812). The
tip(whole_percent_field(ui, tl!("Opacity"), &mut b.opacity, 1.0..=100.0, 62.0), tl!("Set opacity for stroke")); // Eraser's says so when a typed value was past that (#11938).
if brush {
tip(whole_percent_field(ui, tl!("Opacity"), &mut b.opacity, 1.0..=100.0, 62.0), tl!("Set opacity for stroke"));
} else if eraser_percent(ui, tl!("Opacity"), &mut b.opacity) {
range_alert(&mut app.ui, ONE_TO_100);
}
let pressure = pressure_button(ui, b.pressure_opacity, tl!("Always use pressure for opacity"), brush); let pressure = pressure_button(ui, b.pressure_opacity, tl!("Always use pressure for opacity"), brush);
if tip(pressure, tl!("Always use Pressure for Opacity. When off, Brush Preset controls pressure")).clicked() { if tip(pressure, tl!("Always use Pressure for Opacity. When off, Brush Preset controls pressure")).clicked() {
b.pressure_opacity = !b.pressure_opacity; b.pressure_opacity = !b.pressure_opacity;
@@ -190,14 +195,22 @@ fn brush_specific_options(app: &mut PhotocraftApp, ui: &mut egui::Ui, t: Tokens,
}); });
// A Pencil- or Block-mode Eraser is always full flow, without build-up (Photoshop). // A Pencil- or Block-mode Eraser is always full flow, without build-up (Photoshop).
ui.add_enabled_ui(!block && !crate::eraser_ui::pencil_mode(app, app.ui.tool), |ui| { ui.add_enabled_ui(!block && !crate::eraser_ui::pencil_mode(app, app.ui.tool), |ui| {
tip(percent_field(ui, tl!("Flow"), &mut b.flow, 1.0..=100.0, 62.0), tl!("Set flow rate for stroke")); if brush {
tip(percent_field(ui, tl!("Flow"), &mut b.flow, 1.0..=100.0, 62.0), tl!("Set flow rate for stroke"));
} else if eraser_percent(ui, tl!("Flow"), &mut b.flow) {
range_alert(&mut app.ui, ONE_TO_100);
}
let airbrush = icons::button(ui, "sparkles", 24.0, b.build_up, tl!("Enable airbrush-style build-up effects")); let airbrush = icons::button(ui, "sparkles", 24.0, b.build_up, tl!("Enable airbrush-style build-up effects"));
if crate::brush_picker::named(airbrush, "Enable airbrush-style build-up effects").clicked() { if crate::brush_picker::named(airbrush, "Enable airbrush-style build-up effects").clicked() {
b.build_up = !b.build_up; b.build_up = !b.build_up;
} }
}); });
opt_label(ui, tl!("Smoothing")); opt_label(ui, tl!("Smoothing"));
tip(smoothing_field(ui, b, 58.0), tl!("Set smoothing for stroke\nUse higher values to reduce the shakiness of brush strokes")); if brush {
tip(smoothing_field(ui, b, 58.0), tl!("Set smoothing for stroke\nUse higher values to reduce the shakiness of brush strokes"));
} else if super::brush_controls::smoothing_field_alerting(ui, b, 58.0).1 {
range_alert(&mut app.ui, ZERO_TO_100);
}
smoothing_options(ui, b, brush); smoothing_options(ui, b, brush);
widgets::vline(ui, 22.0); widgets::vline(ui, 22.0);
let pressure = pressure_button(ui, b.pressure_size, tl!("Always use pressure for size"), brush); let pressure = pressure_button(ui, b.pressure_size, tl!("Always use pressure for size"), brush);
@@ -851,6 +864,31 @@ fn pressure_button(ui: &mut egui::Ui, on: bool, name: &str, own_tip: bool) -> eg
icons::button_with_icon_size_tip(ui, "circle-dot", 24.0, (24.0f32 * 0.52).round(), on, name, !own_tip) icons::button_with_icon_size_tip(ui, "circle-dot", 24.0, (24.0f32 * 0.52).round(), on, name, !own_tip)
} }
/// The reference's alerts for an Eraser Opacity or Flow, and Smoothing, typed out of range (#11938).
pub const ONE_TO_100: &str = "An integer between 1% and 100% is required. Closest value inserted.";
pub const ZERO_TO_100: &str = "An integer between 0% and 100% is required. Closest value inserted.";
/// The Eraser's Opacity or Flow: whole percents 1..100 that say when a typed value was past them.
fn eraser_percent(ui: &mut egui::Ui, label: &str, value: &mut f32) -> bool {
opt_label(ui, label);
let mut percent = *value * 100.0;
let (r, alert) = widgets::value_field_whole_alerting(ui, label, &mut percent, 1.0..=100.0, "%", 62.0);
if r.changed() {
*value = percent / 100.0;
}
alert
}
/// Shows one of the range alerts as a modal with OK, once even if more than one pass asks.
fn range_alert(ui_state: &mut crate::state::UiState, text: &str) {
let message = tl!(text).to_owned();
let shown =
ui_state.dialogs.iter().any(|d| d.kind == crate::state::DialogKind::Error && d.fields.get("message").and_then(|m| m.as_str()) == Some(&message));
if !shown {
ui_state.open_dialog(crate::state::DialogKind::Error, serde_json::json!({ "message": message }).as_object().cloned().unwrap_or_default());
}
}
/// [`percent_field`] that keeps whole percents (`33.7` becomes 34 %). A value set elsewhere (a /// [`percent_field`] that keeps whole percents (`33.7` becomes 34 %). A value set elsewhere (a
/// `tools.setBrush` fraction, an older saved 0) shows as the whole percent in range. /// `tools.setBrush` fraction, an older saved 0) shows as the whole percent in range.
fn whole_percent_field(ui: &mut egui::Ui, label: &str, value: &mut f32, range: std::ops::RangeInclusive<f32>, width: f32) -> egui::Response { fn whole_percent_field(ui: &mut egui::Ui, label: &str, value: &mut f32, range: std::ops::RangeInclusive<f32>, width: f32) -> egui::Response {
@@ -867,3 +905,7 @@ fn hint(ui: &mut egui::Ui, s: &str) {
let t = Tokens::get(ui.ctx()); let t = Tokens::get(ui.ctx());
ui.label(RichText::new(s).color(t.text_faint)); ui.label(RichText::new(s).color(t.text_faint));
} }
#[cfg(test)]
#[path = "../eraser_range_alert_tests.rs"]
mod eraser_range_alert_tests;
+2 -1
View File
@@ -50,7 +50,8 @@ pub(crate) use fields::compact_wrapping_value_field;
#[cfg(test)] #[cfg(test)]
use fields::{FieldOpts, NUMERIC_INPUT_LIMIT, value_field_in}; use fields::{FieldOpts, NUMERIC_INPUT_LIMIT, value_field_in};
pub use fields::{ pub use fields::{
POPUP_ARROW_W, PopupFieldResponse, VALUE_FIELD_H, compact_value_field, elided_label, elided_label_sensed, popup_value_field, value_field, value_field_named, POPUP_ARROW_W, PopupFieldResponse, VALUE_FIELD_H, compact_value_field, elided_label, elided_label_sensed, popup_value_field, value_field,
value_field_named, value_field_whole_alerting,
}; };
pub(crate) use panel::body_margin; pub(crate) use panel::body_margin;
pub use panel::{ pub use panel::{
+46
View File
@@ -12,6 +12,52 @@ pub fn value_field(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive
value_field_in(ui, value, range, suffix, width, 0.0, FieldOpts::default(), None).0 value_field_in(ui, value, range, suffix, width, 0.0, FieldOpts::default(), None).0
} }
/// A [`value_field_named`] for whole numbers that says when a typed value was past `range`
/// (Photoshop's "Closest value inserted" alerts, #11938): it stores the closest whole number in
/// range, and returns true once the field is left after such a value was typed, so the caller
/// shows its alert. While the field has the keyboard it keeps the typed text (a `0` on the way to
/// `05` stays). A scrub past the ends just stops there and says nothing.
pub fn value_field_whole_alerting(
ui: &mut Ui,
name: &str,
value: &mut f32,
range: std::ops::RangeInclusive<f32>,
suffix: &str,
width: f32,
) -> (Response, bool) {
let (lo, hi) = (*range.start(), *range.end());
let fit = |v: f32| if v.is_finite() { v.round().clamp(lo, hi) } else { lo };
*value = fit(*value);
let draft_id = ui.make_persistent_id(egui::IdSalt::new(("whole-alerting-typed", name)));
// Typed text only while its field still has the keyboard: one left without being drawn (a
// tool switch) shows the stored value when it comes back.
let draft = ui.ctx().data(|d| d.get_temp::<(f32, f32, Option<egui::Id>)>(draft_id));
let draft = draft.filter(|(_, stored, field)| *stored == *value && field.is_some_and(|f| ui.ctx().memory(|m| m.has_focus(f))));
let mut typed = draft.map_or(*value, |(typed, _, _)| typed);
// The field's own range is wide, so the typed value reaches here unclamped.
let r = value_field_named(ui, name, &mut typed, -1.0e9..=1.0e9, suffix, width);
let out_id = r.id.with("out-of-range");
if r.gained_focus() {
ui.ctx().data_mut(|d| d.remove_temp::<bool>(out_id));
}
if r.changed() && typed.is_finite() {
*value = fit(typed);
let typing = (r.has_focus() || r.lost_focus()) && ui.ctx().dragged_id().is_none();
let out = typing && !(lo..=hi).contains(&typed);
ui.ctx().data_mut(|d| d.insert_temp(out_id, out));
if typing && r.has_focus() {
ui.ctx().data_mut(|d| d.insert_temp(draft_id, (typed, *value, Some(r.id))));
} else {
ui.ctx().data_mut(|d| d.remove_temp::<(f32, f32, Option<egui::Id>)>(draft_id));
}
}
if !r.has_focus() {
ui.ctx().data_mut(|d| d.remove_temp::<(f32, f32, Option<egui::Id>)>(draft_id));
}
let alert = r.lost_focus() && ui.ctx().data_mut(|d| d.remove_temp::<bool>(out_id)).unwrap_or(false);
(r, alert)
}
/// A [`value_field`] whose visible caption was drawn apart from it (an options-bar caption in a /// A [`value_field`] whose visible caption was drawn apart from it (an options-bar caption in a
/// nested layout): `name` names the number when the caption's scrub link did not reach it. /// nested layout): `name` names the number when the caption's scrub link did not reach it.
pub fn value_field_named(ui: &mut Ui, name: &str, value: &mut f32, range: std::ops::RangeInclusive<f32>, suffix: &str, width: f32) -> Response { pub fn value_field_named(ui: &mut Ui, name: &str, value: &mut f32, range: std::ops::RangeInclusive<f32>, suffix: &str, width: f32) -> Response {