Commit 41fbd37
[iOS] Distinguish between mouse and stylus when hovering (#3991)
## Description
<!--
Description and motivation for this PR.
Include 'Fixes #<number>' if this is fixing some issue.
-->
Improves the hover gesture on iOS by distinguishing between a mouse and
a stylus, if possible.
With these changes the handler will now assume a mouse (just like
previously with the enum bug), but if `zOffset` is available (iOS 16.1)
and it is over 0.0 it will instead be recognised as styus.
~*Maybe it makes more sense to invert the logic and assume a mouse as
default, and stylus if > 0. Let me know what you think.*~
See #3977 for more context.
Fixes #3977
## Test plan
<!--
Describe how did you test this change here.
-->
Here's a small test page that can be added to the basic-example app for
testing;
```jsx
import { useState } from 'react';
import { View, Text } from 'react-native';
import {
GestureDetector,
GestureHandlerRootView,
useHoverGesture,
useSimultaneousGestures,
useTapGesture,
} from 'react-native-gesture-handler';
import { runOnJS } from 'react-native-worklets';
const POINTER_TYPES: Record<number, string> = {
0: "TOUCH",
1: "STYLUS",
2: "MOUSE",
3: "KEY",
4: "OTHER",
}
export default function Hover() {
const [hoverPointerType, setHoverPointerType] = useState(-1);
const [tapPointerType, setTapPointerType] = useState(-1);
const hoverGesture = useHoverGesture({
onUpdate: (event) => {
runOnJS(setHoverPointerType)(event.pointerType);
},
});
const tapGesture = useTapGesture({
onFinalize: (event) => {
runOnJS(setTapPointerType)(event.pointerType);
},
});
const gesture = useSimultaneousGestures(hoverGesture, tapGesture);
return (
<GestureHandlerRootView
style={{
flex: 1,
backgroundColor: 'white',
justifyContent: 'center',
alignItems: 'center',
}}>
<Text>Hover pointer type: {POINTER_TYPES[hoverPointerType] ?? "N/A"}</Text>
<Text>Tap pointer type: {POINTER_TYPES[tapPointerType] ?? "N/A"}</Text>
<GestureDetector gesture={gesture}>
<View
style={{ width: 200, height: 200, backgroundColor: 'salmon' }}></View>
</GestureDetector>
</GestureHandlerRootView>
);
}
```
Mouse is testable in the iPad simulator by enabling `I/O -> Input ->
Send Pointer to Device` in the top menus.
Mouse is also testable on a real iPad by Linking keyboard and mouse to
it from a mac in the Displays settings.
Apple Pencil is only testable with an actual pencil on a real iPad.
---------
Co-authored-by: Michał <michal.bert@swmansion.com>1 parent 7406046 commit 41fbd37
12 files changed
Lines changed: 41 additions & 14 deletions
File tree
- packages/react-native-gesture-handler/apple
- Handlers
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
Lines changed: 23 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
50 | 59 | | |
51 | 60 | | |
52 | 61 | | |
| |||
153 | 162 | | |
154 | 163 | | |
155 | 164 | | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
156 | 176 | | |
157 | 177 | | |
158 | 178 | | |
159 | 179 | | |
160 | | - | |
| 180 | + | |
161 | 181 | | |
162 | 182 | | |
163 | 183 | | |
| |||
173 | 193 | | |
174 | 194 | | |
175 | 195 | | |
| 196 | + | |
176 | 197 | | |
177 | 198 | | |
178 | 199 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
| 90 | + | |
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
173 | | - | |
| 173 | + | |
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
238 | 238 | | |
239 | 239 | | |
240 | 240 | | |
241 | | - | |
| 241 | + | |
242 | 242 | | |
243 | 243 | | |
244 | 244 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
194 | | - | |
| 194 | + | |
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| |||
0 commit comments