diff --git a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/ui/MyVerticalScrollbar.kt b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/ui/MyVerticalScrollbar.kt index b2e2770..5cce208 100644 --- a/composeApp/src/androidMain/kotlin/mg/dot/feufaro/ui/MyVerticalScrollbar.kt +++ b/composeApp/src/androidMain/kotlin/mg/dot/feufaro/ui/MyVerticalScrollbar.kt @@ -9,8 +9,6 @@ import androidx.compose.foundation.gestures.scrollBy import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material.icons.Icons -import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.* import androidx.compose.ui.Alignment @@ -34,24 +32,26 @@ actual fun MyVerticalScrollbar( val coroutineScope = rememberCoroutineScope() val density = LocalDensity.current - var containerHeight by remember { mutableStateOf(1f) } + var containerHeight by remember { mutableStateOf(0f) } val thumbHeight = 75.dp - val scrollPercentage by remember(scrollState, lazyListState) { - derivedStateOf { - if (scrollState != null && scrollState.maxValue > 0) { - scrollState.value.toFloat() / scrollState.maxValue - } else if (lazyListState != null) { - val layoutInfo = lazyListState.layoutInfo - val totalItems = layoutInfo.totalItemsCount - if (totalItems == 0) 0f else { - val firstVisibleItem = lazyListState.firstVisibleItemIndex - firstVisibleItem.toFloat() / totalItems - } - } else { + val scrollPercentage: Float = when { + scrollState != null && scrollState.maxValue > 0 -> { + (scrollState.value.toFloat() / scrollState.maxValue.toFloat()).coerceIn(0f, 1f) + } + lazyListState != null -> { + val layoutInfo = lazyListState.layoutInfo + val totalItems = layoutInfo.totalItemsCount + if (totalItems <= 1) { 0f + } else { + val firstVisibleItem = lazyListState.firstVisibleItemIndex.toFloat() + val firstVisibleOffset = lazyListState.firstVisibleItemScrollOffset.toFloat() + val avgItemSize = layoutInfo.visibleItemsInfo.firstOrNull()?.size?.toFloat()?.coerceAtLeast(1f) ?: 1f + ((firstVisibleItem + (firstVisibleOffset / avgItemSize)) / (totalItems - 1).coerceAtLeast(1)).coerceIn(0f, 1f) } } + else -> 0f } Box( @@ -59,60 +59,64 @@ actual fun MyVerticalScrollbar( containerHeight = coordinates.size.height.toFloat() } ) { - content() - - Box( - modifier = Modifier - .align(Alignment.CenterEnd) - .fillMaxHeight() - .width(15.dp) - .background(Color.Transparent) - ) { - val thumbHeightPx = with(density) { thumbHeight.toPx() } - val maxOffset = containerHeight - thumbHeightPx - val currentThumbOffset = (scrollPercentage * maxOffset).coerceIn(0f, maxOffset.coerceAtLeast(0f)) + Box(modifier = Modifier.fillMaxSize()) { + content() + } + if (containerHeight > 0f) { Box( modifier = Modifier - .offset(y = with(density) { currentThumbOffset.toDp() }) - .width(10.dp) - .height(thumbHeight) - .align(Alignment.TopEnd) - .clip(RoundedCornerShape(5.dp)) - .background( - brush = Brush.verticalGradient( - colors = listOf( - MaterialTheme.colorScheme.secondary.copy(alpha = 1f), - MaterialTheme.colorScheme.secondary.copy(alpha = 0.75f), - MaterialTheme.colorScheme.secondary.copy(alpha = 0.5f) + .align(Alignment.CenterEnd) + .fillMaxHeight() + .width(15.dp) + .background(Color.Transparent) + ) { + val thumbHeightPx = with(density) { thumbHeight.toPx() } + val maxOffset = (containerHeight - thumbHeightPx).coerceAtLeast(0f) + val currentThumbOffset = (scrollPercentage * maxOffset).coerceIn(0f, maxOffset) + + Box( + modifier = Modifier + .offset(y = with(density) { currentThumbOffset.toDp() }) + .width(10.dp) + .height(thumbHeight) + .align(Alignment.TopEnd) + .clip(RoundedCornerShape(5.dp)) + .background( + brush = Brush.verticalGradient( + colors = listOf( + MaterialTheme.colorScheme.secondary.copy(alpha = 1f), + MaterialTheme.colorScheme.secondary.copy(alpha = 0.75f), + MaterialTheme.colorScheme.secondary.copy(alpha = 0.5f) + ) ) ) - ) - .draggable( - orientation = Orientation.Vertical, - state = rememberDraggableState { delta -> - coroutineScope.launch { - if (maxOffset > 0) { - val scrollRatio = delta / maxOffset - if (scrollState != null) { - scrollState.scrollBy(scrollRatio * scrollState.maxValue) - } else if (lazyListState != null) { - val totalItems = lazyListState.layoutInfo.totalItemsCount - lazyListState.scrollBy(scrollRatio * totalItems * 120f) + .draggable( + orientation = Orientation.Vertical, + state = rememberDraggableState { delta -> + coroutineScope.launch { + if (maxOffset > 0f) { + val scrollRatio = delta / maxOffset + if (scrollState != null && scrollState.maxValue > 0) { + scrollState.scrollBy(scrollRatio * scrollState.maxValue) + } else if (lazyListState != null) { + val totalItems = lazyListState.layoutInfo.totalItemsCount.coerceAtLeast(1) + lazyListState.scrollBy(scrollRatio * totalItems * 120f) + } } } } - } + ) + ) { + Box( + modifier = Modifier + .align(Alignment.TopCenter) + .padding(top = 4.dp) + .size(6.dp) + .blur(1.dp) + .background(MaterialTheme.colorScheme.background, RoundedCornerShape(3.dp)) ) - ) { - Box( - modifier = Modifier - .align(Alignment.TopCenter) - .padding(top = 4.dp) - .size(6.dp) - .blur(1.dp) - .background(MaterialTheme.colorScheme.background, RoundedCornerShape(3.dp)) - ) + } } } } diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt index a8f8877..f004d9b 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/TimeUnitObject.kt @@ -1821,7 +1821,10 @@ fun EditSourceCompose( Column(modifier = Modifier.fillMaxSize()) { if (isEditorVisible) { Card( - modifier = Modifier.fillMaxSize().padding(4.dp), + modifier = Modifier + .fillMaxWidth() + .heightIn(min = 240.dp, max = 460.dp) + .padding(4.dp), shape = RoundedCornerShape(12.dp), elevation = CardDefaults.cardElevation(defaultElevation = 4.dp) ) { @@ -1911,7 +1914,7 @@ fun EditSourceCompose( ) } - Row(modifier = Modifier.fillMaxSize().background(Color.White)) { + Row(modifier = Modifier.fillMaxWidth().weight(1f).background(Color.White)) { BasicTextField( value = codeContent, onValueChange = { codeContent = it }, diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/MidiControlPanel.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/MidiControlPanel.kt index 5ad6c5a..e8f7b1d 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/MidiControlPanel.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/MidiControlPanel.kt @@ -94,18 +94,64 @@ fun MidiControlPanel( val coroutineScope = rememberCoroutineScope() val prefs: Settings = provideSettings() - LaunchedEffect(currentPos, duration, solfaScrollState.viewportSize, solfaScrollState.maxValue) { - if (duration > 0f && solfaScrollState.maxValue > 0) { - val progress = (currentPos / duration).coerceIn(0f, 1f) - val viewportHeight = solfaScrollState.viewportSize.toFloat() - val totalContentHeight = solfaScrollState.maxValue + viewportHeight + val activeIndex by sharedScreenModel.activeIndex.collectAsState() + val tuoTimestamps by sharedScreenModel.tuoTimestamps.collectAsState() + val tuoList by sharedScreenModel.tuoList.collectAsState() - val targetY = (progress * (totalContentHeight-200)) - (viewportHeight / 2) - val finalTarget = targetY.coerceIn(0f, solfaScrollState.maxValue.toFloat()) + val targetAutoScrollY by remember( + isPause, + currentPos, + duration, + activeIndex, + tuoTimestamps, + tuoList, + solfaScrollState.viewportSize, + solfaScrollState.maxValue, + platform + ) { + derivedStateOf { + val maxScroll = solfaScrollState.maxValue + val viewport = solfaScrollState.viewportSize + if (isPause || maxScroll <= 0 || viewport <= 0) { + null + } else { + val isAndroidLandscapeNow = platform.startsWith("Android") && viewport < 650 + val totalTuos = (tuoList.size - 1).coerceAtLeast(1) + + val progress = if (activeIndex >= 0 && totalTuos > 0) { + (activeIndex.toFloat() / totalTuos.toFloat()).coerceIn(0f, 1f) + } else if (duration > 0f) { + (currentPos / duration).coerceIn(0f, 1f) + } else { + 0f + } + + val bottomGuardPx = if (isAndroidLandscapeNow) { + (viewport * 0.35f).coerceAtLeast(120f) + } else { + (viewport * 0.25f).coerceAtLeast(140f) + } + val focusOffsetPx = if (isAndroidLandscapeNow) { + viewport * 0.22f + } else { + viewport * 0.42f + } + + val effectiveMaxScroll = (maxScroll.toFloat() - bottomGuardPx).coerceAtLeast(0f) + val rawTarget = (progress * (maxScroll.toFloat() + viewport * 0.2f)) - focusOffsetPx + rawTarget.coerceIn(0f, effectiveMaxScroll).toInt() + } + } + } + + LaunchedEffect(targetAutoScrollY) { + val target = targetAutoScrollY ?: return@LaunchedEffect + val delta = kotlin.math.abs(solfaScrollState.value - target) + if (delta > 6) { solfaScrollState.animateScrollTo( - value = finalTarget.toInt(), - animationSpec = tween ( - durationMillis = 300, + value = target, + animationSpec = tween( + durationMillis = 180, easing = LinearEasing ) ) diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt index 4760772..d0cfad5 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt @@ -1208,6 +1208,7 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode }) // synchro _mediaPlayer?.requestSync(this) + syncDurationFromPlayer() //finalizeMarkers() _mediaPlayer?.syncNavigationMonitor(this) } catch(e: Exception) { @@ -1257,14 +1258,21 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode _mediaPlayer?.let { player -> player.setVolume(level) } } + fun syncDurationFromPlayer() { + _mediaPlayer?.let { player -> + val d = player.getDuration().toFloat() + if (d > 0f && _duration.value != d) { + _duration.value = d + } + } + } fun updateProgress(){ _mediaPlayer?.let { player-> + val d = player.getDuration().toFloat() + if ((d > 0f) && _duration.value != d) _duration.value = d if (_isPlay.value) { val p = player.getCurrentPosition().toFloat() - val d = player.getDuration().toFloat() - - if (p >= 0) _currentPos.value = p - if ((d > 0) && _duration.value != d) _duration.value = d + if (p >= 0f) _currentPos.value = p } } }