Scoop API

scoop:scoop.ui.component:0.1.0

src/scroll/control.scoop

Selected production source · src/scroll/control.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.style.Theme
8import scoop.window.*
9import scoop.window.accessibility.*
11internal 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 enabled
37 )
40/** Shared input, range publication and themed bars for eager and lazy viewports. */
41internal class ScrollControl(
42 private val scope: ComponentScope,
43 private val props: () -> ScrollControlProps
44) {
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 = scope
49 .createArea(focusable = true) { context, event -> handle(context, event) }
50 private val focus = scope.focusTarget(area, props().focus)
51 private val pan = scope
52 .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.layoutDirection
55 val gutter: Size get() = bars.gutter
57 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.state
66 viewport.paintCache = value.paintCache
67 viewport.physics = value.scrollPhysics
68 viewport.reduceMotion = value.theme.tokens.motion.preference == MotionPreference.Reduced
69 pan
70 .update(
71 value.axes,
72 value.chain,
73 if (value.enabled)
74 value.touchScroll
75 else
76 TouchScrollOptions(
77 false,
78 value.touchScroll.threshold,
79 value.touchScroll.lockAxis,
80 value.touchScroll.claimAtBoundary
81 )
82 )
83 focus.options = value.focus
84 area.scope.enabled = value.enabled
85 semantics?.bindReference(value.semantics.reference)
86 semantics
87 ?.update(
88 value.semantics.properties(enabled = value.enabled),
89 relations = value.semantics.relations
90 )
91 return bars.update()
92 }
93 fun contentViewport(outer: Size, direction: LayoutDirection): Rect = bars
94 .contentViewport(outer, direction)
95 fun layout(
96 viewportSize: Size,
97 contentSize: Size,
98 adjustment: ScrollAdjustment? = None
99 ): 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.size
109 val presentedDirection = context.layoutDirection
110 val body = contentViewport(size, presentedDirection)
111 context
112 .onFrameComplete { frame ->
113 if (frame.submission == FrameSubmission.Accepted && scope.isMounted) {
114 direction = presentedDirection
115 }
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 = value
127 .theme
128 .surfaceGeometry(
129 bounds,
130 visual,
131 scrollSurfaceOptions(value.style, value.focusOutline),
132 presentedDirection
133 )
134 val painter = bars.painter()
135 context
136 .draw { paint ->
137 painter.drawSurface(paint.canvas, geometry, visual, SurfaceLayer.Background)
138 }
139 decoration
140 .scene(context, bounds, geometry.contentStyle) { local ->
141 local.place(area, bounds)
142 local.save()
143 try {
144 local.translate(body.x, body.y)
145 viewport
146 .present(
147 local,
148 layout,
149 Some(children),
150 contentOrigin
151 )
152 } finally {
153 local.restore()
154 }
155 bars.scene(local, layout)
156 }
157 context
158 .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 return
177 }
178 if (context.phase != RoutedPhase.Default || !props().enabled) return
179 val input = props()
180 val state = props().state
181 when (event) {
182 case Pointer(InputPointerEvent.Wheel(_, _, val phase, val momentum)) ->
183 viewport
184 .consumeScroll(
185 context,
186 input.lineStep,
187 input.axes,
188 input.chain,
189 phase,
190 momentum
191 )
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 }
203private fun scrollKey(
204 state: ScrollState,
205 axes: ScrollAxes,
206 direction: LayoutDirection,
207 key: KeyEvent
208): Boolean {
209 val modifiers = key.modifiers
210 if (key.state !=
211 ElementState.Pressed ||
212 key.synthetic ||
213 modifiers.control ||
214 modifiers.alt ||
215 modifiers.shift ||
216 modifiers.superKey ||
217 modifiers.function
218 )
219 return false
220 val current = state.snapshot
221 val reversed = !axes.vertical && direction == LayoutDirection.Rtl
222 val step = if (axes.vertical)
223 Point(0.0, current.visibleSize.height)
224 else
225 Point(
226 if (reversed) -current.visibleSize.width else current.visibleSize.width,
227 0.0
228 )
229 val start = Point(
230 if (axes.horizontal) 0.0 else current.offset.x,
231 if (axes.vertical) 0.0 else current.offset.y
232 )
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.y
236 )
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 state
242 .scrollTo(
243 if (reversed) end else start,
244 ScrollOrigin.User
245 )
246 case Named(NamedKey.End) ->
247 state
248 .scrollTo(
249 if (reversed) start else end,
250 ScrollOrigin.User
251 )
252 else -> return false
253 }
254 return true