Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

A held pointer leaves the window on its release, so a press isn't dropped when the mouse is outside

shinyobjectz6b12b58ca527
diff --git a/engine/page/host/src/inject.rs b/engine/page/host/src/inject.rs
index 93ba6b36..54b00ebc 100644
--- a/engine/page/host/src/inject.rs
+++ b/engine/page/host/src/inject.rs
@@ -392,7 +392,13 @@ pub fn resize(world: &mut World, win: Entity, w: f32, h: f32) {
/// points, as AppKit's scrollingDeltaY hands them, natural scrolling already in them: winit makes them physical pixels,
/// which ScrollArea and onWheel would move twice as far on a 2x screen. And a press's click count is kept (Clicks), for
/// onPointerDown's `detail`: the header's double click zooms the window.
-pub fn window_pointer(mut evs: MessageReader<WindowEvent>, target: Res<Target>, windows: Query<&Window>, mut out: MessageWriter<PointerInput>, mut pending: ResMut<Pending>, mut clicks: ResMut<Clicks>, mut at: Local<Option<Vec2>>) {
+///
+/// A pointer with a button down leaves the window on its release, as Chrome's does: AppKit reports the mouse itself
+/// outside the window between an assistive client's press and its release, and the release was dropped with the
+/// pointer's place (a press on a card opened nothing, 11-24).
+pub fn window_pointer(mut evs: MessageReader<WindowEvent>, target: Res<Target>, windows: Query<&Window>, mut out: MessageWriter<PointerInput>, mut pending: ResMut<Pending>, mut clicks: ResMut<Clicks>, mut at: Local<Option<Vec2>>, mut down: Local<Vec<MouseButton>>, mut gone: Local<bool>) {
+ // off every camera's viewport, so hover ends (picking keeps a pointer's last place when it leaves)
+ const OFF: Vec2 = Vec2::splat(-1.0e6);
let mut put = |position: Vec2, action: PointerAction| {
out.write(PointerInput::new(PointerId::Mouse, Location { target: target.norm.clone(), position }, action));
};
@@ -401,6 +407,9 @@ pub fn window_pointer(mut evs: MessageReader<WindowEvent>, target: Res<Target>,
WindowEvent::CursorMoved(e) => {
let delta = at.map(|a| e.position - a).unwrap_or(Vec2::ZERO);
*at = Some(e.position);
+ *gone = false;
+ // where an injected press is (`at_of`): it moves there first unless it's the pointer's place now
+ pending.cursor = *at;
put(e.position, PointerAction::Move { delta });
}
WindowEvent::MouseButtonInput(e) => {
@@ -418,9 +427,20 @@ pub fn window_pointer(mut evs: MessageReader<WindowEvent>, target: Res<Target>,
} else if b == PointerButton::Primary {
pending.clicks = clicks.press(p);
}
+ if !down.contains(&e.button) {
+ down.push(e.button);
+ }
put(p, PointerAction::Press(b));
}
- ButtonState::Released => put(p, PointerAction::Release(b)),
+ ButtonState::Released => {
+ down.retain(|d| *d != e.button);
+ put(p, PointerAction::Release(b));
+ if down.is_empty() && std::mem::take(&mut *gone) {
+ *at = None;
+ pending.cursor = None;
+ put(OFF, PointerAction::Move { delta: Vec2::ZERO });
+ }
+ }
}
}
WindowEvent::MouseWheel(e) => {
@@ -428,10 +448,11 @@ pub fn window_pointer(mut evs: MessageReader<WindowEvent>, target: Res<Target>,
let k = if e.unit == MouseScrollUnit::Pixel { windows.get(e.window).map(|w| w.scale_factor()).unwrap_or(1.0) } else { 1.0 };
put(p, PointerAction::Scroll { x: e.x / k, y: e.y / k, unit: e.unit, phase: e.phase });
}
+ WindowEvent::CursorLeft(_) if !down.is_empty() => *gone = true,
WindowEvent::CursorLeft(_) => {
- // off every camera's viewport, so hover ends (picking keeps a pointer's last place when it leaves)
*at = None;
- put(Vec2::splat(-1.0e6), PointerAction::Move { delta: Vec2::ZERO });
+ pending.cursor = None;
+ put(OFF, PointerAction::Move { delta: Vec2::ZERO });
}
_ => {}
}
diff --git a/engine/page/host/tests/pointer.rs b/engine/page/host/tests/pointer.rs
new file mode 100644
index 00000000..e3fd69ac
--- /dev/null
+++ b/engine/page/host/tests/pointer.rs
@@ -0,0 +1,75 @@
+//! The window on screen's pointer (inject::window_pointer): what its events become for picking.
+
+use std::time::Duration;
+
+use bevy::camera::NormalizedRenderTarget;
+use bevy::input::ButtonState;
+use bevy::input::mouse::MouseButtonInput;
+use bevy::picking::pointer::{PointerAction, PointerInput};
+use bevy::prelude::*;
+use bevy::window::{CursorLeft, CursorMoved, WindowEvent};
+use raea_page_host::inject::{Clicks, Pending, Target, window_pointer};
+
+fn app() -> App {
+ let mut app = App::new();
+ let norm = NormalizedRenderTarget::None { width: 100, height: 100 };
+ app.add_message::<WindowEvent>().add_message::<PointerInput>();
+ app.insert_resource(Target { image: Handle::default(), norm, scale: 1.0, windowed: true });
+ app.insert_resource(Clicks::new(Duration::from_millis(500))).init_resource::<Pending>();
+ app.add_systems(Update, window_pointer);
+ app
+}
+
+/// The window's events of one frame, and the pointer actions they became.
+fn frame(app: &mut App, evs: Vec<WindowEvent>) -> Vec<(Vec2, PointerAction)> {
+ for e in evs {
+ app.world_mut().write_message(e);
+ }
+ app.update();
+ app.world_mut().resource_mut::<Messages<PointerInput>>().drain().map(|i| (i.location.position, i.action)).collect()
+}
+
+fn moved(x: f32, y: f32) -> WindowEvent {
+ CursorMoved { window: Entity::PLACEHOLDER, position: Vec2::new(x, y), delta: None }.into()
+}
+
+fn button(state: ButtonState) -> WindowEvent {
+ MouseButtonInput { button: MouseButton::Left, state, window: Entity::PLACEHOLDER }.into()
+}
+
+fn left() -> WindowEvent {
+ CursorLeft { window: Entity::PLACEHOLDER }.into()
+}
+
+fn releases(got: &[(Vec2, PointerAction)]) -> Vec<Vec2> {
+ got.iter().filter(|g| matches!(g.1, PointerAction::Release(_))).map(|g| g.0).collect()
+}
+
+#[test]
+fn a_held_pointer_leaves_on_its_release() {
+ let mut app = app();
+ frame(&mut app, vec![moved(40.0, 30.0), button(ButtonState::Pressed)]);
+ // the mouse itself is reported outside the window while the press is held: nothing yet
+ assert!(frame(&mut app, vec![left()]).is_empty());
+ let got = frame(&mut app, vec![button(ButtonState::Released)]);
+ assert_eq!(releases(&got), vec![Vec2::new(40.0, 30.0)], "the release is where the press was");
+ assert!(matches!(got.last(), Some((p, PointerAction::Move { .. })) if p.x < 0.0), "then it leaves: {got:?}");
+ assert_eq!(app.world().resource::<Pending>().cursor, None, "an injected press at the same place moves there first");
+}
+
+#[test]
+fn a_held_pointer_back_in_the_window_stays() {
+ let mut app = app();
+ frame(&mut app, vec![moved(40.0, 30.0), button(ButtonState::Pressed), left(), moved(50.0, 30.0)]);
+ let got = frame(&mut app, vec![button(ButtonState::Released)]);
+ assert_eq!(got.len(), 1, "its release alone: {got:?}");
+ assert_eq!(app.world().resource::<Pending>().cursor, Some(Vec2::new(50.0, 30.0)));
+}
+
+#[test]
+fn a_pointer_with_no_button_down_leaves_at_once() {
+ let mut app = app();
+ let got = frame(&mut app, vec![moved(40.0, 30.0), left()]);
+ assert!(matches!(got.last(), Some((p, PointerAction::Move { .. })) if p.x < 0.0), "{got:?}");
+ assert_eq!(app.world().resource::<Pending>().cursor, None);
+}