Skip to content

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

fix(ui): widen tool-options drag targets (#5135)

Ikki Nishihara42fb79e909cd
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index f8cc8f21a..5929e9338 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -2988,3 +2988,6 @@ mod channel_tint_tests {
assert_eq!(channel_tint(Rgb, 0, 77, false), Color32::from_gray(77));
}
}
+
+#[cfg(test)]
+mod options_hit_area_tests;
diff --git a/crates/ui-egui/src/options_hit_area_tests.rs b/crates/ui-egui/src/options_hit_area_tests.rs
new file mode 100644
index 000000000..d444f0647
--- /dev/null
+++ b/crates/ui-egui/src/options_hit_area_tests.rs
@@ -0,0 +1,199 @@
+//! #5052: the actual Brush options caption and field form one scrub target.
+use crate::PhotocraftApp;
+use egui::{Modifiers, Pos2, Rect, pos2, vec2};
+use egui_kittest::{Harness, kittest::Queryable};
+
+fn harness() -> Harness<'static, PhotocraftApp> {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.ui.tool = crate::state::Tool::Brush;
+ app.session.tools.brush.hardness = 0.5;
+ let mut h = Harness::builder().with_size(vec2(1400.0, 60.0)).build_ui_state(
+ |ui, app: &mut PhotocraftApp| {
+ if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
+ crate::panels::options_bar(app, ui);
+ }
+ },
+ app,
+ );
+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Studio);
+ h.run_steps(4);
+ h
+}
+
+fn geometry(h: &Harness<'_, PhotocraftApp>) -> (Rect, Rect) {
+ let label = h.get_by_label("Hardness").rect();
+ let field = h
+ .get_all_by_role(egui::accesskit::Role::SpinButton)
+ .map(|n| n.rect())
+ .filter(|r| r.left() >= label.right())
+ .min_by(|a, b| a.left().total_cmp(&b.left()))
+ .unwrap();
+ (label, field)
+}
+
+fn drag(h: &mut Harness<'_, PhotocraftApp>, at: Pos2, dx: f32, mods: Modifiers) {
+ h.hover_at(at);
+ h.run();
+ h.drag_at(at);
+ h.run();
+ for i in 1..=4 {
+ h.event(egui::Event::ModifiersChanged(mods));
+ h.hover_at(at + vec2(dx * i as f32 / 4.0, 0.0));
+ h.run();
+ }
+ h.drop_at(at + vec2(dx, 0.0));
+ h.run_steps(2);
+}
+
+#[test]
+fn options_hardness_label_drag_cannot_copy_selected_caption() {
+ let mut h = harness();
+ let (l, _) = geometry(&h);
+ drag(&mut h, pos2(l.left() + 2.0, l.center().y), 20.0, Modifiers::NONE);
+ assert!(!h.ctx.plugin::<egui::text_selection::LabelSelectionState>().lock().has_selection(), "scrubbing Hardness must not create label text selection");
+ h.event(egui::Event::Copy);
+ h.step();
+ assert!(
+ !h.output().platform_output.commands.iter().any(|c| matches!(c, egui::OutputCommand::CopyText(s) if s.contains("Hard"))),
+ "a scrub caption must not select text for copying"
+ );
+ assert!(h.state().session.tools.brush.hardness > 0.5);
+}
+
+#[test]
+fn options_hardness_caption_upper_and_lower_margins_scrub() {
+ for dy in [-10.0, 10.0] {
+ let mut h = harness();
+ let (l, _) = geometry(&h);
+ drag(&mut h, l.center() + vec2(0.0, dy), 12.0, Modifiers::NONE);
+ assert!((h.state().session.tools.brush.hardness - 0.62).abs() < 0.0001, "24pt caption margin dy={dy}: {}", h.state().session.tools.brush.hardness);
+ assert_eq!(h.state().session.journal.len(), 1, "one command for the whole gesture");
+ }
+}
+
+#[test]
+fn options_hardness_gap_and_suffix_scrub() {
+ for suffix in [false, true] {
+ let mut h = harness();
+ let (l, f) = geometry(&h);
+ let x = if suffix { f.right() + 8.0 } else { (l.right() + f.left() - 4.0) / 2.0 };
+ drag(&mut h, pos2(x, l.center().y), 12.0, Modifiers::NONE);
+ assert!((h.state().session.tools.brush.hardness - 0.62).abs() < 0.0001, "new region suffix={suffix}: {}", h.state().session.tools.brush.hardness);
+ assert_eq!(h.state().session.journal.len(), 1);
+ }
+}
+
+fn click(h: &mut Harness<'_, PhotocraftApp>, at: Pos2) {
+ h.hover_at(at);
+ h.run();
+ h.drag_at(at);
+ h.run();
+ h.drop_at(at);
+ h.run_steps(2);
+}
+
+#[test]
+fn options_numeric_editing_and_focused_text_selection_keep_priority() {
+ let mut h = harness();
+ let (_, f) = geometry(&h);
+ click(&mut h, f.center());
+ let id = h.ctx.memory(|m| m.focused()).expect("click focuses the numeric editor");
+ drag(&mut h, pos2(f.left() + 2.0, f.center().y), f.width() - 4.0, Modifiers::NONE);
+ assert_eq!(h.ctx.memory(|m| m.focused()), Some(id), "text drag keeps numeric focus");
+ assert_eq!(h.state().session.tools.brush.hardness, 0.5, "selecting digits must not scrub");
+ let state = egui::TextEdit::load_state(&h.ctx, id).expect("focused text state");
+ assert!(!state.cursor.char_range().unwrap().is_empty(), "digit selection still works");
+ h.key_press_modifiers(Modifiers::COMMAND, egui::Key::A);
+ h.event(egui::Event::Text("25+5".into()));
+ h.run();
+ h.key_press(egui::Key::Enter);
+ h.run_steps(2);
+ assert!((h.state().session.tools.brush.hardness - 0.3).abs() < 0.0001);
+}
+
+#[test]
+fn options_expanded_hit_click_does_not_edit_or_request_focus() {
+ let mut h = harness();
+ let (_, f) = geometry(&h);
+ click(&mut h, f.center());
+ let before_focus = h.ctx.memory(|m| m.focused()).expect("numeric editor focus");
+ let before_journal = h.state().session.journal.len();
+ let l = h.get_by_label("Hardness").rect();
+ click(&mut h, l.center() + vec2(0.0, 10.0));
+ assert_eq!(h.state().session.tools.brush.hardness, 0.5);
+ println!(
+ "focus_before={before_focus:?} focus_after={:?} value={} journal_before={before_journal} journal_after={}",
+ h.ctx.memory(|m| m.focused()),
+ h.state().session.tools.brush.hardness,
+ h.state().session.journal.len()
+ );
+ assert_eq!(h.ctx.memory(|m| m.focused()), Some(before_focus), "matches actual main: background retains numeric focus without acquiring it");
+ assert_eq!(h.state().session.journal.len(), before_journal);
+}
+
+#[test]
+fn options_added_regions_preserve_modifiers_and_numeric_drag_rate() {
+ for (mods, dx, want) in [(Modifiers::SHIFT, 3.0, 0.8), (Modifiers::ALT, 20.0, 0.52)] {
+ let mut h = harness();
+ let (l, f) = geometry(&h);
+ drag(&mut h, pos2(f.right() + 8.0, l.center().y), dx, mods);
+ assert!((h.state().session.tools.brush.hardness - want).abs() < 0.0001);
+ }
+ let mut h = harness();
+ let (_, f) = geometry(&h);
+ drag(&mut h, f.center(), 20.0, Modifiers::NONE);
+ assert!((h.state().session.tools.brush.hardness - 0.6).abs() < 0.0001, "numeric drag retains its own rate");
+}
+
+#[test]
+fn options_disabled_eraser_caption_and_added_regions_do_nothing() {
+ let mut h = harness();
+ h.state_mut().ui.tool = crate::state::Tool::Eraser;
+ h.state_mut().ui.tool_options.eraser_mode = "block".into();
+ h.run_steps(3);
+ let (l, f) = geometry(&h);
+ for at in [l.center(), pos2(f.right() + 8.0, l.center().y), l.center() + vec2(0.0, 10.0)] {
+ drag(&mut h, at, 12.0, Modifiers::NONE);
+ assert_eq!(h.state().session.tools.brush.hardness, 0.5);
+ assert!(h.state().session.journal.is_empty());
+ }
+}
+
+#[test]
+fn options_field_outer_top_and_bottom_margins_scrub() {
+ for top in [true, false] {
+ let mut h = harness();
+ let (_, f) = geometry(&h);
+ let y = if top { f.top() - 0.5 } else { f.bottom() + 0.5 };
+ drag(&mut h, pos2(f.center().x, y), 12.0, Modifiers::NONE);
+ assert!((h.state().session.tools.brush.hardness - 0.62).abs() < 0.0001, "field margin top={top}");
+ assert_eq!(h.state().session.journal.len(), 1);
+ }
+}
+
+#[test]
+fn options_gesture_keeps_its_start_owner_when_crossing_regions() {
+ let mut h = harness();
+ let (l, _) = geometry(&h);
+ drag(&mut h, pos2(l.right() - 2.0, l.center().y), 24.0, Modifiers::NONE);
+ assert!((h.state().session.tools.brush.hardness - 0.74).abs() < 0.0001);
+ assert_eq!(h.state().session.journal.len(), 1);
+ let mut h = harness();
+ let (_, f) = geometry(&h);
+ drag(&mut h, pos2(f.left() + 2.0, f.center().y), -30.0, Modifiers::NONE);
+ assert!((h.state().session.tools.brush.hardness - 0.35).abs() < 0.0001);
+ assert_eq!(h.state().session.journal.len(), 1);
+}
+
+#[test]
+fn options_tab_moves_from_hardness_to_opacity_without_a_caption_stop() {
+ let mut h = harness();
+ let (_, f) = geometry(&h);
+ click(&mut h, f.center());
+ h.key_press(egui::Key::Tab);
+ h.run_steps(2);
+ h.key_press(egui::Key::ArrowDown);
+ h.run_steps(2);
+ assert_eq!(h.state().session.tools.brush.hardness, 0.5);
+ assert!((h.state().session.tools.brush.opacity - 0.99).abs() < 0.0001);
+}
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 470a72bfe..769f5a8b9 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -1561,8 +1561,8 @@ fn body_text_width(ui: &egui::Ui, s: &str) -> f32 {
fn opt_label(ui: &mut egui::Ui, s: &str) {
let t = Tokens::get(ui.ctx());
let text = if t.pro && !s.ends_with(':') { format!("{s}:") } else { s.to_string() };
- let label = ui.label(RichText::new(tl!(&text)).color(t.text_dim));
- widgets::scrub_label(ui, &label);
+ let label = ui.add(egui::Label::new(RichText::new(tl!(&text)).color(t.text_dim)).selectable(false));
+ widgets::scrub_option_label(ui, &label);
}
fn percent_field(ui: &mut egui::Ui, label: &str, value: &mut f32, range: std::ops::RangeInclusive<f32>, width: f32) {
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index b9b033670..c6e62ecaa 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -384,6 +384,8 @@ struct LabelScrub {
rect: Rect,
/// How far right of the label the field may start (the whole row for a slider row).
max_gap: f32,
+ /// Options captions defer interaction until the adjacent field bounds are known.
+ option_area: bool,
/// Whole steps to move the value (left negative; Shift ×10, Alt a tenth as fast).
steps: f32,
started: bool,
@@ -402,9 +404,35 @@ pub fn scrub_label(ui: &Ui, label: &Response) -> Response {
scrub_label_within(ui, label, 24.0)
}
+/// A tool-options caption whose scrub target includes the adjacent field's outer box.
+/// Keep the original label bounds for linking; register the hit area before the numeric editor.
+pub fn scrub_option_label(ui: &Ui, label: &Response) {
+ let scrub = LabelScrub {
+ pass: ui.ctx().cumulative_pass_nr(),
+ label: label.id.with("pc-option-scrub"),
+ rect: label.rect,
+ max_gap: 24.0,
+ option_area: true,
+ steps: 0.0,
+ started: false,
+ dragging: false,
+ stopped: false,
+ };
+ ui.data_mut(|d| d.insert_temp(label_scrub_id(), scrub));
+}
+
fn scrub_label_within(ui: &Ui, label: &Response, max_gap: f32) -> Response {
// Drag only, not focusable: Tab goes to the value field, not to its label as well.
let resp = label.interact(Sense::DRAG);
+ let scrub = label_scrub_response(ui, &resp, label.rect, max_gap);
+ ui.data_mut(|d| {
+ d.insert_temp(label_scrub_id(), scrub);
+ d.insert_temp(resp.id.with("pc-scrub-linked"), false);
+ });
+ resp
+}
+
+fn label_scrub_response(ui: &Ui, resp: &Response, original_rect: Rect, max_gap: f32) -> LabelScrub {
// Whether a field took this label's drags last frame: only then is it scrubby.
let linked = ui.data(|d| d.get_temp::<bool>(resp.id.with("pc-scrub-linked"))).unwrap_or(false);
if linked && resp.enabled() && (resp.hovered() || resp.dragged()) {
@@ -434,21 +462,17 @@ fn scrub_label_within(ui: &Ui, label: &Response, max_gap: f32) -> Response {
steps = acc.trunc();
ui.data_mut(|d| d.insert_temp(acc_id, acc - steps));
}
- let scrub = LabelScrub {
+ LabelScrub {
pass: ui.ctx().cumulative_pass_nr(),
label: resp.id,
- rect: resp.rect,
+ rect: original_rect,
max_gap,
+ option_area: false,
steps,
started: resp.drag_started(),
dragging: resp.dragged(),
stopped: resp.drag_stopped(),
- };
- ui.data_mut(|d| {
- d.insert_temp(label_scrub_id(), scrub);
- d.insert_temp(resp.id.with("pc-scrub-linked"), false);
- });
- resp
+ }
}
/// The label drag left for the field at `rect` this frame, if its label is just left of it on
@@ -505,7 +529,17 @@ fn value_field_box(
let FieldOpts { decimals, units, wrap_drag } = opts;
let t = Tokens::get(ui.ctx());
let (rect, slot) = ui.allocate_exact_size(size, Sense::hover());
- let scrub = take_label_scrub(ui, rect);
+ let scrub = take_label_scrub(ui, rect).map(|s| {
+ if !s.option_area {
+ return s;
+ }
+ // A direct click+drag target avoids egui snapping the padding/suffix to the nearby
+ // numeric button. It is not focusable; the editor drawn next wins inside its bounds.
+ // Preserve a popup field's trailing arrow, and never extend outside the UI clip.
+ let hit = Rect::from_min_max(pos2(s.rect.left(), rect.top()), pos2(rect.right() - trailing, rect.bottom())).intersect(ui.clip_rect());
+ let resp = ui.interact(hit, s.label, Sense::CLICK | Sense::DRAG);
+ label_scrub_response(ui, &resp, s.rect, s.max_gap)
+ });
let (dpi, ppi) = ui.data(|d| d.get_temp::<(f64, f64)>(field_resolution_id())).unwrap_or((72.0, 72.0));
// Typed text loses its spaces before parsing, so a " pt" suffix reads as "pt".
let typed_suffix = suffix.trim();