Skip to content

Commit

Permalink
fix: correct logical grouping in shortcut condtion
Browse files Browse the repository at this point in the history
Grouped the numeric key check with the modifier key check to ensure the condition is true only when both a modifier key and a numeric key are pressed
  • Loading branch information
SanjaySargam authored and david-allison committed Dec 2, 2024
1 parent 333ef64 commit d556c20
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
7 changes: 5 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import com.ichi2.anim.ActivityTransitionAnimation.Direction
import com.ichi2.anim.ActivityTransitionAnimation.Direction.DEFAULT
import com.ichi2.anim.ActivityTransitionAnimation.Direction.NONE
import com.ichi2.anki.analytics.UsageAnalytics
import com.ichi2.anki.android.input.Shortcut
import com.ichi2.anki.android.input.ShortcutGroup
import com.ichi2.anki.android.input.ShortcutGroupProvider
import com.ichi2.anki.android.input.shortcut
Expand Down Expand Up @@ -690,8 +691,10 @@ open class AnkiActivity : AppCompatActivity, SimpleMessageDialogListener, Shortc

val done = super.onKeyUp(keyCode, event)

// Show snackbar only if the current activity have shortcuts, a modifier key is pressed and the keyCode is an unmapped alphabet key
if (!done && shortcuts != null && (event.isCtrlPressed || event.isAltPressed || event.isMetaPressed) && (keyCode in KeyEvent.KEYCODE_A..KeyEvent.KEYCODE_Z) || (keyCode in KeyEvent.KEYCODE_NUMPAD_0..KeyEvent.KEYCODE_NUMPAD_9)) {
if (done || shortcuts == null) return false

// Show snackbar only if the current activity have shortcuts, a modifier key is pressed and the keyCode is an unmapped alphabet or num key
if (Shortcut.isPotentialShortcutCombination(event, keyCode)) {
showSnackbar(R.string.show_shortcuts_message, Snackbar.LENGTH_SHORT)
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.ichi2.anki.android.input

import android.view.KeyEvent
import android.view.KeyboardShortcutInfo
import androidx.annotation.CheckResult
import androidx.annotation.StringRes
import com.ichi2.anki.AnkiActivityProvider
import com.ichi2.anki.CollectionManager.TR
Expand Down Expand Up @@ -76,6 +77,12 @@ data class Shortcut(val shortcut: String, val label: String) {
else -> KeyEvent.keyCodeFromString(key)
}
}

companion object {
@CheckResult
fun isPotentialShortcutCombination(event: KeyEvent, keyCode: Int): Boolean =
(event.isCtrlPressed || event.isAltPressed || event.isMetaPressed) && ((keyCode in KeyEvent.KEYCODE_A..KeyEvent.KEYCODE_Z) || (keyCode in KeyEvent.KEYCODE_NUMPAD_0..KeyEvent.KEYCODE_NUMPAD_9))
}
}

/**
Expand Down
34 changes: 34 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/anki/ShortcutTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 David Allison <[email protected]>
*
* 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

import android.view.KeyEvent
import com.ichi2.anki.android.input.Shortcut
import org.mockito.kotlin.mock
import kotlin.test.Test
import kotlin.test.assertFalse

class ShortcutTest {
val keyEventWithNoModifiers: KeyEvent = mock { }

@Test
fun `single number is not a shortcut hint`() {
assertFalse(
Shortcut.isPotentialShortcutCombination(keyEventWithNoModifiers, KeyEvent.KEYCODE_NUMPAD_1)
)
}
}

0 comments on commit d556c20

Please sign in to comment.