Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

3D mouse: only the window with focus moves (#2077) (#2212)

The Nomad4c30d944f541
diff --git a/apps/solidcraft/src/space_mouse.rs b/apps/solidcraft/src/space_mouse.rs
index 00568cf085..96c0d1ab31 100644
--- a/apps/solidcraft/src/space_mouse.rs
+++ b/apps/solidcraft/src/space_mouse.rs
@@ -625,7 +625,10 @@ mod tests {
wheel: WheelGuard::default(),
started: now,
};
- mouse.tick_at(&mut app, &Context::default(), now);
+ // A window that has run a frame with focus, as the app's always has by now.
+ let ctx = Context::default();
+ let _ = ctx.run_ui(egui::RawInput::default(), |_| {});
+ mouse.tick_at(&mut app, &ctx, now);
let DeviceMotion { motion, seconds } = app.viewport.device_motion.expect("the frame hands the page its motion");
assert_eq!(motion, Motion { translation: [0.0; 3], rotation: [0.0, 0.0, -1.0] });
assert!(seconds > 0.0);
diff --git a/crates/ui-egui/src/space_mouse.rs b/crates/ui-egui/src/space_mouse.rs
index 4b4ef6fed3..f6663b3698 100644
--- a/crates/ui-egui/src/space_mouse.rs
+++ b/crates/ui-egui/src/space_mouse.rs
@@ -139,7 +139,8 @@ pub struct DeviceMotion {
/// picker is open. Returns whether there was motion to hand over; it then asks for the next frame
/// itself.
pub fn navigate_frame(app: &mut SolveApp, ctx: &egui::Context, mapping: DriverMapping, axes: Axes, seconds: f64) -> bool {
- if !app.preferences.space_mouse_enabled || app.home.open || app.file_picker_busy() || axes == Axes::default() {
+ // Only the window with focus: with two windows open, the cap moves the one being used, not both.
+ if !app.preferences.space_mouse_enabled || app.home.open || app.file_picker_busy() || axes == Axes::default() || !ctx.input(|i| i.focused) {
return false;
}
let speed = f64::from(app.preferences.space_mouse_speed);
@@ -177,6 +178,15 @@ mod tests {
crate::navigation::device_camera(cam, motion, seconds, height, !sketch, reverse_zoom);
}
+ /// A window that has run a frame, with or without focus. A context that has never run one
+ /// reads as unfocused, so the tests run one first, as the app always has.
+ fn window(focused: bool) -> egui::Context {
+ let ctx = egui::Context::default();
+ let mut output = ctx.run_ui(egui::RawInput { focused, ..Default::default() }, |_| {});
+ output.textures_delta.clear();
+ ctx
+ }
+
/// One frame: the 3D mouse hands its motion over, and the modelling viewport applies it.
fn frame(app: &mut SolveApp, ctx: &egui::Context, mapping: DriverMapping, axes: Axes, seconds: f64) -> bool {
let handed = navigate_frame(app, ctx, mapping, axes, seconds);
@@ -339,7 +349,7 @@ mod tests {
#[test]
fn a_frame_moves_the_view_and_keeps_the_next_frame_coming() {
- let ctx = egui::Context::default();
+ let ctx = window(true);
let mut app = app();
let before = app.cam;
assert!(frame(&mut app, &ctx, DriverMapping::default(), PUSH, 0.02));
@@ -351,9 +361,20 @@ mod tests {
assert_eq!(app.cam.distance, still.distance);
}
+ #[test]
+ fn only_the_focused_window_moves() {
+ // Two windows side by side (#2077): the cap moves the one with focus and leaves the other.
+ let mut app = app();
+ let before = app.cam;
+ assert!(!frame(&mut app, &window(false), DriverMapping::default(), PUSH, 0.02));
+ assert_eq!(app.cam.distance, before.distance);
+ assert!(frame(&mut app, &window(true), DriverMapping::default(), PUSH, 0.02));
+ assert!(app.cam.distance < before.distance);
+ }
+
#[test]
fn an_open_file_picker_holds_the_view_until_it_closes() {
- let ctx = egui::Context::default();
+ let ctx = window(true);
let mut app = app();
let slot: crate::picker_test_support::Slot = Default::default();
crate::picker_test_support::open(&mut app, &slot);
@@ -372,7 +393,7 @@ mod tests {
#[test]
fn a_long_gap_between_frames_moves_as_far_as_a_short_one() {
- let ctx = egui::Context::default();
+ let ctx = window(true);
let (mut short, mut long) = (app(), app());
frame(&mut short, &ctx, DriverMapping::default(), PUSH, 0.05);
frame(&mut long, &ctx, DriverMapping::default(), PUSH, 30.0);
@@ -387,7 +408,7 @@ mod tests {
#[test]
fn navigation_off_the_start_page_and_a_hostile_speed_leave_the_view_alone() {
- let ctx = egui::Context::default();
+ let ctx = window(true);
let mut off = app();
off.preferences.space_mouse_enabled = false;
let mut home = app();