mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #18052 from davidmann4/fix/17483-opacity-fraction-alert
Fill and Stroke: a typed fraction in Opacity raises the integer alert (#17483)
This commit is contained in:
3 files changed
+186
-2
No files matched your search
@@ -45,8 +45,9 @@ pub(crate) fn blending_rows(app: &PhotocraftApp, forced: Option<bool>) -> Vec<Pa
|
||||
vec![
|
||||
Param::section(tl!("Blending")),
|
||||
document_mode_param(app, tl!("Mode:")),
|
||||
// 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),
|
||||
// Whole percents from 1 (New Layer's row starts at 0): a typed 0 is clamped to 1 with the range alert (#14649),
|
||||
// and a typed fraction is rounded half up with the same alert, its Enter leaving the dialog open (#17483).
|
||||
Param::new("opacity", tl!("Opacity:"), ParamKind::number(Some(1.0), Some(100.0), 100.0, 1.0, 0)).unit(UnitKind::Percent).whole_only(),
|
||||
preserve,
|
||||
]
|
||||
}
|
||||
|
||||
@@ -289,6 +289,8 @@ mod no_selection_fallback_tests;
|
||||
#[cfg(test)]
|
||||
mod opacity_alert_tests;
|
||||
#[cfg(test)]
|
||||
mod opacity_fraction_tests;
|
||||
#[cfg(test)]
|
||||
mod preserve_transparency_state_tests;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,181 @@
|
||||
//! #17483: the Opacity row that Edit › Fill and Edit › Stroke share takes whole percents only. A typed fraction is
|
||||
//! rounded half up with the integer alert titled after the dialog, a zero fraction or a trailing decimal symbol is a
|
||||
//! whole number, and the Enter that commits a fraction raises the alert and leaves the dialog open with the rounded
|
||||
//! value instead of applying. The numbers are typed with the decimal symbol the app reads, so the tests pass on any
|
||||
//! machine region; the framework's own trial types both a comma and a point.
|
||||
|
||||
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<PhotocraftApp>>;
|
||||
|
||||
/// A white document with a selection, the app's state before the dialog `open` opens.
|
||||
fn document() -> Box<PhotocraftApp> {
|
||||
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<PhotocraftApp>| 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<f64> {
|
||||
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<String> {
|
||||
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<Option<String>> {
|
||||
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 %");
|
||||
}
|
||||
Reference in new issue
Block a user