mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Round the Magic Wand's Tolerance field to whole numbers
The wand's Tolerance took a plain value_field over an f32 option, so typing 12.5 stored 12.5 while the wand floored it to 12: the field showed a number the tool did not use. A decimal written with the interface language's comma (12,5) was rejected outright, and the old value stayed. Adds FieldOpts::integer and widgets::value_field_int: the field rounds the number its parser reads, so the value it holds while the number is typed is the one it keeps and the DragValue keeps the typed text, and it rounds whatever value another writer leaves behind (a drag, a preference file). read() takes the decimal comma as a decimal point, as the zoom fields do. The wand's Tolerance uses it. The Paint Bucket's field on the same option is untouched, and the drag and arrow-key step stays 1. Fixes #11786 Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
1 parent
f68e227026
commit
d0b24e1abd
6 files changed
+204
-4
No files matched your search
@@ -969,6 +969,8 @@ mod export_quality_tests;
|
||||
mod options_hit_area_tests;
|
||||
#[cfg(test)]
|
||||
mod select_and_mask_button_tests;
|
||||
#[cfg(test)]
|
||||
mod wand_tolerance_field_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod saved_path_edit_tests;
|
||||
|
||||
@@ -382,7 +382,9 @@ fn selection_specific_options(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool:
|
||||
selection_mode_buttons(app, ui);
|
||||
widgets::vline(ui, 22.0);
|
||||
opt_label(ui, tl!("Tolerance"));
|
||||
widgets::value_field(ui, &mut app.ui.tool_options.tolerance, 0.0..=255.0, "", 58.0);
|
||||
// The wand reads a whole-number tolerance (#11786): the field rounds a typed decimal
|
||||
// and never keeps a fraction, so it shows the number the wand uses.
|
||||
widgets::value_field_int(ui, &mut app.ui.tool_options.tolerance, 0.0..=255.0, "", 58.0);
|
||||
widgets::checkbox(ui, &mut app.ui.tool_options.anti_alias, tl!("Anti-alias"));
|
||||
widgets::checkbox(ui, &mut app.ui.tool_options.contiguous, tl!("Contiguous"));
|
||||
widgets::checkbox(ui, &mut app.ui.tool_options.sample_all.magic_wand, tl!("Sample All Layers"));
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
//! #11786: the Magic Wand's Tolerance is a whole number 0–255, and the wand floors what it is
|
||||
//! given (`wand_region`: `(tolerance + 1e-3).floor()`). A field that keeps a fraction therefore
|
||||
//! shows a number the tool does not use, and a decimal typed with the decimal comma (the UI
|
||||
//! language's separator) is rejected outright. Typing `12.5` or `12,5` must leave 13 behind, so the
|
||||
//! field and the wand agree; 300 and -5 clamp, as today.
|
||||
|
||||
use egui::accesskit::Role;
|
||||
use egui::{PointerButton, Pos2, vec2};
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use photocraft_color::{ColorMode, SampleType};
|
||||
use photocraft_doc::{Document, Layer, LayerContent};
|
||||
use photocraft_geom::{Rect, Size};
|
||||
use photocraft_raster::Surface;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::Tool;
|
||||
|
||||
/// The Magic Wand's options bar, with a document open, after a few settled frames.
|
||||
fn wand_bar() -> Harness<'static, PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
|
||||
app.sync_views();
|
||||
app.ui.tool = Tool::MagicWand;
|
||||
let mut h = Harness::builder().with_size(vec2(1000.0, 60.0)).build_ui_state(
|
||||
|ui, app: &mut PhotocraftApp| {
|
||||
if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
|
||||
return;
|
||||
}
|
||||
crate::panels::options_bar(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
/// The number the Tolerance field shows, as a person reads it off the screen.
|
||||
fn shown_tolerance(h: &Harness<'_, PhotocraftApp>) -> f32 {
|
||||
let node = h.get_by_role(Role::SpinButton);
|
||||
node.accesskit_node().value().unwrap_or_default().parse().unwrap_or(f32::NAN)
|
||||
}
|
||||
|
||||
/// Types `text` over the field's number and presses Enter, as a person would, and returns the
|
||||
/// option the edit left behind.
|
||||
fn type_tolerance(h: &mut Harness<'_, PhotocraftApp>, text: &str) -> f32 {
|
||||
h.get_by_role(Role::SpinButton).click();
|
||||
h.run();
|
||||
h.event(egui::Event::Text(text.into()));
|
||||
h.run_steps(2);
|
||||
h.key_press(egui::Key::Enter);
|
||||
h.run_steps(2);
|
||||
h.state().ui.tool_options.tolerance
|
||||
}
|
||||
|
||||
/// A typed decimal rounds to the nearest whole number, whichever separator it was typed with: the
|
||||
/// wand's option is never a fraction for it to floor differently from what the field shows.
|
||||
#[test]
|
||||
fn typed_decimals_round_to_the_nearest_whole_number() {
|
||||
for (text, want) in [("12.5", 13.0), ("12,5", 13.0), ("12.4", 12.0), ("12.6", 13.0)] {
|
||||
let mut h = wand_bar();
|
||||
assert_eq!(type_tolerance(&mut h, text), want, "typing {text}");
|
||||
assert_eq!(shown_tolerance(&h), want, "the field shows the number it kept ({text})");
|
||||
}
|
||||
}
|
||||
|
||||
/// A typed number outside the range clamps, as in any value field.
|
||||
#[test]
|
||||
fn typed_values_outside_the_range_clamp() {
|
||||
for (text, want) in [("300", 255.0), ("-5", 0.0)] {
|
||||
let mut h = wand_bar();
|
||||
assert_eq!(type_tolerance(&mut h, text), want, "typing {text}");
|
||||
assert_eq!(shown_tolerance(&h), want, "the field shows the clamped number ({text})");
|
||||
}
|
||||
}
|
||||
|
||||
/// The Paint Bucket's plain field shares this option and keeps today's behaviour, and a restored
|
||||
/// preference file can hold a fraction: the wand's field takes whatever it finds as the whole
|
||||
/// number it shows, so the wand can never floor a value the field is displaying.
|
||||
#[test]
|
||||
fn a_fractional_option_becomes_the_whole_number_the_field_shows() {
|
||||
let mut h = wand_bar();
|
||||
h.state_mut().ui.tool_options.tolerance = 12.5;
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().ui.tool_options.tolerance, 13.0);
|
||||
assert_eq!(shown_tolerance(&h), 13.0);
|
||||
}
|
||||
|
||||
/// Six white pixels whose second one is 13 levels darker: inside the wand's reach at Tolerance 13,
|
||||
/// outside it at 12, so one click tells the two numbers apart.
|
||||
fn wand_canvas() -> Harness<'static, PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
let mut doc = Document::new("Wand", Size::new(6, 1), ColorMode::Rgb, SampleType::U8);
|
||||
let mut pixels = Surface::with_default(doc.pixel_format(), &[1.0, 1.0, 1.0, 1.0]);
|
||||
pixels.write_pixel(1, 0, &[242.0 / 255.0, 242.0 / 255.0, 242.0 / 255.0, 1.0]);
|
||||
doc.layers.push(Layer::new("Layer 1", LayerContent::Raster(pixels)));
|
||||
let layer = doc.layers[0].id;
|
||||
app.session.add_document(doc, None);
|
||||
app.session.select_layer(layer).unwrap();
|
||||
app.ui.tool = Tool::MagicWand;
|
||||
app.ui.extras.rulers = false;
|
||||
app.sync_views();
|
||||
let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state(
|
||||
|ui, app: &mut PhotocraftApp| {
|
||||
let ctx = ui.ctx().clone();
|
||||
if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
|
||||
return;
|
||||
}
|
||||
// The options bar is a panel of its own; the canvas takes the height that is left.
|
||||
crate::panels::options_bar(app, ui);
|
||||
egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
|
||||
},
|
||||
app,
|
||||
);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
h.run_steps(4);
|
||||
// 40× zoom on a one-pixel-tall document: the pixels are 40 pt apart and easy to hit.
|
||||
let v = &mut h.state_mut().ui.views[0];
|
||||
v.zoom = 40.0;
|
||||
v.center = [3.0, 0.5];
|
||||
v.fit_pending = false;
|
||||
h.run_steps(2);
|
||||
h
|
||||
}
|
||||
|
||||
/// Where document point `(x, y)` lands on screen, as the canvas' own transform maps it.
|
||||
fn screen(h: &Harness<'static, PhotocraftApp>, x: f32, y: f32) -> Pos2 {
|
||||
let app = h.state();
|
||||
let v = &app.ui.views[0];
|
||||
crate::canvas::ViewXform {
|
||||
rect: crate::rulers::content_rect(app, app.last_canvas_rect),
|
||||
zoom: v.zoom,
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
}
|
||||
.to_screen(x, y)
|
||||
}
|
||||
|
||||
/// Starts from `option` in the shared tool state, clicks the first pixel with the Magic Wand, and
|
||||
/// returns the area selected.
|
||||
fn wand_click(option: f32) -> Rect {
|
||||
let mut h = wand_canvas();
|
||||
h.state_mut().ui.tool_options.tolerance = option;
|
||||
h.run_steps(2);
|
||||
let p = screen(&h, 0.5, 0.5);
|
||||
h.event(egui::Event::PointerMoved(p));
|
||||
h.run();
|
||||
for pressed in [true, false] {
|
||||
h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE });
|
||||
h.run_steps(2);
|
||||
}
|
||||
h.state().session.active().unwrap().doc.selection.as_ref().map_or(Rect::EMPTY, |s| s.content_bounds())
|
||||
}
|
||||
|
||||
/// The wand selects by the whole number the field holds, so a typed `12.5` selects what 13 selects,
|
||||
/// not what the 12 it would have floored to selects.
|
||||
#[test]
|
||||
fn the_wand_selects_by_the_number_the_field_shows() {
|
||||
let (at_12, at_13) = (wand_click(12.0), wand_click(13.0));
|
||||
assert_ne!(at_12, at_13, "Tolerance 12 and 13 must select different areas for this test to mean anything");
|
||||
assert_eq!(wand_click(12.5), at_13, "a typed 12.5 gives the 13 the field shows");
|
||||
}
|
||||
@@ -68,8 +68,8 @@ pub use text_field::{
|
||||
INPUT_EVENT_LIMIT, TEXT_FIELD_H, TEXT_FIELD_LIMIT, cap_text_events, focus_first_field, text_field, text_field_ex, text_field_with_trailing,
|
||||
};
|
||||
pub use units::{
|
||||
FieldUnits, fmt_num, fmt_num_prec, fmt_num2, parse_num, set_field_resolution, unit_decimals, unit_of_key, value_field_extent, value_field_len,
|
||||
value_field_prec, value_field_units,
|
||||
FieldUnits, fmt_num, fmt_num_prec, fmt_num2, parse_num, set_field_resolution, unit_decimals, unit_of_key, value_field_extent, value_field_int,
|
||||
value_field_len, value_field_prec, value_field_units,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -71,6 +71,11 @@ pub(super) struct FieldOpts {
|
||||
/// keystroke (the zoom fields: typing `150` over `50` must not zoom to 1 % on the way). Esc,
|
||||
/// Tab and Alt do not apply it (`zoom_field`).
|
||||
pub(super) hold_typed: bool,
|
||||
/// Whole numbers only ([`super::value_field_int`], #11786): a typed decimal rounds to the
|
||||
/// nearest one, `12.5` and `12,5` both giving `13`, and the value the field holds is never a
|
||||
/// fraction, whichever path wrote it (a drag, another field on the same option, a preference
|
||||
/// file). The number the field shows is therefore the number its caller reads.
|
||||
pub(super) integer: bool,
|
||||
}
|
||||
|
||||
/// Arrow-key and drag step for a value field: [`FieldOpts::step_from_decimals`] uses `decimals`;
|
||||
@@ -400,7 +405,7 @@ fn number_edit(
|
||||
units: crate::numeric_expression::Units,
|
||||
opts: FieldOpts,
|
||||
) -> Response {
|
||||
let FieldOpts { decimals, wrap_drag, hold_typed, .. } = opts;
|
||||
let FieldOpts { decimals, wrap_drag, hold_typed, integer, .. } = opts;
|
||||
let (id, ctx) = (ui.next_auto_id(), ui.ctx().clone());
|
||||
// Focused before the DragValue is drawn, so it gains focus this frame and selects its text.
|
||||
if ui.data_mut(|d| d.remove_temp::<bool>(first_field_id())).unwrap_or(false) {
|
||||
@@ -487,11 +492,22 @@ fn number_edit(
|
||||
return lead.and_then(|lead| zoom_number(&lead, units));
|
||||
}
|
||||
}
|
||||
} else if integer {
|
||||
// A whole-number field takes the decimal comma too (`12,5` is `12.5`); two
|
||||
// separators (`1,5,2`) are not a number, as in any other field.
|
||||
match crate::zoom_field::read(s) {
|
||||
crate::zoom_field::Entry::Number(ready) => ready,
|
||||
crate::zoom_field::Entry::Invalid(_) => return None,
|
||||
}
|
||||
} else {
|
||||
s.to_owned()
|
||||
};
|
||||
let s = ready.as_str();
|
||||
let v = if hold_typed { zoom_number(s, units) } else { parse_num_in(s, units) };
|
||||
// A whole-number field already rounds what it reads here, in the parser: the value
|
||||
// it holds while the number is being typed is then the one it keeps, where rounding
|
||||
// it after the DragValue wrote it down would rewrite the text under the typist.
|
||||
let v = if integer { v.map(|v| v.round()) } else { v };
|
||||
if ctx.memory(|m| m.has_focus(id)) && plain(s).is_none() {
|
||||
// Still typing arithmetic: hold it, and stop applying keystrokes once it parses.
|
||||
ctx.data_mut(|d| d.insert_temp(held, v.is_some()));
|
||||
@@ -520,6 +536,12 @@ fn number_edit(
|
||||
if *value != before && lo <= hi {
|
||||
*value = value.clamp(lo, hi);
|
||||
}
|
||||
// A whole-number field keeps whole numbers whatever wrote one: a drag between two values, a
|
||||
// plain field on the same option, a preference file. The number it shows is then the number
|
||||
// its caller reads, and rounding a whole number changes nothing.
|
||||
if integer && lo <= hi && value.is_finite() && *value != value.round() {
|
||||
*value = value.round().clamp(lo, hi);
|
||||
}
|
||||
resp.flags.set(egui::response::Flags::CHANGED, *value != before);
|
||||
resp
|
||||
}
|
||||
|
||||
@@ -28,6 +28,14 @@ pub fn value_field_prec(ui: &mut Ui, value: &mut f32, range: std::ops::RangeIncl
|
||||
value_field_in(ui, value, range, suffix, width, 0.0, FieldOpts { decimals: Some(decimals), ..Default::default() }, None).0
|
||||
}
|
||||
|
||||
/// A [`value_field`] of whole numbers with no decimal places: the Magic Wand's Tolerance, an
|
||||
/// integer 0–255 that its tool floors (#11786). A typed decimal rounds to the nearest whole number,
|
||||
/// `12.5` and `12,5` both giving `13`, and the value the field holds is never a fraction, so the
|
||||
/// number it shows is the number the tool uses. Out-of-range entries clamp, as in any value field.
|
||||
pub fn value_field_int(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive<f32>, suffix: &str, width: f32) -> Response {
|
||||
value_field_in(ui, value, range, suffix, width, 0.0, FieldOpts { decimals: Some(0), integer: true, ..Default::default() }, None).0
|
||||
}
|
||||
|
||||
/// A field's unit and the resolution a typed unit converts at (`1in` in a px field).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct FieldUnits {
|
||||
|
||||
Reference in new issue
Block a user