Skip to content

Commit

Permalink
Move TouchHistoryMath from React Repo to React Native
Browse files Browse the repository at this point in the history
Summary:
This hasn't been used by React core for a while. It's unclear why it was in the first place.

We can move it back next to the Panresponder.

facebook/react#12557

Reviewed By: sophiebits

Differential Revision: D7529949

fbshipit-source-id: ef9892cfa8d2d6768da216b81befabe02795a245
  • Loading branch information
sebmarkbage authored and facebook-github-bot committed Apr 6, 2018
1 parent 445b0c7 commit 06085d3
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 17 deletions.
151 changes: 151 additions & 0 deletions Libraries/Interaction/TouchHistoryMath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const TouchHistoryMath = {
/**
* This code is optimized and not intended to look beautiful. This allows
* computing of touch centroids that have moved after `touchesChangedAfter`
* timeStamp. You can compute the current centroid involving all touches
* moves after `touchesChangedAfter`, or you can compute the previous
* centroid of all touches that were moved after `touchesChangedAfter`.
*
* @param {TouchHistoryMath} touchHistory Standard Responder touch track
* data.
* @param {number} touchesChangedAfter timeStamp after which moved touches
* are considered "actively moving" - not just "active".
* @param {boolean} isXAxis Consider `x` dimension vs. `y` dimension.
* @param {boolean} ofCurrent Compute current centroid for actively moving
* touches vs. previous centroid of now actively moving touches.
* @return {number} value of centroid in specified dimension.
*/
centroidDimension: function(
touchHistory,
touchesChangedAfter,
isXAxis,
ofCurrent,
) {
const touchBank = touchHistory.touchBank;
let total = 0;
let count = 0;

const oneTouchData =
touchHistory.numberActiveTouches === 1
? touchHistory.touchBank[touchHistory.indexOfSingleActiveTouch]
: null;

if (oneTouchData !== null) {
if (
oneTouchData.touchActive &&
oneTouchData.currentTimeStamp > touchesChangedAfter
) {
total +=
ofCurrent && isXAxis
? oneTouchData.currentPageX
: ofCurrent && !isXAxis
? oneTouchData.currentPageY
: !ofCurrent && isXAxis
? oneTouchData.previousPageX
: oneTouchData.previousPageY;
count = 1;
}
} else {
for (let i = 0; i < touchBank.length; i++) {
const touchTrack = touchBank[i];
if (
touchTrack !== null &&
touchTrack !== undefined &&
touchTrack.touchActive &&
touchTrack.currentTimeStamp >= touchesChangedAfter
) {
let toAdd; // Yuck, program temporarily in invalid state.
if (ofCurrent && isXAxis) {
toAdd = touchTrack.currentPageX;
} else if (ofCurrent && !isXAxis) {
toAdd = touchTrack.currentPageY;
} else if (!ofCurrent && isXAxis) {
toAdd = touchTrack.previousPageX;
} else {
toAdd = touchTrack.previousPageY;
}
total += toAdd;
count++;
}
}
}
return count > 0 ? total / count : TouchHistoryMath.noCentroid;
},

currentCentroidXOfTouchesChangedAfter: function(
touchHistory,
touchesChangedAfter,
) {
return TouchHistoryMath.centroidDimension(
touchHistory,
touchesChangedAfter,
true, // isXAxis
true, // ofCurrent
);
},

currentCentroidYOfTouchesChangedAfter: function(
touchHistory,
touchesChangedAfter,
) {
return TouchHistoryMath.centroidDimension(
touchHistory,
touchesChangedAfter,
false, // isXAxis
true, // ofCurrent
);
},

previousCentroidXOfTouchesChangedAfter: function(
touchHistory,
touchesChangedAfter,
) {
return TouchHistoryMath.centroidDimension(
touchHistory,
touchesChangedAfter,
true, // isXAxis
false, // ofCurrent
);
},

previousCentroidYOfTouchesChangedAfter: function(
touchHistory,
touchesChangedAfter,
) {
return TouchHistoryMath.centroidDimension(
touchHistory,
touchesChangedAfter,
false, // isXAxis
false, // ofCurrent
);
},

currentCentroidX: function(touchHistory) {
return TouchHistoryMath.centroidDimension(
touchHistory,
0, // touchesChangedAfter
true, // isXAxis
true, // ofCurrent
);
},

currentCentroidY: function(touchHistory) {
return TouchHistoryMath.centroidDimension(
touchHistory,
0, // touchesChangedAfter
false, // isXAxis
true, // ofCurrent
);
},

noCentroid: -1,
};

module.exports = TouchHistoryMath;
17 changes: 0 additions & 17 deletions Libraries/Renderer/shims/TouchHistoryMath.js

This file was deleted.

1 comment on commit 06085d3

@sjmueller
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to change the import in PanResponder.js to use a ./:

const TouchHistoryMath = require('./TouchHistoryMath');

Results in the following error:

Unable to resolve module TouchHistoryMath from /Users/sam/Repos/blink/blink-mobile/node_modules/react-native/Libraries/Interaction/PanResponder.js: Module TouchHistoryMath does not exist in the Haste module map

Please sign in to comment.