mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Paint Bucket: alert when a typed Tolerance fraction is rounded
The reference application rounds a fractional Tolerance (12.7 becomes 13) and shows "An integer between 0 and 255 is required. Closest value inserted." (#12533). The Tolerance cell now commits the typed fraction, rounds it to the closest whole number and opens that alert. Opacity keeps rounding a typed fraction without an alert.
This commit is contained in:
1 parent
750a67a96d
commit
bf4fca7232
3 files changed
+70
-9
No files matched your search
@@ -1,6 +1,7 @@
|
||||
//! #12533: the Paint Bucket's Tolerance takes whole numbers 0..255 and Opacity whole percents
|
||||
//! 1..100; a value typed past either range becomes the closest one with the reference's "Closest
|
||||
//! value inserted" alert, and a typed fraction rounds without one. The seven controls carry the
|
||||
//! value inserted" alert. A typed fraction rounds to the closest whole number: in Tolerance with
|
||||
//! the same alert, in Opacity without one. The seven controls carry the
|
||||
//! reference's tooltips, and the Source dropdown has no "Fill" label. The Magic Wand and the Magic
|
||||
//! Eraser share Tolerance and keep their own behaviour.
|
||||
|
||||
@@ -104,6 +105,26 @@ fn tolerance_is_a_whole_number() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fractional_tolerance_takes_the_closest_value_and_alerts() {
|
||||
for (typed, want) in [("12.7", 13.0), ("12.4", 12.0), ("0.4", 0.0), ("254.6", 255.0)] {
|
||||
let mut h = bar(Tool::PaintBucket);
|
||||
type_into(&mut h, "Tolerance", typed);
|
||||
assert_eq!(h.state().ui.tool_options.tolerance, want, "{typed}");
|
||||
assert_eq!(alerts(&h), vec![TOLERANCE_ALERT.to_string()], "{typed}: one alert");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fractional_opacity_rounds_without_an_alert() {
|
||||
for (typed, want) in [("50.5", 51.0), ("50.4", 50.0), ("1.4", 1.0), ("99.6", 100.0)] {
|
||||
let mut h = bar(Tool::PaintBucket);
|
||||
type_into(&mut h, "Opacity", typed);
|
||||
assert_eq!(h.state().ui.tool_options.fill_opacity, want, "{typed}");
|
||||
assert!(alerts(&h).is_empty(), "{typed}: no alert, got {:?}", alerts(&h));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opacity_past_1_100_takes_the_closest_value_and_alerts() {
|
||||
for (typed, want) in [("150", 100.0), ("0", 1.0), ("-3", 1.0)] {
|
||||
|
||||
@@ -66,9 +66,14 @@ pub(crate) fn mode_select<'a>(mode: Bind<'a, PaintMode>, entries: &'a [Entry<Pai
|
||||
/// whole-number alert would write "1 %" with a space; the reference writes "1%".
|
||||
pub(crate) const PAINT_BUCKET_OPACITY_ALERT: &str = "An integer between 1% and 100% is required. Closest value inserted.";
|
||||
|
||||
/// The reference's alert for a Paint Bucket Tolerance typed past 0..255 or as a fraction (#12533),
|
||||
/// the framework's wording of a whole-number range without a unit.
|
||||
pub(crate) const PAINT_BUCKET_TOLERANCE_ALERT: &str = "An integer between 0 and 255 is required. Closest value inserted.";
|
||||
|
||||
/// The Paint Bucket's members: Fill source, Mode, Opacity, Tolerance, Anti-alias, Contiguous and All Layers. Opacity
|
||||
/// takes whole percents 1..100 and Tolerance whole numbers 0..255; a value typed past either range becomes the
|
||||
/// closest one with the reference's alert (#12533).
|
||||
/// closest one with the reference's alert, as does a fraction typed in Tolerance; a fraction typed in Opacity rounds
|
||||
/// without one (#12533).
|
||||
pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
if app.ui.tool != Tool::PaintBucket {
|
||||
return;
|
||||
@@ -89,15 +94,17 @@ pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
PaintMode::Behind => {}
|
||||
}
|
||||
let opacity = number(tl!("Opacity"), &mut o.fill_opacity, 1.0..=100.0).whole().unit("%").tooltip(tl!("Set opacity for fill")).show(ui).out_of_range;
|
||||
let tolerance = number(tl!("Tolerance"), &mut o.tolerance, 0.0..=255.0).whole().tooltip(tl!("Range of colors to fill")).show(ui).out_of_range;
|
||||
let tolerance = number(tl!("Tolerance"), &mut o.tolerance, 0.0..=255.0).whole_alerting_fractions().tooltip(tl!("Range of colors to fill")).show(ui);
|
||||
Checkbox::new(Bind::bound(&mut o.anti_alias)).label(tl!("Anti-alias").to_owned()).tooltip(tl!("Smoothes edge transition").to_owned()).show(ui);
|
||||
Checkbox::new(Bind::bound(&mut o.contiguous)).label(tl!("Contiguous").to_owned()).tooltip(tl!("Fill only contiguous pixels").to_owned()).show(ui);
|
||||
Checkbox::new(Bind::bound(&mut o.sample_all.paint_bucket)).label(tl!("All Layers").to_owned()).tooltip(tl!("Fill composite image").to_owned()).show(ui);
|
||||
if opacity.is_some() {
|
||||
let _ = crate::dialogs::alerts::alert(app, tl!(PAINT_BUCKET_OPACITY_ALERT));
|
||||
} else if let Some(alert) = tolerance {
|
||||
} else if let Some(alert) = tolerance.out_of_range {
|
||||
// The framework words a whole-number field without a unit as the reference does:
|
||||
// "An integer between 0 and 255 is required. Closest value inserted."
|
||||
crate::tools::option_cells::range_alert(app, ui, &alert);
|
||||
} else if tolerance.rounded {
|
||||
let _ = crate::dialogs::alerts::alert(app, tl!(PAINT_BUCKET_TOLERANCE_ALERT));
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ pub(crate) struct NumberCell<'a> {
|
||||
range: RangeInclusive<f64>,
|
||||
unit: Option<Label>,
|
||||
whole: bool,
|
||||
alert_fractions: bool,
|
||||
popup_slider: bool,
|
||||
tooltip: Option<Label>,
|
||||
enabled: bool,
|
||||
@@ -30,7 +31,18 @@ pub(crate) struct NumberCell<'a> {
|
||||
|
||||
/// A number cell named `caption` over `value`, held to `range`.
|
||||
pub(crate) fn number<'a>(caption: impl Into<Label>, value: &'a mut f32, range: RangeInclusive<f64>) -> NumberCell<'a> {
|
||||
NumberCell { caption: caption.into(), value, range, unit: None, whole: false, popup_slider: false, tooltip: None, enabled: true, icon: None }
|
||||
NumberCell {
|
||||
caption: caption.into(),
|
||||
value,
|
||||
range,
|
||||
unit: None,
|
||||
whole: false,
|
||||
alert_fractions: false,
|
||||
popup_slider: false,
|
||||
tooltip: None,
|
||||
enabled: true,
|
||||
icon: None,
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> NumberCell<'a> {
|
||||
@@ -48,6 +60,15 @@ impl<'a> NumberCell<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// The cell holds whole numbers, and a typed fraction is rounded to the closest one after it
|
||||
/// commits and reported in [`NumberCellOutput::rounded`] for the caller's alert.
|
||||
#[must_use]
|
||||
pub(crate) fn whole_alerting_fractions(mut self) -> Self {
|
||||
self.whole = true;
|
||||
self.alert_fractions = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// A chevron after the field opens a slider over the same range.
|
||||
#[must_use]
|
||||
pub(crate) fn popup_slider(mut self) -> Self {
|
||||
@@ -79,17 +100,22 @@ impl<'a> NumberCell<'a> {
|
||||
/// Shows the cell. Returns the pass's edit events, which a caller keys its history step by
|
||||
/// (`history_keys::coalesce`), and the alert of a typed value that was clamped to the range.
|
||||
pub(crate) fn show(self, ui: &mut egui::Ui) -> NumberCellOutput {
|
||||
let Self { caption, value, range, unit, whole, popup_slider, tooltip, enabled, icon } = self;
|
||||
let Self { caption, value, range, unit, whole, alert_fractions, popup_slider, tooltip, enabled, icon } = self;
|
||||
let mut side = SideLabel::new().scrub(true);
|
||||
if let Some(icon) = icon {
|
||||
side = side.icon(icon).layout(FieldLayout::Inside);
|
||||
}
|
||||
let mut out = NumberCellOutput { events: EditEvents::default(), out_of_range: None };
|
||||
let mut out = NumberCellOutput { events: EditEvents::default(), out_of_range: None, rounded: false };
|
||||
Field::new(caption.clone()).enabled(enabled).side(side).sizing(Sizing::Content).show(ui, |ui, _| {
|
||||
let mut field = NumberField::named(caption.clone(), Bind::bound(&mut *value)).range(range.clone()).scrub_by_label();
|
||||
if whole {
|
||||
let codec = NumberCodec::new(Env::of(ui).units().reader(None), 0);
|
||||
field = field.step(1.0).commit_decimals(0).codec(codec);
|
||||
field = field.step(1.0).codec(codec);
|
||||
// A cell that alerts on fractions commits the fraction as typed, so it can tell one
|
||||
// was typed; it rounds the value below.
|
||||
if !alert_fractions {
|
||||
field = field.commit_decimals(0);
|
||||
}
|
||||
}
|
||||
if let Some(unit) = unit.clone() {
|
||||
field = field.unit(unit);
|
||||
@@ -101,8 +127,12 @@ impl<'a> NumberCell<'a> {
|
||||
field = field.tooltip(tooltip);
|
||||
}
|
||||
let shown = field.show(ui);
|
||||
out = NumberCellOutput { events: shown.edit.events, out_of_range: shown.out_of_range };
|
||||
out = NumberCellOutput { events: shown.edit.events, out_of_range: shown.out_of_range, rounded: false };
|
||||
});
|
||||
if alert_fractions && out.events.committed().is_some_and(|v| v.is_finite() && v.fract() != 0.0) {
|
||||
*value = value.round().clamp(*range.start() as f32, *range.end() as f32);
|
||||
out.rounded = true;
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
@@ -113,6 +143,9 @@ pub(crate) struct NumberCellOutput {
|
||||
pub events: EditEvents<f32>,
|
||||
/// A typed value that was clamped to the range, in the pass it committed.
|
||||
pub out_of_range: Option<RangeAlert>,
|
||||
/// A typed fraction was rounded to a whole number, in the pass it committed (only for a cell
|
||||
/// built with [`NumberCell::whole_alerting_fractions`]).
|
||||
pub rounded: bool,
|
||||
}
|
||||
|
||||
/// Opens the out-of-range alert for `alert`; `ui` supplies the env that words its range. One alert
|
||||
|
||||
Reference in new issue
Block a user