-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract 'anki.android.input' from compat
After the minSdk 24 bump, these classes no longer belong in compat Note: these changes were not performed in the minSdk 24 bump as they dominated the diff
- Loading branch information
Showing
12 changed files
with
169 additions
and
137 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
91 changes: 91 additions & 0 deletions
91
AnkiDroid/src/main/java/com/ichi2/anki/android/input/Shortcut.kt
This file contains 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,91 @@ | ||
/* | ||
* Copyright (c) 2024 Sanjay Sargam <[email protected]> | ||
* 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.android.input | ||
|
||
import android.view.KeyEvent | ||
import android.view.KeyboardShortcutInfo | ||
import androidx.annotation.StringRes | ||
import com.ichi2.anki.AnkiActivityProvider | ||
import com.ichi2.anki.CollectionManager.TR | ||
import net.ankiweb.rsdroid.Translations | ||
|
||
/** | ||
* Data class representing a keyboard shortcut. | ||
* | ||
* @param shortcut The string representation of the keyboard shortcut (e.g., "Ctrl+Alt+S"). | ||
* @param label The string resource for the shortcut label. | ||
*/ | ||
data class Shortcut(val shortcut: String, val label: String) { | ||
|
||
/** | ||
* Converts the shortcut string into a KeyboardShortcutInfo object. | ||
* | ||
* @return A KeyboardShortcutInfo object representing the keyboard shortcut. | ||
*/ | ||
fun toShortcutInfo(): KeyboardShortcutInfo { | ||
val parts = shortcut.split("+") | ||
val key = parts.last() | ||
val keycode: Int = getKey(key) | ||
val modifierFlags: Int = parts.dropLast(1).sumOf { getModifier(it) } | ||
|
||
return KeyboardShortcutInfo(label, keycode, modifierFlags) | ||
} | ||
|
||
/** | ||
* Maps a modifier string to its corresponding KeyEvent meta flag. | ||
* | ||
* @param modifier The modifier string (e.g., "Ctrl", "Alt", "Shift"). | ||
* @return The corresponding KeyEvent meta flag. | ||
*/ | ||
private fun getModifier(modifier: String): Int { | ||
return when (modifier) { | ||
"Ctrl" -> KeyEvent.META_CTRL_ON | ||
"Alt" -> KeyEvent.META_ALT_ON | ||
"Shift" -> KeyEvent.META_SHIFT_ON | ||
|
||
else -> 0 | ||
} | ||
} | ||
|
||
/** | ||
* Maps a key string to its corresponding keycode. | ||
* | ||
* @param key The key string. | ||
* @return The corresponding keycode, or 0 if the key string is invalid or not recognized. | ||
*/ | ||
private fun getKey(key: String): Int { | ||
return when (key) { | ||
"/" -> KeyEvent.KEYCODE_SLASH | ||
"Esc" -> KeyEvent.KEYCODE_ESCAPE | ||
in "0".."9" -> KeyEvent.KEYCODE_0 + (key.toInt() - 0) // Handle number keys | ||
else -> KeyEvent.keyCodeFromString(key) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Provides a [Shortcut], from the shortcut keys and the resource id of its description. | ||
*/ | ||
fun AnkiActivityProvider.shortcut(shortcut: String, @StringRes labelRes: Int) = | ||
Shortcut(shortcut, ankiActivity.getString(labelRes)) | ||
|
||
/** | ||
* Provides a [Shortcut], from the shortcut keys and the function from anki strings. | ||
*/ | ||
fun shortcut(shortcut: String, getTranslation: Translations.() -> String) = | ||
Shortcut(shortcut, getTranslation(TR)) |
30 changes: 30 additions & 0 deletions
30
AnkiDroid/src/main/java/com/ichi2/anki/android/input/ShortcutGroup.kt
This file contains 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,30 @@ | ||
/* | ||
* Copyright (c) 2024 Sanjay Sargam <[email protected]> | ||
* 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.android.input | ||
|
||
import android.view.KeyboardShortcutGroup | ||
import androidx.annotation.StringRes | ||
import com.ichi2.anki.AnkiActivity | ||
|
||
data class ShortcutGroup(val shortcuts: List<Shortcut>, @StringRes val id: Int) { | ||
fun toShortcutGroup(activity: AnkiActivity): KeyboardShortcutGroup { | ||
val shortcuts = shortcuts.map { it.toShortcutInfo() } | ||
val groupLabel = activity.getString(id) | ||
return KeyboardShortcutGroup(groupLabel, shortcuts) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
AnkiDroid/src/main/java/com/ichi2/anki/android/input/ShortcutGroupProvider.kt
This file contains 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,25 @@ | ||
/* | ||
* Copyright (c) 2024 Sanjay Sargam <[email protected]> | ||
* 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.android.input | ||
|
||
interface ShortcutGroupProvider { | ||
/** | ||
* Lists of shortcuts for this fragment, and the IdRes of the name of this shortcut group. | ||
*/ | ||
val shortcuts: ShortcutGroup? | ||
} |
This file contains 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.