From 23c2de7c221163279e0e36565fdfe68209f9d193 Mon Sep 17 00:00:00 2001 From: Stypox Date: Tue, 21 Oct 2025 23:52:03 +0200 Subject: [PATCH] Extract FixedHeightCenteredText from LongPressMenu --- .../ui/components/menu/LongPressMenu.kt | 30 +++++--------- .../util/text/FixedHeightCenteredText.kt | 39 +++++++++++++++++++ 2 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/util/text/FixedHeightCenteredText.kt diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenu.kt b/app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenu.kt index 141903143..e959ce615 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenu.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenu.kt @@ -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, + ) } } } diff --git a/app/src/main/java/org/schabi/newpipe/util/text/FixedHeightCenteredText.kt b/app/src/main/java/org/schabi/newpipe/util/text/FixedHeightCenteredText.kt new file mode 100644 index 000000000..57de24269 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/text/FixedHeightCenteredText.kt @@ -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) + ) + } +}