nice border around images in the dark theme

This commit is contained in:
Sergey Chernov 2026-05-24 08:57:44 +03:00
parent 44a83a17dd
commit 316b10d015

View File

@ -43,8 +43,13 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithContent import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.CompositingStrategy
import androidx.compose.ui.graphics.StrokeCap import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.PointerEventPass import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.PointerType import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.input.pointer.pointerInput
@ -81,6 +86,7 @@ import net.sergeych.toread.fb2.Fb2TextSpan
import net.sergeych.toread.fb2.Fb2TextStyle import net.sergeych.toread.fb2.Fb2TextStyle
import net.sergeych.toread.text.HyphenationRegistry import net.sergeych.toread.text.HyphenationRegistry
import net.sergeych.toread.text.SoftHyphen import net.sergeych.toread.text.SoftHyphen
import kotlin.math.min
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
@ -579,6 +585,8 @@ private fun BookImage(
} }
val imageTitle = image?.alt?.ifBlank { null } ?: book.title val imageTitle = image?.alt?.ifBlank { null } ?: book.title
val imageBackgroundColor = readerImageBackgroundColor() val imageBackgroundColor = readerImageBackgroundColor()
val imageShape = RoundedCornerShape(8.dp)
val smoothImageEdges = imageBackgroundColor != MaterialTheme.colorScheme.surface
val density = LocalDensity.current val density = LocalDensity.current
BoxWithConstraints( BoxWithConstraints(
modifier = modifier modifier = modifier
@ -612,7 +620,12 @@ private fun BookImage(
} else { } else {
Modifier.fillMaxSize() Modifier.fillMaxSize()
} }
Box(imageModifier.background(imageBackgroundColor)) { Box(
imageModifier
.then(if (smoothImageEdges) Modifier.softImageEdgeFade() else Modifier)
.clip(imageShape)
.background(imageBackgroundColor),
) {
Image( Image(
bitmap = bitmap, bitmap = bitmap,
contentDescription = imageTitle, contentDescription = imageTitle,
@ -626,6 +639,59 @@ private fun BookImage(
} }
} }
private fun Modifier.softImageEdgeFade(): Modifier =
graphicsLayer {
compositingStrategy = CompositingStrategy.Offscreen
}.drawWithContent {
drawContent()
val fade = min(8.dp.toPx(), min(size.width, size.height) / 4f)
if (fade <= 0f) return@drawWithContent
drawRect(
brush = Brush.verticalGradient(
0f to Color.Transparent,
1f to Color.Black,
startY = 0f,
endY = fade,
),
size = Size(size.width, fade),
blendMode = BlendMode.DstIn,
)
drawRect(
brush = Brush.verticalGradient(
0f to Color.Black,
1f to Color.Transparent,
startY = size.height - fade,
endY = size.height,
),
topLeft = Offset(0f, size.height - fade),
size = Size(size.width, fade),
blendMode = BlendMode.DstIn,
)
drawRect(
brush = Brush.horizontalGradient(
0f to Color.Transparent,
1f to Color.Black,
startX = 0f,
endX = fade,
),
size = Size(fade, size.height),
blendMode = BlendMode.DstIn,
)
drawRect(
brush = Brush.horizontalGradient(
0f to Color.Black,
1f to Color.Transparent,
startX = size.width - fade,
endX = size.width,
),
topLeft = Offset(size.width - fade, 0f),
size = Size(fade, size.height),
blendMode = BlendMode.DstIn,
)
}
private fun Fb2Text.toAnnotatedString( private fun Fb2Text.toAnnotatedString(
language: String?, language: String?,
hyphenation: HyphenationRegistry, hyphenation: HyphenationRegistry,