Skip to content
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

feat: Replace Press Alt+K to show keyboard shortcuts warning #17934

Merged
merged 2 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 1 addition & 38 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import android.media.AudioManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.KeyboardShortcutGroup
import android.view.Menu
import android.view.MenuItem
Expand Down Expand Up @@ -47,13 +46,11 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.color.MaterialColors
import com.google.android.material.snackbar.Snackbar
import com.ichi2.anim.ActivityTransitionAnimation
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 @@ -658,26 +655,13 @@ open class AnkiActivity :
super.onProvideKeyboardShortcuts(data, menu, deviceId)
}

/**
* Shows keyboard shortcuts dialog
*/
fun showKeyboardShortcutsDialog() {
val shortcutsGroup = getShortcuts()
// Don't show keyboard shortcuts dialog if there is no available shortcuts and also
// if there's 1 item because shortcutsGroup always includes generalShortcutGroup.
if (shortcutsGroup.size <= 1) return
Timber.i("displaying keyboard shortcut screen")
requestShowKeyboardShortcuts()
}

/**
* Get current activity keyboard shortcuts
*/
fun getShortcuts(): List<KeyboardShortcutGroup> {
private fun getShortcuts(): List<KeyboardShortcutGroup> {
val generalShortcutGroup =
ShortcutGroup(
listOf(
shortcut("Alt+K", R.string.show_keyboard_shortcuts_dialog),
shortcut("Ctrl+Z", R.string.undo),
),
R.string.pref_cat_general,
Expand All @@ -686,27 +670,6 @@ open class AnkiActivity :
return listOfNotNull(shortcuts?.toShortcutGroup(this), generalShortcutGroup)
}

override fun onKeyUp(
keyCode: Int,
event: KeyEvent,
): Boolean {
if (event.isAltPressed && keyCode == KeyEvent.KEYCODE_K) {
showKeyboardShortcutsDialog()
BrayanDSO marked this conversation as resolved.
Show resolved Hide resolved
return true
}

val done = super.onKeyUp(keyCode, event)

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
BrayanDSO marked this conversation as resolved.
Show resolved Hide resolved
if (Shortcut.isPotentialShortcutCombination(event, keyCode)) {
showSnackbar(R.string.show_shortcuts_message, Snackbar.LENGTH_SHORT)
return true
}
return false
}

/**
* If storage permissions are not granted, shows a toast message and finishes the activity.
*
Expand Down
4 changes: 0 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,6 @@ open class CardBrowser :
Timber.i("Ctrl+K: Toggle Mark")
toggleMark()
return true
} else if (event.isAltPressed) {
Timber.i("Alt+K: Show keyboard shortcuts dialog")
showKeyboardShortcutsDialog()
return true
}
}
KeyEvent.KEYCODE_R -> {
Expand Down
13 changes: 0 additions & 13 deletions AnkiDroid/src/main/java/com/ichi2/anki/android/input/Shortcut.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ 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 @@ -77,18 +76,6 @@ data class Shortcut(
in "0".."9" -> KeyEvent.KEYCODE_0 + (key.toInt() - 0) // Handle number keys
else -> KeyEvent.keyCodeFromString(key)
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package com.ichi2.anki.preferences

import android.content.res.Configuration
import androidx.annotation.StringRes
import androidx.preference.Preference
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.R
import com.ichi2.anki.cardviewer.ViewerCommand
Expand All @@ -41,6 +43,16 @@ class ControlsSettingsFragment : SettingsFragment() {
.forEach { pref -> pref.value = commands[pref.key]?.defaultValue?.toPreferenceString() }

setDynamicTitle()

// TODO replace the preference with something dismissible. This is meant only to improve
// the discoverability of the system shortcut for the shortcuts dialog.
requirePreference<Preference>(R.string.pref_keyboard_shortcuts_key).apply {
BrayanDSO marked this conversation as resolved.
Show resolved Hide resolved
isVisible = resources.configuration.keyboard == Configuration.KEYBOARD_QWERTY
setOnPreferenceClickListener {
requireActivity().requestShowKeyboardShortcuts()
BrayanDSO marked this conversation as resolved.
Show resolved Hide resolved
true
}
}
}

private fun setDynamicTitle() {
Expand Down
5 changes: 5 additions & 0 deletions AnkiDroid/src/main/res/drawable/ic_keyboard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="?attr/colorControlNormal" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M20,5L4,5c-1.1,0 -1.99,0.9 -1.99,2L2,17c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,7c0,-1.1 -0.9,-2 -2,-2zM11,8h2v2h-2L11,8zM11,11h2v2h-2v-2zM8,8h2v2L8,10L8,8zM8,11h2v2L8,13v-2zM7,13L5,13v-2h2v2zM7,10L5,10L5,8h2v2zM16,17L8,17v-2h8v2zM16,13h-2v-2h2v2zM16,10h-2L14,8h2v2zM19,13h-2v-2h2v2zM19,10h-2L17,8h2v2z"/>

</vector>
2 changes: 0 additions & 2 deletions AnkiDroid/src/main/res/values/02-strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ opening the system text to speech settings fails">
<string name="voice_not_supported">Voice not supported. Try another or install a voice engine.</string>

<!--Keyboard shortcuts dialog-->
<string name="show_keyboard_shortcuts_dialog" comment="Description of the shortcut that shows the keyboard shortcuts dialog">Show keyboard shortcuts dialog</string>
<string name="deck_picker_group" comment="Label for the group of shortcuts related to the deck picker">Deck Picker</string>
<string name="delete_deck_without_confirmation" comment="Description of the shortcut that deletes the deck without asking for confirmation">Delete deck without confirmation</string>
<string name="note_editor_group" comment="Label for the group of shortcuts related to the note editor">Note Editor</string>
Expand All @@ -414,7 +413,6 @@ opening the system text to speech settings fails">
<string name="copy_the_template" comment="Description of the shortcut that copy the template as markdown">Copy template as markdown</string>
<string name="edit_browser_appearance" comment="Description of the shortcut that edits the browser appearance of card template">Edit browser appearance</string>

<string name="show_shortcuts_message">Press Alt+K to show keyboard shortcuts</string>
<string name="missing_user_action_dialog_message" comment="%s is the user action number">User action %s is not set in this notetype. Please configure it</string>

<string name="directory_inaccessible_info">Learn more about how to restore access here %s or go to settings to update collection folder.</string>
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/10-preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<string name="gestures_summ">Assign gestures to actions such as answering and editing cards.</string>
<string name="gestures_corner_touch" maxLength="41">9-point touch</string>
<string name="gestures_corner_touch_summary">Allow touch gestures in screen corners</string>
<string name="show_keyboard_shortcuts" maxLength="41">Show keyboard shortcuts</string>
<string name="gestures_full_screen_nav_drawer" maxLength="41">Full screen navigation drawer</string>
<string name="gestures_fullscreen_nav_drawer_summary">Open navigation drawer when swiped right from anywhere on the screen</string>
<string name="gestures_none" maxLength="41" comment="No gestures">None</string>
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<string name="gestures_preference">gestures</string>
<string name="gestures_corner_touch_preference">gestureCornerTouch</string>
<string name="nav_drawer_gesture_key">gestureFullScreenNavigationDrawer</string>
<string name="pref_keyboard_shortcuts_key">showKeyboardShortcuts</string>
<string name="pref_swipe_sensitivity_key">swipeSensitivity</string>
<string name="show_answer_command_key">binding_SHOW_ANSWER</string>
<string name="answer_again_command_key">binding_FLIP_OR_ANSWER_EASE1</string>
Expand Down
11 changes: 9 additions & 2 deletions AnkiDroid/src/main/res/xml/preferences_controls.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:title="@string/pref_cat_controls"
android:key="@string/pref_controls_screen_key">
android:key="@string/pref_controls_screen_key"
tools:context=".preferences.ControlsSettingsFragment">

<SwitchPreferenceCompat
android:defaultValue="false"
Expand Down Expand Up @@ -48,7 +49,13 @@
android:defaultValue="100"
android:valueFrom="20"
android:valueTo="180"
app:displayValue="true"/>
app:displayValue="true" />

<Preference
android:key="@string/pref_keyboard_shortcuts_key"
android:title="@string/show_keyboard_shortcuts"
android:icon="@drawable/ic_keyboard"
/>

<PreferenceCategory android:title="@string/answer_buttons">
<com.ichi2.preferences.ControlPreference
Expand Down
34 changes: 0 additions & 34 deletions AnkiDroid/src/test/java/com/ichi2/anki/ShortcutTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class PreferencesAnalyticsTest : RobolectricTest() {
// Preferences that only click: don't have a value
R.string.tts_key, // tts
R.string.pref_reset_languages_key, // resetLanguages
R.string.pref_keyboard_shortcuts_key, // showKeyboardShortcuts
// Opens App Bar buttons fragment
R.string.custom_buttons_link_preference, // custom_buttons_link
// Opens Custom sync server fragment
Expand Down