mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Paint Bucket: whole-number Opacity and Tolerance with alerts, tooltips, no Fill label
The Paint Bucket's Opacity and Tolerance now take whole numbers (1..100 % and 0..255); a value typed past either range becomes the closest one with the reference application's alert, and a typed fraction rounds without one. The seven controls carry the reference tooltips and the Source dropdown no longer has a "Fill" label. The Magic Wand and Magic Eraser keep their behaviour. Fixes #12533.
This commit is contained in:
1 parent
c05c4c5fdf
commit
750a67a96d
3 files changed
+189
-13
No files matched your search
@@ -733,6 +733,8 @@ mod eyedropper_context_menu_tests;
|
||||
#[cfg(test)]
|
||||
mod menu_automate_groups_tests;
|
||||
#[cfg(test)]
|
||||
mod paint_bucket_options_fields_tests;
|
||||
#[cfg(test)]
|
||||
mod patch_shift_constrain_tests;
|
||||
#[cfg(test)]
|
||||
mod paths_scroll_tests;
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
//! #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
|
||||
//! 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.
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
use craft::ui::runtime::env::Env;
|
||||
use egui::accesskit::Role;
|
||||
use egui::vec2;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::Tool;
|
||||
|
||||
type H = Harness<'static, Box<PhotocraftApp>>;
|
||||
|
||||
const TOLERANCE_ALERT: &str = "An integer between 0 and 255 is required. Closest value inserted.";
|
||||
const OPACITY_ALERT: &str = "An integer between 1% and 100% is required. Closest value inserted.";
|
||||
|
||||
thread_local! {
|
||||
/// The decimal mark the bar's fields read, from the env they are drawn in: the system region's
|
||||
/// unless the host sets one, so a typed fraction must use it to be read as one.
|
||||
static MARK: Cell<char> = const { Cell::new('.') };
|
||||
}
|
||||
|
||||
/// The options bar with `tool` active, and the dialog host its alerts open on.
|
||||
fn bar(tool: Tool) -> H {
|
||||
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;
|
||||
app.ui.tool_options.tolerance = 32.0;
|
||||
app.ui.tool_options.fill_opacity = 37.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>| {
|
||||
MARK.set(Env::of(ui).format().separators().0);
|
||||
crate::panels::options_bar(app, ui);
|
||||
crate::dialogs::run(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
/// `text` with `.` replaced by the fields' decimal mark.
|
||||
fn localized(text: &str) -> String {
|
||||
text.replace('.', &MARK.get().to_string())
|
||||
}
|
||||
|
||||
/// The number field named `label`, with or without its caption's colon.
|
||||
fn field<'a>(h: &'a H, label: &str) -> Option<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(':') == label))
|
||||
}
|
||||
|
||||
/// Types `text` (with `.` as the decimal mark) into the field named `label` and commits it with Enter.
|
||||
fn type_into(h: &mut H, label: &str, text: &str) {
|
||||
let text = localized(text);
|
||||
field(h, label).unwrap_or_else(|| panic!("the {label} field")).click();
|
||||
h.run_steps(1);
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
|
||||
h.event(egui::Event::Text(text));
|
||||
h.run_steps(1);
|
||||
h.key_press(egui::Key::Enter);
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
/// The messages of the open framework alerts.
|
||||
fn alerts(h: &H) -> Vec<String> {
|
||||
h.state().framework.dialogs.open_dialogs().iter().filter_map(|d| d.fields.as_ref()?.get("message")?.as_str().map(str::to_string)).collect()
|
||||
}
|
||||
|
||||
/// The description (tooltip) of the control labelled `label`: not its caption's text node.
|
||||
fn tip(h: &H, label: &str) -> Option<String> {
|
||||
let named = move |n: &egui_kittest::kittest::AccessKitNode<'_>| n.role() != Role::Label && n.label().is_some_and(|l| l.trim_end_matches(':') == label);
|
||||
let node = h.query_all(egui_kittest::kittest::by().predicate(named)).next()?;
|
||||
node.accesskit_node().description().map(|d| d.to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tolerance_past_0_255_takes_the_closest_value_and_alerts() {
|
||||
for (typed, want) in [("300", 255.0), ("256", 255.0), ("-5", 0.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 tolerance_is_a_whole_number() {
|
||||
for (typed, want) in [("12.7", 13.0), ("12.4", 12.0), ("255", 255.0), ("0", 0.0), ("abc", 32.0)] {
|
||||
let mut h = bar(Tool::PaintBucket);
|
||||
type_into(&mut h, "Tolerance", typed);
|
||||
assert_eq!(h.state().ui.tool_options.tolerance, want, "{typed}");
|
||||
if !typed.contains('.') {
|
||||
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)] {
|
||||
let mut h = bar(Tool::PaintBucket);
|
||||
type_into(&mut h, "Opacity", typed);
|
||||
assert_eq!(h.state().ui.tool_options.fill_opacity, want, "{typed}");
|
||||
assert_eq!(alerts(&h), vec![OPACITY_ALERT.to_string()], "{typed}: one alert");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opacity_in_range_rounds_to_a_whole_percent_without_an_alert() {
|
||||
for (typed, want) in [("50.5", 51.0), ("50.4", 50.0), ("100", 100.0), ("1", 1.0), ("abc", 37.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 the_seven_controls_have_the_reference_tooltips() {
|
||||
let h = bar(Tool::PaintBucket);
|
||||
for (control, want) in [
|
||||
("Source", "Set source for fill area"),
|
||||
("Mode", "Fill Mode"),
|
||||
("Opacity", "Set opacity for fill"),
|
||||
("Tolerance", "Range of colors to fill"),
|
||||
("Anti-alias", "Smoothes edge transition"),
|
||||
("Contiguous", "Fill only contiguous pixels"),
|
||||
("All Layers", "Fill composite image"),
|
||||
] {
|
||||
assert_eq!(tip(&h, control).as_deref(), Some(want), "{control}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_source_dropdown_has_no_fill_label() {
|
||||
let h = bar(Tool::PaintBucket);
|
||||
assert!(h.query_by_label("Fill").is_none() && h.query_by_label("Fill:").is_none(), "a Fill label is shown");
|
||||
assert!(h.query_all_by_role(Role::ComboBox).any(|n| n.accesskit_node().label().as_deref() == Some("Source")), "the Source dropdown is shown");
|
||||
}
|
||||
|
||||
/// The Magic Wand and the Magic Eraser share Tolerance and keep today's behaviour: a value past
|
||||
/// 0..255 is clamped without an alert.
|
||||
#[test]
|
||||
fn magic_wand_and_magic_eraser_tolerance_are_unchanged() {
|
||||
for tool in [Tool::MagicWand, Tool::MagicEraser] {
|
||||
for (typed, want) in [("300", 255.0), ("-5", 0.0), ("128", 128.0)] {
|
||||
let mut h = bar(tool);
|
||||
type_into(&mut h, "Tolerance", typed);
|
||||
assert_eq!(h.state().ui.tool_options.tolerance, want, "{tool:?} {typed}");
|
||||
assert!(alerts(&h).is_empty(), "{tool:?} {typed}: no alert, got {:?}", alerts(&h));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
//! The Paint Bucket's members of the options bar, and the Mode list of the Paint Bucket and the stamp tools.
|
||||
|
||||
use craft::ui::contract::{Bind, Entry, Show as _};
|
||||
use craft::ui::choice::Checkbox;
|
||||
use craft::ui::contract::{Bind, Entry, Props as _, Show as _};
|
||||
use craft::ui::patterns::blend_keys::{BlendKeyEntry, blend_entries};
|
||||
use craft::ui::pickers::Select;
|
||||
use photocraft_doc::BlendMode;
|
||||
|
||||
use super::labelled;
|
||||
use super::paint::PaintMode;
|
||||
use super::{check, labelled, number_field};
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::Tool;
|
||||
use crate::tools::option_cells::number;
|
||||
|
||||
/// The text of a Mode entry.
|
||||
fn label_of(mode: PaintMode) -> String {
|
||||
@@ -60,7 +62,13 @@ pub(crate) fn mode_select<'a>(mode: Bind<'a, PaintMode>, entries: &'a [Entry<Pai
|
||||
Select::new(tl!("Mode").to_owned(), mode, entries)
|
||||
}
|
||||
|
||||
/// The Paint Bucket's members: Fill source, Mode, Opacity, Tolerance, Anti-alias, Contiguous and All Layers.
|
||||
/// The reference's alert for a Paint Bucket Opacity typed past 1..100 % (#12533). The framework's
|
||||
/// 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 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).
|
||||
pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
if app.ui.tool != Tool::PaintBucket {
|
||||
return;
|
||||
@@ -68,9 +76,10 @@ pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
let entries = mode_entries(Tool::PaintBucket, true, false);
|
||||
let sources = [Entry::new(false, tl!("Foreground").to_owned()), Entry::new(true, tl!("Pattern").to_owned())];
|
||||
let o = &mut app.ui.tool_options;
|
||||
labelled(ui, tl!("Fill"), |ui| Select::new(tl!("Source").to_owned(), Bind::bound(&mut o.bucket_fill_pattern), &sources).show(ui));
|
||||
// The Source dropdown stands alone, without a caption, as in the reference.
|
||||
Select::new(tl!("Source").to_owned(), Bind::bound(&mut o.bucket_fill_pattern), &sources).tooltip(tl!("Set source for fill area").to_owned()).show(ui);
|
||||
let mut mode = if o.bucket_clear { PaintMode::Clear } else { PaintMode::Blend(o.bucket_blend_mode) };
|
||||
labelled(ui, tl!("Mode"), |ui| mode_select(Bind::bound(&mut mode), &entries).show(ui));
|
||||
labelled(ui, tl!("Mode"), |ui| mode_select(Bind::bound(&mut mode), &entries).tooltip(tl!("Fill Mode").to_owned()).show(ui));
|
||||
match mode {
|
||||
PaintMode::Clear => o.bucket_clear = true,
|
||||
PaintMode::Blend(blend) => {
|
||||
@@ -79,13 +88,16 @@ pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
}
|
||||
PaintMode::Behind => {}
|
||||
}
|
||||
if let Some(opacity) = number_field(ui, tl!("Opacity"), o.fill_opacity, 1.0..=100.0, "%").landed() {
|
||||
o.fill_opacity = *opacity;
|
||||
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;
|
||||
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 {
|
||||
// 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);
|
||||
}
|
||||
if let Some(tolerance) = number_field(ui, tl!("Tolerance"), o.tolerance, 0.0..=255.0, "").landed() {
|
||||
o.tolerance = *tolerance;
|
||||
}
|
||||
check(ui, tl!("Anti-alias"), &mut o.anti_alias);
|
||||
check(ui, tl!("Contiguous"), &mut o.contiguous);
|
||||
check(ui, tl!("All Layers"), &mut o.sample_all.paint_bucket);
|
||||
}
|
||||
Reference in new issue
Block a user