All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.nacular.doodle.dom.impl.ElementRulerImpl.kt Maven / Gradle / Ivy

There is a newer version: 0.10.2
Show newest version
package io.nacular.doodle.dom.impl

import io.nacular.doodle.dom.ElementRuler
import io.nacular.doodle.dom.HTMLElement
import io.nacular.doodle.dom.HtmlFactory
import io.nacular.doodle.dom.height
import io.nacular.doodle.dom.insert
import io.nacular.doodle.dom.parent
import io.nacular.doodle.dom.width
import io.nacular.doodle.dom.willChange
import io.nacular.doodle.geometry.Size

internal class ElementRulerImpl(htmlFactory: HtmlFactory): ElementRuler {

    override fun width (element: HTMLElement) = measure(element) { width               }
    override fun height(element: HTMLElement) = measure(element) { height              }
    override fun size  (element: HTMLElement) = measure(element) { Size(width, height) }

    private val ruler = htmlFactory.root

    fun  measure(element: HTMLElement, block: HTMLElement.() -> T): T {
        val old = element.style.willChange

        element.style.willChange = "transform"

        return when (element.parent) {
            null -> {
                ruler.insert(element, 0)

                element.run(block).also {
                    ruler.removeChild(element)
                }
            }
            else -> element.run(block)
        }.also {
            element.style.willChange = old
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy