fix: Progress bar changing width when scrubbing

This commit is contained in:
PartyDonut 2025-10-22 19:29:22 +02:00
parent 654be43d60
commit 4c530f1c36
4 changed files with 34 additions and 10 deletions

View file

@ -52,7 +52,7 @@ fun VideoPlayerScreen(
) { ) {
val leanBackEnabled = leanBackEnabled(LocalContext.current) val leanBackEnabled = leanBackEnabled(LocalContext.current)
ExoPlayer { player -> ExoPlayer { player ->
ScaledContent(if (leanBackEnabled) 0.75f else 1f) { ScaledContent(if (leanBackEnabled) 0.65f else 1f) {
CustomVideoControls(player) CustomVideoControls(player)
} }
} }

View file

@ -23,6 +23,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.wrapContentSize import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
@ -62,6 +63,7 @@ import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastCoerceIn import androidx.compose.ui.util.fastCoerceIn
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
@ -71,6 +73,7 @@ import nl.jknaapen.fladder.objects.Translate
import nl.jknaapen.fladder.objects.VideoPlayerObject import nl.jknaapen.fladder.objects.VideoPlayerObject
import nl.jknaapen.fladder.utility.capitalize import nl.jknaapen.fladder.utility.capitalize
import nl.jknaapen.fladder.utility.formatTime import nl.jknaapen.fladder.utility.formatTime
import nl.jknaapen.fladder.utility.measureTextWidth
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
import kotlin.time.Duration import kotlin.time.Duration
@ -135,7 +138,7 @@ internal fun ProgressBar(
if (label.isNotBlank()) { if (label.isNotBlank()) {
Text( Text(
text = label, text = label,
style = MaterialTheme.typography.bodyLarge.copy( style = MaterialTheme.typography.titleLarge.copy(
color = Color.White, color = Color.White,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
), ),
@ -156,12 +159,20 @@ internal fun ProgressBar(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
modifier = modifier.fillMaxWidth() modifier = modifier.fillMaxWidth()
) { ) {
val timeTextStyle = MaterialTheme.typography.titleLarge.copy(
fontWeight = FontWeight.Bold
)
//Calculate min width to stop resizing of progressbar
val textWidth = measureTextWidth("-" + formatTime(duration), timeTextStyle)
Text( Text(
formatTime(currentPosition), formatTime(currentPosition),
color = Color.White, color = Color.White,
style = MaterialTheme.typography.bodyLarge.copy( modifier = Modifier.widthIn(min = textWidth),
fontWeight = FontWeight.Bold textAlign = TextAlign.Start,
) maxLines = 1,
style = timeTextStyle
) )
SimpleProgressBar( SimpleProgressBar(
player, player,
@ -184,9 +195,10 @@ internal fun ProgressBar(
) )
), ),
color = Color.White, color = Color.White,
style = MaterialTheme.typography.bodyLarge.copy( textAlign = TextAlign.End,
fontWeight = FontWeight.Bold modifier = Modifier.widthIn(min = textWidth),
) maxLines = 1,
style = timeTextStyle
) )
} }
} }

View file

@ -11,11 +11,9 @@ fun ScaledContent(
content: @Composable () -> Unit content: @Composable () -> Unit
) { ) {
val density = LocalDensity.current val density = LocalDensity.current
val fontScale = 1f / scale
CompositionLocalProvider( CompositionLocalProvider(
LocalDensity provides Density( LocalDensity provides Density(
density = density.density * scale, density = density.density * scale,
fontScale = fontScale
) )
) { ) {
content() content()

View file

@ -0,0 +1,14 @@
package nl.jknaapen.fladder.utility
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.Dp
@Composable
fun measureTextWidth(text: String, style: TextStyle): Dp {
val textMeasurer = rememberTextMeasurer()
val widthInPixels = textMeasurer.measure(text, style).size.width
return with(LocalDensity.current) { widthInPixels.toDp() }
}