Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a420b53d authored by Alan Viverette's avatar Alan Viverette Committed by Android (Google) Code Review
Browse files

Merge "Clean up FastScroller. Expand overlay to fit text content."

parents 0149db46 e918a48d
Loading
Loading
Loading
Loading
+81 −64
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
@@ -43,6 +44,7 @@ class FastScroller {
    // Scroll thumb not showing
    private static final int STATE_NONE = 0;
    // Not implemented yet - fade-in transition
    @SuppressWarnings("unused")
    private static final int STATE_ENTER = 1;
    // Scroll thumb visible and moving along with the scrollbar
    private static final int STATE_VISIBLE = 2;
@@ -89,6 +91,7 @@ class FastScroller {

    private RectF mOverlayPos;
    private int mOverlaySize;
    private int mOverlayPadding;

    AbsListView mList;
    boolean mScrollCompleted;
@@ -130,6 +133,7 @@ class FastScroller {
    private final Rect mTmpRect = new Rect();

    private final Runnable mDeferStartDrag = new Runnable() {
        @Override
        public void run() {
            if (mList.mIsAttached) {
                beginDrag();
@@ -272,7 +276,7 @@ class FastScroller {

    private void init(Context context) {
        // Get both the scrollbar states drawables
        TypedArray ta = context.getTheme().obtainStyledAttributes(ATTRS);
        final TypedArray ta = context.getTheme().obtainStyledAttributes(ATTRS);
        useThumbDrawable(context, ta.getDrawable(THUMB_DRAWABLE));
        mTrackDrawable = ta.getDrawable(TRACK_DRAWABLE);

@@ -284,8 +288,11 @@ class FastScroller {

        getSectionsFromIndexer();

        mOverlaySize = context.getResources().getDimensionPixelSize(
        final Resources res = context.getResources();
        mOverlaySize = res.getDimensionPixelSize(
                com.android.internal.R.dimen.fastscroll_overlay_size);
        mOverlayPadding = res.getDimensionPixelSize(
                com.android.internal.R.dimen.fastscroll_overlay_padding);
        mOverlayPos = new RectF();
        mScrollFade = new ScrollFade();
        mPaint = new Paint();
@@ -371,44 +378,53 @@ class FastScroller {

        // If user is dragging the scroll bar, draw the alphabet overlay
        if (mState == STATE_DRAGGING && mDrawOverlay) {
            final Drawable overlay = mOverlayDrawable;
            final Paint paint = mPaint;
            final String sectionText = mSectionText;
            final Rect tmpRect = mTmpRect;

            // TODO: Use a text view in an overlay for transition animations and
            // handling of text overflow.
            paint.getTextBounds(sectionText, 0, sectionText.length(), tmpRect);
            final int textWidth = tmpRect.width();
            final int textHeight = tmpRect.height();

            overlay.getPadding(tmpRect);
            final int overlayWidth = Math.max(
                    mOverlaySize, textWidth + tmpRect.left + tmpRect.right + mOverlayPadding * 2);
            final int overlayHeight = Math.max(
                    mOverlaySize, textHeight + tmpRect.top + tmpRect.bottom + mOverlayPadding * 2);
            final RectF pos = mOverlayPos;

            if (mOverlayPosition == OVERLAY_AT_THUMB) {
                int left = 0;
                final Rect thumbBounds = mThumbDrawable.getBounds();

                switch (mPosition) {
                    default:
                    case View.SCROLLBAR_POSITION_RIGHT:
                        left = Math.max(0,
                                mThumbDrawable.getBounds().left - mThumbW - mOverlaySize);
                        break;
                    case View.SCROLLBAR_POSITION_LEFT:
                        left = Math.min(mThumbDrawable.getBounds().right + mThumbW,
                                mList.getWidth() - mOverlaySize);
                        pos.left = Math.min(
                                thumbBounds.right + mThumbW, mList.getWidth() - overlayWidth);
                        break;
                    case View.SCROLLBAR_POSITION_RIGHT:
                    default:
                        pos.left = Math.max(0, thumbBounds.left - mThumbW - overlayWidth);
                        break;
                }

                int top = Math.max(0,
                        Math.min(y + (mThumbH - mOverlaySize) / 2, mList.getHeight() - mOverlaySize));

                final RectF pos = mOverlayPos;
                pos.left = left;
                pos.right = pos.left + mOverlaySize;
                pos.top = top;
                pos.bottom = pos.top + mOverlaySize;
                if (mOverlayDrawable != null) {
                    mOverlayDrawable.setBounds((int) pos.left, (int) pos.top,
                            (int) pos.right, (int) pos.bottom);
                pos.top = Math.max(0, Math.min(
                        y + (mThumbH - overlayHeight) / 2, mList.getHeight() - overlayHeight));
            }
            }
            mOverlayDrawable.draw(canvas);
            final Paint paint = mPaint;
            float descent = paint.descent();
            final RectF rectF = mOverlayPos;
            final Rect tmpRect = mTmpRect;
            mOverlayDrawable.getPadding(tmpRect);
            final int hOff = (tmpRect.right - tmpRect.left) / 2;
            final int vOff = (tmpRect.bottom - tmpRect.top) / 2;
            canvas.drawText(mSectionText, (int) (rectF.left + rectF.right) / 2 - hOff,
                    (int) (rectF.bottom + rectF.top) / 2 + mOverlaySize / 4 - descent - vOff,
                    paint);

            pos.right = pos.left + overlayWidth;
            pos.bottom = pos.top + overlayHeight;

            overlay.setBounds((int) pos.left, (int) pos.top, (int) pos.right, (int) pos.bottom);
            overlay.draw(canvas);

            final float hOff = (tmpRect.right - tmpRect.left) / 2.0f;
            final float vOff = (tmpRect.bottom - tmpRect.top) / 2.0f;
            final float cX = pos.centerX() - hOff;
            final float cY = pos.centerY() + (overlayHeight / 4.0f) - paint.descent() - vOff;
            canvas.drawText(mSectionText, cX, cY, paint);
        } else if (mState == STATE_EXIT) {
            if (alpha == 0) { // Done with exit
                setState(STATE_NONE);
@@ -888,6 +904,7 @@ class FastScroller {
            return alpha;
        }

        @Override
        public void run() {
            if (getState() != STATE_EXIT) {
                startFade();
+3 −1
Original line number Diff line number Diff line
@@ -50,8 +50,10 @@
    <!-- Margin at the edge of the screen to ignore touch events for in the windowshade. -->
    <dimen name="status_bar_edge_ignore">5dp</dimen>

    <!-- Size of the fastscroll hint letter -->
    <!-- Minimum size of the fastscroll overlay -->
    <dimen name="fastscroll_overlay_size">104dp</dimen>
    <!-- Padding of the fastscroll overlay -->
    <dimen name="fastscroll_overlay_padding">16dp</dimen>
    <!-- Width of the fastscroll thumb -->
    <dimen name="fastscroll_thumb_width">64dp</dimen>
    <!-- Height of the fastscroll thumb -->
+1 −1
Original line number Diff line number Diff line
@@ -309,9 +309,9 @@
  <java-symbol type="dimen" name="dropdownitem_icon_width" />
  <java-symbol type="dimen" name="dropdownitem_text_padding_left" />
  <java-symbol type="dimen" name="fastscroll_overlay_size" />
  <java-symbol type="dimen" name="fastscroll_overlay_padding" />
  <java-symbol type="dimen" name="fastscroll_thumb_height" />
  <java-symbol type="dimen" name="fastscroll_thumb_width" />
  <java-symbol type="dimen" name="fastscroll_thumb_width" />
  <java-symbol type="dimen" name="password_keyboard_spacebar_vertical_correction" />
  <java-symbol type="dimen" name="search_view_preferred_width" />
  <java-symbol type="dimen" name="textview_error_popup_default_width" />