Component: Refactor extra key
This commit is contained in:
parent
83699de49f
commit
5b44db1657
@ -5,7 +5,7 @@ import io.neoterm.component.codegen.CodeGenComponent
|
||||
import io.neoterm.component.colorscheme.ColorSchemeComponent
|
||||
import io.neoterm.component.completion.CompletionComponent
|
||||
import io.neoterm.component.config.ConfigureComponent
|
||||
import io.neoterm.component.extrakey.ExtraKeysComponent
|
||||
import io.neoterm.component.extrakey.ExtraKeyComponent
|
||||
import io.neoterm.component.font.FontComponent
|
||||
import io.neoterm.component.pm.PackageComponent
|
||||
import io.neoterm.component.profile.ProfileComponent
|
||||
@ -30,7 +30,7 @@ object NeoInitializer {
|
||||
ComponentManager.registerComponent(ColorSchemeComponent::class.java)
|
||||
ComponentManager.registerComponent(FontComponent::class.java)
|
||||
ComponentManager.registerComponent(UserScriptComponent::class.java)
|
||||
ComponentManager.registerComponent(ExtraKeysComponent::class.java)
|
||||
ComponentManager.registerComponent(ExtraKeyComponent::class.java)
|
||||
ComponentManager.registerComponent(CompletionComponent::class.java)
|
||||
ComponentManager.registerComponent(PackageComponent::class.java)
|
||||
ComponentManager.registerComponent(SessionComponent::class.java)
|
||||
|
@ -175,7 +175,7 @@ open class NeoColorScheme : CodeGenObject, ConfigFileBasedObject {
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
fun loadConfigure(file: File): Boolean {
|
||||
fun testLoadConfigure(file: File): Boolean {
|
||||
val loaderService = ComponentManager.getComponent<ConfigureComponent>()
|
||||
|
||||
val configure: NeoConfigureFile?
|
||||
|
@ -2,36 +2,42 @@ package io.neoterm.component.extrakey
|
||||
|
||||
import android.content.Context
|
||||
import io.neoterm.App
|
||||
import io.neoterm.frontend.logging.NLog
|
||||
import io.neoterm.frontend.component.helper.ConfigFileBasedComponent
|
||||
import io.neoterm.frontend.config.NeoTermPath
|
||||
import io.neoterm.frontend.component.NeoComponent
|
||||
import io.neoterm.utils.AssetsUtils
|
||||
import io.neoterm.frontend.logging.NLog
|
||||
import io.neoterm.frontend.terminal.extrakey.ExtraKeysView
|
||||
import io.neoterm.utils.AssetsUtils
|
||||
import java.io.File
|
||||
import java.io.FileFilter
|
||||
|
||||
/**
|
||||
* @author kiva
|
||||
*/
|
||||
class ExtraKeysComponent : NeoComponent {
|
||||
class ExtraKeyComponent : ConfigFileBasedComponent<NeoExtraKey>() {
|
||||
companion object {
|
||||
private val FILTER = FileFilter {
|
||||
it.extension == "nl"
|
||||
}
|
||||
}
|
||||
override fun onServiceInit() {
|
||||
checkForFiles()
|
||||
}
|
||||
|
||||
override fun onServiceDestroy() {
|
||||
}
|
||||
|
||||
override fun onServiceObtained() {
|
||||
checkForFiles()
|
||||
}
|
||||
override val checkComponentFileWhenObtained = true
|
||||
|
||||
private val extraKeys: MutableMap<String, NeoExtraKey> = mutableMapOf()
|
||||
|
||||
override fun onCheckComponentFiles() {
|
||||
File(NeoTermPath.EKS_PATH).mkdirs()
|
||||
|
||||
val defaultFile = File(NeoTermPath.EKS_DEFAULT_FILE)
|
||||
if (!defaultFile.exists()) {
|
||||
extractDefaultConfig(App.get())
|
||||
}
|
||||
reloadExtraKeyConfig()
|
||||
}
|
||||
|
||||
override fun onCreateComponentObject(): NeoExtraKey {
|
||||
return NeoExtraKey()
|
||||
}
|
||||
|
||||
fun showShortcutKeys(program: String, extraKeysView: ExtraKeysView?) {
|
||||
if (extraKeysView == null) {
|
||||
return
|
||||
@ -52,16 +58,6 @@ class ExtraKeysComponent : NeoComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkForFiles() {
|
||||
File(NeoTermPath.EKS_PATH).mkdirs()
|
||||
|
||||
val defaultFile = File(NeoTermPath.EKS_DEFAULT_FILE)
|
||||
if (!defaultFile.exists()) {
|
||||
extractDefaultConfig(App.get())
|
||||
}
|
||||
loadConfigure()
|
||||
}
|
||||
|
||||
private fun extractDefaultConfig(context: Context) {
|
||||
try {
|
||||
AssetsUtils.extractAssetsDir(context, "eks", NeoTermPath.EKS_PATH)
|
||||
@ -70,13 +66,13 @@ class ExtraKeysComponent : NeoComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadConfigure() {
|
||||
private fun reloadExtraKeyConfig() {
|
||||
val configDir = File(NeoTermPath.EKS_PATH)
|
||||
|
||||
configDir.listFiles(FILTER).forEach {
|
||||
if (it.absolutePath != NeoTermPath.EKS_DEFAULT_FILE) {
|
||||
val extraKey = NeoExtraKey()
|
||||
if (extraKey.loadConfigure(it)) {
|
||||
val extraKey = this.loadConfigure(it)
|
||||
if (extraKey != null) {
|
||||
registerShortcutKeys(extraKey)
|
||||
}
|
||||
}
|
@ -2,18 +2,20 @@ package io.neoterm.component.extrakey
|
||||
|
||||
import io.neolang.visitor.ConfigVisitor
|
||||
import io.neoterm.component.config.ConfigureComponent
|
||||
import io.neoterm.frontend.component.ComponentManager
|
||||
import io.neoterm.frontend.component.helper.ConfigFileBasedObject
|
||||
import io.neoterm.frontend.config.NeoConfigureFile
|
||||
import io.neoterm.frontend.logging.NLog
|
||||
import io.neoterm.frontend.component.ComponentManager
|
||||
import io.neoterm.frontend.terminal.extrakey.ExtraKeysView
|
||||
import io.neoterm.frontend.terminal.extrakey.button.IExtraButton
|
||||
import io.neoterm.frontend.terminal.extrakey.button.TextButton
|
||||
import org.jetbrains.annotations.TestOnly
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* @author kiva
|
||||
*/
|
||||
class NeoExtraKey {
|
||||
class NeoExtraKey : ConfigFileBasedObject {
|
||||
companion object {
|
||||
const val EKS_META_CONTEXT_NAME = "extra-key"
|
||||
|
||||
@ -42,28 +44,11 @@ class NeoExtraKey {
|
||||
}
|
||||
}
|
||||
|
||||
fun loadConfigure(file: File): Boolean {
|
||||
// TODO: Refactor with NeoColorScheme#onConfigLoaded
|
||||
val loaderService = ComponentManager.getComponent<ConfigureComponent>()
|
||||
|
||||
val configure: NeoConfigureFile?
|
||||
try {
|
||||
configure = loaderService.newLoader(file).loadConfigure()
|
||||
if (configure == null) {
|
||||
throw RuntimeException("Parse configuration failed.")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
NLog.e("ExtraKey", "Failed to load extra key config: ${file.absolutePath}: ${e.localizedMessage}")
|
||||
return false
|
||||
}
|
||||
|
||||
val visitor = configure.getVisitor()
|
||||
|
||||
override fun onConfigLoaded(configVisitor: ConfigVisitor) {
|
||||
// program
|
||||
val programArray = visitor.getArray(EKS_META_CONTEXT_PATH, EKS_META_PROGRAM)
|
||||
val programArray = configVisitor.getArray(EKS_META_CONTEXT_PATH, EKS_META_PROGRAM)
|
||||
if (programArray.isEmpty()) {
|
||||
NLog.e("ExtraKey", "Failed to load extra key config: ${file.absolutePath}: Extra Key must have programs attribute")
|
||||
return false
|
||||
throw RuntimeException("Extra Key must have programs attribute")
|
||||
}
|
||||
|
||||
programArray.forEach {
|
||||
@ -73,14 +58,13 @@ class NeoExtraKey {
|
||||
}
|
||||
|
||||
// key
|
||||
val keyArray = visitor.getArray(EKS_META_CONTEXT_PATH, EKS_META_KEY)
|
||||
val keyArray = configVisitor.getArray(EKS_META_CONTEXT_PATH, EKS_META_KEY)
|
||||
keyArray.takeWhile { it.isBlock() }
|
||||
.forEach {
|
||||
val display = it.eval(EKS_META_DISPLAY)
|
||||
val code = it.eval(EKS_META_CODE)
|
||||
if (!code.isValid()) {
|
||||
NLog.e("ExtraKey", "Failed to load extra key config: ${file.absolutePath}: Key must have a code")
|
||||
return false
|
||||
throw RuntimeException("Key must have a code")
|
||||
}
|
||||
|
||||
val codeText = code.asString()
|
||||
@ -95,12 +79,31 @@ class NeoExtraKey {
|
||||
|
||||
// We must cal toDouble() before toInt()
|
||||
// Because in NeoLang, numbers are default to Double
|
||||
version = getMetaByVisitor(visitor, EKS_META_VERSION)?.toDouble()?.toInt() ?: 0
|
||||
withDefaultKeys = "true" == getMetaByVisitor(visitor, EKS_META_WITH_DEFAULT)
|
||||
return true
|
||||
version = getMetaByVisitor(configVisitor, EKS_META_VERSION)?.toDouble()?.toInt() ?: 0
|
||||
withDefaultKeys = "true" == getMetaByVisitor(configVisitor, EKS_META_WITH_DEFAULT)
|
||||
}
|
||||
|
||||
private fun getMetaByVisitor(visitor: ConfigVisitor, metaName: String): String? {
|
||||
return visitor.getStringValue(EKS_META_CONTEXT_PATH, metaName)
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
fun testLoadConfigure(file: File): Boolean {
|
||||
val loaderService = ComponentManager.getComponent<ConfigureComponent>()
|
||||
|
||||
val configure: NeoConfigureFile?
|
||||
try {
|
||||
configure = loaderService.newLoader(file).loadConfigure()
|
||||
if (configure == null) {
|
||||
throw RuntimeException("Parse configuration failed.")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
NLog.e("ExtraKey", "Failed to load extra key config: ${file.absolutePath}: ${e.localizedMessage}")
|
||||
return false
|
||||
}
|
||||
|
||||
val visitor = configure.getVisitor()
|
||||
onConfigLoaded(visitor)
|
||||
return true
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ import android.view.View
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import io.neoterm.backend.KeyHandler
|
||||
import io.neoterm.backend.TerminalSession
|
||||
import io.neoterm.component.extrakey.ExtraKeysComponent
|
||||
import io.neoterm.component.extrakey.ExtraKeyComponent
|
||||
import io.neoterm.frontend.component.ComponentManager
|
||||
import io.neoterm.frontend.config.NeoPreference
|
||||
import io.neoterm.frontend.session.shell.ShellTermSession
|
||||
@ -222,7 +222,7 @@ class TermViewClient(val context: Context) : TerminalViewClient {
|
||||
if ((lastTitle != title || force)
|
||||
&& updateExtraKeysVisibility()) {
|
||||
removeExtraKeys()
|
||||
ComponentManager.getComponent<ExtraKeysComponent>().showShortcutKeys(title, extraKeysView)
|
||||
ComponentManager.getComponent<ExtraKeyComponent>().showShortcutKeys(title, extraKeysView)
|
||||
extraKeysView.updateButtons()
|
||||
lastTitle = title
|
||||
}
|
||||
|
@ -7,7 +7,8 @@ import android.view.*
|
||||
import android.widget.GridLayout
|
||||
import android.widget.LinearLayout
|
||||
import io.neoterm.R
|
||||
import io.neoterm.component.extrakey.NeoExtraKey
|
||||
import io.neoterm.component.extrakey.ExtraKeyComponent
|
||||
import io.neoterm.frontend.component.ComponentManager
|
||||
import io.neoterm.frontend.config.NeoPreference
|
||||
import io.neoterm.frontend.config.NeoTermPath
|
||||
import io.neoterm.frontend.session.shell.client.event.ToggleImeEvent
|
||||
@ -62,11 +63,14 @@ class ExtraKeysView(context: Context, attrs: AttributeSet) : LinearLayout(contex
|
||||
}
|
||||
}
|
||||
|
||||
private val extraKeyComponent: ExtraKeyComponent
|
||||
|
||||
init {
|
||||
alpha = DEFAULT_ALPHA
|
||||
gravity = Gravity.TOP
|
||||
orientation = LinearLayout.VERTICAL
|
||||
typeface = Typeface.createFromAsset(context.assets, "eks_font.ttf")
|
||||
extraKeyComponent = ComponentManager.getComponent<ExtraKeyComponent>()
|
||||
|
||||
initBuiltinKeys()
|
||||
loadDefaultUserKeys()
|
||||
@ -117,20 +121,18 @@ class ExtraKeysView(context: Context, attrs: AttributeSet) : LinearLayout(contex
|
||||
|
||||
fun loadDefaultUserKeys() {
|
||||
clearUserKeys()
|
||||
val defaultConfig = NeoExtraKey()
|
||||
if (defaultConfig.loadConfigure(File(NeoTermPath.EKS_DEFAULT_FILE))) {
|
||||
val defaultConfig = extraKeyComponent.loadConfigure(File(NeoTermPath.EKS_DEFAULT_FILE))
|
||||
if (defaultConfig != null) {
|
||||
userKeys.addAll(defaultConfig.shortcutKeys)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateButtons() {
|
||||
for (bar in buttonBars) {
|
||||
bar.removeAllViews()
|
||||
}
|
||||
buttonBars.forEach { it.removeAllViews() }
|
||||
|
||||
var targetButtonBarIndex = 0
|
||||
for ((index, value) in builtinKeys.plus(userKeys).withIndex()) {
|
||||
addKeyButton(getButtonBarOrNew(targetButtonBarIndex), value)
|
||||
builtinKeys.plus(userKeys).forEachIndexed { index, button ->
|
||||
addKeyButton(getButtonBarOrNew(targetButtonBarIndex), button)
|
||||
targetButtonBarIndex = (index + 1) / MAX_BUTTONS_PER_LINE
|
||||
}
|
||||
updateButtonBars()
|
||||
|
@ -20,7 +20,7 @@ class ConfigureFileTest {
|
||||
}
|
||||
|
||||
val color = NeoColorScheme()
|
||||
if (color.loadConfigure(File("NeoLang/example/color-scheme.nl"))) {
|
||||
if (color.testLoadConfigure(File("NeoLang/example/color-scheme.nl"))) {
|
||||
println("colorName: ${color.colorName}")
|
||||
println("colorVersion: ${color.colorVersion}")
|
||||
println("background: ${color.backgroundColor}")
|
||||
@ -38,7 +38,7 @@ class ConfigureFileTest {
|
||||
}
|
||||
|
||||
val extraKey = NeoExtraKey()
|
||||
if (extraKey.loadConfigure(File("app/src/main/assets/eks/vim.nl"))) {
|
||||
if (extraKey.testLoadConfigure(File("app/src/main/assets/eks/vim.nl"))) {
|
||||
println("programs: ${extraKey.programNames}")
|
||||
println("version: ${extraKey.version}")
|
||||
println("with-default: ${extraKey.withDefaultKeys}")
|
||||
|
Loading…
Reference in New Issue
Block a user