diff --git a/crates/ui-egui/src/dialogs/blend_options.rs b/crates/ui-egui/src/dialogs/blend_options.rs index 6d48c6ec82..955be98b05 100644 --- a/crates/ui-egui/src/dialogs/blend_options.rs +++ b/crates/ui-egui/src/dialogs/blend_options.rs @@ -2,7 +2,7 @@ //! Layers panel's, so a mode has one key and one shortcut everywhere. use craft::ui::patterns::form_dialogs::{DialogFields, Param, ParamKind}; -use craft::ui::runtime::env::Label; +use craft::ui::runtime::env::{Label, UnitKind}; use photocraft_color::BlendMode; use serde_json::{Map, Value, json}; @@ -25,7 +25,8 @@ pub(crate) fn blending_rows() -> Vec { vec![ Param::section(tl!("Blending")), blend_param("mode", tl!("Mode:"), false, BlendMode::Normal), - super::new_layer_ui::opacity_param(), + // Whole percents from 1 (New Layer's row starts at 0): a typed 0 is clamped to 1 with the range alert (#14649). + Param::new("opacity", tl!("Opacity:"), ParamKind::number(Some(1.0), Some(100.0), 100.0, 1.0, 0)).unit(UnitKind::Percent), Param::new("preserveTransparency", tl!("Preserve Transparency"), ParamKind::bool(false)), ] } diff --git a/crates/ui-egui/src/dialogs/fill_ui.rs b/crates/ui-egui/src/dialogs/fill_ui.rs index 7b460e473a..c8de3238f3 100644 --- a/crates/ui-egui/src/dialogs/fill_ui.rs +++ b/crates/ui-egui/src/dialogs/fill_ui.rs @@ -148,5 +148,8 @@ pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamRes } } +#[cfg(test)] +mod opacity_alert_tests; + #[cfg(test)] mod tests; diff --git a/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs new file mode 100644 index 0000000000..52d7fa585f --- /dev/null +++ b/crates/ui-egui/src/dialogs/fill_ui/opacity_alert_tests.rs @@ -0,0 +1,111 @@ +//! #14649: the Opacity row that Edit › Fill and Edit › Stroke share takes whole percents from 1 to 100. A typed value +//! past them is clamped, once the field is left, with the range alert titled after the dialog; letters restore the +//! previous value without one. + +use egui::accesskit::Role; +use egui::vec2; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use crate::PhotocraftApp; + +type H = Harness<'static, Box>; + +/// A document with a selection and the dialog `open` opens, hosted with its alerts. +fn dialog(open: fn(&mut PhotocraftApp)) -> H { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 40, "height": 30})).unwrap(); + app.run("select.rect", json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); + crate::theme::install_fonts(app.ctx()); + crate::theme::apply(app.ctx(), &crate::theme::AppTheme::pro()); + open(&mut app); + let mut h = Harness::builder() + .with_context(app.ctx().clone()) + .with_size(vec2(1200.0, 900.0)) + .with_step_dt(1.0 / 60.0) + .build_ui_state(|ui, app: &mut Box| crate::dialogs::run(app, ui), app); + h.run_steps(4); + h +} + +fn fill(app: &mut PhotocraftApp) { + super::open(app); +} + +fn stroke(app: &mut PhotocraftApp) { + crate::dialogs::stroke_ui::open(app); +} + +fn opacity<'a>(h: &'a H) -> egui_kittest::Node<'a> { + h.query_all_by_role(Role::SpinButton).find(|n| n.accesskit_node().label().is_some_and(|l| l.trim_end_matches(':') == "Opacity")).expect("the Opacity field") +} + +fn value(h: &H) -> Option { + opacity(h).accesskit_node().numeric_value() +} + +/// Types `text` over Opacity and leaves the field with Tab. +fn type_and_tab(h: &mut H, text: &str) { + let at = opacity(h).rect().center(); + h.hover_at(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); + } + 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(4); +} + +/// The message of the open alert, if any. +fn alert(h: &H) -> Option { + let dialogs = h.state().framework.dialogs.open_dialogs(); + dialogs.iter().find(|d| d.key.as_deref() == Some("alert")).and_then(|d| d.fields.as_ref()?.get("message")?.as_str().map(str::to_string)) +} + +/// Answers the open alert, leaving the dialog `command` open. +fn dismiss(h: &mut H, command: &str) { + let dialogs = h.state().framework.dialogs.open_dialogs(); + for id in dialogs.iter().filter(|d| d.key.as_deref() != Some(command)).map(|d| d.id) { + h.state_mut().framework.dialogs.close_by_id(id); + } + h.run_steps(3); +} + +fn titles(h: &H) -> Vec { + h.query_all_by_role(Role::Dialog).chain(h.query_all_by_role(Role::AlertDialog)).filter_map(|n| n.accesskit_node().label()).collect() +} + +fn check(open: fn(&mut PhotocraftApp), command: &str, title: &str) { + let mut h = dialog(open); + for (typed, kept) in [("0", 1.0), ("150", 100.0), ("50", 50.0), ("12.3", 100.0)] { + type_and_tab(&mut h, typed); + assert_eq!(value(&h), Some(kept), "{title}: typed {typed}"); + if kept == 50.0 { + assert_eq!(alert(&h), None, "{title}: {typed} is in range"); + continue; + } + let message = alert(&h).unwrap_or_else(|| panic!("{title}: {typed} raises the range alert")); + assert!(message.contains("integer between 1") && message.contains("100"), "{title}: {message}"); + assert!(titles(&h).iter().all(|t| t == title), "{title}: the alert is titled after the dialog: {:?}", titles(&h)); + dismiss(&mut h, command); + } + type_and_tab(&mut h, "42"); + type_and_tab(&mut h, "abc"); + assert_eq!(value(&h), Some(42.0), "{title}: letters restore the previous value"); + assert_eq!(alert(&h), None, "{title}: letters raise no alert"); +} + +#[test] +fn fill_opacity_takes_1_to_100_with_the_range_alert() { + check(fill, super::COMMAND, "Fill"); +} + +#[test] +fn stroke_opacity_takes_1_to_100_with_the_range_alert() { + check(stroke, crate::dialogs::stroke_ui::COMMAND, "Stroke"); +}