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: zoom buttons on a desktop, and one zoom step for the buttons, Ctrl and the wheel, and Ctrl +/- (#35)

Joshbed49ce8b081
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt
index 232d73c..cc7434c 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt
@@ -545,8 +545,8 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {
val center = androidx.compose.ui.geometry.Offset(v.viewW / 2f, v.viewH / 2f)
when {
ctrl && keyCode == android.view.KeyEvent.KEYCODE_P -> print(doc)
- ctrl && (keyCode == android.view.KeyEvent.KEYCODE_EQUALS || keyCode == android.view.KeyEvent.KEYCODE_PLUS || keyCode == android.view.KeyEvent.KEYCODE_NUMPAD_ADD) -> v.zoomTo(v.zoom + 0.25f, center)
- ctrl && (keyCode == android.view.KeyEvent.KEYCODE_MINUS || keyCode == android.view.KeyEvent.KEYCODE_NUMPAD_SUBTRACT) -> v.zoomTo(v.zoom - 0.25f, center)
+ ctrl && (keyCode == android.view.KeyEvent.KEYCODE_EQUALS || keyCode == android.view.KeyEvent.KEYCODE_PLUS || keyCode == android.view.KeyEvent.KEYCODE_NUMPAD_ADD) -> v.zoomStep(1, center)
+ ctrl && (keyCode == android.view.KeyEvent.KEYCODE_MINUS || keyCode == android.view.KeyEvent.KEYCODE_NUMPAD_SUBTRACT) -> v.zoomStep(-1, center)
ctrl -> return false
keyCode == android.view.KeyEvent.KEYCODE_N || keyCode == android.view.KeyEvent.KEYCODE_PAGE_DOWN -> v.goToPage(v.currentPage + 1)
keyCode == android.view.KeyEvent.KEYCODE_P || keyCode == android.view.KeyEvent.KEYCODE_PAGE_UP -> v.goToPage(v.currentPage - 1)
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 5698596..f8f4dd4 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
@@ -61,9 +61,6 @@ data class CanvasLook(
/** 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
@@ -72,7 +69,8 @@ private const val WHEEL_TURN_MS = 800L
* 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. A mouse wheel scrolls (zooms
- * with Ctrl), and a mouse drag goes to [onMouseSelect] when there is one.
+ * with Ctrl, then [onWheelZoom]), a mouse drag goes to [onMouseSelect] when there is one, and a
+ * hovering mouse to [onMouseHover].
*/
@Composable
fun PageCanvas(
@@ -90,6 +88,10 @@ fun PageCanvas(
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,
+ /** The mouse moved over the pages with no button down (screen position). */
+ onMouseHover: (Offset) -> Unit = {},
+ /** Ctrl and the wheel zoomed. */
+ onWheelZoom: () -> Unit = {},
overlay: DrawScope.() -> Unit = {},
) {
val density = LocalDensity.current
@@ -100,6 +102,8 @@ fun PageCanvas(
val maxScale = with(density) { 4.dp.toPx() }
val flinger = remember { arrayOfNulls<Job>(1) }
val mouseSelect by rememberUpdatedState(onMouseSelect)
+ val mouseHover by rememberUpdatedState(onMouseHover)
+ val wheelZoom by rememberUpdatedState(onWheelZoom)
// keep the renderers working on what is in view
LaunchedEffect(state, images, generation) {
@@ -139,13 +143,17 @@ fun PageCanvas(
awaitPointerEventScope {
while (true) {
val event = awaitPointerEvent()
- if (event.type != PointerEventType.Scroll) continue
val change = event.changes.firstOrNull() ?: continue
+ if (event.type == PointerEventType.Move && change.type == PointerType.Mouse && !change.pressed) mouseHover(change.position)
+ if (event.type != PointerEventType.Scroll) 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)
+ event.keyboardModifiers.isCtrlPressed -> {
+ state.zoomStep(if (d.y < 0f) 1 else -1, change.position)
+ wheelZoom()
+ }
state.mode == PageMode.SinglePage -> {
val rest = state.scrollBy(0f, -d.y * WHEEL_STEP)
if (rest.y != 0f && change.uptimeMillis - lastTurn > WHEEL_TURN_MS) {
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 3be4bbb..b8af63a 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
@@ -164,6 +164,12 @@ fun ViewerScreen(vm: AppViewModel, doc: OpenDocument) {
val search = doc.search
val selection = doc.selection
+ // on a desktop, zoom buttons at the bottom right: with the bars, or for a moment when the mouse
+ // comes near the corner or the wheel zooms, as the reference has them
+ val config = androidx.compose.ui.platform.LocalConfiguration.current
+ val desktop = remember(config) { isDesktop(context, config) }
+ val zoomButtons = remember(doc) { ZoomButtonsState(scope) }
+ LaunchedEffect(ctl.barsVisible) { if (!ctl.barsVisible) zoomButtons.hide() }
val canvasLook = CanvasLook(background = c.desk, pageShadow = c.pageEdge, invert = ctl.nightMode)
BackHandler(enabled = ctl.searching || ctl.tool != ViewerTool.None || selection.selection != null) {
@@ -221,6 +227,12 @@ fun ViewerScreen(vm: AppViewModel, doc: OpenDocument) {
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,
+ onMouseHover = { p ->
+ val v = doc.viewer
+ val corner = with(density) { p.x > v.viewW - ZOOM_CORNER_WIDTH.toPx() && p.y > v.viewH - ZOOM_CORNER_HEIGHT.toPx() }
+ if (desktop && corner) zoomButtons.poke()
+ },
+ onWheelZoom = { if (desktop) zoomButtons.poke() },
onLongPress = { p, at ->
if (at != null) scope.launch {
val slop = 12f / doc.viewer.pixelsPerPoint(at.page).coerceAtLeast(0.1f) * density.density
@@ -274,6 +286,15 @@ fun ViewerScreen(vm: AppViewModel, doc: OpenDocument) {
)
}
+ if (desktop && !vm.readingMode) ZoomButtons(
+ doc.viewer,
+ visible = ctl.tool == ViewerTool.None && !ctl.searching && (ctl.barsVisible || zoomButtons.showing),
+ // above the bottom bar when it shows
+ bottom = maxOf(122.dp, with(density) { doc.viewer.padBottom.toDp() } + 12.dp),
+ onHover = { if (!ctl.barsVisible) zoomButtons.poke() },
+ modifier = Modifier.align(Alignment.BottomEnd),
+ )
+
HintToast(hint)
// the file's name in a translucent white chip under the top bar for a moment after it opens
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 fae7946..eda5461 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
@@ -130,6 +130,18 @@ class ViewerState(pages: List<PageSize>) {
return Offset(dx + (scrollX - ox), dy + (scrollY - oy))
}
+ /**
+ * Zoom in ([steps] > 0) or out by [ZOOM_STEP] a step about [focus], as the zoom buttons, Ctrl
+ * and the wheel, and Ctrl and + or − do; a single page back at fit-width settles on its slot.
+ */
+ fun zoomStep(steps: Int, focus: Offset) {
+ zoomTo(zoom + steps * ZOOM_STEP, focus)
+ if (mode == PageMode.SinglePage && zoom <= MIN_ZOOM + 0.001f) goToPage(currentPage)
+ }
+
+ val canZoomIn: Boolean get() = zoom < maxZoom - 0.001f
+ val canZoomOut: Boolean get() = zoom > minZoom + 0.001f
+
/** Zoom by [factor] about screen point [focus]. */
fun zoomBy(factor: Float, focus: Offset) = zoomTo(zoom * factor, focus)
@@ -324,6 +336,9 @@ class ViewerState(pages: List<PageSize>) {
const val LOWRES_BELOW = 0.6f
const val LOWRES_LEVEL = -1
const val MAX_ZOOM = 10f
+
+ /** What a zoom step adds or takes away (fit-width is 1), as the reference's step. */
+ const val ZOOM_STEP = 0.25f
const val TILE = 512
const val LEVEL_STEP = 1.4142135f
private const val SNAP_VELOCITY = 400f
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ZoomButtons.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ZoomButtons.kt
new file mode 100644
index 0000000..c0edf97
--- /dev/null
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/viewer/ZoomButtons.kt
@@ -0,0 +1,145 @@
+package io.github.artcraftandroid.pdfcraft.ui.viewer
+
+import android.content.Context
+import android.content.pm.PackageManager
+import android.content.res.Configuration
+import androidx.compose.animation.AnimatedVisibility
+import androidx.compose.animation.fadeIn
+import androidx.compose.animation.fadeOut
+import androidx.compose.foundation.background
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.shape.CircleShape
+import androidx.compose.material3.ExperimentalMaterial3Api
+import androidx.compose.material3.Icon
+import androidx.compose.material3.PlainTooltip
+import androidx.compose.material3.Text
+import androidx.compose.material3.TooltipBox
+import androidx.compose.material3.TooltipDefaults
+import androidx.compose.material3.rememberTooltipState
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.Stable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.alpha
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.draw.shadow
+import androidx.compose.ui.graphics.vector.ImageVector
+import androidx.compose.ui.input.pointer.PointerEventType
+import androidx.compose.ui.input.pointer.pointerInput
+import androidx.compose.ui.res.stringResource
+import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.dp
+import io.github.artcraftandroid.pdfcraft.R
+import io.github.artcraftandroid.pdfcraft.ui.LucideIcons
+import io.github.artcraftandroid.pdfcraft.ui.pdfColors
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
+
+/** How long the zoom buttons stay after the mouse comes near them while the bars are hidden. */
+private const val ZOOM_BUTTONS_MS = 2500L
+
+/** The corner a mouse brings the zoom buttons up from: this far from the right and from the bottom. */
+val ZOOM_CORNER_WIDTH = 100.dp
+val ZOOM_CORNER_HEIGHT = 172.dp
+
+/**
+ * Whether the app runs on a desktop: Samsung DeX, ChromeOS or an Android PC. The zoom buttons
+ * show only there, as the reference's do.
+ */
+fun isDesktop(context: Context, config: Configuration): Boolean {
+ val pm = context.packageManager
+ return samsungDesktop(config) || pm.hasSystemFeature(PackageManager.FEATURE_PC) ||
+ pm.hasSystemFeature("org.chromium.arc.device_management")
+}
+
+// Samsung's documented test for DeX: its configuration field matches the "enabled" constant
+private fun samsungDesktop(config: Configuration): Boolean = runCatching {
+ val cls = config.javaClass
+ cls.getField("SEM_DESKTOP_MODE_ENABLED").getInt(cls) == cls.getField("semDesktopModeEnabled").getInt(config)
+}.getOrDefault(false)
+
+/** The zoom buttons' brief appearances while the bars are hidden. */
+@Stable
+class ZoomButtonsState(private val scope: CoroutineScope) {
+ var showing by mutableStateOf(false)
+ private set
+ private var hideJob: Job? = null
+
+ /** Show the buttons for a moment (the mouse came near, or the wheel zoomed). */
+ fun poke() {
+ showing = true
+ hideJob?.cancel()
+ hideJob = scope.launch {
+ delay(ZOOM_BUTTONS_MS)
+ showing = false
+ }
+ }
+
+ fun hide() {
+ hideJob?.cancel()
+ showing = false
+ }
+}
+
+/**
+ * Zoom in over zoom out, at the bottom right, as the reference has them on a desktop. Each zooms
+ * a step about the top left, as the reference's do; a button is dimmed at the end of the range.
+ */
+@Composable
+fun ZoomButtons(state: ViewerState, visible: Boolean, bottom: Dp, onHover: () -> Unit, modifier: Modifier = Modifier) {
+ AnimatedVisibility(visible, modifier, enter = fadeIn(), exit = fadeOut()) {
+ Column(
+ Modifier
+ .padding(end = 50.dp, bottom = bottom)
+ // a pointer resting on the buttons keeps them up
+ .pointerInput(Unit) {
+ awaitPointerEventScope {
+ while (true) {
+ if (awaitPointerEvent().type == PointerEventType.Move) onHover()
+ }
+ }
+ },
+ verticalArrangement = Arrangement.spacedBy(9.dp),
+ ) {
+ ZoomButton(LucideIcons.Plus, stringResource(R.string.viewer_zoom_in), state.canZoomIn) {
+ state.zoomStep(1, androidx.compose.ui.geometry.Offset.Zero)
+ }
+ ZoomButton(LucideIcons.Minus, stringResource(R.string.viewer_zoom_out), state.canZoomOut) {
+ state.zoomStep(-1, androidx.compose.ui.geometry.Offset.Zero)
+ }
+ }
+ }
+}
+
+@OptIn(ExperimentalMaterial3Api::class)
+@Composable
+private fun ZoomButton(icon: ImageVector, label: String, enabled: Boolean, onClick: () -> Unit) {
+ val c = pdfColors
+ TooltipBox(
+ positionProvider = TooltipDefaults.rememberPlainTooltipPositionProvider(),
+ tooltip = { PlainTooltip { Text(label) } },
+ state = rememberTooltipState(),
+ ) {
+ Box(
+ Modifier
+ .size(40.dp)
+ .shadow(4.dp, CircleShape)
+ .clip(CircleShape)
+ .background(c.surfaceRaised)
+ .clickable(enabled = enabled, onClick = onClick),
+ contentAlignment = Alignment.Center,
+ ) {
+ Icon(icon, label, tint = c.icon, modifier = Modifier.size(22.dp).alpha(if (enabled) 1f else 0.38f))
+ }
+ }
+}
diff --git a/apps/pdfcraft-android/android/app/src/main/res/values-es/strings.xml b/apps/pdfcraft-android/android/app/src/main/res/values-es/strings.xml
index 50a7591..7763216 100644
--- a/apps/pdfcraft-android/android/app/src/main/res/values-es/strings.xml
+++ b/apps/pdfcraft-android/android/app/src/main/res/values-es/strings.xml
@@ -445,6 +445,8 @@
<string name="viewer_add_comment">Añadir comentario</string>
<string name="viewer_strikethrough">Tachar</string>
<string name="viewer_edit_text">Editar texto</string>
+ <string name="viewer_zoom_in">Acercar</string>
+ <string name="viewer_zoom_out">Alejar</string>
<string name="viewer_read_from_here">Leer desde aquí</string>
<string name="viewer_copy_text">Copiar texto</string>
<!-- Translator note: accessibility labels of the two handles at the ends of selected text. -->
diff --git a/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml b/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml
index 908c4ec..c7a2fea 100644
--- a/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml
+++ b/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml
@@ -446,6 +446,8 @@
<string name="viewer_add_comment">Add comment</string>
<string name="viewer_strikethrough">Strikethrough</string>
<string name="viewer_edit_text">Edit text</string>
+ <string name="viewer_zoom_in">Zoom in</string>
+ <string name="viewer_zoom_out">Zoom out</string>
<string name="viewer_read_from_here">Read from here</string>
<string name="viewer_copy_text">Copy text</string>
<!-- Translator note: accessibility labels of the two handles at the ends of selected text. -->
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 0743177..7e44728 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,26 @@ class ViewerGeometryTest {
assertEquals(400f, back.y, 0.1f)
}
+ @Test
+ fun zoomStepsAddAQuarterAndStopAtTheEnds() {
+ val s = state(List(3) { letter })
+ s.zoomStep(1, Offset.Zero)
+ assertEquals(1.25f, s.zoom, 0.0001f)
+ s.zoomStep(-2, Offset.Zero)
+ assertEquals(0.75f, s.zoom, 0.0001f)
+ assertTrue(s.canZoomIn && s.canZoomOut)
+ s.zoomTo(ViewerState.MAX_ZOOM, Offset.Zero)
+ assertTrue(!s.canZoomIn)
+ // a single page zoomed back out to fit-width settles on its own slot
+ s.switchMode(PageMode.SinglePage)
+ s.goToPage(1)
+ s.zoomStep(2, Offset(300f, 300f))
+ s.zoomStep(-2, Offset(300f, 300f))
+ assertEquals(1f, s.zoom, 0.0001f)
+ assertTrue(!s.canZoomOut)
+ assertEquals(s.viewW, s.scrollX, 0.5f)
+ }
+
@Test
fun aDragOffItsPageIsHeldToThatPage() {
val s = state(List(4) { letter })