Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.
Rust androiddocument-scannerdocximage-editorjetpack-composekotlinliquid-glassofflineopen-sourcepdfpdf-compressorpdf-editorpdf-mergerpdf-readerpdf-splitterpdf-toolspdf-viewerpptxspreadsheet-editorxlsx
fix: polish PDF markup selection and glass toolbars
Chethan616cb204a7ea709
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.ktindex 344f350..fd6268a 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@@ -63,14 +63,15 @@ fun Modifier.viewerGlass()/**- * Rounded clip plus a soft, scroll-aware edge mask for a horizontally scrolling strip that rides on a- * [viewerGlass] surface.+ * Soft, scroll-aware edge mask for a horizontally scrolling strip that rides on a [viewerGlass]+ * surface.** **The bug this exists to kill.** `Modifier.horizontalScroll` clips to its node's *rectangular** bounds, and [viewerGlass] only ever *draws* its rounded shape — `drawBackdrop` paints, it does not* clip. A scrolling row of chips on glass was therefore chopped by a straight vertical line sitting- * inside the capsule's own curve: the one thing a floating glass control must never look like. With- * this, chips slide in and out *behind* the container's curved ends instead.+ * inside the capsule's own curve: the one thing a floating glass control must never look like. The+ * mask below is a draw-time fade only — it deliberately does not hard-clip to the glass shape — so+ * controls remain visually above the surface while they are being dragged.** Apply it **inside** the glass and **outside** the scroll, with the padding moved to the far end:*@@ -97,12 +98,13 @@ fun Modifier.carouselEdges(shape: Shape = ViewerGlassShape,fade: Dp = 18.dp): 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.+ // Keep the edge fade isolated. `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. `clip`+ // stays false on purpose; the glass panel owns the surface shape, while this scrolling content+ // remains a floating layer that is not chopped by the panel's corners..graphicsLayer {compositingStrategy = CompositingStrategy.Offscreen- clip = true- this.shape = shape+ clip = false}.drawWithContent {drawContent()diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfContinuousPage.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfContinuousPage.ktindex de3c452..9cc3139 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfContinuousPage.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfContinuousPage.kt@@ -187,8 +187,14 @@ internal fun PdfContinuousPage(}is PdfMarkup.TextBlockLineMarkup -> ocrBlocks.firstOrNull { it.id == markup.blockId }?.let { b ->val r = ocrTextRangeToRect(b, OcrTextRange(markup.blockId, markup.start, markup.end), frame)- val y = if (markup.strikeThrough) r.center.y else r.bottom - r.height * 0.10f- drawLine(markup.color.copy(markup.alpha), Offset(r.left, y), Offset(r.right, y), markup.width)+ val y = markup.textMarkupLineY(r) ?: return@let+ drawLine(+ color = markup.color.copy(markup.alpha),+ start = Offset(r.left, y),+ end = Offset(r.right, y),+ strokeWidth = markup.width.coerceIn(2f, 4f),+ cap = StrokeCap.Round+ )}is PdfMarkup.ImageMarkup -> {val r = Rect(min(markup.start.x, markup.end.x), min(markup.start.y, markup.end.y), max(markup.start.x, markup.end.x), max(markup.start.y, markup.end.y))@@ -262,6 +268,30 @@ internal fun PdfContinuousPage(}}}++ // OCR markups are anchored to extracted text rather than freely movable+ // geometry, so they get their own subtle focus ring instead of the generic+ // resize frame. The precise range remains visible and the action bubble below+ // supplies the delete affordance.+ marks.getOrNull(selectedMarkupIndex)+ ?.takeIf { it is PdfMarkup.TextBlockHighlightMarkup || it is PdfMarkup.TextBlockLineMarkup }+ ?.textMarkupRangeRect(ocrBlocks, frame)+ ?.let { r ->+ val focus = Color(0xFF0A84FF).copy(0.9f)+ val bounds = if (marks[selectedMarkupIndex] is PdfMarkup.TextBlockLineMarkup) {+ val y = marks[selectedMarkupIndex].textMarkupLineY(r) ?: r.bottom+ Rect(r.left - 5f, y - 5f, r.right + 5f, y + 5f)+ } else {+ r.inflate(3f)+ }+ drawRoundRect(+ focus,+ bounds.topLeft,+ bounds.size,+ CornerRadius(6f, 6f),+ style = Stroke(2f)+ )+ }}if (activeTool == PdfEditTool.SelectText) {@@ -343,7 +373,8 @@ internal fun PdfContinuousPage(val selBounds = sel?.movableBounds()if (selBounds != null && selBounds.inflate(30f).contains(down.position)) return@awaitEachGesture- val idx = marks.indexOfLast { it.hitTest(down.position) }+ val frame = Rect(0f, 0f, size.width.toFloat(), size.height.toFloat())+ val idx = marks.indexOfLast { it.hitTest(down.position, ocrBlocks, frame) }if (idx >= 0) {val hit = marks[idx]val up = waitForUpOrCancellation()@@ -360,7 +391,9 @@ internal fun PdfContinuousPage(is PdfMarkup.RectMarkup,is PdfMarkup.OvalMarkup,is PdfMarkup.LineMarkup,- is PdfMarkup.StrokeMarkup -> onSelectMarkup(idx)+ is PdfMarkup.StrokeMarkup,+ is PdfMarkup.TextBlockHighlightMarkup,+ is PdfMarkup.TextBlockLineMarkup -> onSelectMarkup(idx)else -> Unit}onShowControls()@@ -447,9 +480,21 @@ internal fun PdfContinuousPage(if (activeTool == PdfEditTool.Eraser) {Box(Modifier.matchParentSize().pointerInput(page) {- detectTapGestures { p -> val idx = marks.indexOfLast { it.hitTest(p) }; if (idx >= 0) marks.removeAt(idx); onInteraction() }+ detectTapGestures {+ p ->+ val frame = Rect(0f, 0f, size.width.toFloat(), size.height.toFloat())+ val idx = marks.indexOfLast { it.hitTest(p, ocrBlocks, frame) }+ if (idx >= 0) marks.removeAt(idx)+ onInteraction()+ }}.pointerInput(page) {- detectDragGestures(onDrag = { ch, _ -> ch.consume(); val idx = marks.indexOfLast { it.hitTest(ch.position) }; if (idx >= 0) marks.removeAt(idx); onInteraction() })+ detectDragGestures(onDrag = { ch, _ ->+ ch.consume()+ val frame = Rect(0f, 0f, size.width.toFloat(), size.height.toFloat())+ val idx = marks.indexOfLast { it.hitTest(ch.position, ocrBlocks, frame) }+ if (idx >= 0) marks.removeAt(idx)+ onInteraction()+ })})}@@ -711,6 +756,48 @@ internal fun PdfContinuousPage(}}}++ // Text highlights, underlines, and strike-throughs are precise OCR ranges, not+ // transformable shapes. When one is tapped, expose the same clear destructive action+ // used by the other professional markup tools.+ marks.getOrNull(selectedMarkupIndex)+ ?.takeIf { it is PdfMarkup.TextBlockHighlightMarkup || it is PdfMarkup.TextBlockLineMarkup }+ ?.textMarkupRangeRect(ocrBlocks, Rect(0f, 0f, csz.width, csz.height))+ ?.let { rangeRect ->+ val selected = marks[selectedMarkupIndex]+ val anchorRect = if (selected is PdfMarkup.TextBlockLineMarkup) {+ val y = selected.textMarkupLineY(rangeRect) ?: rangeRect.bottom+ Rect(rangeRect.left, y - 4f, rangeRect.right, y + 4f)+ } else rangeRect+ val density = LocalDensity.current+ val gapPx = with(density) { 10.dp.toPx() }+ val barHpx = with(density) { 44.dp.toPx() }+ val barWpx = with(density) { 96.dp.toPx() }+ val placeBelow = anchorRect.top < barHpx + gapPx+ val by = (if (placeBelow) anchorRect.bottom + gapPx else anchorRect.top - barHpx - gapPx)+ .coerceIn(0f, (csz.height - barHpx).coerceAtLeast(0f))+ val bx = ((anchorRect.left + anchorRect.right) / 2f - barWpx / 2f)+ .coerceIn(0f, (csz.width - barWpx).coerceAtLeast(0f))++ Row(+ Modifier+ .offset { IntOffset(bx.roundToInt(), by.roundToInt()) }+ .clip(RoundedCornerShape(22.dp))+ .background(Color(0xFF1C1F26).copy(0.97f))+ .border(1.dp, Color.White.copy(0.14f), RoundedCornerShape(22.dp))+ .padding(horizontal = 4.dp, vertical = 3.dp),+ verticalAlignment = Alignment.CenterVertically+ ) {+ BasicText(+ "Delete",+ style = TextStyle(Color(0xFFFF6B6B), 13.sp, FontWeight.SemiBold),+ modifier = Modifier+ .clip(RoundedCornerShape(18.dp))+ .clickable { onDeleteMarkup(selectedMarkupIndex) }+ .padding(horizontal = 16.dp, vertical = 9.dp)+ )+ }+ }}}}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.ktindex 62b0088..fe74414 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@@ -8,9 +8,9 @@ import androidx.compose.animation.animateColorAsStateimport androidx.compose.animation.core.animateDpAsStateimport androidx.compose.animation.core.animateFloatimport androidx.compose.animation.core.animateFloatAsState-import androidx.compose.ui.draw.clipToBoundsimport androidx.compose.ui.graphics.TransformOriginimport androidx.compose.ui.graphics.graphicsLayer+import androidx.compose.ui.zIndeximport androidx.compose.foundation.gestures.detectDragGesturesAfterLongPressimport androidx.compose.foundation.gestures.detectTapGesturesimport androidx.compose.material.icons.rounded.KeyboardArrowUp@@ -233,7 +233,7 @@ internal fun PdfViewerBottomToolbar(// bottom — so the tool panels and the "Editor Tools" pill never move.Box(Modifier.fillMaxWidth()) {Column(- Modifier.fillMaxWidth().align(Alignment.BottomCenter),+ Modifier.fillMaxWidth().align(Alignment.BottomCenter).zIndex(2f),verticalArrangement = Arrangement.spacedBy(8.dp)) {@@ -265,7 +265,6 @@ internal fun PdfViewerBottomToolbar(transformOrigin = TransformOrigin(0.5f, 1f)translationY = (1f - reveal) * 8.dp.toPx()}- .clipToBounds().graphicsLayer { scaleX = toolCompress; transformOrigin = TransformOrigin(0f, 0.5f) }.viewerGlass(backdrop, glass).padding(12.dp),verticalArrangement = Arrangement.spacedBy(12.dp)@@ -512,7 +511,7 @@ internal fun PdfViewerBottomToolbar(transformOrigin = TransformOrigin(0.5f, 1f)translationY = (1f - reveal) * 8.dp.toPx()}- .clipToBounds()+ .zIndex(1f)) {if (docKind == DocKind.Ppt) {// PowerPoint editing isn't available yet — a friendly placeholder instead of the@@ -751,7 +750,7 @@ internal fun PdfViewerBottomToolbar(visible = selectorGate && !showFindBar && !showSignaturePad,enter = fadeIn(tween(180)),exit = fadeOut(tween(FaceHandoffMillis)),- modifier = Modifier.align(Alignment.BottomEnd)+ modifier = Modifier.align(Alignment.BottomEnd).zIndex(3f)) {ShareMorphButton(backdrop = backdrop,@@ -764,5 +763,3 @@ internal fun PdfViewerBottomToolbar(}}}--diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerInternals.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerInternals.ktindex f2aac2a..074ce7d 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerInternals.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerInternals.kt@@ -123,6 +123,9 @@ internal sealed class PdfMarkup {val d = distToSegment(p, start, end)d <= width.coerceAtLeast(16f)}+ // OCR-anchored markups are hit-tested with the page's OCR geometry. Keep the+ // geometry-free overload conservative so callers that do not have that geometry+ // cannot accidentally select a whole page-sized annotation.is TextBlockHighlightMarkup -> falseis TextBlockLineMarkup -> falseis ImageMarkup -> {@@ -321,6 +324,68 @@ internal fun ocrTextRangeToRect(block: OcrTextBlock, range: OcrTextRange, frame:)}+/** Exact page-space bounds for an OCR-anchored markup. */+internal fun PdfMarkup.textMarkupRangeRect(+ blocks: List<OcrTextBlock>,+ frame: Rect+): Rect? = when (this) {+ is PdfMarkup.TextBlockHighlightMarkup -> blocks.firstOrNull { it.id == blockId }?.let { block ->+ expandedTextHighlightRect(+ ocrTextRangeToRect(block, OcrTextRange(blockId, start, end), frame)+ )+ }+ is PdfMarkup.TextBlockLineMarkup -> blocks.firstOrNull { it.id == blockId }?.let { block ->+ ocrTextRangeToRect(block, OcrTextRange(blockId, start, end), frame)+ }+ else -> null+}++/** Baseline-aware y-position used by both the on-screen renderer and PDF export. */+internal fun PdfMarkup.textMarkupLineY(rangeRect: Rect): Float? = when (this) {+ is PdfMarkup.TextBlockLineMarkup -> if (strikeThrough) {+ rangeRect.center.y+ } else {+ // PdfTextService exposes the glyph baseline as `bottom`. A small gap keeps the+ // underline below descenders instead of cutting through the glyphs, as the old+ // `bottom - 10%` placement did.+ rangeRect.bottom + (rangeRect.height * 0.08f).coerceIn(1.5f, 6f)+ }+ else -> null+}++/** A forgiving touch target around a precise OCR markup, matching professional PDF tools. */+internal fun PdfMarkup.textMarkupHitBounds(+ blocks: List<OcrTextBlock>,+ frame: Rect+): Rect? {+ val rangeRect = textMarkupRangeRect(blocks, frame) ?: return null+ return when (this) {+ is PdfMarkup.TextBlockHighlightMarkup -> rangeRect.inflate(8f)+ is PdfMarkup.TextBlockLineMarkup -> {+ val y = textMarkupLineY(rangeRect) ?: return null+ val verticalTouch = max(14f, width * 3f)+ Rect(+ rangeRect.left - 12f,+ y - verticalTouch,+ rangeRect.right + 12f,+ y + verticalTouch+ )+ }+ else -> null+ }+}++/** Hit-testing overload for OCR markups; other annotations retain their existing behavior. */+internal fun PdfMarkup.hitTest(+ point: Offset,+ blocks: List<OcrTextBlock>,+ frame: Rect+): Boolean = when (this) {+ is PdfMarkup.TextBlockHighlightMarkup,+ is PdfMarkup.TextBlockLineMarkup -> textMarkupHitBounds(blocks, frame)?.contains(point) == true+ else -> hitTest(point)+}+internal fun expandedTextHighlightRect(rect: Rect, verticalScale: Float = 1f): Rect {val padX = (rect.height * 0.05f).coerceIn(0.75f, 3f)val padTop = rect.height * 0.13f * verticalScale@@ -655,7 +720,7 @@ internal fun buildExportOverlays(ocrBlocks.firstOrNull { it.id == markup.blockId }?.let { b ->val range = OcrTextRange(markup.blockId, markup.start, markup.end)val r = ocrTextRangeToRect(b, range, Rect(0f, 0f, 1f, 1f))- val y = if (markup.strikeThrough) r.center.y else r.bottom - (r.bottom - r.top) * 0.1f+ val y = markup.textMarkupLineY(r) ?: return@letlist.add(ExportOverlay.LineShape(start = NormalizedPoint(r.left, y),