From 16218e8c824bfdc2c49c79b7eb7b867e7ce2ceda Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 03:26:28 +0100 Subject: [PATCH] Fill and Stroke: a typed fraction in Opacity raises the integer alert (#17483) The Opacity row Fill and Stroke share takes whole percents only (Param::whole_only, storytold/craft-libs#4383): a typed fraction is rounded half up with the integer alert titled after the dialog, and the Enter that commits it raises the alert and leaves the dialog open at the rounded value instead of filling. Stacked on #18273, the pin that carries it. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs/blend_options.rs | 5 +- crates/ui-egui/src/dialogs/fill_ui.rs | 2 + .../dialogs/fill_ui/opacity_fraction_tests.rs | 181 ++++++++++++++++++ 3 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/dialogs/fill_ui/opacity_fraction_tests.rs diff --git a/crates/ui-egui/src/dialogs/blend_options.rs b/crates/ui-egui/src/dialogs/blend_options.rs index 19262911f5..68986a3536 100644 --- a/crates/ui-egui/src/dialogs/blend_options.rs +++ b/crates/ui-egui/src/dialogs/blend_options.rs @@ -45,8 +45,9 @@ pub(crate) fn blending_rows(app: &PhotocraftApp, forced: Option) -> Vec>; + +/// A white document with a selection, the app's state before the dialog `open` opens. +fn document() -> Box { + 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(); + app +} + +/// The document with the dialog `open` opens, hosted with its alerts. +fn dialog(open: fn(&mut PhotocraftApp)) -> H { + let mut app = document(); + 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); +} + +/// `whole` and `fraction` written with the decimal symbol the app's number fields read ("12,3" on a comma region). +fn decimal(whole: &str, fraction: &str) -> String { + let format = crate::i18n::framework_i18n().format(); + let symbol: String = format.number_ungrouped(0.5, 1).chars().filter(|c| !c.is_ascii_digit()).collect(); + format!("{whole}{symbol}{fraction}") +} + +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 `key`. +fn type_over(h: &mut H, text: &str, key: egui::Key) { + 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(key); + 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)) +} + +/// Whether the dialog `command` is still open. +fn is_open(h: &H, command: &str) -> bool { + h.state().framework.dialogs.open_dialogs().iter().any(|d| d.key.as_deref() == Some(command)) +} + +/// 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); +} + +/// The names of the open dialog windows, unnamed ones included. +fn titles(h: &H) -> Vec> { + h.query_all_by_role(Role::Dialog).chain(h.query_all_by_role(Role::AlertDialog)).map(|n| n.accesskit_node().label()).collect() +} + +fn check(open: fn(&mut PhotocraftApp), command: &str, title: &str) { + let mut h = dialog(open); + let rounded = [ + (("12", "3"), 12.0), + (("12", "4"), 12.0), + (("12", "5"), 13.0), + (("12", "7"), 13.0), + (("50", "5"), 51.0), + (("1", "5"), 2.0), + (("99", "5"), 100.0), + (("50", "01"), 50.0), + // Past the range: the alert and the clamp it raised before. + (("0", "4"), 1.0), + (("150", "5"), 100.0), + ]; + for ((whole, fraction), kept) in rounded { + let typed = decimal(whole, fraction); + type_over(&mut h, &typed, egui::Key::Tab); + assert_eq!(value(&h), Some(kept), "{title}: typed {typed}"); + let message = alert(&h).unwrap_or_else(|| panic!("{title}: {typed} raises the integer alert")); + assert!(message.contains("integer between 1") && message.contains("100"), "{title}: {message}"); + let windows = titles(&h); + assert!(windows.len() == 2 && windows.iter().all(|t| t.as_deref() == Some(title)), "{title}: the dialog and its alert: {windows:?}"); + dismiss(&mut h, command); + } + for ((whole, fraction), kept) in [(("50", "0"), 50.0), (("50", "00"), 50.0), (("12", ""), 12.0)] { + let typed = decimal(whole, fraction); + type_over(&mut h, &typed, egui::Key::Tab); + assert_eq!(value(&h), Some(kept), "{title}: typed {typed}"); + assert_eq!(alert(&h), None, "{title}: {typed} is a whole number"); + } +} + +#[test] +fn fill_opacity_rounds_a_typed_fraction_with_the_integer_alert() { + check(fill, super::COMMAND, "Fill"); +} + +#[test] +fn stroke_opacity_rounds_a_typed_fraction_with_the_integer_alert() { + check(stroke, crate::dialogs::stroke_ui::COMMAND, "Stroke"); +} + +/// The red of a pixel inside the selection. +fn red(app: &PhotocraftApp) -> f32 { + let st = app.session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(7, 7)[0] +} + +/// The red a scripted foreground fill at `opacity` leaves inside the selection. +fn scripted(opacity: f64) -> f32 { + let mut app = document(); + app.run(super::COMMAND, json!({"contents": "foreground", "opacity": opacity})).unwrap(); + red(&app) +} + +/// Enter over 20.5 raises the alert and applies nothing; after the alert the dialog is still open at 21, and its OK +/// fills at 21 %, not at 20.5 %. +#[test] +fn enter_over_a_fraction_raises_the_alert_and_does_not_fill() { + let mut h = dialog(fill); + let (history, before) = (h.state().session.active().unwrap().history.past_len(), red(h.state())); + type_over(&mut h, &decimal("20", "5"), egui::Key::Enter); + assert!(alert(&h).is_some(), "Enter over a fraction raises the integer alert"); + assert!(is_open(&h, super::COMMAND), "the dialog stays open behind the alert"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), history, "no Fill state is added"); + assert_eq!(red(h.state()), before, "no pixel changes"); + dismiss(&mut h, super::COMMAND); + assert!(is_open(&h, super::COMMAND), "the dialog is still open after the alert"); + assert_eq!(value(&h), Some(21.0), "it shows the rounded value"); + + h.get_by_label("OK").click(); + h.run_steps(4); + assert!(!is_open(&h, super::COMMAND), "OK applies"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), history + 1, "one Fill state"); + assert_eq!(red(h.state()), scripted(21.0), "the fill is at 21 %"); + assert_ne!(red(h.state()), scripted(20.5), "not at the typed 20.5 %"); +}