forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Scrollable.kt
1005 lines (890 loc) · 36.5 KB
/
Scrollable.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.foundation.gestures
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.AnimationState
import androidx.compose.animation.core.DecayAnimationSpec
import androidx.compose.animation.core.animateDecay
import androidx.compose.animation.core.spring
import androidx.compose.animation.splineBasedDecay
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.FocusedBoundsObserverNode
import androidx.compose.foundation.MutatePriority
import androidx.compose.foundation.OverscrollEffect
import androidx.compose.foundation.gestures.BringIntoViewSpec.Companion.DefaultBringIntoViewSpec
import androidx.compose.foundation.gestures.Orientation.Horizontal
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.relocation.BringIntoViewResponderNode
import androidx.compose.foundation.rememberOverscrollEffect
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.MotionDurationScale
import androidx.compose.ui.focus.FocusProperties
import androidx.compose.ui.focus.FocusPropertiesModifierNode
import androidx.compose.ui.focus.FocusTargetModifierNode
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.KeyInputModifierNode
import androidx.compose.ui.input.key.isCtrlPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.type
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollDispatcher
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.NestedScrollSource.Companion.Drag
import androidx.compose.ui.input.nestedscroll.NestedScrollSource.Companion.Fling
import androidx.compose.ui.input.nestedscroll.NestedScrollSource.Companion.Wheel
import androidx.compose.ui.input.nestedscroll.nestedScrollModifierNode
import androidx.compose.ui.input.pointer.PointerEvent
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.modifier.ModifierLocalMap
import androidx.compose.ui.modifier.ModifierLocalModifierNode
import androidx.compose.ui.modifier.modifierLocalMapOf
import androidx.compose.ui.modifier.modifierLocalOf
import androidx.compose.ui.node.CompositionLocalConsumerModifierNode
import androidx.compose.ui.node.DelegatingNode
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.node.ObserverModifierNode
import androidx.compose.ui.node.currentValueOf
import androidx.compose.ui.node.observeReads
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.Velocity
import kotlin.math.abs
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/**
* Configure touch scrolling and flinging for the UI element in a single [Orientation].
*
* Users should update their state themselves using default [ScrollableState] and its
* `consumeScrollDelta` callback or by implementing [ScrollableState] interface manually and reflect
* their own state in UI when using this component.
*
* If you don't need to have fling or nested scroll support, but want to make component simply
* draggable, consider using [draggable].
*
* @sample androidx.compose.foundation.samples.ScrollableSample
*
* @param state [ScrollableState] state of the scrollable. Defines how scroll events will be
* interpreted by the user land logic and contains useful information about on-going events.
* @param orientation orientation of the scrolling
* @param enabled whether or not scrolling in enabled
* @param reverseDirection reverse the direction of the scroll, so top to bottom scroll will
* behave like bottom to top and left to right will behave like right to left.
* @param flingBehavior logic describing fling behavior when drag has finished with velocity. If
* `null`, default from [ScrollableDefaults.flingBehavior] will be used.
* @param interactionSource [MutableInteractionSource] that will be used to emit
* drag events when this scrollable is being dragged.
*/
@Stable
@OptIn(ExperimentalFoundationApi::class)
fun Modifier.scrollable(
state: ScrollableState,
orientation: Orientation,
enabled: Boolean = true,
reverseDirection: Boolean = false,
flingBehavior: FlingBehavior? = null,
interactionSource: MutableInteractionSource? = null
): Modifier = scrollable(
state = state,
orientation = orientation,
enabled = enabled,
reverseDirection = reverseDirection,
flingBehavior = flingBehavior,
interactionSource = interactionSource,
overscrollEffect = null
)
/**
* Configure touch scrolling and flinging for the UI element in a single [Orientation].
*
* Users should update their state themselves using default [ScrollableState] and its
* `consumeScrollDelta` callback or by implementing [ScrollableState] interface manually and reflect
* their own state in UI when using this component.
*
* If you don't need to have fling or nested scroll support, but want to make component simply
* draggable, consider using [draggable].
*
* This overload provides the access to [OverscrollEffect] that defines the behaviour of the
* over scrolling logic. Consider using [ScrollableDefaults.overscrollEffect] for the platform
* look-and-feel.
*
* @sample androidx.compose.foundation.samples.ScrollableSample
*
* @param state [ScrollableState] state of the scrollable. Defines how scroll events will be
* interpreted by the user land logic and contains useful information about on-going events.
* @param orientation orientation of the scrolling
* @param overscrollEffect effect to which the deltas will be fed when the scrollable have
* some scrolling delta left. Pass `null` for no overscroll. If you pass an effect you should
* also apply [androidx.compose.foundation.overscroll] modifier.
* @param enabled whether or not scrolling in enabled
* @param reverseDirection reverse the direction of the scroll, so top to bottom scroll will
* behave like bottom to top and left to right will behave like right to left.
* @param flingBehavior logic describing fling behavior when drag has finished with velocity. If
* `null`, default from [ScrollableDefaults.flingBehavior] will be used.
* @param interactionSource [MutableInteractionSource] that will be used to emit
* drag events when this scrollable is being dragged.
* @param bringIntoViewSpec The configuration that this scrollable should use to perform
* scrolling when scroll requests are received from the focus system.
*
* Note: This API is experimental as it brings support for some experimental features:
* [overscrollEffect] and [bringIntoViewScroller].
*/
@Stable
@ExperimentalFoundationApi
fun Modifier.scrollable(
state: ScrollableState,
orientation: Orientation,
overscrollEffect: OverscrollEffect?,
enabled: Boolean = true,
reverseDirection: Boolean = false,
flingBehavior: FlingBehavior? = null,
interactionSource: MutableInteractionSource? = null,
bringIntoViewSpec: BringIntoViewSpec = ScrollableDefaults.bringIntoViewSpec()
) = this then ScrollableElement(
state,
orientation,
overscrollEffect,
enabled,
reverseDirection,
flingBehavior,
interactionSource,
bringIntoViewSpec
)
@OptIn(ExperimentalFoundationApi::class)
private class ScrollableElement(
val state: ScrollableState,
val orientation: Orientation,
val overscrollEffect: OverscrollEffect?,
val enabled: Boolean,
val reverseDirection: Boolean,
val flingBehavior: FlingBehavior?,
val interactionSource: MutableInteractionSource?,
val bringIntoViewSpec: BringIntoViewSpec
) : ModifierNodeElement<ScrollableNode>() {
override fun create(): ScrollableNode {
return ScrollableNode(
state,
orientation,
overscrollEffect,
enabled,
reverseDirection,
flingBehavior,
interactionSource,
bringIntoViewSpec
)
}
override fun update(node: ScrollableNode) {
node.update(
state,
orientation,
overscrollEffect,
enabled,
reverseDirection,
flingBehavior,
interactionSource,
bringIntoViewSpec
)
}
override fun hashCode(): Int {
var result = state.hashCode()
result = 31 * result + orientation.hashCode()
result = 31 * result + overscrollEffect.hashCode()
result = 31 * result + enabled.hashCode()
result = 31 * result + reverseDirection.hashCode()
result = 31 * result + flingBehavior.hashCode()
result = 31 * result + interactionSource.hashCode()
result = 31 * result + bringIntoViewSpec.hashCode()
return result
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ScrollableElement) return false
if (state != other.state) return false
if (orientation != other.orientation) return false
if (overscrollEffect != other.overscrollEffect) return false
if (enabled != other.enabled) return false
if (reverseDirection != other.reverseDirection) return false
if (flingBehavior != other.flingBehavior) return false
if (interactionSource != other.interactionSource) return false
if (bringIntoViewSpec != other.bringIntoViewSpec) return false
return true
}
override fun InspectorInfo.inspectableProperties() {
name = "scrollable"
properties["orientation"] = orientation
properties["state"] = state
properties["overscrollEffect"] = overscrollEffect
properties["enabled"] = enabled
properties["reverseDirection"] = reverseDirection
properties["flingBehavior"] = flingBehavior
properties["interactionSource"] = interactionSource
properties["scrollableBringIntoViewConfig"] = bringIntoViewSpec
}
}
@OptIn(ExperimentalFoundationApi::class)
private class ScrollableNode(
private var state: ScrollableState,
private var orientation: Orientation,
private var overscrollEffect: OverscrollEffect?,
private var enabled: Boolean,
private var reverseDirection: Boolean,
private var flingBehavior: FlingBehavior?,
private var interactionSource: MutableInteractionSource?,
bringIntoViewSpec: BringIntoViewSpec
) : DelegatingNode(), ObserverModifierNode, CompositionLocalConsumerModifierNode,
FocusPropertiesModifierNode, KeyInputModifierNode {
val nestedScrollDispatcher = NestedScrollDispatcher()
// Place holder fling behavior, we'll initialize it when the density is available.
val defaultFlingBehavior = platformDefaultFlingBehavior()
val scrollingLogic = ScrollingLogic(
scrollableState = state,
orientation = orientation,
overscrollEffect = overscrollEffect,
reverseDirection = reverseDirection,
flingBehavior = flingBehavior ?: defaultFlingBehavior,
nestedScrollDispatcher = nestedScrollDispatcher,
)
val nestedScrollConnection =
ScrollableNestedScrollConnection(enabled = enabled, scrollingLogic = scrollingLogic)
val contentInViewNode =
delegate(
ContentInViewNode(
orientation,
state,
reverseDirection,
bringIntoViewSpec
)
)
val scrollableContainer = delegate(ModifierLocalScrollableContainerProvider(enabled))
init {
/**
* Nested scrolling
*/
delegate(nestedScrollModifierNode(nestedScrollConnection, nestedScrollDispatcher))
/**
* Focus scrolling
*/
delegate(FocusTargetModifierNode())
delegate(BringIntoViewResponderNode(contentInViewNode))
delegate(FocusedBoundsObserverNode { contentInViewNode.onFocusBoundsChanged(it) })
}
/**
* Pointer gesture handling
*/
val scrollableGesturesNode = delegate(
ScrollableGesturesNode(
interactionSource = interactionSource,
orientation = orientation,
enabled = enabled,
nestedScrollDispatcher = nestedScrollDispatcher,
scrollLogic = scrollingLogic
)
)
fun update(
state: ScrollableState,
orientation: Orientation,
overscrollEffect: OverscrollEffect?,
enabled: Boolean,
reverseDirection: Boolean,
flingBehavior: FlingBehavior?,
interactionSource: MutableInteractionSource?,
bringIntoViewSpec: BringIntoViewSpec
) {
if (this.enabled != enabled) { // enabled changed
nestedScrollConnection.enabled = enabled
scrollableContainer.enabled = enabled
}
// a new fling behavior was set, change the resolved one.
val resolvedFlingBehavior = flingBehavior ?: defaultFlingBehavior
scrollingLogic.update(
scrollableState = state,
orientation = orientation,
overscrollEffect = overscrollEffect,
reverseDirection = reverseDirection,
flingBehavior = resolvedFlingBehavior,
nestedScrollDispatcher = nestedScrollDispatcher
)
scrollableGesturesNode.update(
interactionSource = interactionSource,
orientation = orientation,
enabled = enabled
)
contentInViewNode.update(
orientation,
state,
reverseDirection,
bringIntoViewSpec
)
this.state = state
this.orientation = orientation
this.overscrollEffect = overscrollEffect
this.enabled = enabled
this.reverseDirection = reverseDirection
this.flingBehavior = flingBehavior
this.interactionSource = interactionSource
}
@Suppress("SuspiciousCompositionLocalModifierRead")
override fun onAttach() {
updateDefaultFlingBehavior()
observeReads { currentValueOf(LocalDensity) } // monitor change in Density
}
// TODO(https://youtrack.jetbrains.com/issue/COMPOSE-731/Scrollable-doesnt-react-on-density-changes)
// it isn't called, because LocalDensity is staticCompositionLocalOf
override fun onObservedReadsChanged() {
// if density changes, update the default fling behavior.
updateDefaultFlingBehavior()
}
private fun updateDefaultFlingBehavior() {
val density = currentValueOf(LocalDensity)
defaultFlingBehavior.updateDensity(density)
}
override fun applyFocusProperties(focusProperties: FocusProperties) {
focusProperties.canFocus = false
}
// Key handler for Page up/down scrolling behavior.
override fun onKeyEvent(event: KeyEvent): Boolean {
return if (enabled &&
(event.key == Key.PageDown || event.key == Key.PageUp) &&
(event.type == KeyEventType.KeyDown) &&
(!event.isCtrlPressed)
) {
with(scrollingLogic) {
val scrollAmount: Offset = if (orientation == Orientation.Vertical) {
val viewportHeight = contentInViewNode.viewportSize.height
val yAmount = if (event.key == Key.PageUp) {
viewportHeight.toFloat()
} else {
-viewportHeight.toFloat()
}
Offset(0f, yAmount)
} else {
val viewportWidth = contentInViewNode.viewportSize.width
val xAmount = if (event.key == Key.PageUp) {
viewportWidth.toFloat()
} else {
-viewportWidth.toFloat()
}
Offset(xAmount, 0f)
}
// A coroutine is launched for every individual scroll event in the
// larger scroll gesture. If we see degradation in the future (that is,
// a fast scroll gesture on a slow device causes UI jank [not seen up to
// this point), we can switch to a more efficient solution where we
// lazily launch one coroutine (with the first event) and use a Channel
// to communicate the scroll amount to the UI thread.
coroutineScope.launch {
scrollableState.scroll(MutatePriority.UserInput) {
dispatchScroll(scrollAmount, Wheel)
}
}
}
true
} else {
false
}
}
override fun onPreKeyEvent(event: KeyEvent) = false
}
/**
* The configuration of how a scrollable reacts to bring into view requests.
*
* Note: API shape and naming are still being refined, therefore API is marked as experimental.
*/
@ExperimentalFoundationApi
@Stable
interface BringIntoViewSpec {
/**
* A retargetable Animation Spec to be used as the animation to run to fulfill the
* BringIntoView requests.
*/
val scrollAnimationSpec: AnimationSpec<Float> get() = DefaultScrollAnimationSpec
/**
* Calculate the offset needed to bring one of the scrollable container's child into view.
*
* @param offset from the side closest to the origin (For the x-axis this is 'left',
* for the y-axis this is 'top').
* @param size is the child size.
* @param containerSize Is the main axis size of the scrollable container.
*
* All distances above are represented in pixels.
*
* @return The necessary amount to scroll to satisfy the bring into view request.
* Returning zero from here means that the request was satisfied and the scrolling animation
* should stop.
*
* This will be called for every frame of the scrolling animation. This means that, as the
* animation progresses, the offset will naturally change to fulfill the scroll request.
*/
fun calculateScrollDistance(
offset: Float,
size: Float,
containerSize: Float
): Float
companion object {
/**
* The default animation spec used by [Modifier.scrollable] to run Bring Into View requests.
*/
val DefaultScrollAnimationSpec: AnimationSpec<Float> = spring()
internal val DefaultBringIntoViewSpec = object : BringIntoViewSpec {
override val scrollAnimationSpec: AnimationSpec<Float> = DefaultScrollAnimationSpec
override fun calculateScrollDistance(
offset: Float,
size: Float,
containerSize: Float
): Float {
val trailingEdge = offset + size
val leadingEdge = offset
return when {
// If the item is already visible, no need to scroll.
leadingEdge >= 0 && trailingEdge <= containerSize -> 0f
// If the item is visible but larger than the parent, we don't scroll.
leadingEdge < 0 && trailingEdge > containerSize -> 0f
// Find the minimum scroll needed to make one of the edges coincide with the parent's
// edge.
abs(leadingEdge) < abs(trailingEdge - containerSize) -> leadingEdge
else -> trailingEdge - containerSize
}
}
}
}
}
@Composable
internal expect fun rememberFlingBehavior(): FlingBehavior
/**
* Contains the default values used by [scrollable]
*/
object ScrollableDefaults {
/**
* Create and remember default [FlingBehavior] that will represent natural fling curve.
*/
@Composable
fun flingBehavior(): FlingBehavior = rememberFlingBehavior()
/**
* Create and remember default [OverscrollEffect] that will be used for showing over scroll
* effects.
*/
@Composable
@ExperimentalFoundationApi
fun overscrollEffect(): OverscrollEffect {
return rememberOverscrollEffect()
}
/**
* Used to determine the value of `reverseDirection` parameter of [Modifier.scrollable]
* in scrollable layouts.
*
* @param layoutDirection current layout direction (e.g. from [LocalLayoutDirection])
* @param orientation orientation of scroll
* @param reverseScrolling whether scrolling direction should be reversed
*
* @return `true` if scroll direction should be reversed, `false` otherwise.
*/
fun reverseDirection(
layoutDirection: LayoutDirection,
orientation: Orientation,
reverseScrolling: Boolean
): Boolean {
// A finger moves with the content, not with the viewport. Therefore,
// always reverse once to have "natural" gesture that goes reversed to layout
var reverseDirection = !reverseScrolling
// But if rtl and horizontal, things move the other way around
val isRtl = layoutDirection == LayoutDirection.Rtl
if (isRtl && orientation != Orientation.Vertical) {
reverseDirection = !reverseDirection
}
return reverseDirection
}
/**
* A default implementation for [BringIntoViewSpec] that brings a child into view
* using the least amount of effort.
*/
@ExperimentalFoundationApi
fun bringIntoViewSpec(): BringIntoViewSpec = DefaultBringIntoViewSpec
}
internal interface ScrollConfig {
/**
* Enables animated transition of scroll on mouse wheel events.
*/
val isSmoothScrollingEnabled: Boolean
get() = false
fun isPreciseWheelScroll(event: PointerEvent): Boolean = false
fun Density.calculateMouseWheelScroll(event: PointerEvent, bounds: IntSize): Offset
}
internal expect fun CompositionLocalConsumerModifierNode.platformScrollConfig(): ScrollConfig
/**
* A node that detects and processes all scrollable gestures.
*/
private class ScrollableGesturesNode(
val scrollLogic: ScrollingLogic,
val orientation: Orientation,
val enabled: Boolean,
val nestedScrollDispatcher: NestedScrollDispatcher,
val interactionSource: MutableInteractionSource?
) : DelegatingNode() {
val draggableState = ScrollDraggableState(scrollLogic)
private val startDragImmediately = { scrollLogic.shouldScrollImmediately() }
private val onDragStopped: suspend CoroutineScope.(velocity: Velocity) -> Unit = { velocity ->
nestedScrollDispatcher.coroutineScope.launch {
scrollLogic.onDragStopped(velocity)
}
}
val draggableGesturesNode = delegate(
DraggableNode(
draggableState,
orientation = orientation,
enabled = enabled,
interactionSource = interactionSource,
reverseDirection = false,
startDragImmediately = startDragImmediately,
onDragStopped = onDragStopped,
canDrag = CanDragCalculation,
onDragStarted = NoOpOnDragStarted
)
)
val mouseWheelScrollNode = delegate(MouseWheelScrollNode(scrollLogic, enabled))
fun update(
orientation: Orientation,
enabled: Boolean,
interactionSource: MutableInteractionSource?,
) {
// update draggable node
draggableGesturesNode.update(
draggableState,
orientation = orientation,
enabled = enabled,
interactionSource = interactionSource,
reverseDirection = false,
startDragImmediately = startDragImmediately,
onDragStarted = NoOpOnDragStarted,
onDragStopped = onDragStopped,
canDrag = CanDragCalculation
)
mouseWheelScrollNode.enabled = enabled
}
}
private val CanDragCalculation: (PointerInputChange) -> Boolean =
{ down -> down.type != PointerType.Mouse }
private val NoOpOnDragStarted: suspend CoroutineScope.(startedPosition: Offset) -> Unit = {}
/**
* Holds all scrolling related logic: controls nested scrolling, flinging, overscroll and delta
* dispatching.
*/
@OptIn(ExperimentalFoundationApi::class)
internal class ScrollingLogic(
var scrollableState: ScrollableState,
private var orientation: Orientation,
private var overscrollEffect: OverscrollEffect?,
private var reverseDirection: Boolean,
private var flingBehavior: FlingBehavior,
private var nestedScrollDispatcher: NestedScrollDispatcher,
) {
private val isNestedFlinging = mutableStateOf(false)
fun Float.toOffset(): Offset = when {
this == 0f -> Offset.Zero
orientation == Horizontal -> Offset(this, 0f)
else -> Offset(0f, this)
}
fun Offset.singleAxisOffset(): Offset =
if (orientation == Horizontal) copy(y = 0f) else copy(x = 0f)
fun Offset.toFloat(): Float =
if (orientation == Horizontal) this.x else this.y
fun Velocity.toFloat(): Float =
if (orientation == Horizontal) this.x else this.y
fun Velocity.singleAxisVelocity(): Velocity =
if (orientation == Horizontal) copy(y = 0f) else copy(x = 0f)
fun Velocity.update(newValue: Float): Velocity =
if (orientation == Horizontal) copy(x = newValue) else copy(y = newValue)
fun Float.reverseIfNeeded(): Float = if (reverseDirection) this * -1 else this
fun Offset.reverseIfNeeded(): Offset = if (reverseDirection) this * -1f else this
/**
* @return the amount of scroll that was consumed
*/
fun ScrollScope.dispatchScroll(
initialAvailableDelta: Offset,
source: NestedScrollSource
): Offset {
val performScroll: (Offset) -> Offset = { delta ->
val consumedByPreScroll = nestedScrollDispatcher.dispatchPreScroll(delta, source)
val scrollAvailableAfterPreScroll = delta - consumedByPreScroll
val singleAxisDeltaForSelfScroll =
scrollAvailableAfterPreScroll.singleAxisOffset().reverseIfNeeded().toFloat()
// Consume on a single axis
val consumedBySelfScroll =
scrollBy(singleAxisDeltaForSelfScroll).toOffset().reverseIfNeeded()
val deltaAvailableAfterScroll = scrollAvailableAfterPreScroll - consumedBySelfScroll
val consumedByPostScroll = nestedScrollDispatcher.dispatchPostScroll(
consumedBySelfScroll,
deltaAvailableAfterScroll,
source
)
consumedByPreScroll + consumedBySelfScroll + consumedByPostScroll
}
val overscroll = overscrollEffect
return if (source == Wheel) {
performScroll(initialAvailableDelta)
} else if (overscroll != null && shouldDispatchOverscroll) {
overscroll.applyToScroll(initialAvailableDelta, source, performScroll)
} else {
performScroll(initialAvailableDelta)
}
}
private val shouldDispatchOverscroll
get() = scrollableState.canScrollForward || scrollableState.canScrollBackward
fun performRawScroll(scroll: Offset): Offset {
return if (scrollableState.isScrollInProgress) {
Offset.Zero
} else {
dispatchRawDelta(scroll)
}
}
fun dispatchRawDelta(scroll: Offset): Offset {
return scrollableState.dispatchRawDelta(scroll.toFloat().reverseIfNeeded())
.reverseIfNeeded().toOffset()
}
suspend fun onDragStopped(initialVelocity: Velocity) {
// Self started flinging, set
registerNestedFling(true)
val availableVelocity = initialVelocity.singleAxisVelocity()
scrollableState.scroll {
val performFling: suspend (Velocity) -> Velocity = { velocity ->
val preConsumedByParent = nestedScrollDispatcher
.dispatchPreFling(velocity)
val available = velocity - preConsumedByParent
val velocityLeft = doFlingAnimation(available)
val consumedPost =
nestedScrollDispatcher.dispatchPostFling(
(available - velocityLeft),
velocityLeft
)
val totalLeft = velocityLeft - consumedPost
velocity - totalLeft
}
val overscroll = overscrollEffect
if (overscroll != null && shouldDispatchOverscroll) {
overscroll.applyToFling(availableVelocity, performFling)
} else {
performFling(availableVelocity)
}
}
// Self stopped flinging, reset
registerNestedFling(false)
}
suspend fun ScrollScope.doFlingAnimation(available: Velocity): Velocity {
var result: Velocity = available
val outerScopeScroll: (Offset) -> Offset = { delta ->
dispatchScroll(delta.reverseIfNeeded(), Fling).reverseIfNeeded()
}
val scope = object : ScrollScope {
override fun scrollBy(pixels: Float): Float {
return outerScopeScroll.invoke(pixels.toOffset()).toFloat()
}
}
with(scope) {
with(flingBehavior) {
result = result.update(
performFling(available.toFloat().reverseIfNeeded()).reverseIfNeeded()
)
}
}
return result
}
fun shouldScrollImmediately(): Boolean {
return scrollableState.isScrollInProgress || isNestedFlinging.value ||
overscrollEffect?.isInProgress ?: false
}
fun registerNestedFling(isFlinging: Boolean) {
isNestedFlinging.value = isFlinging
}
fun update(
scrollableState: ScrollableState,
orientation: Orientation,
overscrollEffect: OverscrollEffect?,
reverseDirection: Boolean,
flingBehavior: FlingBehavior,
nestedScrollDispatcher: NestedScrollDispatcher,
) {
this.scrollableState = scrollableState
this.orientation = orientation
this.overscrollEffect = overscrollEffect
this.reverseDirection = reverseDirection
this.flingBehavior = flingBehavior
this.nestedScrollDispatcher = nestedScrollDispatcher
}
}
private class ScrollDraggableState(
var scrollLogic: ScrollingLogic
) : DraggableState, DragScope {
var latestScrollScope: ScrollScope = NoOpScrollScope
override fun dragBy(pixels: Float) {
with(scrollLogic) {
with(latestScrollScope) {
dispatchScroll(pixels.toOffset(), Drag)
}
}
}
override suspend fun drag(dragPriority: MutatePriority, block: suspend DragScope.() -> Unit) {
scrollLogic.scrollableState.scroll(dragPriority) {
latestScrollScope = this
block()
}
}
override fun dispatchRawDelta(delta: Float) {
with(scrollLogic) { performRawScroll(delta.toOffset()) }
}
}
private val NoOpScrollScope: ScrollScope = object : ScrollScope {
override fun scrollBy(pixels: Float): Float = pixels
}
private class ScrollableNestedScrollConnection(
val scrollingLogic: ScrollingLogic,
var enabled: Boolean
) : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
// child will fling, set
if (source == Fling) {
scrollingLogic.registerNestedFling(true)
}
return Offset.Zero
}
override fun onPostScroll(
consumed: Offset,
available: Offset,
source: NestedScrollSource
): Offset = if (enabled) {
scrollingLogic.performRawScroll(available)
} else {
Offset.Zero
}
override suspend fun onPostFling(
consumed: Velocity,
available: Velocity
): Velocity {
return if (enabled) {
var velocityLeft: Velocity = available
with(scrollingLogic) {
scrollableState.scroll {
velocityLeft = doFlingAnimation(available)
}
}
available - velocityLeft
} else {
Velocity.Zero
}.also {
// Flinging child finished flinging, reset
scrollingLogic.registerNestedFling(false)
}
}
}
/**
* Compatibility interface for default fling behaviors that depends on [Density].
*/
internal interface ScrollableDefaultFlingBehavior : FlingBehavior {
/**
* Update the internal parameters of FlingBehavior in accordance with the new [androidx.compose.ui.unit.Density] value.
*
* @param density new density value.
*/
fun updateDensity(density: Density) = Unit
}
/**
* This method returns [ScrollableDefaultFlingBehavior] whose density will be managed by the
* [ScrollableElement] because it's not created inside [Composable] context.
* This is different from [rememberFlingBehavior] which creates [FlingBehavior] whose density
* depends on [LocalDensity] and is automatically resolved.
*/
internal expect fun platformDefaultFlingBehavior(): ScrollableDefaultFlingBehavior
internal class DefaultFlingBehavior(
var flingDecay: DecayAnimationSpec<Float>,
private val motionDurationScale: MotionDurationScale = DefaultScrollMotionDurationScale
) : ScrollableDefaultFlingBehavior {
// For Testing
var lastAnimationCycleCount = 0
override suspend fun ScrollScope.performFling(initialVelocity: Float): Float {
lastAnimationCycleCount = 0
// come up with the better threshold, but we need it since spline curve gives us NaNs
return withContext(motionDurationScale) {
if (abs(initialVelocity) > 1f) {
var velocityLeft = initialVelocity
var lastValue = 0f
val animationState = AnimationState(
initialValue = 0f,
initialVelocity = initialVelocity,
)
try {
animationState.animateDecay(flingDecay) {
val delta = value - lastValue
val consumed = scrollBy(delta)
lastValue = value
velocityLeft = this.velocity
// avoid rounding errors and stop if anything is unconsumed
if (abs(delta - consumed) > 0.5f) this.cancelAnimation()
lastAnimationCycleCount++
}
} catch (exception: CancellationException) {
velocityLeft = animationState.velocity
}
velocityLeft
} else {
initialVelocity
}
}
}
override fun updateDensity(density: Density) {
flingDecay = splineBasedDecay(density)
}
}
// TODO: b/203141462 - make this public and move it to ui
/**
* Whether this modifier is inside a scrollable container, provided by [Modifier.scrollable].
* Defaults to false.
*/
internal val ModifierLocalScrollableContainer = modifierLocalOf { false }
internal val NoOpFlingBehavior = object : FlingBehavior {
override suspend fun ScrollScope.performFling(initialVelocity: Float): Float = 0f
}
private const val DefaultScrollMotionDurationScaleFactor = 1f
internal val DefaultScrollMotionDurationScale = object : MotionDurationScale {
override val scaleFactor: Float
get() = DefaultScrollMotionDurationScaleFactor
}
private class ModifierLocalScrollableContainerProvider(var enabled: Boolean) :
ModifierLocalModifierNode,
Modifier.Node() {
private val modifierLocalMap =
modifierLocalMapOf(entry = ModifierLocalScrollableContainer to true)
override val providedValues: ModifierLocalMap
get() = if (enabled) {
modifierLocalMap
} else {
modifierLocalMapOf()
}
}
internal val UnityDensity = object : Density {