-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(whiteboard): support gestures/keybinds #20268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BrayanDSO
wants to merge
5
commits into
ankidroid:main
Choose a base branch
from
BrayanDSO:feat/whiteboard-multi-finger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+588
−104
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc7892c
feat: whiteboard commands
BrayanDSO e2207b9
feat: split whiteboard commands from the others
BrayanDSO 6aa9323
test(whiteboard): multi touch detector
BrayanDSO a8a822b
refactor: simplify method
BrayanDSO 35c09cc
fix: check fingers individually in touch detector
BrayanDSO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
AnkiDroid/src/main/java/com/ichi2/anki/preferences/reviewer/WhiteboardAction.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Copyright (c) 2026 Brayan Oliveira <69634269+brayandso@users.noreply.github.com> | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU General Public License as published by the Free Software | ||
| * Foundation; either version 3 of the License, or (at your option) any later | ||
| * version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
| * PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along with | ||
| * this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.ichi2.anki.preferences.reviewer | ||
|
|
||
| import android.content.SharedPreferences | ||
| import com.ichi2.anki.reviewer.MappableAction | ||
| import com.ichi2.anki.reviewer.ReviewerBinding | ||
|
|
||
| enum class WhiteboardAction : MappableAction<ReviewerBinding> { | ||
| TOGGLE_ERASER, | ||
| CLEAR, | ||
| UNDO, | ||
| REDO, | ||
| ; | ||
|
|
||
| override val preferenceKey: String get() = "binding_whiteboard_$name" | ||
|
|
||
| override fun getBindings(prefs: SharedPreferences): List<ReviewerBinding> { | ||
| val prefValue = prefs.getString(preferenceKey, null) ?: return emptyList() | ||
| return ReviewerBinding.fromPreferenceString(prefValue) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/whiteboard/MultiTouchDetector.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| * Copyright (c) 2026 Brayan Oliveira <69634269+brayandso@users.noreply.github.com> | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU General Public License as published by the Free Software | ||
| * Foundation; either version 3 of the License, or (at your option) any later | ||
| * version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
| * PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along with | ||
| * this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.ichi2.anki.ui.windows.reviewer.whiteboard | ||
|
|
||
| import android.view.MotionEvent | ||
| import kotlin.math.abs | ||
|
|
||
| /** | ||
| * Detects multi-finger touch and scroll gestures and triggers a callback with the vertical delta. | ||
| */ | ||
| class MultiTouchDetector( | ||
| /** Distance in pixels a touch can wander before we think the user is scrolling */ | ||
| private val touchSlop: Int, | ||
| ) { | ||
| private var startX: Float = 0f | ||
| private var startY: Float = 0f | ||
| private var currentX: Float = 0f | ||
| private var currentY: Float = 0f | ||
|
|
||
| private var startX0: Float = 0f | ||
| private var startY0: Float = 0f | ||
| private var startX1: Float = 0f | ||
| private var startY1: Float = 0f | ||
|
|
||
| private var isWithinTapTolerance: Boolean = false | ||
| private var onScrollByListener: OnScrollByListener? = null | ||
| private var onMultiTouchListener: OnMultiTouchListener? = null | ||
|
|
||
| fun setOnScrollByListener(listener: OnScrollByListener) { | ||
| onScrollByListener = listener | ||
| } | ||
|
|
||
| fun setOnMultiTouchListener(listener: OnMultiTouchListener) { | ||
| onMultiTouchListener = listener | ||
| } | ||
|
|
||
| /** | ||
| * Processes the motion event. | ||
| * @return True if the event was handled (consumed), False otherwise. | ||
| */ | ||
| fun onTouchEvent(event: MotionEvent): Boolean { | ||
| if (event.pointerCount < 2) return false | ||
|
|
||
| return when (event.actionMasked) { | ||
| MotionEvent.ACTION_POINTER_DOWN -> { | ||
| reinitialize(event) | ||
| true | ||
| } | ||
| MotionEvent.ACTION_POINTER_UP -> { | ||
| if (isWithinTapTolerance) { | ||
| onMultiTouchListener?.onMultiTouch(event.pointerCount) | ||
| // Prevent cascading events (e.g., 3-finger tap triggering 3 then 2) | ||
| isWithinTapTolerance = false | ||
| } | ||
| true | ||
| } | ||
| MotionEvent.ACTION_MOVE -> tryScroll(event) | ||
| else -> false | ||
| } | ||
| } | ||
|
|
||
| private fun reinitialize(event: MotionEvent) { | ||
| isWithinTapTolerance = true | ||
|
|
||
| startX0 = event.getX(0) | ||
| startY0 = event.getY(0) | ||
| startX1 = event.getX(1) | ||
| startY1 = event.getY(1) | ||
|
|
||
| startX = (startX0 + startX1) / 2f | ||
| startY = (startY0 + startY1) / 2f | ||
| } | ||
|
|
||
| private fun updatePositions(event: MotionEvent) { | ||
| // Check if any individual finger exceeded the touch slop | ||
| if (isWithinTapTolerance) { | ||
| val dx0 = abs(startX0 - event.getX(0)) | ||
| val dy0 = abs(startY0 - event.getY(0)) | ||
| val dx1 = abs(startX1 - event.getX(1)) | ||
| val dy1 = abs(startY1 - event.getY(1)) | ||
|
|
||
| if (dx0 >= touchSlop || dy0 >= touchSlop || dx1 >= touchSlop || dy1 >= touchSlop) { | ||
| isWithinTapTolerance = false | ||
| } | ||
| } | ||
|
|
||
| currentX = (event.getX(0) + event.getX(1)) / 2f | ||
| currentY = (event.getY(0) + event.getY(1)) / 2f | ||
| } | ||
|
|
||
| private fun tryScroll(event: MotionEvent): Boolean { | ||
| updatePositions(event) | ||
| if (isWithinTapTolerance) { | ||
| return false | ||
| } | ||
| val dy = (startY - currentY).toInt() | ||
| if (dy != 0) { | ||
| onScrollByListener?.onVerticalScrollBy(dy) | ||
| startX = currentX | ||
| startY = currentY | ||
| } | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| fun interface OnScrollByListener { | ||
| /** | ||
| * @param y the amount of pixels to scroll vertically. | ||
| * @see [android.view.View.scrollBy] | ||
| */ | ||
| fun onVerticalScrollBy(y: Int) | ||
| } | ||
|
|
||
| fun interface OnMultiTouchListener { | ||
| /** | ||
| * @param pointerCount the amount of simultaneous touches | ||
| */ | ||
| fun onMultiTouch(pointerCount: Int) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nameis subject to accidental refactor and we need to guard against this.