scoop:scoop.ui:0.1.0
src/application/component.scoop
Selected production source · src/application/component.scoop
1
package scoop.ui3
/** A persistent application root without layout, fonts, or a native window. */4
public abstract class ApplicationComponent protected constructor(5
public val scope: ApplicationScope6
) {7
private val ownership = InstanceOwnership(scope)8
internal fun claim(scope: ApplicationScope) {9
ownership.claim(scope)10
}11
public abstract fun render(): ApplicationContent12
public open fun onMount() {}13
public open fun onUnmount() {}14
}16
/** Native application attachments use the ordinary mount and Props lifetime. */17
public 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() {}25
}27
public class ApplicationContent public constructor(public val items: Array<ApplicationItem>)29
public class ApplicationItem internal constructor(30
internal val key: ViewKey,31
internal val description: ApplicationElementDescription32
)34
public fun <E: ApplicationElement> applicationElement(35
create: (ApplicationScope) -> E,36
key: ViewKey,37
comparison: InputComparison<Unit>? = None38
): ApplicationItem = applicationElement(Unit, { _, scope -> create(scope) }, key, comparison)40
public fun <P, E: ApplicationElement> applicationElement(41
props: P,42
create: (Props<P>, ApplicationScope) -> E,43
key: ViewKey,44
comparison: InputComparison<P>? = None45
): ApplicationItem = ApplicationItem(key, TypedApplicationDescription(props, create, comparison))47
internal interface ApplicationElementDescription {48
fun sameType(other: ApplicationElementDescription): Boolean49
fun create(scope: ApplicationScope): ApplicationElementInstance50
}52
internal interface ApplicationElementInstance {53
val value: ApplicationElement54
fun replace(description: ApplicationElementDescription): Boolean55
}57
private 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
}70
}72
private class TypedApplicationInstance<P, E: ApplicationElement>(73
private val props: Props<P>,74
private val instance: E75
): ApplicationElementInstance {76
override val value: ApplicationElement get() = instance77
override fun replace(description: ApplicationElementDescription): Boolean {78
val next = description as TypedApplicationDescription<P, E>79
return props.replace(next.input, next.comparison)80
}81
}