Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 80 additions & 5 deletions app/src/main/java/com/kmworks/kmreader/ui/components/Components.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.FilterQuality
Expand Down Expand Up @@ -75,10 +80,12 @@ import com.kmworks.kmreader.core.download.DownloadMark
import com.kmworks.kmreader.core.logging.AppLog
import com.kmworks.kmreader.data.Book
import com.kmworks.kmreader.data.Series
import com.kmworks.kmreader.ui.theme.cardBackground
import dagger.hilt.android.EntryPointAccessors
import java.net.URI

private val CoverShape = RoundedCornerShape(8.dp)
private val CoverCornerRadius = 8.dp
private val CoverShape = RoundedCornerShape(CoverCornerRadius)
/** Cover aspect ratios seen in this process, so a cover re-entering composition knows its ratio at once. */
private object CoverAspectCache {
private const val MAX_ENTRIES = 400
Expand All @@ -99,6 +106,37 @@ private object CoverAspectCache {
/** iOS `Color.gray.opacity(0.2)` for the loading skeleton fill. */
private const val PlaceholderFillAlpha = 0.2f

/**
* iOS `ThumbnailImage.borderOverlay`: a half-point `Color.primary` ring at 15 %, drawn above the cover and its
* overlay. Its comment calls it "the card edge where covers blend into the background": a dark cover has no visible
* edge against a dark page even with its white shadow (4 % white, 2.5 pt blur), and the pure-black theme made that
* the only edge a grid had. The light theme gets the same ring in black, which is what `Color.primary` is there.
*/
private val CoverEdgeWidth = 0.5.dp
private const val CoverEdgeAlpha = 0.15f

/**
* The [CoverEdgeWidth] ring, stroked *inside* the cover rect.
*
* `Modifier.border` traces the outline, so half the hairline fell outside the rect and whatever is painted after it —
* the text-overlay scrim that closes a card in overlay mode, the caption under a grid card — swallowed that half.
* That is why only the top, left and right of the ring survived and the bottom edge, the one that has to separate the
* card from the page, disappeared. Stroking the same path inset by half its width keeps the whole line inside the
* cover, where only a later sibling can cover it — which is why a card that draws its text overlay outside [CoverArt]
* draws this ring itself, after that overlay.
*/
private fun Modifier.coverEdgeRing(color: Color): Modifier = drawWithContent {
drawContent()
val w = CoverEdgeWidth.toPx()
drawRoundRect(
color = color,
topLeft = Offset(w / 2f, w / 2f),
size = Size(size.width - w, size.height - w),
cornerRadius = CornerRadius((CoverCornerRadius.toPx() - w / 2f).coerceAtLeast(0f)),
style = Stroke(width = w),
)
}


/**
* Cover art: gray placeholder with the reading icon until the image loads.
Expand Down Expand Up @@ -140,8 +178,15 @@ fun CoverArt(
shadowStyle: CoverShadowStyle = CoverShadowStyle.Platform,
/** Callback after the cover loads (horizontal cards extract the average-color tint; mirrors iOS ThumbnailTintColorCache). */
onCoverBitmap: ((Bitmap) -> Unit)? = null,
/**
* Draw the iOS `ThumbnailImage.borderOverlay` ring on the cover. A card that paints its text overlay *outside*
* [CoverArt] (a grid card in overlay mode) turns this off and rings the same rect itself after that overlay: the
* overlay's black scrim closes over the cover's bottom edge and would swallow the ring's bottom side.
*/
edge: Boolean = true,
) {
val display = LocalCardDisplay.current
val coverEdge = MaterialTheme.colorScheme.onBackground.copy(alpha = CoverEdgeAlpha)
// iOS semantics (ThumbnailImage + GridCardView): the slot never moves, and a card that draws the text overlay
// passes `preserveAspectRatioOverride: false`, i.e. it always crops to the slot whatever the "preserve cover
// ratio" switch says. Outside overlay mode the switch decides: fit inside the slot + hug the bottom, or fill.
Expand Down Expand Up @@ -410,9 +455,20 @@ fun CoverArt(
// iOS attaches the overlay to `framedImageCard`, which exists only once the image does; drawing
// the badge on the empty slot first is what makes it visibly jump onto the cover.
if (loadedAspect != null) overlay?.invoke(this)
// iOS `ThumbnailImage.borderOverlay`, above the content overlay and above the placeholder: the
// cover's own edge is the card edge in a grid, and on a dark page a dark cover had none at all.
// Not gated on `loadedAspect`: the frame is slot-sized until an aspect is known, which is exactly
// where the placeholder sits, so an unloaded grid already reads as cards (iOS rings its
// placeholderCard too).
if (edge) Box(Modifier.matchParentSize().coverEdgeRing(coverEdge))
}
}
}
// Nothing to frame (no cover URL, or the session loader isn't up yet): the placeholder owns the whole slot,
// so the ring follows the slot — the frame above can only ring inside it.
if (edge && (modelUrl == null || loader == null)) {
Box(Modifier.matchParentSize().coverEdgeRing(coverEdge))
}
}
}

Expand Down Expand Up @@ -488,6 +544,9 @@ fun SeriesGridCard(
reserveProgressSpace: Boolean = false,
) {
val display = LocalCardDisplay.current
// A grid card whose text sits on the cover paints that overlay outside CoverArt, so the cover ring has to be
// drawn here, after it, on the same rect (iOS puts `borderOverlay` above the content overlay for this reason).
val textOnCover = !display.coverOnly && display.textOverlay
Column(
modifier = modifier.combinedClickable(onClick = onClick, onLongClick = onLongClick),
) {
Expand All @@ -501,6 +560,7 @@ fun SeriesGridCard(
stableId = series.id,
followThumbnailPreference = true,
unread = series.unreadCount > 0,
edge = !textOnCover,
overlay = {
if (display.showUnreadIndicator && series.unreadCount > 0) {
UnreadCountBadge(
Expand All @@ -510,7 +570,7 @@ fun SeriesGridCard(
}
},
)
if (!display.coverOnly && display.textOverlay) {
if (textOnCover) {
CardTextOverlay(
title = series.title,
// Mirrors iOS SeriesCardView: oneshot detail line = oneshotLine + book.closed icon
Expand All @@ -519,6 +579,11 @@ fun SeriesGridCard(
subtitleLeadingIcon = series.oneshot,
downloadedMark = seriesDownloadedMark(series),
)
Box(
Modifier
.matchParentSize()
.coverEdgeRing(MaterialTheme.colorScheme.onBackground.copy(alpha = CoverEdgeAlpha)),
)
}
}
if (display.showProgressBar) {
Expand Down Expand Up @@ -594,6 +659,8 @@ fun BookGridCard(
reserveProgressSpace: Boolean = false,
) {
val display = LocalCardDisplay.current
// See SeriesGridCard: the ring has to be painted after a text overlay that lives outside CoverArt.
val textOnCover = !display.coverOnly && display.textOverlay
Column(
modifier = modifier.combinedClickable(onClick = onClick, onLongClick = onLongClick),
) {
Expand All @@ -607,13 +674,14 @@ fun BookGridCard(
stableId = book.id,
followThumbnailPreference = true,
unread = book.progress == null,
edge = !textOnCover,
overlay = {
if (book.progress == 1f) {
CompletedIndicatorBadge(modifier = Modifier.align(Alignment.TopEnd))
}
},
)
if (!display.coverOnly && display.textOverlay) {
if (textOnCover) {
// Mirrors iOS BookCardView: the oneshot subtitle is the author line (with a small book icon);
// otherwise the series title is shown per settings. The status line mirrors overlayDetail's
// statusContent (page count / progress / last-read time / colored status).
Expand All @@ -631,6 +699,11 @@ fun BookGridCard(
statusColor = statusColor,
downloadedMark = bookDownloadedMark(book),
)
Box(
Modifier
.matchParentSize()
.coverEdgeRing(MaterialTheme.colorScheme.onBackground.copy(alpha = CoverEdgeAlpha)),
)
}
}
if (display.showProgressBar) {
Expand Down Expand Up @@ -1013,7 +1086,7 @@ private val HorizontalCardShape = RoundedCornerShape(12.dp)

/**
* Horizontal card skeleton (mirrors iOS HorizontalCardSkeleton): corner radius 12, average cover color as the
* background (falling back to surface without a tint), padding 8, a √2 cover filling the card height (≈47.2×66.8dp),
* background (falling back to [cardBackground] without a tint), padding 8, a √2 cover filling the card height (≈47.2×66.8dp),
* and a text column one glyph-slack taller than the √2 slot (≈66.8dp, see [HorizontalCardTextHeight]).
*
* The card height is fixed — the √2 slot plus the glyph slack — so every card in a row is the same size. [status]
Expand Down Expand Up @@ -1047,7 +1120,9 @@ internal fun HorizontalCardContainer(
Unit
}
}
val fallback = MaterialTheme.colorScheme.surface
// iOS `HorizontalCardSkeleton` fills untinted cards with `Color.cardBackground`; taking `surface` here left the
// card body at #141416 on the pure-black page, so below the tint the card and the background were one tone.
val fallback = MaterialTheme.colorScheme.cardBackground
val background by animateColorAsState(targetValue = tint ?: fallback, label = "horizontalCardTint")
val palette = HorizontalCardPalette(
tinted = tint != null,
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/kmworks/kmreader/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package com.kmworks.kmreader.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance

// KMReader accent colors from the original: pure black in light mode / pure white in dark mode (monochrome scheme), with no brand color and no gradients.
private val LightColors = lightColorScheme(
Expand Down Expand Up @@ -74,6 +76,22 @@ private val DarkSoftColors = darkColorScheme(
outline = Color(0xFF34343A),
)

/** iOS `Color.cardBackground` in dark mode (#2C2C2E) — see [cardBackground]. */
private val CardBackgroundDark = Color(0xFF2C2C2E)

/**
* Fill of a card that sits on the page (iOS `Color.cardBackground`: white in light, #2C2C2E in dark).
*
* Cards used to take plain [ColorScheme.surface]. In the pure-black theme that is #141416, roughly 1.1:1 against
* #000000, so a card's own bottom edge dissolved into the page even where the card paints no shadow; iOS paints its
* cards at #2C2C2E (~1.6:1) for exactly that. The value is a fixed step rather than a `surfaceContainer*` role
* because the ladder above `surface` is already spoken for — dialog cards sit on `surfaceContainerHigh` and cards
* floating inside a dialog step up to `surfaceContainerHighest` (see AppModalDialog / KmDropdownMenu), and reusing
* that role here would make a page card collide with them in the light theme, where it is white.
*/
val ColorScheme.cardBackground: Color
get() = if (background.luminance() < 0.5f) CardBackgroundDark else surface

@Composable
fun KMReaderTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
Expand Down