Skip to content

Commit 32cd1ad

Browse files
authored
api: remove ::dummy from Id types
`::dummy` was used for testing purposes and became redundant after adding e.g. `from_raw` and `into_raw` methods on `Id` types.
1 parent 6e1b9fa commit 32cd1ad

35 files changed

+219
-352
lines changed

examples/window.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl ApplicationHandler for Application {
522522
fn device_event(
523523
&mut self,
524524
_event_loop: &dyn ActiveEventLoop,
525-
device_id: DeviceId,
525+
device_id: Option<DeviceId>,
526526
event: DeviceEvent,
527527
) {
528528
info!("Device {device_id:?} event: {event:?}");

src/application.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub trait ApplicationHandler {
196196
fn device_event(
197197
&mut self,
198198
event_loop: &dyn ActiveEventLoop,
199-
device_id: DeviceId,
199+
device_id: Option<DeviceId>,
200200
event: DeviceEvent,
201201
) {
202202
let _ = (event_loop, device_id, event);
@@ -363,7 +363,7 @@ impl<A: ?Sized + ApplicationHandler> ApplicationHandler for &mut A {
363363
fn device_event(
364364
&mut self,
365365
event_loop: &dyn ActiveEventLoop,
366-
device_id: DeviceId,
366+
device_id: Option<DeviceId>,
367367
event: DeviceEvent,
368368
) {
369369
(**self).device_event(event_loop, device_id, event);
@@ -431,7 +431,7 @@ impl<A: ?Sized + ApplicationHandler> ApplicationHandler for Box<A> {
431431
fn device_event(
432432
&mut self,
433433
event_loop: &dyn ActiveEventLoop,
434-
device_id: DeviceId,
434+
device_id: Option<DeviceId>,
435435
event: DeviceEvent,
436436
) {
437437
(**self).device_event(event_loop, device_id, event);

src/changelog/unreleased.md

+3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ changelog entry.
125125
- `Window::set_max_inner_size` to `set_max_surface_size`.
126126

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

129131
### Removed
130132

@@ -153,6 +155,7 @@ changelog entry.
153155
- On Android, remove all `MonitorHandle` support instead of emitting false data.
154156
- Remove `impl From<u64> for WindowId` and `impl From<WindowId> for u64`. Replaced with
155157
`WindowId::into_raw()` and `from_raw()`.
158+
- Remove `dummy()` from `WindowId` and `DeviceId`.
156159

157160
### Fixed
158161

src/event.rs

+33-60
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(crate) enum Event {
7878
///
7979
/// [`ApplicationHandler::device_event`]: crate::application::ApplicationHandler::device_event
8080
#[allow(clippy::enum_variant_names)]
81-
DeviceEvent { device_id: DeviceId, event: DeviceEvent },
81+
DeviceEvent { device_id: Option<DeviceId>, event: DeviceEvent },
8282

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

205205
/// If `true`, the event was generated synthetically by winit
@@ -236,7 +236,7 @@ pub enum WindowEvent {
236236
/// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
237237
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
238238
CursorMoved {
239-
device_id: DeviceId,
239+
device_id: Option<DeviceId>,
240240

241241
/// (x,y) coords in pixels relative to the top-left corner of the window. Because the range
242242
/// of this data is limited by the display area and it may have been transformed by
@@ -255,7 +255,7 @@ pub enum WindowEvent {
255255
/// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
256256
/// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
257257
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
258-
CursorEntered { device_id: DeviceId },
258+
CursorEntered { device_id: Option<DeviceId> },
259259

260260
/// The cursor has left the window.
261261
///
@@ -266,13 +266,13 @@ pub enum WindowEvent {
266266
/// [`border`]: https://developer.mozilla.org/en-US/docs/Web/CSS/border
267267
/// [`padding`]: https://developer.mozilla.org/en-US/docs/Web/CSS/padding
268268
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
269-
CursorLeft { device_id: DeviceId },
269+
CursorLeft { device_id: Option<DeviceId> },
270270

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

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

277277
/// Two-finger pinch gesture, often used for magnification.
278278
///
@@ -281,7 +281,7 @@ pub enum WindowEvent {
281281
/// - Only available on **macOS** and **iOS**.
282282
/// - On iOS, not recognized by default. It must be enabled when needed.
283283
PinchGesture {
284-
device_id: DeviceId,
284+
device_id: Option<DeviceId>,
285285
/// Positive values indicate magnification (zooming in) and negative
286286
/// values indicate shrinking (zooming out).
287287
///
@@ -297,7 +297,7 @@ pub enum WindowEvent {
297297
/// - Only available on **iOS**.
298298
/// - On iOS, not recognized by default. It must be enabled when needed.
299299
PanGesture {
300-
device_id: DeviceId,
300+
device_id: Option<DeviceId>,
301301
/// Change in pixels of pan gesture from last update.
302302
delta: PhysicalPosition<f32>,
303303
phase: TouchPhase,
@@ -321,7 +321,7 @@ pub enum WindowEvent {
321321
///
322322
/// - Only available on **macOS 10.8** and later, and **iOS**.
323323
/// - On iOS, not recognized by default. It must be enabled when needed.
324-
DoubleTapGesture { device_id: DeviceId },
324+
DoubleTapGesture { device_id: Option<DeviceId> },
325325

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

349349
/// Touch event has been received
350350
///
@@ -440,25 +440,6 @@ pub enum WindowEvent {
440440
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
441441
pub struct DeviceId(pub(crate) platform_impl::DeviceId);
442442

443-
impl Default for DeviceId {
444-
fn default() -> Self {
445-
Self::dummy()
446-
}
447-
}
448-
449-
impl DeviceId {
450-
/// Returns a dummy id, useful for unit testing.
451-
///
452-
/// # Notes
453-
///
454-
/// The only guarantee made about the return value of this function is that
455-
/// it will always be equal to itself and to future values returned by this function.
456-
/// No other guarantees are made. This may be equal to a real `DeviceId`.
457-
pub const fn dummy() -> Self {
458-
DeviceId(platform_impl::DeviceId::dummy())
459-
}
460-
}
461-
462443
/// Identifier of a finger in a touch event.
463444
///
464445
/// Whenever a touch event is received it contains a `FingerId` which uniquely identifies the finger
@@ -467,14 +448,8 @@ impl DeviceId {
467448
pub struct FingerId(pub(crate) platform_impl::FingerId);
468449

469450
impl FingerId {
470-
/// Returns a dummy id, useful for unit testing.
471-
///
472-
/// # Notes
473-
///
474-
/// The only guarantee made about the return value of this function is that
475-
/// it will always be equal to itself and to future values returned by this function.
476-
/// No other guarantees are made. This may be equal to a real `FingerId`.
477-
pub const fn dummy() -> Self {
451+
#[cfg(test)]
452+
pub(crate) const fn dummy() -> Self {
478453
FingerId(platform_impl::FingerId::dummy())
479454
}
480455
}
@@ -865,7 +840,7 @@ pub enum TouchPhase {
865840
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
866841
#[derive(Debug, Clone, Copy, PartialEq)]
867842
pub struct Touch {
868-
pub device_id: DeviceId,
843+
pub device_id: Option<DeviceId>,
869844
pub phase: TouchPhase,
870845
pub location: PhysicalPosition<f64>,
871846
/// Describes how hard the screen was pressed. May be `None` if the platform
@@ -1046,7 +1021,6 @@ mod tests {
10461021
($closure:expr) => {{
10471022
#[allow(unused_mut)]
10481023
let mut x = $closure;
1049-
let did = event::DeviceId::dummy();
10501024
let fid = event::FingerId::dummy();
10511025

10521026
#[allow(deprecated)]
@@ -1057,7 +1031,7 @@ mod tests {
10571031
use crate::window::WindowId;
10581032

10591033
// Mainline events.
1060-
let wid = WindowId::dummy();
1034+
let wid = WindowId::from_raw(0);
10611035
x(NewEvents(event::StartCause::Init));
10621036
x(AboutToWait);
10631037
x(LoopExiting);
@@ -1076,39 +1050,39 @@ mod tests {
10761050
with_window_event(HoveredFile("x.txt".into()));
10771051
with_window_event(HoveredFileCancelled);
10781052
with_window_event(Ime(Enabled));
1079-
with_window_event(CursorMoved { device_id: did, position: (0, 0).into() });
1053+
with_window_event(CursorMoved { device_id: None, position: (0, 0).into() });
10801054
with_window_event(ModifiersChanged(event::Modifiers::default()));
1081-
with_window_event(CursorEntered { device_id: did });
1082-
with_window_event(CursorLeft { device_id: did });
1055+
with_window_event(CursorEntered { device_id: None });
1056+
with_window_event(CursorLeft { device_id: None });
10831057
with_window_event(MouseWheel {
1084-
device_id: did,
1058+
device_id: None,
10851059
delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
10861060
phase: event::TouchPhase::Started,
10871061
});
10881062
with_window_event(MouseInput {
1089-
device_id: did,
1063+
device_id: None,
10901064
state: event::ElementState::Pressed,
10911065
button: event::MouseButton::Other(0),
10921066
});
10931067
with_window_event(PinchGesture {
1094-
device_id: did,
1068+
device_id: None,
10951069
delta: 0.0,
10961070
phase: event::TouchPhase::Started,
10971071
});
1098-
with_window_event(DoubleTapGesture { device_id: did });
1072+
with_window_event(DoubleTapGesture { device_id: None });
10991073
with_window_event(RotationGesture {
1100-
device_id: did,
1074+
device_id: None,
11011075
delta: 0.0,
11021076
phase: event::TouchPhase::Started,
11031077
});
11041078
with_window_event(PanGesture {
1105-
device_id: did,
1079+
device_id: None,
11061080
delta: PhysicalPosition::<f32>::new(0.0, 0.0),
11071081
phase: event::TouchPhase::Started,
11081082
});
1109-
with_window_event(TouchpadPressure { device_id: did, pressure: 0.0, stage: 0 });
1083+
with_window_event(TouchpadPressure { device_id: None, pressure: 0.0, stage: 0 });
11101084
with_window_event(Touch(event::Touch {
1111-
device_id: did,
1085+
device_id: None,
11121086
phase: event::TouchPhase::Started,
11131087
location: (0.0, 0.0).into(),
11141088
finger_id: fid,
@@ -1123,7 +1097,7 @@ mod tests {
11231097
use event::DeviceEvent::*;
11241098

11251099
let with_device_event =
1126-
|dev_ev| x(event::Event::DeviceEvent { device_id: did, event: dev_ev });
1100+
|dev_ev| x(event::Event::DeviceEvent { device_id: None, event: dev_ev });
11271101

11281102
with_device_event(MouseMotion { delta: (0.0, 0.0).into() });
11291103
with_device_event(MouseWheel {
@@ -1168,21 +1142,20 @@ mod tests {
11681142
});
11691143
let _ = event::StartCause::Init.clone();
11701144

1171-
let did = crate::event::DeviceId::dummy().clone();
11721145
let fid = crate::event::FingerId::dummy().clone();
1173-
HashSet::new().insert(did);
1174-
let mut set = [did, did, did];
1146+
HashSet::new().insert(fid);
1147+
let mut set = [fid, fid, fid];
11751148
set.sort_unstable();
11761149
let mut set2 = BTreeSet::new();
1177-
set2.insert(did);
1178-
set2.insert(did);
1150+
set2.insert(fid);
1151+
set2.insert(fid);
11791152

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

11841157
let _ = event::Touch {
1185-
device_id: did,
1158+
device_id: None,
11861159
phase: event::TouchPhase::Started,
11871160
location: (0.0, 0.0).into(),
11881161
finger_id: fid,

src/platform_impl/android/mod.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ impl EventLoop {
316316
match event {
317317
InputEvent::MotionEvent(motion_event) => {
318318
let window_id = window::WindowId(WindowId);
319-
let device_id = event::DeviceId(DeviceId(motion_event.device_id()));
319+
let device_id = Some(event::DeviceId(DeviceId(motion_event.device_id())));
320320

321321
let phase = match motion_event.action() {
322322
MotionAction::Down | MotionAction::PointerDown => {
@@ -388,7 +388,7 @@ impl EventLoop {
388388

389389
let window_id = window::WindowId(WindowId);
390390
let event = event::WindowEvent::KeyboardInput {
391-
device_id: event::DeviceId(DeviceId(key.device_id())),
391+
device_id: Some(event::DeviceId(DeviceId(key.device_id()))),
392392
event: event::KeyEvent {
393393
state,
394394
physical_key: keycodes::to_physical_key(keycode),
@@ -668,10 +668,6 @@ impl OwnedDisplayHandle {
668668
pub(crate) struct WindowId;
669669

670670
impl WindowId {
671-
pub const fn dummy() -> Self {
672-
WindowId
673-
}
674-
675671
pub const fn into_raw(self) -> u64 {
676672
0
677673
}
@@ -684,16 +680,11 @@ impl WindowId {
684680
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
685681
pub struct DeviceId(i32);
686682

687-
impl DeviceId {
688-
pub const fn dummy() -> Self {
689-
DeviceId(0)
690-
}
691-
}
692-
693683
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
694684
pub struct FingerId(i32);
695685

696686
impl FingerId {
687+
#[cfg(test)]
697688
pub const fn dummy() -> Self {
698689
FingerId(0)
699690
}

src/platform_impl/apple/appkit/app.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType, N
77
use objc2_foundation::{MainThreadMarker, NSObject};
88

99
use super::app_state::AppState;
10-
use super::DEVICE_ID;
1110
use crate::event::{DeviceEvent, ElementState};
1211

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

6261
if delta_x != 0.0 || delta_y != 0.0 {
6362
app_state.maybe_queue_with_handler(move |app, event_loop| {
64-
app.device_event(event_loop, DEVICE_ID, DeviceEvent::MouseMotion {
63+
app.device_event(event_loop, None, DeviceEvent::MouseMotion {
6564
delta: (delta_x, delta_y),
6665
});
6766
});
@@ -70,7 +69,7 @@ fn maybe_dispatch_device_event(app_state: &Rc<AppState>, event: &NSEvent) {
7069
NSEventType::LeftMouseDown | NSEventType::RightMouseDown | NSEventType::OtherMouseDown => {
7170
let button = unsafe { event.buttonNumber() } as u32;
7271
app_state.maybe_queue_with_handler(move |app, event_loop| {
73-
app.device_event(event_loop, DEVICE_ID, DeviceEvent::Button {
72+
app.device_event(event_loop, None, DeviceEvent::Button {
7473
button,
7574
state: ElementState::Pressed,
7675
});
@@ -79,7 +78,7 @@ fn maybe_dispatch_device_event(app_state: &Rc<AppState>, event: &NSEvent) {
7978
NSEventType::LeftMouseUp | NSEventType::RightMouseUp | NSEventType::OtherMouseUp => {
8079
let button = unsafe { event.buttonNumber() } as u32;
8180
app_state.maybe_queue_with_handler(move |app, event_loop| {
82-
app.device_event(event_loop, DEVICE_ID, DeviceEvent::Button {
81+
app.device_event(event_loop, None, DeviceEvent::Button {
8382
button,
8483
state: ElementState::Released,
8584
});

0 commit comments

Comments
 (0)