Bug 383026. Centralize code that converts DOM scroll offsets to/from layout scroll positions. r=dbaron

--HG--
extra : rebase_source : a6118e0eeea749a601e055741e35cdcd15d81e57
This commit is contained in:
Robert O'Callahan 2012-08-10 23:17:06 +12:00
parent 16858576bf
commit d53617a13e
6 changed files with 37 additions and 24 deletions

View File

@ -607,10 +607,7 @@ PRInt32
nsGenericElement::GetScrollTop()
{
nsIScrollableFrame* sf = GetScrollFrame();
return sf ?
nsPresContext::AppUnitsToIntCSSPixels(sf->GetScrollPosition().y) :
0;
return sf ? sf->GetScrollPositionCSSPixels().y : 0;
}
NS_IMETHODIMP
@ -626,9 +623,7 @@ nsGenericElement::SetScrollTop(PRInt32 aScrollTop)
{
nsIScrollableFrame* sf = GetScrollFrame();
if (sf) {
nsPoint pt = sf->GetScrollPosition();
sf->ScrollToCSSPixels(nsIntPoint(nsPresContext::AppUnitsToIntCSSPixels(pt.x),
aScrollTop));
sf->ScrollToCSSPixels(nsIntPoint(sf->GetScrollPositionCSSPixels().x, aScrollTop));
}
return NS_OK;
}
@ -637,10 +632,7 @@ PRInt32
nsGenericElement::GetScrollLeft()
{
nsIScrollableFrame* sf = GetScrollFrame();
return sf ?
nsPresContext::AppUnitsToIntCSSPixels(sf->GetScrollPosition().x) :
0;
return sf ? sf->GetScrollPositionCSSPixels().x : 0;
}
NS_IMETHODIMP
@ -656,9 +648,7 @@ nsGenericElement::SetScrollLeft(PRInt32 aScrollLeft)
{
nsIScrollableFrame* sf = GetScrollFrame();
if (sf) {
nsPoint pt = sf->GetScrollPosition();
sf->ScrollToCSSPixels(nsIntPoint(aScrollLeft,
nsPresContext::AppUnitsToIntCSSPixels(pt.y)));
sf->ScrollToCSSPixels(nsIntPoint(aScrollLeft, sf->GetScrollPositionCSSPixels().y));
}
return NS_OK;
}

View File

@ -4322,11 +4322,13 @@ nsGlobalWindow::GetScrollXY(PRInt32* aScrollX, PRInt32* aScrollY,
return GetScrollXY(aScrollX, aScrollY, true);
}
if (aScrollX)
*aScrollX = nsPresContext::AppUnitsToIntCSSPixels(scrollPos.x);
if (aScrollY)
*aScrollY = nsPresContext::AppUnitsToIntCSSPixels(scrollPos.y);
nsIntPoint scrollPosCSSPixels = sf->GetScrollPositionCSSPixels();
if (aScrollX) {
*aScrollX = scrollPosCSSPixels.x;
}
if (aScrollY) {
*aScrollY = scrollPosCSSPixels.y;
}
return NS_OK;
}

View File

@ -1683,18 +1683,19 @@ void
nsGfxScrollFrameInner::ScrollToCSSPixels(nsIntPoint aScrollPosition)
{
nsPoint current = GetScrollPosition();
nsIntPoint currentCSSPixels = GetScrollPositionCSSPixels();
nsPoint pt(nsPresContext::CSSPixelsToAppUnits(aScrollPosition.x),
nsPresContext::CSSPixelsToAppUnits(aScrollPosition.y));
nscoord halfPixel = nsPresContext::CSSPixelsToAppUnits(0.5f);
nsRect range(pt.x - halfPixel, pt.y - halfPixel, 2*halfPixel - 1, 2*halfPixel - 1);
if (nsPresContext::AppUnitsToIntCSSPixels(current.x) == aScrollPosition.x) {
if (currentCSSPixels.x == aScrollPosition.x) {
pt.x = current.x;
range.x = pt.x;
range.width = 0;
} else {
// current.x must be outside 'range', so we must move in the correct direction.
}
if (nsPresContext::AppUnitsToIntCSSPixels(current.y) == aScrollPosition.y) {
if (currentCSSPixels.y == aScrollPosition.y) {
pt.y = current.y;
range.y = pt.y;
range.height = 0;
@ -1704,6 +1705,14 @@ nsGfxScrollFrameInner::ScrollToCSSPixels(nsIntPoint aScrollPosition)
ScrollTo(pt, nsIScrollableFrame::INSTANT, &range);
}
nsIntPoint
nsGfxScrollFrameInner::GetScrollPositionCSSPixels()
{
nsPoint pt = GetScrollPosition();
return nsIntPoint(nsPresContext::AppUnitsToIntCSSPixels(pt.x),
nsPresContext::AppUnitsToIntCSSPixels(pt.y));
}
/*
* this method wraps calls to ScrollToImpl(), either in one shot or incrementally,
* based on the setting of the smoothness scroll pref

View File

@ -166,6 +166,7 @@ public:
ScrollToWithOrigin(aScrollPosition, aMode, nsGkAtoms::other, aRange);
}
void ScrollToCSSPixels(nsIntPoint aScrollPosition);
nsIntPoint GetScrollPositionCSSPixels();
void ScrollToImpl(nsPoint aScrollPosition, const nsRect& aRange);
void ScrollVisual(nsPoint aOldScrolledFramePosition);
void ScrollBy(nsIntPoint aDelta, nsIScrollableFrame::ScrollUnit aUnit,
@ -480,6 +481,9 @@ public:
virtual void ScrollToCSSPixels(nsIntPoint aScrollPosition) {
mInner.ScrollToCSSPixels(aScrollPosition);
}
virtual nsIntPoint GetScrollPositionCSSPixels() {
return mInner.GetScrollPositionCSSPixels();
}
virtual void ScrollBy(nsIntPoint aDelta, ScrollUnit aUnit, ScrollMode aMode,
nsIntPoint* aOverflow, nsIAtom *aOrigin = nullptr) {
mInner.ScrollBy(aDelta, aUnit, aMode, aOverflow, aOrigin);
@ -725,6 +729,9 @@ public:
virtual void ScrollToCSSPixels(nsIntPoint aScrollPosition) {
mInner.ScrollToCSSPixels(aScrollPosition);
}
virtual nsIntPoint GetScrollPositionCSSPixels() {
return mInner.GetScrollPositionCSSPixels();
}
virtual void ScrollBy(nsIntPoint aDelta, ScrollUnit aUnit, ScrollMode aMode,
nsIntPoint* aOverflow, nsIAtom *aOrigin = nullptr) {
mInner.ScrollBy(aDelta, aUnit, aMode, aOverflow, aOrigin);

View File

@ -138,6 +138,11 @@ public:
* The scroll mode is INSTANT.
*/
virtual void ScrollToCSSPixels(nsIntPoint aScrollPosition) = 0;
/**
* Returns the scroll position in integer CSS pixels, rounded to the nearest
* pixel.
*/
virtual nsIntPoint GetScrollPositionCSSPixels() = 0;
/**
* When scrolling by a relative amount, we can choose various units.
*/

View File

@ -263,9 +263,9 @@ NS_IMETHODIMP nsScrollBoxObject::GetPosition(PRInt32 *x, PRInt32 *y)
if (!sf)
return NS_ERROR_FAILURE;
nsPoint pt = sf->GetScrollPosition();
*x = nsPresContext::AppUnitsToIntCSSPixels(pt.x);
*y = nsPresContext::AppUnitsToIntCSSPixels(pt.y);
nsIntPoint pt = sf->GetScrollPositionCSSPixels();
*x = pt.x;
*y = pt.y;
return NS_OK;
}