Fix landscape auto-scroll, on play midi - Android
This commit is contained in:
parent
facb79f397
commit
ac9b11702f
4 changed files with 137 additions and 76 deletions
|
|
@ -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,8 +59,11 @@ actual fun MyVerticalScrollbar(
|
||||||
containerHeight = coordinates.size.height.toFloat()
|
containerHeight = coordinates.size.height.toFloat()
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
|
Box(modifier = Modifier.fillMaxSize()) {
|
||||||
content()
|
content()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (containerHeight > 0f) {
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.align(Alignment.CenterEnd)
|
.align(Alignment.CenterEnd)
|
||||||
|
|
@ -69,8 +72,8 @@ actual fun MyVerticalScrollbar(
|
||||||
.background(Color.Transparent)
|
.background(Color.Transparent)
|
||||||
) {
|
) {
|
||||||
val thumbHeightPx = with(density) { thumbHeight.toPx() }
|
val thumbHeightPx = with(density) { thumbHeight.toPx() }
|
||||||
val maxOffset = containerHeight - thumbHeightPx
|
val maxOffset = (containerHeight - thumbHeightPx).coerceAtLeast(0f)
|
||||||
val currentThumbOffset = (scrollPercentage * maxOffset).coerceIn(0f, maxOffset.coerceAtLeast(0f))
|
val currentThumbOffset = (scrollPercentage * maxOffset).coerceIn(0f, maxOffset)
|
||||||
|
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
|
|
@ -92,12 +95,12 @@ actual fun MyVerticalScrollbar(
|
||||||
orientation = Orientation.Vertical,
|
orientation = Orientation.Vertical,
|
||||||
state = rememberDraggableState { delta ->
|
state = rememberDraggableState { delta ->
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
if (maxOffset > 0) {
|
if (maxOffset > 0f) {
|
||||||
val scrollRatio = delta / maxOffset
|
val scrollRatio = delta / maxOffset
|
||||||
if (scrollState != null) {
|
if (scrollState != null && scrollState.maxValue > 0) {
|
||||||
scrollState.scrollBy(scrollRatio * scrollState.maxValue)
|
scrollState.scrollBy(scrollRatio * scrollState.maxValue)
|
||||||
} else if (lazyListState != null) {
|
} else if (lazyListState != null) {
|
||||||
val totalItems = lazyListState.layoutInfo.totalItemsCount
|
val totalItems = lazyListState.layoutInfo.totalItemsCount.coerceAtLeast(1)
|
||||||
lazyListState.scrollBy(scrollRatio * totalItems * 120f)
|
lazyListState.scrollBy(scrollRatio * totalItems * 120f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -117,3 +120,4 @@ actual fun MyVerticalScrollbar(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
@ -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 },
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue