Skip to content

Commit

Permalink
Rename WindowEvent::MouseButton to CursorButton
Browse files Browse the repository at this point in the history
Add a new `CursorButton` and `ToolButton` type.
  • Loading branch information
daxpedda committed Jul 22, 2024
1 parent df5221d commit 74bc843
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 51 deletions.
18 changes: 12 additions & 6 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use rwh_06::{DisplayHandle, HasDisplayHandle};
use softbuffer::{Context, Surface};
use winit::application::ApplicationHandler;
use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize};
use winit::event::{DeviceEvent, DeviceId, Ime, MouseButton, MouseScrollDelta, WindowEvent};
use winit::event::{
CursorButton, DeviceEvent, DeviceId, Ime, MouseButton, MouseScrollDelta, WindowEvent,
};
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::keyboard::{Key, ModifiersState};
#[cfg(macos_platform)]
Expand Down Expand Up @@ -373,12 +375,16 @@ impl ApplicationHandler for Application {
}
}
},
WindowEvent::MouseInput { button, state, .. } => {
WindowEvent::CursorInput { button, state, .. } => {
let mods = window.modifiers;
if let Some(action) =
state.is_pressed().then(|| Self::process_mouse_binding(button, &mods)).flatten()
{
self.handle_action(event_loop, window_id, action);
if let CursorButton::Mouse(button) = button {
if let Some(action) = state
.is_pressed()
.then(|| Self::process_mouse_binding(button, &mods))
.flatten()
{
self.handle_action(event_loop, window_id, action);
}
}
},
WindowEvent::CursorLeft { .. } => {
Expand Down
2 changes: 2 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ changelog entry.
`EventLoopExtRunOnDemand::run_app_on_demand` to accept a `impl ApplicationHandler` directly,
instead of requiring a `&mut` reference to it.
- `Force::normalized()` now takes a `Option<ToolAngle>` to calculate the perpendicular force.
- Rename `WindowEvent::MouseButton` to `WindowEvent::CursorButton` and add `CursorButton` and
`ToolButton`. This is part of the new pen/stylus feature.

### Removed

Expand Down
47 changes: 42 additions & 5 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ pub enum WindowEvent {
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
CursorLeft { device_id: DeviceId },

/// A cursor button press has been received.
CursorInput { device_id: DeviceId, state: ElementState, button: CursorButton },

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

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

/// Two-finger pinch gesture, often used for magnification.
///
/// ## Platform-specific
Expand Down Expand Up @@ -1130,6 +1130,21 @@ impl ToolAngle {
}
}

/// Input type for vaiour cursor types.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum CursorButton {
Mouse(MouseButton),
Pen(ToolButton),
Eraser(ToolButton),
}

impl From<MouseButton> for CursorButton {
fn from(mouse: MouseButton) -> Self {
Self::Mouse(mouse)
}
}

/// Describes the input state of a key.
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -1162,6 +1177,28 @@ pub enum MouseButton {
Other(u16),
}

/// Describes a button of a tool, e.g. a pen.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum ToolButton {
Contact,
Barrel,
Other(u16),
}

impl From<ToolButton> for MouseButton {
fn from(tool: ToolButton) -> Self {
match tool {
ToolButton::Contact => MouseButton::Left,
ToolButton::Barrel => MouseButton::Right,
ToolButton::Other(1) => MouseButton::Middle,
ToolButton::Other(3) => MouseButton::Back,
ToolButton::Other(4) => MouseButton::Forward,
ToolButton::Other(other) => MouseButton::Other(other),
}
}
}

/// Describes a difference in the mouse scroll wheel state.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -1275,10 +1312,10 @@ mod tests {
delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
phase: event::TouchPhase::Started,
});
with_window_event(MouseInput {
with_window_event(CursorInput {
device_id: did,
state: event::ElementState::Pressed,
button: event::MouseButton::Other(0),
button: event::MouseButton::Other(0).into(),
});
with_window_event(PinchGesture {
device_id: did,
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/apple/appkit/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,11 +1033,11 @@ impl WinitView {
}

fn mouse_click(&self, event: &NSEvent, button_state: ElementState) {
let button = mouse_button(event);
let button = mouse_button(event).into();

self.update_modifiers(event, false);

self.queue_event(WindowEvent::MouseInput {
self.queue_event(WindowEvent::CursorInput {
device_id: DEVICE_ID,
state: button_state,
button,
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/wayland/seat/pointer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl PointerHandler for WinitState {
ElementState::Released
};
self.events_sink.push_window_event(
WindowEvent::MouseInput { device_id, state, button },
WindowEvent::CursorInput { device_id, state, button: button.into() },
window_id,
);
},
Expand Down
25 changes: 19 additions & 6 deletions src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,21 +1058,30 @@ impl EventProcessor {
// Set the timestamp.
wt.xconn.set_timestamp(event.time as xproto::Timestamp);

let Some(DeviceType::Mouse) = self
.devices
.borrow()
.get(&DeviceId(event.sourceid as xinput::DeviceId))
.map(|device| device.r#type)
else {
return;
};

// Deliver multi-touch events instead of emulated mouse events.
if (event.flags & xinput2::XIPointerEmulated) != 0 {
return;
}

let event = match event.detail as u32 {
xlib::Button1 => {
WindowEvent::MouseInput { device_id, state, button: MouseButton::Left }
WindowEvent::CursorInput { device_id, state, button: MouseButton::Left.into() }
},
xlib::Button2 => {
WindowEvent::MouseInput { device_id, state, button: MouseButton::Middle }
WindowEvent::CursorInput { device_id, state, button: MouseButton::Middle.into() }
},

xlib::Button3 => {
WindowEvent::MouseInput { device_id, state, button: MouseButton::Right }
WindowEvent::CursorInput { device_id, state, button: MouseButton::Right.into() }
},

// Suppress emulated scroll wheel clicks, since we handle the real motion events for
Expand All @@ -1090,10 +1099,14 @@ impl EventProcessor {
},
phase: TouchPhase::Moved,
},
8 => WindowEvent::MouseInput { device_id, state, button: MouseButton::Back },
8 => WindowEvent::CursorInput { device_id, state, button: MouseButton::Back.into() },

9 => WindowEvent::MouseInput { device_id, state, button: MouseButton::Forward },
x => WindowEvent::MouseInput { device_id, state, button: MouseButton::Other(x as u16) },
9 => WindowEvent::CursorInput { device_id, state, button: MouseButton::Forward.into() },
x => WindowEvent::CursorInput {
device_id,
state,
button: MouseButton::Other(x as u16).into(),
},
};

let event = Event::WindowEvent { window_id, event };
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/orbital/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,10 @@ impl EventLoop {
app.window_event(
window_target,
RootWindowId(window_id),
event::WindowEvent::MouseInput {
event::WindowEvent::CursorInput {
device_id: event::DeviceId(DeviceId),
state,
button,
button: button.into(),
},
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl ActiveEventLoop {
},
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseInput { device_id, state, button },
event: WindowEvent::CursorInput { device_id, state, button },
},
]));
}
Expand Down Expand Up @@ -432,10 +432,10 @@ impl ActiveEventLoop {
},
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseInput {
event: WindowEvent::CursorInput {
device_id,
state: ElementState::Pressed,
button,
button: button.into(),
},
},
]));
Expand Down Expand Up @@ -517,10 +517,10 @@ impl ActiveEventLoop {
},
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseInput {
event: WindowEvent::CursorInput {
device_id,
state: ElementState::Released,
button,
button: button.into(),
},
},
]));
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/web/web_sys/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::pointer::PointerHandler;
use super::{event, fullscreen, ButtonsState, ResizeScaleHandle};
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
use crate::error::OsError as RootOE;
use crate::event::{Force, InnerSizeWriter, MouseButton, MouseScrollDelta};
use crate::event::{CursorButton, Force, InnerSizeWriter, MouseButton, MouseScrollDelta};
use crate::keyboard::{Key, KeyLocation, ModifiersState, PhysicalKey};
use crate::platform_impl::OsError;
use crate::window::{WindowAttributes, WindowId as RootWindowId};
Expand Down Expand Up @@ -376,7 +376,7 @@ impl Canvas {
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
T: 'static
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, CursorButton),
{
self.pointer_handler.on_cursor_move(
&self.common,
Expand Down
21 changes: 20 additions & 1 deletion src/platform_impl/web/web_sys/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use wasm_bindgen::{JsCast, JsValue};
use web_sys::{KeyboardEvent, MouseEvent, PointerEvent, WheelEvent};

use super::Engine;
use crate::event::{MouseButton, MouseScrollDelta};
use crate::event::{CursorButton, MouseButton, MouseScrollDelta, ToolButton};
use crate::keyboard::{Key, KeyLocation, ModifiersState, NamedKey, PhysicalKey};

bitflags::bitflags! {
Expand Down Expand Up @@ -35,6 +35,15 @@ impl From<ButtonsState> for MouseButton {
}
}

impl From<CursorButton> for ButtonsState {
fn from(cursor: CursorButton) -> Self {
match cursor {
CursorButton::Mouse(mouse) => mouse.into(),
CursorButton::Pen(tool) | CursorButton::Eraser(tool) => tool.into(),
}
}
}

impl From<MouseButton> for ButtonsState {
fn from(value: MouseButton) -> Self {
match value {
Expand All @@ -48,6 +57,16 @@ impl From<MouseButton> for ButtonsState {
}
}

impl From<ToolButton> for ButtonsState {
fn from(tool: ToolButton) -> Self {
match tool {
ToolButton::Contact => ButtonsState::LEFT,
ToolButton::Barrel => ButtonsState::RIGHT,
ToolButton::Other(value) => Self::from_bits_retain(value),
}
}
}

pub fn mouse_buttons(event: &MouseEvent) -> ButtonsState {
ButtonsState::from_bits_retain(event.buttons())
}
Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/web/web_sys/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::canvas::Common;
use super::event;
use super::event_handle::EventListenerHandle;
use crate::dpi::PhysicalPosition;
use crate::event::{Force, MouseButton};
use crate::event::{CursorButton, Force, MouseButton};
use crate::keyboard::ModifiersState;

#[allow(dead_code)]
Expand Down Expand Up @@ -167,7 +167,7 @@ impl PointerHandler {
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
T: 'static
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, CursorButton),
{
let window = canvas_common.window.clone();
let canvas = canvas_common.raw().clone();
Expand Down Expand Up @@ -204,7 +204,7 @@ impl PointerHandler {
id,
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
event::mouse_buttons(&event),
button,
button.into(),
);

return;
Expand Down
Loading

0 comments on commit 74bc843

Please sign in to comment.