also expose onDrawHorizontalScrollBar() in View

retool WebView's calculations to correctly compute scrollbar height and parameters
This commit is contained in:
Mike Reed
2009-09-04 14:01:48 -04:00
parent e5b6d02f34
commit e8853fc570
2 changed files with 102 additions and 102 deletions

View File

@@ -5343,12 +5343,36 @@ public class View implements Drawable.Callback, KeyEvent.Callback, Accessibility
size = cache.scrollBarSize; size = cache.scrollBarSize;
} }
final int scrollX = mScrollX;
final int scrollY = mScrollY;
final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
if (drawHorizontalScrollBar) { if (drawHorizontalScrollBar) {
onDrawHorizontalScrollBar(canvas, scrollBar, width, height, size); scrollBar.setParameters(
computeHorizontalScrollRange(),
computeHorizontalScrollOffset(),
computeHorizontalScrollExtent(), false);
final int top = scrollY + height - size - (mUserPaddingBottom & inside);
final int verticalScrollBarGap = drawVerticalScrollBar ?
getVerticalScrollbarWidth() : 0;
onDrawHorizontalScrollBar(canvas, scrollBar,
scrollX + (mPaddingLeft & inside),
top,
scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap,
top + size);
} }
if (drawVerticalScrollBar) { if (drawVerticalScrollBar) {
onDrawVerticalScrollBar(canvas, scrollBar, width, height, size); scrollBar.setParameters(computeVerticalScrollRange(),
computeVerticalScrollOffset(),
computeVerticalScrollExtent(), true);
// TODO: Deal with RTL languages to position scrollbar on left
final int left = scrollX + width - size - (mUserPaddingRight & inside);
onDrawVerticalScrollBar(canvas, scrollBar,
left,
scrollY + (mPaddingTop & inside),
left + size,
scrollY + height - (mUserPaddingBottom & inside));
} }
} }
} }
@@ -5368,56 +5392,20 @@ public class View implements Drawable.Callback, KeyEvent.Callback, Accessibility
* <p>Draw the horizontal scrollbar if * <p>Draw the horizontal scrollbar if
* {@link #isHorizontalScrollBarEnabled()} returns true.</p> * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
* *
* <p>The length of the scrollbar and its thumb is computed according to the
* values returned by {@link #computeHorizontalScrollRange()},
* {@link #computeHorizontalScrollExtent()} and
* {@link #computeHorizontalScrollOffset()}. Refer to
* {@link android.widget.ScrollBarDrawable} for more information about how
* these values relate to each other.</p>
*
* @param canvas the canvas on which to draw the scrollbar * @param canvas the canvas on which to draw the scrollbar
* @param scrollBar the scrollbar's drawable * @param scrollBar the scrollbar's drawable
* @param width the width of the drawing surface
* @param height the height of the drawing surface
* @param size the size of the scrollbar
* *
* @see #isHorizontalScrollBarEnabled() * @see #isHorizontalScrollBarEnabled()
* @see #computeHorizontalScrollRange() * @see #computeHorizontalScrollRange()
* @see #computeHorizontalScrollExtent() * @see #computeHorizontalScrollExtent()
* @see #computeHorizontalScrollOffset() * @see #computeHorizontalScrollOffset()
* @see android.widget.ScrollBarDrawable * @see android.widget.ScrollBarDrawable
*/
private void onDrawHorizontalScrollBar(Canvas canvas, ScrollBarDrawable scrollBar, int width,
int height, int size) {
final int viewFlags = mViewFlags;
final int scrollX = mScrollX;
final int scrollY = mScrollY;
final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
final int top = scrollY + height - size - (mUserPaddingBottom & inside);
final int verticalScrollBarGap =
(viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL ?
getVerticalScrollbarWidth() : 0;
scrollBar.setBounds(scrollX + (mPaddingLeft & inside), top,
scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap, top + size);
scrollBar.setParameters(
computeHorizontalScrollRange(),
computeHorizontalScrollOffset(),
computeHorizontalScrollExtent(), false);
scrollBar.draw(canvas);
}
/**
* @hide * @hide
*/ */
protected void onDrawVScrollBar(Canvas canvas, ScrollBarDrawable scrollBar, protected void onDrawHorizontalScrollBar(Canvas canvas,
int l, int t, int r, int b) { Drawable scrollBar,
int l, int t, int r, int b) {
scrollBar.setBounds(l, t, r, b); scrollBar.setBounds(l, t, r, b);
scrollBar.setParameters(computeVerticalScrollRange(),
computeVerticalScrollOffset(),
computeVerticalScrollExtent(), true);
scrollBar.draw(canvas); scrollBar.draw(canvas);
} }
@@ -5425,39 +5413,21 @@ public class View implements Drawable.Callback, KeyEvent.Callback, Accessibility
* <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()} * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
* returns true.</p> * returns true.</p>
* *
* <p>The length of the scrollbar and its thumb is computed according to the
* values returned by {@link #computeVerticalScrollRange()},
* {@link #computeVerticalScrollExtent()} and
* {@link #computeVerticalScrollOffset()}. Refer to
* {@link android.widget.ScrollBarDrawable} for more information about how
* these values relate to each other.</p>
*
* @param canvas the canvas on which to draw the scrollbar * @param canvas the canvas on which to draw the scrollbar
* @param scrollBar the scrollbar's drawable * @param scrollBar the scrollbar's drawable
* @param width the width of the drawing surface
* @param height the height of the drawing surface
* @param size the size of the scrollbar
* *
* @see #isVerticalScrollBarEnabled() * @see #isVerticalScrollBarEnabled()
* @see #computeVerticalScrollRange() * @see #computeVerticalScrollRange()
* @see #computeVerticalScrollExtent() * @see #computeVerticalScrollExtent()
* @see #computeVerticalScrollOffset() * @see #computeVerticalScrollOffset()
* @see android.widget.ScrollBarDrawable * @see android.widget.ScrollBarDrawable
* @hide
*/ */
private void onDrawVerticalScrollBar(Canvas canvas, ScrollBarDrawable scrollBar, int width, protected void onDrawVerticalScrollBar(Canvas canvas,
int height, int size) { Drawable scrollBar,
int l, int t, int r, int b) {
final int scrollX = mScrollX; scrollBar.setBounds(l, t, r, b);
final int scrollY = mScrollY; scrollBar.draw(canvas);
final int inside = (mViewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
// TODO: Deal with RTL languages to position scrollbar on left
final int left = scrollX + width - size - (mUserPaddingRight & inside);
onDrawVScrollBar(canvas, scrollBar,
left,
scrollY + (mPaddingTop & inside),
left + size,
scrollY + height - (mUserPaddingBottom & inside));
} }
/** /**

View File

@@ -31,6 +31,7 @@ import android.graphics.Picture;
import android.graphics.Point; import android.graphics.Point;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.Region; import android.graphics.Region;
import android.graphics.drawable.Drawable;
import android.net.http.SslCertificate; import android.net.http.SslCertificate;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@@ -67,6 +68,7 @@ import android.widget.ArrayAdapter;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.ScrollBarDrawable;
import android.widget.Scroller; import android.widget.Scroller;
import android.widget.Toast; import android.widget.Toast;
import android.widget.ZoomButtonsController; import android.widget.ZoomButtonsController;
@@ -936,22 +938,31 @@ public class WebView extends AbsoluteLayout
} }
} }
/*
* returns the height of the titlebarview (if any). Does not care about
* scrolling
*/
private int getTitleHeight() {
return mTitleBar != null ? mTitleBar.getHeight() : 0;
}
/*
* Return the amount of the titlebarview (if any) that is visible
*/
private int getVisibleTitleHeight() {
return Math.max(getTitleHeight() - mScrollY, 0);
}
/* /*
* Return the height of the view where the content of WebView should render * Return the height of the view where the content of WebView should render
* to. Note that this excludes mTitleBar, if there is one. * to. Note that this excludes mTitleBar, if there is one.
*/ */
private int getViewHeight() { private int getViewHeight() {
int height = getHeight(); int height = getHeight();
if (isHorizontalScrollBarEnabled() && mOverlayHorizontalScrollbar) { if (isHorizontalScrollBarEnabled() && !mOverlayHorizontalScrollbar) {
height -= getHorizontalScrollbarHeight(); height -= getHorizontalScrollbarHeight();
} }
if (mTitleBar != null) { return height - getVisibleTitleHeight();
int titleBarVisibleHeight = mTitleBar.getHeight() - mScrollY;
if (titleBarVisibleHeight > 0) {
height -= titleBarVisibleHeight;
}
}
return height;
} }
/** /**
@@ -1741,7 +1752,15 @@ public class WebView extends AbsoluteLayout
// Expects y in view coordinates // Expects y in view coordinates
private int pinLocY(int y) { private int pinLocY(int y) {
return pinLoc(y, getViewHeight(), computeVerticalScrollRange()); int titleH = getTitleHeight();
// if the titlebar is still visible, just pin against 0
if (y <= titleH) {
return Math.max(y, 0);
}
// convert to 0-based coordinate (subtract the title height)
// pin(), and then add the title height back in
return pinLoc(y - titleH, getViewHeight(),
computeVerticalScrollRange()) + titleH;
} }
/** /**
@@ -1783,10 +1802,17 @@ public class WebView extends AbsoluteLayout
* embedded into the WebView. * embedded into the WebView.
*/ */
/*package*/ int viewToContentY(int y) { /*package*/ int viewToContentY(int y) {
if (mTitleBar != null) { return viewToContentX(y - getTitleHeight());
y -= mTitleBar.getHeight(); }
}
return viewToContentX(y); /**
* Given a distance in content space, convert it to view space. Note: this
* does not reflect translation, just scaling, so this should not be called
* with coordinates, but should be called for dimensions like width or
* height.
*/
/*package*/ int contentToViewDimension(int d) {
return Math.round(d * mActualScale);
} }
/** /**
@@ -1794,7 +1820,7 @@ public class WebView extends AbsoluteLayout
* space. Also used for absolute heights. * space. Also used for absolute heights.
*/ */
/*package*/ int contentToViewX(int x) { /*package*/ int contentToViewX(int x) {
return Math.round(x * mActualScale); return contentToViewDimension(x);
} }
/** /**
@@ -1802,11 +1828,7 @@ public class WebView extends AbsoluteLayout
* space. Takes into account the height of the title bar. * space. Takes into account the height of the title bar.
*/ */
/*package*/ int contentToViewY(int y) { /*package*/ int contentToViewY(int y) {
int val = Math.round(y * mActualScale); return contentToViewDimension(y) + getTitleHeight();
if (mTitleBar != null) {
val += mTitleBar.getHeight();
}
return val;
} }
// Called by JNI to invalidate the View, given rectangle coordinates in // Called by JNI to invalidate the View, given rectangle coordinates in
@@ -2024,7 +2046,7 @@ public class WebView extends AbsoluteLayout
if (mDrawHistory) { if (mDrawHistory) {
return mHistoryWidth; return mHistoryWidth;
} else { } else {
return contentToViewX(mContentWidth); return contentToViewDimension(mContentWidth);
} }
} }
@@ -2036,7 +2058,7 @@ public class WebView extends AbsoluteLayout
if (mDrawHistory) { if (mDrawHistory) {
return mHistoryHeight; return mHistoryHeight;
} else { } else {
int height = contentToViewX(mContentHeight); int height = contentToViewDimension(mContentHeight);
if (mFindIsUp) { if (mFindIsUp) {
height += FIND_HEIGHT; height += FIND_HEIGHT;
} }
@@ -2046,14 +2068,7 @@ public class WebView extends AbsoluteLayout
@Override @Override
protected int computeVerticalScrollOffset() { protected int computeVerticalScrollOffset() {
int offset = super.computeVerticalScrollOffset(); return Math.max(mScrollY - getTitleHeight(), 0);
if (mTitleBar != null) {
// Need to adjust so that the resulting offset is at minimum
// the height of the title bar, if it is visible.
offset += mTitleBar.getHeight()*computeVerticalScrollRange()
/getViewHeight();
}
return offset;
} }
@Override @Override
@@ -2061,6 +2076,15 @@ public class WebView extends AbsoluteLayout
return getViewHeight(); return getViewHeight();
} }
/** @hide */
@Override
protected void onDrawVerticalScrollBar(Canvas canvas,
Drawable scrollBar,
int l, int t, int r, int b) {
scrollBar.setBounds(l, t + getVisibleTitleHeight(), r, b);
scrollBar.draw(canvas);
}
/** /**
* Get the url for the current page. This is not always the same as the url * Get the url for the current page. This is not always the same as the url
* passed to WebViewClient.onPageStarted because although the load for * passed to WebViewClient.onPageStarted because although the load for
@@ -2412,8 +2436,8 @@ public class WebView extends AbsoluteLayout
// keys are hit, this should be safe. Right? // keys are hit, this should be safe. Right?
return false; return false;
} }
cx = contentToViewX(cx); cx = contentToViewDimension(cx);
cy = contentToViewY(cy); cy = contentToViewDimension(cy);
if (mHeightCanMeasure) { if (mHeightCanMeasure) {
// move our visible rect according to scroll request // move our visible rect according to scroll request
if (cy != 0) { if (cy != 0) {
@@ -2482,12 +2506,12 @@ public class WebView extends AbsoluteLayout
} }
if (mHeightCanMeasure) { if (mHeightCanMeasure) {
if (getMeasuredHeight() != contentToViewX(mContentHeight) if (getMeasuredHeight() != contentToViewDimension(mContentHeight)
&& updateLayout) { && updateLayout) {
requestLayout(); requestLayout();
} }
} else if (mWidthCanMeasure) { } else if (mWidthCanMeasure) {
if (getMeasuredWidth() != contentToViewX(mContentWidth) if (getMeasuredWidth() != contentToViewDimension(mContentWidth)
&& updateLayout) { && updateLayout) {
requestLayout(); requestLayout();
} }
@@ -3272,7 +3296,7 @@ public class WebView extends AbsoluteLayout
// Initialize our generation number. // Initialize our generation number.
mTextGeneration = 0; mTextGeneration = 0;
} }
mWebTextView.setTextSize(contentToViewX(nativeFocusCandidateTextSize())); mWebTextView.setTextSize(contentToViewDimension(nativeFocusCandidateTextSize()));
Rect visibleRect = new Rect(); Rect visibleRect = new Rect();
calcOurContentVisibleRect(visibleRect); calcOurContentVisibleRect(visibleRect);
// Note that sendOurVisibleRect calls viewToContent, so the coordinates // Note that sendOurVisibleRect calls viewToContent, so the coordinates
@@ -4506,9 +4530,15 @@ public class WebView extends AbsoluteLayout
} }
} }
private int computeMaxScrollY() {
int maxContentH = contentToViewDimension(mContentHeight)
+ getTitleHeight();
return Math.max(maxContentH - getHeight(), 0);
}
public void flingScroll(int vx, int vy) { public void flingScroll(int vx, int vy) {
int maxX = Math.max(computeHorizontalScrollRange() - getViewWidth(), 0); int maxX = Math.max(computeHorizontalScrollRange() - getViewWidth(), 0);
int maxY = Math.max(computeVerticalScrollRange() - getViewHeight(), 0); int maxY = computeMaxScrollY();
mScroller.fling(mScrollX, mScrollY, vx, vy, 0, maxX, 0, maxY); mScroller.fling(mScrollX, mScrollY, vx, vy, 0, maxX, 0, maxY);
invalidate(); invalidate();
@@ -4519,7 +4549,7 @@ public class WebView extends AbsoluteLayout
return; return;
} }
int maxX = Math.max(computeHorizontalScrollRange() - getViewWidth(), 0); int maxX = Math.max(computeHorizontalScrollRange() - getViewWidth(), 0);
int maxY = Math.max(computeVerticalScrollRange() - getViewHeight(), 0); int maxY = computeMaxScrollY();
mVelocityTracker.computeCurrentVelocity(1000, mMaximumFling); mVelocityTracker.computeCurrentVelocity(1000, mMaximumFling);
int vx = (int) mVelocityTracker.getXVelocity(); int vx = (int) mVelocityTracker.getXVelocity();