Giter Club home page Giter Club logo

Comments (7)

JakeWharton avatar JakeWharton commented on May 18, 2024 1

from android-ktx.

JakeWharton avatar JakeWharton commented on May 18, 2024

Name won't work. Extensions cannot override members.

from android-ktx.

tomaszrykala avatar tomaszrykala commented on May 18, 2024

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

from android-ktx.

tomaszrykala avatar tomaszrykala commented on May 18, 2024

Will do, thanks.

from android-ktx.

fredy-mederos avatar fredy-mederos commented on May 18, 2024

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

from android-ktx.

consp1racy avatar consp1racy commented on May 18, 2024

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

from android-ktx.

consp1racy avatar consp1racy commented on May 18, 2024

@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.

from android-ktx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.