Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

fix: keep PDF toolbar chips above carousel clipping

Chethan61697b8b9e702c3
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/components/ViewerGlass.kt b/app/src/main/java/com/chethan616/clearpdf/ui/components/ViewerGlass.kt
index fafa27a..da0365d 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/components/ViewerGlass.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/components/ViewerGlass.kt
@@ -1,6 +1,19 @@
package com.chethan616.clearpdf.ui.components
import androidx.compose.foundation.ScrollState
+import androidx.compose.foundation.gestures.Orientation
+import androidx.compose.foundation.gestures.rememberScrollableState
+import androidx.compose.foundation.gestures.scrollable
+import androidx.compose.foundation.layout.PaddingValues
+import androidx.compose.foundation.layout.calculateStartPadding
+import androidx.compose.foundation.layout.calculateEndPadding
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.Stable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableFloatStateOf
+import androidx.compose.runtime.mutableIntStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.BlendMode
@@ -9,6 +22,8 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.CompositingStrategy
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.graphicsLayer
+import androidx.compose.ui.layout.Layout
+import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.kyant.backdrop.Backdrop
@@ -17,6 +32,7 @@ import com.kyant.backdrop.effects.blur
import com.kyant.backdrop.effects.lens
import com.kyant.backdrop.effects.vibrancy
import com.kyant.shapes.RoundedRectangle
+import kotlin.math.roundToInt
/**
* The PDF viewer top bar's glass, as a modifier for surfaces that are not buttons.
@@ -62,6 +78,91 @@ fun Modifier.viewerGlass(
onDrawSurface = { drawRect(color) }
)
+/**
+ * Scroll metrics for a carousel whose interactive children must be able to paint beyond the
+ * scrolling viewport. Unlike [ScrollState], this state is intentionally independent of
+ * `horizontalScroll`: that modifier installs a clipping layer around its children, which turns a
+ * pressed liquid chip into a flat-cut shape at the edge of the viewport.
+ */
+@Stable
+class ViewerCarouselState internal constructor() {
+ var value by mutableFloatStateOf(0f)
+ private set
+
+ var maxValue by mutableIntStateOf(0)
+ private set
+
+ internal fun updateMaxValue(newMaxValue: Int) {
+ maxValue = newMaxValue.coerceAtLeast(0)
+ if (value > maxValue) value = maxValue.toFloat()
+ }
+
+ internal fun consumeScrollDelta(delta: Float): Float {
+ val previous = value
+ value = (value + delta).coerceIn(0f, maxValue.toFloat())
+ return value - previous
+ }
+}
+
+@Composable
+fun rememberViewerCarouselState(): ViewerCarouselState = remember { ViewerCarouselState() }
+
+/**
+ * A horizontally scrolling strip with an overflow-safe interactive layer.
+ *
+ * `scrollable` handles touch, mouse, accessibility and fling without imposing the child clipping
+ * used by `horizontalScroll`. The small layout below measures the strip unbounded, updates the
+ * scroll range, and translates the actual buttons itself. The parent therefore remains free to
+ * draw the full liquid-button shape while the carousel keeps the same edge fade.
+ */
+@Composable
+fun ViewerOverflowCarousel(
+ modifier: Modifier = Modifier,
+ state: ViewerCarouselState = rememberViewerCarouselState(),
+ contentPadding: PaddingValues = PaddingValues(0.dp),
+ itemSpacing: Dp = 0.dp,
+ content: @Composable () -> Unit
+) {
+ val scrollableState = rememberScrollableState { delta -> state.consumeScrollDelta(delta) }
+ Layout(
+ modifier = modifier
+ .carouselEdges(state, clipContent = false)
+ .scrollable(scrollableState, Orientation.Horizontal),
+ content = content
+ ) { measurables, constraints ->
+ val start = contentPadding.calculateStartPadding(layoutDirection).roundToPx()
+ val end = contentPadding.calculateEndPadding(layoutDirection).roundToPx()
+ val left = if (layoutDirection == androidx.compose.ui.unit.LayoutDirection.Ltr) start else end
+ val right = if (layoutDirection == androidx.compose.ui.unit.LayoutDirection.Ltr) end else start
+ val top = contentPadding.calculateTopPadding().roundToPx()
+ val bottom = contentPadding.calculateBottomPadding().roundToPx()
+ val spacing = itemSpacing.roundToPx()
+ val childConstraints = constraints.copy(
+ minWidth = 0,
+ maxWidth = Constraints.Infinity,
+ minHeight = 0,
+ maxHeight = (constraints.maxHeight - top - bottom).coerceAtLeast(0)
+ )
+ val placeables = measurables.map { it.measure(childConstraints) }
+ val childrenWidth = placeables.sumOf { it.width }
+ val gapsWidth = spacing * (placeables.size - 1).coerceAtLeast(0)
+ val contentWidth = left + childrenWidth + gapsWidth + right
+ val width = if (constraints.maxWidth == Constraints.Infinity) contentWidth else constraints.maxWidth
+ val contentHeight = placeables.maxOfOrNull { it.height } ?: 0
+ val height = (top + contentHeight + bottom).coerceIn(constraints.minHeight, constraints.maxHeight)
+ state.updateMaxValue((contentWidth - width).coerceAtLeast(0))
+
+ layout(width, height) {
+ var x = left - state.value.roundToInt()
+ placeables.forEach { placeable ->
+ val y = top + ((height - top - bottom - placeable.height) / 2).coerceAtLeast(0)
+ placeable.placeRelative(x, y)
+ x += placeable.width + spacing
+ }
+ }
+ }
+}
+
/**
* Soft, scroll-aware edge mask for a horizontally scrolling strip that rides on a [viewerGlass]
* surface.
@@ -100,6 +201,21 @@ fun Modifier.carouselEdges(
// Keep the fade mask, but allow a caller with its own overflow-safe layer to opt out of
// clipping the interactive children to the viewport's rounded outline.
clipContent: Boolean = true
+): Modifier = carouselEdges(state.value, state.maxValue, shape, fade, clipContent)
+
+fun Modifier.carouselEdges(
+ state: ViewerCarouselState,
+ shape: Shape = ViewerGlassShape,
+ fade: Dp = 18.dp,
+ clipContent: Boolean = true
+): Modifier = carouselEdges(state.value.roundToInt(), state.maxValue, shape, fade, clipContent)
+
+private fun Modifier.carouselEdges(
+ value: Int,
+ max: Int,
+ shape: Shape,
+ fade: Dp,
+ clipContent: Boolean
): Modifier = this
// One layer does both jobs. `Offscreen` is not decoration: it is what makes the `DstIn` below
// mask this strip rather than punch a hole through everything already on the canvas.
@@ -114,12 +230,11 @@ fun Modifier.carouselEdges(
val fadePx = fade.toPx()
if (w <= fadePx * 2f) return@drawWithContent
- val max = state.maxValue
// `maxValue` is Int.MAX_VALUE until the strip has been measured. Treated as "nothing to
// scroll", otherwise a row that fits flashes a trailing fade on its first frame.
val scrollable = max in 1..<Int.MAX_VALUE
- val lead = if (scrollable) (state.value / fadePx).coerceIn(0f, 1f) else 0f
- val trail = if (scrollable) ((max - state.value) / fadePx).coerceIn(0f, 1f) else 0f
+ val lead = if (scrollable) (value / fadePx).coerceIn(0f, 1f) else 0f
+ val trail = if (scrollable) ((max - value) / fadePx).coerceIn(0f, 1f) else 0f
if (lead == 0f && trail == 0f) return@drawWithContent
val edge = fadePx / w
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerBottomToolbar.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerBottomToolbar.kt
index 77ada9a..72d89f5 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerBottomToolbar.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerBottomToolbar.kt
@@ -27,11 +27,11 @@ import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.background
-import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
@@ -40,7 +40,6 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
-import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicText
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowForward
@@ -86,8 +85,9 @@ import com.chethan616.clearpdf.ui.components.GlassMotion
import com.chethan616.clearpdf.ui.components.LiquidButton
import com.chethan616.clearpdf.ui.components.LiquidIconButton
import com.chethan616.clearpdf.ui.components.ShareMorphButton
-import com.chethan616.clearpdf.ui.components.carouselEdges
+import com.chethan616.clearpdf.ui.components.ViewerOverflowCarousel
import com.chethan616.clearpdf.ui.components.viewerGlass
+import com.chethan616.clearpdf.ui.components.rememberViewerCarouselState
import com.chethan616.clearpdf.ui.utils.UISensor
import com.chethan616.clearpdf.utils.DocKind
import com.kyant.backdrop.backdrops.LayerBackdrop
@@ -311,15 +311,12 @@ internal fun PdfViewerBottomToolbar(
// the pen / shapes / OCR buttons slide away behind the capsule curve instead of
// being chopped by a hard vertical line. Content padding keeps the first/last
// button spaced like the rest.
- val toolRowScroll = rememberScrollState()
- Row(
- modifier = Modifier
- .weight(1f)
- .carouselEdges(toolRowScroll, clipContent = false)
- .horizontalScroll(toolRowScroll)
- .padding(horizontal = 12.dp, vertical = 8.dp),
- horizontalArrangement = Arrangement.spacedBy(8.dp),
- verticalAlignment = Alignment.CenterVertically
+ val toolRowScroll = rememberViewerCarouselState()
+ ViewerOverflowCarousel(
+ modifier = Modifier.weight(1f),
+ state = toolRowScroll,
+ contentPadding = PaddingValues(horizontal = 12.dp, vertical = 16.dp),
+ itemSpacing = 8.dp
) {
when {
showDrawTools -> {
@@ -438,15 +435,12 @@ internal fun PdfViewerBottomToolbar(
// so the toolbar stays compact instead of stacking rows. Same curved-edge fade as the
// tool row above, so the colour beads disappear behind the capsule curve rather than a
// straight cut.
- val attrRowScroll = rememberScrollState()
- Row(
- Modifier
- .fillMaxWidth()
- .carouselEdges(attrRowScroll, clipContent = false)
- .horizontalScroll(attrRowScroll)
- .padding(horizontal = 12.dp, vertical = 8.dp),
- horizontalArrangement = Arrangement.spacedBy(8.dp),
- verticalAlignment = Alignment.CenterVertically
+ val attrRowScroll = rememberViewerCarouselState()
+ ViewerOverflowCarousel(
+ modifier = Modifier.fillMaxWidth(),
+ state = attrRowScroll,
+ contentPadding = PaddingValues(horizontal = 12.dp, vertical = 16.dp),
+ itemSpacing = 8.dp
) {
if (showDrawTools) {
listOf("S" to 3f, "M" to 6f, "L" to 11f, "XL" to 18f).forEach { (label, w) ->
@@ -559,24 +553,18 @@ internal fun PdfViewerBottomToolbar(
} else if (docKind == DocKind.Word) {
// Curated Word reading/markup set — Select Text, Highlight, Find. No PDF-centric
// shapes / add-image / text-box / note / eraser / sign.
- val wordScroll = rememberScrollState()
+ val wordScroll = rememberViewerCarouselState()
ViewerGlassOverflowSurface(
modifier = Modifier.fillMaxWidth()
.graphicsLayer { scaleX = toolCompress; transformOrigin = TransformOrigin(0f, 0.5f) },
backdrop = backdrop,
color = pillGlass
) {
- Row(
- Modifier
- .fillMaxWidth()
- // Keep the existing fade mask and scroll behavior; the extra vertical
- // breathing room is what lets a pressed chip deform without touching the
- // viewport's top or bottom edge.
- .carouselEdges(wordScroll, clipContent = false)
- .horizontalScroll(wordScroll)
- .padding(horizontal = 12.dp, vertical = 10.dp),
- horizontalArrangement = Arrangement.spacedBy(8.dp),
- verticalAlignment = Alignment.CenterVertically
+ ViewerOverflowCarousel(
+ modifier = Modifier.fillMaxWidth(),
+ state = wordScroll,
+ contentPadding = PaddingValues(horizontal = 12.dp, vertical = 16.dp),
+ itemSpacing = 8.dp
) {
fun wSurface(color: Color, active: Boolean) = color.copy(if (active) 0.96f else 0.80f)
val selOn = activeTool == PdfEditTool.SelectText
@@ -602,23 +590,18 @@ internal fun PdfViewerBottomToolbar(
}
}
} else {
- val toolScroll = rememberScrollState()
+ val toolScroll = rememberViewerCarouselState()
ViewerGlassOverflowSurface(
modifier = Modifier.fillMaxWidth()
.graphicsLayer { scaleX = toolCompress; transformOrigin = TransformOrigin(0f, 0.5f) },
backdrop = backdrop,
color = pillGlass
) {
- Row(
- Modifier
- .fillMaxWidth()
- // The existing carouselEdges mask remains unchanged; this row is simply
- // rendered above the separate glass sibling so its chips can overflow cleanly.
- .carouselEdges(toolScroll, clipContent = false)
- .horizontalScroll(toolScroll)
- .padding(horizontal = 12.dp, vertical = 10.dp),
- horizontalArrangement = Arrangement.spacedBy(8.dp),
- verticalAlignment = Alignment.CenterVertically
+ ViewerOverflowCarousel(
+ modifier = Modifier.fillMaxWidth(),
+ state = toolScroll,
+ contentPadding = PaddingValues(horizontal = 12.dp, vertical = 16.dp),
+ itemSpacing = 8.dp
) {
// Each tool wears its own professional colour (like the Sign button):
// a solid colour chip, brighter when active. White ink reads on all of them.