Skip to content

Commit

Permalink
Eliminate selection offset caused by handle view orientation change.
Browse files Browse the repository at this point in the history
  • Loading branch information
maoabc committed Aug 13, 2022
1 parent 2113407 commit dd62a76
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class TextSelectionHandleView extends View {

private final int mInitialOrientation;
private int mOrientation;
private float mAdjHotspotOffsetX = 0;

public static final int LEFT = 0;
public static final int RIGHT = 2;
Expand Down Expand Up @@ -149,7 +150,7 @@ public void hide() {

public void removeFromParent() {
if (!isParentNull()) {
((ViewGroup)this.getParent()).removeView(this);
((ViewGroup) this.getParent()).removeView(this);
}
}

Expand All @@ -160,10 +161,9 @@ public void positionAtCursor(final int cx, final int cy, boolean forceOrientatio
}

private void moveTo(int x, int y, boolean forceOrientationCheck) {
float oldHotspotX = mHotspotX;
checkChangedOrientation(x, forceOrientationCheck);
mPointX = (int) (x - (isShowing() ? oldHotspotX : mHotspotX));
mPointX = (int) (x - mHotspotX);
mPointY = y;
checkChangedOrientation(forceOrientationCheck);

if (isPositionVisible()) {
int[] coords = null;
Expand Down Expand Up @@ -198,11 +198,14 @@ private void moveTo(int x, int y, boolean forceOrientationCheck) {

public void changeOrientation(int orientation) {
if (mOrientation != orientation) {
final float hotspotX = mHotspotX;
setOrientation(orientation);
mAdjHotspotOffsetX = (mHotspotX - hotspotX);
mTouchToWindowOffsetX += mAdjHotspotOffsetX;
}
}

private void checkChangedOrientation(int posX, boolean force) {
private void checkChangedOrientation(boolean force) {
if (!mIsDragging && !force) {
return;
}
Expand Down Expand Up @@ -231,6 +234,7 @@ private void checkChangedOrientation(int posX, boolean force) {
if (parent == null || !parent.getChildVisibleRect(hostView, clip, null)) {
return;
}
final int posX = (int) (mPointX + mAdjHotspotOffsetX);

if (posX - mHandleWidth < clip.left) {
changeOrientation(RIGHT);
Expand Down Expand Up @@ -315,6 +319,7 @@ public boolean onTouchEvent(MotionEvent event) {

case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mAdjHotspotOffsetX = 0;
mIsDragging = false;
}
return true;
Expand Down

0 comments on commit dd62a76

Please sign in to comment.