@ng-native/desktop 0.0.0-stage → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/app.plugin.cjs +17 -0
- package/bin/app-config.mjs +168 -0
- package/bin/autolink.mjs +224 -0
- package/bin/cli.mjs +158 -0
- package/bin/host.mjs +427 -0
- package/bin/ng-native-desktop.mjs +4 -0
- package/bin/package.mjs +971 -0
- package/bin/plist.mjs +68 -0
- package/bin/prebuilt.mjs +328 -0
- package/bin/toolchain.mjs +192 -0
- package/native/CMakeLists.txt +276 -0
- package/native/appkit/CMakeLists.txt +109 -0
- package/native/appkit/address-search.h +26 -0
- package/native/appkit/address-search.mm +277 -0
- package/native/appkit/animation-frames.h +14 -0
- package/native/appkit/animation-frames.mm +106 -0
- package/native/appkit/app-info.h +12 -0
- package/native/appkit/app-info.mm +98 -0
- package/native/appkit/app-updater.mm +128 -0
- package/native/appkit/app-windows.h +81 -0
- package/native/appkit/app-windows.mm +318 -0
- package/native/appkit/appkit-backend.h +73 -0
- package/native/appkit/appkit-backend.mm +276 -0
- package/native/appkit/apple-authentication.h +19 -0
- package/native/appkit/apple-authentication.mm +248 -0
- package/native/appkit/apple-button-view.h +10 -0
- package/native/appkit/apple-button-view.mm +37 -0
- package/native/appkit/apple-id-button.h +16 -0
- package/native/appkit/apple-id-button.mm +50 -0
- package/native/appkit/audio.h +16 -0
- package/native/appkit/audio.mm +275 -0
- package/native/appkit/battery.h +13 -0
- package/native/appkit/battery.mm +92 -0
- package/native/appkit/bookmarks.h +17 -0
- package/native/appkit/bookmarks.mm +145 -0
- package/native/appkit/calendar.h +25 -0
- package/native/appkit/calendar.mm +660 -0
- package/native/appkit/camera-view.h +15 -0
- package/native/appkit/camera-view.mm +130 -0
- package/native/appkit/camera.h +39 -0
- package/native/appkit/camera.mm +362 -0
- package/native/appkit/cg-canvas.h +17 -0
- package/native/appkit/cg-canvas.mm +518 -0
- package/native/appkit/component-view.h +17 -0
- package/native/appkit/crypto.h +19 -0
- package/native/appkit/crypto.mm +78 -0
- package/native/appkit/desktop-expo.h +29 -0
- package/native/appkit/desktop-expo.mm +385 -0
- package/native/appkit/expo-image-drawing.h +46 -0
- package/native/appkit/expo-image-drawing.mm +131 -0
- package/native/appkit/expo-image-view.h +15 -0
- package/native/appkit/expo-image-view.mm +182 -0
- package/native/appkit/expo-ui-renderer.h +68 -0
- package/native/appkit/expo-ui-renderer.mm +101 -0
- package/native/appkit/expo-ui-view.h +26 -0
- package/native/appkit/expo-ui-view.mm +134 -0
- package/native/appkit/expo-ui.swift +1284 -0
- package/native/appkit/file-system.h +16 -0
- package/native/appkit/file-system.mm +140 -0
- package/native/appkit/fonts.h +12 -0
- package/native/appkit/fonts.mm +48 -0
- package/native/appkit/geocoder.h +19 -0
- package/native/appkit/geocoder.mm +101 -0
- package/native/appkit/glass-effect.h +22 -0
- package/native/appkit/glass-effect.mm +63 -0
- package/native/appkit/glass-view.h +8 -0
- package/native/appkit/glass-view.mm +65 -0
- package/native/appkit/header-toolbar.h +60 -0
- package/native/appkit/header-toolbar.mm +629 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1200 -0
- package/native/appkit/image-manipulator.h +14 -0
- package/native/appkit/image-manipulator.mm +191 -0
- package/native/appkit/image-request.h +22 -0
- package/native/appkit/image-request.mm +62 -0
- package/native/appkit/image-view.h +8 -0
- package/native/appkit/image-view.mm +142 -0
- package/native/appkit/input-replay.h +25 -0
- package/native/appkit/input-replay.mm +418 -0
- package/native/appkit/language-model.h +14 -0
- package/native/appkit/language-model.mm +99 -0
- package/native/appkit/language-model.swift +472 -0
- package/native/appkit/local-authentication.h +15 -0
- package/native/appkit/local-authentication.mm +133 -0
- package/native/appkit/location.h +16 -0
- package/native/appkit/location.mm +214 -0
- package/native/appkit/main.mm +1515 -0
- package/native/appkit/map-view.h +15 -0
- package/native/appkit/map-view.mm +109 -0
- package/native/appkit/maps.h +35 -0
- package/native/appkit/maps.mm +343 -0
- package/native/appkit/menu-bar.h +22 -0
- package/native/appkit/menu-bar.mm +208 -0
- package/native/appkit/modal-focus.h +16 -0
- package/native/appkit/modal-focus.mm +26 -0
- package/native/appkit/modal-host-view.h +79 -0
- package/native/appkit/modal-host-view.mm +442 -0
- package/native/appkit/native-controls.h +22 -0
- package/native/appkit/native-controls.mm +431 -0
- package/native/appkit/network-state.h +12 -0
- package/native/appkit/network-state.mm +64 -0
- package/native/appkit/network.h +14 -0
- package/native/appkit/network.mm +396 -0
- package/native/appkit/notifications.h +19 -0
- package/native/appkit/notifications.mm +309 -0
- package/native/appkit/photo-library.h +33 -0
- package/native/appkit/photo-library.mm +548 -0
- package/native/appkit/pointer-buttons.h +32 -0
- package/native/appkit/presentation-label.h +15 -0
- package/native/appkit/presentation-label.mm +12 -0
- package/native/appkit/presented-screen.h +19 -0
- package/native/appkit/presented-screen.mm +181 -0
- package/native/appkit/printing.h +42 -0
- package/native/appkit/printing.mm +393 -0
- package/native/appkit/push.h +19 -0
- package/native/appkit/push.mm +81 -0
- package/native/appkit/red-box.h +26 -0
- package/native/appkit/red-box.mm +387 -0
- package/native/appkit/screen-capture.h +21 -0
- package/native/appkit/screen-capture.mm +92 -0
- package/native/appkit/scroll-view.h +13 -0
- package/native/appkit/scroll-view.mm +251 -0
- package/native/appkit/scrollers.h +25 -0
- package/native/appkit/scrollers.mm +43 -0
- package/native/appkit/search-field.h +34 -0
- package/native/appkit/search-field.mm +291 -0
- package/native/appkit/search-toolbar-item.h +19 -0
- package/native/appkit/search-toolbar-item.mm +269 -0
- package/native/appkit/share-picker.h +34 -0
- package/native/appkit/share-picker.mm +121 -0
- package/native/appkit/split-view.h +77 -0
- package/native/appkit/split-view.mm +1206 -0
- package/native/appkit/store-review.h +13 -0
- package/native/appkit/store-review.mm +28 -0
- package/native/appkit/surface-view.h +45 -0
- package/native/appkit/surface-view.mm +558 -0
- package/native/appkit/svg-view.h +17 -0
- package/native/appkit/svg-view.mm +57 -0
- package/native/appkit/symbol-drawing.h +24 -0
- package/native/appkit/symbol-drawing.mm +117 -0
- package/native/appkit/symbol-view.h +8 -0
- package/native/appkit/symbol-view.mm +33 -0
- package/native/appkit/system-access.h +34 -0
- package/native/appkit/system-access.mm +210 -0
- package/native/appkit/system.h +24 -0
- package/native/appkit/system.mm +403 -0
- package/native/appkit/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +76 -0
- package/native/appkit/text-input-field.mm +444 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +477 -0
- package/native/appkit/text-recognition.h +13 -0
- package/native/appkit/text-recognition.mm +102 -0
- package/native/appkit/tracking.h +13 -0
- package/native/appkit/tracking.mm +51 -0
- package/native/appkit/tray-and-dock.h +45 -0
- package/native/appkit/tray-and-dock.mm +130 -0
- package/native/appkit/undo.h +23 -0
- package/native/appkit/undo.mm +125 -0
- package/native/appkit/updates.h +17 -0
- package/native/appkit/updates.mm +71 -0
- package/native/appkit/video-view.h +10 -0
- package/native/appkit/video-view.mm +42 -0
- package/native/appkit/video.h +38 -0
- package/native/appkit/video.mm +404 -0
- package/native/appkit/web-browser.h +12 -0
- package/native/appkit/web-browser.mm +97 -0
- package/native/appkit/web-view-component.h +9 -0
- package/native/appkit/web-view-component.mm +56 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +394 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
- package/native/cmake/libraries/react-native-worklets.cmake +46 -0
- package/native/cmake/libraries.cmake +109 -0
- package/native/cmake/linked-modules.cmake +112 -0
- package/native/cmake/react-native.cmake +175 -0
- package/native/cmake/runtime-dependencies.cmake +34 -0
- package/native/cmake/swift.cmake +43 -0
- package/native/cmake/third-party.cmake +74 -0
- package/native/core/include/ngn/absent-views.h +27 -0
- package/native/core/include/ngn/accessibility.h +140 -0
- package/native/core/include/ngn/address-search.h +69 -0
- package/native/core/include/ngn/animation-frames.h +39 -0
- package/native/core/include/ngn/app-info.h +52 -0
- package/native/core/include/ngn/app-menus.h +146 -0
- package/native/core/include/ngn/app-windows.h +181 -0
- package/native/core/include/ngn/apple-authentication.h +116 -0
- package/native/core/include/ngn/asset.h +28 -0
- package/native/core/include/ngn/audio.h +125 -0
- package/native/core/include/ngn/back-swipe.h +112 -0
- package/native/core/include/ngn/battery.h +48 -0
- package/native/core/include/ngn/bytes.h +59 -0
- package/native/core/include/ngn/calendar.h +180 -0
- package/native/core/include/ngn/camera.h +128 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +105 -0
- package/native/core/include/ngn/crypto.h +38 -0
- package/native/core/include/ngn/default-menu.h +91 -0
- package/native/core/include/ngn/desktop-expo.h +193 -0
- package/native/core/include/ngn/desktop-modal.h +109 -0
- package/native/core/include/ngn/desktop-props.h +75 -0
- package/native/core/include/ngn/desktop-view-props.h +110 -0
- package/native/core/include/ngn/dev-server.h +33 -0
- package/native/core/include/ngn/download.h +24 -0
- package/native/core/include/ngn/expo-image.h +192 -0
- package/native/core/include/ngn/expo-ui.h +133 -0
- package/native/core/include/ngn/file-system.h +274 -0
- package/native/core/include/ngn/focus-order.h +63 -0
- package/native/core/include/ngn/fonts.h +64 -0
- package/native/core/include/ngn/gestures.h +305 -0
- package/native/core/include/ngn/glass.h +67 -0
- package/native/core/include/ngn/header-bar.h +112 -0
- package/native/core/include/ngn/host-state.h +243 -0
- package/native/core/include/ngn/image-manipulator.h +70 -0
- package/native/core/include/ngn/input-dispatcher.h +211 -0
- package/native/core/include/ngn/input-script.h +56 -0
- package/native/core/include/ngn/input.h +112 -0
- package/native/core/include/ngn/key-value-store.h +41 -0
- package/native/core/include/ngn/language-model.h +69 -0
- package/native/core/include/ngn/launch-frame.h +47 -0
- package/native/core/include/ngn/launch-settings.h +98 -0
- package/native/core/include/ngn/layout-animations.h +24 -0
- package/native/core/include/ngn/library-components.h +13 -0
- package/native/core/include/ngn/library-glue.h +53 -0
- package/native/core/include/ngn/live-regions.h +54 -0
- package/native/core/include/ngn/local-authentication.h +74 -0
- package/native/core/include/ngn/location.h +89 -0
- package/native/core/include/ngn/maps.h +226 -0
- package/native/core/include/ngn/module-registry.h +51 -0
- package/native/core/include/ngn/mounting-backend.h +96 -0
- package/native/core/include/ngn/mounting-manager.h +155 -0
- package/native/core/include/ngn/native-controls.h +161 -0
- package/native/core/include/ngn/native-module.h +109 -0
- package/native/core/include/ngn/network-state.h +44 -0
- package/native/core/include/ngn/network.h +100 -0
- package/native/core/include/ngn/notifications.h +185 -0
- package/native/core/include/ngn/photo-library.h +130 -0
- package/native/core/include/ngn/printing.h +69 -0
- package/native/core/include/ngn/processes.h +114 -0
- package/native/core/include/ngn/red-box.h +119 -0
- package/native/core/include/ngn/reload-thread.h +258 -0
- package/native/core/include/ngn/runtime.h +161 -0
- package/native/core/include/ngn/safe-areas.h +77 -0
- package/native/core/include/ngn/screen-capture.h +35 -0
- package/native/core/include/ngn/screen-presentation.h +85 -0
- package/native/core/include/ngn/screen-stacks.h +80 -0
- package/native/core/include/ngn/scrollers.h +106 -0
- package/native/core/include/ngn/sharing.h +39 -0
- package/native/core/include/ngn/spawn-rules.h +78 -0
- package/native/core/include/ngn/splash-screen.h +41 -0
- package/native/core/include/ngn/split-view.h +270 -0
- package/native/core/include/ngn/sqlite.h +38 -0
- package/native/core/include/ngn/store-review.h +26 -0
- package/native/core/include/ngn/surface-book.h +101 -0
- package/native/core/include/ngn/svg.h +393 -0
- package/native/core/include/ngn/symbol.h +90 -0
- package/native/core/include/ngn/system.h +102 -0
- package/native/core/include/ngn/tabs-host.h +171 -0
- package/native/core/include/ngn/text-input.h +125 -0
- package/native/core/include/ngn/text-measurer.h +38 -0
- package/native/core/include/ngn/text-recognition.h +55 -0
- package/native/core/include/ngn/tracking.h +34 -0
- package/native/core/include/ngn/tray-and-dock.h +107 -0
- package/native/core/include/ngn/undo.h +37 -0
- package/native/core/include/ngn/updates.h +253 -0
- package/native/core/include/ngn/video.h +126 -0
- package/native/core/include/ngn/view-box.h +126 -0
- package/native/core/include/ngn/view-transform.h +26 -0
- package/native/core/include/ngn/view-tree.h +85 -0
- package/native/core/include/ngn/web-browser.h +45 -0
- package/native/core/include/ngn/web-view.h +133 -0
- package/native/core/include/ngn/worklet-host.h +98 -0
- package/native/core/src/absent-views.cpp +41 -0
- package/native/core/src/accessibility.cpp +374 -0
- package/native/core/src/animation-frames.cpp +49 -0
- package/native/core/src/app-menus.cpp +352 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +91 -0
- package/native/core/src/bytes.cpp +158 -0
- package/native/core/src/control-sizes.cpp +232 -0
- package/native/core/src/default-menu.cpp +96 -0
- package/native/core/src/desktop-modal.cpp +162 -0
- package/native/core/src/desktop-view-props.cpp +80 -0
- package/native/core/src/dev-server-helper.cpp +152 -0
- package/native/core/src/dev-server.cpp +103 -0
- package/native/core/src/download.cpp +53 -0
- package/native/core/src/expo-image.cpp +548 -0
- package/native/core/src/expo-ui.cpp +203 -0
- package/native/core/src/focus-order.cpp +441 -0
- package/native/core/src/gestures.cpp +1806 -0
- package/native/core/src/glass.cpp +49 -0
- package/native/core/src/header-bar.cpp +104 -0
- package/native/core/src/host-services.cpp +50 -0
- package/native/core/src/host-state.cpp +61 -0
- package/native/core/src/input-dispatcher.cpp +727 -0
- package/native/core/src/input-script.cpp +123 -0
- package/native/core/src/key-value-store.cpp +169 -0
- package/native/core/src/launch-settings.cpp +148 -0
- package/native/core/src/layout-animations.cpp +62 -0
- package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
- package/native/core/src/libraries/react-native-worklets.cpp +192 -0
- package/native/core/src/libraries/rnscreens.cpp +125 -0
- package/native/core/src/libraries/rnsvg.cpp +345 -0
- package/native/core/src/libraries/safeareacontext.cpp +70 -0
- package/native/core/src/libraries/worklets-idle-frames.js +70 -0
- package/native/core/src/library-glue.cpp +35 -0
- package/native/core/src/live-regions.cpp +122 -0
- package/native/core/src/module-registry.cpp +50 -0
- package/native/core/src/modules/accessibility-manager.cpp +98 -0
- package/native/core/src/modules/address-search.cpp +219 -0
- package/native/core/src/modules/alert-manager.cpp +140 -0
- package/native/core/src/modules/app-info.cpp +74 -0
- package/native/core/src/modules/app-modules.cpp +405 -0
- package/native/core/src/modules/apple-authentication.cpp +188 -0
- package/native/core/src/modules/asset.cpp +111 -0
- package/native/core/src/modules/async-storage.cpp +192 -0
- package/native/core/src/modules/audio.cpp +541 -0
- package/native/core/src/modules/battery.cpp +88 -0
- package/native/core/src/modules/calendar.cpp +457 -0
- package/native/core/src/modules/camera.cpp +221 -0
- package/native/core/src/modules/core-modules.cpp +62 -0
- package/native/core/src/modules/crypto.cpp +117 -0
- package/native/core/src/modules/desktop-expo.cpp +527 -0
- package/native/core/src/modules/file-system.cpp +941 -0
- package/native/core/src/modules/fonts.cpp +163 -0
- package/native/core/src/modules/image-loader.cpp +105 -0
- package/native/core/src/modules/image-manipulator.cpp +305 -0
- package/native/core/src/modules/language-model.cpp +195 -0
- package/native/core/src/modules/local-authentication.cpp +140 -0
- package/native/core/src/modules/location.cpp +186 -0
- package/native/core/src/modules/maps.cpp +500 -0
- package/native/core/src/modules/network-state.cpp +72 -0
- package/native/core/src/modules/networking.cpp +387 -0
- package/native/core/src/modules/notifications.cpp +549 -0
- package/native/core/src/modules/photo-library.cpp +371 -0
- package/native/core/src/modules/printing.cpp +201 -0
- package/native/core/src/modules/screen-capture.cpp +71 -0
- package/native/core/src/modules/sharing.cpp +59 -0
- package/native/core/src/modules/splash-screen.cpp +70 -0
- package/native/core/src/modules/sqlite.cpp +421 -0
- package/native/core/src/modules/store-review.cpp +23 -0
- package/native/core/src/modules/text-recognition.cpp +107 -0
- package/native/core/src/modules/tracking.cpp +46 -0
- package/native/core/src/modules/undo.cpp +59 -0
- package/native/core/src/modules/video.cpp +366 -0
- package/native/core/src/modules/web-browser.cpp +100 -0
- package/native/core/src/modules/web-socket.cpp +266 -0
- package/native/core/src/mounting-manager.cpp +380 -0
- package/native/core/src/native-controls.cpp +129 -0
- package/native/core/src/native-module.cpp +250 -0
- package/native/core/src/network.cpp +219 -0
- package/native/core/src/posix-launcher.cpp +366 -0
- package/native/core/src/processes.cpp +324 -0
- package/native/core/src/red-box-errors.js +83 -0
- package/native/core/src/red-box.cpp +306 -0
- package/native/core/src/runtime.cpp +422 -0
- package/native/core/src/safe-areas.cpp +130 -0
- package/native/core/src/screen-presentation.cpp +61 -0
- package/native/core/src/screen-stacks.cpp +254 -0
- package/native/core/src/scrollers.cpp +181 -0
- package/native/core/src/spawn-rules.cpp +292 -0
- package/native/core/src/split-view.cpp +343 -0
- package/native/core/src/svg.cpp +1883 -0
- package/native/core/src/symbol.cpp +94 -0
- package/native/core/src/tabs-host.cpp +234 -0
- package/native/core/src/text-input.cpp +126 -0
- package/native/core/src/text-layout-manager.cpp +63 -0
- package/native/core/src/tray-and-dock.cpp +154 -0
- package/native/core/src/updates.cpp +965 -0
- package/native/core/src/view-box.cpp +788 -0
- package/native/core/src/view-transform.cpp +45 -0
- package/native/core/src/view-tree.cpp +234 -0
- package/native/core/src/web-view.cpp +143 -0
- package/native/core/src/worklet-host.cpp +10 -0
- package/native/scripts/build-hermes.sh +44 -0
- package/native/scripts/build.sh +171 -0
- package/native/scripts/cache-dir.sh +18 -0
- package/native/scripts/codegen-library.sh +193 -0
- package/native/scripts/compiler-launcher.sh +17 -0
- package/native/scripts/fetch.sh +174 -0
- package/native/scripts/hermes-built.sh +10 -0
- package/native/scripts/host-dir.sh +16 -0
- package/native/scripts/macos-target.sh +8 -0
- package/native/scripts/make-prebuilt.mjs +360 -0
- package/native/scripts/prebuilt-host-cache.mjs +158 -0
- package/native/shim/openssl/sha.h +22 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
- package/package.json +70 -3
- package/prebuilt.json +33 -0
|
@@ -0,0 +1,1806 @@
|
|
|
1
|
+
#include "ngn/gestures.h"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <limits>
|
|
6
|
+
|
|
7
|
+
namespace ngn {
|
|
8
|
+
|
|
9
|
+
namespace {
|
|
10
|
+
|
|
11
|
+
constexpr double kMax = std::numeric_limits<double>::max();
|
|
12
|
+
constexpr double kMin = std::numeric_limits<double>::lowest();
|
|
13
|
+
// How far a mouse drags before a pan with no criteria of its own activates: UIKit's own pan
|
|
14
|
+
// hysteresis, which the library's iOS pan keeps when it is given no minDist.
|
|
15
|
+
constexpr double kDefaultSlop = 10;
|
|
16
|
+
// The library's MouseButton bits.
|
|
17
|
+
constexpr int kLeft = 1;
|
|
18
|
+
constexpr int kRight = 2;
|
|
19
|
+
constexpr int kMiddle = 4;
|
|
20
|
+
// `PointerType.MOUSE`.
|
|
21
|
+
constexpr int kMouse = 2;
|
|
22
|
+
// How far back the velocity looks.
|
|
23
|
+
constexpr double kVelocityWindowMs = 100;
|
|
24
|
+
|
|
25
|
+
std::optional<double> number(const folly::dynamic &config, const char *key) {
|
|
26
|
+
if (!config.isObject()) {
|
|
27
|
+
return std::nullopt;
|
|
28
|
+
}
|
|
29
|
+
auto it = config.find(key);
|
|
30
|
+
if (it == config.items().end() || !it->second.isNumber()) {
|
|
31
|
+
return std::nullopt;
|
|
32
|
+
}
|
|
33
|
+
return it->second.asDouble();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
std::optional<bool> flag(const folly::dynamic &config, const char *key) {
|
|
37
|
+
if (!config.isObject()) {
|
|
38
|
+
return std::nullopt;
|
|
39
|
+
}
|
|
40
|
+
auto it = config.find(key);
|
|
41
|
+
if (it == config.items().end() || !it->second.isBool()) {
|
|
42
|
+
return std::nullopt;
|
|
43
|
+
}
|
|
44
|
+
return it->second.asBool();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
std::vector<int> tags(const folly::dynamic &relations, const char *key) {
|
|
48
|
+
std::vector<int> out;
|
|
49
|
+
if (!relations.isObject()) {
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
auto it = relations.find(key);
|
|
53
|
+
if (it == relations.items().end() || !it->second.isArray()) {
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
for (const auto &tag : it->second) {
|
|
57
|
+
if (tag.isNumber()) {
|
|
58
|
+
out.push_back(static_cast<int>(tag.asDouble()));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
bool contains(const std::vector<int> &list, int tag) {
|
|
65
|
+
return std::find(list.begin(), list.end(), tag) != list.end();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// --- the handlers ---------------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
class PanHandler final : public GestureHandler {
|
|
71
|
+
public:
|
|
72
|
+
using GestureHandler::GestureHandler;
|
|
73
|
+
|
|
74
|
+
void pointerDown(const GesturePointer &pointer) override {
|
|
75
|
+
if (!buttonAllowed(pointer.button)) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
track(pointer);
|
|
79
|
+
GestureHandler::pointerDown(pointer);
|
|
80
|
+
last_ = average();
|
|
81
|
+
start_ = last_;
|
|
82
|
+
tryBegin();
|
|
83
|
+
checkBegan();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
void pointerMove(const GesturePointer &pointer, bool inBounds) override {
|
|
87
|
+
if (!inBounds && shouldCancelWhenOutside_) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
track(pointer);
|
|
91
|
+
last_ = average();
|
|
92
|
+
velocity_ = velocity();
|
|
93
|
+
checkBegan();
|
|
94
|
+
GestureHandler::pointerMove(pointer, inBounds);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
void pointerUp(const GesturePointer &pointer) override {
|
|
98
|
+
GestureHandler::pointerUp(pointer);
|
|
99
|
+
if (state() == GestureState::Active) {
|
|
100
|
+
last_ = average();
|
|
101
|
+
}
|
|
102
|
+
untrack(pointer.id);
|
|
103
|
+
if (pointers_.empty()) {
|
|
104
|
+
clearTimer();
|
|
105
|
+
}
|
|
106
|
+
if (state() == GestureState::Active) {
|
|
107
|
+
end();
|
|
108
|
+
} else {
|
|
109
|
+
resetProgress();
|
|
110
|
+
fail();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
bool wantsScroll() const override {
|
|
115
|
+
return trackpad_;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// A trackpad's two fingers drive the pan as one pointer when the gesture asks for it
|
|
119
|
+
// (`enableTrackpadTwoFingerGesture`), as UIKit's pan does with an iPad's trackpad.
|
|
120
|
+
void scroll(const GesturePointer &pointer, Point delta, std::optional<GesturePhase> phase) override {
|
|
121
|
+
if (!trackpad_) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (phase == GesturePhase::Began || (state() == GestureState::Undetermined && phase == GesturePhase::Changed)) {
|
|
125
|
+
GesturePointer virtualPointer = pointer;
|
|
126
|
+
virtualPointer.id = kScrollPointer;
|
|
127
|
+
scrollAt_ = pointer.page;
|
|
128
|
+
track(virtualPointer);
|
|
129
|
+
owner_.record(*this);
|
|
130
|
+
last_ = average();
|
|
131
|
+
start_ = last_;
|
|
132
|
+
tryBegin();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (!tracks(kScrollPointer)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (phase == GesturePhase::Changed) {
|
|
139
|
+
scrollAt_ = {scrollAt_.x + delta.x, scrollAt_.y + delta.y};
|
|
140
|
+
GesturePointer moved = pointer;
|
|
141
|
+
moved.id = kScrollPointer;
|
|
142
|
+
moved.page = scrollAt_;
|
|
143
|
+
track(moved);
|
|
144
|
+
last_ = average();
|
|
145
|
+
velocity_ = velocity();
|
|
146
|
+
checkBegan();
|
|
147
|
+
if (active) {
|
|
148
|
+
owner_.send(*this, state(), state());
|
|
149
|
+
}
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
// Ended or cancelled.
|
|
153
|
+
untrack(kScrollPointer);
|
|
154
|
+
if (state() == GestureState::Active && phase == GesturePhase::Ended) {
|
|
155
|
+
end();
|
|
156
|
+
} else if (state() == GestureState::Active) {
|
|
157
|
+
cancel();
|
|
158
|
+
} else {
|
|
159
|
+
fail();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
protected:
|
|
164
|
+
void readConfig(const folly::dynamic &config) override {
|
|
165
|
+
GestureHandler::readConfig(config);
|
|
166
|
+
auto custom = [&](const char *key, double &into) {
|
|
167
|
+
if (auto value = number(config, key)) {
|
|
168
|
+
into = *value;
|
|
169
|
+
custom_ = true;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
if (auto value = number(config, "minPointers")) {
|
|
173
|
+
minPointers_ = static_cast<size_t>(*value);
|
|
174
|
+
}
|
|
175
|
+
if (auto value = number(config, "maxPointers")) {
|
|
176
|
+
maxPointers_ = static_cast<size_t>(*value);
|
|
177
|
+
}
|
|
178
|
+
if (auto value = number(config, "minVelocity")) {
|
|
179
|
+
minVelocitySq_ = *value * *value;
|
|
180
|
+
custom_ = true;
|
|
181
|
+
}
|
|
182
|
+
custom("minVelocityX", minVelocityX_);
|
|
183
|
+
custom("minVelocityY", minVelocityY_);
|
|
184
|
+
custom("activeOffsetXStart", activeXStart_);
|
|
185
|
+
custom("activeOffsetXEnd", activeXEnd_);
|
|
186
|
+
custom("failOffsetXStart", failXStart_);
|
|
187
|
+
custom("failOffsetXEnd", failXEnd_);
|
|
188
|
+
custom("activeOffsetYStart", activeYStart_);
|
|
189
|
+
custom("activeOffsetYEnd", activeYEnd_);
|
|
190
|
+
custom("failOffsetYStart", failYStart_);
|
|
191
|
+
custom("failOffsetYEnd", failYEnd_);
|
|
192
|
+
if (auto value = number(config, "activateAfterLongPress")) {
|
|
193
|
+
afterLongPress_ = *value;
|
|
194
|
+
}
|
|
195
|
+
if (auto value = flag(config, "enableTrackpadTwoFingerGesture")) {
|
|
196
|
+
trackpad_ = *value;
|
|
197
|
+
}
|
|
198
|
+
if (auto value = number(config, "minDist")) {
|
|
199
|
+
minDist_ = *value;
|
|
200
|
+
minDistSq_ = *value * *value;
|
|
201
|
+
} else if (!minDist_ && custom_) {
|
|
202
|
+
minDistSq_ = kMax;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
void resetConfig() override {
|
|
207
|
+
GestureHandler::resetConfig();
|
|
208
|
+
minPointers_ = 1;
|
|
209
|
+
maxPointers_ = 10;
|
|
210
|
+
minDist_.reset();
|
|
211
|
+
minDistSq_ = kDefaultSlop * kDefaultSlop;
|
|
212
|
+
activeXStart_ = kMax;
|
|
213
|
+
activeXEnd_ = kMin;
|
|
214
|
+
failXStart_ = kMin;
|
|
215
|
+
failXEnd_ = kMax;
|
|
216
|
+
activeYStart_ = kMax;
|
|
217
|
+
activeYEnd_ = kMin;
|
|
218
|
+
failYStart_ = kMin;
|
|
219
|
+
failYEnd_ = kMax;
|
|
220
|
+
minVelocityX_ = kMax;
|
|
221
|
+
minVelocityY_ = kMax;
|
|
222
|
+
minVelocitySq_ = kMax;
|
|
223
|
+
afterLongPress_ = 0;
|
|
224
|
+
trackpad_ = false;
|
|
225
|
+
custom_ = false;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
folly::dynamic extraData() const override {
|
|
229
|
+
auto data = GestureHandler::extraData();
|
|
230
|
+
data["translationX"] = translation().x;
|
|
231
|
+
data["translationY"] = translation().y;
|
|
232
|
+
data["velocityX"] = std::isnan(velocity_.x) ? 0.0 : velocity_.x;
|
|
233
|
+
data["velocityY"] = std::isnan(velocity_.y) ? 0.0 : velocity_.y;
|
|
234
|
+
return data;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
void onCancel() override {
|
|
238
|
+
clearTimer();
|
|
239
|
+
}
|
|
240
|
+
void onReset() override {
|
|
241
|
+
clearTimer();
|
|
242
|
+
}
|
|
243
|
+
void resetProgress() override {
|
|
244
|
+
if (state() == GestureState::Active) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
start_ = last_;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
public:
|
|
251
|
+
void activate(bool force) override {
|
|
252
|
+
if (state() != GestureState::Active) {
|
|
253
|
+
resetProgress();
|
|
254
|
+
}
|
|
255
|
+
GestureHandler::activate(force);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
private:
|
|
259
|
+
static constexpr int kScrollPointer = -1;
|
|
260
|
+
|
|
261
|
+
Point translation() const {
|
|
262
|
+
return {last_.x - start_.x + offset_.x, last_.y - start_.y + offset_.y};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
void tryBegin() {
|
|
266
|
+
if (state() != GestureState::Undetermined || pointers_.size() < minPointers_) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
resetProgress();
|
|
270
|
+
offset_ = {0, 0};
|
|
271
|
+
velocity_ = {0, 0};
|
|
272
|
+
begin();
|
|
273
|
+
if (afterLongPress_ > 0) {
|
|
274
|
+
after(afterLongPress_, [this] { activate(false); });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
void checkBegan() {
|
|
279
|
+
if (state() != GestureState::Began) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (shouldFail()) {
|
|
283
|
+
fail();
|
|
284
|
+
} else if (shouldActivate()) {
|
|
285
|
+
activate(false);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
bool shouldActivate() const {
|
|
290
|
+
// A pan that waits for a long press activates only when it has waited.
|
|
291
|
+
if (afterLongPress_ > 0) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
auto t = translation();
|
|
295
|
+
if (activeXStart_ != kMax && t.x < activeXStart_) {
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
if (activeXEnd_ != kMin && t.x > activeXEnd_) {
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
if (activeYStart_ != kMax && t.y < activeYStart_) {
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
if (activeYEnd_ != kMin && t.y > activeYEnd_) {
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
double distanceSq = t.x * t.x + t.y * t.y;
|
|
308
|
+
if (minDistSq_ != kMax && distanceSq >= minDistSq_) {
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
if (minVelocityX_ != kMax && std::abs(velocity_.x) >= std::abs(minVelocityX_)) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
if (minVelocityY_ != kMax && std::abs(velocity_.y) >= std::abs(minVelocityY_)) {
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
double velocitySq = velocity_.x * velocity_.x + velocity_.y * velocity_.y;
|
|
318
|
+
return minVelocitySq_ != kMax && velocitySq >= minVelocitySq_;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
bool shouldFail() {
|
|
322
|
+
auto t = translation();
|
|
323
|
+
double distanceSq = t.x * t.x + t.y * t.y;
|
|
324
|
+
if (afterLongPress_ > 0 && distanceSq > kDefaultSlop * kDefaultSlop) {
|
|
325
|
+
clearTimer();
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
if (failXStart_ != kMin && t.x < failXStart_) {
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
if (failXEnd_ != kMax && t.x > failXEnd_) {
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
if (failYStart_ != kMin && t.y < failYStart_) {
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
return failYEnd_ != kMax && t.y > failYEnd_;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
size_t minPointers_{1};
|
|
341
|
+
size_t maxPointers_{10};
|
|
342
|
+
std::optional<double> minDist_;
|
|
343
|
+
double minDistSq_{kDefaultSlop * kDefaultSlop};
|
|
344
|
+
// As the library's: a start offset that is still its default never activates.
|
|
345
|
+
double activeXStart_{kMax};
|
|
346
|
+
double activeXEnd_{kMin};
|
|
347
|
+
double failXStart_{kMin};
|
|
348
|
+
double failXEnd_{kMax};
|
|
349
|
+
double activeYStart_{kMax};
|
|
350
|
+
double activeYEnd_{kMin};
|
|
351
|
+
double failYStart_{kMin};
|
|
352
|
+
double failYEnd_{kMax};
|
|
353
|
+
double minVelocityX_{kMax};
|
|
354
|
+
double minVelocityY_{kMax};
|
|
355
|
+
double minVelocitySq_{kMax};
|
|
356
|
+
double afterLongPress_{0};
|
|
357
|
+
bool trackpad_{false};
|
|
358
|
+
bool custom_{false};
|
|
359
|
+
Point start_{};
|
|
360
|
+
Point last_{};
|
|
361
|
+
Point offset_{};
|
|
362
|
+
Point velocity_{};
|
|
363
|
+
Point scrollAt_{};
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
class TapHandler final : public GestureHandler {
|
|
367
|
+
public:
|
|
368
|
+
using GestureHandler::GestureHandler;
|
|
369
|
+
|
|
370
|
+
void pointerDown(const GesturePointer &pointer) override {
|
|
371
|
+
if (!buttonAllowed(pointer.button)) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
track(pointer);
|
|
375
|
+
GestureHandler::pointerDown(pointer);
|
|
376
|
+
if (state() == GestureState::Undetermined) {
|
|
377
|
+
offset_ = {0, 0};
|
|
378
|
+
}
|
|
379
|
+
start_ = pointer.page;
|
|
380
|
+
last_ = pointer.page;
|
|
381
|
+
update(Kind::Down);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
void pointerMove(const GesturePointer &pointer, bool inBounds) override {
|
|
385
|
+
if (!tracks(pointer.id)) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
track(pointer);
|
|
389
|
+
last_ = average();
|
|
390
|
+
update(Kind::Move);
|
|
391
|
+
GestureHandler::pointerMove(pointer, inBounds);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
void pointerUp(const GesturePointer &pointer) override {
|
|
395
|
+
if (!tracks(pointer.id)) {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
GestureHandler::pointerUp(pointer);
|
|
399
|
+
last_ = average();
|
|
400
|
+
untrack(pointer.id);
|
|
401
|
+
update(Kind::Up);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
void activate(bool force) override {
|
|
405
|
+
GestureHandler::activate(force);
|
|
406
|
+
end();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
protected:
|
|
410
|
+
void readConfig(const folly::dynamic &config) override {
|
|
411
|
+
GestureHandler::readConfig(config);
|
|
412
|
+
if (auto value = number(config, "numberOfTaps")) {
|
|
413
|
+
taps_ = static_cast<int>(*value);
|
|
414
|
+
}
|
|
415
|
+
if (auto value = number(config, "maxDurationMs")) {
|
|
416
|
+
maxDuration_ = *value;
|
|
417
|
+
}
|
|
418
|
+
if (auto value = number(config, "maxDelayMs")) {
|
|
419
|
+
maxDelay_ = *value;
|
|
420
|
+
}
|
|
421
|
+
if (auto value = number(config, "maxDeltaX")) {
|
|
422
|
+
maxDeltaX_ = *value;
|
|
423
|
+
}
|
|
424
|
+
if (auto value = number(config, "maxDeltaY")) {
|
|
425
|
+
maxDeltaY_ = *value;
|
|
426
|
+
}
|
|
427
|
+
if (auto value = number(config, "maxDist")) {
|
|
428
|
+
maxDistSq_ = *value * *value;
|
|
429
|
+
}
|
|
430
|
+
if (auto value = number(config, "minPointers")) {
|
|
431
|
+
minPointers_ = static_cast<size_t>(*value);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
void resetConfig() override {
|
|
436
|
+
GestureHandler::resetConfig();
|
|
437
|
+
taps_ = 1;
|
|
438
|
+
maxDuration_ = 500;
|
|
439
|
+
maxDelay_ = 500;
|
|
440
|
+
maxDeltaX_ = kMin;
|
|
441
|
+
maxDeltaY_ = kMin;
|
|
442
|
+
maxDistSq_ = kMin;
|
|
443
|
+
minPointers_ = 1;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
void onCancel() override {
|
|
447
|
+
resetProgress();
|
|
448
|
+
}
|
|
449
|
+
void resetProgress() override {
|
|
450
|
+
clearTimer();
|
|
451
|
+
tapsSoFar_ = 0;
|
|
452
|
+
maxPointersSeen_ = 0;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private:
|
|
456
|
+
enum class Kind { Down, Move, Up };
|
|
457
|
+
|
|
458
|
+
void update(Kind kind) {
|
|
459
|
+
maxPointersSeen_ = std::max(maxPointersSeen_, pointers_.size());
|
|
460
|
+
if (shouldFail()) {
|
|
461
|
+
fail();
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (state() == GestureState::Undetermined) {
|
|
465
|
+
if (kind == Kind::Down) {
|
|
466
|
+
begin();
|
|
467
|
+
}
|
|
468
|
+
startTap();
|
|
469
|
+
} else if (state() == GestureState::Began) {
|
|
470
|
+
if (kind == Kind::Up) {
|
|
471
|
+
endTap();
|
|
472
|
+
}
|
|
473
|
+
if (kind == Kind::Down) {
|
|
474
|
+
startTap();
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
void startTap() {
|
|
480
|
+
clearTimer();
|
|
481
|
+
after(maxDuration_, [this] { fail(); });
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
void endTap() {
|
|
485
|
+
clearTimer();
|
|
486
|
+
if (++tapsSoFar_ == taps_ && maxPointersSeen_ >= minPointers_) {
|
|
487
|
+
activate(false);
|
|
488
|
+
} else {
|
|
489
|
+
after(maxDelay_, [this] { fail(); });
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
bool shouldFail() const {
|
|
494
|
+
double dx = last_.x - start_.x + offset_.x;
|
|
495
|
+
double dy = last_.y - start_.y + offset_.y;
|
|
496
|
+
if (maxDeltaX_ != kMin && std::abs(dx) > maxDeltaX_) {
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
if (maxDeltaY_ != kMin && std::abs(dy) > maxDeltaY_) {
|
|
500
|
+
return true;
|
|
501
|
+
}
|
|
502
|
+
return maxDistSq_ != kMin && dx * dx + dy * dy > maxDistSq_;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
int taps_{1};
|
|
506
|
+
double maxDuration_{500};
|
|
507
|
+
double maxDelay_{500};
|
|
508
|
+
double maxDeltaX_{kMin};
|
|
509
|
+
double maxDeltaY_{kMin};
|
|
510
|
+
double maxDistSq_{kMin};
|
|
511
|
+
size_t minPointers_{1};
|
|
512
|
+
int tapsSoFar_{0};
|
|
513
|
+
size_t maxPointersSeen_{0};
|
|
514
|
+
Point start_{};
|
|
515
|
+
Point last_{};
|
|
516
|
+
Point offset_{};
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
class LongPressHandler final : public GestureHandler {
|
|
520
|
+
public:
|
|
521
|
+
using GestureHandler::GestureHandler;
|
|
522
|
+
|
|
523
|
+
void pointerDown(const GesturePointer &pointer) override {
|
|
524
|
+
if (!buttonAllowed(pointer.button)) {
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
track(pointer);
|
|
528
|
+
GestureHandler::pointerDown(pointer);
|
|
529
|
+
start_ = pointer.page;
|
|
530
|
+
if (state() == GestureState::Undetermined) {
|
|
531
|
+
startTime_ = pointer.time;
|
|
532
|
+
begin();
|
|
533
|
+
}
|
|
534
|
+
if (pointers_.size() == pointerCount_) {
|
|
535
|
+
if (minDuration_ > 0) {
|
|
536
|
+
after(minDuration_, [this] { activate(false); });
|
|
537
|
+
} else {
|
|
538
|
+
activate(false);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
void pointerMove(const GesturePointer &pointer, bool inBounds) override {
|
|
544
|
+
if (!tracks(pointer.id)) {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
GestureHandler::pointerMove(pointer, inBounds);
|
|
548
|
+
track(pointer);
|
|
549
|
+
auto now = average();
|
|
550
|
+
double dx = now.x - start_.x;
|
|
551
|
+
double dy = now.y - start_.y;
|
|
552
|
+
if (dx * dx + dy * dy <= maxDistSq_) {
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
if (state() == GestureState::Active) {
|
|
556
|
+
cancel();
|
|
557
|
+
} else {
|
|
558
|
+
fail();
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
void pointerUp(const GesturePointer &pointer) override {
|
|
563
|
+
if (!tracks(pointer.id)) {
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
GestureHandler::pointerUp(pointer);
|
|
567
|
+
untrack(pointer.id);
|
|
568
|
+
if (state() == GestureState::Active) {
|
|
569
|
+
end();
|
|
570
|
+
} else {
|
|
571
|
+
fail();
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
protected:
|
|
576
|
+
void readConfig(const folly::dynamic &config) override {
|
|
577
|
+
GestureHandler::readConfig(config);
|
|
578
|
+
if (auto value = number(config, "minDurationMs")) {
|
|
579
|
+
minDuration_ = *value;
|
|
580
|
+
}
|
|
581
|
+
if (auto value = number(config, "maxDist")) {
|
|
582
|
+
maxDistSq_ = *value * *value;
|
|
583
|
+
}
|
|
584
|
+
if (auto value = number(config, "numberOfPointers")) {
|
|
585
|
+
pointerCount_ = static_cast<size_t>(*value);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
void resetConfig() override {
|
|
589
|
+
GestureHandler::resetConfig();
|
|
590
|
+
minDuration_ = 500;
|
|
591
|
+
maxDistSq_ = 100;
|
|
592
|
+
pointerCount_ = 1;
|
|
593
|
+
}
|
|
594
|
+
void onStateChange(GestureState, GestureState) override {
|
|
595
|
+
clearTimer();
|
|
596
|
+
}
|
|
597
|
+
folly::dynamic extraData() const override {
|
|
598
|
+
auto data = GestureHandler::extraData();
|
|
599
|
+
data["duration"] = owner_.now() - startTime_;
|
|
600
|
+
return data;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
private:
|
|
604
|
+
double minDuration_{500};
|
|
605
|
+
// UIKit's default allowable movement: 10 points.
|
|
606
|
+
double maxDistSq_{100};
|
|
607
|
+
size_t pointerCount_{1};
|
|
608
|
+
Point start_{};
|
|
609
|
+
double startTime_{0};
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
// The native view's own recogniser. With a mouse, a scroll view scrolls by the wheel and the
|
|
613
|
+
// trackpad, not by a drag, so a native handler on one activates as it scrolls; on any other view
|
|
614
|
+
// it activates once a press drags past the slop, as the library's web handler does.
|
|
615
|
+
class NativeHandler final : public GestureHandler {
|
|
616
|
+
public:
|
|
617
|
+
using GestureHandler::GestureHandler;
|
|
618
|
+
|
|
619
|
+
void pointerDown(const GesturePointer &pointer) override {
|
|
620
|
+
track(pointer);
|
|
621
|
+
GestureHandler::pointerDown(pointer);
|
|
622
|
+
start_ = average();
|
|
623
|
+
if (state() != GestureState::Undetermined) {
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
begin();
|
|
627
|
+
if (activateOnStart_) {
|
|
628
|
+
activate(false);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
void pointerMove(const GesturePointer &pointer, bool inBounds) override {
|
|
633
|
+
if (!tracks(pointer.id)) {
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
track(pointer);
|
|
637
|
+
if (!inBounds && shouldCancelWhenOutside_) {
|
|
638
|
+
if (state() == GestureState::Active) {
|
|
639
|
+
cancel();
|
|
640
|
+
} else {
|
|
641
|
+
fail();
|
|
642
|
+
}
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
auto now = average();
|
|
646
|
+
double dx = now.x - start_.x;
|
|
647
|
+
double dy = now.y - start_.y;
|
|
648
|
+
if (state() == GestureState::Began && !scrollView() && dx * dx + dy * dy >= kDefaultSlop * kDefaultSlop) {
|
|
649
|
+
activate(false);
|
|
650
|
+
}
|
|
651
|
+
GestureHandler::pointerMove(pointer, inBounds);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
void pointerUp(const GesturePointer &pointer) override {
|
|
655
|
+
if (!tracks(pointer.id)) {
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
GestureHandler::pointerUp(pointer);
|
|
659
|
+
untrack(pointer.id);
|
|
660
|
+
if (!pointers_.empty()) {
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
if (state() == GestureState::Active) {
|
|
664
|
+
end();
|
|
665
|
+
} else {
|
|
666
|
+
fail();
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
bool wantsScroll() const override {
|
|
671
|
+
return scrollView();
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
void scroll(const GesturePointer &pointer, Point, std::optional<GesturePhase> phase) override {
|
|
675
|
+
if (phase == GesturePhase::Ended || phase == GesturePhase::Cancelled) {
|
|
676
|
+
if (state() == GestureState::Active && !tracks(pointer.id)) {
|
|
677
|
+
end();
|
|
678
|
+
}
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
if (state() == GestureState::Undetermined) {
|
|
682
|
+
owner_.record(*this);
|
|
683
|
+
begin();
|
|
684
|
+
}
|
|
685
|
+
if (state() == GestureState::Began) {
|
|
686
|
+
activate(false);
|
|
687
|
+
}
|
|
688
|
+
// A wheel has no phases: the scroll is over when the wheel stops.
|
|
689
|
+
if (!phase) {
|
|
690
|
+
after(kWheelIdleMs, [this] {
|
|
691
|
+
if (state() == GestureState::Active) {
|
|
692
|
+
end();
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
protected:
|
|
699
|
+
void readConfig(const folly::dynamic &config) override {
|
|
700
|
+
GestureHandler::readConfig(config);
|
|
701
|
+
if (auto value = flag(config, "shouldActivateOnStart")) {
|
|
702
|
+
activateOnStart_ = *value;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
void resetConfig() override {
|
|
706
|
+
GestureHandler::resetConfig();
|
|
707
|
+
shouldCancelWhenOutside_ = true;
|
|
708
|
+
activateOnStart_ = false;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
private:
|
|
712
|
+
static constexpr double kWheelIdleMs = 100;
|
|
713
|
+
|
|
714
|
+
bool scrollView() const {
|
|
715
|
+
const auto *node = owner_.tree().find(view());
|
|
716
|
+
return node != nullptr && std::string_view(node->view.componentName) == "ScrollView";
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
bool activateOnStart_{false};
|
|
720
|
+
Point start_{};
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// Driven only by its own state manager (manual activation from a worklet).
|
|
724
|
+
class ManualHandler final : public GestureHandler {
|
|
725
|
+
public:
|
|
726
|
+
using GestureHandler::GestureHandler;
|
|
727
|
+
|
|
728
|
+
void pointerDown(const GesturePointer &pointer) override {
|
|
729
|
+
track(pointer);
|
|
730
|
+
GestureHandler::pointerDown(pointer);
|
|
731
|
+
begin();
|
|
732
|
+
}
|
|
733
|
+
void pointerMove(const GesturePointer &pointer, bool inBounds) override {
|
|
734
|
+
if (!tracks(pointer.id)) {
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
track(pointer);
|
|
738
|
+
GestureHandler::pointerMove(pointer, inBounds);
|
|
739
|
+
}
|
|
740
|
+
void pointerUp(const GesturePointer &pointer) override {
|
|
741
|
+
if (!tracks(pointer.id)) {
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
GestureHandler::pointerUp(pointer);
|
|
745
|
+
untrack(pointer.id);
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
|
|
749
|
+
// The pointer over the view with no button down, as UIKit's hover recogniser reports a mouse.
|
|
750
|
+
class HoverHandler final : public GestureHandler {
|
|
751
|
+
public:
|
|
752
|
+
using GestureHandler::GestureHandler;
|
|
753
|
+
|
|
754
|
+
bool wantsHover() const override {
|
|
755
|
+
return true;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
void hover(const GesturePointer &pointer, bool inside) override {
|
|
759
|
+
lastAverage_ = pointer.page;
|
|
760
|
+
if (inside) {
|
|
761
|
+
if (state() == GestureState::Undetermined) {
|
|
762
|
+
owner_.record(*this);
|
|
763
|
+
begin();
|
|
764
|
+
activate(false);
|
|
765
|
+
} else if (state() == GestureState::Active) {
|
|
766
|
+
owner_.send(*this, state(), state());
|
|
767
|
+
}
|
|
768
|
+
} else if (state() == GestureState::Active || state() == GestureState::Began) {
|
|
769
|
+
end();
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
void pointerDown(const GesturePointer &) override {}
|
|
774
|
+
void pointerMove(const GesturePointer &, bool) override {}
|
|
775
|
+
void pointerUp(const GesturePointer &) override {}
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
class PinchHandler final : public GestureHandler {
|
|
779
|
+
public:
|
|
780
|
+
using GestureHandler::GestureHandler;
|
|
781
|
+
|
|
782
|
+
bool wantsMagnify() const override {
|
|
783
|
+
return true;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
void pointerDown(const GesturePointer &) override {}
|
|
787
|
+
void pointerMove(const GesturePointer &, bool) override {}
|
|
788
|
+
void pointerUp(const GesturePointer &) override {}
|
|
789
|
+
|
|
790
|
+
void magnify(const GesturePointer &pointer, double magnification, GesturePhase phase) override {
|
|
791
|
+
lastAverage_ = pointer.page;
|
|
792
|
+
if (phase == GesturePhase::Began) {
|
|
793
|
+
owner_.record(*this);
|
|
794
|
+
scale_ = 1;
|
|
795
|
+
velocity_ = 0;
|
|
796
|
+
lastTime_ = pointer.time;
|
|
797
|
+
begin();
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
if (phase == GesturePhase::Changed) {
|
|
801
|
+
double before = scale_;
|
|
802
|
+
scale_ *= 1 + magnification;
|
|
803
|
+
double dt = pointer.time - lastTime_;
|
|
804
|
+
velocity_ = dt > 0 ? (scale_ - before) / dt * 1000 : velocity_;
|
|
805
|
+
lastTime_ = pointer.time;
|
|
806
|
+
if (state() == GestureState::Began) {
|
|
807
|
+
activate(false);
|
|
808
|
+
} else if (state() == GestureState::Active) {
|
|
809
|
+
owner_.send(*this, state(), state());
|
|
810
|
+
}
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
if (state() == GestureState::Active && phase == GesturePhase::Ended) {
|
|
814
|
+
end();
|
|
815
|
+
} else if (state() == GestureState::Active) {
|
|
816
|
+
cancel();
|
|
817
|
+
} else {
|
|
818
|
+
fail();
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
protected:
|
|
823
|
+
folly::dynamic extraData() const override {
|
|
824
|
+
auto focal = local(lastAverage_);
|
|
825
|
+
return folly::dynamic::object("scale", scale_)("focalX", focal.x)("focalY", focal.y)(
|
|
826
|
+
"velocity", velocity_)("numberOfPointers", 2)("pointerType", kMouse);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
private:
|
|
830
|
+
double scale_{1};
|
|
831
|
+
double velocity_{0};
|
|
832
|
+
double lastTime_{0};
|
|
833
|
+
};
|
|
834
|
+
|
|
835
|
+
class RotationHandler final : public GestureHandler {
|
|
836
|
+
public:
|
|
837
|
+
using GestureHandler::GestureHandler;
|
|
838
|
+
|
|
839
|
+
bool wantsRotate() const override {
|
|
840
|
+
return true;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
void pointerDown(const GesturePointer &) override {}
|
|
844
|
+
void pointerMove(const GesturePointer &, bool) override {}
|
|
845
|
+
void pointerUp(const GesturePointer &) override {}
|
|
846
|
+
|
|
847
|
+
void rotate(const GesturePointer &pointer, double degrees, GesturePhase phase) override {
|
|
848
|
+
lastAverage_ = pointer.page;
|
|
849
|
+
if (phase == GesturePhase::Began) {
|
|
850
|
+
owner_.record(*this);
|
|
851
|
+
rotation_ = 0;
|
|
852
|
+
velocity_ = 0;
|
|
853
|
+
lastTime_ = pointer.time;
|
|
854
|
+
begin();
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
if (phase == GesturePhase::Changed) {
|
|
858
|
+
// AppKit's degrees are anticlockwise; UIKit's rotation, which the library reports, is
|
|
859
|
+
// clockwise radians.
|
|
860
|
+
double step = -degrees * M_PI / 180;
|
|
861
|
+
rotation_ += step;
|
|
862
|
+
double dt = pointer.time - lastTime_;
|
|
863
|
+
velocity_ = dt > 0 ? step / dt * 1000 : velocity_;
|
|
864
|
+
lastTime_ = pointer.time;
|
|
865
|
+
if (state() == GestureState::Began) {
|
|
866
|
+
activate(false);
|
|
867
|
+
} else if (state() == GestureState::Active) {
|
|
868
|
+
owner_.send(*this, state(), state());
|
|
869
|
+
}
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
if (state() == GestureState::Active && phase == GesturePhase::Ended) {
|
|
873
|
+
end();
|
|
874
|
+
} else if (state() == GestureState::Active) {
|
|
875
|
+
cancel();
|
|
876
|
+
} else {
|
|
877
|
+
fail();
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
protected:
|
|
882
|
+
folly::dynamic extraData() const override {
|
|
883
|
+
auto anchor = local(lastAverage_);
|
|
884
|
+
return folly::dynamic::object("rotation", rotation_)("anchorX", anchor.x)("anchorY", anchor.y)("velocity", velocity_)("numberOfPointers", 2)("pointerType", kMouse);
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
private:
|
|
888
|
+
double rotation_{0};
|
|
889
|
+
double velocity_{0};
|
|
890
|
+
double lastTime_{0};
|
|
891
|
+
};
|
|
892
|
+
|
|
893
|
+
// A quick drag in one of its directions (RIGHT 1, LEFT 2, UP 4, DOWN 8).
|
|
894
|
+
class FlingHandler final : public GestureHandler {
|
|
895
|
+
public:
|
|
896
|
+
using GestureHandler::GestureHandler;
|
|
897
|
+
|
|
898
|
+
void pointerDown(const GesturePointer &pointer) override {
|
|
899
|
+
if (!buttonAllowed(pointer.button)) {
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
track(pointer);
|
|
903
|
+
GestureHandler::pointerDown(pointer);
|
|
904
|
+
start_ = pointer.page;
|
|
905
|
+
if (state() == GestureState::Undetermined) {
|
|
906
|
+
begin();
|
|
907
|
+
after(kMaxDurationMs, [this] { fail(); });
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
void pointerMove(const GesturePointer &pointer, bool inBounds) override {
|
|
912
|
+
if (!tracks(pointer.id)) {
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
915
|
+
track(pointer);
|
|
916
|
+
GestureHandler::pointerMove(pointer, inBounds);
|
|
917
|
+
if (state() != GestureState::Began) {
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
auto now = average();
|
|
921
|
+
auto v = velocity();
|
|
922
|
+
double dx = now.x - start_.x;
|
|
923
|
+
double dy = now.y - start_.y;
|
|
924
|
+
bool swiped = ((direction_ & 1) && dx > kMinDistance && v.x > kMinVelocity) ||
|
|
925
|
+
((direction_ & 2) && -dx > kMinDistance && -v.x > kMinVelocity) ||
|
|
926
|
+
((direction_ & 4) && -dy > kMinDistance && -v.y > kMinVelocity) ||
|
|
927
|
+
((direction_ & 8) && dy > kMinDistance && v.y > kMinVelocity);
|
|
928
|
+
if (swiped) {
|
|
929
|
+
clearTimer();
|
|
930
|
+
activate(false);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
void pointerUp(const GesturePointer &pointer) override {
|
|
935
|
+
if (!tracks(pointer.id)) {
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
GestureHandler::pointerUp(pointer);
|
|
939
|
+
untrack(pointer.id);
|
|
940
|
+
if (state() == GestureState::Active) {
|
|
941
|
+
end();
|
|
942
|
+
} else {
|
|
943
|
+
fail();
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
protected:
|
|
948
|
+
void readConfig(const folly::dynamic &config) override {
|
|
949
|
+
GestureHandler::readConfig(config);
|
|
950
|
+
if (auto value = number(config, "direction")) {
|
|
951
|
+
direction_ = static_cast<int>(*value);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
void resetConfig() override {
|
|
955
|
+
GestureHandler::resetConfig();
|
|
956
|
+
direction_ = 1;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
private:
|
|
960
|
+
static constexpr double kMaxDurationMs = 800;
|
|
961
|
+
static constexpr double kMinDistance = 40;
|
|
962
|
+
static constexpr double kMinVelocity = 300;
|
|
963
|
+
int direction_{1};
|
|
964
|
+
Point start_{};
|
|
965
|
+
};
|
|
966
|
+
|
|
967
|
+
std::unique_ptr<GestureHandler> makeHandler(Gestures &owner, const std::string &name, int tag) {
|
|
968
|
+
if (name == "PanGestureHandler") {
|
|
969
|
+
return std::make_unique<PanHandler>(owner, tag, name);
|
|
970
|
+
}
|
|
971
|
+
if (name == "TapGestureHandler") {
|
|
972
|
+
return std::make_unique<TapHandler>(owner, tag, name);
|
|
973
|
+
}
|
|
974
|
+
if (name == "LongPressGestureHandler") {
|
|
975
|
+
return std::make_unique<LongPressHandler>(owner, tag, name);
|
|
976
|
+
}
|
|
977
|
+
if (name == "NativeViewGestureHandler") {
|
|
978
|
+
return std::make_unique<NativeHandler>(owner, tag, name);
|
|
979
|
+
}
|
|
980
|
+
if (name == "ManualGestureHandler") {
|
|
981
|
+
return std::make_unique<ManualHandler>(owner, tag, name);
|
|
982
|
+
}
|
|
983
|
+
if (name == "HoverGestureHandler") {
|
|
984
|
+
return std::make_unique<HoverHandler>(owner, tag, name);
|
|
985
|
+
}
|
|
986
|
+
if (name == "PinchGestureHandler") {
|
|
987
|
+
return std::make_unique<PinchHandler>(owner, tag, name);
|
|
988
|
+
}
|
|
989
|
+
if (name == "RotationGestureHandler") {
|
|
990
|
+
return std::make_unique<RotationHandler>(owner, tag, name);
|
|
991
|
+
}
|
|
992
|
+
if (name == "FlingGestureHandler") {
|
|
993
|
+
return std::make_unique<FlingHandler>(owner, tag, name);
|
|
994
|
+
}
|
|
995
|
+
return nullptr;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
} // namespace
|
|
999
|
+
|
|
1000
|
+
// --- GestureHandler -------------------------------------------------------------------------------
|
|
1001
|
+
|
|
1002
|
+
GestureHandler::GestureHandler(Gestures &owner, int tag, std::string name)
|
|
1003
|
+
: owner_(owner), tag_(tag), name_(std::move(name)) {}
|
|
1004
|
+
|
|
1005
|
+
void GestureHandler::configure(const folly::dynamic &config, bool reset) {
|
|
1006
|
+
if (reset) {
|
|
1007
|
+
resetConfig();
|
|
1008
|
+
}
|
|
1009
|
+
readConfig(config);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
void GestureHandler::readConfig(const folly::dynamic &config) {
|
|
1013
|
+
if (auto value = flag(config, "enabled")) {
|
|
1014
|
+
enabled_ = *value;
|
|
1015
|
+
if (!enabled_ && state_ != GestureState::Undetermined) {
|
|
1016
|
+
state_ == GestureState::Active ? cancel() : fail();
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
if (auto value = flag(config, "shouldCancelWhenOutside")) {
|
|
1020
|
+
shouldCancelWhenOutside_ = *value;
|
|
1021
|
+
}
|
|
1022
|
+
if (auto value = flag(config, "needsPointerData")) {
|
|
1023
|
+
needsPointerData_ = *value;
|
|
1024
|
+
}
|
|
1025
|
+
if (auto value = flag(config, "manualActivation")) {
|
|
1026
|
+
manualActivation_ = *value;
|
|
1027
|
+
}
|
|
1028
|
+
if (auto value = number(config, "mouseButton")) {
|
|
1029
|
+
mouseButton_ = static_cast<int>(*value);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
void GestureHandler::resetConfig() {
|
|
1034
|
+
enabled_ = true;
|
|
1035
|
+
shouldCancelWhenOutside_ = false;
|
|
1036
|
+
needsPointerData_ = false;
|
|
1037
|
+
manualActivation_ = false;
|
|
1038
|
+
mouseButton_.reset();
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
void GestureHandler::attach(Tag view, GestureAction action) {
|
|
1042
|
+
if (view_ != -1 && view_ != view) {
|
|
1043
|
+
state_ == GestureState::Active ? cancel() : fail();
|
|
1044
|
+
}
|
|
1045
|
+
view_ = view;
|
|
1046
|
+
action_ = action;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
void GestureHandler::setRelations(std::vector<int> waitFor, std::vector<int> simultaneous, std::vector<int> blocks) {
|
|
1050
|
+
waitFor_ = std::move(waitFor);
|
|
1051
|
+
simultaneous_ = std::move(simultaneous);
|
|
1052
|
+
blocks_ = std::move(blocks);
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
bool GestureHandler::shouldWaitFor(const GestureHandler &other) const {
|
|
1056
|
+
return &other != this && contains(waitFor_, other.tag_);
|
|
1057
|
+
}
|
|
1058
|
+
bool GestureHandler::requiresToWaitFor(const GestureHandler &other) const {
|
|
1059
|
+
return &other != this && contains(blocks_, other.tag_);
|
|
1060
|
+
}
|
|
1061
|
+
bool GestureHandler::recognizesSimultaneously(const GestureHandler &other) const {
|
|
1062
|
+
return &other == this || contains(simultaneous_, other.tag_);
|
|
1063
|
+
}
|
|
1064
|
+
bool GestureHandler::cancelledBy(const GestureHandler &other) const {
|
|
1065
|
+
// As the library's InteractionManager: an active native view's recogniser takes over from the
|
|
1066
|
+
// handlers it does not run beside.
|
|
1067
|
+
return &other != this && other.isNative() && other.active;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
bool GestureHandler::sharesPointers(const GestureHandler &other) const {
|
|
1071
|
+
return std::any_of(pointers_.begin(), pointers_.end(), [&](const auto &entry) { return other.tracks(entry.first); });
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
bool GestureHandler::buttonAllowed(int button) const {
|
|
1075
|
+
int bit = button == 0 ? kLeft : button == 2 ? kRight : kMiddle;
|
|
1076
|
+
return mouseButton_ ? (*mouseButton_ & bit) != 0 : bit == kLeft;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
void GestureHandler::pointerDown(const GesturePointer &pointer) {
|
|
1080
|
+
owner_.record(*this);
|
|
1081
|
+
if (needsPointerData_) {
|
|
1082
|
+
sendTouches(1, pointer);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
void GestureHandler::pointerMove(const GesturePointer &pointer, bool inBounds) {
|
|
1087
|
+
if ((!inBounds && shouldCancelWhenOutside_) || !enabled_) {
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
1090
|
+
if (needsPointerData_) {
|
|
1091
|
+
sendTouches(2, pointer);
|
|
1092
|
+
}
|
|
1093
|
+
if (active) {
|
|
1094
|
+
owner_.send(*this, state_, state_);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
void GestureHandler::pointerUp(const GesturePointer &pointer) {
|
|
1099
|
+
if (needsPointerData_) {
|
|
1100
|
+
sendTouches(3, pointer);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
void GestureHandler::pointerCancel() {
|
|
1105
|
+
if (owner_.recorded(*this)) {
|
|
1106
|
+
cancel();
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
void GestureHandler::track(const GesturePointer &pointer) {
|
|
1111
|
+
auto &tracked = pointers_[pointer.id];
|
|
1112
|
+
tracked.page = pointer.page;
|
|
1113
|
+
tracked.samples.emplace_back(pointer.time, pointer.page);
|
|
1114
|
+
while (tracked.samples.size() > 1 && pointer.time - tracked.samples.front().first > kVelocityWindowMs) {
|
|
1115
|
+
tracked.samples.pop_front();
|
|
1116
|
+
}
|
|
1117
|
+
lastAverage_ = average();
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
void GestureHandler::untrack(int pointerId) {
|
|
1121
|
+
pointers_.erase(pointerId);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
Point GestureHandler::average() const {
|
|
1125
|
+
if (pointers_.empty()) {
|
|
1126
|
+
return lastAverage_;
|
|
1127
|
+
}
|
|
1128
|
+
Point sum{0, 0};
|
|
1129
|
+
for (const auto &[id, tracked] : pointers_) {
|
|
1130
|
+
sum.x += tracked.page.x;
|
|
1131
|
+
sum.y += tracked.page.y;
|
|
1132
|
+
}
|
|
1133
|
+
auto n = static_cast<facebook::react::Float>(pointers_.size());
|
|
1134
|
+
return {sum.x / n, sum.y / n};
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
Point GestureHandler::velocity() const {
|
|
1138
|
+
// Points a second, over the samples of the last 100 ms, averaged over the pointers.
|
|
1139
|
+
Point sum{0, 0};
|
|
1140
|
+
int counted = 0;
|
|
1141
|
+
for (const auto &[id, tracked] : pointers_) {
|
|
1142
|
+
if (tracked.samples.size() < 2) {
|
|
1143
|
+
continue;
|
|
1144
|
+
}
|
|
1145
|
+
const auto &[t0, p0] = tracked.samples.front();
|
|
1146
|
+
const auto &[t1, p1] = tracked.samples.back();
|
|
1147
|
+
if (t1 <= t0) {
|
|
1148
|
+
continue;
|
|
1149
|
+
}
|
|
1150
|
+
sum.x += (p1.x - p0.x) / (t1 - t0) * 1000;
|
|
1151
|
+
sum.y += (p1.y - p0.y) / (t1 - t0) * 1000;
|
|
1152
|
+
++counted;
|
|
1153
|
+
}
|
|
1154
|
+
return counted ? Point{sum.x / counted, sum.y / counted} : Point{0, 0};
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
Point GestureHandler::local(Point page) const {
|
|
1158
|
+
if (auto at = owner_.tree().pointIn(view_, owner_.surface(), page)) {
|
|
1159
|
+
return *at;
|
|
1160
|
+
}
|
|
1161
|
+
auto origin = owner_.tree().originIn(view_, owner_.surface()).value_or(Point{0, 0});
|
|
1162
|
+
return {page.x - origin.x, page.y - origin.y};
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
void GestureHandler::after(double ms, std::function<void()> fn) {
|
|
1166
|
+
clearTimer();
|
|
1167
|
+
if (!owner_.schedule()) {
|
|
1168
|
+
return;
|
|
1169
|
+
}
|
|
1170
|
+
cancelTimer_ = owner_.schedule()(
|
|
1171
|
+
ms, [owner = &owner_, due = owner_.now() + ms, fn = std::move(fn)] { owner->runScheduled(due, fn); });
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
bool GestureHandler::leaveBounds() {
|
|
1175
|
+
if (!shouldCancelWhenOutside_) {
|
|
1176
|
+
return false;
|
|
1177
|
+
}
|
|
1178
|
+
if (state_ == GestureState::Active) {
|
|
1179
|
+
cancel();
|
|
1180
|
+
} else if (state_ == GestureState::Began) {
|
|
1181
|
+
fail();
|
|
1182
|
+
}
|
|
1183
|
+
return true;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
void GestureHandler::clearTimer() {
|
|
1187
|
+
if (auto cancel = std::exchange(cancelTimer_, nullptr)) {
|
|
1188
|
+
cancel();
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
void GestureHandler::moveToState(GestureState next) {
|
|
1193
|
+
if (state_ == next) {
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
auto previous = state_;
|
|
1197
|
+
state_ = next;
|
|
1198
|
+
owner_.stateChanged(*this, next, previous);
|
|
1199
|
+
onStateChange(next, previous);
|
|
1200
|
+
if (!enabled_ && (next == GestureState::End || next == GestureState::Failed || next == GestureState::Cancelled)) {
|
|
1201
|
+
state_ = GestureState::Undetermined;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
void GestureHandler::begin() {
|
|
1206
|
+
if (state_ == GestureState::Undetermined) {
|
|
1207
|
+
moveToState(GestureState::Began);
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
void GestureHandler::fail() {
|
|
1212
|
+
if (state_ == GestureState::Active || state_ == GestureState::Began) {
|
|
1213
|
+
moveToState(GestureState::Failed);
|
|
1214
|
+
}
|
|
1215
|
+
resetProgress();
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
void GestureHandler::cancel() {
|
|
1219
|
+
if (state_ == GestureState::Active || state_ == GestureState::Undetermined || state_ == GestureState::Began) {
|
|
1220
|
+
onCancel();
|
|
1221
|
+
moveToState(GestureState::Cancelled);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
void GestureHandler::activate(bool force) {
|
|
1226
|
+
if ((!manualActivation_ || force) && state_ == GestureState::Began) {
|
|
1227
|
+
moveToState(GestureState::Active);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
void GestureHandler::end() {
|
|
1232
|
+
if (state_ == GestureState::Began || state_ == GestureState::Active) {
|
|
1233
|
+
moveToState(GestureState::End);
|
|
1234
|
+
}
|
|
1235
|
+
resetProgress();
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
void GestureHandler::reset() {
|
|
1239
|
+
clearTimer();
|
|
1240
|
+
pointers_.clear();
|
|
1241
|
+
onReset();
|
|
1242
|
+
resetProgress();
|
|
1243
|
+
state_ = GestureState::Undetermined;
|
|
1244
|
+
lastSent_.reset();
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
folly::dynamic GestureHandler::extraData() const {
|
|
1248
|
+
auto at = local(lastAverage_);
|
|
1249
|
+
return folly::dynamic::object("x", at.x)("y", at.y)("absoluteX", lastAverage_.x)(
|
|
1250
|
+
"absoluteY", lastAverage_.y)("numberOfPointers", static_cast<int>(pointers_.size()))("pointerType", kMouse);
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
folly::dynamic GestureHandler::eventData() const {
|
|
1254
|
+
auto data = extraData();
|
|
1255
|
+
data["target"] = view_;
|
|
1256
|
+
data["handlerTag"] = tag_;
|
|
1257
|
+
data["state"] = static_cast<int>(state_);
|
|
1258
|
+
return data;
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
void GestureHandler::sendTouches(int eventType, const GesturePointer &changed) {
|
|
1262
|
+
auto touch = [&](int id, Point page) {
|
|
1263
|
+
auto at = local(page);
|
|
1264
|
+
return folly::dynamic::object("id", id)("x", at.x)("y", at.y)("absoluteX", page.x)("absoluteY", page.y);
|
|
1265
|
+
};
|
|
1266
|
+
folly::dynamic all = folly::dynamic::array();
|
|
1267
|
+
for (const auto &[id, tracked] : pointers_) {
|
|
1268
|
+
all.push_back(touch(id, tracked.page));
|
|
1269
|
+
}
|
|
1270
|
+
int count = static_cast<int>(pointers_.size()) - (eventType == 3 ? 1 : 0);
|
|
1271
|
+
auto body = folly::dynamic::object("handlerTag", tag_)("state", static_cast<int>(state_))("eventType", eventType)(
|
|
1272
|
+
"allTouches", all)("changedTouches", folly::dynamic::array(touch(changed.id, changed.page)))(
|
|
1273
|
+
"numberOfTouches", count)("pointerType", kMouse)("target", view_);
|
|
1274
|
+
owner_.sendEvent(*this, "onGestureHandlerEvent", std::move(body));
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
// --- Gestures --------------------------------------------------------------------------------------
|
|
1278
|
+
|
|
1279
|
+
Gestures::Gestures(const ViewTree &tree, Schedule schedule) : tree_(tree), schedule_(std::move(schedule)) {}
|
|
1280
|
+
|
|
1281
|
+
Gestures::~Gestures() {
|
|
1282
|
+
for (auto &[tag, handler] : handlers_) {
|
|
1283
|
+
handler->reset();
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
GestureHandler *Gestures::find(int handlerTag) const {
|
|
1288
|
+
auto it = handlers_.find(handlerTag);
|
|
1289
|
+
return it == handlers_.end() ? nullptr : it->second.get();
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
void Gestures::create(const std::string &name, int handlerTag, const folly::dynamic &config) {
|
|
1293
|
+
auto handler = makeHandler(*this, name, handlerTag);
|
|
1294
|
+
if (!handler) {
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
drop(handlerTag);
|
|
1298
|
+
handler->configure(config, true);
|
|
1299
|
+
handlers_[handlerTag] = std::move(handler);
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
void Gestures::attach(int handlerTag, Tag view, GestureAction action) {
|
|
1303
|
+
if (auto *handler = find(handlerTag)) {
|
|
1304
|
+
handler->attach(view, action);
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
void Gestures::configure(int handlerTag, const folly::dynamic &config, bool reset) {
|
|
1309
|
+
if (auto *handler = find(handlerTag)) {
|
|
1310
|
+
handler->configure(config, reset);
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
void Gestures::relations(int handlerTag, const folly::dynamic &config) {
|
|
1315
|
+
if (auto *handler = find(handlerTag)) {
|
|
1316
|
+
handler->setRelations(tags(config, "waitFor"), tags(config, "simultaneousHandlers"), tags(config, "blocksHandlers"));
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
void Gestures::drop(int handlerTag) {
|
|
1321
|
+
auto it = handlers_.find(handlerTag);
|
|
1322
|
+
if (it == handlers_.end()) {
|
|
1323
|
+
return;
|
|
1324
|
+
}
|
|
1325
|
+
auto *handler = it->second.get();
|
|
1326
|
+
handler->reset();
|
|
1327
|
+
std::erase(recorded_, handler);
|
|
1328
|
+
std::erase(awaiting_, handler);
|
|
1329
|
+
for (auto *list : {&pressed_, &hovered_, &scrolling_, &magnifying_, &rotating_}) {
|
|
1330
|
+
std::erase(*list, handlerTag);
|
|
1331
|
+
}
|
|
1332
|
+
handlers_.erase(it);
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
void Gestures::clear() {
|
|
1336
|
+
while (!handlers_.empty()) {
|
|
1337
|
+
drop(handlers_.begin()->first);
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
void Gestures::setState(int handlerTag, int state) {
|
|
1342
|
+
auto *handler = find(handlerTag);
|
|
1343
|
+
if (!handler) {
|
|
1344
|
+
return;
|
|
1345
|
+
}
|
|
1346
|
+
switch (static_cast<GestureState>(state)) {
|
|
1347
|
+
case GestureState::Began:
|
|
1348
|
+
handler->begin();
|
|
1349
|
+
break;
|
|
1350
|
+
case GestureState::Active:
|
|
1351
|
+
handler->activate(true);
|
|
1352
|
+
break;
|
|
1353
|
+
case GestureState::End:
|
|
1354
|
+
handler->end();
|
|
1355
|
+
break;
|
|
1356
|
+
case GestureState::Failed:
|
|
1357
|
+
handler->fail();
|
|
1358
|
+
break;
|
|
1359
|
+
case GestureState::Cancelled:
|
|
1360
|
+
handler->cancel();
|
|
1361
|
+
break;
|
|
1362
|
+
default:
|
|
1363
|
+
break;
|
|
1364
|
+
}
|
|
1365
|
+
settle();
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
std::vector<GestureHandler *> Gestures::handlersOn(const std::vector<Tag> &chain) const {
|
|
1369
|
+
// Deepest first, as the event reaches a view's own listeners before its ancestors'.
|
|
1370
|
+
std::vector<GestureHandler *> out;
|
|
1371
|
+
for (auto view = chain.rbegin(); view != chain.rend(); ++view) {
|
|
1372
|
+
for (const auto &[tag, handler] : handlers_) {
|
|
1373
|
+
if (handler->view() == *view && handler->enabled()) {
|
|
1374
|
+
out.push_back(handler.get());
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
return out;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
bool Gestures::handles(const std::vector<Tag> &chain) const {
|
|
1382
|
+
return !handlersOn(chain).empty();
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
bool Gestures::inBounds(const GestureHandler &handler, Point page) const {
|
|
1386
|
+
const auto *node = tree_.find(handler.view());
|
|
1387
|
+
auto at = tree_.pointIn(handler.view(), surface_, page);
|
|
1388
|
+
if (!node || !at) {
|
|
1389
|
+
return false;
|
|
1390
|
+
}
|
|
1391
|
+
const auto &size = node->view.layoutMetrics.frame.size;
|
|
1392
|
+
return at->x >= 0 && at->y >= 0 && at->x <= size.width && at->y <= size.height;
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
void Gestures::pointerDown(SurfaceId surface, const std::vector<Tag> &chain, const GesturePointer &pointer) {
|
|
1396
|
+
surface_ = surface;
|
|
1397
|
+
now_ = pointer.time;
|
|
1398
|
+
pressed_.clear();
|
|
1399
|
+
for (auto *handler : handlersOn(chain)) {
|
|
1400
|
+
pressed_.push_back(handler->tag());
|
|
1401
|
+
}
|
|
1402
|
+
for (int tag : std::vector<int>(pressed_)) {
|
|
1403
|
+
if (auto *handler = find(tag)) {
|
|
1404
|
+
handler->pointerDown(pointer);
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
settle();
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
void Gestures::pointerMove(const GesturePointer &pointer) {
|
|
1411
|
+
now_ = pointer.time;
|
|
1412
|
+
for (int tag : std::vector<int>(pressed_)) {
|
|
1413
|
+
if (auto *handler = find(tag)) {
|
|
1414
|
+
bool inside = inBounds(*handler, pointer.page);
|
|
1415
|
+
if (!inside && handler->leaveBounds()) {
|
|
1416
|
+
continue;
|
|
1417
|
+
}
|
|
1418
|
+
handler->pointerMove(pointer, inside);
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
settle();
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
void Gestures::pointerUp(const GesturePointer &pointer) {
|
|
1425
|
+
now_ = pointer.time;
|
|
1426
|
+
auto pressed = std::exchange(pressed_, {});
|
|
1427
|
+
for (int tag : pressed) {
|
|
1428
|
+
if (auto *handler = find(tag)) {
|
|
1429
|
+
// A release outside is no tap: the handler leaves first, with no move to have done it.
|
|
1430
|
+
if (!inBounds(*handler, pointer.page)) {
|
|
1431
|
+
handler->leaveBounds();
|
|
1432
|
+
}
|
|
1433
|
+
handler->pointerUp(pointer);
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
settle();
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
void Gestures::runScheduled(double due, const std::function<void()> &fn) {
|
|
1440
|
+
now_ = std::max(now_, due);
|
|
1441
|
+
fn();
|
|
1442
|
+
settle();
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
void Gestures::pointerCancel() {
|
|
1446
|
+
auto pressed = std::exchange(pressed_, {});
|
|
1447
|
+
for (int tag : pressed) {
|
|
1448
|
+
if (auto *handler = find(tag)) {
|
|
1449
|
+
handler->pointerCancel();
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
settle();
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
void Gestures::hover(SurfaceId surface, const std::vector<Tag> &chain, const GesturePointer &pointer) {
|
|
1456
|
+
surface_ = surface;
|
|
1457
|
+
now_ = pointer.time;
|
|
1458
|
+
std::vector<int> over;
|
|
1459
|
+
for (auto *handler : handlersOn(chain)) {
|
|
1460
|
+
if (handler->wantsHover()) {
|
|
1461
|
+
over.push_back(handler->tag());
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
for (int tag : std::vector<int>(hovered_)) {
|
|
1465
|
+
if (!contains(over, tag)) {
|
|
1466
|
+
if (auto *handler = find(tag)) {
|
|
1467
|
+
handler->hover(pointer, false);
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
for (int tag : over) {
|
|
1472
|
+
if (auto *handler = find(tag)) {
|
|
1473
|
+
handler->hover(pointer, true);
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
hovered_ = over;
|
|
1477
|
+
settle();
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
bool Gestures::scroll(
|
|
1481
|
+
SurfaceId surface,
|
|
1482
|
+
const std::vector<Tag> &chain,
|
|
1483
|
+
const GesturePointer &pointer,
|
|
1484
|
+
Point delta,
|
|
1485
|
+
std::optional<GesturePhase> phase) {
|
|
1486
|
+
surface_ = surface;
|
|
1487
|
+
now_ = pointer.time;
|
|
1488
|
+
// The handlers a scroll begins on keep it until it ends, wherever the pointer goes. A wheel's
|
|
1489
|
+
// turns have no phases, and go to the handlers under the pointer at each.
|
|
1490
|
+
if (!phase || phase == GesturePhase::Began || scrolling_.empty()) {
|
|
1491
|
+
scrolling_.clear();
|
|
1492
|
+
for (auto *handler : handlersOn(chain)) {
|
|
1493
|
+
if (handler->wantsScroll()) {
|
|
1494
|
+
scrolling_.push_back(handler->tag());
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
bool took = false;
|
|
1499
|
+
for (int tag : std::vector<int>(scrolling_)) {
|
|
1500
|
+
if (auto *handler = find(tag)) {
|
|
1501
|
+
handler->scroll(pointer, delta, phase);
|
|
1502
|
+
took = took || (!handler->isNative() && handler->state() == GestureState::Active);
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
if (phase == GesturePhase::Ended || phase == GesturePhase::Cancelled) {
|
|
1506
|
+
scrolling_.clear();
|
|
1507
|
+
}
|
|
1508
|
+
settle();
|
|
1509
|
+
return took;
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
void Gestures::magnify(
|
|
1513
|
+
SurfaceId surface,
|
|
1514
|
+
const std::vector<Tag> &chain,
|
|
1515
|
+
const GesturePointer &pointer,
|
|
1516
|
+
double magnification,
|
|
1517
|
+
GesturePhase phase) {
|
|
1518
|
+
surface_ = surface;
|
|
1519
|
+
now_ = pointer.time;
|
|
1520
|
+
if (phase == GesturePhase::Began) {
|
|
1521
|
+
magnifying_.clear();
|
|
1522
|
+
for (auto *handler : handlersOn(chain)) {
|
|
1523
|
+
if (handler->wantsMagnify()) {
|
|
1524
|
+
magnifying_.push_back(handler->tag());
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
for (int tag : std::vector<int>(magnifying_)) {
|
|
1529
|
+
if (auto *handler = find(tag)) {
|
|
1530
|
+
handler->magnify(pointer, magnification, phase);
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
if (phase == GesturePhase::Ended || phase == GesturePhase::Cancelled) {
|
|
1534
|
+
magnifying_.clear();
|
|
1535
|
+
}
|
|
1536
|
+
settle();
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
void Gestures::rotate(
|
|
1540
|
+
SurfaceId surface,
|
|
1541
|
+
const std::vector<Tag> &chain,
|
|
1542
|
+
const GesturePointer &pointer,
|
|
1543
|
+
double degrees,
|
|
1544
|
+
GesturePhase phase) {
|
|
1545
|
+
surface_ = surface;
|
|
1546
|
+
now_ = pointer.time;
|
|
1547
|
+
if (phase == GesturePhase::Began) {
|
|
1548
|
+
rotating_.clear();
|
|
1549
|
+
for (auto *handler : handlersOn(chain)) {
|
|
1550
|
+
if (handler->wantsRotate()) {
|
|
1551
|
+
rotating_.push_back(handler->tag());
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
for (int tag : std::vector<int>(rotating_)) {
|
|
1556
|
+
if (auto *handler = find(tag)) {
|
|
1557
|
+
handler->rotate(pointer, degrees, phase);
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
if (phase == GesturePhase::Ended || phase == GesturePhase::Cancelled) {
|
|
1561
|
+
rotating_.clear();
|
|
1562
|
+
}
|
|
1563
|
+
settle();
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
// --- the orchestrator (GestureHandlerOrchestrator.ts) -------------------------------------------
|
|
1567
|
+
|
|
1568
|
+
bool Gestures::finished(GestureState state) {
|
|
1569
|
+
return state == GestureState::End || state == GestureState::Failed || state == GestureState::Cancelled;
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
bool Gestures::recorded(const GestureHandler &handler) const {
|
|
1573
|
+
return std::find(recorded_.begin(), recorded_.end(), &handler) != recorded_.end();
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
void Gestures::record(GestureHandler &handler) {
|
|
1577
|
+
// Handlers that never began and track nothing drop out first.
|
|
1578
|
+
std::erase_if(recorded_, [](GestureHandler *h) {
|
|
1579
|
+
return h->state() == GestureState::Undetermined && h->trackedPointers() == 0 && !h->wantsHover() && !h->wantsScroll() &&
|
|
1580
|
+
!h->wantsMagnify() && !h->wantsRotate();
|
|
1581
|
+
});
|
|
1582
|
+
if (recorded(handler)) {
|
|
1583
|
+
return;
|
|
1584
|
+
}
|
|
1585
|
+
handler.active = false;
|
|
1586
|
+
handler.awaiting = false;
|
|
1587
|
+
handler.activationIndex = std::numeric_limits<size_t>::max();
|
|
1588
|
+
recorded_.push_back(&handler);
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
void Gestures::cleanHandler(GestureHandler &handler) {
|
|
1592
|
+
handler.reset();
|
|
1593
|
+
handler.active = false;
|
|
1594
|
+
handler.awaiting = false;
|
|
1595
|
+
handler.activationIndex = std::numeric_limits<size_t>::max();
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
void Gestures::cleanup() {
|
|
1599
|
+
std::vector<GestureHandler *> keep;
|
|
1600
|
+
for (auto *handler : recorded_) {
|
|
1601
|
+
if (finished(handler->state()) && !handler->awaiting) {
|
|
1602
|
+
cleanHandler(*handler);
|
|
1603
|
+
} else {
|
|
1604
|
+
keep.push_back(handler);
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
recorded_ = std::move(keep);
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
void Gestures::settle() {
|
|
1611
|
+
if (changing_ == 0) {
|
|
1612
|
+
cleanup();
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
bool Gestures::waitsFor(const GestureHandler &handler, const GestureHandler &other) const {
|
|
1617
|
+
return &handler != &other && (handler.shouldWaitFor(other) || other.requiresToWaitFor(handler));
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
bool Gestures::simultaneous(const GestureHandler &a, const GestureHandler &b) const {
|
|
1621
|
+
return &a == &b || a.recognizesSimultaneously(b) || b.recognizesSimultaneously(a);
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
bool Gestures::cancelledBy(const GestureHandler &handler, const GestureHandler &other) const {
|
|
1625
|
+
if (simultaneous(handler, other)) {
|
|
1626
|
+
return false;
|
|
1627
|
+
}
|
|
1628
|
+
if (handler.awaiting || handler.state() == GestureState::Active) {
|
|
1629
|
+
return handler.cancelledBy(other);
|
|
1630
|
+
}
|
|
1631
|
+
// Handlers with no pointer in common on different views (a hover beside a press, a pinch beside
|
|
1632
|
+
// a drag) are not rivals; the library checks such handlers' bounds for overlap, which the
|
|
1633
|
+
// desktop's one pointer never reaches.
|
|
1634
|
+
if (!handler.sharesPointers(other) && handler.view() != other.view()) {
|
|
1635
|
+
return false;
|
|
1636
|
+
}
|
|
1637
|
+
return true;
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
bool Gestures::hasToWait(const GestureHandler &handler) const {
|
|
1641
|
+
return std::any_of(recorded_.begin(), recorded_.end(), [&](GestureHandler *other) {
|
|
1642
|
+
return !finished(other->state()) && waitsFor(handler, *other);
|
|
1643
|
+
});
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
bool Gestures::cancelledByFinished(const GestureHandler &handler) const {
|
|
1647
|
+
return std::any_of(recorded_.begin(), recorded_.end(), [&](GestureHandler *other) {
|
|
1648
|
+
return waitsFor(handler, *other) && other->state() == GestureState::End;
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
bool Gestures::shouldActivate(GestureHandler &handler) const {
|
|
1653
|
+
return std::none_of(recorded_.begin(), recorded_.end(), [&](GestureHandler *other) {
|
|
1654
|
+
return other != &handler && cancelledBy(handler, *other);
|
|
1655
|
+
});
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
void Gestures::tryActivate(GestureHandler &handler) {
|
|
1659
|
+
if (cancelledByFinished(handler)) {
|
|
1660
|
+
handler.cancel();
|
|
1661
|
+
return;
|
|
1662
|
+
}
|
|
1663
|
+
if (hasToWait(handler)) {
|
|
1664
|
+
addAwaiting(handler);
|
|
1665
|
+
return;
|
|
1666
|
+
}
|
|
1667
|
+
auto state = handler.state();
|
|
1668
|
+
if (state == GestureState::Cancelled || state == GestureState::Failed) {
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1671
|
+
if (shouldActivate(handler)) {
|
|
1672
|
+
makeActive(handler);
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
if (state == GestureState::Active) {
|
|
1676
|
+
handler.fail();
|
|
1677
|
+
} else if (state == GestureState::Began) {
|
|
1678
|
+
handler.cancel();
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
void Gestures::addAwaiting(GestureHandler &handler) {
|
|
1683
|
+
if (std::find(awaiting_.begin(), awaiting_.end(), &handler) != awaiting_.end()) {
|
|
1684
|
+
return;
|
|
1685
|
+
}
|
|
1686
|
+
awaiting_.push_back(&handler);
|
|
1687
|
+
handler.awaiting = true;
|
|
1688
|
+
handler.activationIndex = activationIndex_++;
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
void Gestures::cleanupAwaiting(GestureHandler &handler) {
|
|
1692
|
+
std::erase_if(awaiting_, [&](GestureHandler *other) {
|
|
1693
|
+
if (!other->awaiting && waitsFor(*other, handler)) {
|
|
1694
|
+
cleanHandler(*other);
|
|
1695
|
+
return true;
|
|
1696
|
+
}
|
|
1697
|
+
return false;
|
|
1698
|
+
});
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
void Gestures::makeActive(GestureHandler &handler) {
|
|
1702
|
+
auto current = handler.state();
|
|
1703
|
+
handler.active = true;
|
|
1704
|
+
handler.activationIndex = activationIndex_++;
|
|
1705
|
+
for (auto it = recorded_.rbegin(); it != recorded_.rend(); ++it) {
|
|
1706
|
+
if (*it != &handler && cancelledBy(**it, handler)) {
|
|
1707
|
+
(*it)->cancel();
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
for (auto *other : awaiting_) {
|
|
1711
|
+
if (cancelledBy(*other, handler)) {
|
|
1712
|
+
other->awaiting = false;
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
if (!handler.isNative() && onClaim) {
|
|
1716
|
+
onClaim();
|
|
1717
|
+
}
|
|
1718
|
+
send(handler, GestureState::Active, GestureState::Began);
|
|
1719
|
+
if (current != GestureState::Active) {
|
|
1720
|
+
send(handler, GestureState::End, GestureState::Active);
|
|
1721
|
+
if (current != GestureState::End) {
|
|
1722
|
+
send(handler, GestureState::Undetermined, GestureState::End);
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
if (handler.awaiting) {
|
|
1726
|
+
handler.awaiting = false;
|
|
1727
|
+
std::erase(awaiting_, &handler);
|
|
1728
|
+
}
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
void Gestures::stateChanged(GestureHandler &handler, GestureState next, GestureState previous) {
|
|
1732
|
+
++changing_;
|
|
1733
|
+
if (finished(next)) {
|
|
1734
|
+
for (auto *other : std::vector<GestureHandler *>(awaiting_)) {
|
|
1735
|
+
if (!waitsFor(*other, handler) || !other->awaiting) {
|
|
1736
|
+
continue;
|
|
1737
|
+
}
|
|
1738
|
+
if (next != GestureState::End) {
|
|
1739
|
+
tryActivate(*other);
|
|
1740
|
+
continue;
|
|
1741
|
+
}
|
|
1742
|
+
other->cancel();
|
|
1743
|
+
// A discrete handler that ended while it waited was never reported active: it is told it
|
|
1744
|
+
// was cancelled from where it began, as the library does.
|
|
1745
|
+
if (other->state() == GestureState::End) {
|
|
1746
|
+
send(*other, GestureState::Cancelled, GestureState::Began);
|
|
1747
|
+
}
|
|
1748
|
+
other->awaiting = false;
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
if (next == GestureState::Active) {
|
|
1752
|
+
tryActivate(handler);
|
|
1753
|
+
} else if (previous == GestureState::Active || previous == GestureState::End) {
|
|
1754
|
+
if (handler.active) {
|
|
1755
|
+
send(handler, next, previous);
|
|
1756
|
+
} else if (previous == GestureState::Active && (next == GestureState::Cancelled || next == GestureState::Failed)) {
|
|
1757
|
+
send(handler, next, GestureState::Began);
|
|
1758
|
+
}
|
|
1759
|
+
} else if (previous != GestureState::Undetermined || next != GestureState::Cancelled) {
|
|
1760
|
+
send(handler, next, previous);
|
|
1761
|
+
}
|
|
1762
|
+
--changing_;
|
|
1763
|
+
if (std::find(awaiting_.begin(), awaiting_.end(), &handler) == awaiting_.end()) {
|
|
1764
|
+
cleanupAwaiting(handler);
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
void Gestures::send(GestureHandler &handler, GestureState next, GestureState previous) {
|
|
1769
|
+
// makeActive's own sequence (ACTIVE, then END and UNDETERMINED for a handler that already ended)
|
|
1770
|
+
// is the library's: a discrete handler that activated while waiting reports each step.
|
|
1771
|
+
if (next == previous) {
|
|
1772
|
+
if (handler.state() == GestureState::Active) {
|
|
1773
|
+
auto body = handler.eventData();
|
|
1774
|
+
sendEvent(handler, "onGestureHandlerEvent", std::move(body));
|
|
1775
|
+
}
|
|
1776
|
+
return;
|
|
1777
|
+
}
|
|
1778
|
+
auto body = handler.eventData();
|
|
1779
|
+
body["state"] = static_cast<int>(next);
|
|
1780
|
+
body["oldState"] = static_cast<int>(previous);
|
|
1781
|
+
sendEvent(handler, "onGestureHandlerStateChange", body);
|
|
1782
|
+
if (next == GestureState::Active) {
|
|
1783
|
+
body.erase("oldState");
|
|
1784
|
+
sendEvent(handler, "onGestureHandlerEvent", std::move(body));
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
void Gestures::sendEvent(GestureHandler &handler, const std::string &name, folly::dynamic body) {
|
|
1789
|
+
switch (handler.action()) {
|
|
1790
|
+
case GestureAction::ReanimatedWorklet:
|
|
1791
|
+
if (onWorkletEvent) {
|
|
1792
|
+
onWorkletEvent(name, handler.view(), body);
|
|
1793
|
+
}
|
|
1794
|
+
break;
|
|
1795
|
+
case GestureAction::JsFunctionOldApi:
|
|
1796
|
+
case GestureAction::JsFunctionNewApi:
|
|
1797
|
+
if (onDeviceEvent) {
|
|
1798
|
+
onDeviceEvent(body);
|
|
1799
|
+
}
|
|
1800
|
+
break;
|
|
1801
|
+
default:
|
|
1802
|
+
break;
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
} // namespace ngn
|