package eu.friedcutlet.yokan.ui.components import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.draggable import androidx.compose.foundation.gestures.rememberDraggableState import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyListScope import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyGridScope import androidx.compose.foundation.lazy.grid.LazyGridState import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.rememberLazyGridState import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.runtime.Composable import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import kotlinx.coroutines.launch /** * `LazyColumn` wrapped in a [ScrollbarBox] so desktop users see a real * draggable scrollbar on the trailing edge. The list itself remains a stock * `LazyColumn`, so existing item/`stickyHeader` DSL keeps working. * * The whole composable also responds to mouse-drag and touch-drag, so users on * desktop touch screens can swipe through long lists without falling back to * the scrollbar handle. */ @Composable fun ScrollableLazyColumn( modifier: Modifier = Modifier, state: LazyListState = rememberLazyListState(), contentPadding: PaddingValues = PaddingValues(0.dp), verticalArrangement: Arrangement.Vertical = Arrangement.Top, horizontalAlignment: Alignment.Horizontal = Alignment.Start, userScrollEnabled: Boolean = true, content: LazyListScope.() -> Unit, ) { val scope = rememberCoroutineScope() // `draggable` is what gives touchscreens on desktop the same finger-pan // behaviour as mobile. LazyColumn already exposes the wheel/keyboard path. val dragHandle = rememberDraggableState { delta -> scope.launch { state.scrollBy(-delta) } } ScrollbarBox(state = state, modifier = modifier) { LazyColumn( state = state, modifier = Modifier .fillMaxWidth() .draggable(state = dragHandle, orientation = Orientation.Vertical), contentPadding = contentPadding, verticalArrangement = verticalArrangement, horizontalAlignment = horizontalAlignment, userScrollEnabled = userScrollEnabled, content = content, ) } } @Composable fun ScrollableLazyVerticalGrid( columns: GridCells, modifier: Modifier = Modifier, state: LazyGridState = rememberLazyGridState(), contentPadding: PaddingValues = PaddingValues(0.dp), verticalArrangement: Arrangement.Vertical = Arrangement.Top, horizontalArrangement: Arrangement.Horizontal = Arrangement.Start, userScrollEnabled: Boolean = true, content: LazyGridScope.() -> Unit, ) { val scope = rememberCoroutineScope() val dragHandle = rememberDraggableState { delta -> scope.launch { state.scrollBy(-delta) } } ScrollbarBox(state = state, modifier = modifier) { LazyVerticalGrid( columns = columns, state = state, modifier = Modifier .fillMaxWidth() .draggable(state = dragHandle, orientation = Orientation.Vertical), contentPadding = contentPadding, verticalArrangement = verticalArrangement, horizontalArrangement = horizontalArrangement, userScrollEnabled = userScrollEnabled, content = content, ) } } private suspend fun LazyListState.scrollBy(amount: Float) { if (amount == 0f) return scroll { scrollBy(amount) } } private suspend fun LazyGridState.scrollBy(amount: Float) { if (amount == 0f) return scroll { scrollBy(amount) } }