Skip to content

Commit

Permalink
Fixing a cursor position initialization problem
Browse files Browse the repository at this point in the history
This issue was caused during a recent refactoring.  This CL adds the
initialization code for Trackpad mode back.

BUG=628034

Review-Url: https://codereview.chromium.org/2146303002
Cr-Commit-Position: refs/heads/master@{#405610}
  • Loading branch information
joedow authored and Commit bot committed Jul 14, 2016
1 parent 6573f1e commit 1bbf9d2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ public PointF moveViewportCenter(boolean useScreenCenter, float deltaX, float de
return viewportCenter;
}

/**
* Sets the desired center position of the viewport.
*
* @param newX The new x coordinate value for the desired center position.
* @param newY The new y coordinate value for the desired center position.
*/
public void setViewportPosition(float newX, float newY) {
synchronized (mRenderData) {
mViewportPosition.set(newX, newY);
}
}

/**
* Returns the current size of the viewport. This size includes the offset calculations for
* any visible system UI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* are passed to the InputStrategyInterface implementation set by the DesktopView.
*/
public class TouchInputHandler {
private static final float EPSILON = 0.001f;

private final AbstractDesktopView mViewer;
private final Context mContext;
private final RenderData mRenderData;
Expand Down Expand Up @@ -361,8 +363,12 @@ private void resizeImageToFitScreen() {
synchronized (mRenderData) {
screenCenterX = (float) mRenderData.screenWidth / 2;
screenCenterY = (float) mRenderData.screenHeight / 2;

float[] imagePoint = mapScreenPointToImagePoint(screenCenterX, screenCenterY);
mDesktopCanvas.setViewportPosition(imagePoint[0], imagePoint[1]);
}
moveCursorToScreenPoint(screenCenterX, screenCenterY);
mDesktopCanvas.repositionImage(true);
}

private void setInputStrategy(InputStrategyInterface inputStrategy) {
Expand Down Expand Up @@ -399,13 +405,8 @@ private void moveViewportByOffset(float deltaX, float deltaY) {

/** Moves the cursor to the specified position on the screen. */
private void moveCursorToScreenPoint(float screenX, float screenY) {
float[] mappedValues = {screenX, screenY};
synchronized (mRenderData) {
Matrix canvasToImage = new Matrix();
mRenderData.transform.invert(canvasToImage);
canvasToImage.mapPoints(mappedValues);
}
moveCursor((int) mappedValues[0], (int) mappedValues[1]);
float[] imagePoint = mapScreenPointToImagePoint(screenX, screenY);
moveCursor((int) imagePoint[0], (int) imagePoint[1]);
}

/** Moves the cursor to the specified position on the remote host. */
Expand Down Expand Up @@ -437,6 +438,18 @@ private boolean onSwipe() {
return true;
}

/** Translates a point in screen coordinates to a location on the desktop image. */
private float[] mapScreenPointToImagePoint(float screenX, float screenY) {
float[] mappedPoints = {screenX, screenY};
Matrix screenToImage = new Matrix();
synchronized (mRenderData) {
mRenderData.transform.invert(screenToImage);
}
screenToImage.mapPoints(mappedPoints);

return mappedPoints;
}

/** Responds to touch events filtered by the gesture detectors. */
private class GestureListener extends GestureDetector.SimpleOnGestureListener
implements ScaleGestureDetector.OnScaleGestureListener,
Expand Down Expand Up @@ -570,7 +583,7 @@ public boolean onTap(int pointerCount, float x, float y) {
}

if (!mInputStrategy.isIndirectInputMode()) {
if (!mapScreenPointToImage(x, y)) {
if (screenPointLiesOutsideImageBoundary(x, y)) {
return false;
}
moveCursorToScreenPoint(x, y);
Expand All @@ -595,7 +608,7 @@ public void onLongPress(int pointerCount, float x, float y) {
}

if (!mInputStrategy.isIndirectInputMode()) {
if (!mapScreenPointToImage(x, y)) {
if (screenPointLiesOutsideImageBoundary(x, y)) {
return;
}
moveCursorToScreenPoint(x, y);
Expand Down Expand Up @@ -626,21 +639,18 @@ private int mouseButtonFromPointerCount(int pointerCount) {
}
}

/** Verifies the given point maps to a valid location within the desktop image. */
private boolean mapScreenPointToImage(float screenX, float screenY) {
float[] mappedPoints = {screenX, screenY};
int imageWidth;
int imageHeight;
Matrix screenToImage = new Matrix();
/** Determines whether the given screen point lies outside the desktop image. */
private boolean screenPointLiesOutsideImageBoundary(float screenX, float screenY) {
float[] mappedPoints = mapScreenPointToImagePoint(screenX, screenY);
float imageWidth;
float imageHeight;
synchronized (mRenderData) {
mRenderData.transform.invert(screenToImage);
imageWidth = mRenderData.imageWidth;
imageHeight = mRenderData.imageHeight;
imageWidth = (float) mRenderData.imageWidth + EPSILON;
imageHeight = (float) mRenderData.imageHeight + EPSILON;
}
screenToImage.mapPoints(mappedPoints);

return (mappedPoints[0] >= 0 && mappedPoints[0] <= imageWidth)
&& (mappedPoints[1] >= 0 && mappedPoints[1] <= imageHeight);
return mappedPoints[0] < -EPSILON || mappedPoints[0] > imageWidth
|| mappedPoints[1] < -EPSILON || mappedPoints[1] > imageHeight;
}
}
}

0 comments on commit 1bbf9d2

Please sign in to comment.