Scoop API

scoop:scoop.ui:0.1.0

src/view/descriptions.scoop

Selected production source · src/view/descriptions.scoop

1package scoop.ui
3internal interface ComponentDescriptionBase {
4 fun sameType(other: ComponentDescriptionBase): Boolean
5 fun create(scope: ComponentScope): ComponentInstance
6}
8internal interface ElementDescriptionBase {
9 fun sameType(other: ElementDescriptionBase): Boolean
10 fun create(scope: ComponentScope): ElementInstance
13internal interface ComponentInstance {
14 val value: Component
15 fun replace(description: ComponentDescriptionBase): Boolean
18internal interface ElementInstance {
19 val value: Any
20 fun measure(context: MeasureContext, constraints: Constraints): ElementLayout
21 fun onMount()
22 fun update(): UiInvalidation
23 fun onUnmount()
24 fun replace(description: ElementDescriptionBase): Boolean
27internal interface LazyDescriptionBase {
28 fun sameType(other: LazyDescriptionBase): Boolean
29 fun create(scope: ComponentScope): LazyInstance
32internal interface LazyInstance {
33 val value: Any
34 fun measure(context: LazyMeasureContext, constraints: Constraints): ElementLayout
35 fun onMount()
36 fun update(): UiInvalidation
37 fun onUnmount()
38 fun replace(description: LazyDescriptionBase): Boolean
41internal class LazyDescription<P, L, E: LazyElement<L>>(
42 val input: P,
43 private val factory: (Props<P>, ComponentScope) -> E,
44 val comparison: InputComparison<P>? = None
45): LazyDescriptionBase {
46 override fun sameType(other: LazyDescriptionBase): Boolean = other is LazyDescription<P, L, E>
47 override fun create(scope: ComponentScope): LazyInstance {
48 val props = Props(input, comparison)
49 val instance = factory(props, scope)
50 instance.claim(scope)
51 return TypedLazyInstance<P, L, E>(props, instance)
52 }
55private class TypedLazyInstance<P, L, E: LazyElement<L>>(
56 private val props: Props<P>,
57 private val instance: E
58): LazyInstance {
59 override val value: Any get() = instance
60 override fun measure(context: LazyMeasureContext, constraints: Constraints): ElementLayout =
61 TypedElementLayout(
62 instance.measure(context, constraints),
63 { scene, layout -> instance.scene(scene, layout) }
64 )
65 override fun onMount() {
66 instance.onMount()
67 }
68 override fun update(): UiInvalidation = instance.update()
69 override fun onUnmount() {
70 instance.onUnmount()
71 }
72 override fun replace(description: LazyDescriptionBase): Boolean {
73 val next = description as LazyDescription<P, L, E>
74 return props.replace(next.input, next.comparison)
75 }
78internal class ComponentDescription<P, C: Component>(
79 val input: P,
80 private val factory: (Props<P>, ComponentScope) -> C,
81 val comparison: InputComparison<P>? = None
82): ComponentDescriptionBase {
83 override fun sameType(other: ComponentDescriptionBase): Boolean =
84 other is ComponentDescription<P, C>
85 override fun create(scope: ComponentScope): ComponentInstance {
86 val props = Props(input, comparison)
87 val instance = factory(props, scope)
88 instance.claim(scope)
89 return TypedComponentInstance<P, C>(props, instance)
90 }
93private class TypedComponentInstance<P, C: Component>(
94 private val props: Props<P>,
95 private val instance: C
96): ComponentInstance {
97 override val value: Component get() = instance
98 override fun replace(description: ComponentDescriptionBase): Boolean {
99 val next = description as ComponentDescription<P, C>
100 return props.replace(next.input, next.comparison)
101 }
104internal class ElementDescription<P, L, E: Element<L>>(
105 val input: P,
106 private val factory: (Props<P>, ComponentScope) -> E,
107 val comparison: InputComparison<P>? = None
108): ElementDescriptionBase {
109 override fun sameType(other: ElementDescriptionBase): Boolean =
110 other is ElementDescription<P, L, E>
111 override fun create(scope: ComponentScope): ElementInstance {
112 val props = Props(input, comparison)
113 val instance = factory(props, scope)
114 instance.claim(scope)
115 return TypedElementInstance<P, L, E>(props, instance)
116 }
119private class TypedElementInstance<P, L, E: Element<L>>(
120 private val props: Props<P>,
121 private val instance: E
122): ElementInstance {
123 override val value: Any get() = instance
124 override fun measure(context: MeasureContext, constraints: Constraints): ElementLayout =
125 TypedElementLayout(
126 instance.measure(context, constraints),
127 { scene, layout -> instance.scene(scene, layout) }
128 )
129 override fun onMount() {
130 instance.onMount()
131 }
132 override fun update(): UiInvalidation = instance.update()
133 override fun onUnmount() {
134 instance.onUnmount()
135 }
136 override fun replace(description: ElementDescriptionBase): Boolean {
137 val next = description as ElementDescription<P, L, E>
138 return props.replace(next.input, next.comparison)
139 }