From aaec06944ca31f6715f454f1b00933ccbf88e58e Mon Sep 17 00:00:00 2001 From: Hasinjato Date: Fri, 30 Oct 2026 10:51:57 +0300 Subject: [PATCH] Fix! vertical scroll by locking scroll mode once vertical scroll is initiated --- .../kotlin/mg/dot/feufaro/ScreenSolfa.kt | 156 +++++++++++------- 1 file changed, 95 insertions(+), 61 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ScreenSolfa.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ScreenSolfa.kt index 15828fc..ce9e70e 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ScreenSolfa.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ScreenSolfa.kt @@ -3,23 +3,20 @@ package mg.dot.feufaro import SharedScreenModel import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.background -import androidx.compose.foundation.gestures.detectHorizontalDragGestures import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.layout.* import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.PlaylistAdd -import androidx.compose.material.icons.filled.Check -import androidx.compose.material.icons.filled.Code -import androidx.compose.material.icons.filled.Favorite -import androidx.compose.material.icons.filled.FavoriteBorder -import androidx.compose.material.icons.filled.PlaylistRemove +import androidx.compose.material.icons.filled.* import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput @@ -140,11 +137,15 @@ object ScreenSolfa : Screen { qrCodeResult = qrCodeResult, solfaScrollState = scrollState ) { paddingValues -> + var screenWidthPx by remember { mutableStateOf(0) } Box( modifier = Modifier .fillMaxSize() .padding(paddingValues) .padding(horizontal = 16.dp) + .onGloballyPositioned { coordinates -> + screenWidthPx = coordinates.size.width + } ) { Column( Modifier @@ -161,85 +162,118 @@ object ScreenSolfa : Screen { } ) } - .pointerInput(stanza, allowedStanzas, activeFilePath) { + .pointerInput(stanza, allowedStanzas, activeFilePath, screenWidthPx) { awaitPointerEventScope { while (true) { - val event = awaitPointerEvent() - val pressed = event.changes.filter { it.pressed } - if (pressed.isEmpty()) continue + val initialEvent = awaitPointerEvent() + val pressedChanges = initialEvent.changes.filter { it.pressed } + if (pressedChanges.isEmpty()) continue - val isTwoFingers = pressed.size >= 2 - val startX = if (isTwoFingers) (pressed[0].position.x + pressed[1].position.x) / 2f else pressed[0].position.x - val startY = if (isTwoFingers) (pressed[0].position.y + pressed[1].position.y) / 2f else pressed[0].position.y + val initialFingerCount = pressedChanges.size + val primaryChange = pressedChanges.first() + val startPos = primaryChange.position - var currentX = startX + if (startPos.x < 60f) { + continue + } + + var currentX = startPos.x + var currentY = startPos.y + + var dragging = false var isHorizontalDrag = false + var isVerticalDrag = false try { - if (isTwoFingers) pressed.forEach { it.consume() } - while (true) { val moveEvent = awaitPointerEvent() val movePressed = moveEvent.changes.filter { it.pressed } + if (movePressed.isEmpty()) break - if (isTwoFingers && movePressed.size < 2) break - if (!isTwoFingers && movePressed.size != 1) break + val primaryMove = movePressed.firstOrNull { it.id == primaryChange.id } ?: movePressed.first() + currentX = primaryMove.position.x + currentY = primaryMove.position.y - currentX = if (isTwoFingers) { - (movePressed[0].position.x + movePressed[1].position.x) / 2f - } else { - movePressed.first().position.x - } + val diffX = kotlin.math.abs(currentX - startPos.x) + val diffY = kotlin.math.abs(currentY - startPos.y) - val currentY = if (isTwoFingers) { - (movePressed[0].position.y + movePressed[1].position.y) / 2f - } else { - movePressed.first().position.y - } - - val diffX = kotlin.math.abs(currentX - startX) - val diffY = kotlin.math.abs(currentY - startY) - - if (diffX > 15f || diffY > 15f) { - if (diffX > diffY) { + if (diffX > 25f || diffY > 25f) { + dragging = true + if (diffX > diffY * 5.0f) { isHorizontalDrag = true - if (isTwoFingers) movePressed.forEach { it.consume() } - else movePressed.first().consume() +// println("DEBUG GESTURE -> HORIZONTAL dragging détecté (diffX: $diffX, diffY: $diffY)") } else { + isVerticalDrag = true +// println("DEBUG GESTURE -> VERTICAL dragging : libération totale pour le scroll") + } + break + } + } + + if (dragging && isVerticalDrag) { +// println("DEBUG GESTURE -> VERTICAL dragging : libération totale, attente de la levée du doigt...") + while (true) { + val releaseEvent = awaitPointerEvent() + if (releaseEvent.changes.all { !it.pressed }) { break } } + continue } - } catch (e: Exception) {} - if (isHorizontalDrag) { - val totalDragX = currentX - startX - val threshold = 70f + if (dragging && isHorizontalDrag) { +// println("DEBUG GESTURE -> HORIZONTAL dragging détecté") + primaryChange.consume() - if (isTwoFingers) { - val path = when { - totalDragX < -threshold -> sharedScreenModel.getNextPlaylistItemPath() - totalDragX > threshold -> sharedScreenModel.getPrevPlaylistItemPath() - else -> null - } - if (!path.isNullOrBlank()) { - sharedScreenModel.reset() - solfaScreenModel.loadFromFile(path) - } - } else { - val currentStanza = stanza.toString().toIntOrNull() ?: 1 - val target = when { - totalDragX < -threshold -> sharedScreenModel.nextAllowedStanza(currentStanza) - totalDragX > threshold -> sharedScreenModel.prevAllowedStanza(currentStanza) - else -> null - } - if (target != null) { - sharedScreenModel.setStanza(target) - coroutineScope.launch { - scrollState.animateScrollTo(0) + while (true) { + val dragEvent = awaitPointerEvent() + val dragPressed = dragEvent.changes.filter { it.pressed } + if (dragPressed.isEmpty()) { + val totalDragX = currentX - startPos.x + val threshold = screenWidthPx * 0.30f + +// println("DEBUG END DRAG -> totalDragX: ${totalDragX}f, threshold requis: ${threshold}f") + + if (screenWidthPx > 0 && kotlin.math.abs(totalDragX) >= threshold) { + if (initialFingerCount >= 2) { +// println("ACTION -> Changement de playlist (2 doigts)") + val path = if (totalDragX < 0) { + sharedScreenModel.getNextPlaylistItemPath() + } else { + sharedScreenModel.getPrevPlaylistItemPath() + } + if (!path.isNullOrBlank()) { + sharedScreenModel.reset() + solfaScreenModel.loadFromFile(path) + } + } else { +// println("ACTION -> Changement de stanza (1 doigt)") + val currentStanza = stanza.toString().toIntOrNull() ?: 1 + val target = if (totalDragX < 0) { + sharedScreenModel.nextAllowedStanza(currentStanza) + } else { + sharedScreenModel.prevAllowedStanza(currentStanza) + } + if (target != null) { + sharedScreenModel.setStanza(target) + coroutineScope.launch { + scrollState.animateScrollTo(0) + } + } + } + } else { +// println("ACTION IGNORÉE -> Le drag n'a pas atteint les 30% de l'écran.") + } + break } + + val primaryDrag = dragPressed.firstOrNull { it.id == primaryChange.id } ?: dragPressed.first() + currentX = primaryDrag.position.x + primaryDrag.consume() } } + } catch (e: Exception) { +// println("DEBUG EXCEPTION -> ${e.message}") } } }