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

Commit e5f1fff6 authored by Alan Viverette's avatar Alan Viverette
Browse files

Fix fast scroller thumb and track margins for Material

Bug: 20091610
Change-Id: Iab023872381aee16be2c83fcf1a622a20619ca49
parent 1ffaca44
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -1301,6 +1301,7 @@ package android {
    field public static final int thicknessRatio = 16843164; // 0x101019c
    field public static final int thicknessRatio = 16843164; // 0x101019c
    field public static final int thumb = 16843074; // 0x1010142
    field public static final int thumb = 16843074; // 0x1010142
    field public static final int thumbOffset = 16843075; // 0x1010143
    field public static final int thumbOffset = 16843075; // 0x1010143
    field public static final int thumbPosition = 16844013; // 0x10104ed
    field public static final int thumbTextPadding = 16843634; // 0x1010372
    field public static final int thumbTextPadding = 16843634; // 0x1010372
    field public static final int thumbTint = 16843889; // 0x1010471
    field public static final int thumbTint = 16843889; // 0x1010471
    field public static final int thumbTintMode = 16843890; // 0x1010472
    field public static final int thumbTintMode = 16843890; // 0x1010472
+1 −0
Original line number Original line Diff line number Diff line
@@ -1378,6 +1378,7 @@ package android {
    field public static final int thicknessRatio = 16843164; // 0x101019c
    field public static final int thicknessRatio = 16843164; // 0x101019c
    field public static final int thumb = 16843074; // 0x1010142
    field public static final int thumb = 16843074; // 0x1010142
    field public static final int thumbOffset = 16843075; // 0x1010143
    field public static final int thumbOffset = 16843075; // 0x1010143
    field public static final int thumbPosition = 16844013; // 0x10104ed
    field public static final int thumbTextPadding = 16843634; // 0x1010372
    field public static final int thumbTextPadding = 16843634; // 0x1010372
    field public static final int thumbTint = 16843889; // 0x1010471
    field public static final int thumbTint = 16843889; // 0x1010471
    field public static final int thumbTintMode = 16843890; // 0x1010472
    field public static final int thumbTintMode = 16843890; // 0x1010472
+85 −57
Original line number Original line Diff line number Diff line
@@ -46,6 +46,8 @@ import android.view.ViewGroupOverlay;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView.ScaleType;
import android.widget.ImageView.ScaleType;


import com.android.internal.R;

/**
/**
 * Helper class for AbsListView to draw and control the Fast Scroll thumb
 * Helper class for AbsListView to draw and control the Fast Scroll thumb
 */
 */
@@ -82,6 +84,10 @@ class FastScroller {
    private static final int OVERLAY_AT_THUMB = 1;
    private static final int OVERLAY_AT_THUMB = 1;
    private static final int OVERLAY_ABOVE_THUMB = 2;
    private static final int OVERLAY_ABOVE_THUMB = 2;


    // Positions for thumb in relation to track.
    private static final int THUMB_POSITION_MIDPOINT = 0;
    private static final int THUMB_POSITION_INSIDE = 1;

    // Indices for mPreviewResId.
    // Indices for mPreviewResId.
    private static final int PREVIEW_LEFT = 0;
    private static final int PREVIEW_LEFT = 0;
    private static final int PREVIEW_RIGHT = 1;
    private static final int PREVIEW_RIGHT = 1;
@@ -100,7 +106,6 @@ class FastScroller {
    private final ImageView mThumbImage;
    private final ImageView mThumbImage;
    private final ImageView mTrackImage;
    private final ImageView mTrackImage;
    private final View mPreviewImage;
    private final View mPreviewImage;

    /**
    /**
     * Preview image resource IDs for left- and right-aligned layouts. See
     * Preview image resource IDs for left- and right-aligned layouts. See
     * {@link #PREVIEW_LEFT} and {@link #PREVIEW_RIGHT}.
     * {@link #PREVIEW_LEFT} and {@link #PREVIEW_RIGHT}.
@@ -130,6 +135,11 @@ class FastScroller {
    private Drawable mThumbDrawable;
    private Drawable mThumbDrawable;
    private Drawable mTrackDrawable;
    private Drawable mTrackDrawable;
    private int mTextAppearance;
    private int mTextAppearance;
    private int mThumbPosition;

    // Used to convert between y-coordinate and thumb position within track.
    private float mThumbOffset;
    private float mThumbRange;


    /** Total width of decorations. */
    /** Total width of decorations. */
    private int mWidth;
    private int mWidth;
@@ -278,7 +288,6 @@ class FastScroller {
    }
    }


    private void updateAppearance() {
    private void updateAppearance() {
        final Context context = mList.getContext();
        int width = 0;
        int width = 0;


        // Add track to overlay if it has an image.
        // Add track to overlay if it has an image.
@@ -298,12 +307,9 @@ class FastScroller {
        // Account for minimum thumb width.
        // Account for minimum thumb width.
        mWidth = Math.max(width, mThumbMinWidth);
        mWidth = Math.max(width, mThumbMinWidth);


        mPreviewImage.setMinimumWidth(mPreviewMinWidth);
        mPreviewImage.setMinimumHeight(mPreviewMinHeight);

        if (mTextAppearance != 0) {
        if (mTextAppearance != 0) {
            mPrimaryText.setTextAppearance(context, mTextAppearance);
            mPrimaryText.setTextAppearance(mTextAppearance);
            mSecondaryText.setTextAppearance(context, mTextAppearance);
            mSecondaryText.setTextAppearance(mTextAppearance);
        }
        }


        if (mTextColor != null) {
        if (mTextColor != null) {
@@ -316,13 +322,11 @@ class FastScroller {
            mSecondaryText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
            mSecondaryText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
        }
        }


        final int textMinSize = Math.max(0, mPreviewMinHeight);
        final int padding = mPreviewPadding;
        mPrimaryText.setMinimumWidth(textMinSize);
        mPrimaryText.setMinimumHeight(textMinSize);
        mPrimaryText.setIncludeFontPadding(false);
        mPrimaryText.setIncludeFontPadding(false);
        mSecondaryText.setMinimumWidth(textMinSize);
        mPrimaryText.setPadding(padding, padding, padding, padding);
        mSecondaryText.setMinimumHeight(textMinSize);
        mSecondaryText.setIncludeFontPadding(false);
        mSecondaryText.setIncludeFontPadding(false);
        mSecondaryText.setPadding(padding, padding, padding, padding);


        refreshDrawablePressedState();
        refreshDrawablePressedState();
    }
    }
@@ -330,50 +334,53 @@ class FastScroller {
    public void setStyle(@StyleRes int resId) {
    public void setStyle(@StyleRes int resId) {
        final Context context = mList.getContext();
        final Context context = mList.getContext();
        final TypedArray ta = context.obtainStyledAttributes(null,
        final TypedArray ta = context.obtainStyledAttributes(null,
                com.android.internal.R.styleable.FastScroll, android.R.attr.fastScrollStyle, resId);
                R.styleable.FastScroll, R.attr.fastScrollStyle, resId);
        final int N = ta.getIndexCount();
        final int N = ta.getIndexCount();
        for (int i = 0; i < N; i++) {
        for (int i = 0; i < N; i++) {
            final int index = ta.getIndex(i);
            final int index = ta.getIndex(i);
            switch (index) {
            switch (index) {
                case com.android.internal.R.styleable.FastScroll_position:
                case R.styleable.FastScroll_position:
                    mOverlayPosition = ta.getInt(index, OVERLAY_FLOATING);
                    mOverlayPosition = ta.getInt(index, OVERLAY_FLOATING);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_backgroundLeft:
                case R.styleable.FastScroll_backgroundLeft:
                    mPreviewResId[PREVIEW_LEFT] = ta.getResourceId(index, 0);
                    mPreviewResId[PREVIEW_LEFT] = ta.getResourceId(index, 0);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_backgroundRight:
                case R.styleable.FastScroll_backgroundRight:
                    mPreviewResId[PREVIEW_RIGHT] = ta.getResourceId(index, 0);
                    mPreviewResId[PREVIEW_RIGHT] = ta.getResourceId(index, 0);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_thumbDrawable:
                case R.styleable.FastScroll_thumbDrawable:
                    mThumbDrawable = ta.getDrawable(index);
                    mThumbDrawable = ta.getDrawable(index);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_trackDrawable:
                case R.styleable.FastScroll_trackDrawable:
                    mTrackDrawable = ta.getDrawable(index);
                    mTrackDrawable = ta.getDrawable(index);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_textAppearance:
                case R.styleable.FastScroll_textAppearance:
                    mTextAppearance = ta.getResourceId(index, 0);
                    mTextAppearance = ta.getResourceId(index, 0);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_textColor:
                case R.styleable.FastScroll_textColor:
                    mTextColor = ta.getColorStateList(index);
                    mTextColor = ta.getColorStateList(index);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_textSize:
                case R.styleable.FastScroll_textSize:
                    mTextSize = ta.getDimensionPixelSize(index, 0);
                    mTextSize = ta.getDimensionPixelSize(index, 0);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_minWidth:
                case R.styleable.FastScroll_minWidth:
                    mPreviewMinWidth = ta.getDimensionPixelSize(index, 0);
                    mPreviewMinWidth = ta.getDimensionPixelSize(index, 0);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_minHeight:
                case R.styleable.FastScroll_minHeight:
                    mPreviewMinHeight = ta.getDimensionPixelSize(index, 0);
                    mPreviewMinHeight = ta.getDimensionPixelSize(index, 0);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_thumbMinWidth:
                case R.styleable.FastScroll_thumbMinWidth:
                    mThumbMinWidth = ta.getDimensionPixelSize(index, 0);
                    mThumbMinWidth = ta.getDimensionPixelSize(index, 0);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_thumbMinHeight:
                case R.styleable.FastScroll_thumbMinHeight:
                    mThumbMinHeight = ta.getDimensionPixelSize(index, 0);
                    mThumbMinHeight = ta.getDimensionPixelSize(index, 0);
                    break;
                    break;
                case com.android.internal.R.styleable.FastScroll_padding:
                case R.styleable.FastScroll_padding:
                    mPreviewPadding = ta.getDimensionPixelSize(index, 0);
                    mPreviewPadding = ta.getDimensionPixelSize(index, 0);
                    break;
                    break;
                case R.styleable.FastScroll_thumbPosition:
                    mThumbPosition = ta.getInt(index, THUMB_POSITION_MIDPOINT);
                    break;
            }
            }
        }
        }


@@ -478,14 +485,16 @@ class FastScroller {
            final int previewResId = mPreviewResId[mLayoutFromRight ? PREVIEW_RIGHT : PREVIEW_LEFT];
            final int previewResId = mPreviewResId[mLayoutFromRight ? PREVIEW_RIGHT : PREVIEW_LEFT];
            mPreviewImage.setBackgroundResource(previewResId);
            mPreviewImage.setBackgroundResource(previewResId);


            // Add extra padding for text.
            // Propagate padding to text min width/height.
            final Drawable background = mPreviewImage.getBackground();
            final int textMinWidth = Math.max(0, mPreviewMinWidth - mPreviewImage.getPaddingLeft()
            if (background != null) {
                    - mPreviewImage.getPaddingRight());
                final Rect padding = mTempBounds;
            mPrimaryText.setMinimumWidth(textMinWidth);
                background.getPadding(padding);
            mSecondaryText.setMinimumWidth(textMinWidth);
                padding.offset(mPreviewPadding, mPreviewPadding);

                mPreviewImage.setPadding(padding.left, padding.top, padding.right, padding.bottom);
            final int textMinHeight = Math.max(0, mPreviewMinHeight - mPreviewImage.getPaddingTop()
            }
                    - mPreviewImage.getPaddingBottom());
            mPrimaryText.setMinimumHeight(textMinHeight);
            mSecondaryText.setMinimumHeight(textMinHeight);


            // Requires re-layout.
            // Requires re-layout.
            updateLayout();
            updateLayout();
@@ -560,6 +569,8 @@ class FastScroller {
        layoutThumb();
        layoutThumb();
        layoutTrack();
        layoutTrack();


        updateOffsetAndRange();

        final Rect bounds = mTempBounds;
        final Rect bounds = mTempBounds;
        measurePreview(mPrimaryText, bounds);
        measurePreview(mPrimaryText, bounds);
        applyLayout(mPrimaryText, bounds);
        applyLayout(mPrimaryText, bounds);
@@ -758,15 +769,45 @@ class FastScroller {
        final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        track.measure(widthMeasureSpec, heightMeasureSpec);
        track.measure(widthMeasureSpec, heightMeasureSpec);


        final int trackWidth = track.getMeasuredWidth();
        final int top;
        final int bottom;
        if (mThumbPosition == THUMB_POSITION_INSIDE) {
            top = container.top;
            bottom = container.bottom;
        } else {
            final int thumbHalfHeight = thumb.getHeight() / 2;
            final int thumbHalfHeight = thumb.getHeight() / 2;
            top = container.top + thumbHalfHeight;
            bottom = container.bottom - thumbHalfHeight;
        }

        final int trackWidth = track.getMeasuredWidth();
        final int left = thumb.getLeft() + (thumb.getWidth() - trackWidth) / 2;
        final int left = thumb.getLeft() + (thumb.getWidth() - trackWidth) / 2;
        final int right = left + trackWidth;
        final int right = left + trackWidth;
        final int top = container.top + thumbHalfHeight;
        final int bottom = container.bottom - thumbHalfHeight;
        track.layout(left, top, right, bottom);
        track.layout(left, top, right, bottom);
    }
    }


    /**
     * Updates the offset and range used to convert from absolute y-position to
     * thumb position within the track.
     */
    private void updateOffsetAndRange() {
        final View trackImage = mTrackImage;
        final View thumbImage = mThumbImage;
        final float min;
        final float max;
        if (mThumbPosition == THUMB_POSITION_INSIDE) {
            final float halfThumbHeight = thumbImage.getHeight() / 2f;
            min = trackImage.getTop() + halfThumbHeight;
            max = trackImage.getBottom() - halfThumbHeight;
        } else{
            min = trackImage.getTop();
            max = trackImage.getBottom();
        }

        mThumbOffset = min;
        mThumbRange = max - min;
    }

    private void setState(int state) {
    private void setState(int state) {
        mList.removeCallbacks(mDeferHide);
        mList.removeCallbacks(mDeferHide);


@@ -1145,18 +1186,8 @@ class FastScroller {
     *            to place the thumb.
     *            to place the thumb.
     */
     */
    private void setThumbPos(float position) {
    private void setThumbPos(float position) {
        final Rect container = mContainerRect;
        final float thumbMiddle = position * mThumbRange + mThumbOffset;
        final int top = container.top;
        mThumbImage.setTranslationY(thumbMiddle - mThumbImage.getHeight() / 2f);
        final int bottom = container.bottom;

        final View trackImage = mTrackImage;
        final View thumbImage = mThumbImage;
        final float min = trackImage.getTop();
        final float max = trackImage.getBottom();
        final float offset = min;
        final float range = max - min;
        final float thumbMiddle = position * range + offset;
        thumbImage.setTranslationY(thumbMiddle - thumbImage.getHeight() / 2);


        final View previewImage = mPreviewImage;
        final View previewImage = mPreviewImage;
        final float previewHalfHeight = previewImage.getHeight() / 2f;
        final float previewHalfHeight = previewImage.getHeight() / 2f;
@@ -1175,6 +1206,9 @@ class FastScroller {
        }
        }


        // Center the preview on the thumb, constrained to the list bounds.
        // Center the preview on the thumb, constrained to the list bounds.
        final Rect container = mContainerRect;
        final int top = container.top;
        final int bottom = container.bottom;
        final float minP = top + previewHalfHeight;
        final float minP = top + previewHalfHeight;
        final float maxP = bottom - previewHalfHeight;
        final float maxP = bottom - previewHalfHeight;
        final float previewMiddle = MathUtils.constrain(previewPos, minP, maxP);
        final float previewMiddle = MathUtils.constrain(previewPos, minP, maxP);
@@ -1186,19 +1220,13 @@ class FastScroller {
    }
    }


    private float getPosFromMotionEvent(float y) {
    private float getPosFromMotionEvent(float y) {
        final View trackImage = mTrackImage;
        final float min = trackImage.getTop();
        final float max = trackImage.getBottom();
        final float offset = min;
        final float range = max - min;

        // If the list is the same height as the thumbnail or shorter,
        // If the list is the same height as the thumbnail or shorter,
        // effectively disable scrolling.
        // effectively disable scrolling.
        if (range <= 0) {
        if (mThumbRange <= 0) {
            return 0f;
            return 0f;
        }
        }


        return MathUtils.constrain((y - offset) / range, 0f, 1f);
        return MathUtils.constrain((y - mThumbOffset) / mThumbRange, 0f, 1f);
    }
    }


    /**
    /**
+15 −11
Original line number Original line Diff line number Diff line
@@ -14,14 +14,18 @@
     limitations under the License.
     limitations under the License.
-->
-->


<shape xmlns:android="http://schemas.android.com/apk/res/android"
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
       android:insetLeft="16dp">
    <shape
        android:shape="rectangle"
        android:tint="?attr/colorControlActivated">
        <corners
        <corners
            android:topLeftRadius="44dp"
            android:topLeftRadius="44dp"
            android:topRightRadius="44dp"
            android:topRightRadius="44dp"
            android:bottomRightRadius="44dp" />
            android:bottomRightRadius="44dp" />
        <padding
        <padding
        android:paddingLeft="22dp"
            android:left="22dp"
        android:paddingRight="22dp" />
            android:right="22dp" />
    <solid android:color="?attr/colorControlActivated" />
        <solid android:color="@color/white" />
    </shape>
    </shape>
</inset>
+15 −11
Original line number Original line Diff line number Diff line
@@ -14,14 +14,18 @@
     limitations under the License.
     limitations under the License.
-->
-->


<shape xmlns:android="http://schemas.android.com/apk/res/android"
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
       android:insetRight="16dp">
    <shape
        android:shape="rectangle"
        android:tint="?attr/colorControlActivated">
        <corners
        <corners
            android:topLeftRadius="44dp"
            android:topLeftRadius="44dp"
            android:topRightRadius="44dp"
            android:topRightRadius="44dp"
            android:bottomLeftRadius="44dp" />
            android:bottomLeftRadius="44dp" />
        <padding
        <padding
        android:paddingLeft="22dp"
            android:left="22dp"
        android:paddingRight="22dp" />
            android:right="22dp" />
    <solid android:color="?attr/colorControlActivated" />
        <solid android:color="@color/white" />
    </shape>
    </shape>
</inset>
Loading