@@ -521,14 +521,14 @@ pub enum ButtonSource {
521521}
522522
523523impl ButtonSource {
524- /// Convert any [`ButtonSource`] to an equivalent [`MouseButton`]. If a pointer type has no
524+ /// Try to convert a [`ButtonSource`] to an equivalent [`MouseButton`]. If a pointer type has no
525525 /// special handling in an application, this method can be used to handle it like any generic
526526 /// mouse input.
527- pub fn mouse_button ( self ) -> MouseButton {
527+ pub fn mouse_button ( self ) -> Option < MouseButton > {
528528 match self {
529- ButtonSource :: Mouse ( mouse) => mouse,
530- ButtonSource :: Touch { .. } => MouseButton :: Left ,
531- ButtonSource :: Unknown ( code ) => MouseButton :: Other ( code ) ,
529+ ButtonSource :: Mouse ( mouse) => Some ( mouse) ,
530+ ButtonSource :: Touch { .. } => Some ( MouseButton :: Left ) ,
531+ ButtonSource :: Unknown ( _ ) => None ,
532532 }
533533 }
534534}
@@ -1037,21 +1037,113 @@ impl ElementState {
10371037 }
10381038}
10391039
1040- /// Describes a button of a mouse controller.
1040+ /// Identifies a button of a mouse controller.
10411041///
10421042/// ## Platform-specific
10431043///
1044- /// **macOS:** `Back` and `Forward` might not work with all hardware.
1045- /// **Orbital:** `Back` and `Forward` are unsupported due to orbital not supporting them.
1044+ /// The first three buttons should be supported on all platforms.
1045+ /// [`Self::Back`] and [`Self::Forward`] are supported on most platforms
1046+ /// (when using a compatible mouse).
1047+ ///
1048+ /// - **Android, iOS:** Currently not supported.
1049+ /// - **Orbital:** Only left/right/middle buttons are supported at this time.
1050+ /// - **Web, Windows:** Supports left/right/middle/back/forward buttons.
1051+ /// - **Wayland:** Supports buttons 0..=15.
1052+ /// - **macOS, X11:** Supports all button variants.
1053+ /// - **X11:** Supports buttons 0..=250.
10461054#[ derive( Debug , Hash , PartialEq , Eq , PartialOrd , Ord , Clone , Copy ) ]
10471055#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
1056+ #[ repr( u8 ) ]
10481057pub enum MouseButton {
1049- Left ,
1050- Right ,
1051- Middle ,
1052- Back ,
1053- Forward ,
1054- Other ( u16 ) ,
1058+ /// The primary (usually left) button
1059+ Left = 0 ,
1060+ /// The secondary (usually right) button
1061+ Right = 1 ,
1062+ /// The tertiary (usually middle) button
1063+ Middle = 2 ,
1064+ /// The first side button, frequently assigned a back function
1065+ Back = 3 ,
1066+ /// The second side button, frequently assigned a forward function
1067+ Forward = 4 ,
1068+ /// The sixth button
1069+ Button5 = 5 ,
1070+ /// The seventh button
1071+ Button6 = 6 ,
1072+ /// The eighth button
1073+ Button7 = 7 ,
1074+ /// The ninth button
1075+ Button8 = 8 ,
1076+ /// The tenth button
1077+ Button9 = 9 ,
1078+ /// The eleventh button
1079+ Button10 = 10 ,
1080+ /// The twelfth button
1081+ Button11 = 11 ,
1082+ /// The thirteenth button
1083+ Button12 = 12 ,
1084+ /// The fourteenth button
1085+ Button13 = 13 ,
1086+ /// The fifteenth button
1087+ Button14 = 14 ,
1088+ /// The sixteenth button
1089+ Button15 = 15 ,
1090+ Button16 = 16 ,
1091+ Button17 = 17 ,
1092+ Button18 = 18 ,
1093+ Button19 = 19 ,
1094+ Button20 = 20 ,
1095+ Button21 = 21 ,
1096+ Button22 = 22 ,
1097+ Button23 = 23 ,
1098+ Button24 = 24 ,
1099+ Button25 = 25 ,
1100+ Button26 = 26 ,
1101+ Button27 = 27 ,
1102+ Button28 = 28 ,
1103+ Button29 = 29 ,
1104+ Button30 = 30 ,
1105+ Button31 = 31 ,
1106+ }
1107+
1108+ impl MouseButton {
1109+ /// Construct from a `u8` if within the range `0..=31`
1110+ pub fn try_from_u8 ( b : u8 ) -> Option < MouseButton > {
1111+ Some ( match b {
1112+ 0 => MouseButton :: Left ,
1113+ 1 => MouseButton :: Right ,
1114+ 2 => MouseButton :: Middle ,
1115+ 3 => MouseButton :: Back ,
1116+ 4 => MouseButton :: Forward ,
1117+ 5 => MouseButton :: Button5 ,
1118+ 6 => MouseButton :: Button6 ,
1119+ 7 => MouseButton :: Button7 ,
1120+ 8 => MouseButton :: Button8 ,
1121+ 9 => MouseButton :: Button9 ,
1122+ 10 => MouseButton :: Button10 ,
1123+ 11 => MouseButton :: Button11 ,
1124+ 12 => MouseButton :: Button12 ,
1125+ 13 => MouseButton :: Button13 ,
1126+ 14 => MouseButton :: Button14 ,
1127+ 15 => MouseButton :: Button15 ,
1128+ 16 => MouseButton :: Button16 ,
1129+ 17 => MouseButton :: Button17 ,
1130+ 18 => MouseButton :: Button18 ,
1131+ 19 => MouseButton :: Button19 ,
1132+ 20 => MouseButton :: Button20 ,
1133+ 21 => MouseButton :: Button21 ,
1134+ 22 => MouseButton :: Button22 ,
1135+ 23 => MouseButton :: Button23 ,
1136+ 24 => MouseButton :: Button24 ,
1137+ 25 => MouseButton :: Button25 ,
1138+ 26 => MouseButton :: Button26 ,
1139+ 27 => MouseButton :: Button27 ,
1140+ 28 => MouseButton :: Button28 ,
1141+ 29 => MouseButton :: Button29 ,
1142+ 30 => MouseButton :: Button30 ,
1143+ 31 => MouseButton :: Button31 ,
1144+ _ => return None ,
1145+ } )
1146+ }
10551147}
10561148
10571149/// Describes a difference in the mouse scroll wheel state.
@@ -1184,7 +1276,7 @@ mod tests {
11841276 primary: true ,
11851277 state: event:: ElementState :: Pressed ,
11861278 position: ( 0 , 0 ) . into( ) ,
1187- button: event:: MouseButton :: Other ( 0 ) . into ( ) ,
1279+ button: event:: ButtonSource :: Unknown ( 0 ) ,
11881280 } ) ;
11891281 with_window_event( PointerButton {
11901282 device_id: None ,
0 commit comments