Skip to content

Commit f0b9bdd

Browse files
committed
Rename WindowEvent::MouseButton to CursorButton
Add a new `CursorButton` and `ToolButton` type.
1 parent f85cef0 commit f0b9bdd

File tree

12 files changed

+121
-51
lines changed

12 files changed

+121
-51
lines changed

examples/window.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use rwh_06::{DisplayHandle, HasDisplayHandle};
1616
use softbuffer::{Context, Surface};
1717
use winit::application::ApplicationHandler;
1818
use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize};
19-
use winit::event::{DeviceEvent, DeviceId, Ime, MouseButton, MouseScrollDelta, WindowEvent};
19+
use winit::event::{
20+
CursorButton, DeviceEvent, DeviceId, Ime, MouseButton, MouseScrollDelta, WindowEvent,
21+
};
2022
use winit::event_loop::{ActiveEventLoop, EventLoop};
2123
use winit::keyboard::{Key, ModifiersState};
2224
#[cfg(macos_platform)]
@@ -373,12 +375,16 @@ impl ApplicationHandler for Application {
373375
}
374376
}
375377
},
376-
WindowEvent::MouseInput { button, state, .. } => {
378+
WindowEvent::CursorInput { button, state, .. } => {
377379
let mods = window.modifiers;
378-
if let Some(action) =
379-
state.is_pressed().then(|| Self::process_mouse_binding(button, &mods)).flatten()
380-
{
381-
self.handle_action(event_loop, window_id, action);
380+
if let CursorButton::Mouse(button) = button {
381+
if let Some(action) = state
382+
.is_pressed()
383+
.then(|| Self::process_mouse_binding(button, &mods))
384+
.flatten()
385+
{
386+
self.handle_action(event_loop, window_id, action);
387+
}
382388
}
383389
},
384390
WindowEvent::CursorLeft { .. } => {

src/changelog/unreleased.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ changelog entry.
7272
`EventLoopExtRunOnDemand::run_app_on_demand` to accept a `impl ApplicationHandler` directly,
7373
instead of requiring a `&mut` reference to it.
7474
- `Force::normalized()` now takes a `Option<ToolAngle>` to calculate the perpendicular force.
75+
- Rename `WindowEvent::MouseButton` to `WindowEvent::CursorButton` and add `CursorButton` and
76+
`ToolButton`. This is part of the new pen/stylus feature.
7577

7678
### Removed
7779

src/event.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,12 @@ pub enum WindowEvent {
266266
/// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
267267
CursorLeft { device_id: DeviceId },
268268

269+
/// A cursor button press has been received.
270+
CursorInput { device_id: DeviceId, state: ElementState, button: CursorButton },
271+
269272
/// A mouse wheel movement or touchpad scroll occurred.
270273
MouseWheel { device_id: DeviceId, delta: MouseScrollDelta, phase: TouchPhase },
271274

272-
/// An mouse button press has been received.
273-
MouseInput { device_id: DeviceId, state: ElementState, button: MouseButton },
274-
275275
/// Two-finger pinch gesture, often used for magnification.
276276
///
277277
/// ## Platform-specific
@@ -1130,6 +1130,21 @@ impl ToolAngle {
11301130
}
11311131
}
11321132

1133+
/// Input type for vaiour cursor types.
1134+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
1135+
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
1136+
pub enum CursorButton {
1137+
Mouse(MouseButton),
1138+
Pen(ToolButton),
1139+
Eraser(ToolButton),
1140+
}
1141+
1142+
impl From<MouseButton> for CursorButton {
1143+
fn from(mouse: MouseButton) -> Self {
1144+
Self::Mouse(mouse)
1145+
}
1146+
}
1147+
11331148
/// Describes the input state of a key.
11341149
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
11351150
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -1162,6 +1177,28 @@ pub enum MouseButton {
11621177
Other(u16),
11631178
}
11641179

1180+
/// Describes a button of a tool, e.g. a pen.
1181+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
1182+
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
1183+
pub enum ToolButton {
1184+
Contact,
1185+
Barrel,
1186+
Other(u16),
1187+
}
1188+
1189+
impl From<ToolButton> for MouseButton {
1190+
fn from(tool: ToolButton) -> Self {
1191+
match tool {
1192+
ToolButton::Contact => MouseButton::Left,
1193+
ToolButton::Barrel => MouseButton::Right,
1194+
ToolButton::Other(1) => MouseButton::Middle,
1195+
ToolButton::Other(3) => MouseButton::Back,
1196+
ToolButton::Other(4) => MouseButton::Forward,
1197+
ToolButton::Other(other) => MouseButton::Other(other),
1198+
}
1199+
}
1200+
}
1201+
11651202
/// Describes a difference in the mouse scroll wheel state.
11661203
#[derive(Debug, Clone, Copy, PartialEq)]
11671204
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -1275,10 +1312,10 @@ mod tests {
12751312
delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
12761313
phase: event::TouchPhase::Started,
12771314
});
1278-
with_window_event(MouseInput {
1315+
with_window_event(CursorInput {
12791316
device_id: did,
12801317
state: event::ElementState::Pressed,
1281-
button: event::MouseButton::Other(0),
1318+
button: event::MouseButton::Other(0).into(),
12821319
});
12831320
with_window_event(PinchGesture {
12841321
device_id: did,

src/platform_impl/apple/appkit/view.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,11 +1033,11 @@ impl WinitView {
10331033
}
10341034

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

10381038
self.update_modifiers(event, false);
10391039

1040-
self.queue_event(WindowEvent::MouseInput {
1040+
self.queue_event(WindowEvent::CursorInput {
10411041
device_id: DEVICE_ID,
10421042
state: button_state,
10431043
button,

src/platform_impl/linux/wayland/seat/pointer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl PointerHandler for WinitState {
164164
ElementState::Released
165165
};
166166
self.events_sink.push_window_event(
167-
WindowEvent::MouseInput { device_id, state, button },
167+
WindowEvent::CursorInput { device_id, state, button: button.into() },
168168
window_id,
169169
);
170170
},

src/platform_impl/linux/x11/event_processor.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,14 +1062,14 @@ impl EventProcessor {
10621062

10631063
let event = match event.detail as u32 {
10641064
xlib::Button1 => {
1065-
WindowEvent::MouseInput { device_id, state, button: MouseButton::Left }
1065+
WindowEvent::CursorInput { device_id, state, button: MouseButton::Left.into() }
10661066
},
10671067
xlib::Button2 => {
1068-
WindowEvent::MouseInput { device_id, state, button: MouseButton::Middle }
1068+
WindowEvent::CursorInput { device_id, state, button: MouseButton::Middle.into() }
10691069
},
10701070

10711071
xlib::Button3 => {
1072-
WindowEvent::MouseInput { device_id, state, button: MouseButton::Right }
1072+
WindowEvent::CursorInput { device_id, state, button: MouseButton::Right.into() }
10731073
},
10741074

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

1092-
9 => WindowEvent::MouseInput { device_id, state, button: MouseButton::Forward },
1093-
x => WindowEvent::MouseInput { device_id, state, button: MouseButton::Other(x as u16) },
1092+
9 => WindowEvent::CursorInput { device_id, state, button: MouseButton::Forward.into() },
1093+
x => WindowEvent::CursorInput {
1094+
device_id,
1095+
state,
1096+
button: MouseButton::Other(x as u16).into(),
1097+
},
10941098
};
10951099

10961100
let event = Event::WindowEvent { window_id, event };

src/platform_impl/orbital/event_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,10 @@ impl EventLoop {
435435
app.window_event(
436436
window_target,
437437
RootWindowId(window_id),
438-
event::WindowEvent::MouseInput {
438+
event::WindowEvent::CursorInput {
439439
device_id: event::DeviceId(DeviceId),
440440
state,
441-
button,
441+
button: button.into(),
442442
},
443443
);
444444
}

src/platform_impl/web/event_loop/window_target.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl ActiveEventLoop {
381381
},
382382
Event::WindowEvent {
383383
window_id: RootWindowId(id),
384-
event: WindowEvent::MouseInput { device_id, state, button },
384+
event: WindowEvent::CursorInput { device_id, state, button },
385385
},
386386
]));
387387
}
@@ -432,10 +432,10 @@ impl ActiveEventLoop {
432432
},
433433
Event::WindowEvent {
434434
window_id: RootWindowId(id),
435-
event: WindowEvent::MouseInput {
435+
event: WindowEvent::CursorInput {
436436
device_id,
437437
state: ElementState::Pressed,
438-
button,
438+
button: button.into(),
439439
},
440440
},
441441
]));
@@ -517,10 +517,10 @@ impl ActiveEventLoop {
517517
},
518518
Event::WindowEvent {
519519
window_id: RootWindowId(id),
520-
event: WindowEvent::MouseInput {
520+
event: WindowEvent::CursorInput {
521521
device_id,
522522
state: ElementState::Released,
523-
button,
523+
button: button.into(),
524524
},
525525
},
526526
]));

src/platform_impl/web/web_sys/canvas.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::pointer::PointerHandler;
2222
use super::{event, fullscreen, ButtonsState, ResizeScaleHandle};
2323
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
2424
use crate::error::OsError as RootOE;
25-
use crate::event::{Force, InnerSizeWriter, MouseButton, MouseScrollDelta};
25+
use crate::event::{CursorButton, Force, InnerSizeWriter, MouseButton, MouseScrollDelta};
2626
use crate::keyboard::{Key, KeyLocation, ModifiersState, PhysicalKey};
2727
use crate::platform_impl::OsError;
2828
use crate::window::{WindowAttributes, WindowId as RootWindowId};
@@ -376,7 +376,7 @@ impl Canvas {
376376
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
377377
T: 'static
378378
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
379-
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
379+
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, CursorButton),
380380
{
381381
self.pointer_handler.on_cursor_move(
382382
&self.common,

src/platform_impl/web/web_sys/event.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use wasm_bindgen::{JsCast, JsValue};
77
use web_sys::{KeyboardEvent, MouseEvent, PointerEvent, WheelEvent};
88

99
use super::Engine;
10-
use crate::event::{MouseButton, MouseScrollDelta};
10+
use crate::event::{CursorButton, MouseButton, MouseScrollDelta, ToolButton};
1111
use crate::keyboard::{Key, KeyLocation, ModifiersState, NamedKey, PhysicalKey};
1212

1313
bitflags::bitflags! {
@@ -35,6 +35,15 @@ impl From<ButtonsState> for MouseButton {
3535
}
3636
}
3737

38+
impl From<CursorButton> for ButtonsState {
39+
fn from(cursor: CursorButton) -> Self {
40+
match cursor {
41+
CursorButton::Mouse(mouse) => mouse.into(),
42+
CursorButton::Pen(tool) | CursorButton::Eraser(tool) => tool.into(),
43+
}
44+
}
45+
}
46+
3847
impl From<MouseButton> for ButtonsState {
3948
fn from(value: MouseButton) -> Self {
4049
match value {
@@ -48,6 +57,16 @@ impl From<MouseButton> for ButtonsState {
4857
}
4958
}
5059

60+
impl From<ToolButton> for ButtonsState {
61+
fn from(tool: ToolButton) -> Self {
62+
match tool {
63+
ToolButton::Contact => ButtonsState::LEFT,
64+
ToolButton::Barrel => ButtonsState::RIGHT,
65+
ToolButton::Other(value) => Self::from_bits_retain(value),
66+
}
67+
}
68+
}
69+
5170
pub fn mouse_buttons(event: &MouseEvent) -> ButtonsState {
5271
ButtonsState::from_bits_retain(event.buttons())
5372
}

0 commit comments

Comments
 (0)