Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

TextView.setCompoundDrawable... with default arguments #147

Open
tomaszrykala opened this issue Feb 5, 2018 · 7 comments
Open

TextView.setCompoundDrawable... with default arguments #147

tomaszrykala opened this issue Feb 5, 2018 · 7 comments

Comments

@tomaszrykala
Copy link

tomaszrykala commented Feb 5, 2018

eg.

@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
fun TextView.setCompoundDrawablesRelative(
        start: Drawable? = null,
        top: Drawable? = null,
        end: Drawable? = null,
        bottom: Drawable? = null
) {
    setCompoundDrawablesRelative(start, top, end, bottom)
}
// same for the other, similar methods
@JakeWharton
Copy link
Contributor

Name won't work. Extensions cannot override members.

@tomaszrykala
Copy link
Author

True, thanks. So replace set with update, eg. fun TextView.updateCompoundDrawablesRelative(...) ?

@JakeWharton
Copy link
Contributor

JakeWharton commented Feb 5, 2018 via email

@tomaszrykala
Copy link
Author

Will do, thanks.

@fredy-mederos
Copy link

The compound drawables extensions could be "var" extensions like this?

var TextView.drawableTop: Drawable?
    get() = compoundDrawables[1]
    set(value) {
        setCompoundDrawables(
            compoundDrawables[0],
            value,
            compoundDrawables[2],
            compoundDrawables[3]
        )
    }

Thanks

@consp1racy
Copy link

@fredy-mederos That's var TextView.compoundDrawableTop.

@consp1racy
Copy link

@tomaszrykala null is a valid argument for setting compound drawables. Perhaps we should use a marker object.

private object DoNotChange : Drawable() {
    override fun draw(canvas: Canvas) {
    }

    override fun setAlpha(alpha: Int) {
    }

    override fun getOpacity(): Int = PixelFormat.TRANSPARENT

    override fun setColorFilter(colorFilter: ColorFilter?) {
    }
}

fun TextView.updateCompoundDrawablesWithIntrinsicBounds(
        left: Drawable? = DoNotChange,
        top: Drawable? = DoNotChange,
        right: Drawable? = DoNotChange,
        bottom: Drawable? = DoNotChange
) {
    val compoundDrawables = compoundDrawables
    val newLeft = if (left != DoNotChange) left else compoundDrawables[0]
    val newTop = if (top != DoNotChange) top else compoundDrawables[1]
    val newRight = if (right != DoNotChange) right else compoundDrawables[2]
    val newBottom = if (bottom != DoNotChange) bottom else compoundDrawables[3]
    setCompoundDrawablesWithIntrinsicBounds(newLeft, newTop, newRight, newBottom)
}

It's not as fast and pretty, probably not a great candidate for inlining. getCompoundDrawables() involves cloning so we get just one local copy ahead of time.

And do that for all the intrinsic and non-intrinsic and relative and absolute variants, of course.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants