From 6a92d7e67326eeec6040c94983b79f288a32a386 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Thu, 18 Apr 2019 22:48:36 +0200 Subject: Add internal cache for styles generation code. --- src/main/kotlin/pl/treksoft/kvision/core/Css.kt | 10 + src/main/kotlin/pl/treksoft/kvision/core/Style.kt | 34 ++ .../pl/treksoft/kvision/core/StyledComponent.kt | 362 +++++++++++---------- src/main/kotlin/pl/treksoft/kvision/core/Widget.kt | 34 ++ src/main/kotlin/pl/treksoft/kvision/utils/Cache.kt | 198 +++++++++++ 5 files changed, 465 insertions(+), 173 deletions(-) create mode 100644 src/main/kotlin/pl/treksoft/kvision/utils/Cache.kt (limited to 'src/main') diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Css.kt b/src/main/kotlin/pl/treksoft/kvision/core/Css.kt index 9fc50097..8a468fe5 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Css.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Css.kt @@ -552,6 +552,8 @@ class Border private constructor( val w = width?.asString() return w.orEmpty() + " " + (style?.borderStyle).orEmpty() + " " + color.orEmpty() } + + override fun toString() = asString() } /** @@ -573,6 +575,8 @@ class Color private constructor(private val color: String? = null) { fun asString(): String { return color.orEmpty() } + + override fun toString() = asString() } /** @@ -676,6 +680,8 @@ class Background private constructor( } + " " + (repeat?.repeat).orEmpty() + " " + (origin?.origin).orEmpty() + " " + (clip?.clip).orEmpty() + " " + (attachment?.attachment).orEmpty() } + + override fun toString() = asString() } /** @@ -717,6 +723,8 @@ class TextDecoration private constructor( (style?.textDecorationStyle).orEmpty() + " " + color.orEmpty() } + + override fun toString() = asString() } /** @@ -764,4 +772,6 @@ class TextShadow private constructor( (blurRadius?.asString()).orEmpty() + " " + color.orEmpty() } + + override fun toString() = asString() } diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Style.kt b/src/main/kotlin/pl/treksoft/kvision/core/Style.kt index 549aa44c..282d2e7e 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Style.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Style.kt @@ -26,6 +26,7 @@ import com.github.snabbdom.h import org.w3c.dom.Node import pl.treksoft.jquery.JQuery import pl.treksoft.kvision.panel.Root +import kotlin.reflect.KProperty /** * CSS style object. @@ -38,6 +39,7 @@ import pl.treksoft.kvision.panel.Root @Suppress("TooManyFunctions") open class Style(className: String? = null, parentStyle: Style? = null, init: (Style.() -> Unit)? = null) : StyledComponent() { + private val propertyValues: MutableMap = mutableMapOf() override var parent: Container? = Root.getFirstRoot() @@ -133,6 +135,38 @@ open class Style(className: String? = null, parentStyle: Style? = null, init: (S styles.remove(this) } + protected fun refreshOnUpdate(refreshFunction: ((T) -> Unit) = { this.refresh() }) = + RefreshDelegateProvider(null, refreshFunction) + + protected fun refreshOnUpdate(initialValue: T, refreshFunction: ((T) -> Unit) = { this.refresh() }) = + RefreshDelegateProvider(initialValue, refreshFunction) + + protected inner class RefreshDelegateProvider( + private val initialValue: T?, private val refreshFunction: (T) -> Unit + ) { + operator fun provideDelegate(thisRef: Any?, prop: KProperty<*>): RefreshDelegate { + if (initialValue != null) propertyValues[prop.name] = initialValue + return RefreshDelegate(refreshFunction) + } + } + + protected inner class RefreshDelegate(private val refreshFunction: ((T) -> Unit)) { + @Suppress("UNCHECKED_CAST") + operator fun getValue(thisRef: StyledComponent, property: KProperty<*>): T { + val value = propertyValues[property.name] + return if (value != null) { + value as T + } else { + null as T + } + } + + operator fun setValue(thisRef: StyledComponent, property: KProperty<*>, value: T) { + propertyValues[property.name] = value + refreshFunction(value) + } + } + companion object { internal var counter = 0 internal var styles = mutableListOf