mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Background Eraser: no alert for a caption scrub, strokes use the stored Tolerance (#11909)
A scrub of the Tolerance caption while the field has the keyboard stops at the range's end without the alert. Strokes send the Tolerance as the field stores it, so a restored fraction erases the same whether or not the options bar has drawn. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
8f9643c932
commit
2a989225f6
2 files changed
+55
-4
No files matched your search
@@ -121,6 +121,48 @@ fn a_restored_tolerance_out_of_range_shows_as_the_field_stores_it() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_restored_fraction_reaches_the_stroke_as_a_whole_number() {
|
||||
// The options bar need not have drawn: the stroke's params round it themselves.
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
for (saved, want) in [(23.6, 24.0), (0.0, 1.0), (f32::NAN, 50.0)] {
|
||||
app.ui.tool_options.bg_tolerance = saved;
|
||||
let p = crate::eraser_ui::background_eraser_params(&app, &[[1.0, 1.0, 1.0]]);
|
||||
assert_eq!(p["tolerance"].as_f64(), Some(want), "{saved}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_label_scrub_past_100_does_not_alert() {
|
||||
let mut h = app_harness();
|
||||
type_tolerance(&mut h, "100");
|
||||
// The field keeps the keyboard while the caption is scrubbed past the end.
|
||||
let field = h
|
||||
.query_all_by_role(egui::accesskit::Role::SpinButton)
|
||||
.find(|n| n.accesskit_node().label().as_deref() == Some("Tolerance:"))
|
||||
.expect("the Tolerance 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!(tolerance(&h), 100.0);
|
||||
assert_eq!(alert(&h), None, "a scrub stops at 100 without an alert");
|
||||
}
|
||||
|
||||
/// The accessible descriptions (tooltips) of the widgets of `role` named `label`.
|
||||
fn descriptions(h: &Harness<'static, PhotocraftApp>, role: Option<egui::accesskit::Role>, label: &str) -> Vec<String> {
|
||||
let of = |n: egui_kittest::Node<'_>| n.accesskit_node().description().map(|d| d.to_string());
|
||||
|
||||
@@ -53,7 +53,7 @@ pub(crate) fn background_eraser_params(app: &PhotocraftApp, points: &[[f64; 3]])
|
||||
"points": points,
|
||||
"sampling": o.bg_sampling,
|
||||
"limits": o.bg_limits,
|
||||
"tolerance": o.bg_tolerance,
|
||||
"tolerance": stored_tolerance(o.bg_tolerance),
|
||||
"protectForegroundColor": o.bg_protect_fg,
|
||||
"seed": crate::retouch_ui::stroke_seed(points),
|
||||
})
|
||||
@@ -150,7 +150,7 @@ pub const TOLERANCE_RANGE_ALERT: &str = "An integer between 1% and 100% is requi
|
||||
/// caller shows [`TOLERANCE_RANGE_ALERT`].
|
||||
fn tolerance_field(ui: &mut egui::Ui, value: &mut f32) -> bool {
|
||||
// A restored setting (an older 0, a fraction) shows and erases as the field would store it.
|
||||
*value = if value.is_finite() { value.round().clamp(1.0, 100.0) } else { 50.0 };
|
||||
*value = stored_tolerance(*value);
|
||||
// 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, "%", 62.0).on_hover_text(tl!("Erase range of similar colors"));
|
||||
@@ -158,13 +158,22 @@ fn tolerance_field(ui: &mut egui::Ui, value: &mut f32) -> bool {
|
||||
if r.changed() && typed.is_finite() {
|
||||
*value = typed.round().clamp(1.0, 100.0);
|
||||
// Only what was typed alerts, also an expression committed as the field is left
|
||||
// (`50*4` and Enter); a scrub past the ends just stops there.
|
||||
let out = (r.has_focus() || r.lost_focus()) && !(1.0..=100.0).contains(&typed);
|
||||
// (`50*4` and Enter); a scrub past the ends (the field's or its caption's, even while
|
||||
// the field has the keyboard) just stops there.
|
||||
let typing = (r.has_focus() || r.lost_focus()) && ui.ctx().dragged_id().is_none();
|
||||
let out = typing && !(1.0..=100.0).contains(&typed);
|
||||
ui.ctx().data_mut(|d| d.insert_temp(out_id, out));
|
||||
}
|
||||
r.lost_focus() && ui.ctx().data_mut(|d| d.remove_temp::<bool>(out_id)).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// A Tolerance as the field stores it: a whole number in 1..=100, the default 50 for a value
|
||||
/// that is no number. Strokes use it too, so a restored setting erases the same before and after
|
||||
/// the options bar shows it.
|
||||
fn stored_tolerance(v: f32) -> f32 {
|
||||
if v.is_finite() { v.round().clamp(1.0, 100.0) } else { 50.0 }
|
||||
}
|
||||
|
||||
/// Shows [`TOLERANCE_RANGE_ALERT`]: a modal with OK, once even if more than one pass asks.
|
||||
fn open_range_alert(app: &mut PhotocraftApp) {
|
||||
let message = tl!(TOLERANCE_RANGE_ALERT).to_owned();
|
||||
|
||||
Reference in new issue
Block a user