diff --git a/crates/ui-egui/src/dialogs/tests_track_cube.rs b/crates/ui-egui/src/dialogs/tests_track_cube.rs index 3b2de38452..39c0c3a934 100644 --- a/crates/ui-egui/src/dialogs/tests_track_cube.rs +++ b/crates/ui-egui/src/dialogs/tests_track_cube.rs @@ -49,7 +49,7 @@ fn angles_stay_in_the_fields_range_to_a_hundredth() { } fn drag(grip: Grip, angles: [f64; 3], from: Pos2, to: Pos2, shift: bool) -> [f64; 3] { - let mut g = CubeDrag { grip, matrix: rotation(angles), origin: from, last: from, lock: None, fields: Default::default() }; + let mut g = CubeDrag { grip, matrix: rotation(angles), origin: from, last: from, lock: None, fields: Default::default(), pass: 0 }; // In five steps, as a pointer moves. let mut near = angles; for k in 1..=5 { @@ -108,6 +108,33 @@ fn a_face_drag_turns_the_front_toward_the_pointer_and_shift_keeps_one_axis() { assert!(free[0].abs() > 1.0, "without Shift a slanted drag also tilts: {free:?}"); } +/// A gesture ends when the cube stops being drawn during it (its dialog closed, or the Rotation tab +/// was left), so Escape is not held back for a cube that is no longer there. +#[test] +fn a_drag_ends_when_the_cube_is_no_longer_drawn() { + let ctx = egui::Context::default(); + let run = |draw: &mut dyn FnMut(&mut egui::Ui)| ctx.run_ui(Default::default(), draw).textures_delta.clear(); + run(&mut |ui| { + let pass = ui.ctx().cumulative_pass_nr(); + let drag = CubeDrag { + grip: Grip::Face, + matrix: rotation([0.0; 3]), + origin: Pos2::ZERO, + last: Pos2::ZERO, + lock: None, + fields: Default::default(), + pass, + }; + ui.data_mut(|m| m.insert_temp(cube_drag_id(), drag)); + assert!(cube_dragging(ui.ctx())); + }); + assert!(cube_dragging(&ctx), "the next pass still sees the gesture"); + for _ in 0..2 { + run(&mut |_| {}); + } + assert!(!cube_dragging(&ctx), "two passes without the cube end the gesture"); +} + /// The centre is on the front face, the ring's band on the ring, and an edge's middle on that edge. #[test] fn grips_follow_what_is_under_the_pointer() { diff --git a/crates/ui-egui/src/dialogs/three_d_classic.rs b/crates/ui-egui/src/dialogs/three_d_classic.rs index 32c4fbf764..38f9467c00 100644 --- a/crates/ui-egui/src/dialogs/three_d_classic.rs +++ b/crates/ui-egui/src/dialogs/three_d_classic.rs @@ -273,6 +273,16 @@ struct CubeDrag { lock: Option, /// The settings before the gesture, which Escape puts back. fields: serde_json::Map, + /// The last pass that drew the cube during the gesture. + pass: u64, +} + +impl CubeDrag { + /// Was the cube drawn in this pass or the one before? When it was not (its dialog closed, or + /// the Rotation tab or the panel was left during the drag), the gesture is over. + fn live(&self, pass: u64) -> bool { + self.pass.saturating_add(1) >= pass + } } fn cube_drag_id() -> Id { @@ -281,7 +291,8 @@ fn cube_drag_id() -> Id { /// Is a track cube gesture in progress? pub(super) fn cube_dragging(ctx: &egui::Context) -> bool { - ctx.data(|m| m.get_temp::(cube_drag_id())).is_some() + let pass = ctx.cumulative_pass_nr(); + ctx.data(|m| m.get_temp::(cube_drag_id())).is_some_and(|g| g.live(pass)) } /// The rotation after the pointer moves from `drag.last` to `p` on a cube about `center`. @@ -361,24 +372,33 @@ pub(super) fn track_cube(ui: &mut egui::Ui, d: &mut Dialog) { // A value being typed ends first; the cube then takes the keyboard focus below. ui.ctx().memory_mut(|m| m.stop_text_input()); let matrix = rotation(now); - let drag = CubeDrag { grip: grip_at(matrix, center, origin), matrix, origin, last: origin, lock: None, fields: d.fields.clone() }; + let pass = ui.ctx().cumulative_pass_nr(); + let drag = CubeDrag { grip: grip_at(matrix, center, origin), matrix, origin, last: origin, lock: None, fields: d.fields.clone(), pass }; ui.data_mut(|m| m.insert_temp(cube_drag_id(), drag)); } if enabled && (resp.drag_started() || resp.clicked()) { resp.request_focus(); } + let pass = ui.ctx().cumulative_pass_nr(); let mut drag = ui.data(|m| m.get_temp::(cube_drag_id())); + if drag.as_ref().is_some_and(|g| !g.live(pass)) { + ui.data_mut(|m| m.remove::(cube_drag_id())); + drag = None; + } if let Some(current) = &mut drag { + current.pass = pass; if ui.input_mut(|i| i.consume_key(i.modifiers, egui::Key::Escape)) { d.fields = current.fields.clone(); d.fields.remove(super::form::PREVIEWED); ui.data_mut(|m| m.remove::(cube_drag_id())); drag = None; - } else if let Some(p) = resp.interact_pointer_pos() { - let shift = ui.input(|i| i.modifiers.shift); - current.matrix = drag_turn(current, p, center, shift); - current.last = p; - next = Some(angles_of(current.matrix, now)); + } else { + if let Some(p) = resp.interact_pointer_pos() { + let shift = ui.input(|i| i.modifiers.shift); + current.matrix = drag_turn(current, p, center, shift); + current.last = p; + next = Some(angles_of(current.matrix, now)); + } ui.data_mut(|m| m.insert_temp(cube_drag_id(), current.clone())); } }