diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs index 9f20214104..d03c3339dc 100644 --- a/crates/ui-egui/src/panels/properties.rs +++ b/crates/ui-egui/src/panels/properties.rs @@ -122,15 +122,28 @@ fn layer_controls(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer) { }); ui.add_space(6.0); } - let mut o = layer.opacity * 100.0; - let r = widgets::slider_row(ui, tl!("Opacity"), &mut o, 0.0..=100.0, "%", None); - if r.drag_stopped() || (r.changed() && !r.dragged()) { - let _ = app.run("layer.setProps", json!({"layer": layer.id.0, "opacity": o / 100.0})); + pct_slider(app, ui, layer, "opacity", tl!("Opacity"), layer.opacity); + pct_slider(app, ui, layer, "fill", crate::i18n::tr_ctx(crate::i18n::current(), "layers", "Fill"), layer.fill_opacity); +} + +/// An Opacity or Fill (`key`) slider, applied as it moves: a drag is one history step, as with the +/// Layers panel's sliders. Applying only when the drag stopped wrote back the old value, since the +/// slider takes the pointer only while it is dragged or clicked, not on the release (#9683, #7601). +fn pct_slider(app: &mut PhotocraftApp, ui: &mut egui::Ui, layer: &Layer, key: &str, label: &str, current: f32) { + let mut v = current * 100.0; + let r = widgets::slider_row(ui, label, &mut v, 0.0..=100.0, "%", None); + let stamp = r.id.with("drag-stamp"); + if r.drag_started() { + let n = ui.ctx().cumulative_pass_nr(); + ui.data_mut(|d| d.insert_temp(stamp, n)); } - let mut f = layer.fill_opacity * 100.0; - let r = widgets::slider_row(ui, crate::i18n::tr_ctx(crate::i18n::current(), "layers", "Fill"), &mut f, 0.0..=100.0, "%", None); - if r.drag_stopped() || (r.changed() && !r.dragged()) { - let _ = app.run("layer.setProps", json!({"layer": layer.id.0, "fill": f / 100.0})); + if r.changed() { + let drag = if r.dragged() { ui.data(|d| d.get_temp::(stamp)) } else { None }; + let (id, p) = super::options_bar::pct_action(&json!({"layer": layer.id.0}), key, v, drag); + if let Err(e) = app.run(&id, p) { + app.ui.status = e; + app.ui.status_error = true; + } } } @@ -277,3 +290,73 @@ pub(super) fn selection_mode_buttons(app: &mut PhotocraftApp, ui: &mut egui::Ui) } ui.spacing_mut().item_spacing.x = 8.0; } + +#[cfg(test)] +mod tests { + use egui::accesskit::Role; + use egui_kittest::kittest::{NodeT, Queryable}; + + use super::*; + + /// #9683, #7601: dragging the Studio Properties panel's Opacity or Fill slider bounced back to + /// the old value on release. A drag keeps where it was let go, as one history step. + #[test] + fn opacity_and_fill_sliders_keep_a_drag() { + for key in ["Opacity", "Fill"] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 120})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(vec2(320.0, 400.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if ui.ctx().cumulative_pass_nr() == 0 { + PhotocraftApp::setup_context(ui.ctx(), crate::theme::ThemeKind::ALL[0]); + return; + } + let st = app.session.active().unwrap(); + let layer = st.doc.layer(st.active_layer.unwrap()).unwrap().clone(); + layer_controls(app, ui, &layer); + }, + app, + ); + h.run_steps(3); + let value = |h: &egui_kittest::Harness<'_, PhotocraftApp>| { + let st = h.state().session.active().unwrap(); + let l = st.doc.layer(st.active_layer.unwrap()).unwrap(); + if key == "Opacity" { l.opacity } else { l.fill_opacity } + }; + let steps = |h: &egui_kittest::Harness<'_, PhotocraftApp>| h.state().session.active().unwrap().history.entries().len(); + let before = steps(&h); + assert!((value(&h) - 1.0).abs() < 1e-6); + let r = h + .query_all_by_role(Role::Slider) + .find(|n| n.accesskit_node().label().as_deref() == Some(key)) + .map(|n| n.rect()) + .unwrap_or_else(|| panic!("no {key} slider")); + // The press lands away from the current value and the release moves on again, so the + // press and release frames change the value as well as the drag. + let (from, to) = (egui::pos2(r.left() + r.width() * 0.8, r.center().y), egui::pos2(r.left() + r.width() * 0.35, r.center().y)); + h.hover_at(from); + h.run_steps(1); + h.drag_at(from); + h.run_steps(1); + let mut last = value(&h); + for i in 1..=6 { + h.hover_at(from + (to - from) * (i as f32 / 6.0)); + h.run_steps(1); + let v = value(&h); + assert!(v < last, "{key}: the value follows the drag, not only the release ({last} to {v})"); + last = v; + } + // The pointer moves on and releases in one frame, as a quick flick does. + let release = to - egui::vec2(r.width() * 0.05, 0.0); + h.hover_at(release); + h.drop_at(release); + h.run_steps(3); + let v = value(&h); + assert!(v > 0.15 && v < last, "{key}: the release position wasn't applied ({last} to {v})"); + assert_eq!(steps(&h), before + 1, "{key}: one history step for the drag"); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert!((value(&h) - 1.0).abs() < 1e-6, "{key}: one undo reverts the whole drag"); + } + } +}