make editing the "+" layouts work

This commit is contained in:
Helium314 2025-03-08 10:26:24 +01:00
parent c47da4203f
commit 2b8c39b125
3 changed files with 28 additions and 3 deletions

View file

@ -59,8 +59,7 @@ object LayoutParser {
/** Parse simple layouts, defined only as rows of (normal) keys with popup keys. */
fun parseSimpleString(layoutText: String): List<List<KeyData>> {
val rowStrings = layoutText.replace("\r\n", "\n").split("\\n\\s*\\n".toRegex()).filter { it.isNotBlank() }
return rowStrings.map { row ->
return LayoutUtils.getSimpleRowStrings(layoutText).map { row ->
row.split("\n").mapNotNull { parseKey(it) }
}
}

View file

@ -1,6 +1,8 @@
package helium314.keyboard.latin.utils
import android.content.Context
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.SimplePopups
import helium314.keyboard.keyboard.internal.keyboard_parser.getOrCreate
import helium314.keyboard.latin.R
import helium314.keyboard.latin.settings.Defaults.default
import helium314.keyboard.latin.utils.LayoutType.Companion.folder
@ -26,6 +28,7 @@ object LayoutUtils {
fun getLMainLayoutsForLocales(locales: List<Locale>, context: Context): Collection<String> =
locales.flatMapTo(HashSet()) { getAvailableLayouts(LayoutType.MAIN, context, it) }.sorted()
/** gets content for built-in (non-custom) layout [layoutName], with fallback to qwerty */
fun getContent(layoutType: LayoutType, layoutName: String, context: Context): String {
val layouts = context.assets.list(layoutType.folder)!!
layouts.firstOrNull { it.startsWith("$layoutName.") }
@ -33,4 +36,27 @@ object LayoutUtils {
val fallback = layouts.first { it.startsWith(layoutType.default) } // must exist!
return context.assets.open(layoutType.folder + File.separator + fallback).reader().readText()
}
fun getContentWithPlus(mainLayoutName: String, locale: Locale, context: Context): String {
val content = getContent(LayoutType.MAIN, mainLayoutName, context)
if (!mainLayoutName.endsWith("+"))
return content
// the stuff below will not work if we add "+" layouts in json format
// ideally we should serialize keyData to json to solve this
val rows = getSimpleRowStrings(content)
val localeKeyboardInfos = getOrCreate(context, locale)
return rows.mapIndexed { i, row ->
val extraKeys = localeKeyboardInfos.getExtraKeys(i + 1) ?: return@mapIndexed row
val rowList = row.split("\n").filterNot { it.isEmpty() }.toMutableList()
extraKeys.forEach { key ->
val popups = (key.popup as? SimplePopups)?.popupKeys?.joinToString(" ")
?.takeIf { it.isNotEmpty() }?.let { " $it" } ?: ""
rowList.add(key.label + popups)
}
rowList.joinToString("\n")
}.joinToString("\n\n")
}
fun getSimpleRowStrings(layoutContent: String): List<String> =
layoutContent.replace("\r\n", "\n").split("\\n\\s*\\n".toRegex()).filter { it.isNotBlank() }
}

View file

@ -393,7 +393,7 @@ private fun MainLayoutRow(
if (showLayoutEditDialog != null) {
val layoutName = showLayoutEditDialog!!.first
val startContent = showLayoutEditDialog?.second
?: if (layoutName in appLayouts) LayoutUtils.getContent(LayoutType.MAIN, layoutName, ctx)
?: if (layoutName in appLayouts) LayoutUtils.getContentWithPlus(layoutName, currentSubtype.locale, ctx)
else null
LayoutEditDialog(
onDismissRequest = { showLayoutEditDialog = null },