diff --git a/crates/engine/src/cmd/retouch_cmds.rs b/crates/engine/src/cmd/retouch_cmds.rs index 868a5428ba..142ac973ed 100644 --- a/crates/engine/src/cmd/retouch_cmds.rs +++ b/crates/engine/src/cmd/retouch_cmds.rs @@ -411,7 +411,7 @@ pub fn specs() -> Vec { menu: &[], shortcut: None, params: brush_params!( - r#","state":row (a History panel row, as history.setSource),"snapshot":id (a history.newSnapshot id; default: the History panel's source, as paint.historyBrush; a source of another canvas size or colour mode is an error),"style":"tightShort|tightMedium|tightLong|looseMedium|looseLong|dab|tightCurl|looseCurl"="tightShort","area":0..100=50,"tolerance":0..100=0,"opacity":0..100=100 (overrides the common 1..100: how strongly the history state is painted; 0 changes nothing; no flow, mode is Normal)"# + r#","state":row (a History panel row, as history.setSource),"snapshot":id (a history.newSnapshot id; default: the History panel's source, as paint.historyBrush; a source of another canvas size or colour mode is an error),"style":"tightShort|tightMedium|tightLong|looseMedium|looseLong|dab|tightCurl|looseCurl"="tightShort","area":0..500=50,"tolerance":0..100=0,"opacity":0..100=100 (overrides the common 1..100: how strongly the history state is painted; 0 changes nothing; no flow, mode is Normal)"# ), enabled: has_pixel_layer, run: art_history::run, diff --git a/crates/engine/src/cmd/retouch_cmds/art_history.rs b/crates/engine/src/cmd/retouch_cmds/art_history.rs index a38b510f3e..474d8ec4d9 100644 --- a/crates/engine/src/cmd/retouch_cmds/art_history.rs +++ b/crates/engine/src/cmd/retouch_cmds/art_history.rs @@ -204,7 +204,8 @@ pub fn run(s: &mut Session, p: &Value) -> Result { let hardness = ranged(p, "hardness", s.tools.brush.hardness * 100.0, 0.0, 100.0)? / 100.0; let opacity = ranged(p, "opacity", 100.0, 0.0, 100.0)? / 100.0; let flow = ranged(p, "flow", 100.0, 0.0, 100.0)? / 100.0; - let area = ranged(p, "area", 50.0, 0.0, 100.0)?; + // Area takes 0..500 px, as the reference's field does (#12630). + let area = ranged(p, "area", 50.0, 0.0, 500.0)?; // Percent of the channel range; 0 (paint everywhere) is the default, as in Photoshop. let tolerance = ranged(p, "tolerance", 0.0, 0.0, 100.0)? / 100.0; let style = style_of(p)?; diff --git a/crates/ui-egui/src/panels/options_bar.rs b/crates/ui-egui/src/panels/options_bar.rs index f3987dd75f..d6fd9ce55b 100644 --- a/crates/ui-egui/src/panels/options_bar.rs +++ b/crates/ui-egui/src/panels/options_bar.rs @@ -240,10 +240,16 @@ fn stamp_members(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) { edit_brush(app, &flow, |b, v| b.flow = v); return; } - if matches!(tool, Tool::CloneStamp | Tool::HistoryBrush | Tool::ArtHistoryBrush) { + if matches!(tool, Tool::CloneStamp | Tool::HistoryBrush) { let opacity = percent_field(ui, tl!("Opacity"), brush.opacity, 1.0..=100.0); edit_brush(app, &opacity, |b, v| b.opacity = v); } + // The Art History Brush's Opacity carries the reference's tooltip (#12630). + if tool == Tool::ArtHistoryBrush { + let opacity = number_field_with_tip(ui, tl!("Opacity"), brush.opacity * 100.0, 1.0..=100.0, "%", Some(tl!("Set opacity for stroke"))) + .map(|percent| percent / 100.0); + edit_brush(app, &opacity, |b, v| b.opacity = v); + } if matches!(tool, Tool::CloneStamp | Tool::HistoryBrush) { let flow = percent_field(ui, tl!("Flow"), brush.flow, 1.0..=100.0); edit_brush(app, &flow, |b, v| b.flow = v); @@ -276,9 +282,18 @@ pub(super) fn check(ui: &mut egui::Ui, label: &str, value: &mut bool) { /// A number field named `label` over `value` (in `unit` when it is not empty), limited to `range`. The events of this /// pass carry the edited value; an emptied field gives none. pub(super) fn number_field(ui: &mut egui::Ui, label: &str, value: f32, range: RangeInclusive, unit: &'static str) -> EditEvents { + number_field_with_tip(ui, label, value, range, unit, None) +} + +/// [`number_field`] with `tip` as the field's tooltip when there is one. +fn number_field_with_tip(ui: &mut egui::Ui, label: &str, value: f32, range: RangeInclusive, unit: &'static str, tip: Option<&str>) -> EditEvents { let events = labelled(ui, label, |ui| { let field = NumberField::new(label.to_owned(), Bind::given(Some(f64::from(value)))).range(range.clone()).scrub_by_label(); let field = if unit.is_empty() { field } else { field.unit(unit) }; + let field = match tip { + Some(tip) => field.tooltip(tip.to_owned()), + None => field, + }; field.show(ui).events.clone() }); events.unwrap_or_default().filter_map(|v| v.map(|v| v as f32)) diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 8080910375..a9817e05e9 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -51,6 +51,9 @@ pub mod type_tool; pub(super) mod type_transform; pub mod vector_ui; +#[cfg(test)] +mod art_history_fields_tests; + #[cfg(test)] mod art_history_default_style_tests; #[cfg(test)] diff --git a/crates/ui-egui/src/tools/art_history_fields_tests.rs b/crates/ui-egui/src/tools/art_history_fields_tests.rs new file mode 100644 index 0000000000..5047a0a7fb --- /dev/null +++ b/crates/ui-egui/src/tools/art_history_fields_tests.rs @@ -0,0 +1,125 @@ +//! #12630: the Art History Brush's Area takes whole pixels 0..500 and Tolerance whole percents +//! 0..100; a value typed past either range becomes the closest one with the reference's "Closest +//! value inserted" alert. Opacity, Area and Tolerance carry the reference's tooltips, and the bar +//! has no hint text. + +use egui::accesskit::Role; +use egui::vec2; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use super::retouch_ui::bar::history::{ART_HISTORY_AREA_ALERT, ART_HISTORY_TOLERANCE_ALERT}; +use crate::PhotocraftApp; +use crate::state::Tool; + +type H = Harness<'static, Box>; + +/// The options bar with the Art History Brush active, and the dialog host its alerts open on. +fn bar() -> H { + 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::ArtHistoryBrush; + crate::theme::install_fonts(app.ctx()); + crate::theme::apply(app.ctx(), &crate::theme::AppTheme::pro()); + let mut h = Harness::builder().with_context(app.ctx().clone()).with_size(vec2(1800.0, 300.0)).with_step_dt(1.0 / 60.0).build_ui_state( + move |ui, app: &mut Box| { + crate::panels::options_bar(app, ui); + crate::dialogs::run(app, ui); + }, + app, + ); + h.run_steps(4); + h +} + +/// The number field named `label`, with or without its caption's colon. +fn field<'a>(h: &'a H, label: &str) -> Option> { + h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().is_some_and(|l| l.trim_end_matches(':') == label)) +} + +/// Types `text` into the field named `label` and commits it with Enter. +fn type_into(h: &mut H, label: &str, text: &str) { + field(h, label).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::Enter); + h.run_steps(3); +} + +/// The messages of the open framework alerts. +fn alerts(h: &H) -> Vec { + h.state().framework.dialogs.open_dialogs().iter().filter_map(|d| d.fields.as_ref()?.get("message")?.as_str().map(str::to_string)).collect() +} + +/// The description (tooltip) of the number field named `label`. +fn tip(h: &H, label: &str) -> Option { + field(h, label)?.accesskit_node().description().map(|d| d.to_string()) +} + +#[test] +fn area_past_0_500_takes_the_closest_value_and_alerts() { + for (typed, want) in [("600", 500.0), ("-5", 0.0)] { + let mut h = bar(); + type_into(&mut h, "Area", typed); + assert_eq!(h.state().ui.tool_options.art_history_area, want, "{typed}"); + assert_eq!(alerts(&h), vec![ART_HISTORY_AREA_ALERT.to_string()], "{typed}: one alert"); + } +} + +#[test] +fn area_in_range_is_a_whole_number_without_an_alert() { + for (typed, want) in [("500", 500.0), ("250", 250.0), ("0", 0.0), ("37.6", 38.0)] { + let mut h = bar(); + type_into(&mut h, "Area", typed); + assert_eq!(h.state().ui.tool_options.art_history_area, want, "{typed}"); + assert!(alerts(&h).is_empty(), "{typed} is in range: {:?}", alerts(&h)); + } +} + +#[test] +fn tolerance_past_0_100_takes_the_closest_value_and_alerts() { + for (typed, want) in [("150", 100.0), ("-3", 0.0)] { + let mut h = bar(); + type_into(&mut h, "Tolerance", typed); + assert_eq!(h.state().ui.tool_options.art_history_tolerance, want, "{typed}"); + assert_eq!(alerts(&h), vec![ART_HISTORY_TOLERANCE_ALERT.to_string()], "{typed}: one alert"); + } +} + +#[test] +fn tolerance_in_range_is_a_whole_number_without_an_alert() { + for (typed, want) in [("100", 100.0), ("12.4", 12.0)] { + let mut h = bar(); + type_into(&mut h, "Tolerance", typed); + assert_eq!(h.state().ui.tool_options.art_history_tolerance, want, "{typed}"); + assert!(alerts(&h).is_empty(), "{typed} is in range"); + } +} + +#[test] +fn opacity_area_and_tolerance_have_the_reference_tooltips() { + let h = bar(); + assert_eq!(tip(&h, "Opacity").as_deref(), Some("Set opacity for stroke")); + assert_eq!(tip(&h, "Area").as_deref(), Some("Painting area diameter")); + assert_eq!(tip(&h, "Tolerance").as_deref(), Some("Painting tolerance")); +} + +#[test] +fn the_bar_has_no_hint_text() { + let h = bar(); + assert!(h.query_by_label("Stylized dabs from a history state").is_none()); +} + +#[test] +fn the_command_takes_an_area_up_to_500() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 80, "height": 80})).unwrap(); + let stroke = |area: f64| json!({"points": [[10, 40], [70, 40]], "size": 8, "area": area}); + assert!(app.run("paint.artHistoryBrush", stroke(500.0)).is_ok()); + assert!(app.run("paint.artHistoryBrush", stroke(250.0)).is_ok()); + assert!(app.run("paint.artHistoryBrush", stroke(500.5)).is_err()); + assert!(app.run("paint.artHistoryBrush", stroke(-1.0)).is_err()); +} diff --git a/crates/ui-egui/src/tools/retouch_ui/bar/history.rs b/crates/ui-egui/src/tools/retouch_ui/bar/history.rs index ad92a10acc..45d908c1d5 100644 --- a/crates/ui-egui/src/tools/retouch_ui/bar/history.rs +++ b/crates/ui-egui/src/tools/retouch_ui/bar/history.rs @@ -50,7 +50,7 @@ pub(super) fn history_brush(ui: &mut egui::Ui, app: &mut PhotocraftApp) -> Optio None } -/// Art History Brush: Style, Area, Tolerance. +/// Art History Brush: Style, Area, Tolerance. Its alerts carry the reference's words, so it opens them itself. pub(super) fn art_history(ui: &mut egui::Ui, app: &mut PhotocraftApp) -> Option { let styles: Vec> = [ ("tightShort", tl!("Tight Short")), @@ -69,13 +69,30 @@ pub(super) fn art_history(ui: &mut egui::Ui, app: &mut PhotocraftApp) -> Option< Field::new(tl!("Style")).side(SideLabel::new().scrub(true)).sizing(Sizing::Content).show(ui, |ui, _| { Select::new(tl!("Style"), Bind::bound(&mut o.art_history_style), &styles).show(ui); }); - let area = number(tl!("Area"), &mut o.art_history_area, 0.0..=100.0).show(ui).out_of_range; - let tolerance = number(tl!("Tolerance"), &mut o.art_history_tolerance, 0.0..=100.0).show(ui).out_of_range; - Divider::vertical().show(ui); - hint(ui, tl!("Stylized dabs from a history state")); - area.or(tolerance) + // Area: whole pixels 0..500, no slider; Tolerance: whole percents 0..100 with a slider. A value + // typed past either range becomes the closest one with the reference's own alert (#12630). + let area = number(tl!("Area"), &mut o.art_history_area, 0.0..=500.0).whole().unit("px").tooltip(tl!("Painting area diameter")).show(ui).out_of_range; + let tolerance = number(tl!("Tolerance"), &mut o.art_history_tolerance, 0.0..=100.0) + .whole() + .unit("%") + .popup_slider() + .tooltip(tl!("Painting tolerance")) + .show(ui) + .out_of_range; + if area.is_some() { + let _ = crate::dialogs::alerts::alert(app, tl!(ART_HISTORY_AREA_ALERT)); + } else if tolerance.is_some() { + let _ = crate::dialogs::alerts::alert(app, tl!(ART_HISTORY_TOLERANCE_ALERT)); + } + None } +/// The reference's alert for an Art History Brush Area typed past 0..500 px (#12630). +pub(crate) const ART_HISTORY_AREA_ALERT: &str = "An integer between 0 pixel and 500 Pixels is required. Closest value inserted."; + +/// The reference's alert for an Art History Brush Tolerance typed past 0..100 (#12630). +pub(crate) const ART_HISTORY_TOLERANCE_ALERT: &str = "An integer between 0 and 100 is required. Closest value inserted."; + /// Pattern Stamp: Aligned, Impressionist, Pattern, Scale, Angle. pub(super) fn pattern_stamp(ui: &mut egui::Ui, app: &mut PhotocraftApp) -> Option { let patterns: Vec> = app.session.patterns.items.iter().map(|p| Entry::new(p.id.clone(), p.display_name().to_string())).collect();