scoop:scoop.ui.component:0.1.0
src/scroll/control.scoop
Selected production source · src/scroll/control.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.ui.style.Theme8
import scoop.window.*9
import scoop.window.accessibility.*11
internal class ScrollControlProps(12
val state: ScrollState,13
val axes: ScrollAxes,14
val lineStep: Double,15
val chain: ScrollChain,16
val scrollbars: ScrollbarVisibility,17
val scrollbar: ScrollbarOptions,18
val enabled: Boolean,19
val focus: FocusOptions,20
val paintCache: PaintCachePolicy,21
val theme: Theme,22
val style: StyleOverride = styleOverride(),23
val focusOutline: OutlineStyle? = None,24
val scrollbarBehavior: ScrollbarBehavior = scrollbarBehavior(),25
val semantics: ComponentSemantics = ComponentSemantics(),26
val touchScroll: TouchScrollOptions = TouchScrollOptions(),27
val scrollPhysics: ScrollPhysicsOptions = ScrollPhysicsOptions()28
) {29
val bars = ViewportScrollbarOptions(30
theme,31
axes,32
scrollbars,33
scrollbar,34
scrollbarBehavior,35
lineStep,36
enabled37
)38
}40
/** Shared input, range publication and themed bars for eager and lazy viewports. */41
internal class ScrollControl(42
private val scope: ComponentScope,43
private val props: () -> ScrollControlProps44
) {45
private val decoration = scope.resource { StylePainter() }46
val semantics = createComponentSemantics(scope, SemanticRole.ScrollArea)47
val viewport = scope.scrollViewport(props().state)48
private val area = scope49
.createArea(focusable = true) { context, event -> handle(context, event) }50
private val focus = scope.focusTarget(area, props().focus)51
private val pan = scope52
.scrollPan(area, viewport, props().axes, props().chain, props().touchScroll)53
private val bars = ViewportScrollbars(scope, viewport, area, semantics) { props().bars }54
private var direction = scope.layoutDirection55
val gutter: Size get() = bars.gutter57
init {58
area.scope.defaultAction = { context, event -> handle(context, event) }59
scope.routeChildren(area)60
scope.routeSemanticsChildren(semantics)61
semantics?.bindFocus(Some(focus))62
}63
fun update(): Boolean {64
val value = props()65
viewport.state = value.state66
viewport.paintCache = value.paintCache67
viewport.physics = value.scrollPhysics68
viewport.reduceMotion = value.theme.tokens.motion.preference == MotionPreference.Reduced69
pan70
.update(71
value.axes,72
value.chain,73
if (value.enabled)74
value.touchScroll75
else76
TouchScrollOptions(77
false,78
value.touchScroll.threshold,79
value.touchScroll.lockAxis,80
value.touchScroll.claimAtBoundary81
)82
)83
focus.options = value.focus84
area.scope.enabled = value.enabled85
semantics?.bindReference(value.semantics.reference)86
semantics87
?.update(88
value.semantics.properties(enabled = value.enabled),89
relations = value.semantics.relations90
)91
return bars.update()92
}93
fun contentViewport(outer: Size, direction: LayoutDirection): Rect = bars94
.contentViewport(outer, direction)95
fun layout(96
viewportSize: Size,97
contentSize: Size,98
adjustment: ScrollAdjustment? = None99
): ScrollLayout = bars.layout(viewportSize, contentSize, adjustment)101
fun scene(102
context: SceneContext,103
layout: ScrollLayout,104
contentOrigin: Point = Point(0.0, 0.0),105
children: (SceneContext) -> Unit = { content -> content.children() }106
) {107
val value = props()108
val size = context.size109
val presentedDirection = context.layoutDirection110
val body = contentViewport(size, presentedDirection)111
context112
.onFrameComplete { frame ->113
if (frame.submission == FrameSubmission.Accepted && scope.isMounted) {114
direction = presentedDirection115
}116
}117
val bounds = Rect(0.0, 0.0, size.width, size.height)118
when (semantics) {119
case Some(val handle) -> context.semantics(handle, bounds)120
case None -> {}121
}122
val visual = SurfaceVisual(123
SurfaceKind.Plain,124
InteractionVisual(enabled = value.enabled, focusVisible = area.isFocusVisible)125
)126
val geometry = value127
.theme128
.surfaceGeometry(129
bounds,130
visual,131
scrollSurfaceOptions(value.style, value.focusOutline),132
presentedDirection133
)134
val painter = bars.painter()135
context136
.draw { paint ->137
painter.drawSurface(paint.canvas, geometry, visual, SurfaceLayer.Background)138
}139
decoration140
.scene(context, bounds, geometry.contentStyle) { local ->141
local.place(area, bounds)142
local.save()143
try {144
local.translate(body.x, body.y)145
viewport146
.present(147
local,148
layout,149
Some(children),150
contentOrigin151
)152
} finally {153
local.restore()154
}155
bars.scene(local, layout)156
}157
context158
.draw { paint ->159
painter.drawSurface(paint.canvas, geometry, visual, SurfaceLayer.Foreground)160
}161
}163
private fun handle(context: RoutedEventContext, event: InputEvent) {164
if (context.phase == RoutedPhase.Notification) {165
when (event) {166
case FocusChanged(_) -> {167
if (scope.isMounted) scope.requestFrame()168
}170
case FocusVisibilityChanged(_) -> {171
if (scope.isMounted) scope.requestFrame()172
}174
else -> {}175
}176
return177
}178
if (context.phase != RoutedPhase.Default || !props().enabled) return179
val input = props()180
val state = props().state181
when (event) {182
case Pointer(InputPointerEvent.Wheel(_, _, val phase, val momentum)) ->183
viewport184
.consumeScroll(185
context,186
input.lineStep,187
input.axes,188
input.chain,189
phase,190
momentum191
)193
case Keyboard(val key) -> {194
if (context.target === area.scope && scrollKey(state, input.axes, direction, key))195
context.consume()196
}198
else -> {}199
}200
}201
}203
private fun scrollKey(204
state: ScrollState,205
axes: ScrollAxes,206
direction: LayoutDirection,207
key: KeyEvent208
): Boolean {209
val modifiers = key.modifiers210
if (key.state !=211
ElementState.Pressed ||212
key.synthetic ||213
modifiers.control ||214
modifiers.alt ||215
modifiers.shift ||216
modifiers.superKey ||217
modifiers.function218
)219
return false220
val current = state.snapshot221
val reversed = !axes.vertical && direction == LayoutDirection.Rtl222
val step = if (axes.vertical)223
Point(0.0, current.visibleSize.height)224
else225
Point(226
if (reversed) -current.visibleSize.width else current.visibleSize.width,227
0.0228
)229
val start = Point(230
if (axes.horizontal) 0.0 else current.offset.x,231
if (axes.vertical) 0.0 else current.offset.y232
)233
val end = Point(234
if (axes.horizontal) current.maximum.x else current.offset.x,235
if (axes.vertical) current.maximum.y else current.offset.y236
)237
when (key.logicalKey) {238
case Named(NamedKey.PageUp) -> state.scrollBy(Point(-step.x, -step.y), ScrollOrigin.User)239
case Named(NamedKey.PageDown) -> state.scrollBy(step, ScrollOrigin.User)240
case Named(NamedKey.Home) ->241
state242
.scrollTo(243
if (reversed) end else start,244
ScrollOrigin.User245
)246
case Named(NamedKey.End) ->247
state248
.scrollTo(249
if (reversed) start else end,250
ScrollOrigin.User251
)252
else -> return false253
}254
return true255
}