scoop:scoop.ui.component:0.1.0
src/carousel/builder.scoop
Selected production source · src/carousel/builder.scoop
1
package scoop.ui.component3
import scoop.surface.*4
import scoop.ui.*5
import scoop.ui.style.*7
public class CarouselItemBuilder internal constructor() {8
public var size: CarouselItemSize = CarouselItemSize.Fraction(1.0)9
public var style: StyleOverride = styleOverride()10
}12
public class CarouselViewportBuilder internal constructor() {13
public var padding: Double = 4.014
public var focus: FocusOptions = FocusOptions()15
public var style: StyleOverride = styleOverride()16
internal fun snapshot(): CarouselViewportOptions =17
CarouselViewportOptions(padding, focus, style)18
}20
public class CarouselButtonBuilder internal constructor() {21
public var visible: Boolean = true22
public var focus: FocusOptions = FocusOptions()23
public var style: ButtonStyle = buttonStyle()24
public var content: View? = None25
public var semantics: ComponentSemantics = ComponentSemantics()26
internal fun snapshot(): CarouselButtonOptions =27
CarouselButtonOptions(visible, focus, style, content, semantics)28
}30
public class CarouselBuilder internal constructor() {31
public var selected: Binding<Long>? = None32
public var initialIndex: Long = 0L33
public var itemSize: (Long) -> CarouselItemSize = { _ -> CarouselItemSize.Fraction(1.0) }34
public var orientation: Orientation = Orientation.Horizontal35
public var loop: Boolean = false36
public var enabled: Boolean = true37
public var nativeSwipe: Boolean = true38
public var semantics: ComponentSemantics = ComponentSemantics()39
public var length: Double? = None40
public var spacing: Double = 16.041
public var dragThreshold: Double = 4.042
public var projectionSeconds: Double = 0.1543
public var autoplay: CarouselAutoplay? = None44
public var handle: CarouselHandle? = None45
public var viewport: CarouselViewportOptions = CarouselViewportOptions()46
public var previous: CarouselButtonOptions = CarouselButtonOptions()47
public var next: CarouselButtonOptions = CarouselButtonOptions()48
public var theme: ThemeSource? = None49
public var style: StyleOverride = styleOverride()50
public var transition: StyleValue<AnimationSpec?> = StyleValue.Inherit51
public var onSelect: (Long, ViewKey) -> Unit = { _, _ -> Unit }52
public var onDragCancel: (DragCancelReason) -> Unit = {}53
public var key: ViewKey = ViewKey.Position54
private val entries = ArrayList<CarouselEntry>()55
public fun item(key: ViewKey, content: View, configure: CarouselItemBuilder.() -> Unit = {}) {56
val builder = CarouselItemBuilder();57
builder.configure()58
carouselExtent(builder.size, 1.0, 0.0)59
entries.add(CarouselEntry(key, content, builder.size, builder.style))60
}61
public fun viewport(configure: CarouselViewportBuilder.() -> Unit) {62
val builder = CarouselViewportBuilder();63
builder.configure();64
viewport = builder.snapshot()65
}66
public fun previous(configure: CarouselButtonBuilder.() -> Unit) {67
val builder = CarouselButtonBuilder();68
builder.configure();69
previous = builder.snapshot()70
}71
public fun next(configure: CarouselButtonBuilder.() -> Unit) {72
val builder = CarouselButtonBuilder();73
builder.configure();74
next = builder.snapshot()75
}76
public fun transition(value: AnimationSpec?) {77
transition = StyleValue.Set(value)78
}79
internal fun snapshot(source: ItemSource?): CarouselProps {80
val published = entries.toArray()81
val resolved = source ?: CarouselItems(published)82
when (source) {83
case Some(_) -> requireComponent(84
entries.size == 0L,85
"carousel cannot combine source and item entries"86
)87
case None -> {}88
}89
val sizes: (Long) -> CarouselItemSize =90
when (source) {91
case Some(_) -> itemSize92
case None -> ({ index -> published[index].size })93
}94
return CarouselProps(95
resolved,96
selected,97
initialIndex,98
sizes,99
orientation,100
loop,101
enabled,102
length,103
spacing,104
dragThreshold,105
projectionSeconds,106
autoplay,107
handle,108
viewport,109
previous,110
next,111
theme,112
style,113
transition,114
onSelect,115
onDragCancel,116
nativeSwipe,117
semantics118
)119
}120
}122
internal class CarouselEntry(123
val key: ViewKey,124
val content: View,125
val size: CarouselItemSize,126
val style: StyleOverride127
)129
private class CarouselItems(private val values: Array<CarouselEntry>): ItemSource {130
public override val count: Long get() = values.size131
public override fun keyAt(index: Long): ViewKey = values[index].key132
public override fun indexOfKey(key: ViewKey): Long? {133
for (index in 0L ..< count)134
if (values[index].key == key)135
return Some(index)136
return None137
}138
public override fun build(index: Long): View {139
val entry = values[index]140
return frame(entry.content) { style = entry.style }141
}142
}144
public fun carousel(source: ItemSource, configure: CarouselBuilder.() -> Unit = {}): View =145
makeCarousel(Some(source), configure)147
public fun carousel(configure: CarouselBuilder.() -> Unit): View = makeCarousel(None, configure)149
internal fun makeCarousel(source: ItemSource?, configure: CarouselBuilder.() -> Unit): View {150
val builder = CarouselBuilder();151
builder.configure()152
return component(153
builder.snapshot(source),154
{ props, mount -> Carousel(props, scope = mount) },155
builder.key156
)157
}