Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/assets/layouts/main/japanese_12key.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
// kana-grid (keep this line in a copy: no extra keys or popups are added, and no split or gesture typing)
[
{"label": "emoji", "width": 0.2},
{"label": "あ", "width": 0.2, "popup": {"relevant": [{"label": "!fixedOrder!5"}, {"label": "あ"}, {"label": "い"}, {"label": "う"}, {"label": "え"}, {"label": "お"}, {"label": "ぁ"}, {"label": "ぃ"}, {"label": "ぅ"}, {"label": "ぇ"}, {"label": "ぉ"}]}},
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/helium314/keyboard/keyboard/KeyboardId.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class KeyboardId(elementId: Int, params: KeyboardLayoutSet.Params) {
|| LayoutParser.isHexLayout(mSubtype.mainLayoutName, mContext)
)

/** A kana grid is complete as written: nothing is added to it, it is not split, and swiping it types no words */
val isKanaGrid: Boolean
get() = isAlphabetKeyboard && LayoutParser.isKanaGridLayout(mSubtype.mainLayoutName, mContext)

fun navigateNext(): Boolean {
return (mEditorInfo.imeOptions and EditorInfo.IME_FLAG_NAVIGATE_NEXT) != 0 ||
imeAction() == EditorInfo.IME_ACTION_NEXT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ class PointerTracker private constructor(
}
// A gesture should start only from a non-modifier key. Note that the gesture detection is
// disabled when the key is repeating.
mIsDetectingGesture = (mKeyboard?.mId?.isAlphabetKeyboard == true) &&
mIsDetectingGesture = (mKeyboard?.mId?.isAlphabetKeyboard == true) && mKeyboard?.mId?.isKanaGrid == false &&
key != null && !key.isModifier() && !mKeySwipeAllowed && !sInKeySwipe
if (mIsDetectingGesture) {
mBatchInputArbiter.addDownEventPoint(x, y, eventTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package helium314.keyboard.keyboard.internal

import android.content.Context
import helium314.keyboard.keyboard.Key
import helium314.keyboard.keyboard.KeyboardSwitcher
import helium314.keyboard.latin.common.Constants
import helium314.keyboard.latin.LatinIME
import helium314.keyboard.latin.R
import helium314.keyboard.latin.settings.Settings
Expand Down Expand Up @@ -59,16 +59,16 @@ object KeyboardBottomRowGeometry {
val bottomRowAreaHeight = totalHeight - bottomRowY

val mainKb = KeyboardSwitcher.getInstance().keyboard
// hex layouts have their space keys in a middle row, so the space key does not mark the bottom row
// hex layouts have interlocking rows of their own, so they keep the calculated bottom row
if (mainKb != null && mainKb.mOccupiedHeight == totalHeight && !mainKb.isHexagonal
&& (mainKb.mId.isAlphabetKeyboard || mainKb.mId.isAlphaOrSymbolKeyboard)) {
val spaceKey = mainKb.sortedKeys.firstOrNull { it.code == Constants.CODE_SPACE }
if (spaceKey != null) {
val bottomKey = findBottomRowKey(mainKb.sortedKeys)
if (bottomKey != null) {
return Dimensions(
bottomRowY = spaceKey.y,
bottomRowKeyHeight = spaceKey.height,
bottomRowAreaHeight = mainKb.mOccupiedHeight - spaceKey.y,
bottomPadding = mainKb.mOccupiedHeight - (spaceKey.y + spaceKey.height),
bottomRowY = bottomKey.y,
bottomRowKeyHeight = bottomKey.height,
bottomRowAreaHeight = mainKb.mOccupiedHeight - bottomKey.y,
bottomPadding = mainKb.mOccupiedHeight - (bottomKey.y + bottomKey.height),
totalKeyboardHeight = mainKb.mOccupiedHeight
)
}
Expand All @@ -82,4 +82,10 @@ object KeyboardBottomRowGeometry {
totalKeyboardHeight = totalHeight
)
}

/**
* A key of the lowest row. This is usually the space bar's row, but not every layout has the space bar there
* (Japanese 12-key grid, custom layouts).
*/
internal fun findBottomRowKey(keys: List<Key>): Key? = keys.filterNot { it.isSpacer }.maxByOrNull { it.y }
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ open class KeyboardBuilder<KP : KeyboardParams>(protected val mContext: Context,
}

open fun build(): Keyboard {
if (!mParams.isHexagonal && mParams.mId.mIsSplitLayout && mParams.mId.isAlphaOrSymbolKeyboard) {
if (!mParams.isHexagonal && !mParams.mId.isKanaGrid && mParams.mId.mIsSplitLayout && mParams.mId.isAlphaOrSymbolKeyboard) {
addSplit()
}
addKeysToParams()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import helium314.keyboard.latin.common.isEmoji
import helium314.keyboard.latin.define.DebugFlags
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.LayoutType
import helium314.keyboard.latin.utils.LayoutUtils
import helium314.keyboard.latin.utils.POPUP_KEYS_LAYOUT
import helium314.keyboard.latin.utils.POPUP_KEYS_NUMBER
import helium314.keyboard.latin.utils.replaceFirst
Expand Down Expand Up @@ -115,7 +114,7 @@ class KeyboardParser(private val params: KeyboardParams, private val context: Co
}

// the kana grid is complete as written: 4 rows with its own side keys, and popups holding only kana
val isKanaGrid = params.mId.isAlphabetKeyboard && params.mId.mSubtype.mainLayoutName == LayoutUtils.JAPANESE_12KEY
val isKanaGrid = params.mId.isKanaGrid
val isCompleteGrid = params.isHexagonal || isKanaGrid

val numberRows = getNumberRows()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,23 @@ object LayoutParser {
private const val TAG = "LayoutParser"
private val layoutCache = hashMapOf<String, (KeyboardParams) -> MutableList<MutableList<KeyData>>>()
private val hexLayoutCache = java.util.concurrent.ConcurrentHashMap<String, Boolean>()
private val kanaGridCache = java.util.concurrent.ConcurrentHashMap<String, Boolean>()
// a comment line in the layout file, so copies of the layout keep the behaviour whatever their name is
private const val KANA_GRID_MARKER = "// kana-grid"

fun clearCache() {
layoutCache.clear()
hexLayoutCache.clear()
kanaGridCache.clear()
}

/** True for a main layout that is a complete kana grid, see [KANA_GRID_MARKER] */
fun isKanaGridLayout(layoutName: String, context: Context?): Boolean {
if (context == null) return false
return kanaGridCache[layoutName] ?: runCatching {
getLayoutFileContent(LayoutType.MAIN, layoutName.substringBefore("+"), context)
.lineSequence().any { it.startsWith(KANA_GRID_MARKER) }
}.getOrDefault(false).also { kanaGridCache[layoutName] = it }
}

fun isHexLayout(layoutName: String, context: Context?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fun createPopupKeysArray(popupSet: PopupSet<*>?, params: KeyboardParams, label:
val popupKeysDelegate = lazy { mutableSetOf<String>() }
val popupKeys by popupKeysDelegate
val baseTypes = if (params.mId.isAlphabetKeyboard) params.mPopupKeyTypes else allPopupKeyTypes
val types = if (params.isHexagonal) {
// hex layouts and kana grids need their layout popups even if the user turned them off: the kana grid can't be typed without
val types = if (params.isHexagonal || params.mId.isKanaGrid) {
listOf(POPUP_KEYS_LAYOUT) + (baseTypes - POPUP_KEYS_LAYOUT)
} else {
baseTypes
Expand Down
75 changes: 74 additions & 1 deletion app/src/test/java/helium314/keyboard/KeyboardParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import helium314.keyboard.keyboard.Keyboard
import helium314.keyboard.keyboard.KeyboardId
import helium314.keyboard.keyboard.KeyboardLayoutSet
import helium314.keyboard.keyboard.internal.KeySpecParser.KeySpecParserError
import helium314.keyboard.keyboard.internal.KeyboardBottomRowGeometry
import helium314.keyboard.keyboard.internal.KeyboardBuilder
import helium314.keyboard.keyboard.internal.KeyboardParams
import helium314.keyboard.keyboard.internal.TouchPositionCorrection
Expand Down Expand Up @@ -46,6 +47,9 @@ import java.util.Locale
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import helium314.keyboard.latin.utils.POPUP_KEYS_ORDER_DEFAULT
import helium314.keyboard.latin.common.Constants.Separators
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue

Expand Down Expand Up @@ -773,6 +777,74 @@ f""", // no newline at the end
}
}

@Test fun japanese12KeyBottomRowIsNotTheSpaceRow() {
val subtype = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(Locale.JAPANESE, LayoutUtils.JAPANESE_12KEY, true)
val (kb, _) = buildKeyboard(EditorInfo(), subtype, KeyboardId.ELEMENT_ALPHABET)
val bottom = KeyboardBottomRowGeometry.findBottomRowKey(kb.sortedKeys)!!
// the emoji and clipboard bottom rows line up with the symbols / enter row, not with the space bar above it
assertEquals(kb.sortedKeys.first { it.code == KeyCode.SYMBOL_ALPHA }.y, bottom.y)
assertTrue(kb.sortedKeys.first { it.code == Constants.CODE_SPACE }.y < bottom.y)
}

@Test fun qwertyBottomRowIsTheSpaceRow() {
val subtype = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(Locale.ENGLISH, "qwerty", true)
val (kb, _) = buildKeyboard(EditorInfo(), subtype, KeyboardId.ELEMENT_ALPHABET)
val space = kb.sortedKeys.first { it.code == Constants.CODE_SPACE }
val bottom = KeyboardBottomRowGeometry.findBottomRowKey(kb.sortedKeys)!!
assertEquals(space.y, bottom.y)
assertEquals(space.height, bottom.height)
}

@Test fun japanese12KeyPopupsStayWhenLayoutPopupsAreTurnedOff() {
val prefs = latinIME.prefs()
val original = prefs.getString(Settings.PREF_POPUP_KEYS_ORDER, null)
try {
prefs.edit { putString(Settings.PREF_POPUP_KEYS_ORDER,
POPUP_KEYS_ORDER_DEFAULT.replace(POPUP_KEYS_LAYOUT + Separators.KV + true, POPUP_KEYS_LAYOUT + Separators.KV + false)) }
Settings.getInstance().loadSettings(latinIME)
assertTrue(POPUP_KEYS_LAYOUT !in Settings.getValues().mPopupKeyTypes)
val subtype = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(Locale.JAPANESE, LayoutUtils.JAPANESE_12KEY, true)
val (_, rows) = buildKeyboard(EditorInfo(), subtype, KeyboardId.ELEMENT_ALPHABET)
val na = rows.flatten().first { it.mLabel == "な" }
assertEquals("なにぬねの", na.mPopupKeys.orEmpty().joinToString("") { it.mLabel!! })
} finally {
prefs.edit { if (original == null) remove(Settings.PREF_POPUP_KEYS_ORDER) else putString(Settings.PREF_POPUP_KEYS_ORDER, original) }
Settings.getInstance().loadSettings(latinIME)
}
}

@Test fun japanese12KeyIsNotSplit() {
val subtype = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(Locale.JAPANESE, LayoutUtils.JAPANESE_12KEY, true)
val (kb, rows) = buildKeyboard(EditorInfo(), subtype, KeyboardId.ELEMENT_ALPHABET, split = true)
assertTrue(kb.mId.mIsSplitLayout && kb.mId.isKanaGrid)
assertEquals(listOf(5, 5, 5, 5), rows.map { it.size }) // no spacers either
}

@Test fun copyOfJapanese12KeyIsStillAKanaGrid() {
val content = latinIME.assets.open("layouts/main/${LayoutUtils.JAPANESE_12KEY}.json").reader().use { it.readText() }
assertTrue(LayoutUtilsCustom.checkLayout(content, latinIME))
val name = LayoutUtilsCustom.getLayoutName("my kana", LayoutType.MAIN, Locale.JAPANESE)
val file = LayoutUtilsCustom.getLayoutFile(name, LayoutType.MAIN, latinIME)
try {
file.writeText(content)
LayoutUtilsCustom.onLayoutFileChanged()
val subtype = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(Locale.JAPANESE, name, true)
val (kb, rows) = buildKeyboard(EditorInfo(), subtype, KeyboardId.ELEMENT_ALPHABET, split = true)
assertTrue(kb.mId.isKanaGrid)
assertEquals(listOf(5, 5, 5, 5), rows.map { it.size })
} finally {
file.delete()
LayoutUtilsCustom.onLayoutFileChanged()
}
}

@Test fun otherLayoutsAndTheSymbolsKeyboardAreNotKanaGrids() {
val japanese = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(Locale.JAPANESE, LayoutUtils.JAPANESE_12KEY, true)
assertFalse(buildKeyboard(EditorInfo(), japanese, KeyboardId.ELEMENT_SYMBOLS).first.mId.isKanaGrid)
val qwerty = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(Locale.ENGLISH, "qwerty", true)
assertFalse(buildKeyboard(EditorInfo(), qwerty, KeyboardId.ELEMENT_ALPHABET).first.mId.isKanaGrid)
}

@Test fun japanese12KeyKeepsNormalSymbolsKeyboard() {
val subtype = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(Locale.JAPANESE, LayoutUtils.JAPANESE_12KEY, true)
val (_, rows) = buildKeyboard(EditorInfo(), subtype, KeyboardId.ELEMENT_SYMBOLS)
Expand All @@ -781,8 +853,9 @@ f""", // no newline at the end
assertTrue(codes.containsAll(listOf(KeyCode.DELETE, KeyCode.SYMBOL_ALPHA, Constants.CODE_SPACE)), "$codes")
}

private fun buildKeyboard(editorInfo: EditorInfo, subtype: InputMethodSubtype, elementId: Int): Pair<Keyboard, List<List<KeyParams>>> {
private fun buildKeyboard(editorInfo: EditorInfo, subtype: InputMethodSubtype, elementId: Int, split: Boolean = false): Pair<Keyboard, List<List<KeyParams>>> {
val layoutParams = KeyboardLayoutSet.Params()
layoutParams.mIsSplitLayoutEnabled = split
val editorInfoField = KeyboardLayoutSet.Params::class.java.getDeclaredField("mEditorInfo").apply { isAccessible = true }
editorInfoField.set(layoutParams, editorInfo)
val modeField = KeyboardLayoutSet.Params::class.java.getDeclaredField("mMode").apply { isAccessible = true }
Expand Down
Loading