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.

feat: a mouse wheel scrolls and zooms, and a mouse drag selects text (#35)

Joshfc3d06a44f0e
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/PageCanvas.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/PageCanvas.kt
index c3dbdf2..5698596 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/PageCanvas.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/PageCanvas.kt
@@ -17,8 +17,10 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
@@ -27,7 +29,12 @@ import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.graphics.drawscope.DrawScope
+import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.PointerInputChange
+import androidx.compose.ui.input.pointer.PointerType
+import androidx.compose.ui.input.pointer.isCtrlPressed
+import androidx.compose.ui.input.pointer.isSecondaryPressed
+import androidx.compose.ui.input.pointer.isTertiaryPressed
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChanged
import androidx.compose.ui.input.pointer.util.VelocityTracker
@@ -51,11 +58,21 @@ data class CanvasLook(
val invert: Boolean = false,
)
+/** Screen pixels a mouse wheel's notch scrolls (the reference's step). */
+private const val WHEEL_STEP = 100f
+
+/** The zoom one notch of Ctrl and the wheel makes. */
+private const val WHEEL_ZOOM = 1.25f
+
+/** How soon after one the wheel may turn another single page. */
+private const val WHEEL_TURN_MS = 800L
+
/**
* The document view: pages laid out by [state], pinch to zoom, drag and fling to scroll, double
* tap to zoom in or back out (and [onDoubleTap]), tap for [onTap] (bars), long press for [onLongPress]. [overlay]
* draws over the pages in screen space (selection, search hits, comments being made). When
- * [gesturesEnabled] is false a tool above takes the touches itself.
+ * [gesturesEnabled] is false a tool above takes the touches itself. A mouse wheel scrolls (zooms
+ * with Ctrl), and a mouse drag goes to [onMouseSelect] when there is one.
*/
@Composable
fun PageCanvas(
@@ -71,6 +88,8 @@ fun PageCanvas(
onLongPress: (Offset, PagePoint?) -> Unit = { _, _ -> },
onDoubleTap: () -> Unit = {},
onGestureStart: () -> Unit = {},
+ /** A drag with the mouse's main button: from and to, on the page it began on (text selection). */
+ onMouseSelect: ((PagePoint, PagePoint) -> Unit)? = null,
overlay: DrawScope.() -> Unit = {},
) {
val density = LocalDensity.current
@@ -80,6 +99,7 @@ fun PageCanvas(
// device pixels per point beyond which a whole page isn't worth more pixels (tiles take over)
val maxScale = with(density) { 4.dp.toPx() }
val flinger = remember { arrayOfNulls<Job>(1) }
+ val mouseSelect by rememberUpdatedState(onMouseSelect)
// keep the renderers working on what is in view
LaunchedEffect(state, images, generation) {
@@ -112,12 +132,58 @@ fun PageCanvas(
onPress = { flinger[0]?.cancel() },
)
}
+ // a mouse wheel scrolls, a single page turning at its edge, and with Ctrl zooms about
+ // the pointer, as the reference's viewer does
+ .pointerInput(state) {
+ var lastTurn = 0L
+ awaitPointerEventScope {
+ while (true) {
+ val event = awaitPointerEvent()
+ if (event.type != PointerEventType.Scroll) continue
+ val change = event.changes.firstOrNull() ?: continue
+ val d = change.scrollDelta
+ if (d == Offset.Zero) continue
+ flinger[0]?.cancel()
+ when {
+ event.keyboardModifiers.isCtrlPressed -> state.zoomBy(if (d.y < 0f) WHEEL_ZOOM else 1f / WHEEL_ZOOM, change.position)
+ state.mode == PageMode.SinglePage -> {
+ val rest = state.scrollBy(0f, -d.y * WHEEL_STEP)
+ if (rest.y != 0f && change.uptimeMillis - lastTurn > WHEEL_TURN_MS) {
+ lastTurn = change.uptimeMillis
+ state.goToPage(state.currentPage + if (d.y > 0f) 1 else -1)
+ }
+ }
+ else -> state.scrollBy(-d.x * WHEEL_STEP, -d.y * WHEEL_STEP)
+ }
+ change.consume()
+ }
+ }
+ }
.pointerInput(state, gesturesEnabled) {
if (!gesturesEnabled) return@pointerInput
val touchSlop = viewConfiguration.touchSlop
awaitEachGesture {
val down = awaitFirstDown(requireUnconsumed = false)
flinger[0]?.cancel()
+ // a drag with the mouse's main button selects text instead of moving the page; the
+ // right and middle buttons still move it
+ val buttons = currentEvent.buttons
+ val select = mouseSelect
+ ?.takeIf { down.type == PointerType.Mouse && !buttons.isSecondaryPressed && !buttons.isTertiaryPressed }
+ ?.let { f -> state.screenToPage(down.position)?.let { from -> from to f } }
+ if (select != null) {
+ val (from, report) = select
+ while (true) {
+ val event = awaitPointerEvent()
+ val change = event.changes.firstOrNull { it.id == down.id } ?: break
+ if (!change.pressed) break
+ if ((change.position - down.position).getDistance() > touchSlop) {
+ state.screenToPageOf(from.page, change.position)?.let { report(from, it) }
+ change.consume()
+ }
+ }
+ return@awaitEachGesture
+ }
val tracker = VelocityTracker()
tracker.addPosition(down.uptimeMillis, down.position)
var moved = Offset.Zero
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/TextSelection.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/TextSelection.kt
index 692d57a..961b739 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/TextSelection.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/TextSelection.kt
@@ -7,6 +7,7 @@ import androidx.compose.runtime.setValue
import io.github.artcraftandroid.pdfcraft.core.TextRange
import io.github.artcraftandroid.pdfcraft.engine.Engine
import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@@ -15,7 +16,7 @@ data class Selection(val range: TextRange, val rects: List<FloatArray>, val star
/**
* Text selection: a long press selects the word under the finger, the handles at either end
- * extend it glyph by glyph. A selection stays on one page, as a phone screen shows one page's
+ * extend it glyph by glyph, and a mouse drag selects the text it crosses. A selection stays on one page, as a phone screen shows one page's
* text at a time.
*/
@Stable
@@ -37,6 +38,32 @@ class TextSelectionState(private val engine: Engine, private val doc: ULong, pri
return true
}
+ // the latest mouse drag waiting to be shown, and the work showing one (one at a time)
+ private var span: Pair<PagePoint, PagePoint>? = null
+ private var spanJob: Job? = null
+
+ /**
+ * A mouse drag from [from] to [to] on one page: the text between them, as a desktop selects.
+ * Moves arrive faster than the engine answers, so only the latest is shown.
+ */
+ fun selectSpan(from: PagePoint, to: PagePoint) {
+ span = from to to
+ if (spanJob?.isActive == true) return
+ spanJob = scope.launch {
+ while (true) {
+ val (a, b) = span ?: break
+ span = null
+ val page = a.page.toUInt()
+ val (ga, gb) = withContext(engine.commands) {
+ engine.session.nearestGlyph(doc, page, a.x, a.y) to engine.session.nearestGlyph(doc, page, b.x, b.y)
+ }
+ if (ga == null || gb == null) continue
+ anchor = ga.toInt()
+ show(TextRange(page, minOf(ga, gb), maxOf(ga, gb) + 1u))
+ }
+ }
+ }
+
/** Select all the text of [page]. */
fun selectPage(page: Int) {
scope.launch {
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerScreen.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerScreen.kt
index a9b6b58..3be4bbb 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerScreen.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerScreen.kt
@@ -219,6 +219,8 @@ fun ViewerScreen(vm: AppViewModel, doc: OpenDocument) {
},
// a double tap zooms with the bars out of the way, as the reference's does
onDoubleTap = { if (ctl.tool == ViewerTool.None && !ctl.searching) ctl.barsVisible = false },
+ // with no tool out, a mouse drag selects text, as a desktop does
+ onMouseSelect = if (ctl.tool == ViewerTool.None) selection::selectSpan else null,
onLongPress = { p, at ->
if (at != null) scope.launch {
val slop = 12f / doc.viewer.pixelsPerPoint(at.page).coerceAtLeast(0.1f) * density.density
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerState.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerState.kt
index 9f9bc90..fae7946 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerState.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ViewerState.kt
@@ -219,9 +219,19 @@ class ViewerState(pages: List<PageSize>) {
val lx = (p.x + scrollX) / zoom
val ly = (p.y + scrollY) / zoom
val i = l.pageAt(lx, ly) ?: if (nearest && l.rects.isNotEmpty()) l.nearestPage(lx, ly) else return null
- val r = l.rects[i]
- val s = l.scaleOf(i)
- return PagePoint(i, ((lx - r.left) / s).coerceIn(0f, pages[i].width), ((ly - r.top) / s).coerceIn(0f, pages[i].height))
+ return screenToPageOf(i, p)
+ }
+
+ /** Screen point [p] in page [page]'s points, held to the page (a drag that leaves the page). */
+ fun screenToPageOf(page: Int, p: Offset): PagePoint? {
+ val l = layout
+ val r = l.rects.getOrNull(page) ?: return null
+ val size = pages.getOrNull(page) ?: return null
+ val s = l.scaleOf(page)
+ if (s <= 0f) return null
+ val lx = (p.x + scrollX) / zoom
+ val ly = (p.y + scrollY) / zoom
+ return PagePoint(page, ((lx - r.left) / s).coerceIn(0f, size.width), ((ly - r.top) / s).coerceIn(0f, size.height))
}
/** Screen position of a point on page [page] given in its points. */
diff --git a/apps/pdfcraft-android/android/app/src/test/java/io/github/artcraftandroid/pdfcraft/ui/ViewerGeometryTest.kt b/apps/pdfcraft-android/android/app/src/test/java/io/github/artcraftandroid/pdfcraft/ui/ViewerGeometryTest.kt
index bef844e..0743177 100644
--- a/apps/pdfcraft-android/android/app/src/test/java/io/github/artcraftandroid/pdfcraft/ui/ViewerGeometryTest.kt
+++ b/apps/pdfcraft-android/android/app/src/test/java/io/github/artcraftandroid/pdfcraft/ui/ViewerGeometryTest.kt
@@ -144,6 +144,25 @@ class ViewerGeometryTest {
assertEquals(400f, back.y, 0.1f)
}
+ @Test
+ fun aDragOffItsPageIsHeldToThatPage() {
+ val s = state(List(4) { letter })
+ s.goToPage(1)
+ val top = s.pageToScreen(1, 0f, 0f)
+ // the gap above page 1 belongs to no page, but a drag that began on page 1 stays on it
+ val above = Offset(top.x + 50f, top.y - 10f)
+ assertNull(s.screenToPage(above))
+ val held = s.screenToPageOf(1, above)!!
+ assertEquals(1, held.page)
+ assertEquals(0f, held.y, 0.001f)
+ // and past the page's far corner, it is held to that corner
+ val far = s.pageToScreen(1, 612f, 792f) + Offset(40f, 40f)
+ val corner = s.screenToPageOf(1, far)!!
+ assertEquals(612f, corner.x, 0.001f)
+ assertEquals(792f, corner.y, 0.001f)
+ assertNull(s.screenToPageOf(9, above))
+ }
+
@Test
fun currentPageFollowsScroll() {
val s = state(List(10) { letter })