diff --git a/app/src/main/assets/layouts/number_row.txt b/app/src/main/assets/layouts/number_row.txt new file mode 100644 index 000000000..a7fed81c6 --- /dev/null +++ b/app/src/main/assets/layouts/number_row.txt @@ -0,0 +1,10 @@ +1 ¹ ½ ⅓ ¼ ⅛ +2 ² ⅔ +3 ³ ¾ ⅜ +4 ⁴ +5 ⁵ ⅝ +6 ⁶ +7 ⁷ ⅞ +8 ⁸ +9 ⁹ +0 ⁰ ⁿ ∅ diff --git a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/KeyboardParser.kt b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/KeyboardParser.kt index c74991d7f..dbbc53922 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/KeyboardParser.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/KeyboardParser.kt @@ -8,9 +8,11 @@ import helium314.keyboard.keyboard.Key import helium314.keyboard.keyboard.Key.KeyParams import helium314.keyboard.keyboard.KeyboardId import helium314.keyboard.keyboard.internal.KeyboardParams +import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyData import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyLabel import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyType +import helium314.keyboard.keyboard.internal.keyboard_parser.floris.SimplePopups import helium314.keyboard.keyboard.internal.keyboard_parser.floris.TextKeyData import helium314.keyboard.latin.common.isEmoji import helium314.keyboard.latin.define.DebugFlags @@ -19,12 +21,9 @@ import helium314.keyboard.latin.utils.POPUP_KEYS_LAYOUT import helium314.keyboard.latin.utils.POPUP_KEYS_NUMBER import helium314.keyboard.latin.utils.ScriptUtils import helium314.keyboard.latin.utils.ScriptUtils.script -import helium314.keyboard.latin.utils.ToolbarKey -import helium314.keyboard.latin.utils.removeFirst import helium314.keyboard.latin.utils.replaceFirst import helium314.keyboard.latin.utils.splitAt import helium314.keyboard.latin.utils.sumOf -import helium314.keyboard.latin.utils.toolbarKeyStrings /** * Abstract parser class that handles creation of keyboard from [KeyData] arranged in rows, @@ -67,11 +66,12 @@ class KeyboardParser(private val params: KeyboardParams, private val context: Co params.mBaseWidth = params.mOccupiedWidth - params.mLeftPadding - params.mRightPadding } - addNumberRowOrPopupKeys(baseKeys) + val numberRow = getNumberRow() + addNumberRowOrPopupKeys(baseKeys, numberRow) if (params.mId.isAlphabetKeyboard) addSymbolPopupKeys(baseKeys) if (params.mId.isAlphaOrSymbolKeyboard && params.mId.mNumberRowEnabled) - baseKeys.add(0, params.mLocaleKeyboardInfos.getNumberRow() + baseKeys.add(0, numberRow .mapTo(mutableListOf()) { it.copy(newLabelFlags = Key.LABEL_FLAGS_DISABLE_HINT_LABEL or defaultLabelFlags) }) val allFunctionalKeys = RawKeyboardParser.parseLayout(params, context, true) @@ -232,12 +232,12 @@ class KeyboardParser(private val params: KeyboardParams, private val context: Co return functionalKeysLeft to functionalKeysRight } - private fun addNumberRowOrPopupKeys(baseKeys: MutableList>) { + private fun addNumberRowOrPopupKeys(baseKeys: MutableList>, numberRow: MutableList) { if (!params.mId.mNumberRowEnabled && params.mId.mElementId == KeyboardId.ELEMENT_SYMBOLS) { // replace first symbols row with number row, but use the labels as popupKeys - val numberRow = params.mLocaleKeyboardInfos.getNumberRow() - numberRow.forEachIndexed { index, keyData -> keyData.popup.symbol = baseKeys[0].getOrNull(index)?.label } - baseKeys[0] = numberRow.toMutableList() + val numberRowCopy = numberRow.toMutableList() + numberRowCopy.forEachIndexed { index, keyData -> keyData.popup.symbol = baseKeys[0].getOrNull(index)?.label } + baseKeys[0] = numberRowCopy } else if (!params.mId.mNumberRowEnabled && params.mId.isAlphabetKeyboard && hasNumbersOnTopRow()) { if (baseKeys[0].any { it.popup.main != null || !it.popup.relevant.isNullOrEmpty() } // first row of baseKeys has any layout popup key && params.mPopupKeyLabelSources.let { @@ -250,7 +250,7 @@ class KeyboardParser(private val params: KeyboardParams, private val context: Co params.mPopupKeyLabelSources.remove(POPUP_KEYS_NUMBER) } // add number to the first 10 keys in first row - baseKeys.first().take(10).forEachIndexed { index, keyData -> keyData.popup.numberIndex = index } + baseKeys.first().take(10).forEachIndexed { index, keyData -> keyData.popup.numberLabel = numberRow.getOrNull(index)?.label } if (baseKeys.first().size < 10) { Log.w(TAG, "first row only has ${baseKeys.first().size} keys: ${baseKeys.first().map { it.label }}") } @@ -268,6 +268,34 @@ class KeyboardParser(private val params: KeyboardParams, private val context: Co } } + private fun getNumberRow(): MutableList { + val row = RawKeyboardParser.parseLayout("number_row.txt", params, context).first() + val localizedNumbers = params.mLocaleKeyboardInfos.localizedNumberKeys + if (localizedNumbers?.size != 10) return row + if (Settings.getInstance().current.mLocalizedNumberRow) { + // replace 0-9 with localized numbers, and move latin number into popup + for (i in row.indices) { + val key = row[i] + val number = key.label.toIntOrNull() ?: continue + when (number) { + 0 -> row[i] = key.copy(newLabel = localizedNumbers[9], newCode = KeyCode.UNSPECIFIED, newPopup = SimplePopups(listOf(key.label)).merge(key.popup)) + in 1..9 -> row[i] = key.copy(newLabel = localizedNumbers[number - 1], newCode = KeyCode.UNSPECIFIED, newPopup = SimplePopups(listOf(key.label)).merge(key.popup)) + } + } + } else { + // add localized numbers to popups on 0-9 + for (i in row.indices) { + val key = row[i] + val number = key.label.toIntOrNull() ?: continue + when (number) { + 0 -> row[i] = key.copy(newPopup = SimplePopups(listOf(localizedNumbers[9])).merge(key.popup)) + in 1..9 -> row[i] = key.copy(newPopup = SimplePopups(listOf(localizedNumbers[number - 1])).merge(key.popup)) + } + } + } + return row + } + // some layouts have different number layout, and there we don't want the numbers on the top row // todo: actually should not be in here, but in subtype extra values private fun hasNumbersOnTopRow() = params.mId.mSubtype.keyboardLayoutSetName !in listOf("pcqwerty", "lao", "thai", "korean_sebeolsik_390", "korean_sebeolsik_final") diff --git a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LocaleKeyboardInfos.kt b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LocaleKeyboardInfos.kt index ce53c6a42..b71df6841 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LocaleKeyboardInfos.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LocaleKeyboardInfos.kt @@ -13,7 +13,6 @@ import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.utils.SubtypeLocaleUtils import java.io.InputStream import java.util.Locale -import kotlin.math.round class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) { private val popupKeys = hashMapOf>() @@ -31,19 +30,8 @@ class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) { private set private var labelQuestion = "?" val currencyKey = getCurrencyKey(locale) - private var numberKeys = ((1..9) + 0).map { it.toString() } - private val numbersPopupKeys = arrayOf( - mutableListOf("¹", "½", "⅓","¼", "⅛"), - mutableListOf("²", "⅔"), - mutableListOf("³", "¾", "⅜"), - mutableListOf("⁴"), - mutableListOf("⁵", "⅝"), - mutableListOf("⁶"), - mutableListOf("⁷", "⅞"), - mutableListOf("⁸"), - mutableListOf("⁹"), - mutableListOf("⁰", "ⁿ", "∅"), - ) + var localizedNumberKeys: List? = null + private set val hasZwnjKey = when (locale.language) { // todo: move to the info file "fa", "ne", "kn", "te" -> true else -> false @@ -91,7 +79,7 @@ class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) { READER_MODE_POPUP_KEYS -> addPopupKeys(line, priority) READER_MODE_EXTRA_KEYS -> if (!onlyPopupKeys) addExtraKey(line.split(colonSpaceRegex, 2)) READER_MODE_LABELS -> if (!onlyPopupKeys) addLabel(line.split(colonSpaceRegex, 2)) - READER_MODE_NUMBER_ROW -> setNumberRow(line.splitOnWhitespace(), onlyPopupKeys) + READER_MODE_NUMBER_ROW -> localizedNumberKeys = line.splitOnWhitespace() } } } @@ -164,32 +152,6 @@ class LocaleKeyboardInfos(dataStream: InputStream?, locale: Locale) { } } - // set number row only, does not affect popupKeys - // setting more than 10 number keys will cause crashes, but could actually be implemented at some point - private fun setNumberRow(split: List, onlyAddToPopupKeys: Boolean) { - if (onlyAddToPopupKeys) { - // as of now this should never be used, but better have it - numberKeys.forEachIndexed { i, n -> - if (numberKeys[i] != n && n !in numbersPopupKeys[i]) - numbersPopupKeys[i].add(0, n) - } - return - } - if (Settings.getInstance().current.mLocalizedNumberRow) { - numberKeys.forEachIndexed { i, n -> numbersPopupKeys[i].add(0, n) } - numberKeys = split - } else { - split.forEachIndexed { i, n -> numbersPopupKeys[i].add(0, n) } - } - } - - // get number row including popupKeys - fun getNumberRow(): List = - numberKeys.mapIndexed { i, label -> - label.toTextKey(numbersPopupKeys[i]) - } - - fun getNumberLabel(numberIndex: Int?): String? = numberIndex?.let { numberKeys.getOrNull(it) } } private fun addFixedColumnOrder(popupKeys: MutableCollection) { diff --git a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/PopupSet.kt b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/PopupSet.kt index 4f239d951..f0cb2cb40 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/PopupSet.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/floris/PopupSet.kt @@ -27,7 +27,7 @@ open class PopupSet( } open fun isEmpty(): Boolean = main == null && relevant.isNullOrEmpty() - var numberIndex: Int? = null + var numberLabel: String? = null var symbol: String? = null // maybe list of keys? fun merge(other: PopupSet?): PopupSet { diff --git a/app/src/main/java/helium314/keyboard/latin/utils/PopupKeysUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/PopupKeysUtils.kt index 167aacfa3..072651097 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/PopupKeysUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/PopupKeysUtils.kt @@ -25,7 +25,7 @@ fun createPopupKeysArray(popupSet: PopupSet<*>?, params: KeyboardParams, label: val types = if (params.mId.isAlphabetKeyboard) params.mPopupKeyTypes else allPopupKeyTypes types.forEach { type -> when (type) { - POPUP_KEYS_NUMBER -> params.mLocaleKeyboardInfos.getNumberLabel(popupSet?.numberIndex)?.let { popupKeys.add(it) } + POPUP_KEYS_NUMBER -> popupSet?.numberLabel?.let { popupKeys.add(it) } POPUP_KEYS_LAYOUT -> popupSet?.getPopupKeyLabels(params)?.let { popupKeys.addAll(it) } POPUP_KEYS_SYMBOLS -> popupSet?.symbol?.let { popupKeys.add(it) } POPUP_KEYS_LANGUAGE -> params.mLocaleKeyboardInfos.getPopupKeys(label)?.let { popupKeys.addAll(it) } @@ -60,7 +60,7 @@ fun getHintLabel(popupSet: PopupSet<*>?, params: KeyboardParams, label: String): var hintLabel: String? = null for (type in params.mPopupKeyLabelSources) { when (type) { - POPUP_KEYS_NUMBER -> params.mLocaleKeyboardInfos.getNumberLabel(popupSet?.numberIndex)?.let { hintLabel = it } + POPUP_KEYS_NUMBER -> popupSet?.numberLabel?.let { hintLabel = it } POPUP_KEYS_LAYOUT -> popupSet?.getPopupKeyLabels(params)?.let { hintLabel = it.firstOrNull() } POPUP_KEYS_SYMBOLS -> popupSet?.symbol?.let { hintLabel = it } POPUP_KEYS_LANGUAGE -> params.mLocaleKeyboardInfos.getPopupKeys(label)?.let { hintLabel = it.firstOrNull() }