-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Revise MouseButton type #4324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhardy
wants to merge
6
commits into
rust-windowing:master
Choose a base branch
from
dhardy:push-oxmvmpxmqlqx
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Revise MouseButton type #4324
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10963d4
Add web_sys::event::MouseButton; avoid incorrect state check for unkn…
dhardy ff6d5fe
winit-win32: deduplicate L/R/M mouse button handling
dhardy 3a80434
Standardise extra button codes on Wayland, X11
dhardy 514cd77
ButtonSource::Unknown has undefined meaning
dhardy b284d26
Use ButtonSource::Unknown to handle unknown codes
dhardy 0f3f8c2
Revise MouseButton type: enum -> struct
dhardy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -514,25 +514,21 @@ pub enum ButtonSource { | |
finger_id: FingerId, | ||
force: Option<Force>, | ||
}, | ||
/// A pointer button of unknown source. | ||
/// | ||
/// Codes are undefined and may not be reproducible across platforms or winit versions. | ||
Unknown(u16), | ||
} | ||
|
||
impl ButtonSource { | ||
/// Convert any [`ButtonSource`] to an equivalent [`MouseButton`]. If a pointer type has no | ||
/// Try to convert a [`ButtonSource`] to an equivalent [`MouseButton`]. If a pointer type has no | ||
/// special handling in an application, this method can be used to handle it like any generic | ||
/// mouse input. | ||
pub fn mouse_button(self) -> MouseButton { | ||
pub fn mouse_button(self) -> Option<MouseButton> { | ||
match self { | ||
ButtonSource::Mouse(mouse) => mouse, | ||
ButtonSource::Touch { .. } => MouseButton::Left, | ||
ButtonSource::Unknown(button) => match button { | ||
0 => MouseButton::Left, | ||
1 => MouseButton::Middle, | ||
2 => MouseButton::Right, | ||
3 => MouseButton::Back, | ||
4 => MouseButton::Forward, | ||
_ => MouseButton::Other(button), | ||
}, | ||
ButtonSource::Mouse(mouse) => Some(mouse), | ||
ButtonSource::Touch { .. } => Some(MouseButton::Left), | ||
ButtonSource::Unknown(_) => None, | ||
} | ||
} | ||
} | ||
|
@@ -1041,21 +1037,113 @@ impl ElementState { | |
} | ||
} | ||
|
||
/// Describes a button of a mouse controller. | ||
/// Identifies a button of a mouse controller. | ||
/// | ||
/// ## Platform-specific | ||
/// | ||
/// **macOS:** `Back` and `Forward` might not work with all hardware. | ||
/// **Orbital:** `Back` and `Forward` are unsupported due to orbital not supporting them. | ||
/// The first three buttons should be supported on all platforms. | ||
/// [`Self::Back`] and [`Self::Forward`] are supported on most platforms | ||
/// (when using a compatible mouse). | ||
/// | ||
/// - **Android, iOS:** Currently not supported. | ||
/// - **Orbital:** Only left/right/middle buttons are supported at this time. | ||
/// - **Web, Windows:** Supports left/right/middle/back/forward buttons. | ||
/// - **Wayland:** Supports buttons 0..=15. | ||
/// - **macOS, X11:** Supports all button variants. | ||
/// - **X11:** Supports buttons 0..=250. | ||
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[repr(u8)] | ||
pub enum MouseButton { | ||
Left, | ||
Right, | ||
Middle, | ||
Back, | ||
Forward, | ||
Other(u16), | ||
/// The primary (usually left) button | ||
Left = 0, | ||
/// The secondary (usually right) button | ||
Right = 1, | ||
/// The tertiary (usually middle) button | ||
Middle = 2, | ||
/// The first side button, frequently assigned a back function | ||
Back = 3, | ||
/// The second side button, frequently assigned a forward function | ||
Forward = 4, | ||
/// The sixth button | ||
Button5 = 5, | ||
/// The seventh button | ||
Button6 = 6, | ||
/// The eighth button | ||
Button7 = 7, | ||
/// The ninth button | ||
Button8 = 8, | ||
/// The tenth button | ||
Button9 = 9, | ||
/// The eleventh button | ||
Button10 = 10, | ||
/// The twelfth button | ||
Button11 = 11, | ||
/// The thirteenth button | ||
Button12 = 12, | ||
/// The fourteenth button | ||
Button13 = 13, | ||
/// The fifteenth button | ||
Button14 = 14, | ||
/// The sixteenth button | ||
Button15 = 15, | ||
Button16 = 16, | ||
Button17 = 17, | ||
Button18 = 18, | ||
Button19 = 19, | ||
Button20 = 20, | ||
Button21 = 21, | ||
Button22 = 22, | ||
Button23 = 23, | ||
Button24 = 24, | ||
Button25 = 25, | ||
Button26 = 26, | ||
Button27 = 27, | ||
Button28 = 28, | ||
Button29 = 29, | ||
Button30 = 30, | ||
Button31 = 31, | ||
} | ||
|
||
impl MouseButton { | ||
/// Construct from a `u8` if within the range `0..=31` | ||
pub fn try_from_u8(b: u8) -> Option<MouseButton> { | ||
Comment on lines
+1108
to
+1110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative would be to use the |
||
Some(match b { | ||
0 => MouseButton::Left, | ||
1 => MouseButton::Right, | ||
2 => MouseButton::Middle, | ||
3 => MouseButton::Back, | ||
4 => MouseButton::Forward, | ||
5 => MouseButton::Button5, | ||
6 => MouseButton::Button6, | ||
7 => MouseButton::Button7, | ||
8 => MouseButton::Button8, | ||
9 => MouseButton::Button9, | ||
10 => MouseButton::Button10, | ||
11 => MouseButton::Button11, | ||
12 => MouseButton::Button12, | ||
13 => MouseButton::Button13, | ||
14 => MouseButton::Button14, | ||
15 => MouseButton::Button15, | ||
16 => MouseButton::Button16, | ||
17 => MouseButton::Button17, | ||
18 => MouseButton::Button18, | ||
19 => MouseButton::Button19, | ||
20 => MouseButton::Button20, | ||
21 => MouseButton::Button21, | ||
22 => MouseButton::Button22, | ||
23 => MouseButton::Button23, | ||
24 => MouseButton::Button24, | ||
25 => MouseButton::Button25, | ||
26 => MouseButton::Button26, | ||
27 => MouseButton::Button27, | ||
28 => MouseButton::Button28, | ||
29 => MouseButton::Button29, | ||
30 => MouseButton::Button30, | ||
31 => MouseButton::Button31, | ||
_ => return None, | ||
}) | ||
} | ||
} | ||
|
||
/// Describes a difference in the mouse scroll wheel state. | ||
|
@@ -1188,7 +1276,7 @@ mod tests { | |
primary: true, | ||
state: event::ElementState::Pressed, | ||
position: (0, 0).into(), | ||
button: event::MouseButton::Other(0).into(), | ||
button: event::ButtonSource::Unknown(0), | ||
}); | ||
with_window_event(PointerButton { | ||
device_id: None, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ButtonSource::mouse_button
now returnsNone
onButtonSource::Unknown
(no other reasonable option).