Skip to main content

ui_events_floem_winit/
pointer.rs

1// Copyright 2025 the UI Events Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! Support routines for converting pointer data from [`winit`].
5
6use ui_events::pointer::PointerButton;
7use winit::event::MouseButton;
8
9/// Try to make a [`PointerButton`] from a [`MouseButton`].
10///
11/// Because values of [`MouseButton::Other`] can start at 0, they are mapped
12/// to the arbitrary buttons B7..B32.
13/// Values greater than 25 will not be mapped.
14pub fn try_from_winit_button(b: MouseButton) -> Option<PointerButton> {
15    Some(match b {
16        MouseButton::Left => PointerButton::Primary,
17        MouseButton::Right => PointerButton::Secondary,
18        MouseButton::Middle => PointerButton::Auxiliary,
19        MouseButton::Back => PointerButton::X1,
20        MouseButton::Forward => PointerButton::X2,
21        MouseButton::Other(u) => match u {
22            6 => PointerButton::B7,
23            7 => PointerButton::B8,
24            8 => PointerButton::B9,
25            9 => PointerButton::B10,
26            10 => PointerButton::B11,
27            11 => PointerButton::B12,
28            12 => PointerButton::B13,
29            13 => PointerButton::B14,
30            14 => PointerButton::B15,
31            15 => PointerButton::B16,
32            16 => PointerButton::B17,
33            17 => PointerButton::B18,
34            18 => PointerButton::B19,
35            19 => PointerButton::B20,
36            20 => PointerButton::B21,
37            21 => PointerButton::B22,
38            22 => PointerButton::B23,
39            23 => PointerButton::B24,
40            24 => PointerButton::B25,
41            25 => PointerButton::B26,
42            26 => PointerButton::B27,
43            27 => PointerButton::B28,
44            28 => PointerButton::B29,
45            29 => PointerButton::B30,
46            30 => PointerButton::B31,
47            31 => PointerButton::B32,
48            _ => {
49                return None;
50            }
51        },
52    })
53}