[orx-panel] Rename WatchDiv to WatchListDiv, add WatchPropertyDiv

This commit is contained in:
Edwin Jakobs
2020-07-06 13:23:13 +02:00
parent a9e12634bc
commit 3528d35cbf
3 changed files with 80 additions and 5 deletions

View File

@@ -113,8 +113,8 @@ fun main() = application {
}
}
watchDiv("matrix", watchList = programState.matrix) { row ->
watchDiv("row", watchList = row) { item ->
watchListDiv("matrix", watchList = programState.matrix) { row ->
watchListDiv("row", watchList = row) { item ->
this.id = "some-row"
slider {
label = "value"

View File

@@ -5,7 +5,7 @@ import kotlinx.coroutines.yield
import org.openrndr.draw.Drawer
import org.openrndr.launch
class WatchDiv<T : Any>(private val watchList: List<T>, private val builder: WatchDiv<T>.(T) -> Unit) : Div(), DisposableElement {
class WatchListDiv<T : Any>(private val watchList: List<T>, private val builder: WatchListDiv<T>.(T) -> Unit) : Div(), DisposableElement {
override var disposed: Boolean = false
private var listState = emptyList<T>()
private var watchJob: Job? = null
@@ -62,8 +62,8 @@ class WatchDiv<T : Any>(private val watchList: List<T>, private val builder: Wat
}
}
fun <T : Any> Element.watchDiv(vararg classes: String, watchList: List<T>, builder: WatchDiv<T>.(T) -> Unit) {
val wd = WatchDiv(watchList, builder)
fun <T : Any> Element.watchListDiv(vararg classes: String, watchList: List<T>, builder: WatchListDiv<T>.(T) -> Unit) {
val wd = WatchListDiv(watchList, builder)
wd.classes.addAll(classes.map { ElementClass(it) })
this.append(wd)
wd.regenerate()

View File

@@ -0,0 +1,75 @@
package org.openrndr.panel.elements
import kotlinx.coroutines.Job
import kotlinx.coroutines.yield
import org.openrndr.draw.Drawer
import org.openrndr.launch
import org.openrndr.panel.elements.*
import kotlin.reflect.KMutableProperty0
class WatchPropertyDiv<T : Any>(
private val watchProperty: KMutableProperty0<T>,
private val builder: WatchPropertyDiv<T>.(T) -> Unit
) : Div(),
DisposableElement {
override var disposed: Boolean = false
private var propertyState = watchProperty.get()
private var watchJob: Job? = null
override fun dispose() {
super.dispose()
for (child in children) {
child.parent = null
(child as? DisposableElement)?.dispose()
}
children.clear()
}
fun regenerate(force: Boolean = false) {
var regenerate = force
if (watchProperty.get() != propertyState) {
regenerate = true
}
if (regenerate) {
for (child in children) {
child.parent = null
(child as? DisposableElement)?.dispose()
}
propertyState = watchProperty.get()
children.clear()
builder(propertyState)
requestRedraw()
}
}
fun checkJob() {
if (watchJob == null) {
watchJob = (root() as? Body)?.controlManager?.program?.launch {
while (!disposed) {
regenerate()
yield()
}
}
}
}
override fun draw(drawer: Drawer) {
checkJob()
super.draw(drawer)
}
}
fun <T : Any> Element.watchPropertyDiv(
vararg classes: String,
watchProperty: KMutableProperty0<T>,
builder: WatchPropertyDiv<T>.(T) -> Unit
) {
val wd = WatchPropertyDiv(watchProperty, builder)
wd.classes.addAll(classes.map { ElementClass(it) })
this.append(wd)
wd.regenerate(true)
wd.checkJob()
}