Compare commits

...

5 Commits

Author SHA1 Message Date
f238b03833 A dedicated builder for ComposeHtmlVisionRenderer 2024-05-23 19:36:13 +03:00
5038829288 Merge remote-tracking branch 'github/master' into dev 2024-03-15 13:13:01 +03:00
SPC-code
8e5503f0d8
Merge pull request #76 from SciProgCentre/dev
0.3.0-dev-17
2023-12-05 16:47:35 +03:00
Alexander Nozik
fb12ca8902
Delete kotlin-js-store directory 2022-04-13 17:07:15 +03:00
Alexander Nozik
c6258e72fb
Merge pull request #71 from mipt-npm/dev
0.2.0
2022-01-24 09:44:18 +03:00
3 changed files with 31 additions and 17 deletions

View File

@ -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 {

View File

@ -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
@ -26,3 +27,23 @@ public interface ComposeHtmlVisionRenderer : ElementVisionRenderer {
public companion object
}
/**
* A compose-html renderer for a vision of given type
*/
public inline fun <reified T : Vision> ComposeHtmlVisionRenderer(
acceptRating: Int = ElementVisionRenderer.DEFAULT_RATING,
noinline renderFunction: @Composable DOMScope<Element>.(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<Element>.render(name: Name, vision: Vision, meta: Meta) {
renderFunction(name, T::class.cast(vision), meta)
}
override fun toString(): String = "ElementVisionRender(${T::class.simpleName})"
}

View File

@ -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<T : Vision>(
public val kClass: KClass<T>,
private val acceptRating: Int = ElementVisionRenderer.DEFAULT_RATING,
private val renderFunction: TagConsumer<HTMLElement>.(name: Name, vision: T, meta: Meta) -> Unit,
) : ElementVisionRenderer {
public inline fun <reified T : Vision> ElementVisionRenderer(
acceptRating: Int = ElementVisionRenderer.DEFAULT_RATING,
noinline renderFunction: TagConsumer<HTMLElement>.(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<T : Vision>(
) {
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 <reified T : Vision> ElementVisionRenderer(
acceptRating: Int = ElementVisionRenderer.DEFAULT_RATING,
noinline renderFunction: TagConsumer<HTMLElement>.(name: Name, vision: T, meta: Meta) -> Unit,
): ElementVisionRenderer = SingleTypeVisionRenderer(T::class, acceptRating, renderFunction)