Skip to content

Commit

Permalink
Change DeviceId to Option<DeviceId>
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Aug 13, 2024
1 parent c69047e commit 469f6c3
Show file tree
Hide file tree
Showing 28 changed files with 208 additions and 227 deletions.
2 changes: 1 addition & 1 deletion examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ impl ApplicationHandler for Application {
fn device_event(
&mut self,
_event_loop: &dyn ActiveEventLoop,
device_id: DeviceId,
device_id: Option<DeviceId>,
event: DeviceEvent,
) {
info!("Device {device_id:?} event: {event:?}");
Expand Down
6 changes: 3 additions & 3 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub trait ApplicationHandler {
fn device_event(
&mut self,
event_loop: &dyn ActiveEventLoop,
device_id: DeviceId,
device_id: Option<DeviceId>,
event: DeviceEvent,
) {
let _ = (event_loop, device_id, event);
Expand Down Expand Up @@ -363,7 +363,7 @@ impl<A: ?Sized + ApplicationHandler> ApplicationHandler for &mut A {
fn device_event(
&mut self,
event_loop: &dyn ActiveEventLoop,
device_id: DeviceId,
device_id: Option<DeviceId>,
event: DeviceEvent,
) {
(**self).device_event(event_loop, device_id, event);
Expand Down Expand Up @@ -431,7 +431,7 @@ impl<A: ?Sized + ApplicationHandler> ApplicationHandler for Box<A> {
fn device_event(
&mut self,
event_loop: &dyn ActiveEventLoop,
device_id: DeviceId,
device_id: Option<DeviceId>,
event: DeviceEvent,
) {
(**self).device_event(event_loop, device_id, event);
Expand Down
2 changes: 2 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ changelog entry.
application delegate yourself.
- On iOS, no longer act as-if the application successfully open all URLs. Override
`application:didFinishLaunchingWithOptions:` and provide the desired behaviour yourself.
- Every event carrying a `DeviceId` now uses `Option<DeviceId>` instead. This signifies that the
device can't be uniquely differentiated.

### Removed

Expand Down
62 changes: 34 additions & 28 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(crate) enum Event {
///
/// [`ApplicationHandler::device_event`]: crate::application::ApplicationHandler::device_event
#[allow(clippy::enum_variant_names)]
DeviceEvent { device_id: DeviceId, event: DeviceEvent },
DeviceEvent { device_id: Option<DeviceId>, event: DeviceEvent },

/// See [`ApplicationHandler::suspended`] for details.
///
Expand Down Expand Up @@ -194,7 +194,7 @@ pub enum WindowEvent {
/// numpad keys act as if NumLock wasn't active. When this is used, the OS sends fake key
/// events which are not marked as `is_synthetic`.
KeyboardInput {
device_id: DeviceId,
device_id: Option<DeviceId>,
event: KeyEvent,

/// If `true`, the event was generated synthetically by winit
Expand Down Expand Up @@ -231,7 +231,7 @@ pub enum WindowEvent {
/// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
CursorMoved {
device_id: DeviceId,
device_id: Option<DeviceId>,

/// (x,y) coords 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 transformed by
Expand All @@ -249,7 +249,7 @@ pub enum WindowEvent {
/// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
/// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
CursorEntered { device_id: DeviceId },
CursorEntered { device_id: Option<DeviceId> },

/// The cursor has left the window.
///
Expand All @@ -260,13 +260,13 @@ pub enum WindowEvent {
/// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
/// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
CursorLeft { device_id: DeviceId },
CursorLeft { device_id: Option<DeviceId> },

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

/// An mouse button press has been received.
MouseInput { device_id: DeviceId, state: ElementState, button: MouseButton },
MouseInput { device_id: Option<DeviceId>, state: ElementState, button: MouseButton },

/// Two-finger pinch gesture, often used for magnification.
///
Expand All @@ -275,7 +275,7 @@ pub enum WindowEvent {
/// - Only available on **macOS** and **iOS**.
/// - On iOS, not recognized by default. It must be enabled when needed.
PinchGesture {
device_id: DeviceId,
device_id: Option<DeviceId>,
/// Positive values indicate magnification (zooming in) and negative
/// values indicate shrinking (zooming out).
///
Expand All @@ -291,7 +291,7 @@ pub enum WindowEvent {
/// - Only available on **iOS**.
/// - On iOS, not recognized by default. It must be enabled when needed.
PanGesture {
device_id: DeviceId,
device_id: Option<DeviceId>,
/// Change in pixels of pan gesture from last update.
delta: PhysicalPosition<f32>,
phase: TouchPhase,
Expand All @@ -315,7 +315,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: DeviceId },
DoubleTapGesture { device_id: Option<DeviceId> },

/// Two-finger rotation gesture.
///
Expand All @@ -327,7 +327,7 @@ pub enum WindowEvent {
/// - Only available on **macOS** and **iOS**.
/// - On iOS, not recognized by default. It must be enabled when needed.
RotationGesture {
device_id: DeviceId,
device_id: Option<DeviceId>,
/// change in rotation in degrees
delta: f32,
phase: TouchPhase,
Expand All @@ -338,7 +338,7 @@ pub enum WindowEvent {
/// At the moment, only supported on Apple forcetouch-capable macbooks.
/// The parameters are: pressure level (value between 0 and 1 representing how hard the
/// touchpad is being pressed) and stage (integer representing the click level).
TouchpadPressure { device_id: DeviceId, pressure: f32, stage: i64 },
TouchpadPressure { device_id: Option<DeviceId>, pressure: f32, stage: i64 },

/// Touch event has been received
///
Expand Down Expand Up @@ -841,7 +841,7 @@ pub enum TouchPhase {
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Touch {
pub device_id: DeviceId,
pub device_id: Option<DeviceId>,
pub phase: TouchPhase,
pub location: PhysicalPosition<f64>,
/// Describes how hard the screen was pressed. May be `None` if the platform
Expand Down Expand Up @@ -1023,7 +1023,6 @@ mod tests {
($closure:expr) => {{
#[allow(unused_mut)]
let mut x = $closure;
let did = event::DeviceId::dummy();
let fid = event::FingerId::dummy();

#[allow(deprecated)]
Expand Down Expand Up @@ -1053,39 +1052,39 @@ mod tests {
with_window_event(HoveredFile("x.txt".into()));
with_window_event(HoveredFileCancelled);
with_window_event(Ime(Enabled));
with_window_event(CursorMoved { device_id: did, position: (0, 0).into() });
with_window_event(CursorMoved { device_id: None, position: (0, 0).into() });
with_window_event(ModifiersChanged(event::Modifiers::default()));
with_window_event(CursorEntered { device_id: did });
with_window_event(CursorLeft { device_id: did });
with_window_event(CursorEntered { device_id: None });
with_window_event(CursorLeft { device_id: None });
with_window_event(MouseWheel {
device_id: did,
device_id: None,
delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
phase: event::TouchPhase::Started,
});
with_window_event(MouseInput {
device_id: did,
device_id: None,
state: event::ElementState::Pressed,
button: event::MouseButton::Other(0),
});
with_window_event(PinchGesture {
device_id: did,
device_id: None,
delta: 0.0,
phase: event::TouchPhase::Started,
});
with_window_event(DoubleTapGesture { device_id: did });
with_window_event(DoubleTapGesture { device_id: None });
with_window_event(RotationGesture {
device_id: did,
device_id: None,
delta: 0.0,
phase: event::TouchPhase::Started,
});
with_window_event(PanGesture {
device_id: did,
device_id: None,
delta: PhysicalPosition::<f32>::new(0.0, 0.0),
phase: event::TouchPhase::Started,
});
with_window_event(TouchpadPressure { device_id: did, pressure: 0.0, stage: 0 });
with_window_event(TouchpadPressure { device_id: None, pressure: 0.0, stage: 0 });
with_window_event(Touch(event::Touch {
device_id: did,
device_id: None,
phase: event::TouchPhase::Started,
location: (0.0, 0.0).into(),
finger_id: fid,
Expand All @@ -1100,7 +1099,7 @@ mod tests {
use event::DeviceEvent::*;

let with_device_event =
|dev_ev| x(event::Event::DeviceEvent { device_id: did, event: dev_ev });
|dev_ev| x(event::Event::DeviceEvent { device_id: None, event: dev_ev });

with_device_event(MouseMotion { delta: (0.0, 0.0).into() });
with_device_event(MouseWheel {
Expand Down Expand Up @@ -1146,20 +1145,27 @@ mod tests {
let _ = event::StartCause::Init.clone();

let did = crate::event::DeviceId::dummy().clone();
let fid = crate::event::FingerId::dummy().clone();
HashSet::new().insert(did);
let mut set = [did, did, did];
set.sort_unstable();
let mut set2 = BTreeSet::new();
set2.insert(did);
set2.insert(did);

let fid = crate::event::FingerId::dummy().clone();
HashSet::new().insert(fid);
let mut set = [fid, fid, fid];
set.sort_unstable();
let mut set2 = BTreeSet::new();
set2.insert(fid);
set2.insert(fid);

HashSet::new().insert(event::TouchPhase::Started.clone());
HashSet::new().insert(event::MouseButton::Left.clone());
HashSet::new().insert(event::Ime::Enabled);

let _ = event::Touch {
device_id: did,
device_id: None,
phase: event::TouchPhase::Started,
location: (0.0, 0.0).into(),
finger_id: fid,
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl EventLoop {
match event {
InputEvent::MotionEvent(motion_event) => {
let window_id = window::WindowId(WindowId);
let device_id = event::DeviceId(DeviceId(motion_event.device_id()));
let device_id = Some(event::DeviceId(DeviceId(motion_event.device_id())));

let phase = match motion_event.action() {
MotionAction::Down | MotionAction::PointerDown => {
Expand Down Expand Up @@ -389,7 +389,7 @@ impl EventLoop {

let window_id = window::WindowId(WindowId);
let event = event::WindowEvent::KeyboardInput {
device_id: event::DeviceId(DeviceId(key.device_id())),
device_id: Some(event::DeviceId(DeviceId(key.device_id()))),
event: event::KeyEvent {
state,
physical_key: keycodes::to_physical_key(keycode),
Expand Down
7 changes: 3 additions & 4 deletions src/platform_impl/apple/appkit/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType, N
use objc2_foundation::{MainThreadMarker, NSObject};

use super::app_state::AppState;
use super::DEVICE_ID;
use crate::event::{DeviceEvent, ElementState};

declare_class!(
Expand Down Expand Up @@ -61,7 +60,7 @@ fn maybe_dispatch_device_event(app_state: &Rc<AppState>, event: &NSEvent) {

if delta_x != 0.0 || delta_y != 0.0 {
app_state.maybe_queue_with_handler(move |app, event_loop| {
app.device_event(event_loop, DEVICE_ID, DeviceEvent::MouseMotion {
app.device_event(event_loop, None, DeviceEvent::MouseMotion {
delta: (delta_x, delta_y),
});
});
Expand All @@ -70,7 +69,7 @@ fn maybe_dispatch_device_event(app_state: &Rc<AppState>, event: &NSEvent) {
NSEventType::LeftMouseDown | NSEventType::RightMouseDown | NSEventType::OtherMouseDown => {
let button = unsafe { event.buttonNumber() } as u32;
app_state.maybe_queue_with_handler(move |app, event_loop| {
app.device_event(event_loop, DEVICE_ID, DeviceEvent::Button {
app.device_event(event_loop, None, DeviceEvent::Button {
button,
state: ElementState::Pressed,
});
Expand All @@ -79,7 +78,7 @@ fn maybe_dispatch_device_event(app_state: &Rc<AppState>, event: &NSEvent) {
NSEventType::LeftMouseUp | NSEventType::RightMouseUp | NSEventType::OtherMouseUp => {
let button = unsafe { event.buttonNumber() } as u32;
app_state.maybe_queue_with_handler(move |app, event_loop| {
app.device_event(event_loop, DEVICE_ID, DeviceEvent::Button {
app.device_event(event_loop, None, DeviceEvent::Button {
button,
state: ElementState::Released,
});
Expand Down
4 changes: 0 additions & 4 deletions src/platform_impl/apple/appkit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub(crate) use self::monitor::{MonitorHandle, VideoModeHandle};
pub(crate) use self::window::{Window, WindowId};
pub(crate) use self::window_delegate::PlatformSpecificWindowAttributes;
pub(crate) use crate::cursor::OnlyCursorImageSource as PlatformCustomCursorSource;
use crate::event::DeviceId as RootDeviceId;
pub(crate) use crate::icon::NoIcon as PlatformIcon;
pub(crate) use crate::platform_impl::Fullscreen;

Expand All @@ -41,9 +40,6 @@ impl DeviceId {
}
}

// Constant device ID; to be removed when if backend is updated to report real device IDs.
pub(crate) const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId);

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FingerId;

Expand Down
Loading

0 comments on commit 469f6c3

Please sign in to comment.