Android: players above and below the board; White and Black on lines of their own

The game screen's top bar had both names on one line beside three buttons, so Black's
was cut; the games list split one line between them. Now (android-ui.md §6.2, §7.7, §7.11):
the player bars stand above and below the board (Black above, swapped with the board;
beside it in landscape), each with the side's disc, the name in full while it fits, the
rating and the share of the result; the top bar names the event, year and round, a tap
shows the game's details. List rows are 80 dp: White, Black, then event · year · ECO,
read by TalkBack as one sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-24 12:52:06 +03:00
parent 9734108b83
commit 7c383f6c93
5 changed files with 339 additions and 97 deletions

View file

@ -2,6 +2,16 @@ package io.github.vrubelroman.chesspenguin.ui
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.border
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
@ -21,6 +31,7 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.withStyle
import io.github.vrubelroman.chesspenguin.ChessPenguinApp
import io.github.vrubelroman.chesspenguin.R
import io.github.vrubelroman.chesspenguin.theme.Tabular
import io.github.vrubelroman.chesspenguin.data.Notation
import io.github.vrubelroman.chesspenguin.data.DbEntry
import io.github.vrubelroman.chesspenguin.data.Source
@ -89,6 +100,81 @@ fun shortName(name: String): String {
return if (initials.isEmpty()) surname else "$surname $initials"
}
/// A player's name (android-ui.md §7.11): in full while it fits, else "Surname F.", else
/// cut with "…". `abbreviate = false` keeps the text as it is (a study's title for White).
@Composable
fun PlayerName(
name: String,
style: TextStyle,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
maxLines: Int = 1,
abbreviate: Boolean = true,
) {
val measurer = rememberTextMeasurer()
BoxWithConstraints(modifier) {
val text = if (abbreviate && measurer.measure(name, style, softWrap = false).size.width > constraints.maxWidth) shortName(name) else name
Text(text, style = style, color = color, maxLines = maxLines, overflow = TextOverflow.Ellipsis)
}
}
/// A player's share of the result: "1", "0", "½", or null (unknown, unfinished).
fun sideResult(result: String, white: Boolean): String? = when (result) {
"1-0" -> if (white) "1" else "0"
"0-1" -> if (white) "0" else "1"
"1/2-1/2" -> "½"
else -> null
}
/// The rating of a player, or nothing ("0", "?", "").
fun elo(value: String): String = value.trim().takeIf { v -> v.toIntOrNull()?.let { it > 0 } == true } ?: ""
/// The colour of a side: a small disc (white with a rim, or black).
@Composable
fun SideDisc(white: Boolean) {
Box(
Modifier.size(12.dp).clip(CircleShape)
.background(if (white) Color(0xFFF5F5F0) else Color(0xFF26282B))
.border(1.dp, MaterialTheme.colorScheme.outline, CircleShape),
)
}
/// One player's line: name (giving way), Elo, the share of the result on the right.
@Composable
fun PlayerLine(
name: String,
elo: String,
result: String?,
style: TextStyle,
modifier: Modifier = Modifier,
disc: Boolean? = null,
nameColor: Color = Color.Unspecified,
maxLines: Int = 1,
abbreviate: Boolean = true,
) {
Row(modifier, verticalAlignment = Alignment.CenterVertically) {
if (disc != null) {
SideDisc(disc)
Spacer(Modifier.width(10.dp))
}
Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {
PlayerName(name, style, Modifier.weight(1f, fill = false), nameColor, maxLines, abbreviate)
if (elo.isNotEmpty()) {
Spacer(Modifier.width(8.dp))
Text(elo, style = MaterialTheme.typography.bodyMedium.merge(Tabular), color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 1)
}
}
Text(
result ?: "",
style = style.merge(Tabular),
color = if (result == "1") MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = androidx.compose.ui.text.style.TextAlign.End,
maxLines = 1,
modifier = Modifier.width(24.dp),
)
}
}
/// "1/2-1/2" → "½-½".
fun shortResult(result: String): String = when (result) {
"1/2-1/2" -> "½-½"

View file

@ -7,6 +7,7 @@ import android.view.HapticFeedbackConstants
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.indication
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
@ -232,8 +233,11 @@ fun GameScreen(db: String, startPlace: Int, query: String, onBack: () -> Unit, o
val g = game
val tag = { name: String -> g?.tags?.firstOrNull { it.name == name }?.value ?: "" }
val white = surname(tag("White"))
val black = surname(tag("Black"))
// The player bars above and below the board (§7.11); the top bar names the event.
val whiteName = tag("White").trim().takeIf { it.isNotEmpty() && it != "?" }
val blackName = tag("Black").trim().takeIf { it.isNotEmpty() && it != "?" }
val whiteLabel = stringResource(R.string.tag_white)
val blackLabel = stringResource(R.string.tag_black)
val resultText = g?.let { resultLine(context, it.result) } ?: ""
val (items, where) = remember(g, resultText) { g?.let { layout(it, resultText) } ?: (emptyList<Item>() to emptyMap()) }
val nodes = g?.nodes
@ -547,22 +551,34 @@ fun GameScreen(db: String, startPlace: Int, query: String, onBack: () -> Unit, o
IconButton(onClick = onBack) { Icon(Icons.AutoMirrored.Rounded.ArrowBack, stringResource(R.string.back)) }
},
title = {
Column {
// The event and year (else the site); the game's place and round. A tap
// shows the game's details.
val event = joinKnown(tag("Event"), year(tag("Date"))).ifEmpty { joinKnown(tag("Site")) }
val round = tag("Round").trim().takeIf { it.isNotEmpty() && it != "?" && it != "-" }
val placeLine = joinKnown(
if (total > 0) stringResource(R.string.game_of, place + 1, total) else "",
round?.let { stringResource(R.string.tag_round) + " " + it } ?: "",
)
Column(
Modifier.clickable(
enabled = g != null,
onClickLabel = stringResource(R.string.game_info),
role = Role.Button,
) { info = true },
) {
Text(
when {
g == null -> ""
black == "?" && white != "?" -> white
else -> "$white – $black"
},
event.ifEmpty { placeLine },
style = MaterialTheme.typography.titleMedium,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
if (total > 0) {
if (event.isNotEmpty() && placeLine.isNotEmpty()) {
Text(
joinKnown(stringResource(R.string.game_of, place + 1, total), g?.let { shortResult(it.result) }?.takeIf { it != "*" } ?: ""),
placeLine,
style = MaterialTheme.typography.bodyMedium.merge(Tabular),
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
}
@ -635,18 +651,35 @@ fun GameScreen(db: String, startPlace: Int, query: String, onBack: () -> Unit, o
bottomBar = { if (!landscape) controls(Modifier) },
) { padding ->
val inner = Modifier.fillMaxSize().padding(top = padding.calculateTopPadding(), bottom = padding.calculateBottomPadding())
// Black above the board, White below (swapped with the board, §7.11). A study with
// a title for White and no Black keeps both bars, so the board does not jump.
val study = whiteName != null && blackName == null
val result = g?.result ?: ""
val whiteBar: @Composable () -> Unit = {
PlayerBar(whiteName ?: whiteLabel, elo(tag("WhiteElo")), true, sideResult(result, true), known = whiteName != null, abbreviate = !study)
}
val blackBar: @Composable () -> Unit = {
PlayerBar(blackName ?: blackLabel, elo(tag("BlackElo")), false, sideResult(result, false), known = blackName != null)
}
val topBar = if (flipped) whiteBar else blackBar
val bottomBar = if (flipped) blackBar else whiteBar
if (landscape) {
// The navigation buttons stand on a side of the screen: nothing goes under them.
// The bars go beside the board, so it keeps the full height.
Row(inner.windowInsetsPadding(WindowInsets.navigationBars.only(WindowInsetsSides.Horizontal))) {
board(Modifier.fillMaxHeight())
Column(Modifier.weight(1f).fillMaxHeight()) {
topBar()
side(Modifier.weight(1f))
bottomBar()
controls(Modifier)
}
}
} else {
Column(inner) {
topBar()
board(Modifier.fillMaxWidth())
bottomBar()
side(Modifier.weight(1f))
}
}
@ -712,12 +745,10 @@ fun GameScreen(db: String, startPlace: Int, query: String, onBack: () -> Unit, o
)
}
if (reviewReport && rs != null) {
val whiteName = tag("White").takeIf { it.isNotBlank() && it != "?" } ?: stringResource(R.string.tag_white)
val blackName = tag("Black").takeIf { it.isNotBlank() && it != "?" } ?: stringResource(R.string.tag_black)
ReviewSheet(
state = rs,
white = whiteName,
black = blackName,
white = whiteName ?: whiteLabel,
black = blackName ?: blackLabel,
onError = { w, j ->
reviewReport = false
val hits = rs.moves.filter { it.white == w && it.judgement == j }
@ -898,7 +929,21 @@ private fun pgnDate(date: String): String {
}
/// "Carlsen, Magnus" → "Carlsen"; "Magnus Carlsen" stays as it is.
private fun surname(name: String): String = name.substringBefore(',').trim().ifEmpty { "?" }
/// A player above or below the board (§7.11): the side's disc, the name (full while it
/// fits), the rating, the share of the result. A missing name shows the side, greyed.
@Composable
private fun PlayerBar(name: String, elo: String, white: Boolean, result: String?, known: Boolean, abbreviate: Boolean = true) {
PlayerLine(
name = name,
elo = elo,
result = result,
style = MaterialTheme.typography.titleSmall,
modifier = Modifier.fillMaxWidth().heightIn(min = 36.dp).padding(horizontal = 16.dp),
disc = white,
nameColor = if (known) androidx.compose.ui.graphics.Color.Unspecified else MaterialTheme.colorScheme.onSurfaceVariant,
abbreviate = abbreviate,
)
}
/// "1-0 · White won" for the end of the notation.
private fun resultLine(context: Context, result: String): String = when (result) {

View file

@ -50,6 +50,7 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.ImeAction
@ -276,27 +277,30 @@ private fun numberWidth(count: Int): androidx.compose.ui.unit.Dp {
}
}
/// One game: its number in the database, "White – Black" and the result, then event ·
/// year · ECO (§6.2). An unfinished game ("*", most book positions) shows no result.
/// One game (§6.2): its number, White and Black each on a line of their own (with the
/// rating and the share of the result), then event · year · ECO. The row is read as one
/// sentence by TalkBack.
@Composable
private fun GameRowView(number: Int, numberWidth: androidx.compose.ui.unit.Dp, row: GameRow?, onClick: () -> Unit) {
val said = row?.let { rowDescription(number, it) } ?: ""
Row(
Modifier
.fillMaxWidth()
.height(64.dp)
.height(80.dp)
.clickable(enabled = row != null, onClick = onClick)
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically,
.clearAndSetSemantics { if (row != null) contentDescription = said }
.padding(horizontal = 16.dp, vertical = 6.dp),
verticalAlignment = Alignment.Top,
) {
val said = stringResource(R.string.game_number, groupDigits(number))
Text(
groupDigits(number),
style = MaterialTheme.typography.bodySmall.merge(Tabular),
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.End,
maxLines = 1,
modifier = Modifier.width(numberWidth).semantics { contentDescription = said },
)
// Level with White's line.
Box(Modifier.width(numberWidth).height(24.dp), contentAlignment = Alignment.CenterEnd) {
Text(
groupDigits(number),
style = MaterialTheme.typography.bodySmall.merge(Tabular),
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
)
}
Spacer(Modifier.width(12.dp))
GameRowText(row, Modifier.weight(1f))
}
@ -304,48 +308,52 @@ private fun GameRowView(number: Int, numberWidth: androidx.compose.ui.unit.Dp, r
@Composable
private fun GameRowText(row: GameRow?, modifier: Modifier) {
Column(modifier, verticalArrangement = Arrangement.Center) {
Column(modifier) {
if (row == null) return@Column
Row(verticalAlignment = Alignment.CenterVertically) {
Row(Modifier.weight(1f)) {
Text(
shortName(row.white),
style = MaterialTheme.typography.bodyLarge,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f, fill = false),
)
// A study or a book position often has a title for White and no Black.
if (shortName(row.black) != "?" || shortName(row.white) == "?") {
Text(" – ", style = MaterialTheme.typography.bodyLarge)
Text(
shortName(row.black),
style = MaterialTheme.typography.bodyLarge,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f, fill = false),
)
}
}
val result = shortResult(row.result)
if (result != "*") {
Text(
result,
style = MaterialTheme.typography.bodyLarge.merge(Tabular),
textAlign = TextAlign.End,
modifier = Modifier.width(48.dp).padding(start = 8.dp),
)
}
val style = MaterialTheme.typography.bodyLarge
val muted = MaterialTheme.colorScheme.onSurfaceVariant
val white = row.white.trim().takeIf { it.isNotEmpty() && it != "?" }
val black = row.black.trim().takeIf { it.isNotEmpty() && it != "?" }
if (white != null && black == null) {
// A study or a book position: a title for White and no Black — two lines of it.
PlayerLine(white, elo(row.whiteElo), null, style, Modifier.height(48.dp), maxLines = 2, abbreviate = false)
} else {
PlayerLine(white ?: "?", elo(row.whiteElo), sideResult(row.result, true), style, Modifier.height(24.dp), nameColor = if (white == null) muted else androidx.compose.ui.graphics.Color.Unspecified)
PlayerLine(black ?: "?", elo(row.blackElo), sideResult(row.result, false), style, Modifier.height(24.dp), nameColor = if (black == null) muted else androidx.compose.ui.graphics.Color.Unspecified)
}
val details = joinKnown(row.event, year(row.date), row.eco)
if (details.isNotEmpty()) {
Text(
details,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
color = muted,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.height(20.dp),
)
}
}
}
/// "No. 12. White: Carlsen, Magnus, 2830. Black: … . 1-0, White won. Event · 2025 · C42."
@Composable
private fun rowDescription(number: Int, row: GameRow): String {
val player = { label: String, name: String, rating: String ->
"$label: " + joinKnownComma(name.trim().takeIf { it.isNotEmpty() } ?: "?", elo(rating))
}
val result = when (row.result) {
"1-0" -> "1-0, " + stringResource(R.string.white_won)
"0-1" -> "0-1, " + stringResource(R.string.black_won)
"1/2-1/2" -> "½-½, " + stringResource(R.string.draw)
else -> ""
}
return listOf(
stringResource(R.string.game_number, groupDigits(number)),
player(stringResource(R.string.tag_white), row.white, row.whiteElo),
player(stringResource(R.string.tag_black), row.black, row.blackElo),
result,
joinKnown(row.event, year(row.date), row.eco),
).filter { it.isNotEmpty() }.joinToString(". ")
}
private fun joinKnownComma(vararg parts: String): String = parts.filter { it.isNotEmpty() }.joinToString(", ")