diff options
Diffstat (limited to 'src/main/kotlin/pl/treksoft')
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/core/Style.kt | 14 | ||||
-rw-r--r-- | src/main/kotlin/pl/treksoft/kvision/panel/Root.kt | 39 |
2 files changed, 28 insertions, 25 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/core/Style.kt b/src/main/kotlin/pl/treksoft/kvision/core/Style.kt index c2858fa0..62154f77 100644 --- a/src/main/kotlin/pl/treksoft/kvision/core/Style.kt +++ b/src/main/kotlin/pl/treksoft/kvision/core/Style.kt @@ -40,7 +40,7 @@ import pl.treksoft.kvision.panel.Root open class Style(className: String? = null, parentStyle: Style? = null, init: (Style.() -> Unit)? = null) : StyledComponent() { - override var parent: Container? = null + override var parent: Container? = Root.getFirstRoot() private val newClassName: String = if (parentStyle == null) { className ?: "kv_styleclass_${counter++}" @@ -54,15 +54,7 @@ open class Style(className: String? = null, parentStyle: Style? = null, init: (S var className: String by refreshOnUpdate(newClassName) init { - val root = Root.getLastRoot() - @Suppress("LeakingThis") - parent = root - if (root != null) { - @Suppress("LeakingThis") - root.addStyle(this) - } else { - println("At least one Root object is required to create a style object!") - } + styles.add(this) @Suppress("LeakingThis") init?.invoke(this) } @@ -141,10 +133,12 @@ open class Style(className: String? = null, parentStyle: Style? = null, init: (S } override fun dispose() { + styles.remove(this) } companion object { internal var counter = 0 + internal var styles = mutableListOf<Style>() /** * DSL builder extension function. diff --git a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt index 1a90f7fc..16d3a0f8 100644 --- a/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt +++ b/src/main/kotlin/pl/treksoft/kvision/panel/Root.kt @@ -46,17 +46,21 @@ import pl.treksoft.kvision.utils.snOpt */ @Suppress("TooManyFunctions") class Root(id: String, private val fixed: Boolean = false, init: (Root.() -> Unit)? = null) : SimplePanel() { - private val styles: MutableList<Style> = mutableListOf() private val modals: MutableList<Modal> = mutableListOf() private val contextMenus: MutableList<ContextMenu> = mutableListOf() private var rootVnode: VNode = renderVNode() internal var renderDisabled = false + private val isFirstRoot = roots.isEmpty() + init { rootVnode = KVManager.patch(id, this.renderVNode()) this.id = id roots.add(this) + if (isFirstRoot) { + Style.styles.forEach { it.parent = this } + } @Suppress("LeakingThis") init?.invoke(this) } @@ -71,12 +75,6 @@ class Root(id: String, private val fixed: Boolean = false, init: (Root.() -> Uni } } - internal fun addStyle(style: Style) { - styles.add(style) - style.parent = this - refresh() - } - internal fun addModal(modal: Modal) { modals.add(modal) modal.parent = this @@ -96,10 +94,14 @@ class Root(id: String, private val fixed: Boolean = false, init: (Root.() -> Uni } private fun stylesVNodes(): Array<VNode> { - val visibleStyles = styles.filter { it.visible } - return if (visibleStyles.isNotEmpty()) { - val stylesDesc = visibleStyles.joinToString("\n") { it.generateStyle() } - arrayOf(h("style", arrayOf(stylesDesc))) + return if (isFirstRoot) { + val visibleStyles = Style.styles.filter { it.visible } + if (visibleStyles.isNotEmpty()) { + val stylesDesc = visibleStyles.joinToString("\n") { it.generateStyle() } + arrayOf(h("style", arrayOf(stylesDesc))) + } else { + arrayOf() + } } else { arrayOf() } @@ -135,18 +137,25 @@ class Root(id: String, private val fixed: Boolean = false, init: (Root.() -> Uni } override fun dispose() { - styles.forEach { it.dispose() } - modals.forEach { it.dispose() } - contextMenus.forEach { it.dispose() } super.dispose() roots.remove(this) + if (isFirstRoot) { + Style.styles.clear() + } } companion object { internal val roots: MutableList<Root> = mutableListOf() + internal fun getFirstRoot(): Root? { + return if (roots.isNotEmpty()) + roots[0] + else + null + } + internal fun getLastRoot(): Root? { - return if (roots.size > 0) + return if (roots.isNotEmpty()) roots[roots.size - 1] else null |