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

commonMain.io.nacular.doodle.utils.Dimension.kt Maven / Gradle / Ivy

package io.nacular.doodle.utils

import io.nacular.doodle.core.View
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

public enum class Dimension { Width, Height }

/**
 * Utility to help speed up working with sets of [Dimension].
 */
internal class DimensionSet(private val delegate: Set): Set by delegate {
    private val hasWidth  = Dimension.Width in delegate
    private val hasHeight = Dimension.Height in delegate

    override fun contains(element: Dimension): Boolean = when (element) {
        Dimension.Width  -> hasWidth
        Dimension.Height -> hasHeight
    }
}

internal inline fun dimensionSetProperty(initial: Set, noinline onChange: View.(old: Set, new: Set) -> Unit = { _,_ -> }) = object: ReadWriteProperty> {
    private var value = DimensionSet(initial)

    override fun getValue(thisRef: View, property: KProperty<*>) = value

    override fun setValue(thisRef: View, property: KProperty<*>, value: Set) {
        if (this.value == value) return

        val old = this.value
        this.value = DimensionSet(value)

        onChange(thisRef, old, this.value)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy