Skip to content

Commit b26ab27

Browse files
committed
Extract FixedHeightCenteredText from LongPressMenu
1 parent ff13219 commit b26ab27

2 files changed

Lines changed: 49 additions & 20 deletions

File tree

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenu.kt

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import androidx.compose.ui.text.PlaceholderVerticalAlign
6262
import androidx.compose.ui.text.SpanStyle
6363
import androidx.compose.ui.text.buildAnnotatedString
6464
import androidx.compose.ui.text.font.FontWeight
65-
import androidx.compose.ui.text.style.TextAlign
6665
import androidx.compose.ui.text.style.TextDecoration
6766
import androidx.compose.ui.text.withStyle
6867
import androidx.compose.ui.tooling.preview.Preview
@@ -80,6 +79,7 @@ import org.schabi.newpipe.ui.theme.AppTheme
8079
import org.schabi.newpipe.ui.theme.customColors
8180
import org.schabi.newpipe.util.Either
8281
import org.schabi.newpipe.util.Localization
82+
import org.schabi.newpipe.util.text.FixedHeightCenteredText
8383
import org.schabi.newpipe.util.text.fadedMarquee
8484
import java.time.OffsetDateTime
8585

@@ -112,6 +112,8 @@ fun getLongPressMenuView(
112112
}
113113
}
114114

115+
internal val MinButtonWidth = 86.dp
116+
115117
@Composable
116118
fun LongPressMenu(
117119
longPressable: LongPressable,
@@ -130,10 +132,9 @@ fun LongPressMenu(
130132
.fillMaxWidth()
131133
.padding(start = 6.dp, end = 6.dp, bottom = 16.dp)
132134
) {
133-
val minButtonWidth = 86.dp
134-
val buttonHeight = 86.dp
135+
val buttonHeight = MinButtonWidth // landscape aspect ratio, square in the limit
135136
val headerWidthInButtons = 5 // the header is 5 times as wide as the buttons
136-
val buttonsPerRow = (this.maxWidth / minButtonWidth).toInt()
137+
val buttonsPerRow = (this.maxWidth / MinButtonWidth).toInt()
137138

138139
// the channel icon goes in the menu header, so do not show a button for it
139140
val actions = longPressActions.toMutableList()
@@ -498,22 +499,11 @@ fun LongPressMenuButton(
498499
contentDescription = null,
499500
modifier = Modifier.size(32.dp),
500501
)
501-
Box {
502-
// this allows making the box always the same height (i.e. the height of two text
503-
// lines), while making the text appear centered if it is just a single line
504-
Text(
505-
text = "",
506-
style = MaterialTheme.typography.bodySmall,
507-
minLines = 2,
508-
)
509-
Text(
510-
text = text,
511-
style = MaterialTheme.typography.bodySmall,
512-
maxLines = 2,
513-
textAlign = TextAlign.Center,
514-
modifier = Modifier.align(Alignment.Center)
515-
)
516-
}
502+
FixedHeightCenteredText(
503+
text = text,
504+
lines = 2,
505+
style = MaterialTheme.typography.bodySmall,
506+
)
517507
}
518508
}
519509
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.schabi.newpipe.util.text
2+
3+
import androidx.compose.foundation.layout.Box
4+
import androidx.compose.material3.LocalTextStyle
5+
import androidx.compose.material3.Text
6+
import androidx.compose.runtime.Composable
7+
import androidx.compose.ui.Alignment
8+
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.text.TextStyle
10+
import androidx.compose.ui.text.style.TextAlign
11+
12+
/**
13+
* Like [Text] but with a fixed bounding box of [lines] text lines, and with text always centered
14+
* within it even when its actual length uses less than [lines] lines.
15+
*/
16+
@Composable
17+
fun FixedHeightCenteredText(
18+
text: String,
19+
lines: Int,
20+
modifier: Modifier = Modifier,
21+
style: TextStyle = LocalTextStyle.current,
22+
) {
23+
Box(modifier = modifier) {
24+
// this allows making the box always the same height (i.e. the height of [lines] text
25+
// lines), while making the text appear centered if it is just a single line
26+
Text(
27+
text = "",
28+
style = style,
29+
minLines = lines,
30+
)
31+
Text(
32+
text = text,
33+
style = style,
34+
maxLines = lines,
35+
textAlign = TextAlign.Center,
36+
modifier = Modifier.align(Alignment.Center)
37+
)
38+
}
39+
}

0 commit comments

Comments
 (0)