Bug 809199 - Eliminate use of ViewportMetrics from PanZoomController. r=Cwiiis

This commit is contained in:
Kartikaya Gupta 2012-11-07 11:47:08 -05:00
parent 46d17b95d2
commit 7d92cc7bef
4 changed files with 70 additions and 68 deletions

View File

@ -268,9 +268,7 @@ public class GeckoLayerClient
private void adjustViewport(DisplayPortMetrics displayPort) {
ImmutableViewportMetrics metrics = getViewportMetrics();
ViewportMetrics clampedMetrics = new ViewportMetrics(metrics);
clampedMetrics.setViewport(clampedMetrics.getClampedViewport());
ViewportMetrics clampedMetrics = new ViewportMetrics(metrics.clamp());
if (displayPort == null) {
displayPort = DisplayPortCalculator.calculate(metrics, mPanZoomController.getVelocityVector());

View File

@ -143,14 +143,61 @@ public class ImmutableViewportMetrics {
FloatUtils.interpolate(zoomFactor, to.zoomFactor, t));
}
public ImmutableViewportMetrics offsetViewportBy(PointF delta) {
public ImmutableViewportMetrics setViewportOrigin(float newOriginX, float newOriginY) {
return new ImmutableViewportMetrics(
pageRectLeft, pageRectTop, pageRectRight, pageRectBottom,
cssPageRectLeft, cssPageRectTop, cssPageRectRight, cssPageRectBottom,
viewportRectLeft + delta.x,
viewportRectTop + delta.y,
viewportRectRight + delta.x,
viewportRectBottom + delta.y,
newOriginX, newOriginY, newOriginX + getWidth(), newOriginY + getHeight(),
zoomFactor);
}
public ImmutableViewportMetrics offsetViewportBy(PointF delta) {
return setViewportOrigin(viewportRectLeft + delta.x, viewportRectTop + delta.y);
}
/* This will set the zoom factor and re-scale page-size and viewport offset
* accordingly. The given focus will remain at the same point on the screen
* after scaling.
*/
public ImmutableViewportMetrics scaleTo(float newZoomFactor, PointF focus) {
// cssPageRect* is invariant, since we're setting the scale factor
// here. The page rect is based on the CSS page rect.
float newPageRectLeft = cssPageRectLeft * newZoomFactor;
float newPageRectTop = cssPageRectTop * newZoomFactor;
float newPageRectRight = cssPageRectLeft + ((cssPageRectRight - cssPageRectLeft) * newZoomFactor);
float newPageRectBottom = cssPageRectTop + ((cssPageRectBottom - cssPageRectTop) * newZoomFactor);
PointF origin = getOrigin();
origin.offset(focus.x, focus.y);
origin = PointUtils.scale(origin, newZoomFactor / zoomFactor);
origin.offset(-focus.x, -focus.y);
return new ImmutableViewportMetrics(
newPageRectLeft, newPageRectTop, newPageRectRight, newPageRectBottom,
cssPageRectLeft, cssPageRectTop, cssPageRectRight, cssPageRectBottom,
origin.x, origin.y, origin.x + getWidth(), origin.y + getHeight(),
newZoomFactor);
}
/** Clamps the viewport to remain within the page rect. */
public ImmutableViewportMetrics clamp() {
RectF newViewport = getViewport();
// The viewport bounds ought to never exceed the page bounds.
if (newViewport.right > pageRectRight)
newViewport.offset(pageRectRight - newViewport.right, 0);
if (newViewport.left < pageRectLeft)
newViewport.offset(pageRectLeft - newViewport.left, 0);
if (newViewport.bottom > pageRectBottom)
newViewport.offset(0, pageRectBottom - newViewport.bottom);
if (newViewport.top < pageRectTop)
newViewport.offset(0, pageRectTop - newViewport.top);
return new ImmutableViewportMetrics(
pageRectLeft, pageRectTop, pageRectRight, pageRectBottom,
cssPageRectLeft, cssPageRectTop, cssPageRectRight, cssPageRectBottom,
newViewport.left, newViewport.top, newViewport.right, newViewport.bottom,
zoomFactor);
}

View File

@ -103,24 +103,6 @@ public class ViewportMetrics {
return RectUtils.scale(mViewportRect, 1/mZoomFactor);
}
/** Returns the viewport rectangle, clamped within the page-size. */
public RectF getClampedViewport() {
RectF clampedViewport = new RectF(mViewportRect);
// The viewport bounds ought to never exceed the page bounds.
if (clampedViewport.right > mPageRect.right)
clampedViewport.offset(mPageRect.right - clampedViewport.right, 0);
if (clampedViewport.left < mPageRect.left)
clampedViewport.offset(mPageRect.left - clampedViewport.left, 0);
if (clampedViewport.bottom > mPageRect.bottom)
clampedViewport.offset(0, mPageRect.bottom - clampedViewport.bottom);
if (clampedViewport.top < mPageRect.top)
clampedViewport.offset(0, mPageRect.top - clampedViewport.top);
return clampedViewport;
}
public RectF getPageRect() {
return mPageRect;
}
@ -157,27 +139,6 @@ public class ViewportMetrics {
mZoomFactor = zoomFactor;
}
/* This will set the zoom factor and re-scale page-size and viewport offset
* accordingly. The given focus will remain at the same point on the screen
* after scaling.
*/
public void scaleTo(float newZoomFactor, PointF focus) {
// mCssPageRect is invariant, since we're setting the scale factor
// here. The page rect is based on the CSS page rect.
mPageRect = RectUtils.scale(mCssPageRect, newZoomFactor);
float scaleFactor = newZoomFactor / mZoomFactor;
PointF origin = getOrigin();
origin.offset(focus.x, focus.y);
origin = PointUtils.scale(origin, scaleFactor);
origin.offset(-focus.x, -focus.y);
setOrigin(origin);
mZoomFactor = newZoomFactor;
}
public String toJSON() {
// Round off height and width. Since the height and width are the size of the screen, it
// makes no sense to send non-integer coordinates to Gecko.

View File

@ -12,7 +12,6 @@ import org.mozilla.gecko.PrefsHelper;
import org.mozilla.gecko.ZoomConstraints;
import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
import org.mozilla.gecko.gfx.PointUtils;
import org.mozilla.gecko.gfx.ViewportMetrics;
import org.mozilla.gecko.util.EventDispatcher;
import org.mozilla.gecko.util.FloatUtils;
import org.mozilla.gecko.util.GeckoEventListener;
@ -145,10 +144,6 @@ public class PanZoomController
return mTarget.getViewportMetrics();
}
private ViewportMetrics getMutableMetrics() {
return new ViewportMetrics(getMetrics());
}
// for debugging bug 713011; it can be taken out once that is resolved.
private void checkMainThread() {
if (mMainThread != Thread.currentThread()) {
@ -728,12 +723,12 @@ public class PanZoomController
/* Returns the nearest viewport metrics with no overscroll visible. */
private ImmutableViewportMetrics getValidViewportMetrics() {
return getValidViewportMetrics(getMutableMetrics());
return getValidViewportMetrics(getMetrics());
}
private ImmutableViewportMetrics getValidViewportMetrics(ViewportMetrics viewportMetrics) {
private ImmutableViewportMetrics getValidViewportMetrics(ImmutableViewportMetrics viewportMetrics) {
/* First, we adjust the zoom factor so that we can make no overscrolled area visible. */
float zoomFactor = viewportMetrics.getZoomFactor();
float zoomFactor = viewportMetrics.zoomFactor;
RectF pageRect = viewportMetrics.getPageRect();
RectF viewport = viewportMetrics.getViewport();
@ -778,16 +773,16 @@ public class PanZoomController
// by different scale factors, we end up scrolled to the end on one axis
// after applying the scale
PointF center = new PointF(focusX, focusY);
viewportMetrics.scaleTo(minZoomFactor, center);
viewportMetrics = viewportMetrics.scaleTo(minZoomFactor, center);
} else if (zoomFactor > maxZoomFactor) {
PointF center = new PointF(viewport.width() / 2.0f, viewport.height() / 2.0f);
viewportMetrics.scaleTo(maxZoomFactor, center);
viewportMetrics = viewportMetrics.scaleTo(maxZoomFactor, center);
}
/* Now we pan to the right origin. */
viewportMetrics.setViewport(viewportMetrics.getClampedViewport());
viewportMetrics = viewportMetrics.clamp();
return new ImmutableViewportMetrics(viewportMetrics);
return viewportMetrics;
}
private class AxisX extends Axis {
@ -931,9 +926,9 @@ public class PanZoomController
* scale operation. You must hold the monitor while calling this.
*/
private void scaleWithFocus(float zoomFactor, PointF focus) {
ViewportMetrics viewportMetrics = getMutableMetrics();
viewportMetrics.scaleTo(zoomFactor, focus);
mTarget.setViewportMetrics(new ImmutableViewportMetrics(viewportMetrics));
ImmutableViewportMetrics viewportMetrics = getMetrics();
viewportMetrics = viewportMetrics.scaleTo(zoomFactor, focus);
mTarget.setViewportMetrics(viewportMetrics);
}
public boolean getRedrawHint() {
@ -1038,16 +1033,17 @@ public class PanZoomController
float finalZoom = viewport.width() / zoomToRect.width();
ViewportMetrics finalMetrics = getMutableMetrics();
finalMetrics.setOrigin(new PointF(zoomToRect.left * finalMetrics.getZoomFactor(),
zoomToRect.top * finalMetrics.getZoomFactor()));
finalMetrics.scaleTo(finalZoom, new PointF(0.0f, 0.0f));
ImmutableViewportMetrics finalMetrics = getMetrics();
finalMetrics = finalMetrics.setViewportOrigin(
zoomToRect.left * finalMetrics.zoomFactor,
zoomToRect.top * finalMetrics.zoomFactor);
finalMetrics = finalMetrics.scaleTo(finalZoom, new PointF(0.0f, 0.0f));
// 2. now run getValidViewportMetrics on it, so that the target viewport is
// clamped down to prevent overscroll, over-zoom, and other bad conditions.
ImmutableViewportMetrics finalValidMetrics = getValidViewportMetrics(finalMetrics);
finalMetrics = getValidViewportMetrics(finalMetrics);
bounce(finalValidMetrics);
bounce(finalMetrics);
return true;
}