scoop:scoop.ui.component:0.1.0
src/conversation/attachment_group.scoop
Selected production source · src/conversation/attachment_group.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.platform.time.MonotonicTimeSource5
import scoop.surface.*6
import scoop.time.Duration7
import scoop.ui.*8
import scoop.ui.style.*9
import scoop.window.*10
import scoop.ui.component.carousel as attachmentCarousel12
public 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? = None21
)23
public class AttachmentGroupBuilder internal constructor() {24
public var length: Double? = None25
public var spacing: Double = 12.026
public var enabled: Boolean = true27
public var handle: CarouselHandle? = None28
public var focus: FocusOptions = FocusOptions()29
public var fade: Boolean = true30
public var theme: ThemeSource? = None31
public var key: ViewKey = ViewKey.Position32
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
theme58
)59
}60
}62
private class AttachmentGroupEntry(val key: ViewKey, val view: View)64
private class AttachmentGroupItems(private val values: Array<AttachmentGroupEntry>): ItemSource {65
public override val count: Long get() = values.size66
public override fun keyAt(index: Long): ViewKey = values[index].key67
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 None72
}73
public override fun build(index: Long): View = values[index].view74
}76
public class AttachmentGroup(77
private val props: Props<AttachmentGroupProps>,78
scope: ComponentScope79
): 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.value87
val owner = input.handle ?: handle88
val theme = themes.read(input.theme)89
val ring = theme.surfaceMetrics(SurfaceVisual(SurfaceKind.Plain)).focusOutset90
val density = theme.tokens.typography.body.fontSize / 14.091
val content =92
attachmentCarousel(input.source) {93
this.itemSize = { _ -> CarouselItemSize.Content }94
this.length = input.length95
this.spacing = input.spacing96
this.enabled = input.enabled97
this.handle = Some(owner)98
this.theme = input.theme99
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
}110
}112
internal class AttachmentGroupInkProps(113
val props: AttachmentGroupProps,114
val handle: CarouselHandle,115
val density: Double116
)118
internal class AttachmentGroupInk(119
private val props: Props<AttachmentGroupInkProps>,120
scope: ComponentScope121
): Element<Unit>(scope) {122
private val area = scope.createArea()123
private val deadline = scope.createDeadline { finish() }124
private var previous: AttachmentGroupInkProps? = None125
private var activity = scope.nativeWindow.activity126
init {127
scope.routeChildren(area)128
scope.resource { area.scope.addDefaultAction { context, event -> input(context, event) } }129
scope.observeWindowActivity {130
value ->131
val previous = activity132
activity = value133
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.isOccluded149
)150
props.value.handle.finishWheel()151
}152
public override fun update(): UiInvalidation {153
val input = props.value154
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.handle161
)162
deadline.cancel()163
case None -> {}164
}165
previous = Some(input)166
area.scope.enabled = input.props.enabled167
return UiInvalidation.Layout168
}169
private fun input(context: RoutedEventContext, event: InputEvent) {170
if (!scope.isMounted || !props.value.props.enabled)171
return172
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.x185
else186
-amount.x187
)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.Cancelled201
) {202
deadline.cancel();203
finish()204
}205
}206
else -> {}207
}208
}209
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<210
Unit211
> {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.value218
val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)219
context.place(area, bounds)220
val snaps = input.handle.snapPositions221
val offset = input.handle.position222
val start = input.props.fade && offset > 0.5223
val end = input.props.fade && snaps.size > 0L && offset < snaps[snaps.size - 1L] - 0.5224
val fade = (start || end) && bounds.width > 0.0 && bounds.height > 0.0225
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
end233
else234
start235
val right =236
if (context.layoutDirection == LayoutDirection.Rtl)237
start238
else239
end240
val stops = ArrayList<GradientStop>()241
stops.add(242
GradientStop(243
0.0,244
if (left)245
Color.Transparent246
else247
Color.White248
)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.Transparent259
else260
Color.White261
)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
}273
}275
public fun attachmentGroup(configure: AttachmentGroupBuilder.() -> Unit): View =276
makeAttachmentGroup(None, configure)278
public fun attachmentGroup(279
source: ItemSource,280
configure: AttachmentGroupBuilder.() -> Unit = {}281
): View = makeAttachmentGroup(Some(source), configure)283
internal fun makeAttachmentGroup(284
source: ItemSource?,285
configure: AttachmentGroupBuilder.() -> Unit286
): View {287
val builder = AttachmentGroupBuilder();288
builder.configure()289
return component(290
builder.snapshot(source),291
{ current, mount -> AttachmentGroup(current, mount) },292
builder.key293
)294
}