Skip to content

Commit

Permalink
chore: remove platform FingerId
Browse files Browse the repository at this point in the history
The same as for `WindowId`.
  • Loading branch information
kchibisov committed Nov 2, 2024
1 parent c8c1eca commit edfb4b0
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 172 deletions.
23 changes: 17 additions & 6 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,12 +634,23 @@ impl DeviceId {
/// Whenever a touch event is received it contains a `FingerId` which uniquely identifies the finger
/// used for the current interaction.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FingerId(pub(crate) platform_impl::FingerId);
pub struct FingerId(pub(crate) usize);

impl FingerId {
#[cfg(test)]
pub(crate) const fn dummy() -> Self {
FingerId(platform_impl::FingerId::dummy())
/// Convert the [`FingerId`] into the underlying integer.
///
/// This is useful if you need to pass the ID across an FFI boundary, or store it in an atomic.
#[allow(dead_code)]
pub(crate) const fn into_raw(self) -> usize {
self.0
}

/// Construct a [`FingerId`] from the underlying integer.
///
/// This should only be called with integers returned from [`FingerId::into_raw`].
#[allow(dead_code)]
pub(crate) const fn from_raw(id: usize) -> Self {
Self(id)
}
}

Expand Down Expand Up @@ -1154,7 +1165,7 @@ mod tests {
($closure:expr) => {{
#[allow(unused_mut)]
let mut x = $closure;
let fid = event::FingerId::dummy();
let fid = event::FingerId::from_raw(0);

#[allow(deprecated)]
{
Expand Down Expand Up @@ -1291,7 +1302,7 @@ mod tests {
});
let _ = event::StartCause::Init.clone();

let fid = crate::event::FingerId::dummy().clone();
let fid = crate::event::FingerId::from_raw(0).clone();
HashSet::new().insert(fid);
let mut set = [fid, fid, fid];
set.sort_unstable();
Expand Down
1 change: 1 addition & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod windows;
#[cfg(any(x11_platform, docsrs))]
pub mod x11;

#[allow(unused_imports)]
#[cfg(any(
windows_platform,
macos_platform,
Expand Down
25 changes: 8 additions & 17 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::application::ApplicationHandler;
use crate::cursor::Cursor;
use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
use crate::error::{EventLoopError, NotSupportedError, RequestError};
use crate::event::{self, DeviceId, Force, StartCause, SurfaceSizeWriter};
use crate::event::{self, DeviceId, FingerId, Force, StartCause, SurfaceSizeWriter};
use crate::event_loop::{
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
EventLoopProxy as RootEventLoopProxy, OwnedDisplayHandle as RootOwnedDisplayHandle,
Expand Down Expand Up @@ -107,7 +107,7 @@ pub struct EventLoop {
running: bool,
pending_redraw: bool,
cause: StartCause,
primary_pointer: FingerId,
primary_pointer: Option<FingerId>,
ignore_volume_keys: bool,
combining_accent: Option<char>,
}
Expand Down Expand Up @@ -141,7 +141,7 @@ impl EventLoop {

Ok(Self {
android_app: android_app.clone(),
primary_pointer: FingerId::dummy(),
primary_pointer: None,
window_target: ActiveEventLoop {
app: android_app.clone(),
control_flow: Cell::new(ControlFlow::default()),
Expand Down Expand Up @@ -342,14 +342,14 @@ impl EventLoop {
"Input event {device_id:?}, {action:?}, loc={position:?}, \
pointer={pointer:?}, tool_type={tool_type:?}"
);
let finger_id = event::FingerId(FingerId(pointer.pointer_id()));
let finger_id = FingerId::from_raw(pointer.pointer_id() as usize);
let force = Some(Force::Normalized(pointer.pressure() as f64));

match action {
MotionAction::Down | MotionAction::PointerDown => {
let primary = action == MotionAction::Down;
if primary {
self.primary_pointer = finger_id.0;
self.primary_pointer = Some(finger_id);
}
let event = event::WindowEvent::PointerEntered {
device_id,
Expand Down Expand Up @@ -382,7 +382,7 @@ impl EventLoop {
app.window_event(&self.window_target, GLOBAL_WINDOW, event);
},
MotionAction::Move => {
let primary = self.primary_pointer == finger_id.0;
let primary = self.primary_pointer == Some(finger_id);
let event = event::WindowEvent::PointerMoved {
device_id,
primary,
Expand All @@ -401,10 +401,10 @@ impl EventLoop {
MotionAction::Up | MotionAction::PointerUp | MotionAction::Cancel => {
let primary = action == MotionAction::Up
|| (action == MotionAction::Cancel
&& self.primary_pointer == finger_id.0);
&& self.primary_pointer == Some(finger_id));

if primary {
self.primary_pointer = FingerId::dummy();
self.primary_pointer = None;
}

if let MotionAction::Up | MotionAction::PointerUp = action {
Expand Down Expand Up @@ -745,15 +745,6 @@ impl OwnedDisplayHandle {
}
}

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

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

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PlatformSpecificWindowAttributes;

Expand Down
10 changes: 0 additions & 10 deletions src/platform_impl/apple/appkit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,3 @@ pub(crate) use self::window_delegate::PlatformSpecificWindowAttributes;
pub(crate) use crate::cursor::OnlyCursorImageSource as PlatformCustomCursorSource;
pub(crate) use crate::icon::NoIcon as PlatformIcon;
pub(crate) use crate::platform_impl::Fullscreen;

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

impl FingerId {
#[cfg(test)]
pub const fn dummy() -> Self {
FingerId
}
}
2 changes: 2 additions & 0 deletions src/platform_impl/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ mod notification_center;
#[cfg(not(target_os = "macos"))]
mod uikit;

#[allow(unused_imports)]
#[cfg(target_os = "macos")]
pub use self::appkit::*;
#[allow(unused_imports)]
#[cfg(not(target_os = "macos"))]
pub use self::uikit::*;
10 changes: 0 additions & 10 deletions src/platform_impl/apple/uikit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ pub(crate) use crate::cursor::{
pub(crate) use crate::icon::NoIcon as PlatformIcon;
pub(crate) use crate::platform_impl::Fullscreen;

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

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

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct KeyEventExtra {}

Expand Down
19 changes: 10 additions & 9 deletions src/platform_impl/apple/uikit/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ use objc2_ui_kit::{

use super::app_state::{self, EventWrapper};
use super::window::WinitUIWindow;
use super::FingerId;
use crate::dpi::PhysicalPosition;
use crate::event::{
ButtonSource, ElementState, Event, FingerId as RootFingerId, Force, KeyEvent, PointerKind,
PointerSource, TouchPhase, WindowEvent,
ButtonSource, ElementState, Event, FingerId, Force, KeyEvent, PointerKind, PointerSource,
TouchPhase, WindowEvent,
};
use crate::keyboard::{Key, KeyCode, KeyLocation, NamedKey, NativeKeyCode, PhysicalKey};
use crate::platform_impl::KeyEventExtra;
Expand All @@ -35,7 +34,7 @@ pub struct WinitViewState {
pinch_last_delta: Cell<CGFloat>,
pan_last_delta: Cell<CGPoint>,

primary_finger: Cell<Option<usize>>,
primary_finger: Cell<Option<FingerId>>,
fingers: Cell<u8>,
}

Expand Down Expand Up @@ -519,7 +518,7 @@ impl WinitView {
)
};
let window_id = window.id();
let finger_id = RootFingerId(FingerId(touch_id));
let finger_id = FingerId::from_raw(touch_id);

let ivars = self.ivars();

Expand All @@ -529,15 +528,17 @@ impl WinitView {
true
} else {
ivars.fingers.set(ivars.fingers.get() + 1);
// Keep the primary finger around until we clear all the fingers to
// recognize it when user briefly removes it.
match ivars.primary_finger.get() {
Some(primary_id) => primary_id == touch_id,
Some(primary_id) => primary_id == finger_id,
None => {
debug_assert_eq!(
ivars.fingers.get(),
1,
"number of fingers were not counted correctly"
);
ivars.primary_finger.set(Some(touch_id));
ivars.primary_finger.set(Some(finger_id));
true
},
}
Expand Down Expand Up @@ -575,7 +576,7 @@ impl WinitView {
let (primary, source) = if let UITouchType::Pencil = touch_type {
(true, PointerSource::Unknown)
} else {
(ivars.primary_finger.get().unwrap() == touch_id, PointerSource::Touch {
(ivars.primary_finger.get().unwrap() == finger_id, PointerSource::Touch {
finger_id,
force,
})
Expand All @@ -597,7 +598,7 @@ impl WinitView {
true
} else {
ivars.fingers.set(ivars.fingers.get() - 1);
let primary = ivars.primary_finger.get().unwrap() == touch_id;
let primary = ivars.primary_finger.get().unwrap() == finger_id;
if ivars.fingers.get() == 0 {
ivars.primary_finger.set(None);
}
Expand Down
18 changes: 0 additions & 18 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,6 @@ impl Default for PlatformSpecificWindowAttributes {
pub(crate) static X11_BACKEND: Lazy<Mutex<Result<Arc<XConnection>, XNotSupported>>> =
Lazy::new(|| Mutex::new(XConnection::new(Some(x_error_callback)).map(Arc::new)));

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FingerId {
#[cfg(x11_platform)]
X(x11::FingerId),
#[cfg(wayland_platform)]
Wayland(wayland::FingerId),
}

impl FingerId {
#[cfg(test)]
pub const fn dummy() -> Self {
#[cfg(wayland_platform)]
return FingerId::Wayland(wayland::FingerId::dummy());
#[cfg(all(not(wayland_platform), x11_platform))]
return FingerId::X(x11::FingerId::dummy());
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum MonitorHandle {
#[cfg(x11_platform)]
Expand Down
10 changes: 0 additions & 10 deletions src/platform_impl/linux/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ mod state;
mod types;
mod window;

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

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

/// Get the WindowId out of the surface.
#[inline]
fn make_wid(surface: &WlSurface) -> WindowId {
Expand Down
29 changes: 15 additions & 14 deletions src/platform_impl/linux/wayland/seat/touch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use sctk::seat::touch::{TouchData, TouchHandler};
use tracing::warn;

use crate::dpi::LogicalPosition;
use crate::event::{ButtonSource, ElementState, PointerKind, PointerSource, WindowEvent};
use crate::event::{ButtonSource, ElementState, FingerId, PointerKind, PointerSource, WindowEvent};
use crate::platform_impl::wayland;
use crate::platform_impl::wayland::state::WinitState;
use crate::platform_impl::wayland::{self, FingerId};

impl TouchHandler for WinitState {
fn down(
Expand Down Expand Up @@ -40,12 +40,15 @@ impl TouchHandler for WinitState {

// Update the state of the point.
let location = LogicalPosition::<f64>::from(position);
// Only update primary finger once we don't have any touch.
if seat_state.touch_map.is_empty() {
seat_state.first_touch_id = Some(id);
}
let primary = seat_state.first_touch_id == Some(id);
seat_state.touch_map.insert(id, TouchPoint { surface, location });
let primary = seat_state.first_touch_id.get_or_insert(id) == &id;

let position = location.to_physical(scale_factor);
let finger_id =
crate::event::FingerId(crate::platform_impl::FingerId::Wayland(FingerId(id)));
let finger_id = FingerId::from_raw(id as usize);

self.events_sink.push_window_event(
WindowEvent::PointerEntered {
Expand Down Expand Up @@ -93,7 +96,10 @@ impl TouchHandler for WinitState {

// Update the primary touch point.
let primary = seat_state.first_touch_id == Some(id);
if primary {
// Reset primary finger once all the other fingers are lifted to not transfer primary
// finger to some other finger and still accept it when it's briefly moved between the
// windows.
if seat_state.touch_map.is_empty() {
seat_state.first_touch_id = None;
}

Expand All @@ -104,8 +110,7 @@ impl TouchHandler for WinitState {
};

let position = touch_point.location.to_physical(scale_factor);
let finger_id =
crate::event::FingerId(crate::platform_impl::FingerId::Wayland(FingerId(id)));
let finger_id = FingerId::from_raw(id as usize);

self.events_sink.push_window_event(
WindowEvent::PointerButton {
Expand Down Expand Up @@ -167,9 +172,7 @@ impl TouchHandler for WinitState {
primary,
position: touch_point.location.to_physical(scale_factor),
source: PointerSource::Touch {
finger_id: crate::event::FingerId(crate::platform_impl::FingerId::Wayland(
FingerId(id),
)),
finger_id: FingerId::from_raw(id as usize),
force: None,
},
},
Expand Down Expand Up @@ -201,9 +204,7 @@ impl TouchHandler for WinitState {
device_id: None,
primary,
position: Some(position),
kind: PointerKind::Touch(crate::event::FingerId(
crate::platform_impl::FingerId::Wayland(FingerId(id)),
)),
kind: PointerKind::Touch(FingerId::from_raw(id as usize)),
},
window_id,
);
Expand Down
9 changes: 5 additions & 4 deletions src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use xkbcommon_dl::xkb_mod_mask_t;

use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::event::{
ButtonSource, DeviceEvent, DeviceId, ElementState, Event, Ime, MouseButton, MouseScrollDelta,
PointerKind, PointerSource, RawKeyEvent, SurfaceSizeWriter, TouchPhase, WindowEvent,
ButtonSource, DeviceEvent, DeviceId, ElementState, Event, FingerId, Ime, MouseButton,
MouseScrollDelta, PointerKind, PointerSource, RawKeyEvent, SurfaceSizeWriter, TouchPhase,
WindowEvent,
};
use crate::keyboard::ModifiersState;
use crate::platform_impl::common::xkb::{self, XkbState};
Expand All @@ -33,7 +34,7 @@ use crate::platform_impl::platform::x11::ActiveEventLoop;
use crate::platform_impl::x11::atoms::*;
use crate::platform_impl::x11::util::cookie::GenericEventCookie;
use crate::platform_impl::x11::{
mkdid, mkfid, mkwid, util, CookieResultExt, Device, DeviceInfo, Dnd, DndState, ImeReceiver,
mkdid, mkwid, util, CookieResultExt, Device, DeviceInfo, Dnd, DndState, ImeReceiver,
ScrollOrientation, UnownedWindow, WindowId,
};

Expand Down Expand Up @@ -1390,7 +1391,7 @@ impl EventProcessor {
}

let device_id = Some(mkdid(xev.deviceid as xinput::DeviceId));
let finger_id = mkfid(id);
let finger_id = FingerId::from_raw(id as usize);

match phase {
xinput2::XI_TouchBegin => {
Expand Down
Loading

0 comments on commit edfb4b0

Please sign in to comment.