Scoop API

scoop:scoop.ui:0.1.0

src/application/component.scoop

Selected production source · src/application/component.scoop

1package scoop.ui
3/** A persistent application root without layout, fonts, or a native window. */
4public abstract class ApplicationComponent protected constructor(
5 public val scope: ApplicationScope
6) {
7 private val ownership = InstanceOwnership(scope)
8 internal fun claim(scope: ApplicationScope) {
9 ownership.claim(scope)
10 }
11 public abstract fun render(): ApplicationContent
12 public open fun onMount() {}
13 public open fun onUnmount() {}
16/** Native application attachments use the ordinary mount and Props lifetime. */
17public abstract class ApplicationElement protected constructor(public val scope: ApplicationScope) {
18 private val ownership = InstanceOwnership(scope)
19 internal fun claim(scope: ApplicationScope) {
20 ownership.claim(scope)
21 }
22 public open fun onMount() {}
23 public open fun update() {}
24 public open fun onUnmount() {}
27public class ApplicationContent public constructor(public val items: Array<ApplicationItem>)
29public class ApplicationItem internal constructor(
30 internal val key: ViewKey,
31 internal val description: ApplicationElementDescription
34public fun <E: ApplicationElement> applicationElement(
35 create: (ApplicationScope) -> E,
36 key: ViewKey,
37 comparison: InputComparison<Unit>? = None
38): ApplicationItem = applicationElement(Unit, { _, scope -> create(scope) }, key, comparison)
40public fun <P, E: ApplicationElement> applicationElement(
41 props: P,
42 create: (Props<P>, ApplicationScope) -> E,
43 key: ViewKey,
44 comparison: InputComparison<P>? = None
45): ApplicationItem = ApplicationItem(key, TypedApplicationDescription(props, create, comparison))
47internal interface ApplicationElementDescription {
48 fun sameType(other: ApplicationElementDescription): Boolean
49 fun create(scope: ApplicationScope): ApplicationElementInstance
52internal interface ApplicationElementInstance {
53 val value: ApplicationElement
54 fun replace(description: ApplicationElementDescription): Boolean
57private class TypedApplicationDescription<P, E: ApplicationElement>(
58 val input: P,
59 private val factory: (Props<P>, ApplicationScope) -> E,
60 val comparison: InputComparison<P>?
61): ApplicationElementDescription {
62 override fun sameType(other: ApplicationElementDescription): Boolean =
63 other is TypedApplicationDescription<P, E>
64 override fun create(scope: ApplicationScope): ApplicationElementInstance {
65 val props = Props(input, comparison)
66 val instance = factory(props, scope)
67 instance.claim(scope)
68 return TypedApplicationInstance<P, E>(props, instance)
69 }
72private class TypedApplicationInstance<P, E: ApplicationElement>(
73 private val props: Props<P>,
74 private val instance: E
75): ApplicationElementInstance {
76 override val value: ApplicationElement get() = instance
77 override fun replace(description: ApplicationElementDescription): Boolean {
78 val next = description as TypedApplicationDescription<P, E>
79 return props.replace(next.input, next.comparison)
80 }