mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Background Eraser: Angle and Pressure for Size on the options bar (#11908)
The Background Eraser bar gets the Angle field (whole degrees, -180 to 180, with the reference application's alert for a typed value outside that) between Tolerance and Protect Foreground Color, and the Pressure for Size toggle after it. Both edit the tool's own brush through tools.setBrush. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
e6ba4aca42
commit
66fe90cbcb
3 files changed
+177
-1
No files matched your search
@@ -0,0 +1,129 @@
|
||||
//! #11908: the Background Eraser's options bar has an Angle field between Tolerance and Protect
|
||||
//! Foreground Color (0° by default, "Set the brush angle", whole degrees from −180 to 180, with the
|
||||
//! reference application's alert for a typed value outside that) and a Pressure for Size toggle
|
||||
//! after Protect Foreground Color. Both edit the tool's own brush.
|
||||
|
||||
use egui::vec2;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::{DialogKind, Tool};
|
||||
use crate::theme::ThemeKind;
|
||||
|
||||
const PRESSURE: &str = "Always use pressure for size";
|
||||
|
||||
fn bar(tool: Tool) -> Harness<'static, 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;
|
||||
let mut h = Harness::builder().with_size(vec2(1600.0, 300.0)).build_ui_state(|ui, app| crate::panels::options_bar(app, ui), app);
|
||||
PhotocraftApp::setup_context(&h.ctx, ThemeKind::ProMedium);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
fn angle_field<'a>(h: &'a Harness<'static, PhotocraftApp>) -> Option<egui_kittest::Node<'a>> {
|
||||
h.query_all_by_role(egui::accesskit::Role::SpinButton).find(|n| n.accesskit_node().description().as_deref() == Some("Set the brush angle"))
|
||||
}
|
||||
|
||||
fn shown(h: &Harness<'static, PhotocraftApp>) -> String {
|
||||
angle_field(h).expect("the Angle field").accesskit_node().value().map(|v| v.to_string()).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn left(h: &Harness<'static, PhotocraftApp>, label: &str) -> f64 {
|
||||
h.get_by_label(label).accesskit_node().bounding_box().expect(label).x0
|
||||
}
|
||||
|
||||
fn alerts(h: &Harness<'static, PhotocraftApp>) -> Vec<String> {
|
||||
let dialogs = &h.state().ui.dialogs;
|
||||
dialogs.iter().filter(|d| d.kind == DialogKind::Error).filter_map(|d| d.fields.get("message")?.as_str().map(str::to_owned)).collect()
|
||||
}
|
||||
|
||||
fn type_angle(h: &mut Harness<'static, PhotocraftApp>, text: &str) -> f32 {
|
||||
angle_field(h).expect("the Angle 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(2);
|
||||
h.state().session.tools.brush.angle
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn angle_sits_between_tolerance_and_protect_foreground_color() {
|
||||
let h = bar(Tool::BackgroundEraser);
|
||||
let field = angle_field(&h).expect("the Angle field");
|
||||
let angle = field.accesskit_node().bounding_box().unwrap().x0;
|
||||
assert!(shown(&h).starts_with('0'), "default 0°: {}", shown(&h));
|
||||
assert!(left(&h, "Tolerance:") < angle && angle < left(&h, "Protect Foreground Color"), "Tolerance < Angle < Protect Foreground Color");
|
||||
assert_eq!(h.state().session.tools.brush.angle, 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_angles_are_whole_degrees_clamped_with_the_alert() {
|
||||
let mut h = bar(Tool::BackgroundEraser);
|
||||
assert_eq!(type_angle(&mut h, "45.5"), 45.0);
|
||||
assert_eq!(type_angle(&mut h, "-30"), -30.0);
|
||||
assert!(alerts(&h).is_empty(), "in range: no alert, {:?}", alerts(&h));
|
||||
assert_eq!(type_angle(&mut h, "400"), 180.0);
|
||||
assert_eq!(alerts(&h), vec!["An integer between -180 ° and 180 ° is required. Closest value inserted.".to_string()]);
|
||||
assert!(shown(&h).starts_with("180"), "{}", shown(&h));
|
||||
h.state_mut().ui.dialogs.clear();
|
||||
assert_eq!(type_angle(&mut h, "-200"), -180.0);
|
||||
assert_eq!(alerts(&h).len(), 1, "one alert for -200");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pressure_for_size_follows_protect_foreground_color_off_by_default() {
|
||||
let mut h = bar(Tool::BackgroundEraser);
|
||||
assert!(left(&h, "Protect Foreground Color") < left(&h, PRESSURE));
|
||||
assert!(!h.state().session.tools.brush.pressure_size, "off by default");
|
||||
h.get_by_label(PRESSURE).click();
|
||||
h.run_steps(2);
|
||||
assert!(h.state().session.tools.brush.pressure_size);
|
||||
h.get_by_label(PRESSURE).click();
|
||||
h.run_steps(2);
|
||||
assert!(!h.state().session.tools.brush.pressure_size);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn angle_and_pressure_are_the_background_erasers_own() {
|
||||
let mut h = bar(Tool::Brush);
|
||||
h.state_mut().run("tools.setBrush", json!({"brush": {"angle": -20.0}})).unwrap();
|
||||
h.run_steps(2);
|
||||
h.state_mut().ui.tool = Tool::BackgroundEraser;
|
||||
h.run_steps(3);
|
||||
type_angle(&mut h, "30");
|
||||
h.get_by_label(PRESSURE).click();
|
||||
h.run_steps(2);
|
||||
h.state_mut().ui.tool = Tool::Brush;
|
||||
h.run_steps(3);
|
||||
let brush = &h.state().session.tools.brush;
|
||||
assert_eq!((brush.angle, brush.pressure_size), (-20.0, false), "the Brush keeps its own angle and pressure");
|
||||
h.state_mut().ui.tool = Tool::BackgroundEraser;
|
||||
h.run_steps(3);
|
||||
let brush = &h.state().session.tools.brush;
|
||||
assert_eq!((brush.angle, brush.pressure_size), (30.0, true), "the Background Eraser's values come back");
|
||||
assert!(shown(&h).starts_with("30"), "{}", shown(&h));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_field_shows_the_brush_settings_angle() {
|
||||
let mut h = bar(Tool::BackgroundEraser);
|
||||
// The Brush Settings panel edits the live brush's angle through `tools.setBrush`.
|
||||
h.state_mut().run("tools.setBrush", json!({"brush": {"angle": 60.0}})).unwrap();
|
||||
h.run_steps(2);
|
||||
assert!(shown(&h).starts_with("60"), "{}", shown(&h));
|
||||
type_angle(&mut h, "-75");
|
||||
assert_eq!(h.state().session.tools.brush.angle, -75.0, "what the panel reads");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_magic_eraser_has_neither() {
|
||||
let h = bar(Tool::MagicEraser);
|
||||
assert!(angle_field(&h).is_none());
|
||||
assert!(h.query_by_label(PRESSURE).is_none());
|
||||
}
|
||||
@@ -997,6 +997,8 @@ mod pen_path_ownership_tests;
|
||||
#[cfg(test)]
|
||||
mod artboard_options_bar_tests;
|
||||
#[cfg(test)]
|
||||
mod background_eraser_bar_angle_tests;
|
||||
#[cfg(test)]
|
||||
mod doc_tab_menu_tests;
|
||||
#[cfg(test)]
|
||||
mod layers_empty_area_menu_tests;
|
||||
|
||||
@@ -90,6 +90,37 @@ fn opt(ui: &mut egui::Ui, text: &str) {
|
||||
crate::widgets::scrub_option_label(ui, &label);
|
||||
}
|
||||
|
||||
/// The reference application's alert for a Background Eraser Angle typed outside −180…180.
|
||||
pub(crate) const ANGLE_RANGE_MESSAGE: &str = "An integer between -180 ° and 180 ° is required. Closest value inserted.";
|
||||
|
||||
/// The Background Eraser's Angle: an angle icon and a whole-degree field on the brush tip's
|
||||
/// angle, −180…180, default 0. Returns true when a typed value was out of range and was clamped
|
||||
/// (400 → 180): the caller shows [`ANGLE_RANGE_MESSAGE`] once the entry is committed.
|
||||
fn angle_field(ui: &mut egui::Ui, b: &mut photocraft_engine::BrushSettings) -> bool {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let tip = tl!("Set the brush angle");
|
||||
ui.add(icons::image("rotate-cw", 15.0, t.text_dim)).on_hover_text(tip);
|
||||
// A wider range than the angle's, so a typed 400 reaches us and can be told apart from 180.
|
||||
let mut a = b.angle;
|
||||
let r = widgets::value_field_named(ui, tl!("Angle"), &mut a, -ANGLE_TYPED_MAX..=ANGLE_TYPED_MAX, "°", 52.0);
|
||||
let out_of_range = r.id.with("bg-eraser-angle-out-of-range");
|
||||
if r.changed() && a.is_finite() {
|
||||
let whole = a.trunc();
|
||||
let clamped = whole.clamp(-180.0, 180.0);
|
||||
b.angle = clamped;
|
||||
// Only typing alerts: a scrub or an arrow-key step past the end just stops there.
|
||||
let stepped = r.dragged() || ui.input(|i| i.key_pressed(egui::Key::ArrowUp) || i.key_pressed(egui::Key::ArrowDown) || i.smooth_scroll_delta.y != 0.0);
|
||||
let typed_out = whole != clamped && !stepped;
|
||||
ui.data_mut(|d| d.insert_temp(out_of_range, typed_out));
|
||||
}
|
||||
let r = r.on_hover_text(tip);
|
||||
// Enter (or a click away) commits the entry: the alert comes then, not mid-typing.
|
||||
r.lost_focus() && ui.data_mut(|d| d.remove_temp::<bool>(out_of_range)).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// How far the Angle field lets a typed number through before [`angle_field`] clamps it.
|
||||
const ANGLE_TYPED_MAX: f32 = 1.0e6;
|
||||
|
||||
/// Options bar for the Magic Eraser and Background Eraser. Returns false for other tools.
|
||||
pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bool {
|
||||
let o = &mut app.ui.tool_options;
|
||||
@@ -127,7 +158,21 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
|
||||
widgets::dropdown(ui, "bg-eraser-limits", tl!("Limits:"), &mut o.bg_limits, &limits, 110.0);
|
||||
opt(ui, tl!("Tolerance:"));
|
||||
widgets::value_field(ui, &mut o.bg_tolerance, 0.0..=100.0, "%", 62.0);
|
||||
widgets::checkbox(ui, &mut o.bg_protect_fg, tl!("Protect Foreground Color"));
|
||||
// Angle and Pressure for Size edit the tool's own brush (the shell swaps one in per
|
||||
// tool), through `tools.setBrush` like the Brush Settings panel.
|
||||
let before = app.session.tools.brush.clone();
|
||||
let mut brush = before.clone();
|
||||
if angle_field(ui, &mut brush) {
|
||||
let message = tl!(ANGLE_RANGE_MESSAGE).to_string();
|
||||
app.ui.open_dialog(crate::state::DialogKind::Error, json!({ "message": message }).as_object().cloned().unwrap_or_default());
|
||||
}
|
||||
widgets::checkbox(ui, &mut app.ui.tool_options.bg_protect_fg, tl!("Protect Foreground Color"));
|
||||
let name = tl!("Always use pressure for size");
|
||||
let pressure = icons::button_with_icon_size_tip(ui, "circle-dot", 24.0, (24.0f32 * 0.52).round(), brush.pressure_size, name, false);
|
||||
if pressure.on_hover_text(tl!("Always use Pressure for Size. When off, Brush Preset controls pressure")).clicked() {
|
||||
brush.pressure_size = !brush.pressure_size;
|
||||
}
|
||||
crate::brush_panel::commit_gesture(app, ui.ctx(), &before, &brush);
|
||||
}
|
||||
_ => return false,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user