Added stylus-only mode for PDF drawing and annotations. (#27)

This change introduces a "Stylus Only Mode" toggle in the annotation dock, allowing users to restrict drawing input to stylus pens while maintaining touch support for scrolling and navigation.

Key changes include:
- Added `isStylusOnlyMode` state and persistence logic in `PdfViewerScreen`.
- Updated `AnnotationDock` with a toggle button for the new mode.
- Modified pointer input handling in `PdfPageComposable` and `PdfVerticalReader` to filter touch events when stylus-only mode is active.
- Adjusted tap gesture detection to ensure scrolling and page clicks still function correctly under various input modes.
This commit is contained in:
Aryan 2026-03-04 13:18:26 +05:30 committed by GitHub
parent d459535ffd
commit b47b6a9164
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 97 additions and 25 deletions

View file

@ -81,6 +81,7 @@ import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.input.pointer.PointerEventTimeoutCancellationException
import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.input.pointer.changedToUp
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChanged
@ -416,6 +417,7 @@ internal fun PdfPageComposable(
draggingBoxId: String? = null,
isScrollLocked: Boolean = false,
isVisible: Boolean = true,
isStylusOnlyMode: Boolean = false
) {
SideEffect { Timber.tag("PdfDrawPerf").v("PdfPageComposable Recompose: Page $pageIndex") }
val pdfDocumentItem = pdfDocument.item
@ -2237,9 +2239,14 @@ internal fun PdfPageComposable(
isZoomEnabled,
isVerticalScroll,
isEditMode,
selectedTool
selectedTool,
isStylusOnlyMode
) {
if (isEditMode && selectedTool != InkType.TEXT) return@pointerInput
val isTapDetectionAllowed = !isEditMode ||
selectedTool == InkType.TEXT ||
isStylusOnlyMode
if (!isTapDetectionAllowed) return@pointerInput
detectTapGestures(onTap = { tapOffset ->
Timber.d(
@ -2600,7 +2607,8 @@ internal fun PdfPageComposable(
offset,
isScrolling,
isVerticalScroll,
selectedTool
selectedTool,
isStylusOnlyMode
) {
val canDraw = isEditMode && selectedTool != InkType.TEXT && !isScrolling && !isVerticalScroll && actualBitmapWidthPx > 0 && actualBitmapHeightPx > 0
@ -2611,6 +2619,13 @@ internal fun PdfPageComposable(
try {
awaitEachGesture {
val down = awaitFirstDown(requireUnconsumed = false)
Timber.tag("PointerTypeDebug").d("Page $pageIndex: Input Type detected: ${down.type}")
if (isStylusOnlyMode && down.type == PointerType.Touch) {
return@awaitEachGesture
}
val dragPointerId = down.id
val startPos = down.position
var dragStarted = false