Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better popup resizing with pinch gestures #3075

Merged
merged 4 commits into from
Apr 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 54 additions & 22 deletions app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnticipateInterpolator;
Expand Down Expand Up @@ -1010,6 +1011,14 @@ private class PopupWindowGestureListener extends GestureDetector.SimpleOnGesture
private boolean isMoving;
private boolean isResizing;

//initial co-ordinates and distance between fingers
private double initPointerDistance = -1;
private float initFirstPointerX = -1;
private float initFirstPointerY = -1;
private float initSecPointerX = -1;
private float initSecPointerY = -1;


@Override
public boolean onDoubleTap(final MotionEvent e) {
if (DEBUG) {
Expand Down Expand Up @@ -1201,6 +1210,16 @@ public boolean onTouch(final View v, final MotionEvent event) {
playerImpl.hideControls(0, 0);
animateView(playerImpl.getCurrentDisplaySeek(), false, 0, 0);
animateView(playerImpl.getResizingIndicator(), true, 200, 0);

//record co-ordinates of fingers
initFirstPointerX = event.getX(0);
initFirstPointerY = event.getY(0);
initSecPointerX = event.getX(1);
initSecPointerY = event.getY(1);
//record distance between fingers
initPointerDistance = Math.hypot(initFirstPointerX - initSecPointerX,
initFirstPointerY - initSecPointerY);

isResizing = true;
}

Expand All @@ -1224,6 +1243,13 @@ public boolean onTouch(final View v, final MotionEvent event) {

if (isResizing) {
isResizing = false;

initPointerDistance = -1;
initFirstPointerX = -1;
initFirstPointerY = -1;
initSecPointerX = -1;
initSecPointerY = -1;

animateView(playerImpl.getResizingIndicator(), false, 100, 0);
playerImpl.changeState(playerImpl.getCurrentState());
}
Expand All @@ -1238,29 +1264,35 @@ public boolean onTouch(final View v, final MotionEvent event) {
}

private boolean handleMultiDrag(final MotionEvent event) {
if (event.getPointerCount() != 2) {
return false;
}

final float firstPointerX = event.getX(0);
final float secondPointerX = event.getX(1);

final float diff = Math.abs(firstPointerX - secondPointerX);
if (firstPointerX > secondPointerX) {
// second pointer is the anchor (the leftmost pointer)
popupLayoutParams.x = (int) (event.getRawX() - diff);
} else {
// first pointer is the anchor
popupLayoutParams.x = (int) event.getRawX();
if (initPointerDistance != -1 && event.getPointerCount() == 2) {
// get the movements of the fingers
double firstPointerMove = Math.hypot(event.getX(0) - initFirstPointerX,
event.getY(0) - initFirstPointerY);
double secPointerMove = Math.hypot(event.getX(1) - initSecPointerX,
event.getY(1) - initSecPointerY);

// minimum threshold beyond which pinch gesture will work
int minimumMove = ViewConfiguration.get(PopupVideoPlayer.this).getScaledTouchSlop();

if (Math.max(firstPointerMove, secPointerMove) > minimumMove) {
// calculate current distance between the pointers
double currentPointerDistance =
Math.hypot(event.getX(0) - event.getX(1),
event.getY(0) - event.getY(1));

// change co-ordinates of popup so the center stays at the same position
double newWidth = (popupWidth * currentPointerDistance / initPointerDistance);
initPointerDistance = currentPointerDistance;
popupLayoutParams.x += (popupWidth - newWidth) / 2;

checkPopupPositionBounds();
updateScreenSize();

updatePopupSize((int) Math.min(screenWidth, newWidth), -1);
return true;
}
}

checkPopupPositionBounds();
updateScreenSize();

final int width = (int) Math.min(screenWidth, diff);
updatePopupSize(width, -1);

return true;
return false;
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down