Calculate settings/punctuation morekeys based on coord

This commit is contained in:
Aleksandras Kostarevas 2024-08-28 19:57:20 +03:00
parent c644be00a2
commit b8fc09e25c
8 changed files with 1287 additions and 1184 deletions

View File

@ -274,11 +274,15 @@ data class KeyAttributes(
val shiftable: Boolean? = null,
) {
fun getEffectiveAttributes(row: Row, keyboard: Keyboard): KeyAttributes {
val attrs = listOf(this, row.attributes, keyboard.attributes, DefaultKeyAttributes)
val attrs = if(row.isBottomRow) {
listOf(this, row.attributes, DefaultKeyAttributes)
} else {
listOf(this, row.attributes, keyboard.attributes, DefaultKeyAttributes)
}
val effectiveWidth = resolve(attrs) { it.width }
val defaultMoreKeyMode = if(row.isLetterRow && effectiveWidth == KeyWidth.Regular) {
val defaultMoreKeyMode = if((row.isLetterRow || row.isBottomRow) && effectiveWidth == KeyWidth.Regular) {
MoreKeyMode.All
} else {
MoreKeyMode.OnlyFromKeyspec
@ -373,6 +377,13 @@ data class BaseKey(
*/
val hint: String? = null,
) : AbstractKey {
override fun countsToKeyCoordinate(params: KeyboardParams, row: Row, keyboard: Keyboard): Boolean {
val attributes = attributes.getEffectiveAttributes(row, keyboard)
val moreKeyMode = attributes.moreKeyMode!!
return moreKeyMode.autoNumFromCoord && moreKeyMode.autoSymFromCoord
}
override fun computeData(params: KeyboardParams, row: Row, keyboard: Keyboard, coordinate: KeyCoordinate): ComputedKeyData {
val attributes = attributes.getEffectiveAttributes(row, keyboard)
val shifted = (attributes.shiftable == true) && when(params.mId.mElementId) {
@ -401,18 +412,22 @@ data class BaseKey(
val outputText = KeySpecParser.getOutputText(expandedSpec)
val moreKeyMode = attributes.moreKeyMode!!
val autoMoreKeys = listOfNotNull(
if (moreKeyMode.autoFromKeyspec) {
getDefaultMoreKeysForKey(code, relevantSpecShortcut)
} else { null },
if (moreKeyMode.autoNumFromCoord) {
if (moreKeyMode.autoNumFromCoord && row.isLetterRow) {
getNumForCoordinate(coordinate)
} else { null },
if (moreKeyMode.autoSymFromCoord) {
if (moreKeyMode.autoSymFromCoord && row.isLetterRow) {
getSymsForCoordinate(coordinate)
} else { null },
if (moreKeyMode.autoSymFromCoord) {
getSpecialFromRow(coordinate, row)
} else { null }
).joinToString(",")
@ -492,13 +507,8 @@ data class CaseSelector(
*/
val symbolsShifted: Key = normal
) : AbstractKey {
override fun computeData(
params: KeyboardParams,
row: Row,
keyboard: Keyboard,
coordinate: KeyCoordinate
): ComputedKeyData? =
when(params.mId.mElementId) {
private fun selectKeyFromElement(elementId: Int): Key =
when(elementId) {
KeyboardId.ELEMENT_ALPHABET -> normal
// KeyboardState.kt currently doesn't distinguish between these
@ -512,7 +522,18 @@ data class CaseSelector(
KeyboardId.ELEMENT_SYMBOLS -> symbols
KeyboardId.ELEMENT_SYMBOLS_SHIFTED -> symbolsShifted
else -> normal
}.computeData(params, row, keyboard, coordinate)
}
override fun countsToKeyCoordinate(params: KeyboardParams, row: Row, keyboard: Keyboard): Boolean =
selectKeyFromElement(params.mId.mElementId).countsToKeyCoordinate(params, row, keyboard)
override fun computeData(
params: KeyboardParams,
row: Row,
keyboard: Keyboard,
coordinate: KeyCoordinate
): ComputedKeyData? =
selectKeyFromElement(params.mId.mElementId).computeData(params, row, keyboard, coordinate)
}
typealias Key = @Serializable(with = KeyPathSerializer::class) AbstractKey
@ -601,6 +622,8 @@ enum class KeyVisualStyle {
@Serializable
@SerialName("gap")
class GapKey(val attributes: KeyAttributes = KeyAttributes()) : AbstractKey {
override fun countsToKeyCoordinate(params: KeyboardParams, row: Row, keyboard: Keyboard): Boolean = false
override fun computeData(
params: KeyboardParams,
row: Row,

View File

@ -8,11 +8,18 @@ import org.futo.inputmethod.keyboard.internal.MoreKeySpec
data class KeyCoordinate(
val regularRow: Int,
val regularColumn: Int,
val element: KeyboardLayoutElement
val element: KeyboardLayoutElement,
val measurement: KeyCoordinateMeasurement
)
data class KeyCoordinateMeasurement(
val totalRows: Int,
val numColumnsByRow: List<Int>
)
@Serializable
sealed interface AbstractKey {
fun countsToKeyCoordinate(params: KeyboardParams, row: Row, keyboard: Keyboard): Boolean
fun computeData(params: KeyboardParams, row: Row, keyboard: Keyboard, coordinate: KeyCoordinate): ComputedKeyData?
}

View File

@ -2,8 +2,8 @@ package org.futo.inputmethod.v2keyboard
val KeySpecShortcuts = mapOf(
"q" to listOf("keyspec_q"),
"," to listOf("keyspec_comma", "morekeys_comma"),
"." to listOf("keyspec_period", "morekeys_period"),
"," to listOf("keyspec_comma"),
"." to listOf("keyspec_period"),
"1" to listOf("keyspec_symbols_1", "additional_morekeys_symbols_1", "morekeys_symbols_1"),
"2" to listOf("keyspec_symbols_2", "additional_morekeys_symbols_2", "morekeys_symbols_2"),
"3" to listOf("keyspec_symbols_3", "additional_morekeys_symbols_3", "morekeys_symbols_3"),
@ -30,6 +30,7 @@ val KeySpecShortcuts = mapOf(
"]" to listOf("keyspec_right_square_bracket"),
"{" to listOf("keyspec_left_curly_bracket"),
"}" to listOf("keyspec_right_curly_bracket"),
"*" to listOf("*", "morekeys_star"),
// U+2260: "≠" NOT EQUAL TO
// U+2248: "≈" ALMOST EQUAL TO

View File

@ -361,11 +361,32 @@ data class LayoutEngine(
}.toFloat()
}
// Measure key coordinate
val numColumnsPerRow = mutableListOf<Int>()
rows.forEach { row ->
val numColumns = row.keys.sumOf { if(it.countsToKeyCoordinate(params, row, keyboard)) (1 as Int) else 0 }
if(numColumns > 0) {
numColumnsPerRow.add(numColumns)
}
}
val keyCoordinateMeasurement = KeyCoordinateMeasurement(
totalRows = numColumnsPerRow.size,
numColumnsByRow = numColumnsPerRow.toList()
)
var regularRow = 0
val computedRowWithoutWidths = rows.map { row ->
var regularColumn = 0
row.keys.mapNotNull { key ->
key.computeData(params, row, keyboard, KeyCoordinate(regularRow, regularColumn, layoutParams.element))?.let { data ->
val coordinate = KeyCoordinate(
regularRow,
regularColumn,
layoutParams.element,
keyCoordinateMeasurement
)
key.computeData(params, row, keyboard, coordinate)?.let { data ->
if(data.countsToKeyCoordinate) {
regularColumn += 1
}
@ -564,8 +585,6 @@ data class LayoutEngine(
}
}
params.mIconsSet.loadIcons(null, provider!!)
params.mThemeId = 3
params.mTextsSet.setLocale(params.mId.locale, context)

View File

@ -44,4 +44,16 @@ fun getNumForCoordinate(keyCoordinate: KeyCoordinate): String {
}
}
return ""
}
fun getSpecialFromRow(keyCoordinate: KeyCoordinate, row: Row): String {
if(row.isBottomRow) {
val numCols = keyCoordinate.measurement.numColumnsByRow.getOrNull(keyCoordinate.regularRow) ?: -10
if(keyCoordinate.regularColumn == 0) {
return "!text/morekeys_bottomrow_comma"
}else if(keyCoordinate.regularColumn == numCols - 1) {
return "!text/morekeys_period"
}
}
return ""
}

View File

@ -104,6 +104,7 @@ val TemplateAlt2Key = BaseKey(
data class EnterKey(
val attributes: KeyAttributes = KeyAttributes(width = KeyWidth.FunctionalKey)
) : AbstractKey {
override fun countsToKeyCoordinate(params: KeyboardParams, row: Row, keyboard: Keyboard): Boolean = false
override fun computeData(
params: KeyboardParams,
row: Row,
@ -169,6 +170,7 @@ data class EnterKey(
data class ActionKey(
val attributes: KeyAttributes = KeyAttributes()
) : AbstractKey {
override fun countsToKeyCoordinate(params: KeyboardParams, row: Row, keyboard: Keyboard): Boolean = false
override fun computeData(
params: KeyboardParams,
row: Row,
@ -217,21 +219,27 @@ data class ContextualKey(
KeyboardId.MODE_TIME to BaseKey(spec = ":", attributes = attributes),
)
private fun selectKey(params: KeyboardParams, keyboard: Keyboard): Key? {
if(keyboard.useZWNJKey) {
return TemplateZWNJKey
}
val key = keys[params.mId.mMode] ?: fallbackKey
return key
}
override fun countsToKeyCoordinate(params: KeyboardParams, row: Row, keyboard: Keyboard): Boolean {
return selectKey(params, keyboard)?.countsToKeyCoordinate(params, row, keyboard) ?: false
}
override fun computeData(
params: KeyboardParams,
row: Row,
keyboard: Keyboard,
coordinate: KeyCoordinate
): ComputedKeyData? {
if(keyboard.useZWNJKey) {
return TemplateZWNJKey.computeData(params, row, keyboard, coordinate)
}
val key = keys[params.mId.mMode] ?: fallbackKey
return key?.computeData(
params, row, keyboard, coordinate
)
return selectKey(params, keyboard)?.computeData(params, row, keyboard, coordinate)
}
}

View File

@ -264,6 +264,9 @@
"tablet_period": [
"!text/morekeys_tablet_punctuation"
],
"bottomrow_comma": [
"!icon/action_settings|!code/action_settings"
],
"exclamation": [
"¡",
"‽"