Extract FixedHeightCenteredText from LongPressMenu

This commit is contained in:
Stypox 2025-10-21 23:52:03 +02:00
parent 162c9ce565
commit 23c2de7c22
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 49 additions and 20 deletions

View File

@ -62,7 +62,6 @@ import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
@ -80,6 +79,7 @@ import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.ui.theme.customColors
import org.schabi.newpipe.util.Either
import org.schabi.newpipe.util.Localization
import org.schabi.newpipe.util.text.FixedHeightCenteredText
import org.schabi.newpipe.util.text.fadedMarquee
import java.time.OffsetDateTime
@ -112,6 +112,8 @@ fun getLongPressMenuView(
}
}
internal val MinButtonWidth = 86.dp
@Composable
fun LongPressMenu(
longPressable: LongPressable,
@ -130,10 +132,9 @@ fun LongPressMenu(
.fillMaxWidth()
.padding(start = 6.dp, end = 6.dp, bottom = 16.dp)
) {
val minButtonWidth = 86.dp
val buttonHeight = 86.dp
val buttonHeight = MinButtonWidth // landscape aspect ratio, square in the limit
val headerWidthInButtons = 5 // the header is 5 times as wide as the buttons
val buttonsPerRow = (this.maxWidth / minButtonWidth).toInt()
val buttonsPerRow = (this.maxWidth / MinButtonWidth).toInt()
// the channel icon goes in the menu header, so do not show a button for it
val actions = longPressActions.toMutableList()
@ -498,22 +499,11 @@ fun LongPressMenuButton(
contentDescription = null,
modifier = Modifier.size(32.dp),
)
Box {
// this allows making the box always the same height (i.e. the height of two text
// lines), while making the text appear centered if it is just a single line
Text(
text = "",
style = MaterialTheme.typography.bodySmall,
minLines = 2,
)
Text(
text = text,
style = MaterialTheme.typography.bodySmall,
maxLines = 2,
textAlign = TextAlign.Center,
modifier = Modifier.align(Alignment.Center)
)
}
FixedHeightCenteredText(
text = text,
lines = 2,
style = MaterialTheme.typography.bodySmall,
)
}
}
}

View File

@ -0,0 +1,39 @@
package org.schabi.newpipe.util.text
import androidx.compose.foundation.layout.Box
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
/**
* Like [Text] but with a fixed bounding box of [lines] text lines, and with text always centered
* within it even when its actual length uses less than [lines] lines.
*/
@Composable
fun FixedHeightCenteredText(
text: String,
lines: Int,
modifier: Modifier = Modifier,
style: TextStyle = LocalTextStyle.current,
) {
Box(modifier = modifier) {
// this allows making the box always the same height (i.e. the height of [lines] text
// lines), while making the text appear centered if it is just a single line
Text(
text = "",
style = style,
minLines = lines,
)
Text(
text = text,
style = style,
maxLines = lines,
textAlign = TextAlign.Center,
modifier = Modifier.align(Alignment.Center)
)
}
}