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
Selection: avoid per-drag-step screen recomposition, hide toolbar when selection is off-screen, tighter word extension
Chethan6162f6d8d66320c
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.ktindex bd99c66..4e43e55 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt@@ -3,6 +3,7 @@ package com.chethan616.clearpdf.ui.screenimport android.app.Activityimport android.content.Intentimport androidx.compose.runtime.mutableIntStateOf+import androidx.compose.runtime.derivedStateOfimport android.graphics.Bitmapimport androidx.activity.compose.BackHandlerimport androidx.activity.compose.rememberLauncherForActivityResult@@ -592,7 +593,8 @@ fun PdfViewerScreen(val currentPageIndex = listState.firstVisibleItemIndex.coerceIn(0, safePageCount - 1)// Back clears an active text selection first (registered after the viewer's own BackHandler, so// it takes precedence), exactly like a TextView's selection mode.- BackHandler(enabled = textSelection.hasSelection) { textSelection.clear() }+ val selectionActive by remember { derivedStateOf { textSelection.hasSelection } }+ BackHandler(enabled = selectionActive) { textSelection.clear() }// Any editing tool owns the page gestures; a selection must not linger underneath it.LaunchedEffect(activeTool) { if (activeTool != PdfEditTool.None) textSelection.clear() }@@ -1316,9 +1318,13 @@ fun PdfViewerScreen(}})- val hasHighlightOverlap = textSelection.hasSelection && textSelection.rangesByPage().any { (page, ranges) ->- annotationsByPage[page].orEmpty().any { m ->- m is PdfMarkup.TextBlockHighlightMarkup && ranges.any { r -> r.blockId == m.blockId && m.start < r.end && m.end > r.start }+ // A lambda, evaluated inside the toolbar: reading the selection here would recompose this+ // whole screen on every handle-drag step.+ val hasHighlightOverlap: () -> Boolean = {+ textSelection.hasSelection && textSelection.rangesByPage().any { (page, ranges) ->+ annotationsByPage[page].orEmpty().any { m ->+ m is PdfMarkup.TextBlockHighlightMarkup && ranges.any { r -> r.blockId == m.blockId && m.start < r.end && m.end > r.start }+ }}}PdfSelectionToolbar(diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfSelectionOverlay.kt b/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfSelectionOverlay.ktindex 565dacd..cb1f278 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfSelectionOverlay.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfSelectionOverlay.kt@@ -13,6 +13,7 @@ import androidx.compose.foundation.lazy.LazyListStateimport androidx.compose.foundation.magnifierimport androidx.compose.foundation.text.selection.LocalTextSelectionColorsimport androidx.compose.runtime.Composable+import androidx.compose.runtime.derivedStateOfimport androidx.compose.runtime.getValueimport androidx.compose.runtime.rememberimport androidx.compose.runtime.rememberCoroutineScope@@ -241,6 +242,8 @@ fun PdfSelectionHandles(}val minTouchPx = with(density) { 44.dp.toPx() }+ // Derived so a drag (which changes anchor/focus every frame) doesn't recompose this overlay.+ val hasSelection by remember(state) { derivedStateOf { state.hasSelection } }val startDesc = stringResource(R.string.selection_start_handle)val endDesc = stringResource(R.string.selection_end_handle)@@ -254,7 +257,7 @@ fun PdfSelectionHandles(// Platform loupe (API 28+; a no-op below). Inactive while the centre is Unspecified..magnifier(sourceCenter = { state.magnifierCenter })) {- if (!state.hasSelection) return@Box+ if (!hasSelection) return@BoxCanvas(Modifier.fillMaxSize()) {// Subscribe to everything that moves page geometry, so the handles are redrawn in the samediff --git a/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfSelectionToolbar.kt b/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfSelectionToolbar.ktindex 35df187..206899e 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfSelectionToolbar.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfSelectionToolbar.kt@@ -159,7 +159,7 @@ fun PdfSelectionToolbar(backdrop: Backdrop,actions: PdfSelectionActions,highlightColor: Color,- hasHighlightOverlap: Boolean,+ hasHighlightOverlap: () -> Boolean,modifier: Modifier = Modifier) {val isDark = LocalIsDarkMode.current@@ -202,7 +202,11 @@ fun PdfSelectionToolbar()// Accessibility: announce the selection size politely whenever it changes.- val count = if (state.hasSelection) state.selectedText().length else 0+ // Computed only once a gesture settles, so a drag doesn't rebuild the text every frame.+ val settled = state.hasSelection && !state.gestureActive+ val selStart = state.start+ val selEnd = state.end+ val count = remember(settled, selStart, selEnd) { if (settled) state.selectedText().length else 0 }BoxWithConstraints(modifier.fillMaxSize()) {if (count > 0) {@@ -246,7 +250,7 @@ fun PdfSelectionToolbar(onLongClick = { haptics.performHapticFeedback(HapticFeedbackType.LongPress); page = ToolbarPage.Colors },onClick = act { actions.onHighlight(highlightColor.toArgbLong()) }))- if (hasHighlightOverlap) add(ToolbarItem("rm", removeL, Icons.Rounded.FormatColorReset, tint = LiquidGlassColors.Red, onClick = act(actions.onRemoveHighlight)))+ if (shown && hasHighlightOverlap()) add(ToolbarItem("rm", removeL, Icons.Rounded.FormatColorReset, tint = LiquidGlassColors.Red, onClick = act(actions.onRemoveHighlight)))add(ToolbarItem("ul", underlineL, Icons.Rounded.FormatUnderlined, onClick = act(actions.onUnderline)))add(ToolbarItem("st", strikeL, Icons.Rounded.StrikethroughS, onClick = act(actions.onStrike)))add(ToolbarItem("share", shareL, Icons.Rounded.Share, onClick = act(actions.onShare)))@@ -327,6 +331,9 @@ fun PdfSelectionToolbar(val visible = union?.takeIf { it.bottom > 0f && it.top < screenH }?.let { Rect(it.left, it.top.coerceAtLeast(0f), it.right, it.bottom.coerceAtMost(screenH)) }if (visible != null) lastBounds[0] = visible+ // Selection scrolled out of view: hide (the platform toolbar does too). The last+ // bounds are only reused while the capsule fades out after the selection cleared.+ if (visible == null && state.hasSelection) return@layoutval b = visible ?: lastBounds[0] ?: return@layoutval w = placeable.width.toFloat()val h = placeable.height.toFloat()diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfTextSelectionState.kt b/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfTextSelectionState.ktindex 6690fc5..11228b2 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfTextSelectionState.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/selection/PdfTextSelectionState.kt@@ -289,10 +289,14 @@ class PdfTextSelectionState {if (raw >= w0e) {val c = (raw.offset - 1).coerceAtLeast(0)val w = l.wordAt(c)- select(w0s, TextPos(raw.page, maxOf(w.last + 1, raw.offset).coerceAtMost(l.length)))+ // Whole word only if the finger is actually inside one (wordAt falls back to a+ // neighbouring word for whitespace, which would overshoot).+ val e = if (c in w) maxOf(w.last + 1, raw.offset) else raw.offset+ select(w0s, TextPos(raw.page, e.coerceAtMost(l.length)))} else if (raw <= w0s) {- val w = l.wordAt(raw.offset.coerceAtMost(l.length - 1))- select(w0e, TextPos(raw.page, minOf(w.first, raw.offset)))+ val c = raw.offset.coerceIn(0, (l.length - 1).coerceAtLeast(0))+ val w = l.wordAt(c)+ select(w0e, TextPos(raw.page, if (c in w) minOf(w.first, raw.offset) else raw.offset))} else {select(w0s, w0e)}