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
feat: add custom PDF selection handles
Chethan6166053197f99c1
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 8b87aeb..de3c452 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@@ -113,6 +113,8 @@ internal fun PdfContinuousPage(var draftRectEnd by remember(page, activeTool) { mutableStateOf<Offset?>(null) }var selDragStart by remember(page, activeTool) { mutableStateOf<Offset?>(null) }var selDragEnd by remember(page, activeTool) { mutableStateOf<Offset?>(null) }+ val selectionHandleDiameterPx = with(LocalDensity.current) { 32.dp.toPx() }+ val selectionHandleHitRadiusPx = with(LocalDensity.current) { 30.dp.toPx() }// Page layout (rebuilt on the Pdf_Tools model): the image is drawn at its TRUE// aspect via ContentScale.FillWidth, so the box height follows the bitmap. There@@ -152,10 +154,10 @@ internal fun PdfContinuousPage(selectedOcrRanges.forEach { range ->ocrBlocks.firstOrNull { it.id == range.blockId }?.let { b ->val r = expandedTextHighlightRect(ocrTextRangeToRect(b, range, frame), verticalScale = 1.35f)- // Match the find bar's tight word geometry, with a little more vertical air so- // the selected glyphs remain legible at normal phone sizes.- drawRoundRect(Color(0xFF2F80ED).copy(0.62f), r.topLeft, r.size, CornerRadius((r.height * 0.14f).coerceIn(2f, 5f), (r.height * 0.14f).coerceIn(2f, 5f)))- drawRoundRect(Color(0xFF60A5FA).copy(0.96f), r.topLeft, r.size, CornerRadius((r.height * 0.14f).coerceIn(2f, 5f), (r.height * 0.14f).coerceIn(2f, 5f)), style = Stroke(1.25f))+ // Match the reference's soft cyan fill: no border, no font changes, and+ // enough vertical air for ascenders/descenders without covering other lines.+ val radius = (r.height * 0.14f).coerceIn(2f, 5f)+ drawRoundRect(Color(0xFF9ADBF0).copy(0.82f), r.topLeft, r.size, CornerRadius(radius, radius))}}@@ -262,6 +264,13 @@ internal fun PdfContinuousPage(}}+ if (activeTool == PdfEditTool.SelectText) {+ ocrSelectionHandleAnchors(ocrBlocks, selectedOcrRanges, frame)?.let { (start, end) ->+ drawTextSelectionHandle(start, selectionHandleDiameterPx)+ drawTextSelectionHandle(end, selectionHandleDiameterPx)+ }+ }+// In-progress draftsif (draftPoints.size > 1) {val isHl = activeTool == PdfEditTool.Highlight@@ -510,13 +519,14 @@ internal fun PdfContinuousPage(// Direct drag (no long-press wait): sweep a contiguous run of words in reading order// and update the range live. The page stays in text-selection mode, while the parent// viewer still receives two-finger pinch events for zoom.- .pointerInput(page, ocrBlocks) {+ .pointerInput(page, ocrBlocks, selectedOcrRanges, selectionHandleHitRadiusPx) {fun updateRange() {val s = selDragStart; val e = selDragEndif (s == null || e == null || ocrBlocks.isEmpty()) returnval frame = Rect(0f, 0f, size.width.toFloat(), size.height.toFloat())onSelectOcrRange(ocrWordRangesBetween(ocrBlocks, frame, s, e))}+// Do not use detectDragGestures here: it commits to a one-finger drag before// the second finger of a pinch arrives. That is why zooming used to leave a// text selection behind. We wait for touch slop, then only consume while the@@ -524,6 +534,19 @@ internal fun PdfContinuousPage(// and leaves the event stream available to the viewer's pinch detector.awaitEachGesture {val down = awaitFirstDown(requireUnconsumed = false)+ val frame = Rect(0f, 0f, size.width.toFloat(), size.height.toFloat())+ val handles = ocrSelectionHandleAnchors(ocrBlocks, selectedOcrRanges, frame)+ val handleMode = when {+ handles == null -> 0+ (down.position - handles.first).getDistance() <= selectionHandleHitRadiusPx -> 1+ (down.position - handles.second).getDistance() <= selectionHandleHitRadiusPx -> 2+ else -> 0+ }+ val fixedHandlePoint = when (handleMode) {+ 1 -> handles?.second+ 2 -> handles?.first+ else -> null+ }val pointerId = down.idvar selecting = falsevar multiTouch = false@@ -542,12 +565,13 @@ internal fun PdfContinuousPage(if (change != null) {if (!selecting && (change.position - down.position).getDistance() > viewConfiguration.touchSlop) {selecting = true- selDragStart = down.position+ selDragStart = fixedHandlePoint ?: down.positionselDragEnd = change.positionupdateRange()onInteraction()} else if (selecting) {change.consume()+ selDragStart = fixedHandlePoint ?: down.positionselDragEnd = change.positionupdateRange()onInteraction()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 817ae25..f2aac2a 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@@ -328,6 +328,54 @@ internal fun expandedTextHighlightRect(rect: Rect, verticalScale: Float = 1f): Rreturn Rect(rect.left - padX, rect.top - padTop, rect.right + padX, rect.bottom + padBottom)}+internal fun ocrSelectionHandleAnchors(+ blocks: List<OcrTextBlock>,+ ranges: List<OcrTextRange>,+ frame: Rect+): Pair<Offset, Offset>? {+ if (ranges.isEmpty()) return null+ val blocksById = blocks.associateBy { it.id }+ val ordered = ranges.sortedWith(+ compareBy<OcrTextRange>(+ { blocksById[it.blockId]?.top ?: Float.MAX_VALUE },+ { blocksById[it.blockId]?.left ?: Float.MAX_VALUE },+ { it.start }+ )+ )+ val first = ordered.first()+ val last = ordered.last()+ val firstRect = expandedTextHighlightRect(+ ocrTextRangeToRect(blocksById[first.blockId] ?: return null, first, frame),+ verticalScale = 1.35f+ )+ val lastRect = expandedTextHighlightRect(+ ocrTextRangeToRect(blocksById[last.blockId] ?: return null, last, frame),+ verticalScale = 1.35f+ )+ return Offset(firstRect.left, firstRect.bottom) to Offset(lastRect.right, lastRect.bottom)+}++/** Custom organic handle used by the PDF selection layer; deliberately not a platform handle. */+internal fun DrawScope.drawTextSelectionHandle(+ anchor: Offset,+ diameter: Float,+ color: Color = Color(0xFF4285F4)+) {+ val neckHalfWidth = diameter * 0.22f+ val bodyRadius = diameter * 0.47f+ val top = anchor.y - 1f+ val bottom = top + bodyRadius * 2f+ val path = Path().apply {+ moveTo(anchor.x - neckHalfWidth, top)+ lineTo(anchor.x + neckHalfWidth, top)+ lineTo(anchor.x + neckHalfWidth, top + 6f)+ cubicTo(anchor.x + bodyRadius, top + 9f, anchor.x + bodyRadius, bottom - 2f, anchor.x, bottom)+ cubicTo(anchor.x - bodyRadius, bottom - 2f, anchor.x - bodyRadius, top + 9f, anchor.x - neckHalfWidth, top + 6f)+ close()+ }+ drawPath(path, color)+}+internal fun OcrTextBlock.wordRangeAtPoint(point: Offset, frame: Rect): OcrTextRange? {val blockRect = ocrBlockToRect(this, frame)if (!Rect(blockRect.left - 6f, blockRect.top - 6f, blockRect.right + 6f, blockRect.bottom + 6f).contains(point)) return null