commonMain.io.nacular.doodle.dom.impl.ElementRulerImpl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of browser Show documentation
Show all versions of browser Show documentation
A pure Kotlin, UI framework for the Web and Desktop
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
}
}
}