|
1 | | -/* |
2 | | - * Copyright (c) Microsoft Corporation. |
3 | | - * |
4 | | - * This source code is licensed under the MIT license found in the |
5 | | - * LICENSE file in the root directory of this source tree. |
6 | | - */ |
7 | | - |
8 | | - // [macOS] |
9 | | - |
10 | | -#include <react/renderer/components/view/HostPlatformViewEventEmitter.h> |
11 | | -#include <react/renderer/components/view/KeyEvent.h> |
12 | | - |
13 | | -namespace facebook::react { |
14 | | - |
15 | | -#pragma mark - Focus Events |
16 | | - |
17 | | -void HostPlatformViewEventEmitter::onFocus() const { |
18 | | - dispatchEvent("focus"); |
19 | | -} |
20 | | - |
21 | | -void HostPlatformViewEventEmitter::onBlur() const { |
22 | | - dispatchEvent("blur"); |
23 | | -} |
24 | | - |
25 | | -#pragma mark - Keyboard Events |
26 | | - |
27 | | -static jsi::Value keyEventPayload(jsi::Runtime& runtime, const KeyEvent& event) { |
28 | | - auto payload = jsi::Object(runtime); |
29 | | - payload.setProperty(runtime, "key", jsi::String::createFromUtf8(runtime, event.key)); |
30 | | - payload.setProperty(runtime, "ctrlKey", event.ctrlKey); |
31 | | - payload.setProperty(runtime, "shiftKey", event.shiftKey); |
32 | | - payload.setProperty(runtime, "altKey", event.altKey); |
33 | | - payload.setProperty(runtime, "metaKey", event.metaKey); |
34 | | - payload.setProperty(runtime, "capsLockKey", event.capsLockKey); |
35 | | - payload.setProperty(runtime, "numericPadKey", event.numericPadKey); |
36 | | - payload.setProperty(runtime, "helpKey", event.helpKey); |
37 | | - payload.setProperty(runtime, "functionKey", event.functionKey); |
38 | | - return payload; |
39 | | -}; |
40 | | - |
41 | | -void HostPlatformViewEventEmitter::onKeyDown(const KeyEvent& keyEvent) const { |
42 | | - dispatchEvent("keyDown", [keyEvent](jsi::Runtime& runtime) { |
43 | | - return keyEventPayload(runtime, keyEvent); |
44 | | - }); |
45 | | -} |
46 | | - |
47 | | -void HostPlatformViewEventEmitter::onKeyUp(const KeyEvent& keyEvent) const { |
48 | | - dispatchEvent("keyUp", [keyEvent](jsi::Runtime& runtime) { |
49 | | - return keyEventPayload(runtime, keyEvent); |
50 | | - }); |
51 | | -} |
52 | | - |
53 | | -#pragma mark - Mouse Events |
54 | | - |
55 | | -// Returns an Object instead of value as we read and modify it in dragEventPayload. |
56 | | -static jsi::Object mouseEventPayload(jsi::Runtime& runtime, const MouseEvent& event) { |
57 | | - auto payload = jsi::Object(runtime); |
58 | | - payload.setProperty(runtime, "clientX", event.clientX); |
59 | | - payload.setProperty(runtime, "clientY", event.clientY); |
60 | | - payload.setProperty(runtime, "screenX", event.screenX); |
61 | | - payload.setProperty(runtime, "screenY", event.screenY); |
62 | | - payload.setProperty(runtime, "altKey", event.altKey); |
63 | | - payload.setProperty(runtime, "ctrlKey", event.ctrlKey); |
64 | | - payload.setProperty(runtime, "shiftKey", event.shiftKey); |
65 | | - payload.setProperty(runtime, "metaKey", event.metaKey); |
66 | | - return payload; |
67 | | -}; |
68 | | - |
69 | | -void HostPlatformViewEventEmitter::onMouseEnter(const MouseEvent& mouseEvent) const { |
70 | | - dispatchEvent("mouseEnter", [mouseEvent](jsi::Runtime &runtime) { |
71 | | - return mouseEventPayload(runtime, mouseEvent); |
72 | | - }); |
73 | | -} |
74 | | - |
75 | | -void HostPlatformViewEventEmitter::onMouseLeave(const MouseEvent& mouseEvent) const { |
76 | | - dispatchEvent("mouseLeave", [mouseEvent](jsi::Runtime &runtime) { |
77 | | - return mouseEventPayload(runtime, mouseEvent); |
78 | | - }); |
79 | | -} |
80 | | - |
81 | | -#pragma mark - Drag and Drop Events |
82 | | - |
83 | | -static jsi::Value dataTransferPayload( |
84 | | - jsi::Runtime& runtime, |
85 | | - const std::vector<DataTransferItem>& dataTransferItems) { |
86 | | - auto filesArray = jsi::Array(runtime, dataTransferItems.size()); |
87 | | - auto itemsArray = jsi::Array(runtime, dataTransferItems.size()); |
88 | | - auto typesArray = jsi::Array(runtime, dataTransferItems.size()); |
89 | | - int i = 0; |
90 | | - for (const auto& transferItem : dataTransferItems) { |
91 | | - auto fileObject = jsi::Object(runtime); |
92 | | - fileObject.setProperty(runtime, "name", transferItem.name); |
93 | | - fileObject.setProperty(runtime, "type", transferItem.type); |
94 | | - fileObject.setProperty(runtime, "uri", transferItem.uri); |
95 | | - if (transferItem.size.has_value()) { |
96 | | - fileObject.setProperty(runtime, "size", *transferItem.size); |
97 | | - } |
98 | | - if (transferItem.width.has_value()) { |
99 | | - fileObject.setProperty(runtime, "width", *transferItem.width); |
100 | | - } |
101 | | - if (transferItem.height.has_value()) { |
102 | | - fileObject.setProperty(runtime, "height", *transferItem.height); |
103 | | - } |
104 | | - filesArray.setValueAtIndex(runtime, i, fileObject); |
105 | | - |
106 | | - auto itemObject = jsi::Object(runtime); |
107 | | - itemObject.setProperty(runtime, "kind", transferItem.kind); |
108 | | - itemObject.setProperty(runtime, "type", transferItem.type); |
109 | | - itemsArray.setValueAtIndex(runtime, i, itemObject); |
110 | | - |
111 | | - typesArray.setValueAtIndex(runtime, i, transferItem.type); |
112 | | - i++; |
113 | | - } |
114 | | - |
115 | | - auto dataTransferObject = jsi::Object(runtime); |
116 | | - dataTransferObject.setProperty(runtime, "files", filesArray); |
117 | | - dataTransferObject.setProperty(runtime, "items", itemsArray); |
118 | | - dataTransferObject.setProperty(runtime, "types", typesArray); |
119 | | - |
120 | | - return dataTransferObject; |
121 | | -} |
122 | | - |
123 | | -static jsi::Value dragEventPayload( |
124 | | - jsi::Runtime& runtime, |
125 | | - const DragEvent& event) { |
126 | | - auto payload = mouseEventPayload(runtime, event); |
127 | | - auto dataTransferObject = |
128 | | - dataTransferPayload(runtime, event.dataTransferItems); |
129 | | - payload.setProperty(runtime, "dataTransfer", dataTransferObject); |
130 | | - return payload; |
131 | | -} |
132 | | - |
133 | | -void HostPlatformViewEventEmitter::onDragEnter(DragEvent const& dragEvent) const { |
134 | | - dispatchEvent("dragEnter", [dragEvent](jsi::Runtime &runtime) { |
135 | | - return dragEventPayload(runtime, dragEvent); |
136 | | - }); |
137 | | -} |
138 | | - |
139 | | -void HostPlatformViewEventEmitter::onDragLeave(DragEvent const& dragEvent) const { |
140 | | - dispatchEvent("dragLeave", [dragEvent](jsi::Runtime &runtime) { |
141 | | - return dragEventPayload(runtime, dragEvent); |
142 | | - }); |
143 | | -} |
144 | | - |
145 | | -void HostPlatformViewEventEmitter::onDrop(DragEvent const& dragEvent) const { |
146 | | - dispatchEvent("drop", [dragEvent](jsi::Runtime &runtime) { |
147 | | - return dragEventPayload(runtime, dragEvent); |
148 | | - }); |
149 | | -} |
150 | | - |
151 | | -} // namespace facebook::react |
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | + // [macOS] |
| 9 | + |
| 10 | +#include <react/renderer/components/view/HostPlatformViewEventEmitter.h> |
| 11 | +#include <react/renderer/components/view/KeyEvent.h> |
| 12 | + |
| 13 | +namespace facebook::react { |
| 14 | + |
| 15 | +#pragma mark - Focus Events |
| 16 | + |
| 17 | +void HostPlatformViewEventEmitter::onFocus() const { |
| 18 | + dispatchEvent("focus"); |
| 19 | +} |
| 20 | + |
| 21 | +void HostPlatformViewEventEmitter::onBlur() const { |
| 22 | + dispatchEvent("blur"); |
| 23 | +} |
| 24 | + |
| 25 | +#pragma mark - Keyboard Events |
| 26 | + |
| 27 | +static jsi::Value keyEventPayload(jsi::Runtime& runtime, const KeyEvent& event) { |
| 28 | + auto payload = jsi::Object(runtime); |
| 29 | + payload.setProperty(runtime, "key", jsi::String::createFromUtf8(runtime, event.key)); |
| 30 | + payload.setProperty(runtime, "ctrlKey", event.ctrlKey); |
| 31 | + payload.setProperty(runtime, "shiftKey", event.shiftKey); |
| 32 | + payload.setProperty(runtime, "altKey", event.altKey); |
| 33 | + payload.setProperty(runtime, "metaKey", event.metaKey); |
| 34 | + payload.setProperty(runtime, "capsLockKey", event.capsLockKey); |
| 35 | + payload.setProperty(runtime, "numericPadKey", event.numericPadKey); |
| 36 | + payload.setProperty(runtime, "helpKey", event.helpKey); |
| 37 | + payload.setProperty(runtime, "functionKey", event.functionKey); |
| 38 | + return payload; |
| 39 | +}; |
| 40 | + |
| 41 | +void HostPlatformViewEventEmitter::onKeyDown(const KeyEvent& keyEvent) const { |
| 42 | + dispatchEvent("keyDown", [keyEvent](jsi::Runtime& runtime) { |
| 43 | + return keyEventPayload(runtime, keyEvent); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +void HostPlatformViewEventEmitter::onKeyUp(const KeyEvent& keyEvent) const { |
| 48 | + dispatchEvent("keyUp", [keyEvent](jsi::Runtime& runtime) { |
| 49 | + return keyEventPayload(runtime, keyEvent); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +#pragma mark - Mouse Events |
| 54 | + |
| 55 | +static jsi::Value mouseEventPayload(jsi::Runtime& runtime, const MouseEvent& event) { |
| 56 | + auto payload = jsi::Object(runtime); |
| 57 | + payload.setProperty(runtime, "clientX", event.clientX); |
| 58 | + payload.setProperty(runtime, "clientY", event.clientY); |
| 59 | + payload.setProperty(runtime, "screenX", event.screenX); |
| 60 | + payload.setProperty(runtime, "screenY", event.screenY); |
| 61 | + payload.setProperty(runtime, "altKey", event.altKey); |
| 62 | + payload.setProperty(runtime, "ctrlKey", event.ctrlKey); |
| 63 | + payload.setProperty(runtime, "shiftKey", event.shiftKey); |
| 64 | + payload.setProperty(runtime, "metaKey", event.metaKey); |
| 65 | + return payload; |
| 66 | +}; |
| 67 | + |
| 68 | +void HostPlatformViewEventEmitter::onMouseEnter(const MouseEvent& mouseEvent) const { |
| 69 | + dispatchEvent("mouseEnter", [mouseEvent](jsi::Runtime &runtime) { |
| 70 | + return mouseEventPayload(runtime, mouseEvent); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +void HostPlatformViewEventEmitter::onMouseLeave(const MouseEvent& mouseEvent) const { |
| 75 | + dispatchEvent("mouseLeave", [mouseEvent](jsi::Runtime &runtime) { |
| 76 | + return mouseEventPayload(runtime, mouseEvent); |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace facebook::react |
0 commit comments