Skip to content

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

Fix/2361 scrub layer labels s20261010 t083444 z og3kyw (#2397)

Nigel Ihza Firdausy743d81307b34
diff --git a/crates/ui-egui/src/layer_pct_slider_tests.rs b/crates/ui-egui/src/layer_pct_slider_tests.rs
index eae2a53cf..4a717e210 100644
--- a/crates/ui-egui/src/layer_pct_slider_tests.rs
+++ b/crates/ui-egui/src/layer_pct_slider_tests.rs
@@ -135,3 +135,31 @@ fn the_background_layer_has_no_pop_up_slider() {
click(&mut h, arrow.center());
assert!(by_role(&h, "Opacity", Role::Slider).is_none(), "Photoshop greys Opacity for the Background");
}
+
+#[test]
+fn dragging_opacity_and_fill_labels_scrubs_percentages_in_one_history_step() {
+ for (name, value) in [("Opacity", (|l: &photocraft_doc::Layer| l.opacity) as fn(&photocraft_doc::Layer) -> f32), ("Fill", |l| l.fill_opacity)] {
+ let mut h = harness();
+ // The label, not the adjacent spin button or popup arrow, is the drag target.
+ // The popup arrow is named after its field; the text label sits on the same row (egui
+ // exposes a label's text as its value, not its name).
+ let arrow = by_role(&h, name, Role::Button).unwrap_or_else(|| panic!("missing {name} popup arrow"));
+ let with_colon = format!("{name}:");
+ let rect = h
+ .query_all_by_role(Role::Label)
+ .filter(|node| {
+ let text = node.accesskit_node().value();
+ (text.as_deref() == Some(name) || text.as_deref() == Some(with_colon.as_str())) && (node.rect().center().y - arrow.center().y).abs() < 10.0
+ })
+ .map(|node| node.rect())
+ .next()
+ .unwrap_or_else(|| panic!("missing {name} text label"));
+ let before = steps(&h);
+ drag(&mut h, rect.center(), rect.center() - vec2(40.0, 0.0));
+ assert!((value(&layer(&h)) - 0.8).abs() < 0.03, "{name}: label scrub changed the value");
+ assert_eq!(steps(&h), before + 1, "{name}: a label scrub is one history step");
+ h.state_mut().session.undo();
+ h.run_steps(3);
+ assert!((value(&layer(&h)) - 1.0).abs() < 1e-6, "{name}: undo restores the original value");
+ }
+}
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 1bfd22938..caab8b001 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -1295,6 +1295,36 @@ fn label(ui: &mut egui::Ui, s: &str) {
ui.label(RichText::new(s).color(t.text_dim));
}
+/// Photoshop's scrubby Opacity/Fill labels: drag horizontally to change the percentage without
+/// hitting the small number field. The unique drag ID coalesces every frame into one undo step.
+fn scrub_pct_label(ui: &mut egui::Ui, text: &str, percent: &mut f32) -> widgets::PopupFieldResponse {
+ let t = Tokens::get(ui.ctx());
+ let resp =
+ ui.add(egui::Label::new(RichText::new(text).color(t.text_dim)).sense(Sense::click_and_drag())).on_hover_cursor(egui::CursorIcon::ResizeHorizontal);
+ let key = resp.id.with("percent-label-drag");
+ if resp.drag_started() {
+ let stamp = ui.ctx().cumulative_pass_nr();
+ ui.data_mut(|d| d.insert_temp(key, (*percent, stamp)));
+ }
+ let mut out = widgets::PopupFieldResponse::default();
+ if resp.dragged()
+ && let Some((start, stamp)) = ui.data(|d| d.get_temp::<(f32, u64)>(key))
+ && let (Some(origin), Some(pos)) = (ui.input(|i| i.pointer.press_origin()), resp.interact_pointer_pos())
+ {
+ // Match DragValue's 0.5 percentage-point per screen point; do not jump on press.
+ let next = (start + (pos.x - origin.x) * 0.5).clamp(0.0, 100.0);
+ if next.is_finite() && next != *percent {
+ *percent = next;
+ out.changed = true;
+ out.drag = Some(stamp);
+ }
+ }
+ if resp.drag_stopped() {
+ ui.data_mut(|d| d.remove::<(f32, u64)>(key));
+ }
+ out
+}
+
/// Width of `s` as [`label`] draws it (body text).
fn body_text_width(ui: &egui::Ui, s: &str) -> f32 {
let font = egui::TextStyle::Body.resolve(ui.style());
@@ -1771,11 +1801,11 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
crate::blend_preview::hover(app, l.id, hovered.filter(|_| chosen.is_empty()));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let mut o = l.opacity * 100.0;
- let r = widgets::popup_value_field(ui, opacity_label, &mut o, 0.0..=100.0, "%", LAYER_PCT_W);
- if r.changed {
- actions.push(pct_action(l, "opacity", o, r.drag));
+ let field = widgets::popup_value_field(ui, opacity_label, &mut o, 0.0..=100.0, "%", LAYER_PCT_W);
+ let scrub = scrub_pct_label(ui, opacity_label, &mut o);
+ if field.changed || scrub.changed {
+ actions.push(pct_action(l, "opacity", o, scrub.drag.or(field.drag)));
}
- label(ui, opacity_label);
});
});
});
@@ -1820,11 +1850,11 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
ui.add_enabled_ui(!bg, |ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let mut f = l.fill_opacity * 100.0;
- let r = widgets::popup_value_field(ui, fill_label, &mut f, 0.0..=100.0, "%", LAYER_PCT_W);
- if r.changed {
- actions.push(pct_action(l, "fill", f, r.drag));
+ let field = widgets::popup_value_field(ui, fill_label, &mut f, 0.0..=100.0, "%", LAYER_PCT_W);
+ let scrub = scrub_pct_label(ui, fill_label, &mut f);
+ if field.changed || scrub.changed {
+ actions.push(pct_action(l, "fill", f, scrub.drag.or(field.drag)));
}
- label(ui, fill_label);
})
});
});