scoop:scoop.ui:0.1.0
src/component.scoop
Selected production source · src/component.scoop
1
package scoop.ui3
public enum UiInvalidation {4
None, Scene, Layout5
}7
/** A persistent instance that composes descriptions from its current inputs and state. */8
public abstract class Component protected constructor(public val scope: ComponentScope) {9
private val ownership = InstanceOwnership(scope)10
internal fun claim(scope: ComponentScope) {11
ownership.claim(scope)12
}13
public abstract fun render(): View14
public open fun onMount() {}15
public open fun onUnmount() {}16
}18
/** A persistent, self-drawn leaf or container using the same public child protocol. */19
public abstract class Element<L> protected constructor(public val scope: ComponentScope) {20
private val ownership = InstanceOwnership(scope)21
internal fun claim(scope: ComponentScope) {22
ownership.claim(scope)23
}24
public abstract fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<L>25
public abstract fun scene(context: SceneContext, layout: L)26
public open fun onMount() {}27
public open fun update(): UiInvalidation = UiInvalidation.Layout28
public open fun onUnmount() {}29
}31
/** A persistent container that asks the framework for only the items it needs. */32
public abstract class LazyElement<L> protected constructor(public val scope: ComponentScope) {33
private val ownership = InstanceOwnership(scope)34
internal fun claim(scope: ComponentScope) {35
ownership.claim(scope)36
}37
public abstract fun measure(38
context: LazyMeasureContext,39
constraints: Constraints40
): LayoutResult<L>41
public abstract fun scene(context: SceneContext, layout: L)42
public open fun onMount() {}43
public open fun update(): UiInvalidation = UiInvalidation.Layout44
public open fun onUnmount() {}45
}47
/** Replace this snapshot when its key mapping or item inputs change. */48
public interface ItemSource {49
public val count: Long50
public fun keyAt(index: Long): ViewKey51
public fun indexOfKey(key: ViewKey): Long?52
public fun build(index: Long): View53
}55
/** Ownership is checked on the actual user instance, including after it has retired. */56
internal class InstanceOwnership(private val scope: Any) {57
private var claimed = false58
fun claim(current: Any) {59
if (claimed || scope !== current)60
throw IllegalStateException(61
Some("component or element instance belongs to another mount")62
)63
claimed = true64
}65
}