mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #12859 from rmsff/fix/blur-strength-range-alert-12650
Blur Strength: alert when a typed value is out of range
This commit is contained in:
4 files changed
+132
-10
No files matched your search
@@ -0,0 +1,107 @@
|
||||
//! #12650: the Blur tool's Strength takes whole numbers 1..100; a value typed past the range becomes
|
||||
//! the closest one and, once the field is left, the reference's "Closest value inserted" alert with
|
||||
//! OK says so. Sharpen and Smudge (not measured) keep today's silent clamp.
|
||||
|
||||
use egui::accesskit::Role;
|
||||
use egui::vec2;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
|
||||
use super::eraser_range_alert_tests::{alerts, bar, ok, type_into};
|
||||
use crate::PhotocraftApp;
|
||||
use crate::eraser_ui::TOLERANCE_RANGE_ALERT;
|
||||
use crate::state::Tool;
|
||||
|
||||
/// Clicks into Strength and gives back where it sits.
|
||||
fn click_strength(h: &mut Harness<'static, PhotocraftApp>) -> egui::Rect {
|
||||
let field = h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().as_deref() == Some("Strength:")).expect("the Strength field");
|
||||
let rect = field.rect();
|
||||
field.click();
|
||||
h.run_steps(1);
|
||||
rect
|
||||
}
|
||||
|
||||
fn strength(h: &Harness<'static, PhotocraftApp>) -> f32 {
|
||||
h.state().ui.tool_options.strength
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blur_strength_past_the_range_takes_the_closest_value_and_alerts() {
|
||||
for (typed, want) in [("150", 100.0), ("0", 1.0)] {
|
||||
let mut h = bar(Tool::Blur);
|
||||
type_into(&mut h, "Strength", typed);
|
||||
assert_eq!(strength(&h), want, "{typed}");
|
||||
assert_eq!(alerts(&h), vec![TOLERANCE_RANGE_ALERT.to_string()], "{typed}: one alert");
|
||||
ok(&mut h);
|
||||
assert!(alerts(&h).is_empty(), "{typed}: OK closes the alert");
|
||||
assert_eq!(strength(&h), want, "{typed}: the closest value is kept");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blur_strength_in_range_stores_a_whole_number_without_an_alert() {
|
||||
for (typed, want) in [("40", 40.0), ("33.6", 34.0), ("1", 1.0), ("100", 100.0)] {
|
||||
let mut h = bar(Tool::Blur);
|
||||
type_into(&mut h, "Strength", typed);
|
||||
assert_eq!(strength(&h), want, "{typed}");
|
||||
assert!(alerts(&h).is_empty(), "{typed} is in range");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blur_strength_that_is_no_number_keeps_the_value_without_an_alert() {
|
||||
let mut h = bar(Tool::Blur);
|
||||
type_into(&mut h, "Strength", "abc");
|
||||
assert_eq!(strength(&h), 50.0, "the previous value stays");
|
||||
assert!(alerts(&h).is_empty(), "no number, no alert");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_caption_scrub_past_either_end_does_not_alert() {
|
||||
for (start, dx, want) in [("90", 50.0, 100.0), ("10", -50.0, 1.0)] {
|
||||
let mut h = bar(Tool::Blur);
|
||||
type_into(&mut h, "Strength", start);
|
||||
// The field keeps the keyboard while its caption is scrubbed past the end.
|
||||
let rect = click_strength(&mut h);
|
||||
// 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(dx * k as f32, 0.0)));
|
||||
h.run_steps(1);
|
||||
}
|
||||
let to = from + vec2(dx * 6.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!(strength(&h), want, "{start}: the scrub reached the end");
|
||||
assert!(alerts(&h).is_empty(), "{start}: a scrub stops at the end without an alert");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sharpen_and_smudge_strength_keep_the_silent_clamp() {
|
||||
for tool in [Tool::Sharpen, Tool::Smudge] {
|
||||
let mut h = bar(tool);
|
||||
type_into(&mut h, "Strength", "150");
|
||||
assert_eq!(strength(&h), 100.0, "{tool:?}");
|
||||
assert!(alerts(&h).is_empty(), "{tool:?}: not measured, so no alert");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opening_the_blur_bar_keeps_a_sharpen_fraction() {
|
||||
let mut h = bar(Tool::Sharpen);
|
||||
h.state_mut().ui.tool_options.strength = 33.6;
|
||||
h.run_steps(2);
|
||||
h.state_mut().ui.tool = Tool::Blur;
|
||||
h.run_steps(4);
|
||||
h.state_mut().ui.tool = Tool::Sharpen;
|
||||
h.run_steps(2);
|
||||
assert_eq!(strength(&h), 33.6, "the Blur bar only shows the shared value");
|
||||
assert!(alerts(&h).is_empty());
|
||||
}
|
||||
@@ -12,7 +12,7 @@ use super::{ONE_TO_100, ZERO_TO_100};
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::{DialogKind, Tool};
|
||||
|
||||
fn bar(tool: Tool) -> Harness<'static, PhotocraftApp> {
|
||||
pub(super) 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;
|
||||
@@ -23,7 +23,7 @@ fn bar(tool: Tool) -> Harness<'static, PhotocraftApp> {
|
||||
}
|
||||
|
||||
/// Types `text` into the field named `label`, then Tabs out of it.
|
||||
fn type_into(h: &mut Harness<'static, PhotocraftApp>, label: &str, text: &str) {
|
||||
pub(super) 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)
|
||||
@@ -38,12 +38,12 @@ fn type_into(h: &mut Harness<'static, PhotocraftApp>, label: &str, text: &str) {
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
fn alerts(h: &Harness<'static, PhotocraftApp>) -> Vec<String> {
|
||||
pub(super) 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>) {
|
||||
pub(super) 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);
|
||||
|
||||
@@ -928,3 +928,7 @@ fn hint(ui: &mut egui::Ui, s: &str) {
|
||||
#[cfg(test)]
|
||||
#[path = "../eraser_range_alert_tests.rs"]
|
||||
mod eraser_range_alert_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../blur_strength_alert_tests.rs"]
|
||||
mod blur_strength_alert_tests;
|
||||
@@ -613,11 +613,6 @@ fn opt(ui: &mut egui::Ui, text: &str) {
|
||||
crate::widgets::scrub_option_label(ui, &label);
|
||||
}
|
||||
|
||||
fn pct(ui: &mut egui::Ui, label: &str, v: &mut f32) {
|
||||
opt(ui, label);
|
||||
crate::widgets::value_field(ui, v, 1.0..=100.0, "%", 58.0);
|
||||
}
|
||||
|
||||
const STRUCTURE_RANGE_ALERT: &str = "An integer between 1 and 7 is required. Closest value inserted.";
|
||||
const COLOR_RANGE_ALERT: &str = "An integer between 0 and 10 is required. Closest value inserted.";
|
||||
const STRUCTURE_TIP: &str = "Adjust how strictly the source structure is preserved";
|
||||
@@ -899,7 +894,20 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
|
||||
crate::widgets::checkbox(ui, &mut o.vibrance, tl!("Vibrance"));
|
||||
}
|
||||
Tool::Blur | Tool::Sharpen | Tool::Smudge => {
|
||||
pct(ui, tl!("Strength:"), &mut o.strength);
|
||||
let label = tl!("Strength:");
|
||||
opt(ui, label);
|
||||
// Only Blur's Strength is measured (#12650); a copy, so a shared fraction is not rounded.
|
||||
let out_of_range = if tool == Tool::Blur {
|
||||
let mut s = o.strength;
|
||||
let (r, alert) = crate::widgets::value_field_whole_alerting(ui, label, &mut s, 1.0..=100.0, "%", 58.0);
|
||||
if r.changed() {
|
||||
o.strength = s;
|
||||
}
|
||||
alert
|
||||
} else {
|
||||
crate::widgets::value_field(ui, &mut o.strength, 1.0..=100.0, "%", 58.0);
|
||||
false
|
||||
};
|
||||
if let Some(all) = o.sample_all.slot(tool) {
|
||||
crate::widgets::checkbox(ui, all, tl!("Sample All Layers"));
|
||||
}
|
||||
@@ -909,6 +917,9 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
|
||||
if tool == Tool::Smudge {
|
||||
crate::widgets::checkbox(ui, &mut o.finger_painting, tl!("Finger Painting"));
|
||||
}
|
||||
if out_of_range {
|
||||
crate::eraser_ui::open_range_alert(app);
|
||||
}
|
||||
}
|
||||
Tool::ColorReplacement => {
|
||||
// Photoshop's options bar: Mode, Sampling (three icon toggles), Limits, Tolerance, Anti-alias.
|
||||
|
||||
Reference in new issue
Block a user