Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion winit-android/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use winit_core::event::{self, DeviceId, FingerId, Force, StartCause, SurfaceSize
use winit_core::event_loop::pump_events::PumpStatus;
use winit_core::event_loop::{
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopProvider,
EventLoopProxy as CoreEventLoopProxy, EventLoopProxyProvider,
EventLoopProxy as CoreEventLoopProxy, EventLoopProxyProvider, HistoricalMoveEvent,
OwnedDisplayHandle as CoreOwnedDisplayHandle,
};
use winit_core::monitor::{Fullscreen, MonitorHandle as CoreMonitorHandle};
Expand Down Expand Up @@ -325,6 +325,7 @@ impl EventLoop {
InputEvent::MotionEvent(motion_event) => {
let device_id = Some(DeviceId::from_raw(motion_event.device_id() as i64));
let action = motion_event.action();
let event_time = Some(Duration::from_nanos(motion_event.event_time() as u64));

let pointers: Option<
Box<dyn Iterator<Item = android_activity::input::Pointer<'_>>>,
Expand Down Expand Up @@ -360,6 +361,7 @@ impl EventLoop {
}
let event = event::WindowEvent::PointerEntered {
device_id,
event_time,
primary,
position,
kind: match tool_type {
Expand All @@ -374,6 +376,7 @@ impl EventLoop {
app.window_event(&self.window_target, GLOBAL_WINDOW, event);
let event = event::WindowEvent::PointerButton {
device_id,
event_time,
primary,
state: event::ElementState::Pressed,
position,
Expand All @@ -390,9 +393,27 @@ impl EventLoop {
app.window_event(&self.window_target, GLOBAL_WINDOW, event);
},
MotionAction::Move => {
let mut history = Vec::with_capacity(pointer.history().len());
for h in pointer.history() {
let event_time = Duration::from_nanos(h.event_time() as u64);
let position = PhysicalPosition { x: h.x() as _, y: h.y() as _ };
let force = Some(Force::Normalized(h.pressure() as f64));
let source = match tool_type {
android_activity::input::ToolType::Finger => {
event::PointerSource::Touch { finger_id, force }
},
// TODO mouse events
android_activity::input::ToolType::Mouse => continue,
_ => event::PointerSource::Unknown,
};

history
.push(HistoricalMoveEvent::new(event_time, position, source));
}
let primary = self.primary_pointer == Some(finger_id);
let event = event::WindowEvent::PointerMoved {
device_id,
event_time,
primary,
position,
source: match tool_type {
Expand All @@ -403,6 +424,7 @@ impl EventLoop {
android_activity::input::ToolType::Mouse => continue,
_ => event::PointerSource::Unknown,
},
history,
};
app.window_event(&self.window_target, GLOBAL_WINDOW, event);
},
Expand All @@ -418,6 +440,7 @@ impl EventLoop {
if let MotionAction::Up | MotionAction::PointerUp = action {
let event = event::WindowEvent::PointerButton {
device_id,
event_time,
primary,
state: event::ElementState::Released,
position,
Expand All @@ -436,6 +459,7 @@ impl EventLoop {

let event = event::WindowEvent::PointerLeft {
device_id,
event_time,
primary,
position: Some(position),
kind: match tool_type {
Expand Down
21 changes: 19 additions & 2 deletions winit-appkit/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;
use std::time::Duration;

use dpi::{LogicalPosition, PhysicalSize};
use objc2::rc::Retained;
Expand Down Expand Up @@ -667,6 +668,7 @@ define_class!(

self.queue_event(WindowEvent::PointerEntered {
device_id: None,
event_time: Some(Duration::from_secs_f64(event.timestamp())),
primary: true,
position,
kind: PointerKind::Mouse,
Expand All @@ -681,6 +683,7 @@ define_class!(

self.queue_event(WindowEvent::PointerLeft {
device_id: None,
event_time: Some(Duration::from_secs_f64(event.timestamp())),
primary: true,
position: Some(position),
kind: PointerKind::Mouse,
Expand Down Expand Up @@ -723,7 +726,12 @@ define_class!(
self.ivars().app_state.maybe_queue_with_handler(move |app, event_loop| {
app.device_event(event_loop, None, DeviceEvent::MouseWheel { delta })
});
self.queue_event(WindowEvent::MouseWheel { device_id: None, delta, phase });
self.queue_event(WindowEvent::MouseWheel {
device_id: None,
event_time: Some(Duration::from_secs_f64(event.timestamp())),
delta,
phase,
});
}

#[unsafe(method(magnifyWithEvent:))]
Expand All @@ -743,6 +751,7 @@ define_class!(

self.queue_event(WindowEvent::PinchGesture {
device_id: None,
event_time: Some(Duration::from_secs_f64(event.timestamp())),
delta: event.magnification(),
phase,
});
Expand All @@ -754,7 +763,10 @@ define_class!(

self.mouse_motion(event);

self.queue_event(WindowEvent::DoubleTapGesture { device_id: None });
self.queue_event(WindowEvent::DoubleTapGesture {
device_id: None,
event_time: Some(Duration::from_secs_f64(event.timestamp())),
});
}

#[unsafe(method(rotateWithEvent:))]
Expand All @@ -774,6 +786,7 @@ define_class!(

self.queue_event(WindowEvent::RotationGesture {
device_id: None,
event_time: Some(Duration::from_secs_f64(event.timestamp())),
delta: event.rotation(),
phase,
});
Expand All @@ -785,6 +798,7 @@ define_class!(

self.queue_event(WindowEvent::TouchpadPressure {
device_id: None,
event_time: Some(Duration::from_secs_f64(event.timestamp())),
pressure: event.pressure(),
stage: event.stage() as i64,
});
Expand Down Expand Up @@ -1199,6 +1213,7 @@ impl WinitView {

self.queue_event(WindowEvent::PointerButton {
device_id: None,
event_time: Some(Duration::from_secs_f64(event.timestamp())),
primary: true,
state: button_state,
position,
Expand Down Expand Up @@ -1227,9 +1242,11 @@ impl WinitView {

self.queue_event(WindowEvent::PointerMoved {
device_id: None,
event_time: Some(Duration::from_secs_f64(event.timestamp())),
primary: true,
position: view_point.to_physical(self.scale_factor()),
source: PointerSource::Mouse,
history: Default::default(),
});
}

Expand Down
38 changes: 32 additions & 6 deletions winit-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cell::LazyCell;
use std::cmp::Ordering;
use std::f64;
use std::sync::{Arc, Mutex, Weak};
use std::time::Duration;

use dpi::{PhysicalPosition, PhysicalSize};
#[cfg(feature = "serde")]
Expand All @@ -12,7 +13,7 @@ use smol_str::SmolStr;
use crate::Instant;
use crate::data_transfer::{DataTransferId, TypedData};
use crate::error::InternalError;
use crate::event_loop::{AsyncRequestSerial, DndAction};
use crate::event_loop::{AsyncRequestSerial, DndAction, HistoricalMoveEvent};
use crate::keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState};
#[cfg(doc)]
use crate::window::Window;
Expand Down Expand Up @@ -224,6 +225,7 @@ pub enum WindowEvent {
/// Should be emitted regardless of window focus.
PointerMoved {
device_id: Option<DeviceId>,
event_time: Option<Duration>,

/// (x,y) coordinates in pixels relative to the top-left corner of the window. Because the
/// range of this data is limited by the display area and it may have been
Expand All @@ -247,13 +249,16 @@ pub enum WindowEvent {
primary: bool,

source: PointerSource,
/// Historical move events between this event and the previous event
history: Vec<HistoricalMoveEvent>,
},

/// The pointer has entered the window.
///
/// Should be emitted regardless of window focus.
PointerEntered {
device_id: Option<DeviceId>,
event_time: Option<Duration>,

/// The position of the pointer when it entered the window.
///
Expand Down Expand Up @@ -281,6 +286,7 @@ pub enum WindowEvent {
/// Should be emitted regardless of window focus.
PointerLeft {
device_id: Option<DeviceId>,
event_time: Option<Duration>,

/// The position of the pointer when it left the window. The position reported can be
/// outside the bounds of the window.
Expand All @@ -305,12 +311,18 @@ pub enum WindowEvent {
},

/// A mouse wheel movement or touchpad scroll occurred.
MouseWheel { device_id: Option<DeviceId>, delta: MouseScrollDelta, phase: TouchPhase },
MouseWheel {
device_id: Option<DeviceId>,
delta: MouseScrollDelta,
phase: TouchPhase,
event_time: Option<Duration>,
},

/// An mouse button press has been received.
PointerButton {
device_id: Option<DeviceId>,
state: ElementState,
event_time: Option<Duration>,

/// The position of the pointer when the button was pressed.
///
Expand Down Expand Up @@ -364,7 +376,7 @@ pub enum WindowEvent {
/// ## Platform-specific
///
/// - Only available on **Wayland**.
HoldGesture { device_id: Option<DeviceId>, phase: TouchPhase },
HoldGesture { device_id: Option<DeviceId>, phase: TouchPhase, event_time: Duration },

/// Two-finger pinch gesture, often used for magnification.
///
Expand All @@ -374,6 +386,7 @@ pub enum WindowEvent {
/// - On iOS, not recognized by default. It must be enabled when needed.
PinchGesture {
device_id: Option<DeviceId>,
event_time: Option<Duration>,
/// Positive values indicate magnification (zooming in) and negative
/// values indicate shrinking (zooming out).
///
Expand All @@ -390,6 +403,7 @@ pub enum WindowEvent {
/// - On iOS, not recognized by default. It must be enabled when needed.
PanGesture {
device_id: Option<DeviceId>,
event_time: Option<Duration>,
/// Change in pixels of pan gesture from last update.
delta: PhysicalPosition<f32>,
phase: TouchPhase,
Expand All @@ -413,7 +427,7 @@ pub enum WindowEvent {
///
/// - Only available on **macOS 10.8** and later, and **iOS**.
/// - On iOS, not recognized by default. It must be enabled when needed.
DoubleTapGesture { device_id: Option<DeviceId> },
DoubleTapGesture { device_id: Option<DeviceId>, event_time: Option<Duration> },

/// Two-finger rotation gesture.
///
Expand All @@ -426,6 +440,7 @@ pub enum WindowEvent {
/// - On iOS, not recognized by default. It must be enabled when needed.
RotationGesture {
device_id: Option<DeviceId>,
event_time: Option<Duration>,
/// change in rotation in degrees
delta: f32,
phase: TouchPhase,
Expand All @@ -439,6 +454,7 @@ pub enum WindowEvent {
/// - **Android / iOS / Wayland / X11 / Windows / Orbital / Web:** Unsupported.
TouchpadPressure {
device_id: Option<DeviceId>,
event_time: Option<Duration>,
/// Value between 0 and 1 representing how hard the touchpad is being
/// pressed.
pressure: f32,
Expand Down Expand Up @@ -1691,30 +1707,36 @@ mod tests {
with_window_event(Ime(Enabled));
with_window_event(PointerMoved {
device_id: None,
event_time: None,
primary: true,
position: (0, 0).into(),
source: PointerSource::Mouse,
history: Default::default(),
});
with_window_event(ModifiersChanged(event::Modifiers::default()));
with_window_event(PointerEntered {
device_id: None,
event_time: None,
primary: true,
position: (0, 0).into(),
kind: PointerKind::Mouse,
});
with_window_event(PointerLeft {
primary: true,
device_id: None,
event_time: None,
position: Some((0, 0).into()),
kind: PointerKind::Mouse,
});
with_window_event(MouseWheel {
device_id: None,
event_time: None,
delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
phase: event::TouchPhase::Started,
});
with_window_event(PointerButton {
device_id: None,
event_time: None,
primary: true,
state: event::ElementState::Pressed,
position: (0, 0).into(),
Expand All @@ -1723,6 +1745,7 @@ mod tests {
});
with_window_event(PointerButton {
device_id: None,
event_time: None,
primary: true,
state: event::ElementState::Released,
position: (0, 0).into(),
Expand All @@ -1734,21 +1757,24 @@ mod tests {
});
with_window_event(PinchGesture {
device_id: None,
event_time: None,
delta: 0.0,
phase: event::TouchPhase::Started,
});
with_window_event(DoubleTapGesture { device_id: None });
with_window_event(DoubleTapGesture { device_id: None, event_time: None, });
with_window_event(RotationGesture {
device_id: None,
event_time: None,
delta: 0.0,
phase: event::TouchPhase::Started,
});
with_window_event(PanGesture {
device_id: None,
event_time: None,
delta: PhysicalPosition::<f32>::new(0.0, 0.0),
phase: event::TouchPhase::Started,
});
with_window_event(TouchpadPressure { device_id: None, pressure: 0.0, stage: 0 });
with_window_event(TouchpadPressure { event_time: None, device_id: None, pressure: 0.0, stage: 0 });
with_window_event(ThemeChanged(crate::window::Theme::Light));
with_window_event(Occluded(true));
}};
Expand Down
Loading
Loading