Skip to content

Commit

Permalink
Add click handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
altavir committed Nov 24, 2023
1 parent e6bdb67 commit 80284a9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package space.kscience.visionforge

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.MetaRepr
import space.kscience.dataforge.meta.MutableMeta

@Serializable
@SerialName("control")
public abstract class VisionControlEvent : VisionEvent, MetaRepr {
public abstract val meta: Meta

override fun toMeta(): Meta = meta
}

public interface ControlVision : Vision {
public val controlEventFlow: Flow<VisionControlEvent>

public fun dispatchControlEvent(event: VisionControlEvent)

override fun receiveEvent(event: VisionEvent) {
if (event is VisionControlEvent) {
dispatchControlEvent(event)
} else super.receiveEvent(event)
}
}

@Serializable
@SerialName("control.click")
public class VisionClickEvent(override val meta: Meta) : VisionControlEvent()


public interface ClickControl : ControlVision {
public fun click(builder: MutableMeta.() -> Unit = {}) {
dispatchControlEvent(VisionClickEvent(Meta(builder)))
}

public fun onClick(scope: CoroutineScope, block: suspend VisionClickEvent.() -> Unit): Job {
return controlEventFlow.filterIsInstance<VisionClickEvent>().onEach(block).launchIn(scope)
}

public companion object {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.context.logger
import space.kscience.dataforge.context.warn
import space.kscience.dataforge.meta.asValue
import space.kscience.dataforge.meta.boolean
import space.kscience.dataforge.meta.descriptors.Described
Expand Down Expand Up @@ -37,7 +38,7 @@ public interface Vision : Described {
/**
* Update this vision using a dif represented by [VisionChange].
*/
public fun receiveChange(change: VisionChange) {
public fun update(change: VisionChange) {
if (change.children?.isNotEmpty() == true) {
error("Vision is not a group")
}
Expand All @@ -46,18 +47,12 @@ public interface Vision : Described {
}
}

public fun onMetaEvent(meta: Meta){
//Do nothing by default
}

/**
* Receive and process a generic [VisionEvent].
*/
public fun receiveEvent(event: VisionEvent) {
when (event) {
is VisionChange -> receiveChange(event)
is VisionMetaEvent -> onMetaEvent(event.meta)
}
if(event is VisionChange) update(event)
else manager?.logger?.warn { "Undispatched event: $event" }
}

override val descriptor: MetaDescriptor?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package space.kscience.visionforge
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.MutableMeta
import space.kscience.dataforge.meta.set
import space.kscience.dataforge.names.Name

/**
Expand All @@ -22,14 +20,4 @@ public sealed interface VisionEvent {
*/
@Serializable
@SerialName("meta")
public class VisionMetaEvent(public val meta: Meta) : VisionEvent


public val Vision.Companion.CLICK_EVENT_KEY: Name get() = Name.of("events", "click", "payload")

/**
* Set the payload to be sent to server on click
*/
public fun Vision.onClickPayload(payloadBuilder: MutableMeta.() -> Unit) {
properties[VisionEvent.CLICK_EVENT_KEY] = Meta(payloadBuilder)
}
public class VisionMetaEvent(public val meta: Meta) : VisionEvent
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import space.kscience.visionforge.Vision.Companion.STYLE_KEY
public interface VisionGroup : Vision {
public val children: VisionChildren

override fun receiveChange(change: VisionChange) {
override fun update(change: VisionChange) {
change.children?.forEach { (name, change) ->
if (change.vision != null || change.vision == NullVision) {
error("VisionGroup is read-only")
} else {
children.getChild(name)?.receiveChange(change)
children.getChild(name)?.update(change)
}
}
change.properties?.let {
Expand All @@ -37,12 +37,12 @@ public interface MutableVisionGroup : VisionGroup {

public fun createGroup(): MutableVisionGroup

override fun receiveChange(change: VisionChange) {
override fun update(change: VisionChange) {
change.children?.forEach { (name, change) ->
when {
change.vision == NullVision -> children.setChild(name, null)
change.vision != null -> children.setChild(name, change.vision)
else -> children.getChild(name)?.receiveChange(change)
else -> children.getChild(name)?.update(change)
}
}
change.properties?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class VisionUpdateTest {
propertyChanged("top".asName(), SolidMaterial.MATERIAL_COLOR_KEY, Meta("red".asValue()))
propertyChanged("origin".asName(), SolidMaterial.MATERIAL_COLOR_KEY, Meta("red".asValue()))
}
targetVision.receiveChange(dif)
targetVision.update(dif)
assertTrue { targetVision.children.getChild("top") is SolidGroup }
assertEquals("red", (targetVision.children.getChild("origin") as Solid).color.string) // Should work
assertEquals(
Expand Down

0 comments on commit 80284a9

Please sign in to comment.