Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dummy() from WindowId and DeviceId and use Option<DeviceId> #3864

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
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
6 changes: 6 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ changelog entry.
- Add basic iOS IME support. The soft keyboard can now be shown using `Window::set_ime_allowed`.
- On macOS, add `WindowExtMacOS::set_borderless_game` and `WindowAttributesExtMacOS::with_borderless_game`
to fully disable the menu bar and dock in Borderless Fullscreen as commonly done in games.
- Add `WindowId::into_raw()` and `from_raw()`.

### Changed

Expand Down Expand Up @@ -124,6 +125,8 @@ changelog entry.
- `Window::set_max_inner_size` to `set_max_surface_size`.

To migrate, you can probably just replace all instances of `inner_size` with `surface_size` in your codebase.
- Every event carrying a `DeviceId` now uses `Option<DeviceId>` instead. A `None` value signifies that the
device can't be uniquely differentiated.

### Removed

Expand All @@ -150,6 +153,9 @@ changelog entry.
- Remove `MonitorHandle::size()` and `refresh_rate_millihertz()` in favor of
`MonitorHandle::current_video_mode()`.
- On Android, remove all `MonitorHandle` support instead of emitting false data.
- Remove `impl From<u64> for WindowId` and `impl From<WindowId> for u64`. Replaced with
`WindowId::into_raw()` and `from_raw()`.
- Remove `dummy()` from `WindowId` and `DeviceId`.

### Fixed

Expand Down
93 changes: 33 additions & 60 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 @@ -199,7 +199,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 @@ -236,7 +236,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 @@ -254,7 +254,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 @@ -265,13 +265,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 @@ -280,7 +280,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 @@ -296,7 +296,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 @@ -320,7 +320,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 @@ -332,7 +332,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 @@ -343,7 +343,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 @@ -439,25 +439,6 @@ pub enum WindowEvent {
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId(pub(crate) platform_impl::DeviceId);

impl Default for DeviceId {
fn default() -> Self {
Self::dummy()
}
}

impl DeviceId {
/// Returns a dummy id, useful for unit testing.
///
/// # Notes
///
/// The only guarantee made about the return value of this function is that
/// it will always be equal to itself and to future values returned by this function.
/// No other guarantees are made. This may be equal to a real `DeviceId`.
pub const fn dummy() -> Self {
DeviceId(platform_impl::DeviceId::dummy())
}
}

/// Identifier of a finger in a touch event.
///
/// Whenever a touch event is received it contains a `FingerId` which uniquely identifies the finger
Expand All @@ -466,14 +447,8 @@ impl DeviceId {
pub struct FingerId(pub(crate) platform_impl::FingerId);

impl FingerId {
/// Returns a dummy id, useful for unit testing.
///
/// # Notes
///
/// The only guarantee made about the return value of this function is that
/// it will always be equal to itself and to future values returned by this function.
/// No other guarantees are made. This may be equal to a real `FingerId`.
pub const fn dummy() -> Self {
#[cfg(test)]
pub(crate) const fn dummy() -> Self {
FingerId(platform_impl::FingerId::dummy())
}
}
Expand Down Expand Up @@ -864,7 +839,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 @@ -1045,7 +1020,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 All @@ -1056,7 +1030,7 @@ mod tests {
use crate::window::WindowId;

// Mainline events.
let wid = WindowId::dummy();
let wid = WindowId::from_raw(0);
x(NewEvents(event::StartCause::Init));
x(AboutToWait);
x(LoopExiting);
Expand All @@ -1075,39 +1049,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 @@ -1122,7 +1096,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 @@ -1167,21 +1141,20 @@ 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];
HashSet::new().insert(fid);
let mut set = [fid, fid, fid];
set.sort_unstable();
let mut set2 = BTreeSet::new();
set2.insert(did);
set2.insert(did);
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
23 changes: 5 additions & 18 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,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 @@ -388,7 +388,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 Expand Up @@ -668,36 +668,23 @@ impl OwnedDisplayHandle {
pub(crate) struct WindowId;

impl WindowId {
pub const fn dummy() -> Self {
WindowId
}
}

impl From<WindowId> for u64 {
fn from(_: WindowId) -> Self {
pub const fn into_raw(self) -> u64 {
0
}
}

impl From<u64> for WindowId {
fn from(_: u64) -> Self {
pub const fn from_raw(_id: u64) -> Self {
Self
}
}

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

impl DeviceId {
pub const fn dummy() -> Self {
DeviceId(0)
}
}

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

impl FingerId {
#[cfg(test)]
pub const fn dummy() -> Self {
FingerId(0)
}
Expand Down
Loading
Loading