Scoop API

scoop:scoop.ui:0.1.0

src/component.scoop

Selected production source · src/component.scoop

1package scoop.ui
3public enum UiInvalidation {
4 None, Scene, Layout
5}
7/** A persistent instance that composes descriptions from its current inputs and state. */
8public 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(): View
14 public open fun onMount() {}
15 public open fun onUnmount() {}
18/** A persistent, self-drawn leaf or container using the same public child protocol. */
19public 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.Layout
28 public open fun onUnmount() {}
31/** A persistent container that asks the framework for only the items it needs. */
32public 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: Constraints
40 ): LayoutResult<L>
41 public abstract fun scene(context: SceneContext, layout: L)
42 public open fun onMount() {}
43 public open fun update(): UiInvalidation = UiInvalidation.Layout
44 public open fun onUnmount() {}
47/** Replace this snapshot when its key mapping or item inputs change. */
48public interface ItemSource {
49 public val count: Long
50 public fun keyAt(index: Long): ViewKey
51 public fun indexOfKey(key: ViewKey): Long?
52 public fun build(index: Long): View
55/** Ownership is checked on the actual user instance, including after it has retired. */
56internal class InstanceOwnership(private val scope: Any) {
57 private var claimed = false
58 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 = true
64 }