diff --git a/crates/ui-egui/src/panels/edit/sliders.rs b/crates/ui-egui/src/panels/edit/sliders.rs index 044fd61513..d484ead4c8 100644 --- a/crates/ui-egui/src/panels/edit/sliders.rs +++ b/crates/ui-egui/src/panels/edit/sliders.rs @@ -2,6 +2,7 @@ use super::{ Align2, ControlSpec, DevelopSettings, Icon, LightcraftApp, PhotoId, Section, Sense, SliderOut, Tokens, Track, Value, controls, divider, json, pos2, section_header, vec2, }; +use crate::state::InteractionOwner; use crate::widgets::slider_salted; /// Commit a slider interaction: begin → live updates → end, so a drag is one undo step, and a typed value, a key @@ -15,24 +16,47 @@ pub fn apply_slider_out( ) { if out.cancelled { if app.ui.dragging_control.as_deref() == Some(spec.id) { - let _ = app.run("develop.cancelInteraction", json!({})); - app.ui.dragging_control = None; + // Cancel only the interaction this drag began. The control's id names the slider, not the engine's + // interaction: an automation client can have begun its own in the middle of the drag + // (`develop.beginInteraction` ends the drag's), and that one's settings are not the drag's to restore. + if owns_live(app) { + let _ = app.run("develop.cancelInteraction", json!({})); + } + release(app); } return; } if out.drag_started { - let _ = app.run("develop.beginInteraction", json!({"label": spec.label})); + let begun = app.run("develop.beginInteraction", json!({"label": spec.label})).is_ok(); app.ui.dragging_control = Some(spec.id.to_string()); + // a begin that failed began no interaction this drag may later end or cancel + app.ui.dragging_interaction = if begun { app.session.interaction.as_ref().map(InteractionOwner::of) } else { None }; } if let Some(v) = out.value { let _ = set(app, v); } if out.drag_stopped { - let _ = app.run("develop.endInteraction", json!({})); - app.ui.dragging_control = None; + // As with a cancel: only the drag's own interaction is ended. One another client began in its place is + // that client's to finish. + if owns_live(app) { + let _ = app.run("develop.endInteraction", json!({})); + } + release(app); } } +/// Whether the session's live engine interaction is the one the dragged control's slider began. A drag that +/// recorded no interaction (`None`) has only its control's id to go on, as before. +fn owns_live(app: &LightcraftApp) -> bool { + app.ui.dragging_interaction.as_ref().is_none_or(|owner| owner.is_live(&app.session)) +} + +/// The drag is over: it owns no engine interaction any more. +fn release(app: &mut LightcraftApp) { + app.ui.dragging_control = None; + app.ui.dragging_interaction = None; +} + pub(crate) fn control(app: &mut LightcraftApp, ui: &mut egui::Ui, d: &DevelopSettings, id: &str, enabled: bool) { let Some(spec) = controls::find(id) else { return }; let v = controls::get(d, id).unwrap_or(spec.default); diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 8e6bc9e5eb..87122eee15 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -185,6 +185,10 @@ pub struct UiState { /// The develop control whose slider is being dragged (geometry sliders show a grid). #[serde(skip)] pub dragging_control: Option, + /// The engine interaction the dragged control's slider began (`panels::edit::apply_slider_out`), so its + /// cancel reaches that interaction and not one another client began in its place. + #[serde(skip)] + pub dragging_interaction: Option, /// Alt (Option on macOS) is held this frame: a dragged Edit slider shows its temporary /// preview ([`lightcraft_pipeline::Overlay::for_control`]). #[serde(skip)] @@ -453,6 +457,7 @@ impl Default for UiState { unnamed_focus: false, hidden_locations: Vec::new(), dragging_control: None, + dragging_interaction: None, alt_held: false, external_edits: Vec::new(), was_focused: true, @@ -647,6 +652,36 @@ fn lenient_ids<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D: Ok(value.as_array().into_iter().flatten().filter_map(|v| v.as_str().map(str::to_string)).collect()) } +/// The engine interaction a UI gesture began, so the gesture cancels only that one. An automation client can +/// begin its own interaction in the middle of a drag (`develop.beginInteraction` ends the drag's); cancelling by +/// the control's id alone would then restore the client's settings instead of the drag's, and close its edit. +#[derive(Clone, Debug)] +pub struct InteractionOwner { + photo: lightcraft_catalog::PhotoId, + label: String, + /// The settings the interaction began from. The interaction never replaces them, and holding a clone keeps + /// their address from being reused, so two interactions are told apart by where they hold them. + original: std::sync::Arc, +} + +impl InteractionOwner { + /// The owner of `interaction`, for the gesture that began it. + pub fn of(interaction: &lightcraft_engine::Interaction) -> Self { + Self { photo: interaction.photo, label: interaction.label.clone(), original: interaction.original.clone() } + } + + /// Whether `interaction` is the one this owner began. + pub fn owns(&self, interaction: &lightcraft_engine::Interaction) -> bool { + interaction.photo == self.photo && interaction.label == self.label && std::sync::Arc::ptr_eq(&interaction.original, &self.original) + } + + /// Whether the session's live interaction is this one. `false` once another client replaced it, so the + /// gesture that began it has nothing left to cancel. + pub fn is_live(&self, session: &lightcraft_engine::Session) -> bool { + session.interaction.as_ref().is_some_and(|interaction| self.owns(interaction)) + } +} + /// The navigation/document/modal/panel state a gesture was admitted under. The same automation /// id can mean a different control after any of these changes; the request is canceled rather /// than retargeted. diff --git a/crates/ui-egui/src/tests.rs b/crates/ui-egui/src/tests.rs index cc3b5a1ee7..f7a19504ae 100644 --- a/crates/ui-egui/src/tests.rs +++ b/crates/ui-egui/src/tests.rs @@ -103,6 +103,7 @@ mod rules_editor; mod sam_setup; mod scan_probes; mod scroll; +mod slider_cancel; mod slider_reset; mod slideshow; mod solo_sections; diff --git a/crates/ui-egui/src/tests/slider_cancel.rs b/crates/ui-egui/src/tests/slider_cancel.rs new file mode 100644 index 0000000000..e0f855440a --- /dev/null +++ b/crates/ui-egui/src/tests/slider_cancel.rs @@ -0,0 +1,128 @@ +//! A slider drag keeps the engine interaction it began: an abandoned drag cancels that one, and one another +//! client (an automation command) began in the middle of the drag is that client's to finish. + +use std::time::Duration; + +use serde_json::json; + +use crate::headless::Headless; +use crate::state::InteractionOwner; +use crate::widgets::SliderOut; +use crate::{LightcraftApp, Services}; + +const T: Duration = Duration::from_secs(20); + +/// The demo library with its first photo selected: the drags below only need a photo to edit. +fn panel() -> Headless { + let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() }); + let mut h = Headless::new(app, [1400.0, 1000.0], 1.0); + let id = h.app.session.visible_cloned()[0].0; + let r = h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [id]}}), T); + assert_eq!(r["ok"], true, "{r}"); + h +} + +/// One step of an Exposure slider drag, as the framework reports it to the panel. +fn drag(h: &mut Headless, out: SliderOut) { + let spec = lightcraft_develop::controls::find("light.exposure").expect("the Exposure control"); + crate::panels::edit::apply_slider_out(&mut h.app, spec, out, |app, v| app.run("develop.set", json!({"control": "light.exposure", "value": v}))); +} + +fn value(h: &Headless, control: &str) -> f64 { + let photo = h.app.session.active().expect("an active photo"); + let spec = lightcraft_develop::controls::find(control).expect("a known control"); + lightcraft_develop::controls::get(&h.app.session.develop_of(photo).unwrap_or_default(), control).unwrap_or(spec.default) +} + +fn settings(h: &Headless) -> lightcraft_develop::DevelopSettings { + let photo = h.app.session.active().expect("an active photo"); + (*h.app.session.develop_of(photo).unwrap_or_default()).clone() +} + +/// Begin a drag on Exposure and move it, as a pointer drag does. +fn drag_to_15(h: &mut Headless) { + drag(h, SliderOut { drag_started: true, ..Default::default() }); + drag(h, SliderOut { value: Some(1.5), ..Default::default() }); + assert!(h.app.session.interaction.as_ref().is_some_and(|i| i.label == "Exposure"), "the drag owns an interaction"); + assert!(h.app.ui.dragging_interaction.is_some(), "and the slider knows it began it"); +} + +/// A drag that ends the way a drag can — abandoned, or released — cancels or commits the interaction it began: +/// the settings are the ones from before it, or its own value, in one undo step. +#[test] +fn a_drag_ends_the_interaction_it_began() { + for abandoned in [true, false] { + let mut h = panel(); + let before = settings(&h); + let undo = h.app.session.undo.len(); + drag_to_15(&mut h); + let out = + if abandoned { SliderOut { cancelled: true, ..Default::default() } } else { SliderOut { drag_stopped: true, ..Default::default() } }; + drag(&mut h, out); + assert!(h.app.session.interaction.is_none(), "abandoned={abandoned}: the drag's interaction is closed"); + assert!( + h.app.ui.dragging_control.is_none() && h.app.ui.dragging_interaction.is_none(), + "abandoned={abandoned}: and the slider lets go of it" + ); + if abandoned { + assert_eq!(settings(&h), before, "an abandoned drag restores the settings from before it"); + assert_eq!(h.app.session.undo.len(), undo, "and records no undo step"); + } else { + assert_eq!(value(&h, "light.exposure"), 1.5, "a released drag lands where it stopped"); + assert_eq!(h.app.session.undo.len() - undo, 1, "as one undo step"); + } + } +} + +/// An automation client can begin its own interaction in the middle of a drag (`develop.beginInteraction` ends the +/// drag's). The drag has no claim on that one: its cancel leaves the client's settings and live interaction as they +/// are, and its release does not commit the client's edit for it. +#[test] +fn a_drag_leaves_the_interaction_another_client_began() { + for ending in ["cancel", "release"] { + let mut h = panel(); + drag_to_15(&mut h); + // the automation command: its own interaction, and one live edit of the Contrast + h.app.run("develop.beginInteraction", json!({"label": "Contrast"})).expect("the client's interaction"); + h.app.run("develop.set", json!({"control": "light.contrast", "value": 25.0})).expect("the client's edit"); + assert_eq!(value(&h, "light.exposure"), 1.5, "the client's begin committed the drag"); + let undo = h.app.session.undo.len(); + // the slider is ended after all: the control's id still names it, the interaction is not its own + let out = if ending == "cancel" { + SliderOut { cancelled: true, ..Default::default() } + } else { + SliderOut { drag_stopped: true, ..Default::default() } + }; + drag(&mut h, out); + assert_eq!(value(&h, "light.contrast"), 25.0, "{ending}: the client's settings stand"); + assert!( + h.app.session.interaction.as_ref().is_some_and(|i| i.label == "Contrast"), + "{ending}: the client's interaction is still open: {:?}", + h.app.session.interaction + ); + assert_eq!(h.app.session.undo.len(), undo, "{ending}: and nothing was committed"); + assert!(h.app.ui.dragging_control.is_none() && h.app.ui.dragging_interaction.is_none(), "{ending}: the slider lets go of it"); + // the client's own commit still works, and still is the one undo step + h.app.run("develop.endInteraction", json!({})).expect("the client's commit"); + assert!(h.app.session.interaction.is_none()); + assert_eq!(h.app.session.undo.len() - undo, 1); + assert_eq!(value(&h, "light.contrast"), 25.0); + } +} + +/// The owner identifies one engine interaction, not a control: the interaction a drag began is its own until another +/// begins, and no two interactions share an owner. +#[test] +fn an_owner_names_one_interaction() { + let mut h = panel(); + drag_to_15(&mut h); + let owner = h.app.ui.dragging_interaction.clone().expect("the drag's owner"); + let live = h.app.session.interaction.clone().expect("the drag's interaction"); + assert!(owner.owns(&live) && owner.is_live(&h.app.session)); + // another client's interaction is not the owner's, though it edits the same photo + h.app.run("develop.beginInteraction", json!({"label": "Exposure"})).expect("another interaction"); + let other = h.app.session.interaction.clone().expect("the other interaction"); + assert!(!owner.owns(&other), "a later interaction with the same label and photo is another one"); + assert!(!owner.is_live(&h.app.session)); + assert!(InteractionOwner::of(&other).owns(&other)); +}