mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Eraser: Opacity, Flow and Smoothing out of range show the Closest value inserted alert (#11938)
On the Eraser's bar, Opacity and Flow take whole percents 1-100 and Smoothing 0-100; a value typed past the range keeps the closest one and, once the field is left, the reference application's alert says so with OK. A new widgets::value_field_whole_alerting reports the clamp. The Brush's bar (not measured) keeps today's silent clamp.
This commit is contained in:
1 parent
e04ecbdaa3
commit
b9e438ed54
5 files changed
+223
-5
No files matched your search
@@ -0,0 +1,120 @@
|
||||
//! #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");
|
||||
}
|
||||
@@ -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).
|
||||
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();
|
||||
|
||||
@@ -179,8 +179,13 @@ fn brush_specific_options(app: &mut PhotocraftApp, ui: &mut egui::Ui, t: Tokens,
|
||||
}
|
||||
}
|
||||
ui.add_enabled_ui(!block, |ui| {
|
||||
// A whole number from 1 to 100, as in Photoshop: 0 paints nothing (#11812).
|
||||
tip(whole_percent_field(ui, tl!("Opacity"), &mut b.opacity, 1.0..=100.0, 62.0), tl!("Set opacity for stroke"));
|
||||
// A whole number from 1 to 100, as in Photoshop: 0 paints nothing (#11812). The
|
||||
// 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);
|
||||
if tip(pressure, tl!("Always use Pressure for Opacity. When off, Brush Preset controls pressure")).clicked() {
|
||||
b.pressure_opacity = !b.pressure_opacity;
|
||||
@@ -188,14 +193,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).
|
||||
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"));
|
||||
if crate::brush_picker::named(airbrush, "Enable airbrush-style build-up effects").clicked() {
|
||||
b.build_up = !b.build_up;
|
||||
}
|
||||
});
|
||||
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);
|
||||
widgets::vline(ui, 22.0);
|
||||
let pressure = pressure_button(ui, b.pressure_size, tl!("Always use pressure for size"), brush);
|
||||
@@ -836,6 +849,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)
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// `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 {
|
||||
@@ -852,3 +890,7 @@ fn hint(ui: &mut egui::Ui, s: &str) {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
ui.label(RichText::new(s).color(t.text_faint));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../eraser_range_alert_tests.rs"]
|
||||
mod eraser_range_alert_tests;
|
||||
@@ -50,7 +50,8 @@ pub(crate) use fields::compact_wrapping_value_field;
|
||||
#[cfg(test)]
|
||||
use fields::{FieldOpts, NUMERIC_INPUT_LIMIT, value_field_in};
|
||||
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 use panel::{
|
||||
|
||||
@@ -12,6 +12,49 @@ 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
|
||||
}
|
||||
|
||||
/// 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)));
|
||||
let draft = ui.ctx().data(|d| d.get_temp::<(f32, f32)>(draft_id));
|
||||
let mut typed = draft.filter(|(_, stored)| *stored == *value).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)));
|
||||
} else {
|
||||
ui.ctx().data_mut(|d| d.remove_temp::<(f32, f32)>(draft_id));
|
||||
}
|
||||
}
|
||||
if !r.has_focus() {
|
||||
ui.ctx().data_mut(|d| d.remove_temp::<(f32, f32)>(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
|
||||
/// 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 {
|
||||
|
||||
Reference in new issue
Block a user