Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Layer Mask Properties: the Feather slider has fine steps near 0 px, as in Photoshop (#6545) (#6727)

Marco Arnulfoa0128945056a
diff --git a/crates/ui-egui/src/adjust_editors.rs b/crates/ui-egui/src/adjust_editors.rs
index fa32a3b39..0c4d5ae26 100644
--- a/crates/ui-egui/src/adjust_editors.rs
+++ b/crates/ui-egui/src/adjust_editors.rs
@@ -216,7 +216,7 @@ fn sliders(ui: &mut egui::Ui, v: &mut Value, rows: &[SliderRow]) -> Edit {
for &(key, text, min, max, default, unit, decimals) in rows {
let mut x = num(v, key, default).clamp(min, max);
// A double-click on the slider puts it back to its default (Photoshop).
- let gestures = widgets::RowGestures { reset: Some(default), wheel_step: None };
+ let gestures = widgets::RowGestures { reset: Some(default), wheel_step: None, ..Default::default() };
let r = widgets::slider_row_prec(ui, text, &mut x, min..=max, unit, None, gestures, decimals);
if r.changed() {
v[key] = json!(row_value(x, min, max, decimals));
@@ -1095,7 +1095,7 @@ fn color_balance(ui: &mut egui::Ui, v: &mut Value, cx: &EditorCx) -> Edit {
];
// Photoshop: a double-click on a slider zeroes it (this tone only); the dialog's sliders step
// with the wheel; Up/Down step the fields; Shift makes those steps 10.
- let gestures = widgets::RowGestures { reset: Some(0.0), wheel_step: cx.dialog.then_some(1.0) };
+ let gestures = widgets::RowGestures { reset: Some(0.0), wheel_step: cx.dialog.then_some(1.0), ..Default::default() };
let mut changed = false;
for (i, (text, a, b)) in rows.iter().enumerate() {
let r = Edit::of(&widgets::slider_row_with(ui, text, &mut vals[i], -100.0..=100.0, "", Some(&[*a, *b]), gestures));
diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rs
index 14f24bc52..effb52694 100644
--- a/crates/ui-egui/src/filter_dialog.rs
+++ b/crates/ui-egui/src/filter_dialog.rs
@@ -811,7 +811,7 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
ui.add_space(4.0);
} else {
// A double-click on the slider puts it back to the filter's default.
- let gestures = crate::widgets::RowGestures { reset: Some(default), wheel_step: None };
+ let gestures = crate::widgets::RowGestures { reset: Some(default), wheel_step: None, ..Default::default() };
crate::widgets::slider_row_with(ui, &label(&p.key), &mut v, min..=max, unit, None, gestures);
}
});
diff --git a/crates/ui-egui/src/mask_props_ui.rs b/crates/ui-egui/src/mask_props_ui.rs
index 295310bf9..26c3791d5 100644
--- a/crates/ui-egui/src/mask_props_ui.rs
+++ b/crates/ui-egui/src/mask_props_ui.rs
@@ -66,8 +66,12 @@ fn sliders(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: u64, document: u64
}
g.0
});
- for (field, label, mut value, max, unit) in [("density", tl!("Density"), density * 100.0, 100.0, "%"), ("feather", tl!("Feather"), feather, 1000.0, "px")] {
- let response = widgets::slider_row(ui, label, &mut value, 0.0..=max, unit, None);
+ // Feather's track is curved like Photoshop's, so a few px can be dragged to (#6545).
+ for (field, label, mut value, max, unit, curve) in
+ [("density", tl!("Density"), density * 100.0, 100.0, "%", None), ("feather", tl!("Feather"), feather, 1000.0, "px", Some(3.0))]
+ {
+ let gestures = widgets::RowGestures { curve, ..Default::default() };
+ let response = widgets::slider_row_with(ui, label, &mut value, 0.0..=max, unit, None, gestures);
response.widget_info(|| egui::WidgetInfo::slider(ui.is_enabled(), f64::from(value), label));
if response.changed() {
let mut params = json!({"document": document, "layer": layer,
diff --git a/crates/ui-egui/src/mask_props_ui_tests.rs b/crates/ui-egui/src/mask_props_ui_tests.rs
index 1cde90468..660b94a15 100644
--- a/crates/ui-egui/src/mask_props_ui_tests.rs
+++ b/crates/ui-egui/src/mask_props_ui_tests.rs
@@ -94,8 +94,32 @@ fn mask_slider_drags_preview_live_and_undo_as_separate_gestures() {
h.state_mut().run("edit.redo", json!({})).unwrap();
assert!((mask(&h).density - 0.5).abs() < 0.01);
h.run_steps(3);
- drag(&mut h, 1, &[0.0, 0.01, 0.02]);
- assert!((mask(&h).feather - 20.0).abs() < 0.1);
+ // #6545: Feather's track is curved like Photoshop's, so a fifth of the way is 8 px, not 200.
+ drag(&mut h, 1, &[0.0, 0.1, 0.2]);
+ assert!((mask(&h).feather - 8.0).abs() < 0.3, "feather {}", mask(&h).feather);
+}
+
+/// #6545: the curve is only the track's. A typed Feather and the arrow keys stay exact px, and
+/// drawing the knob for them never writes the value back.
+#[test]
+fn typed_and_stepped_feather_stays_exact() {
+ let mut h = harness(false, crate::theme::ThemeKind::Pro);
+ h.query_all_by_role(egui::accesskit::Role::SpinButton).nth(1).unwrap().click();
+ h.run_steps(1);
+ h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
+ h.event(egui::Event::Text("5".into()));
+ h.key_press(egui::Key::Enter);
+ h.run_steps(3);
+ assert_eq!(mask(&h).feather, 5.0);
+ h.query_all_by_role(egui::accesskit::Role::SpinButton).nth(1).unwrap().click();
+ h.run_steps(1);
+ h.key_press(egui::Key::ArrowUp);
+ h.run_steps(3);
+ assert_eq!(mask(&h).feather, 6.0);
+ let before = h.state().session.active().unwrap().history.past_len();
+ h.run_steps(5);
+ assert_eq!(mask(&h).feather, 6.0);
+ assert_eq!(h.state().session.active().unwrap().history.past_len(), before, "idle frames write nothing");
}
#[test]
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index b393c0787..19f47830d 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -926,6 +926,34 @@ pub struct RowGestures {
/// Each wheel notch over the slider moves the value by this step (up = higher; ×10 with
/// Shift). Photoshop does this in dialogs, not in the Properties panel.
pub wheel_step: Option<f32>,
+ /// A curved track: the knob at fraction f of the track is `lo + (hi - lo) * f^k`, so small
+ /// values get fine steps (Photoshop's mask Feather, #6545). Typed, scrubbed and stepped
+ /// values stay linear; only the track maps through the curve.
+ pub curve: Option<f32>,
+}
+
+/// The value at fraction `f` of a track over `lo..=hi`, through `curve` (see [`RowGestures`]).
+pub(crate) fn curve_value(f: f32, lo: f32, hi: f32, curve: Option<f32>) -> f32 {
+ match curve {
+ Some(k) if k.is_finite() && k > 0.0 => lo + f.clamp(0.0, 1.0).powf(k) * (hi - lo),
+ _ => lo + f * (hi - lo),
+ }
+}
+
+/// Where `v` sits on a track over `lo..=hi`, as a fraction: the inverse of [`curve_value`].
+pub(crate) fn curve_fraction(v: f32, lo: f32, hi: f32, curve: Option<f32>) -> f32 {
+ let f = ((v - lo) / (hi - lo)).clamp(0.0, 1.0);
+ match curve {
+ // An unset (NaN) value draws the knob at the start rather than nowhere.
+ Some(k) if k.is_finite() && k > 0.0 => {
+ if f.is_finite() {
+ f.powf(1.0 / k)
+ } else {
+ 0.0
+ }
+ }
+ _ => f,
+ }
}
/// Wheel notches this frame while the pointer is over `resp` (Shift ×10). Wheel lines add up in
@@ -1010,7 +1038,7 @@ fn slider_with(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive<f32
&& (resp.dragged() || resp.clicked())
{
let f = ((p.x - track.left()) / track.width()).clamp(0.0, 1.0);
- let nv = lo + f * (hi - lo);
+ let nv = curve_value(f, lo, hi, g.curve);
if (nv - *value).abs() > f32::EPSILON {
*value = nv;
resp.mark_changed();
@@ -1033,7 +1061,7 @@ fn slider_with(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive<f32
}
}
}
- let f = ((*value - lo) / (hi - lo)).clamp(0.0, 1.0);
+ let f = curve_fraction(*value, lo, hi, g.curve);
let knob_x = track.left() + f * track.width();
let painter = ui.painter();
match gradient {
@@ -2098,6 +2126,28 @@ mod tests {
assert_eq!(press(&mut h, Modifiers::SHIFT, Key::ArrowUp), 1000.0);
}
+ /// #6545: a curved track (the mask Feather) has fine steps at the left and coarse ones toward
+ /// the right end, and the knob drawn for a value lands where that value is picked.
+ #[test]
+ fn curved_track_maps_ends_middle_and_round_trips() {
+ use super::{curve_fraction, curve_value};
+ for (f, want) in [(0.0, 0.0), (0.1, 1.0), (0.2, 8.0), (0.5, 125.0), (1.0, 1000.0)] {
+ assert!((curve_value(f, 0.0, 1000.0, Some(3.0)) - want).abs() < 1e-3, "{f}: {}", curve_value(f, 0.0, 1000.0, Some(3.0)));
+ }
+ assert_eq!(curve_value(0.25, 0.0, 1000.0, None), 250.0, "no curve is the linear track");
+ assert_eq!(curve_value(0.5, 10.0, 20.0, Some(3.0)), 11.25, "the curve starts at the range's low end");
+ for i in 0..=100 {
+ let f = i as f32 / 100.0;
+ let back = curve_fraction(curve_value(f, 0.0, 1000.0, Some(3.0)), 0.0, 1000.0, Some(3.0));
+ assert!((back - f).abs() < 1e-4, "{f} -> {back}");
+ let v = i as f32 * 10.0;
+ let again = curve_value(curve_fraction(v, 0.0, 1000.0, Some(3.0)), 0.0, 1000.0, Some(3.0));
+ assert!((again - v).abs() < 1e-3, "{v} -> {again}");
+ }
+ assert_eq!(curve_fraction(-5.0, 0.0, 1000.0, Some(3.0)), 0.0);
+ assert_eq!(curve_fraction(f32::NAN, 0.0, 1000.0, Some(3.0)), 0.0);
+ }
+
#[test]
fn arrow_keys_work_out_a_typed_sum_then_step_it() {
let mut h = focused(100.0, 0.0..=1000.0);