move number row into assets

make it customizable later
This commit is contained in:
Helium314 2024-08-26 21:11:23 +02:00
parent be5c86ac31
commit ee748023fd
5 changed files with 54 additions and 54 deletions

View file

@ -0,0 +1,10 @@
1 ¹ ½ ⅓ ¼ ⅛
2 ² ⅔
3 ³ ¾ ⅜
4 ⁴
5 ⁵ ⅝
6 ⁶
7 ⁷ ⅞
8 ⁸
9 ⁹
0 ⁰ ⁿ ∅

View file

@ -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<MutableList<KeyData>>) {
private fun addNumberRowOrPopupKeys(baseKeys: MutableList<MutableList<KeyData>>, numberRow: MutableList<KeyData>) {
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<KeyData> {
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")

View file

@ -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<String, MutableCollection<String>>()
@ -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<String>? = 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<String>, 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<KeyData> =
numberKeys.mapIndexed { i, label ->
label.toTextKey(numbersPopupKeys[i])
}
fun getNumberLabel(numberIndex: Int?): String? = numberIndex?.let { numberKeys.getOrNull(it) }
}
private fun addFixedColumnOrder(popupKeys: MutableCollection<String>) {

View file

@ -27,7 +27,7 @@ open class PopupSet<T : AbstractKeyData>(
}
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 <U : AbstractKeyData> merge(other: PopupSet<U>?): PopupSet<out AbstractKeyData> {

View file

@ -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() }