From f238b038332fbc7ee226523fb528e132a78f6f12 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 23 May 2024 19:36:13 +0300 Subject: [PATCH] A dedicated builder for ComposeHtmlVisionRenderer --- build.gradle.kts | 2 +- .../html/ComposeHtmlVisionRenderer.kt | 23 ++++++++++++++++++- .../visionforge/html/ElementVisionRenderer.kt | 23 +++++++------------ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index eb7e4fe4..cb29ac4c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ val dataforgeVersion by extra("0.8.0") allprojects { group = "space.kscience" - version = "0.4.1" + version = "0.4.2-dev-1" } subprojects { diff --git a/visionforge-compose-html/src/jsMain/kotlin/space/kscience/visionforge/html/ComposeHtmlVisionRenderer.kt b/visionforge-compose-html/src/jsMain/kotlin/space/kscience/visionforge/html/ComposeHtmlVisionRenderer.kt index 5a54292d..c0642765 100644 --- a/visionforge-compose-html/src/jsMain/kotlin/space/kscience/visionforge/html/ComposeHtmlVisionRenderer.kt +++ b/visionforge-compose-html/src/jsMain/kotlin/space/kscience/visionforge/html/ComposeHtmlVisionRenderer.kt @@ -8,6 +8,7 @@ import org.w3c.dom.Element import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.names.Name import space.kscience.visionforge.Vision +import kotlin.reflect.cast /** * An [ElementVisionRenderer] that could be used directly in Compose-html as well as a stand-alone renderer @@ -25,4 +26,24 @@ public interface ComposeHtmlVisionRenderer : ElementVisionRenderer { } public companion object -} \ No newline at end of file +} + + +/** + * A compose-html renderer for a vision of given type + */ +public inline fun ComposeHtmlVisionRenderer( + acceptRating: Int = ElementVisionRenderer.DEFAULT_RATING, + noinline renderFunction: @Composable DOMScope.(name: Name, vision: T, meta: Meta) -> Unit, +): ComposeHtmlVisionRenderer = object : ComposeHtmlVisionRenderer { + + override fun rateVision(vision: Vision): Int = + if (vision::class == T::class) acceptRating else ElementVisionRenderer.ZERO_RATING + + @Composable + override fun DOMScope.render(name: Name, vision: Vision, meta: Meta) { + renderFunction(name, T::class.cast(vision), meta) + } + + override fun toString(): String = "ElementVisionRender(${T::class.simpleName})" +} diff --git a/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/html/ElementVisionRenderer.kt b/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/html/ElementVisionRenderer.kt index 57d3e4e1..a18cbb9f 100644 --- a/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/html/ElementVisionRenderer.kt +++ b/visionforge-core/src/jsMain/kotlin/space/kscience/visionforge/html/ElementVisionRenderer.kt @@ -9,7 +9,6 @@ import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.misc.DfType import space.kscience.dataforge.names.Name import space.kscience.visionforge.Vision -import kotlin.reflect.KClass import kotlin.reflect.cast /** @@ -46,16 +45,15 @@ public interface ElementVisionRenderer { } /** - * A browser renderer for element of given type + * A browser renderer for a vision of given type */ -public class SingleTypeVisionRenderer( - public val kClass: KClass, - private val acceptRating: Int = ElementVisionRenderer.DEFAULT_RATING, - private val renderFunction: TagConsumer.(name: Name, vision: T, meta: Meta) -> Unit, -) : ElementVisionRenderer { +public inline fun ElementVisionRenderer( + acceptRating: Int = ElementVisionRenderer.DEFAULT_RATING, + noinline renderFunction: TagConsumer.(name: Name, vision: T, meta: Meta) -> Unit, +): ElementVisionRenderer = object : ElementVisionRenderer { override fun rateVision(vision: Vision): Int = - if (vision::class == kClass) acceptRating else ElementVisionRenderer.ZERO_RATING + if (vision::class == T::class) acceptRating else ElementVisionRenderer.ZERO_RATING override fun render( element: Element, @@ -65,14 +63,9 @@ public class SingleTypeVisionRenderer( ) { element.clear() element.append { - renderFunction(name, kClass.cast(vision), meta) + renderFunction(name, T::class.cast(vision), meta) } } - override fun toString(): String = "ElementVisionRender(${kClass.simpleName})" + override fun toString(): String = "ElementVisionRender(${T::class.simpleName})" } - -public inline fun ElementVisionRenderer( - acceptRating: Int = ElementVisionRenderer.DEFAULT_RATING, - noinline renderFunction: TagConsumer.(name: Name, vision: T, meta: Meta) -> Unit, -): ElementVisionRenderer = SingleTypeVisionRenderer(T::class, acceptRating, renderFunction)