Scoop API

scoop:scoop.ui.component:0.1.0

src/carousel/builder.scoop

Selected production source · src/carousel/builder.scoop

1package scoop.ui.component
3import scoop.surface.*
4import scoop.ui.*
5import scoop.ui.style.*
7public class CarouselItemBuilder internal constructor() {
8 public var size: CarouselItemSize = CarouselItemSize.Fraction(1.0)
9 public var style: StyleOverride = styleOverride()
12public class CarouselViewportBuilder internal constructor() {
13 public var padding: Double = 4.0
14 public var focus: FocusOptions = FocusOptions()
15 public var style: StyleOverride = styleOverride()
16 internal fun snapshot(): CarouselViewportOptions =
17 CarouselViewportOptions(padding, focus, style)
20public class CarouselButtonBuilder internal constructor() {
21 public var visible: Boolean = true
22 public var focus: FocusOptions = FocusOptions()
23 public var style: ButtonStyle = buttonStyle()
24 public var content: View? = None
25 public var semantics: ComponentSemantics = ComponentSemantics()
26 internal fun snapshot(): CarouselButtonOptions =
27 CarouselButtonOptions(visible, focus, style, content, semantics)
30public class CarouselBuilder internal constructor() {
31 public var selected: Binding<Long>? = None
32 public var initialIndex: Long = 0L
33 public var itemSize: (Long) -> CarouselItemSize = { _ -> CarouselItemSize.Fraction(1.0) }
34 public var orientation: Orientation = Orientation.Horizontal
35 public var loop: Boolean = false
36 public var enabled: Boolean = true
37 public var nativeSwipe: Boolean = true
38 public var semantics: ComponentSemantics = ComponentSemantics()
39 public var length: Double? = None
40 public var spacing: Double = 16.0
41 public var dragThreshold: Double = 4.0
42 public var projectionSeconds: Double = 0.15
43 public var autoplay: CarouselAutoplay? = None
44 public var handle: CarouselHandle? = None
45 public var viewport: CarouselViewportOptions = CarouselViewportOptions()
46 public var previous: CarouselButtonOptions = CarouselButtonOptions()
47 public var next: CarouselButtonOptions = CarouselButtonOptions()
48 public var theme: ThemeSource? = None
49 public var style: StyleOverride = styleOverride()
50 public var transition: StyleValue<AnimationSpec?> = StyleValue.Inherit
51 public var onSelect: (Long, ViewKey) -> Unit = { _, _ -> Unit }
52 public var onDragCancel: (DragCancelReason) -> Unit = {}
53 public var key: ViewKey = ViewKey.Position
54 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(_) -> itemSize
92 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 semantics
118 )
119 }
122internal class CarouselEntry(
123 val key: ViewKey,
124 val content: View,
125 val size: CarouselItemSize,
126 val style: StyleOverride
129private class CarouselItems(private val values: Array<CarouselEntry>): ItemSource {
130 public override val count: Long get() = values.size
131 public override fun keyAt(index: Long): ViewKey = values[index].key
132 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 None
137 }
138 public override fun build(index: Long): View {
139 val entry = values[index]
140 return frame(entry.content) { style = entry.style }
141 }
144public fun carousel(source: ItemSource, configure: CarouselBuilder.() -> Unit = {}): View =
145 makeCarousel(Some(source), configure)
147public fun carousel(configure: CarouselBuilder.() -> Unit): View = makeCarousel(None, configure)
149internal 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.key
156 )