Merge #18204 into the merge queue batch

fix(ui): show the Background Eraser's pen warning next to the control that needs it (#18059)
This commit is contained in:
David Mann committed 2026-11-30 14:56:12 +01:00
commit fdddb30a7a
2 files changed
+99 -7

No files matched your search

+31 -7
View File
@@ -1,8 +1,8 @@
//! The Background Eraser's compact brush popup (#11910): the Brush Preset picker's compact form
//! (no preset list, search or gear) that its options-bar chip and a canvas right-click open, as in
//! Photoshop. The picker's head shows Size, Hardness and the tip widget; [`rows`] adds Spacing and
//! the "Size:" and "Tolerance:" pen selects (Off · Pen Pressure · Stylus Wheel), the latter with the
//! reference's tablet warning.
//! the "Size:" and "Tolerance:" pen selects (Off · Pen Pressure · Stylus Wheel), each with the
//! reference's tablet warning while it is set to a pen control (#18059).
//!
//! The rows edit the caller's copy of the brush; the Tolerance control is a tool option
//! (`ToolOptions::bg_tolerance_control`).
@@ -22,9 +22,15 @@ use photocraft_engine::paint::Control;
pub const OFF: &str = "off";
pub const PEN_PRESSURE: &str = "penPressure";
pub const STYLUS_WHEEL: &str = "stylusWheel";
/// The tooltip of the warning triangle next to "Tolerance:".
/// The tooltip of the warning triangle next to a pen select set to a pen control.
pub const PRESSURE_WARNING: &str = "Control by pen pressure requires the use of a pressure sensitive tablet";
/// Whether a pen select set to `control` shows the tablet warning: Pen Pressure and Stylus Wheel do,
/// Off does not (#18059).
pub fn warns(control: &str) -> bool {
matches!(control, PEN_PRESSURE | STYLUS_WHEEL)
}
fn pen_entries() -> [Entry<String>; 3] {
[
Entry::new(OFF.to_string(), tl!("Off")),
@@ -56,8 +62,13 @@ pub fn set_size_control(b: &mut BrushSettings, control: &str) {
}
}
/// The reference's tablet warning triangle, next to a pen select set to a pen control.
fn warning(ui: &mut egui::Ui) {
Icon::new(IconSpec::svg(IconId::new("warning"))).tone(Tone::Warn).label(tl!(PRESSURE_WARNING).to_owned()).show(ui);
}
/// The app rows of the compact picker, under its head: Spacing, the "Size:" and "Tolerance:" selects
/// and the tablet warning. They edit `brush` and the tool's Tolerance control. Returns the coalesce
/// and their tablet warnings. They edit `brush` and the tool's Tolerance control. Returns the coalesce
/// key of a Spacing edit, so the caller commits a scrub as one history step.
pub fn rows(ui: &mut egui::Ui, brush: &mut BrushSettings, tolerance_control: &mut String) -> Option<String> {
let mut key = None;
@@ -69,16 +80,29 @@ pub fn rows(ui: &mut egui::Ui, brush: &mut BrushSettings, tolerance_control: &mu
key = out.events.iter().find_map(|e| crate::history_keys::coalesce("brush", e));
}
let entries = pen_entries();
let size = Select::new(tl!("Size:"), Bind::given(size_control(brush).to_string()), &entries).show(ui).events.committed().cloned();
if let Some(control) = size {
let size = Row::new().show(ui, |ui, _| {
let control = size_control(brush);
let picked = Select::new(tl!("Size:"), Bind::given(control.to_string()), &entries).show(ui).events.committed().cloned();
if warns(control) {
warning(ui);
}
picked
});
if let Some(Some(control)) = size.inner {
set_size_control(brush, &control);
}
Row::new().show(ui, |ui, _| {
let picked = Select::new(tl!("Tolerance:"), Bind::given(tolerance_control.clone()), &entries).show(ui).events.committed().cloned();
if warns(tolerance_control) {
warning(ui);
}
if let Some(control) = picked {
*tolerance_control = control;
}
Icon::new(IconSpec::svg(IconId::new("warning"))).tone(Tone::Warn).label(tl!(PRESSURE_WARNING).to_owned()).show(ui);
});
key
}
#[cfg(test)]
#[path = "bg_eraser_popup_warning_tests.rs"]
mod warning_tests;
@@ -0,0 +1,68 @@
//! The Background Eraser's compact popup shows its tablet warning next to each pen select set to a
//! pen control, and hides it for Off (#18059): measured in the reference application as one triangle
//! for one control on Pen Pressure, two when both are, none when both are Off.
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use super::*;
#[test]
fn a_pen_select_on_pen_pressure_or_stylus_wheel_warns() {
for control in [PEN_PRESSURE, STYLUS_WHEEL] {
assert!(warns(control), "{control}");
}
assert!(!warns(OFF), "Off");
}
#[test]
fn the_size_control_alone_decides_its_own_warning() {
let mut b = BrushSettings::default();
assert!(!warns(size_control(&b)), "a fresh brush is Off");
set_size_control(&mut b, PEN_PRESSURE);
assert!(warns(size_control(&b)), "Pen Pressure");
set_size_control(&mut b, OFF);
assert!(!warns(size_control(&b)), "Off again");
set_size_control(&mut b, STYLUS_WHEEL);
assert!(warns(size_control(&b)), "Stylus Wheel");
}
#[test]
fn the_tolerance_control_alone_decides_its_own_warning() {
// The entries are the same three strings, stored as they are (the rows hand them to
// `Select` and keep whatever it commits), so each warns on its own.
let rows = [(OFF, false), (PEN_PRESSURE, true), (STYLUS_WHEEL, true)];
for (control, warned) in rows {
assert_eq!(warns(control), warned, "{control}");
}
// One control on and the other off shows exactly one triangle, as measured.
assert_ne!(warns(PEN_PRESSURE), warns(OFF));
}
/// Renders the rows with the given controls and counts the warning triangles, which carry
/// [`PRESSURE_WARNING`] as their accessible name.
fn rendered_warnings(size: &str, tolerance: &str) -> usize {
let mut brush = BrushSettings::default();
set_size_control(&mut brush, size);
let mut tolerance = tolerance.to_string();
let mut h = Harness::new_ui(move |ui| {
rows(ui, &mut brush, &mut tolerance);
});
h.run();
h.query_all_by_label(PRESSURE_WARNING).count()
}
#[test]
fn each_pen_select_shows_a_triangle_only_while_it_is_on_a_pen_control() {
let cases = [
(OFF, OFF, 0),
(PEN_PRESSURE, OFF, 1),
(OFF, PEN_PRESSURE, 1),
(PEN_PRESSURE, PEN_PRESSURE, 2),
(OFF, STYLUS_WHEEL, 1),
(STYLUS_WHEEL, STYLUS_WHEEL, 2),
];
for (size, tolerance, wanted) in cases {
assert_eq!(rendered_warnings(size, tolerance), wanted, "Size: {size}, Tolerance: {tolerance}");
}
}