scoop:scoop.ui:0.1.0
src/view/view.scoop
Selected production source · src/view/view.scoop
1
package scoop.ui3
/** Identity is local to the immediate parent's complete child sequence. */4
public 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) -> true9
case(Text(val a), Text(val b)) -> a == b10
case(Number(val a), Number(val b)) -> a == b11
else -> false12
}13
public override fun hash(): Long =14
when (this) {15
case Position -> 0L16
case Text(val value) -> value.hash()17
case Number(val value) -> value.hash()18
}19
}21
/** Immutable description; constructing or copying it never mounts an instance. */22
public class View internal constructor(internal val key: ViewKey, internal val body: ViewBody)24
public fun empty(): View = View(ViewKey.Position, ViewBody.Empty)26
/** Keeps logical ownership while presenting one child in the window's layer outlet. */27
public 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()))35
}37
/** Factories retain a concrete class type and create a fresh instance for each new mount. */38
public fun <C: Component> component(39
create: (ComponentScope) -> C,40
key: ViewKey = ViewKey.Position,41
comparison: InputComparison<Unit>? = None42
): View = component(Unit, { _, scope -> create(scope) }, key, comparison)44
public fun <P, C: Component> component(45
props: P,46
create: (Props<P>, ComponentScope) -> C,47
key: ViewKey = ViewKey.Position,48
comparison: InputComparison<P>? = None49
): 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. */52
public fun <L, E: Element<L>> element(53
create: (ComponentScope) -> E,54
children: Array<View> = [],55
key: ViewKey = ViewKey.Position,56
comparison: InputComparison<Unit>? = None57
): View = element(Unit, { _, scope -> create(scope) }, children, key, comparison)59
public 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>? = None65
): View = View(key, ViewBody.Element(ElementDescription(props, create, comparison), children))67
internal enum ViewBody {68
Empty, Component(ComponentDescriptionBase), Element(ElementDescriptionBase, Array<View>),69
Portal(View, Binding<Boolean>, PortalOptions), Lazy(70
LazyDescriptionBase,71
ItemSource,72
Long73
), Direction(View, LayoutDirection)74
}76
/** Type matching has no factory, input replacement, or mounting side effects. */77
internal fun View.sameType(other: View): Boolean =78
when ((body, other.body)) {79
case(Empty, Empty) -> true80
case(Direction(_, _), Direction(_, _)) -> true81
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(_, _, _)) -> true85
else -> false86
}88
/** Factories retain a concrete class type and create a fresh instance for each new mount. */89
public 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>? = None95
): View = lazyElement(source, Unit, { _, scope -> create(scope) }, retention, key, comparison)97
public 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>? = None104
): View =105
View(106
key,107
ViewBody.Lazy(LazyDescription(props, create, comparison), source, retentionLimit(retention))108
)