Scoop API

scoop:scoop.ui:0.1.0

src/view/view.scoop

Selected production source · src/view/view.scoop

1package scoop.ui
3/** Identity is local to the immediate parent's complete child sequence. */
4public enum ViewKey: Equals<ViewKey>, Hash {
5 Position, Text(String), Number(Long)
6 public override operator fun equals(other: ViewKey): Boolean =
7 when ((this, other)) {
8 case(Position, Position) -> true
9 case(Text(val a), Text(val b)) -> a == b
10 case(Number(val a), Number(val b)) -> a == b
11 else -> false
12 }
13 public override fun hash(): Long =
14 when (this) {
15 case Position -> 0L
16 case Text(val value) -> value.hash()
17 case Number(val value) -> value.hash()
18 }
21/** Immutable description; constructing or copying it never mounts an instance. */
22public class View internal constructor(internal val key: ViewKey, internal val body: ViewBody)
24public fun empty(): View = View(ViewKey.Position, ViewBody.Empty)
26/** Keeps logical ownership while presenting one child in the window's layer outlet. */
27public fun portal(
28 open: Binding<Boolean>,
29 content: View,
30 configure: PortalBuilder.() -> Unit = {}
31): View {
32 val builder = PortalBuilder()
33 builder.configure()
34 return View(builder.key, ViewBody.Portal(content, open, builder.snapshot()))
37/** Factories retain a concrete class type and create a fresh instance for each new mount. */
38public fun <C: Component> component(
39 create: (ComponentScope) -> C,
40 key: ViewKey = ViewKey.Position,
41 comparison: InputComparison<Unit>? = None
42): View = component(Unit, { _, scope -> create(scope) }, key, comparison)
44public fun <P, C: Component> component(
45 props: P,
46 create: (Props<P>, ComponentScope) -> C,
47 key: ViewKey = ViewKey.Position,
48 comparison: InputComparison<P>? = None
49): View = View(key, ViewBody.Component(ComponentDescription(props, create, comparison)))
51/** Factories retain a concrete class type and create a fresh instance for each new mount. */
52public fun <L, E: Element<L>> element(
53 create: (ComponentScope) -> E,
54 children: Array<View> = [],
55 key: ViewKey = ViewKey.Position,
56 comparison: InputComparison<Unit>? = None
57): View = element(Unit, { _, scope -> create(scope) }, children, key, comparison)
59public fun <P, L, E: Element<L>> element(
60 props: P,
61 create: (Props<P>, ComponentScope) -> E,
62 children: Array<View> = [],
63 key: ViewKey = ViewKey.Position,
64 comparison: InputComparison<P>? = None
65): View = View(key, ViewBody.Element(ElementDescription(props, create, comparison), children))
67internal enum ViewBody {
68 Empty, Component(ComponentDescriptionBase), Element(ElementDescriptionBase, Array<View>),
69 Portal(View, Binding<Boolean>, PortalOptions), Lazy(
70 LazyDescriptionBase,
71 ItemSource,
72 Long
73 ), Direction(View, LayoutDirection)
76/** Type matching has no factory, input replacement, or mounting side effects. */
77internal fun View.sameType(other: View): Boolean =
78 when ((body, other.body)) {
79 case(Empty, Empty) -> true
80 case(Direction(_, _), Direction(_, _)) -> true
81 case(Component(val a), Component(val b)) -> a.sameType(b)
82 case(Element(val a, _), Element(val b, _)) -> a.sameType(b)
83 case(Lazy(val a, _, _), Lazy(val b, _, _)) -> a.sameType(b)
84 case(Portal(_, _, _), Portal(_, _, _)) -> true
85 else -> false
86 }
88/** Factories retain a concrete class type and create a fresh instance for each new mount. */
89public fun <L, E: LazyElement<L>> lazyElement(
90 source: ItemSource,
91 create: (ComponentScope) -> E,
92 retention: ItemRetention = ItemRetention.Dispose,
93 key: ViewKey = ViewKey.Position,
94 comparison: InputComparison<Unit>? = None
95): View = lazyElement(source, Unit, { _, scope -> create(scope) }, retention, key, comparison)
97public fun <P, L, E: LazyElement<L>> lazyElement(
98 source: ItemSource,
99 props: P,
100 create: (Props<P>, ComponentScope) -> E,
101 retention: ItemRetention = ItemRetention.Dispose,
102 key: ViewKey = ViewKey.Position,
103 comparison: InputComparison<P>? = None
104): View =
105 View(
106 key,
107 ViewBody.Lazy(LazyDescription(props, create, comparison), source, retentionLimit(retention))
108 )