1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright (c) 2017-present Robert Jaros
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package pl.treksoft.kvision.core
import kotlin.reflect.KProperty
/**
* CSS style object.
*
* @constructor
* @param className optional name of the CSS class, it will be generated if not specified
* @param parentStyle parent CSS style object
* @param init an initializer extension function
*/
@Suppress("TooManyFunctions")
open class Style(className: String? = null, parentStyle: Style? = null, init: (Style.() -> Unit)? = null) :
StyledComponent() {
private val propertyValues: MutableMap<String, Any?> = mutableMapOf()
private val newClassName: String = if (parentStyle == null) {
className ?: "kv_styleclass_${counter++}"
} else {
"${parentStyle.className} " + (className ?: ".kv_styleclass_${counter++}")
}
/**
* The name of the CSS class.
*/
var className: String by refreshOnUpdate(newClassName)
init {
@Suppress("LeakingThis")
styles.add(this)
@Suppress("LeakingThis")
init?.invoke(this)
}
internal fun generateStyle(): String {
val styles = getSnStyleInternal()
return ".$className {\n" + styles.joinToString("\n") {
"${it.first}: ${it.second};"
} + "\n}"
}
protected fun <T> refreshOnUpdate(refreshFunction: ((T) -> Unit) = { this.refresh() }) =
RefreshDelegateProvider<T>(null, refreshFunction)
protected fun <T> refreshOnUpdate(initialValue: T, refreshFunction: ((T) -> Unit) = { this.refresh() }) =
RefreshDelegateProvider(initialValue, refreshFunction)
protected inner class RefreshDelegateProvider<T>(
private val initialValue: T?, private val refreshFunction: (T) -> Unit
) {
operator fun provideDelegate(thisRef: Any?, prop: KProperty<*>): RefreshDelegate<T> {
if (initialValue != null) propertyValues[prop.name] = initialValue
return RefreshDelegate(refreshFunction)
}
}
protected inner class RefreshDelegate<T>(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<Style>()
}
}
/**
* DSL builder extension function.
*
* It takes the same parameters as the constructor of the built component.
*/
fun Widget.style(className: String? = null, init: (Style.() -> Unit)? = null): Style {
val style = Style(className, null, init)
this.addCssStyle(style)
return style
}
/**
* DSL builder extension function for cascading styles.
*
* It takes the same parameters as the constructor of the built component.
*/
fun Style.style(className: String? = null, init: (Style.() -> Unit)? = null): Style {
return Style(className, this, init)
}
|