Skip to content

Commit 5ea1cbb

Browse files
authored
Desktop text scaling (#3)
1 parent 1f83f58 commit 5ea1cbb

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

desktop/src/main/kotlin/com/jdamcd/arrivals/desktop/ArrivalsView.kt

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.compose.animation.core.tween
99
import androidx.compose.foundation.background
1010
import androidx.compose.foundation.layout.Arrangement
1111
import androidx.compose.foundation.layout.Box
12+
import androidx.compose.foundation.layout.BoxWithConstraints
1213
import androidx.compose.foundation.layout.Column
1314
import androidx.compose.foundation.layout.Row
1415
import androidx.compose.foundation.layout.fillMaxSize
@@ -27,7 +28,9 @@ import androidx.compose.runtime.Composable
2728
import androidx.compose.runtime.getValue
2829
import androidx.compose.ui.Alignment
2930
import androidx.compose.ui.Modifier
31+
import androidx.compose.ui.graphics.Color
3032
import androidx.compose.ui.text.TextStyle
33+
import androidx.compose.ui.unit.TextUnit
3134
import androidx.compose.ui.unit.dp
3235
import androidx.compose.ui.unit.sp
3336
import com.jdamcd.arrivals.Arrival
@@ -64,23 +67,28 @@ private fun Loading() {
6467
@Composable
6568
private fun Data(state: ArrivalsState.Data, onClickRefresh: () -> Unit) {
6669
Column {
67-
Column(
68-
modifier = Modifier
69-
.padding(top = 20.dp, bottom = 8.dp, start = 32.dp, end = 32.dp)
70-
.weight(1f)
71-
.fillMaxWidth(),
72-
verticalArrangement = Arrangement.SpaceEvenly
70+
BoxWithConstraints(
71+
modifier = Modifier.weight(1f)
72+
.padding(top = 16.dp, bottom = 8.dp, start = 32.dp, end = 32.dp)
7373
) {
74-
state.result.arrivals.forEach {
75-
ArrivalRow(it)
74+
val rowHeight = maxHeight / 3
75+
val textSize = (rowHeight * textScale).value.sp
76+
77+
Column(
78+
modifier = Modifier.fillMaxSize(),
79+
verticalArrangement = Arrangement.SpaceEvenly
80+
) {
81+
state.result.arrivals.forEach {
82+
ArrivalRow(it, textSize)
83+
}
7684
}
7785
}
7886
Row(
7987
modifier = Modifier
8088
.background(color = Footer)
8189
.padding(start = 32.dp, end = 28.dp)
8290
.fillMaxWidth()
83-
.height(70.dp),
91+
.height(footerHeight),
8492
horizontalArrangement = Arrangement.SpaceBetween,
8593
verticalAlignment = Alignment.CenterVertically
8694
) {
@@ -121,46 +129,52 @@ private fun Data(state: ArrivalsState.Data, onClickRefresh: () -> Unit) {
121129

122130
@Composable
123131
fun Error(message: String) {
124-
Column(
125-
modifier = Modifier
126-
.padding(32.dp)
127-
.fillMaxWidth(),
128-
verticalArrangement = Arrangement.spacedBy(32.dp)
132+
BoxWithConstraints(
133+
modifier = Modifier.fillMaxSize()
134+
.padding(top = 16.dp, bottom = 8.dp, start = 32.dp, end = 32.dp)
129135
) {
130-
LedText(message)
136+
val rowHeight = (maxHeight - footerHeight) / 3 // Match text size in Data composable
137+
val textSize = (rowHeight * textScale).value.sp
138+
139+
Column(
140+
modifier = Modifier.fillMaxSize(),
141+
verticalArrangement = Arrangement.SpaceEvenly
142+
) {
143+
LedText(message, textSize)
144+
}
131145
}
132146
}
133147

134148
@Composable
135-
fun ArrivalRow(arrival: Arrival) {
149+
fun ArrivalRow(arrival: Arrival, textSize: TextUnit) {
136150
Row(
137151
horizontalArrangement = Arrangement.SpaceBetween,
138152
modifier = Modifier.fillMaxWidth()
139153
) {
140-
LedText(arrival.destination)
154+
LedText(arrival.destination, textSize)
141155
if (arrival.secondsToStop < 60) {
142-
FlashingLedText(arrival.time)
156+
FlashingLedText(arrival.time, textSize)
143157
} else {
144-
LedText(arrival.time)
158+
LedText(arrival.time, textSize)
145159
}
146160
}
147161
}
148162

149163
@Composable
150-
fun LedText(string: String) {
164+
fun LedText(string: String, textSize: TextUnit, color: Color = LedYellow) {
151165
Text(
152166
text = string,
153-
color = LedYellow,
167+
color = color,
154168
maxLines = 1,
155169
style = TextStyle(
156170
fontFamily = LurFontFamily,
157-
fontSize = 58.sp
171+
fontSize = textSize
158172
)
159173
)
160174
}
161175

162176
@Composable
163-
fun FlashingLedText(string: String) {
177+
fun FlashingLedText(string: String, textSize: TextUnit) {
164178
val infiniteTransition = rememberInfiniteTransition()
165179
val alpha by infiniteTransition.animateFloat(
166180
initialValue = 0f,
@@ -170,13 +184,8 @@ fun FlashingLedText(string: String) {
170184
repeatMode = RepeatMode.Reverse
171185
)
172186
)
173-
Text(
174-
text = string,
175-
color = LedYellow.copy(alpha = alpha),
176-
maxLines = 1,
177-
style = TextStyle(
178-
fontFamily = LurFontFamily,
179-
fontSize = 58.sp
180-
)
181-
)
187+
LedText(string, textSize, LedYellow.copy(alpha = alpha))
182188
}
189+
190+
private val textScale = 0.6f
191+
private val footerHeight = 70.dp

0 commit comments

Comments
 (0)