winit-core: revise MouseButton type

Unify the values of `MouseButton` and thus remove `Other` variant in
limit possible buttons to 32, which was picked based on platform
capabilities, where 32 is the highest.

For the reference, SDL has identical limit.
This commit is contained in:
Diggory Hardy 2025-08-07 09:52:34 +01:00 committed by Kirill Chibisov
parent 779f52a21f
commit 9d9d21cfdb
8 changed files with 191 additions and 229 deletions

View file

@ -27,43 +27,7 @@ bitflags::bitflags! {
const BACK = 0b001000;
const FORWARD = 0b010000;
const ERASER = 0b100000;
}
}
impl From<ButtonsState> for MouseButton {
fn from(value: ButtonsState) -> Self {
match value {
ButtonsState::LEFT => MouseButton::Left,
ButtonsState::RIGHT => MouseButton::Right,
ButtonsState::MIDDLE => MouseButton::Middle,
ButtonsState::BACK => MouseButton::Back,
ButtonsState::FORWARD => MouseButton::Forward,
_ => MouseButton::Other(value.bits()),
}
}
}
impl From<ButtonSource> for ButtonsState {
fn from(value: ButtonSource) -> Self {
match value {
ButtonSource::TabletTool { button, .. } => button.into(),
other => ButtonsState::from(other.mouse_button()),
}
}
}
impl From<MouseButton> for ButtonsState {
fn from(value: MouseButton) -> Self {
match value {
MouseButton::Left => ButtonsState::LEFT,
MouseButton::Right => ButtonsState::RIGHT,
MouseButton::Middle => ButtonsState::MIDDLE,
MouseButton::Back => ButtonsState::BACK,
MouseButton::Forward => ButtonsState::FORWARD,
MouseButton::Other(value) => ButtonsState::from_bits_retain(value),
}
}
}
}}
impl From<TabletToolButton> for ButtonsState {
fn from(tool: TabletToolButton) -> Self {
@ -92,14 +56,16 @@ pub fn raw_button(event: &MouseEvent) -> Option<u16> {
}
}
pub fn mouse_button(button: u16) -> MouseButton {
pub fn mouse_button(button: u16) -> ButtonSource {
match button {
0 => MouseButton::Left,
1 => MouseButton::Middle,
2 => MouseButton::Right,
3 => MouseButton::Back,
4 => MouseButton::Forward,
other => MouseButton::Other(other),
0 => MouseButton::Left.into(),
1 => MouseButton::Middle.into(),
2 => MouseButton::Right.into(),
3 => MouseButton::Back.into(),
4 => MouseButton::Forward.into(),
// Codes above 4 are not observed on Firefox or Chromium. 5 is defined as an eraser,
// which is not a mouse button. No other codes are defined.
i => ButtonSource::Unknown(i),
}
}

View file

@ -7,10 +7,9 @@ use winit_core::event::{ButtonSource, DeviceId, ElementState, PointerKind, Point
use winit_core::keyboard::ModifiersState;
use super::canvas::Common;
use super::event;
use super::event::{self, ButtonsState};
use super::event_handle::EventListenerHandle;
use crate::event::mkdid;
use crate::web_sys::event::ButtonsState;
#[allow(dead_code)]
pub(super) struct PointerHandler {
@ -84,7 +83,7 @@ impl PointerHandler {
let button = event::raw_button(&event).expect("no button pressed");
let source = match event::pointer_source(&event, kind) {
PointerSource::Mouse => ButtonSource::Mouse(event::mouse_button(button)),
PointerSource::Mouse => event::mouse_button(button),
PointerSource::Touch { finger_id, force } => {
ButtonSource::Touch { finger_id, force }
},
@ -138,7 +137,7 @@ impl PointerHandler {
// care if it fails.
let _e = canvas.set_pointer_capture(pointer_id);
ButtonSource::Mouse(event::mouse_button(button))
event::mouse_button(button)
},
PointerSource::Touch { finger_id, force } => {
ButtonSource::Touch { finger_id, force }
@ -217,7 +216,7 @@ impl PointerHandler {
};
let button = match event::pointer_source(&event, kind) {
PointerSource::Mouse => ButtonSource::Mouse(event::mouse_button(button)),
PointerSource::Mouse => event::mouse_button(button),
PointerSource::Touch { finger_id, force } => {
if button != 0 {
tracing::error!("unexpected touch button id: {button}");