From 1eed9bba93f126f1016a341c179c7805fde8e96c Mon Sep 17 00:00:00 2001 From: Codex Mozc Autonomy Date: Thu, 12 Nov 2026 07:10:08 -0500 Subject: [PATCH] Add configurable QWERTY keys to custom keyboards --- .../CustomQwertyKeyDeviceTest.kt | 387 ++++++++++++++++++ .../custom_keyboard/data/KeyDefinition.kt | 3 +- .../import_export/KeyboardBackupPipeline.kt | 8 + .../import_export/KeyboardLayoutExportDtos.kt | 4 +- .../KeyboardLayoutJsonExporter.kt | 1 + .../KeyboardLayoutJsonImporter.kt | 2 +- .../custom_keyboard/ui/KeyEditorFragment.kt | 71 +++- .../custom_keyboard/ui/QwertyKeyEditorView.kt | 210 ++++++++++ .../database/AppDatabase.kt | 8 +- .../ime_service/IMEService.kt | 29 +- .../ime_service/di/AppModule.kt | 2 + .../repository/KeyboardRepository.kt | 8 +- .../setting_activity/AppPreference.kt | 4 + .../CustomKeyboardPreferenceFragment.kt | 4 + .../main/res/layout/fragment_key_editor.xml | 13 + app/src/main/res/values-ja/strings.xml | 24 ++ app/src/main/res/values/strings.xml | 24 ++ app/src/main/res/xml/pref_custom.xml | 9 + .../HierarchicalFlickMigrationTest.kt | 5 +- .../database/QwertyKeyMigrationTest.kt | 53 +++ .../KeyboardLayoutJsonImporterTest.kt | 2 +- .../import_export/QwertyKeyBackupTest.kt | 30 ++ .../ui/QwertyKeyEditorViewTest.kt | 51 +++ .../RomajiBehaviorPersistenceTest.kt | 3 +- .../ng_word/database/NgWordMigrationTest.kt | 5 +- .../controller/QwertyKeyInputController.kt | 249 +++++++++++ .../custom_keyboard/data/KeyModels.kt | 8 +- .../custom_keyboard/data/QwertyKeyConfig.kt | 63 +++ .../custom_keyboard/view/FlickKeyboardView.kt | 51 +++ .../view/QwertyAlternativesView.kt | 82 ++++ .../QwertyKeyInputControllerTest.kt | 73 ++++ .../data/QwertyKeyConfigTest.kt | 41 ++ .../view/QwertyAlternativesViewTest.kt | 37 ++ 33 files changed, 1542 insertions(+), 22 deletions(-) create mode 100644 app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomQwertyKeyDeviceTest.kt create mode 100644 app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/QwertyKeyEditorView.kt create mode 100644 app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/database/QwertyKeyMigrationTest.kt create mode 100644 app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/QwertyKeyBackupTest.kt create mode 100644 app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/QwertyKeyEditorViewTest.kt create mode 100644 custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/QwertyKeyInputController.kt create mode 100644 custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/QwertyKeyConfig.kt create mode 100644 custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/QwertyAlternativesView.kt create mode 100644 custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/controller/QwertyKeyInputControllerTest.kt create mode 100644 custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/data/QwertyKeyConfigTest.kt create mode 100644 custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/QwertyAlternativesViewTest.kt diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomQwertyKeyDeviceTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomQwertyKeyDeviceTest.kt new file mode 100644 index 000000000..8dc098207 --- /dev/null +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomQwertyKeyDeviceTest.kt @@ -0,0 +1,387 @@ +package com.kazumaproject.markdownhelperkeyboard + +import android.content.Intent +import android.graphics.Rect +import android.os.Bundle +import android.os.ParcelFileDescriptor +import android.os.SystemClock +import android.view.InputDevice +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.view.inspector.WindowInspector +import android.widget.TextView +import androidx.navigation.fragment.NavHostFragment +import androidx.preference.PreferenceManager +import androidx.test.core.app.ActivityScenario +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.SdkSuppress +import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry +import androidx.test.runner.lifecycle.Stage +import androidx.test.platform.app.InstrumentationRegistry +import com.kazumaproject.core.domain.skin.KeyboardSkinId +import com.kazumaproject.custom_keyboard.data.* +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui.KeyboardEditorFragment +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui.KeyEditorFragment +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui.QwertyKeyEditorView +import com.kazumaproject.markdownhelperkeyboard.setting_activity.MainActivity +import com.kazumaproject.custom_keyboard.view.FlickKeyboardView +import com.kazumaproject.custom_keyboard.view.QwertyAlternativesView +import com.kazumaproject.markdownhelperkeyboard.ime_service.di.AppModule +import com.kazumaproject.markdownhelperkeyboard.repository.KeyboardRepository +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith + +/** Runs against real IME windows. Use an isolated customqwertytest application ID. */ +@RunWith(AndroidJUnit4::class) +@SdkSuppress(minSdkVersion = 29) +class CustomQwertyKeyDeviceTest { + private val ins = InstrumentationRegistry.getInstrumentation() + private fun shell(command: String) = ParcelFileDescriptor.AutoCloseInputStream( + ins.uiAutomation.executeShellCommand(command)).bufferedReader().use { it.readText().trim() } + private fun main(block: () -> Unit) = ins.runOnMainSync(block) + private fun descendants(view: View): Sequence = sequence { + yield(view) + if (view is ViewGroup) for (i in 0 until view.childCount) yieldAll(descendants(view.getChildAt(i))) + } + private fun views() = WindowInspector.getGlobalWindowViews().asSequence().flatMap(::descendants) + .filter { it.isAttachedToWindow && it.isShown && it.width > 0 && it.height > 0 && + generateSequence(it as View?) { parent -> parent.parent as? View }.all { parent -> parent.alpha > 0.9f } && + it.getGlobalVisibleRect(Rect()) } + private fun await(message: String, predicate: () -> Boolean) { + val deadline = SystemClock.uptimeMillis() + 15000 + while (SystemClock.uptimeMillis() < deadline) { + var ready = false + main { ready = predicate() } + if (ready) return + SystemClock.sleep(50) + } + var details = "" + main { + details = views().filterIsInstance().joinToString { "surface=${it.id}/${it.width}x${it.height}" } + + views().filterIsInstance().joinToString { "editor=${it.config}" } + } + error("$message: $details") + } + private fun key(label: String): Rect { + var result: Rect? = null + await("Missing key $label") { + result = views().filterIsInstance().flatMap(::descendants) + .firstOrNull { it.isShown && ((it as? TextView)?.text?.toString() == label || it.contentDescription == label) } + ?.let { view -> IntArray(2).also(view::getLocationOnScreen).let { Rect(it[0], it[1], it[0] + view.width, it[1] + view.height) } } + result != null + } + return result!! + } + private var downTime = 0L + private fun send(action: Int, x: Float, y: Float) { + if (action == MotionEvent.ACTION_DOWN) downTime = SystemClock.uptimeMillis() + val event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), action, x, y, 0) + event.source = InputDevice.SOURCE_TOUCHSCREEN + try { check(ins.uiAutomation.injectInputEvent(event, true)) } finally { event.recycle() } + } + private fun tap(label: String) { + val bounds = key(label) + send(MotionEvent.ACTION_DOWN, bounds.exactCenterX(), bounds.exactCenterY()) + SystemClock.sleep(40) + send(MotionEvent.ACTION_UP, bounds.exactCenterX(), bounds.exactCenterY()) + SystemClock.sleep(120) + } + private fun hold(label: String, slot: Int? = null, cancel: Boolean = false) { + val key = key(label) + send(MotionEvent.ACTION_DOWN, key.exactCenterX(), key.exactCenterY()) + var popup: QwertyAlternativesView? = null + await("Long-press alternatives not visible") { + popup = views().filterIsInstance().firstOrNull() + popup != null + } + var x = key.exactCenterX() + var y = key.exactCenterY() + if (slot != null) main { + val location = IntArray(2).also(popup!!::getLocationOnScreen) + x = location[0] + (slot % 2 + 0.5f) * popup!!.width / 2 + y = location[1] + (slot / 2 + 0.5f) * popup!!.height / 2 + } + if (slot != null) { send(MotionEvent.ACTION_MOVE, x, y); SystemClock.sleep(60) } + send(if (cancel) MotionEvent.ACTION_CANCEL else MotionEvent.ACTION_UP, x, y) + await("Popup leaked after release") { views().none { it is QwertyAlternativesView } } + } + private fun assertText(scenario: ActivityScenario, expected: String) { + var actual = "" + val deadline = SystemClock.uptimeMillis() + 5000 + do { + scenario.onActivity { actual = it.editText.text.toString() } + if (actual == expected) return + SystemClock.sleep(50) + } while (SystemClock.uptimeMillis() < deadline) + assertEquals(expected, actual) + } + + @Test fun actualImeSupportsConfigurableKeysOnDockedFloatingAndSplitSurfaces() = runBlocking { + val context = ins.targetContext + check(context.packageName.startsWith("com.kazumaproject.customqwertytest")) { "Use an isolated customqwertytest application ID" } + check(shell("getprop ro.kernel.qemu") == "1") { "Run on an emulator" } + val ime = "${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService" + val oldIme = shell("settings get secure default_input_method") + val wasEnabled = shell("ime list -s").lines().contains(ime) + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val db = AppModule.providesLearnDatabase(context) + val repository = KeyboardRepository(db.keyboardLayoutDao()) + val layoutIds = mutableMapOf() + val normal = QwertyKeyLayer("a", "KEY", "peek", 2, + listOf(QwertyPopupSlot("é", "acute"), QwertyPopupSlot(), QwertyPopupSlot("👩‍💻", "emoji"), QwertyPopupSlot("word output", "word"))) + val shifted = normal.copy(output = "keepCase", label = "SHIFTKEY", preview = "shiftPeek", + slots = normal.slots.mapIndexed { i, slot -> if (i == 0) slot.copy(output = "lowCase") else slot }) + val keys = listOf( + KeyData("KEY", 0, 0, false, KeyAction.Text("a"), keyType = KeyType.QWERTY, keyId = "a", qwertyConfig = QwertyKeyConfig(normal, shifted)), + KeyData("NEXT", 0, 1, false, KeyAction.Text("r"), keyType = KeyType.QWERTY, keyId = "r", qwertyConfig = QwertyKeyConfig(QwertyKeyLayer("r", "NEXT", "nextPeek"))), + KeyData("Plain", 0, 2, false, KeyAction.Text("x"), keyId = "plain"), + KeyData("shift", 1, 0, false, KeyAction.ShiftKey, isSpecialKey = true, keyId = "shift"), + KeyData("caps", 1, 1, false, KeyAction.CapLockKey, isSpecialKey = true, keyId = "caps"), + KeyData("space", 1, 2, false, KeyAction.Space, isSpecialKey = true, keyId = "space")) + try { + for (direct in listOf(false, true)) { + layoutIds[direct] = repository.saveLayout(KeyboardLayout(keys, emptyMap(), 3, 2, isDirectMode = direct), "QWERTY device verification $direct", null) + val restored = repository.getFullLayout(layoutIds.getValue(direct)).first() + assertEquals(keys.first().qwertyConfig, restored.keys.first { it.keyId == "a" }.qwertyConfig) + } + shell("ime enable $ime") + shell("ime set $ime") + for (direct in listOf(false, true)) { + val layoutId = layoutIds.getValue(direct) + val stableId = repository.getLayoutsNotFlow().single { it.layoutId == layoutId }.stableId + for (mode in listOf("normal", "floating", "split")) for (preview in listOf(true, false)) { + android.util.Log.i("CustomQwertyDevice", "direct=$direct mode=$mode preview=$preview") + check(prefs.edit().clear() + .putString("keyboard_order_preference", if (mode == "split") "[\"SPLIT\"]" else "[\"CUSTOM\"]") + .putBoolean("save_last_used_keyboard", false) + .putBoolean("keyboard_floating_preference", mode == "floating") + .putBoolean("custom_keyboard_show_key_preview", preview) + .putBoolean("key_popup_use_custom_color_preference", true) + .putInt("key_popup_background_color_preference", 0xff243447.toInt()) + .putInt("key_popup_text_color_preference", 0xffeeeeee.toInt()) + .putBoolean("remember_last_custom_keyboard_preference", true) + .putString("last_used_custom_keyboard_stable_id", stableId) + .putBoolean("independent_multi_touch_preference", true) + .putInt("long_press_timeout_preference", 300) + .putBoolean("live_conversion_preference", false) + .putBoolean("clipboard_preview_enable_preference", false) + .putString("split_keyboard_main_type", "CUSTOM") + .putString("split_keyboard_sub_type", "CUSTOM") + .putString("split_keyboard_main_custom", stableId) + .putString("split_keyboard_sub_custom", stableId) + .putFloat("split_keyboard_main_portrait_width", 180f) + .putFloat("split_keyboard_sub_portrait_width", 180f) + .putString(KeyboardSkinId.PREFERENCE_KEY, if (preview) KeyboardSkinId.DEFAULT.preferenceValue else KeyboardSkinId.CUPERTINO_LIGHT.preferenceValue) + .commit()) + ActivityScenario.launch(Intent(context, FastInputHostActivity::class.java)).use { scenario -> + await("Keyboard surface count") { views().filterIsInstance().count() == if (mode == "split") 2 else 1 } + // Await the initial IME layout and skin transition before the first gesture. + var previous: Rect? = null + var stable = 0 + val deadline = SystemClock.uptimeMillis() + 10000 + while (stable < 6 && SystemClock.uptimeMillis() < deadline) { + val bounds = key("KEY") + stable = if (bounds == previous) stable + 1 else 0 + previous = bounds + SystemClock.sleep(100) + } + check(stable >= 6) { "Keyboard geometry did not settle" } + tap("KEY") + assertText(scenario, "a") + val bounds = key("KEY") + send(MotionEvent.ACTION_DOWN, bounds.exactCenterX(), bounds.exactCenterY()) + SystemClock.sleep(80) + main { + val bubble = views().filterIsInstance().firstOrNull { it.text.toString() == "peek" } + assertEquals("preview setting", preview, bubble != null) + if (preview) assertEquals(0xffeeeeee.toInt(), bubble!!.currentTextColor) + } + send(MotionEvent.ACTION_CANCEL, bounds.exactCenterX(), bounds.exactCenterY()) + assertText(scenario, "a") + hold("KEY") + assertText(scenario, "aé") + hold("KEY", slot = 2) + assertText(scenario, "aé👩‍💻") + hold("KEY", slot = 1) + assertText(scenario, "aé👩‍💻") + hold("KEY", cancel = true) + assertText(scenario, "aé👩‍💻") + tap("shift") + tap("SHIFTKEY") + assertText(scenario, "aé👩‍💻keepCase") + key("KEY") + tap("caps") + hold("SHIFTKEY") + assertText(scenario, "aé👩‍💻keepCaselowCase") + key("SHIFTKEY") + tap("caps") + key("KEY") + tap("Plain") + assertText(scenario, "aé👩‍💻keepCaselowCasex") + if (mode == "normal" && preview) { + overlapHoldAndTap() + assertText(scenario, "aé👩‍💻keepCaselowCasexré") + } + } + } + } + } finally { + layoutIds.values.forEach { repository.deleteLayoutConfirmed(it) } + if (oldIme.isNotEmpty() && oldIme != "null") shell("ime set $oldIme") + if (!wasEnabled) shell("ime disable $ime") + db.close() + } + } + private fun overlapHoldAndTap() { + val first = key("KEY") + val second = key("NEXT") + val started = SystemClock.uptimeMillis() + fun pointers(action: Int, index: Int, two: Boolean) { + val positions = if (two) listOf(first, second) else listOf(first) + val properties = positions.mapIndexed { i, _ -> MotionEvent.PointerProperties().apply { + id = if (i == 0) 3 else 19 + toolType = MotionEvent.TOOL_TYPE_FINGER + } }.toTypedArray() + val coordinates = positions.map { rect -> MotionEvent.PointerCoords().apply { + x = rect.exactCenterX(); y = rect.exactCenterY(); pressure = 1f; size = 1f + } }.toTypedArray() + val event = MotionEvent.obtain(started, SystemClock.uptimeMillis(), + action or (index shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), positions.size, + properties, coordinates, 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0) + try { check(ins.uiAutomation.injectInputEvent(event, true)) } finally { event.recycle() } + } + pointers(MotionEvent.ACTION_DOWN, 0, false) + await("First pointer hold popup") { views().any { it is QwertyAlternativesView } } + pointers(MotionEvent.ACTION_POINTER_DOWN, 1, true) + SystemClock.sleep(40) + pointers(MotionEvent.ACTION_POINTER_UP, 1, true) + await("First hold survived the other key") { views().any { it is QwertyAlternativesView } } + pointers(MotionEvent.ACTION_UP, 0, false) + } + + @Test fun editorConvertsAnExistingKeyAndRetainsDraftAcrossRecreation() = runBlocking { + val context = ins.targetContext + check(context.packageName.startsWith("com.kazumaproject.customqwertytest")) + val db = AppModule.providesLearnDatabase(context) + val repository = KeyboardRepository(db.keyboardLayoutDao()) + val original = KeyData("Old", 0, 0, false, KeyAction.Text("a"), keyId = "editor-key") + val id = repository.saveLayout(KeyboardLayout(listOf(original), emptyMap(), 1, 1), "Editor verification", null) + val expected = QwertyKeyConfig(QwertyKeyLayer("edited", "Key label", "Preview", 2, + listOf(QwertyPopupSlot("one", "First"), QwertyPopupSlot(), QwertyPopupSlot("👩‍💻")))) + try { + ActivityScenario.launch(Intent(context, MainActivity::class.java)).use { scenario -> + fun host(): NavHostFragment = ActivityLifecycleMonitorRegistry.getInstance() + .getActivitiesInStage(Stage.RESUMED).filterIsInstance().first() + .supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment + await("Settings initialized") { runCatching { host().navController.currentDestination != null }.getOrDefault(false) } + main { host().navController.navigate(R.id.keyboardEditorFragment, Bundle().apply { putLong("layoutId", id) }) } + await("Layout editor loaded") { + val fragment = host().childFragmentManager.primaryNavigationFragment as? KeyboardEditorFragment + fragment?.view?.let { descendants(it).filterIsInstance().any { text -> text.text.toString() == "Old" } } == true + } + main { + val editor = host().childFragmentManager.primaryNavigationFragment as KeyboardEditorFragment + editor.onKeySelected("editor-key") + editor.onKeySelected("editor-key") + } + await("Key editor initialized") { + val fragment = host().childFragmentManager.primaryNavigationFragment as? KeyEditorFragment + fragment?.view?.findViewById(R.id.key_label_edittext)?.text?.toString() == "Old" && + fragment.view?.findViewById(R.id.button_done)?.isEnabled == true + } + main { + val fragment = host().childFragmentManager.primaryNavigationFragment as KeyEditorFragment + fragment.requireView().findViewById(R.id.chip_qwerty).performClick() + fragment.requireView().findViewById(R.id.qwerty_key_editor).setConfig(expected) + assertTrue(fragment.requireView().findViewById(R.id.chip_qwerty).isChecked) + assertTrue(fragment.requireView().findViewById(R.id.button_done).isEnabled) + } + scenario.recreate() + await("QWERTY draft restored") { + val fragment = host().childFragmentManager.primaryNavigationFragment as? KeyEditorFragment + fragment?.view?.findViewById(R.id.qwerty_key_editor)?.config == expected + } + main { + val fragment = host().childFragmentManager.primaryNavigationFragment as KeyEditorFragment + val done = fragment.requireView().findViewById(R.id.button_done) + assertTrue(done.isEnabled) + done.performClick() + } + await("Returned to layout editor") { host().childFragmentManager.primaryNavigationFragment is KeyboardEditorFragment } + await("Save action ready") { views().any { it.id == R.id.action_save } } + main { views().first { it.id == R.id.action_save }.performClick() } + val deadline = SystemClock.uptimeMillis() + 10000 + var saved: KeyboardLayout + do { + saved = repository.getFullLayout(id).first() + if (saved.keys.single().keyType == KeyType.QWERTY) break + SystemClock.sleep(100) + } while (SystemClock.uptimeMillis() < deadline) + assertEquals(KeyType.QWERTY, saved.keys.single().keyType) + assertEquals(expected, saved.keys.single().qwertyConfig) + assertEquals(original.row, saved.keys.single().row) + assertEquals(original.column, saved.keys.single().column) + } + } finally { repository.deleteLayoutConfirmed(id); db.close() } + } + + @Test fun romajiInputAndLayoutDuplicationRetainTheConfiguredOutputs() = runBlocking { + val context = ins.targetContext + check(context.packageName.startsWith("com.kazumaproject.customqwertytest")) + val ime = "${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService" + val oldIme = shell("settings get secure default_input_method") + val wasEnabled = shell("ime list -s").lines().contains(ime) + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val db = AppModule.providesLearnDatabase(context) + val repository = KeyboardRepository(db.keyboardLayoutDao()) + val normal = QwertyKeyLayer("a", "KEY", columns = 2, slots = listOf( + QwertyPopupSlot("ka", "literal"), QwertyPopupSlot(), QwertyPopupSlot("👩‍💻"))) + val config = QwertyKeyConfig(normal, normal.copy(label = "SHIFTKEY", output = "a")) + val keys = listOf( + KeyData("KEY", 0, 0, false, KeyAction.Text("a"), keyType = KeyType.QWERTY, keyId = "a", qwertyConfig = config), + KeyData("shift", 0, 1, false, KeyAction.ShiftKey, isSpecialKey = true, keyId = "shift")) + val id = repository.saveLayout(KeyboardLayout(keys, emptyMap(), 2, 1, isRomaji = true), "Romaji verification", null) + var duplicateId: Long? = null + try { + val before = repository.getLayoutsNotFlow().map { it.layoutId }.toSet() + repository.duplicateLayout(id) + duplicateId = repository.getLayoutsNotFlow().single { it.layoutId !in before }.layoutId + assertEquals(config, repository.getFullLayout(duplicateId!!).first().keys.first { it.keyId == "a" }.qwertyConfig) + repository.deleteLayoutConfirmed(duplicateId!!) + duplicateId = null + val stableId = repository.getLayoutsNotFlow().single { it.layoutId == id }.stableId + check(prefs.edit().clear().putString("keyboard_order_preference", "[\"CUSTOM\"]") + .putBoolean("save_last_used_keyboard", false) + .putBoolean("remember_last_custom_keyboard_preference", true) + .putString("last_used_custom_keyboard_stable_id", stableId) + .putBoolean("live_conversion_preference", false) + .putBoolean("custom_keyboard_two_words_preference", true) + .putInt("long_press_timeout_preference", 300).commit()) + shell("ime enable $ime") + shell("ime set $ime") + ActivityScenario.launch(Intent(context, FastInputHostActivity::class.java)).use { scenario -> + key("KEY") + SystemClock.sleep(800) + tap("KEY") + assertText(scenario, "あ") + tap("shift") + tap("SHIFTKEY") + assertText(scenario, "あa") + key("KEY") + hold("KEY") + assertText(scenario, "あaka") + } + } finally { + duplicateId?.let { repository.deleteLayoutConfirmed(it) } + repository.deleteLayoutConfirmed(id) + if (oldIme.isNotEmpty() && oldIme != "null") shell("ime set $oldIme") + if (!wasEnabled) shell("ime disable $ime") + db.close() + } + } + +} diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/data/KeyDefinition.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/data/KeyDefinition.kt index 46d970103..23e8c3756 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/data/KeyDefinition.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/data/KeyDefinition.kt @@ -46,5 +46,6 @@ data class KeyDefinition( val doubleTapAction: String? = null, val doubleTapPolicy: String? = null, val textInputBehavior: String = KeyTextInputBehavior.NORMAL.dbValue, - val hierarchicalFlickJson: String? = null + val hierarchicalFlickJson: String? = null, + val qwertyConfigJson: String? = null ) diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardBackupPipeline.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardBackupPipeline.kt index 1deadaab1..ca8a13ac0 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardBackupPipeline.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardBackupPipeline.kt @@ -8,6 +8,7 @@ import com.google.gson.reflect.TypeToken import com.kazumaproject.custom_keyboard.data.CircularFlickDirection import com.kazumaproject.custom_keyboard.data.FlickDirection import com.kazumaproject.custom_keyboard.data.KeyActionMapper +import com.kazumaproject.custom_keyboard.data.QwertyKeyConfigCodec import com.kazumaproject.custom_keyboard.data.KeyType import com.kazumaproject.custom_keyboard.data.KeyTextInputBehavior import com.kazumaproject.custom_keyboard.data.KeyIconBuiltInDrawable @@ -288,6 +289,12 @@ object KeyboardBackupNormalizer { return@mapIndexedNotNull null } + val qwertyConfig = QwertyKeyConfigCodec.decode(keyDto.qwerty?.toString()) + if (keyDto.keyType == KeyType.QWERTY.name && qwertyConfig == null) { + errors += KeyboardLayoutImportError.ValidationFailed(layoutIndex, + "Invalid QWERTY configuration for key ${keyIndex + 1}") + return@mapIndexedNotNull null + } val identifier = keyDto.keyIdentifier ?.takeIf { it.isNotBlank() && it !in usedKeyIdentifiers } ?: UUID.randomUUID().toString().also { @@ -377,6 +384,7 @@ object KeyboardBackupNormalizer { doubleTapAction = normalizedDoubleTapAction, doubleTapPolicy = normalizedDoubleTapPolicy, hierarchicalFlickJson = hierarchicalFlickJson, + qwertyConfigJson = qwertyConfig?.let(QwertyKeyConfigCodec::encode), textInputBehavior = KeyTextInputBehavior .fromDbValue(keyDto.textInputBehavior) .dbValue diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutExportDtos.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutExportDtos.kt index 73bc12112..ab8638258 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutExportDtos.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutExportDtos.kt @@ -114,7 +114,9 @@ data class KeyDefinitionDto( @SerializedName("textInputBehavior") val textInputBehavior: String? = null, @SerializedName("hierarchicalFlick") - val hierarchicalFlick: JsonElement? = null + val hierarchicalFlick: JsonElement? = null, + @SerializedName("qwerty") + val qwerty: JsonElement? = null ) data class FlickMappingDto( diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonExporter.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonExporter.kt index 788474e24..a99d9f7c0 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonExporter.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonExporter.kt @@ -123,6 +123,7 @@ internal fun KeyWithFlicks.toExportDto(): KeyWithFlicksExportDto { hierarchicalFlick = this.key.hierarchicalFlickJson?.let { JsonParser.parseString(it) }, + qwerty = this.key.qwertyConfigJson?.let(JsonParser::parseString), textInputBehavior = this.key.textInputBehavior ), flicks = this.flicks.map { diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonImporter.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonImporter.kt index 5b914de93..b76c7ad20 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonImporter.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonImporter.kt @@ -24,7 +24,7 @@ object KeyboardLayoutJsonImporter { /** * 想定する最新の schemaVersion。 */ - const val LATEST_SCHEMA_VERSION: Int = 5 + const val LATEST_SCHEMA_VERSION: Int = 6 internal val gson: Gson by lazy { GsonBuilder() diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/KeyEditorFragment.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/KeyEditorFragment.kt index c47cff94b..6bb4d471f 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/KeyEditorFragment.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/KeyEditorFragment.kt @@ -45,6 +45,9 @@ import com.kazumaproject.custom_keyboard.data.KeyIconResolver import com.kazumaproject.custom_keyboard.data.KeyIconType import com.kazumaproject.custom_keyboard.data.KeyItem import com.kazumaproject.custom_keyboard.data.KeyTextInputBehavior +import com.kazumaproject.custom_keyboard.data.QwertyKeyConfig +import com.kazumaproject.custom_keyboard.data.QwertyKeyLayer +import com.kazumaproject.custom_keyboard.data.QwertyKeyConfigCodec import com.kazumaproject.custom_keyboard.data.KeyType import com.kazumaproject.custom_keyboard.data.SpecialKeyColorStyle import com.kazumaproject.custom_keyboard.data.automaticDoubleTapPolicy @@ -104,6 +107,10 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { private val binding get() = _binding!! private var currentKeyData: KeyData? = null + private var qwertyDraftInitialized = false + private var editingKeyIdentifier: String? = null + private var restoredEditingKeyIdentifier: String? = null + private var savedQwertyDraft: QwertyKeyConfig? = null private var currentFlickItems = mutableListOf() private var currentToggleFlickItems = mutableListOf() @@ -175,6 +182,9 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { _binding = FragmentKeyEditorBinding.bind(view) binding.buttonDone.isEnabled = false + restoredEditingKeyIdentifier = savedInstanceState?.getString("qwertyEditingKey") + savedQwertyDraft = QwertyKeyConfigCodec.decode(savedInstanceState?.getString("qwertyDraft"), requireOutput = false) + binding.qwertyKeyEditor.onChanged = { updateDoneButtonState() } setupToolbarAndMenu() setupGridEditor() setupUIListeners() @@ -549,6 +559,7 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { } private fun handleSpecialCategoryUi() { + binding.qwertyKeyEditor.isVisible = false val isFlick = binding.specialCategoryChipGroup.checkedChipId == R.id.chip_special_flick binding.keyLabelLayout.isVisible = false @@ -580,6 +591,30 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { private fun handleInputStyleUi() { val selectedStyle = binding.inputStyleChipGroup.checkedChipId + val isQwerty = selectedStyle == R.id.chip_qwerty + binding.qwertyKeyEditor.isVisible = isQwerty + if (isQwerty) { + binding.keyLabelLayout.isVisible = false + binding.flickGridEditorView.isVisible = false + binding.circularFlickEditorGroup.isVisible = false + binding.specialFlickEditorGroup.isVisible = false + binding.textSelectedDirection.isVisible = false + binding.textCharInputLayout.isVisible = false + binding.textOutputModeTitle.isVisible = false + binding.outputModeChipGroup.isVisible = false + binding.customKeyboardTargetLayout.isVisible = false + currentCellMode = null + if (!qwertyDraftInitialized) { + qwertyDraftInitialized = true + val key = currentKeyData + binding.qwertyKeyEditor.setConfig(savedQwertyDraft ?: key?.qwertyConfig ?: QwertyKeyConfig( + QwertyKeyLayer(output = textOutputFromAction(key?.action) + .ifEmpty { key?.label.orEmpty() }, label = key?.label.orEmpty()) + )) + } + updateIconOverrideVisibility() + return + } val isCircular = selectedStyle == R.id.chip_circular_flick val isFlickLongPress = selectedStyle == R.id.chip_flick_long_press @@ -1036,6 +1071,11 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { if (outputEditMode != OutputEditMode.TOGGLE) { binding.textCharInputLayout.error = null } + if (binding.keyTypeChipGroup.checkedChipId == R.id.chip_normal && + binding.inputStyleChipGroup.checkedChipId == R.id.chip_qwerty) { + binding.buttonDone.isEnabled = binding.qwertyKeyEditor.isInputValid + return + } val isEnabled = when (binding.keyTypeChipGroup.checkedChipId) { R.id.chip_special -> { val isFlick = @@ -1122,7 +1162,8 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { // legacy KeyData.keyId. selectedKeyIdentifier carries item.id for // flexible layouts (so half-cell keys and editor-created keys // resolve correctly even when keyData.keyId is null/blank). - val selectedId = state.selectedKeyIdentifier + val selectedId = state.selectedKeyIdentifier ?: restoredEditingKeyIdentifier.takeIf { savedQwertyDraft != null } + editingKeyIdentifier = selectedId val selectedKeyItem = selectedId?.let { id -> state.layout.items .filterIsInstance() @@ -1246,7 +1287,9 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { outputEditMode = OutputEditMode.NORMAL // Restore the supported editor. - if (key.keyType == KeyType.FLICK_LONG_PRESS) { + if (key.keyType == KeyType.QWERTY || savedQwertyDraft != null) { + binding.inputStyleChipGroup.check(R.id.chip_qwerty) + } else if (key.keyType == KeyType.FLICK_LONG_PRESS) { binding.inputStyleChipGroup.check(R.id.chip_flick_long_press) } else if (key.keyType == KeyType.CIRCULAR_FLICK) { binding.inputStyleChipGroup.check(R.id.chip_circular_flick) @@ -1662,6 +1705,7 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { return } + var newQwertyConfig: QwertyKeyConfig? = null val newLabel: String val newKeyType: KeyType val isSpecial: Boolean @@ -1784,7 +1828,15 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { val isFlickLongPress = binding.inputStyleChipGroup.checkedChipId == R.id.chip_flick_long_press - if (isCircular) { + if (binding.inputStyleChipGroup.checkedChipId == R.id.chip_qwerty) { + if (!binding.qwertyKeyEditor.isInputValid) return + newQwertyConfig = binding.qwertyKeyEditor.config + newKeyType = KeyType.QWERTY + newLabel = newQwertyConfig.normal.displayLabel + newAction = KeyAction.Text(newQwertyConfig.normal.output) + newLongPressFlickMap = emptyMap() + newTwoStepLongPressMap = emptyMap() + } else if (isCircular) { newKeyType = KeyType.CIRCULAR_FLICK newLabel = binding.keyLabelEdittext.text.toString() val tapOutput = currentCircularFlickMaps @@ -1915,7 +1967,8 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { isSpecialKey = isSpecial, action = newAction, // IMPORTANT: special flick should still be flickable (KeyType != NORMAL) - isFlickable = (newKeyType != KeyType.NORMAL), + isFlickable = newKeyType !in setOf(KeyType.NORMAL, KeyType.QWERTY), + qwertyConfig = newQwertyConfig, drawableResId = newDrawableResId, icon = savedIconRef, rowSpan = if (isFlexibleSizeEditing) { @@ -1974,6 +2027,15 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { } + override fun onSaveInstanceState(outState: Bundle) { + if (_binding != null && binding.keyTypeChipGroup.checkedChipId == R.id.chip_normal && + binding.inputStyleChipGroup.checkedChipId == R.id.chip_qwerty) { + outState.putString("qwertyDraft", QwertyKeyConfigCodec.encode(binding.qwertyKeyEditor.config)) + outState.putString("qwertyEditingKey", editingKeyIdentifier) + } + super.onSaveInstanceState(outState) + } + override fun onDestroyView() { super.onDestroyView() if (!didSaveKey) { @@ -1982,6 +2044,7 @@ class KeyEditorFragment : Fragment(R.layout.fragment_key_editor) { } viewModel.doneNavigatingToKeyEditor() + qwertyDraftInitialized = false _binding = null } } diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/QwertyKeyEditorView.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/QwertyKeyEditorView.kt new file mode 100644 index 000000000..3cb97eb7f --- /dev/null +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/QwertyKeyEditorView.kt @@ -0,0 +1,210 @@ +package com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui + +import android.content.Context +import android.text.InputFilter +import android.text.InputType +import android.util.AttributeSet +import android.view.Gravity +import android.widget.CheckBox +import android.widget.EditText +import android.widget.LinearLayout +import android.widget.RadioButton +import android.widget.RadioGroup +import android.widget.TextView +import androidx.core.widget.doAfterTextChanged +import com.google.android.material.button.MaterialButton +import com.google.android.material.textfield.TextInputLayout +import com.kazumaproject.custom_keyboard.data.QwertyKeyConfig +import com.kazumaproject.custom_keyboard.data.QwertyKeyLayer +import com.kazumaproject.custom_keyboard.data.QwertyPopupSlot +import com.kazumaproject.markdownhelperkeyboard.R + +/** Keeps the whole draft in one immutable model while switching layers or grid cells. */ +class QwertyKeyEditorView @JvmOverloads constructor( + context: Context, attrs: AttributeSet? = null +) : LinearLayout(context, attrs) { + var config = QwertyKeyConfig() + private set + var onChanged: (() -> Unit)? = null + private var shifted = false + private var binding = false + private var selectedSlot = -1 + private val normalTab = RadioButton(context).apply { id = generateViewId(); setText(R.string.qwerty_layer_normal) } + private val shiftTab = RadioButton(context).apply { id = generateViewId(); setText(R.string.qwerty_layer_shift) } + private val tabs = RadioGroup(context).apply { + orientation = HORIZONTAL + addView(normalTab) + addView(shiftTab) + check(normalTab.id) + } + private val overrideShift = CheckBox(context).apply { setText(R.string.qwerty_custom_shift) } + private val label = field(R.string.qwerty_key_label) + private val output = field(R.string.qwerty_tap_output) + private val preview = field(R.string.qwerty_preview_label) + private val columns = field(R.string.qwerty_columns).apply { inputType = InputType.TYPE_CLASS_NUMBER } + private val grid = LinearLayout(context).apply { orientation = VERTICAL } + private val selection = TextView(context) + private val slotOutput = field(R.string.qwerty_candidate_output) + private val slotLabel = field(R.string.qwerty_candidate_label) + private val controls = LinearLayout(context).apply { orientation = VERTICAL } + private val append = button(R.string.qwerty_add_slot) { + if (layer().slots.size < QwertyKeyConfig.MAX_SLOTS) { + val slots = layer().slots + QwertyPopupSlot() + selectedSlot = slots.lastIndex + updateLayer(layer().copy(slots = slots)) + render() + } + } + private val before = button(R.string.qwerty_move_before) { moveSlot(-1) } + private val after = button(R.string.qwerty_move_after) { moveSlot(1) } + private val clear = button(R.string.qwerty_clear_slot) { changeSlot(QwertyPopupSlot()); render() } + private val remove = button(R.string.qwerty_remove_slot) { + val slots = layer().slots.toMutableList() + if (selectedSlot in slots.indices) { + slots.removeAt(selectedSlot) + selectedSlot = selectedSlot.coerceAtMost(slots.lastIndex) + updateLayer(layer().copy(slots = slots)) + render() + } + } + val isInputValid: Boolean get() = config.isValid() && columns.text.toString().toIntOrNull() in 1..QwertyKeyConfig.MAX_COLUMNS + + init { + orientation = VERTICAL + addView(tabs) + addView(overrideShift) + listOf(label, output, preview, columns).forEach { addView(it.parent.parent as TextInputLayout) } + addView(TextView(context).apply { setText(R.string.qwerty_grid_help) }) + addView(grid) + addView(append) + addView(selection) + controls.addView(slotOutput.parent.parent as TextInputLayout) + controls.addView(slotLabel.parent.parent as TextInputLayout) + listOf(before, after, clear, remove).forEach(controls::addView) + addView(controls) + tabs.setOnCheckedChangeListener { _, id -> + shifted = id == shiftTab.id + selectedSlot = -1 + render() + } + overrideShift.setOnCheckedChangeListener { _, checked -> + if (!binding) { + config = config.copy(shifted = if (checked) config.normal.uppercase() else null) + render() + onChanged?.invoke() + } + } + listOf(label, output, preview, columns).forEach { edit -> + edit.doAfterTextChanged { + if (!binding) { + updateLayer(layer().copy(label = label.text.toString(), output = output.text.toString(), + preview = preview.text.toString(), columns = columns.text.toString().toIntOrNull() + ?.takeIf { it in 1..QwertyKeyConfig.MAX_COLUMNS } ?: layer().columns)) + (columns.parent.parent as TextInputLayout).error = if (columns.text.toString().toIntOrNull() in 1..QwertyKeyConfig.MAX_COLUMNS) + null else context.getString(R.string.qwerty_columns_error, QwertyKeyConfig.MAX_COLUMNS) + renderGrid() + } + } + } + listOf(slotOutput, slotLabel).forEach { edit -> + edit.doAfterTextChanged { + if (!binding) { + changeSlot(QwertyPopupSlot(slotOutput.text.toString(), slotLabel.text.toString())) + renderGrid() + } + } + } + render() + } + + fun setConfig(value: QwertyKeyConfig) { + config = value + selectedSlot = -1 + render() + onChanged?.invoke() + } + + private fun layer(): QwertyKeyLayer = if (shifted) config.shifted ?: config.normal.uppercase() else config.normal + private fun editable() = !shifted || config.shifted != null + private fun updateLayer(value: QwertyKeyLayer) { + if (!editable()) return + config = if (shifted) config.copy(shifted = value) else config.copy(normal = value) + onChanged?.invoke() + } + private fun changeSlot(value: QwertyPopupSlot) { + val slots = layer().slots.toMutableList() + if (selectedSlot !in slots.indices) return + slots[selectedSlot] = value + updateLayer(layer().copy(slots = slots)) + } + private fun moveSlot(delta: Int) { + val slots = layer().slots.toMutableList() + val destination = selectedSlot + delta + if (selectedSlot !in slots.indices || destination !in slots.indices) return + val previous = slots[selectedSlot] + slots[selectedSlot] = slots[destination] + slots[destination] = previous + selectedSlot = destination + updateLayer(layer().copy(slots = slots)) + render() + } + private fun render() { + binding = true + val value = layer() + overrideShift.visibility = if (shifted) VISIBLE else GONE + overrideShift.isChecked = config.shifted != null + label.setText(value.label) + output.setText(value.output) + preview.setText(value.preview) + columns.setText(value.columns.toString()) + listOf(label, output, preview, columns).forEach { it.isEnabled = editable() } + append.isEnabled = editable() && value.slots.size < QwertyKeyConfig.MAX_SLOTS + val slot = value.slots.getOrNull(selectedSlot) + selection.text = if (slot == null) "" else context.getString(R.string.qwerty_selected_slot, + selectedSlot / value.columns + 1, selectedSlot % value.columns + 1) + controls.visibility = if (slot == null) GONE else VISIBLE + slotOutput.setText(slot?.output.orEmpty()) + slotLabel.setText(slot?.label.orEmpty()) + listOf(slotOutput, slotLabel, before, after, clear, remove).forEach { it.isEnabled = editable() } + before.isEnabled = editable() && selectedSlot > 0 + after.isEnabled = editable() && selectedSlot < value.slots.lastIndex + renderGrid() + binding = false + } + private fun renderGrid() { + grid.removeAllViews() + val value = layer() + val rows = value.slots.chunked(value.columns) + rows.forEachIndexed { rowIndex, slots -> + val row = LinearLayout(context).apply { orientation = HORIZONTAL; gravity = Gravity.CENTER_VERTICAL } + repeat(value.columns) { col -> + val index = rowIndex * value.columns + col + val slot = slots.getOrNull(col) + row.addView(MaterialButton(context).apply { + text = if (slot == null || slot.output.isEmpty()) context.getString(R.string.qwerty_empty_slot) else slot.displayLabel + maxLines = 2 + minWidth = 0 + minimumWidth = 0 + contentDescription = context.getString(R.string.qwerty_selected_slot, rowIndex + 1, col + 1) + ": " + text + isEnabled = slot != null + isCheckable = true + isChecked = index == selectedSlot + setOnClickListener { selectedSlot = index; render() } + }, LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f)) + } + grid.addView(row) + } + } + private fun field(hintRes: Int): EditText { + val layout = TextInputLayout(context).apply { hint = context.getString(hintRes) } + return com.google.android.material.textfield.TextInputEditText(context).apply { + inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS + filters = arrayOf(InputFilter.LengthFilter(QwertyKeyConfig.MAX_TEXT_LENGTH)) + layout.addView(this, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)) + } + } + private fun button(textRes: Int, action: () -> Unit) = MaterialButton(context).apply { + setText(textRes) + setOnClickListener { action() } + } +} diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/database/AppDatabase.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/database/AppDatabase.kt index 2d7d70e7c..674bc28ab 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/database/AppDatabase.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/database/AppDatabase.kt @@ -84,7 +84,7 @@ import com.kazumaproject.markdownhelperkeyboard.zeroquery.custom.CustomZeroQuery CustomZeroQueryEntry::class, TextMacro::class, ], - version = 49, + version = 50, exportSchema = false ) @TypeConverters( @@ -116,6 +116,12 @@ abstract class AppDatabase : RoomDatabase() { abstract fun textMacroDao(): TextMacroDao companion object { + val MIGRATION_49_50 = object : Migration(49, 50) { + override fun migrate(db: SupportSQLiteDatabase) { + db.execSQL("ALTER TABLE `key_definitions` ADD COLUMN `qwertyConfigJson` TEXT") + } + } + val MIGRATION_1_2 = object : Migration(1, 2) { override fun migrate(db: SupportSQLiteDatabase) { diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/IMEService.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/IMEService.kt index 91574ead9..354c4ea27 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/IMEService.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/IMEService.kt @@ -15736,6 +15736,16 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection, ) } + private fun currentCustomQwertyPopupViewStyleSet(): QwertyPopupViewStyleSet { + val styles = currentQwertyPopupViewStyleSet() + if (keyboardThemeMode != "custom") return styles + fun colors(style: PopupViewStyle) = style.copy( + backgroundColor = style.backgroundColor ?: customThemeSpecialKeyColor, + textColor = style.textColor ?: customThemeSpecialKeyTextColor + ) + return styles.copy(keyPreview = colors(styles.keyPreview), variation = colors(styles.variation)) + } + private fun currentFlickPopupViewStyleSet(): FlickPopupViewStyleSet { return FlickPopupViewStyleSet( directional = PopupViewStyle( @@ -15831,6 +15841,7 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection, specialKeyTextSizeSp = appPreference.flick_special_key_text_size_sp ?: 16.0f ) flickView.applyPopupViewStyleSet(currentFlickPopupViewStyleSet()) + flickView.setQwertyPopupSettings(appPreference.custom_keyboard_show_key_preview, currentCustomQwertyPopupViewStyleSet()) applyCurrentFlickGuidePreference(flickView) flickView.setFlickGuideTextSizeSp( (flickGuideTextSizeSpPreference ?: 9).coerceIn(6, 16).toFloat() @@ -16710,8 +16721,18 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection, consumeCustomKeyboardOneShotShift() } + override fun onResolvedText(text: String) { + handleAction(KeyAction.Text(text), false, caseResolved = true) + } + override fun onAction(action: KeyAction, isFlick: Boolean) { - if (deferCursorInput { onAction(action, isFlick) }) return + handleAction(action, isFlick, caseResolved = false) + } + + private fun handleAction(action: KeyAction, isFlick: Boolean, caseResolved: Boolean) { + if (deferCursorInput { handleAction(action, isFlick, caseResolved) }) return + fun outputCase(text: String): String = + if (caseResolved) text else applyCustomLayoutShiftAndCapLock(text) activateSplitView(flickView) if (isKeyboardLayoutEditModeActive()) return flushPendingBackspacesBefore(action) @@ -16746,7 +16767,7 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection, when (qwertyMode.value) { TenKeyQWERTYMode.Custom -> { if (text.isEmpty()) return - val shiftedText = applyCustomLayoutShiftAndCapLock(text) + val shiftedText = outputCase(text) if (dispatchDirectTextIfNeeded(shiftedText)) { consumeCustomKeyboardOneShotShift() return @@ -16773,7 +16794,7 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection, } } else { _inputString.update { - applyCustomLayoutShiftAndCapLock( + outputCase( sb.toString() ) } @@ -16781,7 +16802,7 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection, } else { _inputString.update { - applyCustomLayoutShiftAndCapLock( + outputCase( customRomajiScreenConverter?.convert(sb.toString()) ?: sb.toString() ) diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/di/AppModule.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/di/AppModule.kt index 690e28ddb..347fcfa80 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/di/AppModule.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/ime_service/di/AppModule.kt @@ -61,6 +61,7 @@ import com.kazumaproject.markdownhelperkeyboard.database.AppDatabase.Companion.M import com.kazumaproject.markdownhelperkeyboard.database.AppDatabase.Companion.MIGRATION_44_45 import com.kazumaproject.markdownhelperkeyboard.database.AppDatabase.Companion.MIGRATION_45_46 import com.kazumaproject.markdownhelperkeyboard.database.AppDatabase.Companion.MIGRATION_47_48 +import com.kazumaproject.markdownhelperkeyboard.database.AppDatabase.Companion.MIGRATION_49_50 import com.kazumaproject.markdownhelperkeyboard.database.AppDatabase.Companion.MIGRATION_48_49 import com.kazumaproject.markdownhelperkeyboard.database.AppDatabase.Companion.MIGRATION_46_47 import com.kazumaproject.markdownhelperkeyboard.database.AppDatabase.Companion.MIGRATION_2_3 @@ -168,6 +169,7 @@ object AppModule { MIGRATION_46_47, MIGRATION_47_48, MIGRATION_48_49, + MIGRATION_49_50, ) .build() diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/repository/KeyboardRepository.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/repository/KeyboardRepository.kt index 9c84caf6a..f2e88826b 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/repository/KeyboardRepository.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/repository/KeyboardRepository.kt @@ -7,6 +7,7 @@ import com.kazumaproject.custom_keyboard.data.FlickDirection import com.kazumaproject.custom_keyboard.data.GridPlacement import com.kazumaproject.custom_keyboard.data.KeyAction import com.kazumaproject.custom_keyboard.data.KeyActionMapper +import com.kazumaproject.custom_keyboard.data.QwertyKeyConfigCodec import com.kazumaproject.custom_keyboard.data.KeyData import com.kazumaproject.custom_keyboard.data.KeyIconBuiltInDrawable import com.kazumaproject.custom_keyboard.data.KeyIconRef @@ -937,7 +938,7 @@ class KeyboardRepository @Inject constructor( label = dbKey.label, row = dbKey.row, column = dbKey.column, - isFlickable = dbKey.keyType != KeyType.NORMAL, + isFlickable = dbKey.keyType !in setOf(KeyType.NORMAL, KeyType.QWERTY), keyType = dbKey.keyType, rowSpan = dbKey.rowSpan, colSpan = dbKey.colSpan, @@ -948,6 +949,7 @@ class KeyboardRepository @Inject constructor( action = null, specialKeyColorStyle = specialKeyColorStyle, doubleTapBinding = doubleTapBinding, + qwertyConfig = QwertyKeyConfigCodec.decode(dbKey.qwertyConfigJson), textInputBehavior = KeyTextInputBehavior.fromDbValue(dbKey.textInputBehavior) ) } else { @@ -955,7 +957,7 @@ class KeyboardRepository @Inject constructor( label = dbKey.label, row = dbKey.row, column = dbKey.column, - isFlickable = dbKey.keyType != KeyType.NORMAL, + isFlickable = dbKey.keyType !in setOf(KeyType.NORMAL, KeyType.QWERTY), keyType = dbKey.keyType, rowSpan = dbKey.rowSpan, colSpan = dbKey.colSpan, @@ -966,6 +968,7 @@ class KeyboardRepository @Inject constructor( action = restoredAction, specialKeyColorStyle = specialKeyColorStyle, doubleTapBinding = doubleTapBinding, + qwertyConfig = QwertyKeyConfigCodec.decode(dbKey.qwertyConfigJson), textInputBehavior = KeyTextInputBehavior.fromDbValue(dbKey.textInputBehavior) ) } @@ -1249,6 +1252,7 @@ class KeyboardRepository @Inject constructor( hierarchicalFlickJson = if (keyData.keyType == KeyType.HIERARCHICAL_FLICK) { uiLayout.preservedFlickDefinitions[keyIdentifier] } else null, + qwertyConfigJson = keyData.qwertyConfig?.let(QwertyKeyConfigCodec::encode), textInputBehavior = keyData.textInputBehavior.dbValue ) ) diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/setting_activity/AppPreference.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/setting_activity/AppPreference.kt index 9a310f24c..ac692790a 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/setting_activity/AppPreference.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/setting_activity/AppPreference.kt @@ -2859,6 +2859,10 @@ object AppPreference { it.putBoolean(CUSTOM_DIRECT_INPUT_REPLACE_COMPOSING_KEY, value) } + var custom_keyboard_show_key_preview: Boolean + get() = preferences.getBoolean("custom_keyboard_show_key_preview", true) + set(value) = preferences.edit { it.putBoolean("custom_keyboard_show_key_preview", value) } + var custom_keyboard_input_in_empty_areas_preference: Boolean get() = preferences.getBoolean( CUSTOM_KEYBOARD_INPUT_IN_EMPTY_AREAS_PREFERENCE.first, diff --git a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/setting_activity/ui/setting/CustomKeyboardPreferenceFragment.kt b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/setting_activity/ui/setting/CustomKeyboardPreferenceFragment.kt index 9413a6d2e..f88f20d87 100644 --- a/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/setting_activity/ui/setting/CustomKeyboardPreferenceFragment.kt +++ b/app/src/main/java/com/kazumaproject/markdownhelperkeyboard/setting_activity/ui/setting/CustomKeyboardPreferenceFragment.kt @@ -33,6 +33,10 @@ class CustomKeyboardPreferenceFragment : AsyncPreferenceFragment() { } } + findPreference("custom_qwerty_popup_style")?.setOnPreferenceClickListener { + navigateSafely(R.id.qwertyPopupStyleSettingFragment) + true + } applyLegacySearchResultFilterIfNeeded() } } diff --git a/app/src/main/res/layout/fragment_key_editor.xml b/app/src/main/res/layout/fragment_key_editor.xml index 54581c997..4a0d35233 100644 --- a/app/src/main/res/layout/fragment_key_editor.xml +++ b/app/src/main/res/layout/fragment_key_editor.xml @@ -128,6 +128,13 @@ android:layout_marginTop="8dp" app:singleSelection="true"> + + + + この入力欄ではマッシュルームを使用できません 選択中のモードキーのハイライトを残す キーの配置がフリック入力の場合、モード切り替え後も、現在のひらがな・英語・数字のモードキーにハイライトを残します。 + + QWERTY + 通常 + Shift / Caps Lock + Shift 専用の設定を使う + キーの表示(省略時は入力文字列) + タップで入力する文字列 + 押下プレビューの表示(省略時は入力文字列) + ポップアップの列数 + 列数は 1〜%1$d で指定してください + マスをタップして表示と入力を編集します。「空白にする」は位置を残します。Shift 専用設定がない場合、英字は大文字になります。 + 候補の入力文字列(空欄なら空白マス) + 候補の表示(省略時は入力文字列) + マスを追加 + 前へ移動 + 後ろへ移動 + 空白にする + マスを削除 + 空白 + %1$d 行 %2$d 列 + QWERTY 型キーの押下プレビューを表示 + 押したキーの文字を拡大表示します。長押し候補は表示設定にかかわらず選択できます。 + QWERTY 型ポップアップの表示 + 標準 QWERTY とサイズ・文字設定を共用します diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7a4f3ea3b..e3833ba1a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1980,4 +1980,28 @@ ABC abc 0123456789 ABC 12345 Mushroom is unavailable in this input field Keep the selected mode key highlighted In the flick key layout, keep the current Hiragana, English, or Number mode key highlighted after switching modes. + + QWERTY + Normal + Shift / Caps Lock + Use custom Shift settings + Key label (empty: input text) + Tap input text + Key preview text (empty: input text) + Popup columns + Enter a column count from 1 to %1$d + Tap a cell to edit its label and input. Clear a cell to keep an empty position. Without custom Shift settings, ASCII letters become uppercase. + Candidate input (empty: blank cell) + Candidate label (empty: input text) + Add a cell + Move before + Move after + Clear cell + Remove cell + Empty + Row %1$d, column %2$d + Show QWERTY key previews + Enlarge the pressed key text. Long-press alternatives remain available. + QWERTY popup appearance + Share size and text settings with the standard QWERTY keyboard diff --git a/app/src/main/res/xml/pref_custom.xml b/app/src/main/res/xml/pref_custom.xml index 4e09604b7..509311e54 100644 --- a/app/src/main/res/xml/pref_custom.xml +++ b/app/src/main/res/xml/pref_custom.xml @@ -3,6 +3,15 @@ xmlns:app="http://schemas.android.com/apk/res-auto"> + + () + val name = "qwerty-migration-${System.nanoTime()}.db" + try { + val initial = Room.databaseBuilder(context, AppDatabase::class.java, name).allowMainThreadQueries().build() + try { + val db = initial.openHelper.writableDatabase + db.execSQL("INSERT INTO keyboard_layouts (layoutId, name, columnCount, rowCount, isRomaji, isDirectMode, createdAt, sortOrder, stableId, isFlexiblePlacementLayout, usageMode) VALUES (1, 'Original', 1, 1, 0, 0, 0, 0, 'original', 0, 'Normal')") + runBlocking { + initial.keyboardLayoutDao().insertKeys(listOf(KeyDefinition(ownerLayoutId = 1, + label = "Existing", row = 0, column = 0, keyType = KeyType.NORMAL, + keyIdentifier = "existing"))) + } + db.execSQL("ALTER TABLE key_definitions DROP COLUMN qwertyConfigJson") + db.execSQL("PRAGMA user_version = 49") + } finally { initial.close() } + val migrated = Room.databaseBuilder(context, AppDatabase::class.java, name) + .addMigrations(AppDatabase.MIGRATION_49_50).allowMainThreadQueries().build() + try { + val db = migrated.openHelper.writableDatabase + assertEquals(50, db.version) + db.query("SELECT name FROM keyboard_layouts WHERE layoutId = 1").use { + assertTrue(it.moveToFirst()) + assertEquals("Original", it.getString(0)) + } + db.query("SELECT label, keyType, qwertyConfigJson FROM key_definitions").use { + assertTrue(it.moveToFirst()) + assertEquals("Existing", it.getString(0)) + assertEquals("NORMAL", it.getString(1)) + assertTrue(it.isNull(2)) + } + } finally { migrated.close() } + } finally { context.deleteDatabase(name) } + } +} diff --git a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonImporterTest.kt b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonImporterTest.kt index a1bafde61..255f57345 100644 --- a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonImporterTest.kt +++ b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/KeyboardLayoutJsonImporterTest.kt @@ -256,7 +256,7 @@ class KeyboardLayoutJsonImporterTest { assertTrue("root must be object", root.isJsonObject) val obj = root.asJsonObject - assertEquals(5, obj["schemaVersion"].asInt) + assertEquals(6, obj["schemaVersion"].asInt) assertNotNull(obj["layouts"]) assertTrue("layouts must be array", obj["layouts"].isJsonArray) assertEquals(0, obj["layouts"].asJsonArray.size()) diff --git a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/QwertyKeyBackupTest.kt b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/QwertyKeyBackupTest.kt new file mode 100644 index 000000000..531f06360 --- /dev/null +++ b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/import_export/QwertyKeyBackupTest.kt @@ -0,0 +1,30 @@ +package com.kazumaproject.markdownhelperkeyboard.custom_keyboard.import_export + +import com.kazumaproject.custom_keyboard.data.* +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.* +import org.junit.Assert.* +import org.junit.Test + +class QwertyKeyBackupTest { + @Test fun backupRoundTripRetainsBothLayersLabelsAndBlankPositions() { + val config = QwertyKeyConfig(QwertyKeyLayer("a", "label", "preview", 2, + listOf(QwertyPopupSlot("á", "first"), QwertyPopupSlot(), QwertyPopupSlot("👩‍💻"))), + QwertyKeyLayer("lowercase", "SHIFT", "Custom", 1, listOf(QwertyPopupSlot("Address", "住所")))) + val key = KeyDefinition(ownerLayoutId = 1, label = "label", row = 0, column = 0, + keyType = KeyType.QWERTY, keyIdentifier = "qwerty", action = KeyActionMapper.fromKeyAction(KeyAction.Text("a")), + qwertyConfigJson = QwertyKeyConfigCodec.encode(config)) + val layout = FullKeyboardLayout(CustomKeyboardLayout(layoutId = 1, name = "Test", columnCount = 1, rowCount = 1), + listOf(KeyWithFlicks(key, emptyList(), emptyList(), emptyList(), emptyList(), emptyList())), emptyList()) + val json = KeyboardLayoutJsonExporter.toJson(listOf(layout)) + val result = KeyboardLayoutJsonImporter.parse(json) as KeyboardLayoutImportResult.Success + val restored = result.layouts.single().keysWithFlicks.single().key + assertEquals(KeyType.QWERTY, restored.keyType) + assertEquals(config, QwertyKeyConfigCodec.decode(restored.qwertyConfigJson)) + assertEquals("a", (KeyActionMapper.toKeyAction(restored.action) as KeyAction.Text).text) + } + + @Test fun invalidQwertyPayloadIsReportedInsteadOfDroppingItsSettings() { + val result = KeyboardLayoutJsonImporter.parse("""{"schemaVersion":6,"layouts":[{"layout":{"name":"Broken","columnCount":1,"rowCount":1},"keysWithFlicks":[{"key":{"keyType":"QWERTY","row":0,"column":0,"qwerty":{"normal":{"output":"a","columns":0}}}}]}]}""") + assertTrue(result is KeyboardLayoutImportResult.Failure || result is KeyboardLayoutImportResult.PartialSuccess) + } +} diff --git a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/QwertyKeyEditorViewTest.kt b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/QwertyKeyEditorViewTest.kt new file mode 100644 index 000000000..e7eb8d134 --- /dev/null +++ b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_keyboard/ui/QwertyKeyEditorViewTest.kt @@ -0,0 +1,51 @@ +package com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui + +import android.content.Context +import android.view.ContextThemeWrapper +import android.view.View +import android.view.ViewGroup +import android.widget.EditText +import android.widget.RadioButton +import androidx.test.core.app.ApplicationProvider +import com.google.android.material.button.MaterialButton +import com.google.android.material.textfield.TextInputLayout +import com.kazumaproject.custom_keyboard.data.* +import com.kazumaproject.markdownhelperkeyboard.R +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35]) +class QwertyKeyEditorViewTest { + @Test fun editingColumnsAndSlotLabelsKeepsBlanksAndIndependentShiftLayer() { + val context = ContextThemeWrapper(ApplicationProvider.getApplicationContext(), + com.google.android.material.R.style.Theme_Material3_DayNight_NoActionBar) + val editor = QwertyKeyEditorView(context) + val normal = QwertyKeyLayer("a", columns = 2, + slots = listOf(QwertyPopupSlot("abc", "label"), QwertyPopupSlot(), QwertyPopupSlot("xyz"))) + editor.setConfig(QwertyKeyConfig(normal, normal.copy(output = "keepLowercase"))) + fun edit(hint: Int): EditText = descendants(editor).filterIsInstance() + .first { it.hint == context.getString(hint) }.editText!! + assertTrue(editor.isInputValid) + edit(R.string.qwerty_columns).setText("1") + assertEquals(normal.slots, editor.config.normal.slots) + assertEquals(1, editor.config.normal.columns) + descendants(editor).filterIsInstance().first { it.text == "label" }.performClick() + edit(R.string.qwerty_candidate_label).setText("caption") + assertEquals("caption", editor.config.normal.slots[0].label) + assertEquals("abc", editor.config.normal.slots[0].output) + descendants(editor).filterIsInstance().first { it.text == context.getString(R.string.qwerty_layer_shift) }.performClick() + assertEquals("keepLowercase", edit(R.string.qwerty_tap_output).text.toString()) + edit(R.string.qwerty_tap_output).setText("independent") + assertEquals("a", editor.config.normal.output) + assertEquals("independent", editor.config.shifted!!.output) + edit(R.string.qwerty_columns).setText("0") + assertFalse(editor.isInputValid) + } + + private fun descendants(view: View): List = listOf(view) + if (view is ViewGroup) + (0 until view.childCount).flatMap { descendants(view.getChildAt(it)) } else emptyList() +} diff --git a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_romaji/RomajiBehaviorPersistenceTest.kt b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_romaji/RomajiBehaviorPersistenceTest.kt index 8bd7ebcb4..d68f34a8c 100644 --- a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_romaji/RomajiBehaviorPersistenceTest.kt +++ b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/custom_romaji/RomajiBehaviorPersistenceTest.kt @@ -60,7 +60,7 @@ class RomajiBehaviorPersistenceTest { val context = ApplicationProvider.getApplicationContext() val name = "romaji-migration-${System.nanoTime()}.db" fun open() = Room.databaseBuilder(context, AppDatabase::class.java, name) - .addMigrations(AppDatabase.MIGRATION_47_48, AppDatabase.MIGRATION_48_49).allowMainThreadQueries().build() + .addMigrations(AppDatabase.MIGRATION_47_48, AppDatabase.MIGRATION_48_49, AppDatabase.MIGRATION_49_50).allowMainThreadQueries().build() suspend fun withDatabase(block: suspend (AppDatabase) -> Unit) { val database = open() try { block(database) } finally { database.close() } @@ -73,6 +73,7 @@ class RomajiBehaviorPersistenceTest { val data = """{"pp":{"first":"っ","second":2}}""" db.execSQL("INSERT INTO romaji_maps VALUES (1, 'custom', ?, 1, 1)", arrayOf(data)) db.execSQL("INSERT INTO romaji_maps VALUES (2, 'default', ?, 0, 0)", arrayOf(data)) + db.execSQL("ALTER TABLE key_definitions DROP COLUMN qwertyConfigJson") db.execSQL("ALTER TABLE key_definitions DROP COLUMN hierarchicalFlickJson") db.execSQL("PRAGMA user_version = 47") } diff --git a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/ng_word/database/NgWordMigrationTest.kt b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/ng_word/database/NgWordMigrationTest.kt index 62dd103da..22a93075c 100644 --- a/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/ng_word/database/NgWordMigrationTest.kt +++ b/app/src/test/java/com/kazumaproject/markdownhelperkeyboard/ng_word/database/NgWordMigrationTest.kt @@ -39,17 +39,18 @@ class NgWordMigrationTest { db.execSQL("INSERT INTO ng_word (yomi, tango) VALUES ('きょう', '今日')") db.execSQL("DROP TABLE romaji_maps") db.execSQL("CREATE TABLE romaji_maps (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT NOT NULL, mapData TEXT NOT NULL, isActive INTEGER NOT NULL, isDeletable INTEGER NOT NULL)") + db.execSQL("ALTER TABLE key_definitions DROP COLUMN qwertyConfigJson") db.execSQL("ALTER TABLE key_definitions DROP COLUMN hierarchicalFlickJson") db.execSQL("PRAGMA user_version = 46") } finally { initial.close() } val migrated = Room.databaseBuilder(context, AppDatabase::class.java, name) - .addMigrations(AppDatabase.MIGRATION_46_47, AppDatabase.MIGRATION_47_48, AppDatabase.MIGRATION_48_49) + .addMigrations(AppDatabase.MIGRATION_46_47, AppDatabase.MIGRATION_47_48, AppDatabase.MIGRATION_48_49, AppDatabase.MIGRATION_49_50) .allowMainThreadQueries().build() try { val db = migrated.openHelper.writableDatabase - assertEquals(49, db.version) + assertEquals(50, db.version) db.query("SELECT matchMode FROM ng_word").use { cursor -> assertTrue(cursor.moveToFirst()) assertEquals("PARTIAL", cursor.getString(0)) diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/QwertyKeyInputController.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/QwertyKeyInputController.kt new file mode 100644 index 000000000..565ccf236 --- /dev/null +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/controller/QwertyKeyInputController.kt @@ -0,0 +1,249 @@ +package com.kazumaproject.custom_keyboard.controller + +import android.annotation.SuppressLint +import android.content.res.ColorStateList +import android.graphics.Color +import android.graphics.Point +import android.graphics.Rect +import android.graphics.drawable.ColorDrawable +import android.os.Build +import android.util.TypedValue +import android.view.Gravity +import android.view.MotionEvent +import android.view.View +import android.view.ViewTreeObserver +import android.widget.PopupWindow +import android.widget.TextView +import androidx.core.content.ContextCompat +import androidx.core.view.WindowInsetsCompat +import com.kazumaproject.core.data.popup.QwertyPopupViewStyleSet +import com.kazumaproject.core.domain.flick.FlickGestureMath +import com.kazumaproject.core.domain.flick.GestureSessionConfig +import com.kazumaproject.core.domain.flick.GestureSessionConfigSource +import com.kazumaproject.core.ui.font.KeyboardFontApplicator +import com.kazumaproject.core.ui.font.KeyboardFontSnapshot +import com.kazumaproject.core.ui.skin.KeyboardSkinRegistry +import com.kazumaproject.core.ui.skin.SkinPopupWindowHost +import com.kazumaproject.custom_keyboard.data.QwertyKeyLayer +import com.kazumaproject.custom_keyboard.view.QwertyAlternativesView + +/** Owns one key's gesture; the keyboard already routes each pointer to its original key. */ +class QwertyKeyInputController( + private val gestureConfigSource: GestureSessionConfigSource, + private val layerProvider: () -> QwertyKeyLayer, + private val styleProvider: () -> QwertyPopupViewStyleSet, + private val fontProvider: () -> KeyboardFontSnapshot, + private val showPreview: () -> Boolean, + private val windowAnchorProvider: () -> View?, + private val onPress: (String) -> Unit, + private val onCommit: (String) -> Unit +) { + private var anchor: View? = null + private var activeLayer: QwertyKeyLayer? = null + private var gesture: GestureSessionConfig? = null + private var initialX = 0f + private var initialY = 0f + private var canceled = false + private var choosing = false + private var popup: PopupWindow? = null + private var alternatives: QwertyAlternativesView? = null + private var popupBounds = Rect() + private var observedTree: ViewTreeObserver? = null + private val preDraw = ViewTreeObserver.OnPreDrawListener { positionPopup(); true } + private val longPress = Runnable { + val layer = activeLayer ?: return@Runnable + if (!canceled && layer.slots.any { it.output.isNotEmpty() }) { + dismissPopup() + val key = anchor ?: return@Runnable + val view = QwertyAlternativesView(key.context).apply { + setContent(layer, styleProvider().variation, fontProvider()) + } + alternatives = view + choosing = true + if (!showPopup(view)) { + choosing = false + alternatives = null + } + } + } + + @SuppressLint("ClickableViewAccessibility") + fun attach(view: View) { + cancel() + anchor = view + view.setOnClickListener { layerProvider().output.takeIf { it.isNotEmpty() }?.let(onCommit) } + view.setOnTouchListener { _, event -> + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> { + dismissPopup() + activeLayer = layerProvider() + gesture = gestureConfigSource.snapshot() + initialX = event.rawX + initialY = event.rawY + canceled = false + choosing = false + view.isPressed = true + onPress(activeLayer!!.output) + if (showPreview()) showKeyPreview() + view.postDelayed(longPress, gesture!!.longPressTimeoutMillis) + true + } + MotionEvent.ACTION_MOVE -> { + if (choosing) { + alternatives?.let { content -> + val location = IntArray(2).also(content::getLocationOnScreen) + content.updateSelection(event.rawX - location[0], event.rawY - location[1]) + } + } else gesture?.let { config -> + if (FlickGestureMath.isThresholdCrossed(event.rawX - initialX, + event.rawY - initialY, config.flickThresholdPx, config.flickThresholdShape)) { + view.removeCallbacks(longPress) + } + if (event.x < -config.flickThresholdPx || event.x > view.width + config.flickThresholdPx || + event.y < -config.flickThresholdPx || event.y > view.height + config.flickThresholdPx) { + canceled = true + view.isPressed = false + view.removeCallbacks(longPress) + dismissPopup() + } + } + true + } + MotionEvent.ACTION_UP -> { + val output = if (canceled) null else if (choosing) alternatives?.selectedOutput else activeLayer?.output + endGesture() + output?.takeIf { it.isNotEmpty() }?.let(onCommit) + true + } + MotionEvent.ACTION_CANCEL -> { endGesture(); true } + else -> false + } + } + } + + private fun showKeyPreview() { + val key = anchor ?: return + val layer = activeLayer ?: return + val style = styleProvider().keyPreview + val skin = KeyboardSkinRegistry.find(style.skinId) + val content = TextView(key.context).apply { + text = layer.previewLabel + gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL + includeFontPadding = false + maxLines = 2 + ellipsize = android.text.TextUtils.TruncateAt.END + setTextColor(style.textColor ?: skin?.palette?.text ?: ContextCompat.getColor(context, + com.kazumaproject.core.R.color.keyboard_icon_color)) + setTextSize(TypedValue.COMPLEX_UNIT_SP, style.textSizeSp) + val location = IntArray(2).also(key::getLocationOnScreen) + val geometry = skin?.keyPreview(resources, key.width, key.height, location[0], + resources.displayMetrics.widthPixels, key.top > 0) + if (geometry != null) background = geometry.background else { + setBackgroundResource(com.kazumaproject.core.R.drawable.key_preview_bubble) + style.backgroundColor?.let { backgroundTintList = ColorStateList.valueOf(it) } + } + skin?.configurePreviewText(this) + setPadding(paddingLeft, (6 * resources.displayMetrics.density).toInt(), paddingRight, paddingBottom) + } + KeyboardFontApplicator.apply(content, fontProvider()) + showPopup(content) + } + + private fun showPopup(content: View): Boolean { + val key = anchor ?: return false + if (!key.isAttachedToWindow) return false + val window = PopupWindow(content, 1, 1, false).apply { + isTouchable = false + isClippingEnabled = false + setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) + elevation = 6f + animationStyle = 0 + if (Build.VERSION.SDK_INT >= 29) setIsLaidOutInScreen(true) + } + popup = window + positionPopup() + if (!window.isShowing) { popup = null; return false } + observedTree = key.viewTreeObserver.also { it.addOnPreDrawListener(preDraw) } + KeyboardSkinRegistry.find(styleProvider().variation.skinId)?.showPopup(content) + return true + } + + private fun positionPopup() { + val key = anchor ?: return + val window = popup ?: return + if (!key.isAttachedToWindow) { endGesture(); return } + val layer = activeLayer ?: return + val style = if (choosing) styleProvider().variation else styleProvider().keyPreview + val scale = style.sizeScalePercent.coerceIn(50, 200) / 100f + val density = key.resources.displayMetrics.density + val location = IntArray(2).also(key::getLocationOnScreen) + adjustPopupAnchorForTransform(key, location) + val size = Point(key.resources.displayMetrics.widthPixels, key.resources.displayMetrics.heightPixels) + @Suppress("DEPRECATION") + key.display?.getRealSize(size) + val insets = key.rootWindowInsets?.let { + WindowInsetsCompat.toWindowInsetsCompat(it, key).getInsetsIgnoringVisibility( + WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()) + } + val viewport = Rect(insets?.left ?: 0, insets?.top ?: 0, + size.x - (insets?.right ?: 0), size.y - (insets?.bottom ?: 0)) + val previewGeometry = if (choosing) null else KeyboardSkinRegistry.find(style.skinId)?.keyPreview( + key.resources, key.width, key.height, location[0], size.x, key.top > 0) + val width = if (choosing) (48 * density * scale * layer.columns).toInt() else + previewGeometry?.width ?: (key.width * 2 * scale).toInt() + val height = if (choosing) (56 * density * scale * layer.rowCount).toInt() else + previewGeometry?.height ?: ((key.height * 2 + 64) * scale).toInt() + val w = width.coerceIn(1, viewport.width().coerceAtLeast(1)) + val h = height.coerceIn(1, viewport.height().coerceAtLeast(1)) + val desiredX = location[0] + (previewGeometry?.xOffset ?: (key.width / 2 - w / 2)) + val desiredY = if (choosing) location[1] - h else + location[1] + key.height + (previewGeometry?.yOffset ?: -h) + val x = desiredX.coerceIn(viewport.left, viewport.right - w) + val y = desiredY.coerceIn(viewport.top, viewport.bottom - h) + val bounds = Rect(x, y, x + w, y + h) + val suppliedAnchor = windowAnchorProvider() ?: generateSequence(key as View?) { it.parent as? View } + .filterIsInstance().firstOrNull()?.applicationWindowView ?: key + var windowX = x + var windowY = y + if (Build.VERSION.SDK_INT < 29) { + val screen = IntArray(2).also(suppliedAnchor::getLocationOnScreen) + val local = IntArray(2).also(suppliedAnchor::getLocationInWindow) + windowX -= screen[0] - local[0] + windowY -= screen[1] - local[1] + } + runCatching { + if (!window.isShowing) { + window.width = w + window.height = h + window.showAtLocation(suppliedAnchor, Gravity.NO_GRAVITY, windowX, windowY) + } else if (bounds != popupBounds) window.update(windowX, windowY, w, h) + popupBounds = bounds + }.onFailure { dismissPopup() } + } + + private fun dismissPopup() { + observedTree?.takeIf { it.isAlive }?.removeOnPreDrawListener(preDraw) + observedTree = null + popup?.dismiss() + popup = null + alternatives = null + popupBounds.setEmpty() + } + + private fun endGesture() { + anchor?.removeCallbacks(longPress) + anchor?.isPressed = false + dismissPopup() + activeLayer = null + gesture = null + choosing = false + canceled = true + } + + fun cancel() { + endGesture() + anchor?.setOnTouchListener(null) + anchor?.setOnClickListener(null) + anchor = null + } +} diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/KeyModels.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/KeyModels.kt index 27bc5068b..2bbe2d9c1 100644 --- a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/KeyModels.kt +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/KeyModels.kt @@ -162,7 +162,8 @@ data class KeyData( val keyType: KeyType = if (isFlickable) KeyType.CIRCULAR_FLICK else KeyType.NORMAL, val specialKeyColorStyle: SpecialKeyColorStyle = SpecialKeyColorStyle.SPECIAL, val doubleTapBinding: DoubleTapBinding? = null, - val textInputBehavior: KeyTextInputBehavior = KeyTextInputBehavior.NORMAL + val textInputBehavior: KeyTextInputBehavior = KeyTextInputBehavior.NORMAL, + val qwertyConfig: QwertyKeyConfig? = null ) data class GridPlacement( @@ -635,7 +636,10 @@ enum class KeyType { HIERARCHICAL_FLICK, - FLICK_LONG_PRESS + FLICK_LONG_PRESS, + + /** Configurable key preview and a grid of long-press alternatives. */ + QWERTY } enum class ShapeType { diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/QwertyKeyConfig.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/QwertyKeyConfig.kt new file mode 100644 index 000000000..a8027a3a9 --- /dev/null +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/data/QwertyKeyConfig.kt @@ -0,0 +1,63 @@ +package com.kazumaproject.custom_keyboard.data + +import com.google.gson.Gson +import com.google.gson.annotations.SerializedName + +/** A slot stays in the grid when its output is empty. Strings may contain multiple code points. */ +data class QwertyPopupSlot( + @SerializedName("output") val output: String = "", + @SerializedName("label") val label: String = "" +) { + val displayLabel: String get() = label.ifEmpty { output } +} + +data class QwertyKeyLayer( + @SerializedName("output") val output: String = "", + @SerializedName("label") val label: String = "", + @SerializedName("preview") val preview: String = "", + @SerializedName("columns") val columns: Int = 3, + @SerializedName("slots") val slots: List = emptyList() +) { + val displayLabel: String get() = label.ifEmpty { output } + val previewLabel: String get() = preview.ifEmpty { output } + val rowCount: Int get() = (slots.size + columns - 1) / columns + + fun uppercase(): QwertyKeyLayer { + val transform = KeyCharacterCase.UPPERCASE::transformAsciiLetters + return copy(output = transform(output), label = transform(label), preview = transform(preview), + slots = slots.map { it.copy(output = transform(it.output), label = transform(it.label)) }) + } +} + +data class QwertyKeyConfig( + @SerializedName("normal") val normal: QwertyKeyLayer = QwertyKeyLayer(), + @SerializedName("shifted") val shifted: QwertyKeyLayer? = null +) { + fun layer(characterCase: KeyCharacterCase): QwertyKeyLayer = + if (characterCase == KeyCharacterCase.UPPERCASE) shifted ?: normal.uppercase() else normal + + fun isValid(requireOutput: Boolean = true): Boolean = runCatching { + (listOf(normal) + listOfNotNull(shifted)).all { layer -> + (!requireOutput || layer.output.isNotEmpty()) && layer.columns in 1..MAX_COLUMNS && + layer.slots.size <= MAX_SLOTS && layer.label.length <= MAX_TEXT_LENGTH && + layer.preview.length <= MAX_TEXT_LENGTH && layer.output.length <= MAX_TEXT_LENGTH && + layer.slots.all { it.output.length <= MAX_TEXT_LENGTH && it.label.length <= MAX_TEXT_LENGTH } + } && (!requireOutput || normal.output.isNotEmpty()) + }.getOrDefault(false) + + companion object { + const val MAX_COLUMNS = 12 + const val MAX_SLOTS = 120 + const val MAX_TEXT_LENGTH = 10000 + } +} + +/** Explicit names above are also the external backup contract under R8. */ +object QwertyKeyConfigCodec { + private val gson = Gson() + fun encode(config: QwertyKeyConfig): String = gson.toJson(config) + fun decode(json: String?, requireOutput: Boolean = true): QwertyKeyConfig? = json?.let { + runCatching { gson.fromJson(it, QwertyKeyConfig::class.java) }.getOrNull() + ?.takeIf { config -> config.isValid(requireOutput) } + } +} diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt index 0098aee18..f1c5bc82f 100644 --- a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/FlickKeyboardView.kt @@ -67,6 +67,10 @@ import com.kazumaproject.custom_keyboard.controller.DoubleTapActionDispatcher import com.kazumaproject.custom_keyboard.controller.FlickLongPressInputController import com.kazumaproject.custom_keyboard.controller.StandardFlickInputController import com.kazumaproject.custom_keyboard.controller.TapTaskScheduler +import com.kazumaproject.custom_keyboard.controller.QwertyKeyInputController +import com.kazumaproject.custom_keyboard.data.QwertyKeyConfig +import com.kazumaproject.custom_keyboard.data.QwertyKeyLayer +import com.kazumaproject.core.data.popup.QwertyPopupViewStyleSet import com.kazumaproject.custom_keyboard.controller.TapLongPressInputController import com.kazumaproject.custom_keyboard.data.CircularFlickDirection import com.kazumaproject.custom_keyboard.data.FlickAction @@ -130,6 +134,7 @@ class FlickKeyboardView @JvmOverloads constructor( interface OnKeyboardActionListener { fun onPress(action: KeyAction) + fun onResolvedText(text: String) { onAction(KeyAction.Text(text), false) } fun onAction(action: KeyAction, isFlick: Boolean) fun onActionLongPress(action: KeyAction) fun onActionUpAfterLongPress(action: KeyAction) @@ -161,6 +166,20 @@ class FlickKeyboardView @JvmOverloads constructor( private val centerGuideFlickControllers = mutableListOf() private val standardFlickControllers = mutableListOf() private val flickLongPressControllers = mutableListOf() + private val qwertyControllers = mutableListOf() + private var showQwertyPreview = true + private var qwertyPopupStyle = QwertyPopupViewStyleSet(PopupViewStyle(100, 28f), PopupViewStyle(100, 28f)) + + fun setQwertyPopupSettings(showPreview: Boolean, style: QwertyPopupViewStyleSet) { + val normalized = QwertyPopupViewStyleSet(clampPopupStyle(style.keyPreview), clampPopupStyle(style.variation)) + if (showQwertyPreview == showPreview && qwertyPopupStyle == normalized) return + cancelTrackedTouchState() + cancelInputControllers() + showQwertyPreview = showPreview + qwertyPopupStyle = normalized + restoreInputControllersIfNeeded() + } + private val tapLongPressControllers = mutableListOf() private val doubleTapActionDispatcher = DoubleTapActionDispatcher( timeoutMillis = ViewConfiguration.getDoubleTapTimeout().toLong(), @@ -1026,6 +1045,9 @@ class FlickKeyboardView @JvmOverloads constructor( KeyIconResolver.hasIcon(keyData) && modeSwitchLabelForDisplay(keyData) == null private fun keyLabelForDisplay(keyData: KeyData): String { + if (keyData.keyType == KeyType.QWERTY) { + return keyData.qwertyConfig?.layer(keyCharacterCase)?.displayLabel ?: keyData.label + } modeSwitchLabelForDisplay(keyData)?.let { return it } val canonicalLabel = KeyIconResolver.resolvedLabelForRendering(keyData) return if (shouldTransformMainLabel(keyData)) { @@ -1675,6 +1697,29 @@ class FlickKeyboardView @JvmOverloads constructor( val layout = currentLayout ?: return null when (keyData.keyType) { + KeyType.QWERTY -> { + val config = keyData.qwertyConfig ?: QwertyKeyConfig(QwertyKeyLayer( + output = (keyData.action as? KeyAction.Text)?.text ?: keyData.label, + label = keyData.label + )) + return QwertyKeyInputController( + gestureSessionConfigSource, + layerProvider = { config.layer(keyCharacterCase) }, + styleProvider = { qwertyPopupStyle }, + fontProvider = { keyboardFontSnapshot }, + showPreview = { showQwertyPreview }, + windowAnchorProvider = { popupWindowAnchorProvider?.invoke() }, + onPress = { + cancelTextPreview() + listener?.onPress(KeyAction.Text(it)) + }, + onCommit = { + cancelTextPreview() + doubleTapActionDispatcher.interrupt() + listener?.onResolvedText(it) + } + ).also { it.attach(keyView); qwertyControllers.add(it) } + } KeyType.CIRCULAR_FLICK -> { val circularKeyMapsList = keyData.keyId?.let { layout.circularFlickKeyMaps[it] } @@ -2780,6 +2825,10 @@ class FlickKeyboardView @JvmOverloads constructor( + is QwertyKeyInputController -> { + controller.cancel() + qwertyControllers.remove(controller) + } is TapLongPressInputController -> { controller.cancel() tapLongPressControllers.remove(controller) @@ -2845,6 +2894,8 @@ class FlickKeyboardView @JvmOverloads constructor( flickLongPressControllers.forEach { it.cancel() } flickLongPressControllers.clear() + qwertyControllers.forEach { it.cancel() } + qwertyControllers.clear() tapLongPressControllers.forEach { it.cancel() } tapLongPressControllers.clear() keyInfos.forEach { it.controller = null } diff --git a/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/QwertyAlternativesView.kt b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/QwertyAlternativesView.kt new file mode 100644 index 000000000..f3dccb368 --- /dev/null +++ b/custom_keyboard/src/main/java/com/kazumaproject/custom_keyboard/view/QwertyAlternativesView.kt @@ -0,0 +1,82 @@ +package com.kazumaproject.custom_keyboard.view + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.text.TextUtils +import android.text.TextPaint +import android.util.TypedValue +import android.view.View +import androidx.core.content.ContextCompat +import com.kazumaproject.core.data.popup.PopupViewStyle +import com.kazumaproject.core.ui.font.KeyboardFontApplicator +import com.kazumaproject.core.ui.font.KeyboardFontSnapshot +import com.kazumaproject.core.ui.skin.KeyboardSkinRegistry +import com.kazumaproject.custom_keyboard.data.QwertyKeyLayer + +/** Drawing and hit testing use the same row-major slots, including empty positions. */ +class QwertyAlternativesView(context: Context) : View(context) { + private var layer = QwertyKeyLayer() + private var style = PopupViewStyle(100, 28f) + private val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply { textAlign = Paint.Align.CENTER } + private val paint = Paint(Paint.ANTI_ALIAS_FLAG) + private var skinBackground: android.graphics.drawable.Drawable? = null + var selectedIndex: Int = -1 + private set + val selectedOutput: String? get() = layer.slots.getOrNull(selectedIndex)?.output?.takeIf { it.isNotEmpty() } + + fun setContent(layer: QwertyKeyLayer, style: PopupViewStyle, font: KeyboardFontSnapshot) { + this.layer = layer + this.style = style + skinBackground = KeyboardSkinRegistry.find(style.skinId)?.variationDrawable(resources) + textPaint.textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, + style.textSizeSp, resources.displayMetrics) + KeyboardFontApplicator.apply(textPaint, font) + selectedIndex = layer.slots.indexOfFirst { it.output.isNotEmpty() } + invalidate() + } + + fun updateSelection(x: Float, y: Float) { + if (width <= 0 || height <= 0 || layer.rowCount == 0 || + x < 0 || y < 0 || x >= width || y >= height) return + val index = (y / (height.toFloat() / layer.rowCount)).toInt() * layer.columns + + (x / (width.toFloat() / layer.columns)).toInt() + val selected = index.takeIf { layer.slots.getOrNull(it)?.output?.isNotEmpty() == true } ?: -1 + if (selectedIndex != selected) { + selectedIndex = selected + invalidate() + } + } + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + if (layer.rowCount == 0) return + val skin = KeyboardSkinRegistry.find(style.skinId) + val background = style.backgroundColor ?: skin?.palette?.key ?: + ContextCompat.getColor(context, com.kazumaproject.core.R.color.candidate_background) + paint.color = background + val radius = 8f * resources.displayMetrics.density + if (skin != null) { + skinBackground?.apply { setBounds(0, 0, width, height) }?.draw(canvas) + } else canvas.drawRoundRect(0f, 0f, width.toFloat(), height.toFloat(), radius, radius, paint) + val cellWidth = width.toFloat() / layer.columns + val cellHeight = height.toFloat() / layer.rowCount + layer.slots.forEachIndexed { index, slot -> + if (slot.output.isEmpty()) return@forEachIndexed + val left = index % layer.columns * cellWidth + val top = index / layer.columns * cellHeight + val selected = index == selectedIndex + if (selected) { + paint.color = skin?.palette?.selection ?: ContextCompat.getColor(context, com.kazumaproject.core.R.color.enter_key_bg) + canvas.drawRoundRect(left + 2f, top + 2f, left + cellWidth - 2f, + top + cellHeight - 2f, radius, radius, paint) + } + textPaint.color = if (selected && skin != null) skin.palette.selectionText else + style.textColor ?: skin?.palette?.text ?: ContextCompat.getColor(context, com.kazumaproject.core.R.color.keyboard_icon_color) + val label = TextUtils.ellipsize(slot.displayLabel.replace('\n', ' '), textPaint, + (cellWidth - 8f).coerceAtLeast(1f), TextUtils.TruncateAt.END).toString() + canvas.drawText(label, left + cellWidth / 2, + top + cellHeight / 2 - (textPaint.ascent() + textPaint.descent()) / 2, textPaint) + } + } +} diff --git a/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/controller/QwertyKeyInputControllerTest.kt b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/controller/QwertyKeyInputControllerTest.kt new file mode 100644 index 000000000..e5bb3e102 --- /dev/null +++ b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/controller/QwertyKeyInputControllerTest.kt @@ -0,0 +1,73 @@ +package com.kazumaproject.custom_keyboard.controller + +import android.app.Activity +import android.os.Looper +import android.view.MotionEvent +import android.widget.Button +import android.widget.FrameLayout +import com.kazumaproject.core.data.popup.PopupViewStyle +import com.kazumaproject.core.data.popup.QwertyPopupViewStyleSet +import com.kazumaproject.core.domain.flick.GestureSessionConfig +import com.kazumaproject.core.domain.flick.GestureSessionConfigSource +import com.kazumaproject.core.ui.font.KeyboardFontSnapshot +import com.kazumaproject.custom_keyboard.data.QwertyKeyLayer +import com.kazumaproject.custom_keyboard.data.QwertyPopupSlot +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.time.Duration + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35]) +class QwertyKeyInputControllerTest { + @Test fun tapHoldCancelAndDisabledPreviewCommitOnlyTheirOwnText() { + val activity = Robolectric.buildActivity(Activity::class.java).setup().get() + val root = FrameLayout(activity) + activity.setContentView(root) + activity.window.decorView.layout(0, 0, 1080, 2400) + root.layout(0, 0, 1080, 2400) + val key = Button(activity) + root.addView(key, FrameLayout.LayoutParams(100, 100)) + key.layout(300, 1500, 400, 1600) + val outputs = mutableListOf() + var layer = QwertyKeyLayer("a", slots = listOf(QwertyPopupSlot(), QwertyPopupSlot("long text"))) + val style = PopupViewStyle(100, 28f) + val controller = QwertyKeyInputController(GestureSessionConfigSource { + GestureSessionConfig(1L, 100, 20f, longPressTimeoutMillis = 300) + }, { layer }, { QwertyPopupViewStyleSet(style, style) }, { KeyboardFontSnapshot() }, + { false }, { null }, {}, { outputs.add(it) }) + controller.attach(key) + try { + key.send(MotionEvent.ACTION_DOWN) + key.send(MotionEvent.ACTION_UP) + assertEquals(listOf("a"), outputs) + key.send(MotionEvent.ACTION_DOWN) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(350)) + key.send(MotionEvent.ACTION_UP) + assertEquals(listOf("a", "long text"), outputs) + key.send(MotionEvent.ACTION_DOWN) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(350)) + key.send(MotionEvent.ACTION_CANCEL) + key.send(MotionEvent.ACTION_UP) + assertEquals(2, outputs.size) + layer = QwertyKeyLayer("no alternatives") + key.send(MotionEvent.ACTION_DOWN) + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(350)) + key.send(MotionEvent.ACTION_UP) + assertEquals("no alternatives", outputs.last()) + key.send(MotionEvent.ACTION_DOWN) + controller.cancel() + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(350)) + assertEquals(3, outputs.size) + } finally { controller.cancel(); activity.finish() } + } + + private fun Button.send(action: Int) { + val now = android.os.SystemClock.uptimeMillis() + MotionEvent.obtain(now, now, action, 50f, 50f, 0).also { dispatchTouchEvent(it); it.recycle() } + } +} diff --git a/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/data/QwertyKeyConfigTest.kt b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/data/QwertyKeyConfigTest.kt new file mode 100644 index 000000000..0cdc23d0e --- /dev/null +++ b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/data/QwertyKeyConfigTest.kt @@ -0,0 +1,41 @@ +package com.kazumaproject.custom_keyboard.data + +import org.junit.Assert.* +import org.junit.Test + +class QwertyKeyConfigTest { + private val normal = QwertyKeyLayer("a", "letter", "preview", 2, + listOf(QwertyPopupSlot("á"), QwertyPopupSlot(), QwertyPopupSlot("👩‍💻", "work"))) + + @Test fun backupRetainsLabelsEmptySlotsAndUnicodeStrings() { + val config = QwertyKeyConfig(normal, normal.copy(output = "lowercase override", columns = 1)) + assertEquals(config, QwertyKeyConfigCodec.decode(QwertyKeyConfigCodec.encode(config))) + assertEquals(2, normal.rowCount) + assertEquals("👩‍💻", config.normal.slots[2].output) + } + + @Test fun missingShiftUsesAsciiUppercaseWhileExplicitShiftIsVerbatim() { + val automatic = QwertyKeyConfig(normal).layer(KeyCharacterCase.UPPERCASE) + assertEquals("A", automatic.output) + assertEquals("LETTER", automatic.displayLabel) + assertEquals("á", automatic.slots[0].output) + val explicit = normal.copy(output = "keepCase", label = "Aa") + assertEquals(explicit, QwertyKeyConfig(normal, explicit).layer(KeyCharacterCase.UPPERCASE)) + } + + @Test fun malformedAndUnboundedDefinitionsAreRejected() { + listOf("{}", "null", "{", "{\"normal\":null}", + "{\"normal\":{\"output\":\"a\",\"columns\":0}}", + "{\"normal\":{\"output\":\"a\",\"slots\":[null]}}", + "{\"normal\":{\"output\":\"a\",\"slots\":null}}") + .forEach { assertNull(it, QwertyKeyConfigCodec.decode(it)) } + assertFalse(QwertyKeyConfig(normal.copy(slots = List(121) { QwertyPopupSlot() })).isValid()) + } + + @Test fun defaultsKeepBlankSlotsAndUseOutputAsDisplay() { + assertEquals("address", QwertyPopupSlot("address").displayLabel) + assertEquals(" ", QwertyPopupSlot(" ").output) + assertEquals("a", QwertyKeyLayer("a").previewLabel) + assertNotNull(QwertyKeyConfigCodec.decode(QwertyKeyConfigCodec.encode(QwertyKeyConfig()), requireOutput = false)) + } +} diff --git a/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/QwertyAlternativesViewTest.kt b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/QwertyAlternativesViewTest.kt new file mode 100644 index 000000000..80ea4d1dc --- /dev/null +++ b/custom_keyboard/src/test/java/com/kazumaproject/custom_keyboard/view/QwertyAlternativesViewTest.kt @@ -0,0 +1,37 @@ +package com.kazumaproject.custom_keyboard.view + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.kazumaproject.core.data.popup.PopupViewStyle +import com.kazumaproject.core.ui.font.KeyboardFontSnapshot +import com.kazumaproject.custom_keyboard.data.QwertyKeyLayer +import com.kazumaproject.custom_keyboard.data.QwertyPopupSlot +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35]) +class QwertyAlternativesViewTest { + @Test fun selectionUsesDisplayedGridAndBlankCellsCancelWhileOutsidePreservesSelection() { + val context = ApplicationProvider.getApplicationContext() + val view = QwertyAlternativesView(context) + view.setContent(QwertyKeyLayer("a", columns = 2, slots = listOf( + QwertyPopupSlot(), QwertyPopupSlot("abc", "label"), QwertyPopupSlot("👩‍💻"))), + PopupViewStyle(100, 28f), KeyboardFontSnapshot()) + view.layout(0, 0, 200, 100) + assertEquals("abc", view.selectedOutput) + view.updateSelection(50f, 75f) + assertEquals("👩‍💻", view.selectedOutput) + view.updateSelection(-1f, -1f) + assertEquals("👩‍💻", view.selectedOutput) + view.updateSelection(50f, 25f) + assertNull(view.selectedOutput) + view.updateSelection(150f, 75f) + assertNull(view.selectedOutput) + view.updateSelection(150f, 25f) + assertEquals("abc", view.selectedOutput) + } +}