Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/attachment_group.scoop

Selected production source · src/conversation/attachment_group.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.platform.time.MonotonicTimeSource
5import scoop.surface.*
6import scoop.time.Duration
7import scoop.ui.*
8import scoop.ui.style.*
9import scoop.window.*
10import scoop.ui.component.carousel as attachmentCarousel
12public class AttachmentGroupProps public constructor(
13 public val source: ItemSource,
14 public val length: Double? = None,
15 public val spacing: Double = 12.0,
16 public val enabled: Boolean = true,
17 public val handle: CarouselHandle? = None,
18 public val focus: FocusOptions = FocusOptions(),
19 public val fade: Boolean = true,
20 public val theme: ThemeSource? = None
23public class AttachmentGroupBuilder internal constructor() {
24 public var length: Double? = None
25 public var spacing: Double = 12.0
26 public var enabled: Boolean = true
27 public var handle: CarouselHandle? = None
28 public var focus: FocusOptions = FocusOptions()
29 public var fade: Boolean = true
30 public var theme: ThemeSource? = None
31 public var key: ViewKey = ViewKey.Position
32 private val entries = ArrayList<AttachmentGroupEntry>()
33 public fun item(key: ViewKey, content: View) {
34 entries.add(AttachmentGroupEntry(key, content))
35 }
36 public fun attachment(
37 key: ViewKey,
38 title: String,
39 configure: AttachmentBuilder.() -> Unit = {}
40 ) {
41 item(key, makeAttachment(conversationText(title), configure))
42 }
43 internal fun snapshot(source: ItemSource?): AttachmentGroupProps {
44 requireComponent(
45 !panelPresent(source) || entries.size == 0L,
46 "attachment group cannot mix a source and entries"
47 )
48 nonnegative(spacing, "invalid attachment group spacing")
49 return AttachmentGroupProps(
50 source ?: AttachmentGroupItems(entries.toArray()),
51 length,
52 spacing,
53 enabled,
54 handle,
55 focus,
56 fade,
57 theme
58 )
59 }
62private class AttachmentGroupEntry(val key: ViewKey, val view: View)
64private class AttachmentGroupItems(private val values: Array<AttachmentGroupEntry>): ItemSource {
65 public override val count: Long get() = values.size
66 public override fun keyAt(index: Long): ViewKey = values[index].key
67 public override fun indexOfKey(key: ViewKey): Long? {
68 for (index in 0L ..< values.size)
69 if (values[index].key == key)
70 return Some(index)
71 return None
72 }
73 public override fun build(index: Long): View = values[index].view
76public class AttachmentGroup(
77 private val props: Props<AttachmentGroupProps>,
78 scope: ComponentScope
79): Component(scope) {
80 private val handle = CarouselHandle()
81 private val themes = ThemeAccess(scope)
82 public override fun onMount() {
83 themes.mount(props.value.theme)
84 }
85 public override fun render(): View {
86 val input = props.value
87 val owner = input.handle ?: handle
88 val theme = themes.read(input.theme)
89 val ring = theme.surfaceMetrics(SurfaceVisual(SurfaceKind.Plain)).focusOutset
90 val density = theme.tokens.typography.body.fontSize / 14.0
91 val content =
92 attachmentCarousel(input.source) {
93 this.itemSize = { _ -> CarouselItemSize.Content }
94 this.length = input.length
95 this.spacing = input.spacing
96 this.enabled = input.enabled
97 this.handle = Some(owner)
98 this.theme = input.theme
99 this.viewport =
100 CarouselViewportOptions(lowerBound(4.0, ring / density), input.focus)
101 this.previous = CarouselButtonOptions(visible = false)
102 this.next = CarouselButtonOptions(visible = false)
103 }
104 return element(
105 AttachmentGroupInkProps(input, owner, density),
106 { current, mount -> AttachmentGroupInk(current, mount) },
107 [content]
108 )
109 }
112internal class AttachmentGroupInkProps(
113 val props: AttachmentGroupProps,
114 val handle: CarouselHandle,
115 val density: Double
118internal class AttachmentGroupInk(
119 private val props: Props<AttachmentGroupInkProps>,
120 scope: ComponentScope
121): Element<Unit>(scope) {
122 private val area = scope.createArea()
123 private val deadline = scope.createDeadline { finish() }
124 private var previous: AttachmentGroupInkProps? = None
125 private var activity = scope.nativeWindow.activity
126 init {
127 scope.routeChildren(area)
128 scope.resource { area.scope.addDefaultAction { context, event -> input(context, event) } }
129 scope.observeWindowActivity {
130 value ->
131 val previous = activity
132 activity = value
133 if (
134 (previous.focused && !value.focused) ||
135 (previous.visible && !value.visible) ||
136 (!previous.occluded && value.occluded)
137 )
138 deadline.cancel()
139 }
140 }
141 private fun finish() {
142 if (
143 scope.isMounted &&
144 props.value.props.enabled &&
145 scope.nativeWindow.state ==
146 WindowState.Open &&
147 scope.nativeWindow.isVisible &&
148 !scope.nativeWindow.isOccluded
149 )
150 props.value.handle.finishWheel()
151 }
152 public override fun update(): UiInvalidation {
153 val input = props.value
154 when (previous) {
155 case Some(val old) -> if (
156 !input.props.enabled ||
157 old.props.source !==
158 input.props.source ||
159 old.handle !==
160 input.handle
161 )
162 deadline.cancel()
163 case None -> {}
164 }
165 previous = Some(input)
166 area.scope.enabled = input.props.enabled
167 return UiInvalidation.Layout
168 }
169 private fun input(context: RoutedEventContext, event: InputEvent) {
170 if (!scope.isMounted || !props.value.props.enabled)
171 return
172 when (event) {
173 case Pointer(
174 InputPointerEvent.Button(_, _, ElementState.Pressed, _)
175 ) -> deadline.cancel()
176 case Keyboard(_) -> deadline.cancel()
177 case Contact(InputContactEvent.Began(_)) -> deadline.cancel()
178 case Pointer(InputPointerEvent.Wheel(_, _, val phase, val momentum)) -> {
179 context.consumeScroll(40.0 * props.value.density) {
180 amount ->
181 if (amount.x != 0.0) {
182 props.value.handle.wheel(
183 if (scope.layoutDirection == LayoutDirection.Rtl)
184 amount.x
185 else
186 -amount.x
187 )
188 deadline.schedule(MonotonicTimeSource.now() + Duration.milliseconds(120L))
189 }
190 Point(amount.x, 0.0)
191 }
192 if (
193 phase ==
194 ScrollPhase.Ended ||
195 phase ==
196 ScrollPhase.Cancelled ||
197 momentum ==
198 ScrollPhase.Ended ||
199 momentum ==
200 ScrollPhase.Cancelled
201 ) {
202 deadline.cancel();
203 finish()
204 }
205 }
206 else -> {}
207 }
208 }
209 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
210 Unit
211 > {
212 val child = context.measureChild(0L, constraints);
213 context.placeChild(0L, Point(0.0, 0.0))
214 return LayoutResult(child, Unit)
215 }
216 public override fun scene(context: SceneContext, measured: Unit) {
217 val input = props.value
218 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
219 context.place(area, bounds)
220 val snaps = input.handle.snapPositions
221 val offset = input.handle.position
222 val start = input.props.fade && offset > 0.5
223 val end = input.props.fade && snaps.size > 0L && offset < snaps[snaps.size - 1L] - 0.5
224 val fade = (start || end) && bounds.width > 0.0 && bounds.height > 0.0
225 if (fade)
226 context.saveLayer()
227 context.children()
228 if (fade) {
229 val fraction = upperBound(0.49, 24.0 * input.density / bounds.width)
230 val left =
231 if (context.layoutDirection == LayoutDirection.Rtl)
232 end
233 else
234 start
235 val right =
236 if (context.layoutDirection == LayoutDirection.Rtl)
237 start
238 else
239 end
240 val stops = ArrayList<GradientStop>()
241 stops.add(
242 GradientStop(
243 0.0,
244 if (left)
245 Color.Transparent
246 else
247 Color.White
248 )
249 )
250 if (left)
251 stops.add(GradientStop(fraction, Color.White))
252 if (right)
253 stops.add(GradientStop(1.0 - fraction, Color.White))
254 stops.add(
255 GradientStop(
256 1.0,
257 if (right)
258 Color.Transparent
259 else
260 Color.White
261 )
262 )
263 val mask = Gradient.linear(Point(0.0, 0.0), Point(bounds.width, 0.0), stops)
264 context.saveLayer(blendMode = BlendMode.DstIn)
265 context.draw { paint -> paint.canvas.drawRect(bounds, Paint(gradient = Some(mask))) }
266 context.restore();
267 context.restore()
268 }
269 }
270 public override fun onUnmount() {
271 deadline.cancel()
272 }
275public fun attachmentGroup(configure: AttachmentGroupBuilder.() -> Unit): View =
276 makeAttachmentGroup(None, configure)
278public fun attachmentGroup(
279 source: ItemSource,
280 configure: AttachmentGroupBuilder.() -> Unit = {}
281): View = makeAttachmentGroup(Some(source), configure)
283internal fun makeAttachmentGroup(
284 source: ItemSource?,
285 configure: AttachmentGroupBuilder.() -> Unit
286): View {
287 val builder = AttachmentGroupBuilder();
288 builder.configure()
289 return component(
290 builder.snapshot(source),
291 { current, mount -> AttachmentGroup(current, mount) },
292 builder.key
293 )