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, val shiftable: Boolean? = null,
) { ) {
fun getEffectiveAttributes(row: Row, keyboard: Keyboard): KeyAttributes { 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 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 MoreKeyMode.All
} else { } else {
MoreKeyMode.OnlyFromKeyspec MoreKeyMode.OnlyFromKeyspec
@ -373,6 +377,13 @@ data class BaseKey(
*/ */
val hint: String? = null, val hint: String? = null,
) : AbstractKey { ) : 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 { override fun computeData(params: KeyboardParams, row: Row, keyboard: Keyboard, coordinate: KeyCoordinate): ComputedKeyData {
val attributes = attributes.getEffectiveAttributes(row, keyboard) val attributes = attributes.getEffectiveAttributes(row, keyboard)
val shifted = (attributes.shiftable == true) && when(params.mId.mElementId) { val shifted = (attributes.shiftable == true) && when(params.mId.mElementId) {
@ -407,12 +418,16 @@ data class BaseKey(
getDefaultMoreKeysForKey(code, relevantSpecShortcut) getDefaultMoreKeysForKey(code, relevantSpecShortcut)
} else { null }, } else { null },
if (moreKeyMode.autoNumFromCoord) { if (moreKeyMode.autoNumFromCoord && row.isLetterRow) {
getNumForCoordinate(coordinate) getNumForCoordinate(coordinate)
} else { null }, } else { null },
if (moreKeyMode.autoSymFromCoord) { if (moreKeyMode.autoSymFromCoord && row.isLetterRow) {
getSymsForCoordinate(coordinate) getSymsForCoordinate(coordinate)
} else { null },
if (moreKeyMode.autoSymFromCoord) {
getSpecialFromRow(coordinate, row)
} else { null } } else { null }
).joinToString(",") ).joinToString(",")
@ -492,13 +507,8 @@ data class CaseSelector(
*/ */
val symbolsShifted: Key = normal val symbolsShifted: Key = normal
) : AbstractKey { ) : AbstractKey {
override fun computeData( private fun selectKeyFromElement(elementId: Int): Key =
params: KeyboardParams, when(elementId) {
row: Row,
keyboard: Keyboard,
coordinate: KeyCoordinate
): ComputedKeyData? =
when(params.mId.mElementId) {
KeyboardId.ELEMENT_ALPHABET -> normal KeyboardId.ELEMENT_ALPHABET -> normal
// KeyboardState.kt currently doesn't distinguish between these // KeyboardState.kt currently doesn't distinguish between these
@ -512,7 +522,18 @@ data class CaseSelector(
KeyboardId.ELEMENT_SYMBOLS -> symbols KeyboardId.ELEMENT_SYMBOLS -> symbols
KeyboardId.ELEMENT_SYMBOLS_SHIFTED -> symbolsShifted KeyboardId.ELEMENT_SYMBOLS_SHIFTED -> symbolsShifted
else -> normal 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 typealias Key = @Serializable(with = KeyPathSerializer::class) AbstractKey
@ -601,6 +622,8 @@ enum class KeyVisualStyle {
@Serializable @Serializable
@SerialName("gap") @SerialName("gap")
class GapKey(val attributes: KeyAttributes = KeyAttributes()) : AbstractKey { class GapKey(val attributes: KeyAttributes = KeyAttributes()) : AbstractKey {
override fun countsToKeyCoordinate(params: KeyboardParams, row: Row, keyboard: Keyboard): Boolean = false
override fun computeData( override fun computeData(
params: KeyboardParams, params: KeyboardParams,
row: Row, row: Row,

View File

@ -8,11 +8,18 @@ import org.futo.inputmethod.keyboard.internal.MoreKeySpec
data class KeyCoordinate( data class KeyCoordinate(
val regularRow: Int, val regularRow: Int,
val regularColumn: Int, val regularColumn: Int,
val element: KeyboardLayoutElement val element: KeyboardLayoutElement,
val measurement: KeyCoordinateMeasurement
)
data class KeyCoordinateMeasurement(
val totalRows: Int,
val numColumnsByRow: List<Int>
) )
@Serializable @Serializable
sealed interface AbstractKey { sealed interface AbstractKey {
fun countsToKeyCoordinate(params: KeyboardParams, row: Row, keyboard: Keyboard): Boolean
fun computeData(params: KeyboardParams, row: Row, keyboard: Keyboard, coordinate: KeyCoordinate): ComputedKeyData? 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( val KeySpecShortcuts = mapOf(
"q" to listOf("keyspec_q"), "q" to listOf("keyspec_q"),
"," to listOf("keyspec_comma", "morekeys_comma"), "," to listOf("keyspec_comma"),
"." to listOf("keyspec_period", "morekeys_period"), "." to listOf("keyspec_period"),
"1" to listOf("keyspec_symbols_1", "additional_morekeys_symbols_1", "morekeys_symbols_1"), "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"), "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"), "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_right_square_bracket"),
"{" to listOf("keyspec_left_curly_bracket"), "{" to listOf("keyspec_left_curly_bracket"),
"}" to listOf("keyspec_right_curly_bracket"), "}" to listOf("keyspec_right_curly_bracket"),
"*" to listOf("*", "morekeys_star"),
// U+2260: "≠" NOT EQUAL TO // U+2260: "≠" NOT EQUAL TO
// U+2248: "≈" ALMOST EQUAL TO // U+2248: "≈" ALMOST EQUAL TO

View File

@ -361,11 +361,32 @@ data class LayoutEngine(
}.toFloat() }.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 var regularRow = 0
val computedRowWithoutWidths = rows.map { row -> val computedRowWithoutWidths = rows.map { row ->
var regularColumn = 0 var regularColumn = 0
row.keys.mapNotNull { key -> 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) { if(data.countsToKeyCoordinate) {
regularColumn += 1 regularColumn += 1
} }
@ -564,8 +585,6 @@ data class LayoutEngine(
} }
} }
params.mIconsSet.loadIcons(null, provider!!) params.mIconsSet.loadIcons(null, provider!!)
params.mThemeId = 3 params.mThemeId = 3
params.mTextsSet.setLocale(params.mId.locale, context) params.mTextsSet.setLocale(params.mId.locale, context)

View File

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

View File

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