Property editor minor refactoring

This commit is contained in:
Alexander Nozik 2024-01-01 14:07:08 +03:00
parent 15ac3545b5
commit 30f6d51745
3 changed files with 17 additions and 36 deletions

View File

@ -25,4 +25,6 @@ public interface ComposeVisionRenderer: ElementVisionRenderer {
render(client, name, vision, meta) render(client, name, vision, meta)
} }
} }
public companion object
} }

View File

@ -49,8 +49,8 @@ public fun PropertyEditor(
initialExpanded: Boolean? = null, initialExpanded: Boolean? = null,
) { ) {
var expanded: Boolean by remember { mutableStateOf(initialExpanded ?: true) } var expanded: Boolean by remember { mutableStateOf(initialExpanded ?: true) }
val descriptor: MetaDescriptor? = remember(rootDescriptor, name) { rootDescriptor?.get(name) } val descriptor: MetaDescriptor? by derivedStateOf { rootDescriptor?.get(name) }
var property: MutableMeta by remember { mutableStateOf(rootMeta.getOrCreate(name)) } var displayedValue by remember { mutableStateOf(rootMeta.getValue(name)) }
var editorPropertyState: EditorPropertyState by remember { mutableStateOf(getPropertyState(name)) } var editorPropertyState: EditorPropertyState by remember { mutableStateOf(getPropertyState(name)) }
@ -68,13 +68,13 @@ public fun PropertyEditor(
val token = name.lastOrNull()?.toString() ?: "Properties" val token = name.lastOrNull()?.toString() ?: "Properties"
fun update() { fun update() {
property = rootMeta.getOrCreate(name) displayedValue = rootMeta.getValue(name)
editorPropertyState = getPropertyState(name) editorPropertyState = getPropertyState(name)
} }
LaunchedEffect(rootMeta) { LaunchedEffect(rootMeta) {
updates.collect { updatedName -> updates.collect { updatedName ->
if (updatedName == name) { if (name.startsWith(updatedName)) {
update() update()
} }
} }
@ -85,6 +85,7 @@ public fun PropertyEditor(
alignItems(AlignItems.Center) alignItems(AlignItems.Center)
} }
}) { }) {
//if node has children
if (keys.isNotEmpty()) { if (keys.isNotEmpty()) {
Span({ Span({
classes(TreeStyles.treeCaret) classes(TreeStyles.treeCaret)
@ -110,12 +111,14 @@ public fun PropertyEditor(
marginAll(1.px, 5.px) marginAll(1.px, 5.px)
} }
}) { }) {
ValueChooser(descriptor, editorPropertyState, property.value) { ValueChooser(descriptor, editorPropertyState, displayedValue) {
property.value = it rootMeta.setValue(name, it)
editorPropertyState = getPropertyState(name) update()
} }
} }
}
if (!name.isEmpty()) {
CloseButton(editorPropertyState != EditorPropertyState.Defined) { CloseButton(editorPropertyState != EditorPropertyState.Defined) {
rootMeta.remove(name) rootMeta.remove(name)
update() update()

View File

@ -1,11 +1,9 @@
package space.kscience.visionforge package space.kscience.visionforge
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import space.kscience.dataforge.meta.* import space.kscience.dataforge.meta.*
import space.kscience.dataforge.meta.descriptors.MetaDescriptor import space.kscience.dataforge.meta.descriptors.MetaDescriptor
@ -233,15 +231,12 @@ public abstract class AbstractVisionProperties(
} }
@Transient @Transient
protected val changesInternal: MutableSharedFlow<Name> = MutableSharedFlow<Name>() protected val changesInternal: MutableSharedFlow<Name> = MutableSharedFlow(500, onBufferOverflow = BufferOverflow.DROP_OLDEST)
override val changes: SharedFlow<Name> get() = changesInternal override val changes: SharedFlow<Name> get() = changesInternal
override fun invalidate(propertyName: Name) { override fun invalidate(propertyName: Name) {
//send update signal //send update signal
@OptIn(DelicateCoroutinesApi::class) changesInternal.tryEmit(propertyName)
(vision.manager?.context ?: GlobalScope).launch {
changesInternal.emit(propertyName)
}
//notify children if there are any //notify children if there are any
if (vision is VisionGroup) { if (vision is VisionGroup) {
@ -280,8 +275,8 @@ public operator fun VisionProperties.get(
/** /**
* The root property node with given inheritance and style flags * The root property node with given inheritance and style flags
* @param inherit - inherit properties from the [Vision] parent. If null, infer from descriptor * @param inherit inherit properties from the [Vision] parent. If null, infer from descriptor
* @param includeStyles - include style information. If null, infer from descriptor * @param includeStyles include style information. If null, infer from descriptor
*/ */
public fun MutableVisionProperties.root( public fun MutableVisionProperties.root(
inherit: Boolean? = null, inherit: Boolean? = null,
@ -297,22 +292,3 @@ public operator fun MutableVisionProperties.get(
inherit: Boolean? = null, inherit: Boolean? = null,
includeStyles: Boolean? = null, includeStyles: Boolean? = null,
): MutableMeta = get(name.parseAsName(), inherit, includeStyles) ): MutableMeta = get(name.parseAsName(), inherit, includeStyles)
//
//public operator fun MutableVisionProperties.set(name: Name, value: Number): Unit =
// setValue(name, value.asValue())
//
//public operator fun MutableVisionProperties.set(name: String, value: Number): Unit =
// set(name.parseAsName(), value)
//
//public operator fun MutableVisionProperties.set(name: Name, value: Boolean): Unit =
// setValue(name, value.asValue())
//
//public operator fun MutableVisionProperties.set(name: String, value: Boolean): Unit =
// set(name.parseAsName(), value)
//
//public operator fun MutableVisionProperties.set(name: Name, value: String): Unit =
// setValue(name, value.asValue())
//
//public operator fun MutableVisionProperties.set(name: String, value: String): Unit =
// set(name.parseAsName(), value)