|
| 1 | +/* |
| 2 | + * Copyright 2023 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.samples.apps.nowinandroid.core.designsystem.component.scrollbar |
| 18 | + |
| 19 | +import androidx.compose.animation.animateColorAsState |
| 20 | +import androidx.compose.animation.core.Spring |
| 21 | +import androidx.compose.animation.core.SpringSpec |
| 22 | +import androidx.compose.foundation.background |
| 23 | +import androidx.compose.foundation.gestures.Orientation |
| 24 | +import androidx.compose.foundation.gestures.Orientation.Horizontal |
| 25 | +import androidx.compose.foundation.gestures.Orientation.Vertical |
| 26 | +import androidx.compose.foundation.gestures.ScrollableState |
| 27 | +import androidx.compose.foundation.interaction.InteractionSource |
| 28 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 29 | +import androidx.compose.foundation.interaction.collectIsDraggedAsState |
| 30 | +import androidx.compose.foundation.interaction.collectIsHoveredAsState |
| 31 | +import androidx.compose.foundation.interaction.collectIsPressedAsState |
| 32 | +import androidx.compose.foundation.layout.Box |
| 33 | +import androidx.compose.foundation.layout.fillMaxHeight |
| 34 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 35 | +import androidx.compose.foundation.layout.height |
| 36 | +import androidx.compose.foundation.layout.width |
| 37 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 38 | +import androidx.compose.material3.MaterialTheme |
| 39 | +import androidx.compose.runtime.Composable |
| 40 | +import androidx.compose.runtime.LaunchedEffect |
| 41 | +import androidx.compose.runtime.getValue |
| 42 | +import androidx.compose.runtime.mutableStateOf |
| 43 | +import androidx.compose.runtime.remember |
| 44 | +import androidx.compose.runtime.setValue |
| 45 | +import androidx.compose.ui.Modifier |
| 46 | +import androidx.compose.ui.graphics.Color |
| 47 | +import androidx.compose.ui.unit.dp |
| 48 | +import com.google.samples.apps.nowinandroid.core.designsystem.component.scrollbar.ThumbState.Active |
| 49 | +import com.google.samples.apps.nowinandroid.core.designsystem.component.scrollbar.ThumbState.Dormant |
| 50 | +import com.google.samples.apps.nowinandroid.core.designsystem.component.scrollbar.ThumbState.Inactive |
| 51 | +import kotlinx.coroutines.delay |
| 52 | + |
| 53 | +/** |
| 54 | + * The time period for showing the scrollbar thumb after interacting with it, before it fades away |
| 55 | + */ |
| 56 | +private const val SCROLLBAR_INACTIVE_TO_DORMANT_TIME_IN_MS = 2_000L |
| 57 | + |
| 58 | +/** |
| 59 | + * A [Scrollbar] that allows for fast scrolling of content by dragging its thumb. |
| 60 | + * Its thumb disappears when the scrolling container is dormant. |
| 61 | + * @param modifier a [Modifier] for the [Scrollbar] |
| 62 | + * @param state the driving state for the [Scrollbar] |
| 63 | + * @param orientation the orientation of the scrollbar |
| 64 | + * @param onThumbMoved the fast scroll implementation |
| 65 | + */ |
| 66 | +@Composable |
| 67 | +fun ScrollableState.DraggableScrollbar( |
| 68 | + modifier: Modifier = Modifier, |
| 69 | + state: ScrollbarState, |
| 70 | + orientation: Orientation, |
| 71 | + onThumbMoved: (Float) -> Unit, |
| 72 | +) { |
| 73 | + val interactionSource = remember { MutableInteractionSource() } |
| 74 | + Scrollbar( |
| 75 | + modifier = modifier, |
| 76 | + orientation = orientation, |
| 77 | + interactionSource = interactionSource, |
| 78 | + state = state, |
| 79 | + thumb = { |
| 80 | + DraggableScrollbarThumb( |
| 81 | + interactionSource = interactionSource, |
| 82 | + orientation = orientation, |
| 83 | + ) |
| 84 | + }, |
| 85 | + onThumbMoved = onThumbMoved, |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * A simple [Scrollbar]. |
| 91 | + * Its thumb disappears when the scrolling container is dormant. |
| 92 | + * @param modifier a [Modifier] for the [Scrollbar] |
| 93 | + * @param state the driving state for the [Scrollbar] |
| 94 | + * @param orientation the orientation of the scrollbar |
| 95 | + */ |
| 96 | +@Composable |
| 97 | +fun ScrollableState.DecorativeScrollbar( |
| 98 | + modifier: Modifier = Modifier, |
| 99 | + state: ScrollbarState, |
| 100 | + orientation: Orientation, |
| 101 | +) { |
| 102 | + val interactionSource = remember { MutableInteractionSource() } |
| 103 | + Scrollbar( |
| 104 | + modifier = modifier, |
| 105 | + orientation = orientation, |
| 106 | + interactionSource = interactionSource, |
| 107 | + state = state, |
| 108 | + thumb = { |
| 109 | + DecorativeScrollbarThumb( |
| 110 | + interactionSource = interactionSource, |
| 111 | + orientation = orientation, |
| 112 | + ) |
| 113 | + }, |
| 114 | + ) |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * A scrollbar thumb that is intended to also be a touch target for fast scrolling. |
| 119 | + */ |
| 120 | +@Composable |
| 121 | +private fun ScrollableState.DraggableScrollbarThumb( |
| 122 | + interactionSource: InteractionSource, |
| 123 | + orientation: Orientation, |
| 124 | +) { |
| 125 | + Box( |
| 126 | + modifier = Modifier |
| 127 | + .run { |
| 128 | + when (orientation) { |
| 129 | + Vertical -> width(12.dp).fillMaxHeight() |
| 130 | + Horizontal -> height(12.dp).fillMaxWidth() |
| 131 | + } |
| 132 | + } |
| 133 | + .background( |
| 134 | + color = scrollbarThumbColor( |
| 135 | + interactionSource = interactionSource, |
| 136 | + ), |
| 137 | + shape = RoundedCornerShape(16.dp), |
| 138 | + ), |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * A decorative scrollbar thumb used solely for communicating a user's position in a list. |
| 144 | + */ |
| 145 | +@Composable |
| 146 | +private fun ScrollableState.DecorativeScrollbarThumb( |
| 147 | + interactionSource: InteractionSource, |
| 148 | + orientation: Orientation, |
| 149 | +) { |
| 150 | + Box( |
| 151 | + modifier = Modifier |
| 152 | + .run { |
| 153 | + when (orientation) { |
| 154 | + Vertical -> width(2.dp).fillMaxHeight() |
| 155 | + Horizontal -> height(2.dp).fillMaxWidth() |
| 156 | + } |
| 157 | + } |
| 158 | + .background( |
| 159 | + color = scrollbarThumbColor( |
| 160 | + interactionSource = interactionSource, |
| 161 | + ), |
| 162 | + shape = RoundedCornerShape(16.dp), |
| 163 | + ), |
| 164 | + ) |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * The color of the scrollbar thumb as a function of its interaction state. |
| 169 | + * @param interactionSource source of interactions in the scrolling container |
| 170 | + */ |
| 171 | +@Composable |
| 172 | +private fun ScrollableState.scrollbarThumbColor( |
| 173 | + interactionSource: InteractionSource, |
| 174 | +): Color { |
| 175 | + var state by remember { mutableStateOf(Dormant) } |
| 176 | + val pressed by interactionSource.collectIsPressedAsState() |
| 177 | + val hovered by interactionSource.collectIsHoveredAsState() |
| 178 | + val dragged by interactionSource.collectIsDraggedAsState() |
| 179 | + val active = (canScrollForward || canScrollForward) && |
| 180 | + (pressed || hovered || dragged || isScrollInProgress) |
| 181 | + |
| 182 | + val color by animateColorAsState( |
| 183 | + targetValue = when (state) { |
| 184 | + Active -> MaterialTheme.colorScheme.onSurface.copy(0.5f) |
| 185 | + Inactive -> MaterialTheme.colorScheme.onSurface.copy(alpha = 0.2f) |
| 186 | + Dormant -> Color.Transparent |
| 187 | + }, |
| 188 | + animationSpec = SpringSpec( |
| 189 | + stiffness = Spring.StiffnessLow, |
| 190 | + ), |
| 191 | + label = "Scrollbar thumb color", |
| 192 | + ) |
| 193 | + LaunchedEffect(active) { |
| 194 | + when (active) { |
| 195 | + true -> state = Active |
| 196 | + false -> if (state == Active) { |
| 197 | + state = Inactive |
| 198 | + delay(SCROLLBAR_INACTIVE_TO_DORMANT_TIME_IN_MS) |
| 199 | + state = Dormant |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return color |
| 205 | +} |
| 206 | + |
| 207 | +private enum class ThumbState { |
| 208 | + Active, Inactive, Dormant |
| 209 | +} |
0 commit comments