fix(gradient): the Opacity field drops letters and the other separator (#18642)

The Classic gradient's Opacity field reads typed text with only the ASCII digits,
'-' and the locale's decimal separator kept: a7 is 7, 1e2 is 12, +20 is 20, and
with a decimal comma 33.6 reads 336 and is kept as 100. Text without a digit is
rejected as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
dholzricandClaude Opus 5.5 committed 2026-12-01 23:47:11 -06:00
1 parent 964a688207
commit 82dc9209de
2 files changed
+95 -3

No files matched your search

@@ -0,0 +1,70 @@
//! #18642: the Classic gradient's Opacity field drops every typed character but the digits, `-` and the locale's decimal
//! separator before it reads the value: "a7" is 7, "1e2" is 12, "+20" is 20, and the other separator is dropped, so with
//! a decimal comma "33.6" reads 336 and is kept as 100. Text without a digit is ignored.
use egui::accesskit::Role;
use egui::vec2;
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};
use serde_json::json;
use super::opacity_typed;
use crate::PhotocraftApp;
use crate::state::Tool;
/// The options bar of the Classic gradient at Opacity 50.
fn bar() -> Harness<'static, Box<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::Gradient;
app.ui.tool_options.gradient_classic = true;
app.ui.tool_options.fill_opacity = 50.0;
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<PhotocraftApp>| crate::panels::options_bar(app, ui), app);
h.run_steps(4);
h
}
/// Types `text` into the Opacity field and commits it with Enter.
fn type_opacity(h: &mut Harness<'static, Box<PhotocraftApp>>, text: &str) {
h.query_all_by_role(Role::SpinButton)
.find(|n| n.accesskit_node().label().as_deref().is_some_and(|l| l.trim_end_matches(':') == "Opacity"))
.expect("the Opacity 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 test locale reads `.` as the decimal separator, so the comma is the one dropped.
#[test]
fn typed_text_keeps_only_digits_minus_and_the_decimal_separator() {
let rows: [(&str, f32); 10] =
[("a7", 7.0), ("5%", 5.0), ("50abc", 50.0), ("1e2", 12.0), ("+20", 20.0), (" 7", 7.0), ("1,5", 15.0), ("12,34", 100.0), ("200%", 100.0), ("abc", 50.0)];
for (typed, kept) in rows {
let mut h = bar();
type_opacity(&mut h, typed);
assert_eq!(h.state().ui.tool_options.fill_opacity, kept, "{typed:?}");
}
}
/// With a decimal comma the period is dropped: "33.6" reads 336, past the field's 100.
#[test]
fn a_decimal_comma_drops_the_period() {
for (typed, read) in [("a7", 7.0), ("5%", 5.0), ("50abc", 50.0), ("1e2", 12.0), ("+20", 20.0), ("0.4", 4.0), (" 7", 7.0), ("50,5", 50.5)] {
assert_eq!(opacity_typed(typed, ','), Some(read), "{typed:?}");
}
for (typed, read) in [("33.6", 336.0), ("99.5", 995.0), ("100.6", 1006.0), ("200%", 200.0)] {
assert_eq!(opacity_typed(typed, ','), Some(read), "{typed:?}");
assert!(read > 100.0, "{typed:?} is past the range, which keeps 100");
}
assert_eq!(opacity_typed("abc", ','), None, "no digit: ignored");
}
+25 -3
View File
@@ -2,7 +2,7 @@
//! that opens the gradient presets, the style, and for the Classic gradient the blend mode and Opacity, then Reverse
//! and Dither.
use craft::behavior::IconId;
use craft::behavior::{IconId, NumberCodec};
use craft::color_ui::ui::gradient::GradientStops;
use craft::draw::ui::gradient::{GradientFillWords, GradientStyle, GradientSwatch};
use craft::ui::buttons::IconButton;
@@ -120,8 +120,15 @@ pub(crate) fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
}
labelled(ui, tl!("Opacity"), |ui| {
let field =
NumberField::new(tl!("Opacity").to_owned(), Bind::given(Some(f64::from(fill_opacity)))).range(1.0..=100.0).unit("%").scrub_by_label();
let reader = Env::of(ui).units().reader(None);
let decimal = reader.decimal_separator();
// Three decimals, as the field writes without a codec.
let codec = NumberCodec::new(reader, 3).parse_with(move |text| opacity_typed(text, decimal));
let field = NumberField::new(tl!("Opacity").to_owned(), Bind::given(Some(f64::from(fill_opacity))))
.range(1.0..=100.0)
.unit("%")
.scrub_by_label()
.codec(codec);
if let Some(next) = field.show(ui).value().copied().flatten() {
app.ui.tool_options.fill_opacity = next as f32;
}
@@ -206,3 +213,18 @@ fn preset_picker(app: &mut PhotocraftApp, ui: &mut egui::Ui, at: egui::Pos2) {
#[cfg(test)]
#[path = "../../gradient_mask_target_greyed_modes_tests.rs"]
mod gradient_mask_target_greyed_modes_tests;
#[cfg(test)]
#[path = "../../gradient_opacity_text_filter_tests.rs"]
mod gradient_opacity_text_filter_tests;
/// The Classic Opacity field's reading of typed text (#18642): every character but the ASCII digits, `-` and the
/// locale's decimal separator `decimal` is dropped first, so "a7" is 7, "1e2" is 12 and, with a decimal comma, "33.6"
/// is 336. `None` when no digit is typed or what is left is not one number.
pub(crate) fn opacity_typed(text: &str, decimal: char) -> Option<f64> {
if !text.chars().any(|c| c.is_ascii_digit()) {
return None;
}
let kept: String = text.chars().filter(|&c| c.is_ascii_digit() || c == '-' || c == decimal).map(|c| if c == decimal { '.' } else { c }).collect();
kept.parse::<f64>().ok().filter(|v| v.is_finite())
}