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.layout.*
import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.shape.RoundedCornerShape 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.material3.MaterialTheme
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
@ -34,24 +32,26 @@ actual fun MyVerticalScrollbar(
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val density = LocalDensity.current val density = LocalDensity.current
var containerHeight by remember { mutableStateOf(1f) } var containerHeight by remember { mutableStateOf(0f) }
val thumbHeight = 75.dp val thumbHeight = 75.dp
val scrollPercentage by remember(scrollState, lazyListState) { val scrollPercentage: Float = when {
derivedStateOf { scrollState != null && scrollState.maxValue > 0 -> {
if (scrollState != null && scrollState.maxValue > 0) { (scrollState.value.toFloat() / scrollState.maxValue.toFloat()).coerceIn(0f, 1f)
scrollState.value.toFloat() / scrollState.maxValue }
} else if (lazyListState != null) { lazyListState != null -> {
val layoutInfo = lazyListState.layoutInfo val layoutInfo = lazyListState.layoutInfo
val totalItems = layoutInfo.totalItemsCount val totalItems = layoutInfo.totalItemsCount
if (totalItems == 0) 0f else { if (totalItems <= 1) {
val firstVisibleItem = lazyListState.firstVisibleItemIndex
firstVisibleItem.toFloat() / totalItems
}
} else {
0f 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( Box(
@ -59,60 +59,64 @@ actual fun MyVerticalScrollbar(
containerHeight = coordinates.size.height.toFloat() containerHeight = coordinates.size.height.toFloat()
} }
) { ) {
content() Box(modifier = Modifier.fillMaxSize()) {
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))
if (containerHeight > 0f) {
Box( Box(
modifier = Modifier modifier = Modifier
.offset(y = with(density) { currentThumbOffset.toDp() }) .align(Alignment.CenterEnd)
.width(10.dp) .fillMaxHeight()
.height(thumbHeight) .width(15.dp)
.align(Alignment.TopEnd) .background(Color.Transparent)
.clip(RoundedCornerShape(5.dp)) ) {
.background( val thumbHeightPx = with(density) { thumbHeight.toPx() }
brush = Brush.verticalGradient( val maxOffset = (containerHeight - thumbHeightPx).coerceAtLeast(0f)
colors = listOf( val currentThumbOffset = (scrollPercentage * maxOffset).coerceIn(0f, maxOffset)
MaterialTheme.colorScheme.secondary.copy(alpha = 1f),
MaterialTheme.colorScheme.secondary.copy(alpha = 0.75f), Box(
MaterialTheme.colorScheme.secondary.copy(alpha = 0.5f) 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(
.draggable( orientation = Orientation.Vertical,
orientation = Orientation.Vertical, state = rememberDraggableState { delta ->
state = rememberDraggableState { delta -> coroutineScope.launch {
coroutineScope.launch { if (maxOffset > 0f) {
if (maxOffset > 0) { val scrollRatio = delta / maxOffset
val scrollRatio = delta / maxOffset if (scrollState != null && scrollState.maxValue > 0) {
if (scrollState != null) { scrollState.scrollBy(scrollRatio * scrollState.maxValue)
scrollState.scrollBy(scrollRatio * scrollState.maxValue) } else if (lazyListState != null) {
} else if (lazyListState != null) { val totalItems = lazyListState.layoutInfo.totalItemsCount.coerceAtLeast(1)
val totalItems = lazyListState.layoutInfo.totalItemsCount lazyListState.scrollBy(scrollRatio * totalItems * 120f)
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))
)
} }
} }
} }

View file

@ -1821,7 +1821,10 @@ fun EditSourceCompose(
Column(modifier = Modifier.fillMaxSize()) { Column(modifier = Modifier.fillMaxSize()) {
if (isEditorVisible) { if (isEditorVisible) {
Card( Card(
modifier = Modifier.fillMaxSize().padding(4.dp), modifier = Modifier
.fillMaxWidth()
.heightIn(min = 240.dp, max = 460.dp)
.padding(4.dp),
shape = RoundedCornerShape(12.dp), shape = RoundedCornerShape(12.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 4.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( BasicTextField(
value = codeContent, value = codeContent,
onValueChange = { codeContent = it }, onValueChange = { codeContent = it },

View file

@ -94,18 +94,64 @@ fun MidiControlPanel(
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val prefs: Settings = provideSettings() val prefs: Settings = provideSettings()
LaunchedEffect(currentPos, duration, solfaScrollState.viewportSize, solfaScrollState.maxValue) { val activeIndex by sharedScreenModel.activeIndex.collectAsState()
if (duration > 0f && solfaScrollState.maxValue > 0) { val tuoTimestamps by sharedScreenModel.tuoTimestamps.collectAsState()
val progress = (currentPos / duration).coerceIn(0f, 1f) val tuoList by sharedScreenModel.tuoList.collectAsState()
val viewportHeight = solfaScrollState.viewportSize.toFloat()
val totalContentHeight = solfaScrollState.maxValue + viewportHeight
val targetY = (progress * (totalContentHeight-200)) - (viewportHeight / 2) val targetAutoScrollY by remember(
val finalTarget = targetY.coerceIn(0f, solfaScrollState.maxValue.toFloat()) 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( solfaScrollState.animateScrollTo(
value = finalTarget.toInt(), value = target,
animationSpec = tween ( animationSpec = tween(
durationMillis = 300, durationMillis = 180,
easing = LinearEasing easing = LinearEasing
) )
) )

View file

@ -1208,6 +1208,7 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode
}) })
// synchro // synchro
_mediaPlayer?.requestSync(this) _mediaPlayer?.requestSync(this)
syncDurationFromPlayer()
//finalizeMarkers() //finalizeMarkers()
_mediaPlayer?.syncNavigationMonitor(this) _mediaPlayer?.syncNavigationMonitor(this)
} catch(e: Exception) { } catch(e: Exception) {
@ -1257,14 +1258,21 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode
_mediaPlayer?.let { player -> _mediaPlayer?.let { player ->
player.setVolume(level) } player.setVolume(level) }
} }
fun syncDurationFromPlayer() {
_mediaPlayer?.let { player ->
val d = player.getDuration().toFloat()
if (d > 0f && _duration.value != d) {
_duration.value = d
}
}
}
fun updateProgress(){ fun updateProgress(){
_mediaPlayer?.let { player-> _mediaPlayer?.let { player->
val d = player.getDuration().toFloat()
if ((d > 0f) && _duration.value != d) _duration.value = d
if (_isPlay.value) { if (_isPlay.value) {
val p = player.getCurrentPosition().toFloat() val p = player.getCurrentPosition().toFloat()
val d = player.getDuration().toFloat() if (p >= 0f) _currentPos.value = p
if (p >= 0) _currentPos.value = p
if ((d > 0) && _duration.value != d) _duration.value = d
} }
} }
} }