@@ -531,26 +531,22 @@ pub enum ButtonSource {
531531 button : TabletToolButton ,
532532 data : TabletToolData ,
533533 } ,
534+ /// A pointer button of unknown source.
535+ ///
536+ /// Codes are undefined and may not be reproducible across platforms or winit versions.
534537 Unknown ( u16 ) ,
535538}
536539
537540impl ButtonSource {
538- /// Convert any [`ButtonSource`] to an equivalent [`MouseButton`]. If a pointer type has no
541+ /// Try to convert a [`ButtonSource`] to an equivalent [`MouseButton`]. If a pointer type has no
539542 /// special handling in an application, this method can be used to handle it like any generic
540543 /// mouse input.
541- pub fn mouse_button ( self ) -> MouseButton {
544+ pub fn mouse_button ( self ) -> Option < MouseButton > {
542545 match self {
543- ButtonSource :: Mouse ( mouse) => mouse,
544- ButtonSource :: Touch { .. } => MouseButton :: Left ,
546+ ButtonSource :: Mouse ( mouse) => Some ( mouse) ,
547+ ButtonSource :: Touch { .. } => Some ( MouseButton :: Left ) ,
545548 ButtonSource :: TabletTool { button, .. } => button. into ( ) ,
546- ButtonSource :: Unknown ( button) => match button {
547- 0 => MouseButton :: Left ,
548- 1 => MouseButton :: Middle ,
549- 2 => MouseButton :: Right ,
550- 3 => MouseButton :: Back ,
551- 4 => MouseButton :: Forward ,
552- _ => MouseButton :: Other ( button) ,
553- } ,
549+ ButtonSource :: Unknown ( _) => None ,
554550 }
555551 }
556552}
@@ -1323,21 +1319,114 @@ impl ElementState {
13231319 }
13241320}
13251321
1326- /// Describes a button of a mouse controller.
1322+ /// Identifies a button of a mouse controller.
13271323///
13281324/// ## Platform-specific
13291325///
1330- /// **macOS:** `Back` and `Forward` might not work with all hardware.
1331- /// **Orbital:** `Back` and `Forward` are unsupported due to orbital not supporting them.
1326+ /// The first three buttons should be supported on all platforms.
1327+ /// [`Self::Back`] and [`Self::Forward`] are supported on most platforms
1328+ /// (when using a compatible mouse).
1329+ ///
1330+ /// - **Android, iOS:** Currently not supported.
1331+ /// - **Orbital:** Only left/right/middle buttons are supported at this time.
1332+ /// - **Web, Windows:** Supports left/right/middle/back/forward buttons.
1333+ /// - **Wayland:** Supports buttons 0..=15.
1334+ /// - **macOS:** Supports all button variants.
1335+ /// - **X11:** Technically supports further buttons than this (0..=250), these are emitted in
1336+ /// `ButtonSource::Unknown`.
13321337#[ derive( Debug , Hash , PartialEq , Eq , PartialOrd , Ord , Clone , Copy ) ]
13331338#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
1339+ #[ repr( u8 ) ]
13341340pub enum MouseButton {
1335- Left ,
1336- Right ,
1337- Middle ,
1338- Back ,
1339- Forward ,
1340- Other ( u16 ) ,
1341+ /// The primary (usually left) button
1342+ Left = 0 ,
1343+ /// The secondary (usually right) button
1344+ Right = 1 ,
1345+ /// The tertiary (usually middle) button
1346+ Middle = 2 ,
1347+ /// The first side button, frequently assigned a back function
1348+ Back = 3 ,
1349+ /// The second side button, frequently assigned a forward function
1350+ Forward = 4 ,
1351+ /// The sixth button
1352+ Button6 = 5 ,
1353+ /// The seventh button
1354+ Button7 = 6 ,
1355+ /// The eighth button
1356+ Button8 = 7 ,
1357+ /// The ninth button
1358+ Button9 = 8 ,
1359+ /// The tenth button
1360+ Button10 = 9 ,
1361+ /// The eleventh button
1362+ Button11 = 10 ,
1363+ /// The twelfth button
1364+ Button12 = 11 ,
1365+ /// The thirteenth button
1366+ Button13 = 12 ,
1367+ /// The fourteenth button
1368+ Button14 = 13 ,
1369+ /// The fifteenth button
1370+ Button15 = 14 ,
1371+ /// The sixteenth button
1372+ Button16 = 15 ,
1373+ Button17 = 16 ,
1374+ Button18 = 17 ,
1375+ Button19 = 18 ,
1376+ Button20 = 19 ,
1377+ Button21 = 20 ,
1378+ Button22 = 21 ,
1379+ Button23 = 22 ,
1380+ Button24 = 23 ,
1381+ Button25 = 24 ,
1382+ Button26 = 25 ,
1383+ Button27 = 26 ,
1384+ Button28 = 27 ,
1385+ Button29 = 28 ,
1386+ Button30 = 29 ,
1387+ Button31 = 30 ,
1388+ Button32 = 31 ,
1389+ }
1390+
1391+ impl MouseButton {
1392+ /// Construct from a `u8` if within the range `0..=31`
1393+ pub fn try_from_u8 ( b : u8 ) -> Option < MouseButton > {
1394+ Some ( match b {
1395+ 0 => MouseButton :: Left ,
1396+ 1 => MouseButton :: Right ,
1397+ 2 => MouseButton :: Middle ,
1398+ 3 => MouseButton :: Back ,
1399+ 4 => MouseButton :: Forward ,
1400+ 5 => MouseButton :: Button6 ,
1401+ 6 => MouseButton :: Button7 ,
1402+ 7 => MouseButton :: Button8 ,
1403+ 8 => MouseButton :: Button9 ,
1404+ 9 => MouseButton :: Button10 ,
1405+ 10 => MouseButton :: Button11 ,
1406+ 11 => MouseButton :: Button12 ,
1407+ 12 => MouseButton :: Button13 ,
1408+ 13 => MouseButton :: Button14 ,
1409+ 14 => MouseButton :: Button15 ,
1410+ 15 => MouseButton :: Button16 ,
1411+ 16 => MouseButton :: Button17 ,
1412+ 17 => MouseButton :: Button18 ,
1413+ 18 => MouseButton :: Button19 ,
1414+ 19 => MouseButton :: Button20 ,
1415+ 20 => MouseButton :: Button21 ,
1416+ 21 => MouseButton :: Button22 ,
1417+ 22 => MouseButton :: Button23 ,
1418+ 23 => MouseButton :: Button24 ,
1419+ 24 => MouseButton :: Button25 ,
1420+ 25 => MouseButton :: Button26 ,
1421+ 26 => MouseButton :: Button27 ,
1422+ 27 => MouseButton :: Button28 ,
1423+ 28 => MouseButton :: Button29 ,
1424+ 29 => MouseButton :: Button30 ,
1425+ 30 => MouseButton :: Button31 ,
1426+ 31 => MouseButton :: Button32 ,
1427+ _ => return None ,
1428+ } )
1429+ }
13411430}
13421431
13431432/// Describes a button of a tool, e.g. a pen.
@@ -1349,16 +1438,16 @@ pub enum TabletToolButton {
13491438 Other ( u16 ) ,
13501439}
13511440
1352- impl From < TabletToolButton > for MouseButton {
1441+ impl From < TabletToolButton > for Option < MouseButton > {
13531442 fn from ( tool : TabletToolButton ) -> Self {
1354- match tool {
1443+ Some ( match tool {
13551444 TabletToolButton :: Contact => MouseButton :: Left ,
13561445 TabletToolButton :: Barrel => MouseButton :: Right ,
13571446 TabletToolButton :: Other ( 1 ) => MouseButton :: Middle ,
13581447 TabletToolButton :: Other ( 3 ) => MouseButton :: Back ,
13591448 TabletToolButton :: Other ( 4 ) => MouseButton :: Forward ,
1360- TabletToolButton :: Other ( other ) => MouseButton :: Other ( other ) ,
1361- }
1449+ TabletToolButton :: Other ( _ ) => return None ,
1450+ } )
13621451 }
13631452}
13641453
@@ -1492,7 +1581,7 @@ mod tests {
14921581 primary: true ,
14931582 state: event:: ElementState :: Pressed ,
14941583 position: ( 0 , 0 ) . into( ) ,
1495- button: event:: MouseButton :: Other ( 0 ) . into ( ) ,
1584+ button: event:: ButtonSource :: Unknown ( 0 ) ,
14961585 } ) ;
14971586 with_window_event( PointerButton {
14981587 device_id: None ,
0 commit comments