Fix landscape auto-scroll, on play midi - Android

This commit is contained in:
Hasinjato 2026-12-03 16:26:32 +03:00
parent facb79f397
commit ac9b11702f
4 changed files with 137 additions and 76 deletions

View file

@ -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 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 == 0) 0f else {
val firstVisibleItem = lazyListState.firstVisibleItemIndex
firstVisibleItem.toFloat() / totalItems
}
} else {
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,8 +59,11 @@ actual fun MyVerticalScrollbar(
containerHeight = coordinates.size.height.toFloat()
}
) {
Box(modifier = Modifier.fillMaxSize()) {
content()
}
if (containerHeight > 0f) {
Box(
modifier = Modifier
.align(Alignment.CenterEnd)
@ -69,8 +72,8 @@ actual fun MyVerticalScrollbar(
.background(Color.Transparent)
) {
val thumbHeightPx = with(density) { thumbHeight.toPx() }
val maxOffset = containerHeight - thumbHeightPx
val currentThumbOffset = (scrollPercentage * maxOffset).coerceIn(0f, maxOffset.coerceAtLeast(0f))
val maxOffset = (containerHeight - thumbHeightPx).coerceAtLeast(0f)
val currentThumbOffset = (scrollPercentage * maxOffset).coerceIn(0f, maxOffset)
Box(
modifier = Modifier
@ -92,12 +95,12 @@ actual fun MyVerticalScrollbar(
orientation = Orientation.Vertical,
state = rememberDraggableState { delta ->
coroutineScope.launch {
if (maxOffset > 0) {
if (maxOffset > 0f) {
val scrollRatio = delta / maxOffset
if (scrollState != null) {
if (scrollState != null && scrollState.maxValue > 0) {
scrollState.scrollBy(scrollRatio * scrollState.maxValue)
} else if (lazyListState != null) {
val totalItems = lazyListState.layoutInfo.totalItemsCount
val totalItems = lazyListState.layoutInfo.totalItemsCount.coerceAtLeast(1)
lazyListState.scrollBy(scrollRatio * totalItems * 120f)
}
}
@ -116,4 +119,5 @@ actual fun MyVerticalScrollbar(
}
}
}
}
}

View file

@ -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 },

View file

@ -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
)
)

View file

@ -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
}
}
}