Component: Refactor profile
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package io.neoterm.component.colorscheme
|
package io.neoterm.component.colorscheme
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import io.neolang.visitor.ConfigVisitor
|
||||||
import io.neoterm.App
|
import io.neoterm.App
|
||||||
import io.neoterm.R
|
import io.neoterm.R
|
||||||
import io.neoterm.component.codegen.CodeGenComponent
|
import io.neoterm.component.codegen.CodeGenComponent
|
||||||
@ -49,7 +50,7 @@ class ColorSchemeComponent : ConfigFileBasedComponent<NeoColorScheme>() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateComponentObject(): NeoColorScheme {
|
override fun onCreateComponentObject(configVisitor: ConfigVisitor): NeoColorScheme {
|
||||||
return NeoColorScheme()
|
return NeoColorScheme()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package io.neoterm.component.extrakey
|
package io.neoterm.component.extrakey
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import io.neolang.visitor.ConfigVisitor
|
||||||
import io.neoterm.App
|
import io.neoterm.App
|
||||||
import io.neoterm.frontend.component.helper.ConfigFileBasedComponent
|
import io.neoterm.frontend.component.helper.ConfigFileBasedComponent
|
||||||
import io.neoterm.frontend.config.NeoTermPath
|
import io.neoterm.frontend.config.NeoTermPath
|
||||||
@ -34,7 +35,7 @@ class ExtraKeyComponent : ConfigFileBasedComponent<NeoExtraKey>() {
|
|||||||
reloadExtraKeyConfig()
|
reloadExtraKeyConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateComponentObject(): NeoExtraKey {
|
override fun onCreateComponentObject(configVisitor: ConfigVisitor): NeoExtraKey {
|
||||||
return NeoExtraKey()
|
return NeoExtraKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
package io.neoterm.component.profile
|
package io.neoterm.component.profile
|
||||||
|
|
||||||
import io.neolang.visitor.ConfigVisitor
|
|
||||||
import io.neoterm.component.codegen.CodeGenParameter
|
import io.neoterm.component.codegen.CodeGenParameter
|
||||||
import io.neoterm.component.codegen.generator.ICodeGenerator
|
import io.neoterm.component.codegen.generator.ICodeGenerator
|
||||||
import io.neoterm.component.codegen.impl.NeoProfileGenerator
|
import io.neoterm.component.codegen.impl.NeoProfileGenerator
|
||||||
import io.neoterm.component.codegen.model.CodeGenObject
|
import io.neoterm.component.codegen.model.CodeGenObject
|
||||||
|
import io.neoterm.frontend.component.helper.ConfigFileBasedObject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kiva
|
* @author kiva
|
||||||
*/
|
*/
|
||||||
abstract class NeoProfile : CodeGenObject {
|
abstract class NeoProfile : CodeGenObject, ConfigFileBasedObject {
|
||||||
abstract val profileMetaName: String
|
abstract val profileMetaName: String
|
||||||
|
|
||||||
abstract fun onProfileLoaded(visitor: ConfigVisitor): Boolean
|
|
||||||
|
|
||||||
override fun getCodeGenerator(parameter: CodeGenParameter): ICodeGenerator {
|
override fun getCodeGenerator(parameter: CodeGenParameter): ICodeGenerator {
|
||||||
return NeoProfileGenerator(parameter)
|
return NeoProfileGenerator(parameter)
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,37 @@
|
|||||||
package io.neoterm.component.profile
|
package io.neoterm.component.profile
|
||||||
|
|
||||||
|
import io.neolang.visitor.ConfigVisitor
|
||||||
import io.neoterm.component.config.ConfigureComponent
|
import io.neoterm.component.config.ConfigureComponent
|
||||||
import io.neoterm.frontend.component.ComponentManager
|
import io.neoterm.frontend.component.ComponentManager
|
||||||
import io.neoterm.frontend.component.NeoComponent
|
import io.neoterm.frontend.component.NeoComponent
|
||||||
|
import io.neoterm.frontend.component.helper.ConfigFileBasedComponent
|
||||||
import io.neoterm.frontend.config.NeoConfigureFile
|
import io.neoterm.frontend.config.NeoConfigureFile
|
||||||
import io.neoterm.frontend.config.NeoTermPath
|
import io.neoterm.frontend.config.NeoTermPath
|
||||||
import io.neoterm.frontend.logging.NLog
|
import io.neoterm.frontend.logging.NLog
|
||||||
|
import org.jetbrains.annotations.TestOnly
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kiva
|
* @author kiva
|
||||||
*/
|
*/
|
||||||
class ProfileComponent : NeoComponent {
|
class ProfileComponent : NeoComponent, ConfigFileBasedComponent<NeoProfile>() {
|
||||||
|
override fun onCheckComponentFiles() {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateComponentObject(configVisitor: ConfigVisitor): NeoProfile {
|
||||||
|
val rootContext = configVisitor.getRootContext()
|
||||||
|
|
||||||
|
val profileClass = rootContext.children
|
||||||
|
.mapNotNull { profileRegistry[it.contextName] }
|
||||||
|
.singleOrNull()
|
||||||
|
|
||||||
|
if (profileClass != null) {
|
||||||
|
return profileClass.newInstance()
|
||||||
|
}
|
||||||
|
|
||||||
|
throw IllegalArgumentException("No proper profile registry for found")
|
||||||
|
}
|
||||||
|
|
||||||
private val profileRegistry = mutableMapOf<String, Class<out NeoProfile>>()
|
private val profileRegistry = mutableMapOf<String, Class<out NeoProfile>>()
|
||||||
private val profileList = mutableListOf<NeoProfile>()
|
private val profileList = mutableListOf<NeoProfile>()
|
||||||
|
|
||||||
@ -23,41 +43,6 @@ class ProfileComponent : NeoComponent {
|
|||||||
profileRegistry.remove(metaName)
|
profileRegistry.remove(metaName)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T : NeoProfile> loadConfigure(file: File): T {
|
|
||||||
return loadConfigure(file, T::class.java) as T
|
|
||||||
}
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
fun loadConfigure(file: File, clazz: Class<out NeoProfile>): NeoProfile {
|
|
||||||
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("Profile", "Failed to load profile: ${file.absolutePath}: ${e.localizedMessage}")
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
|
|
||||||
val visitor = configure.getVisitor()
|
|
||||||
val rootContext = visitor.getRootContext()
|
|
||||||
|
|
||||||
val profileClass = rootContext.children
|
|
||||||
.mapNotNull { profileRegistry[it.contextName] }
|
|
||||||
.singleOrNull()
|
|
||||||
|
|
||||||
if (profileClass != null) {
|
|
||||||
val profile = profileClass.newInstance()
|
|
||||||
profile.onProfileLoaded(visitor)
|
|
||||||
return profile
|
|
||||||
}
|
|
||||||
|
|
||||||
throw IllegalArgumentException("No profile registry for ${clazz.simpleName} found")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onServiceInit() {
|
override fun onServiceInit() {
|
||||||
checkForFiles()
|
checkForFiles()
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package io.neoterm.frontend.component.helper
|
package io.neoterm.frontend.component.helper
|
||||||
|
|
||||||
|
import io.neolang.visitor.ConfigVisitor
|
||||||
import io.neoterm.component.config.ConfigureComponent
|
import io.neoterm.component.config.ConfigureComponent
|
||||||
import io.neoterm.frontend.component.ComponentManager
|
import io.neoterm.frontend.component.ComponentManager
|
||||||
import io.neoterm.frontend.component.NeoComponent
|
import io.neoterm.frontend.component.NeoComponent
|
||||||
@ -35,8 +36,9 @@ abstract class ConfigFileBasedComponent<out T : ConfigFileBasedObject> : NeoComp
|
|||||||
val configure = loaderService.newLoader(file).loadConfigure()
|
val configure = loaderService.newLoader(file).loadConfigure()
|
||||||
?: throw RuntimeException("Parse configuration failed.")
|
?: throw RuntimeException("Parse configuration failed.")
|
||||||
|
|
||||||
val componentObject = onCreateComponentObject()
|
val configVisitor = configure.getVisitor()
|
||||||
componentObject.onConfigLoaded(configure.getVisitor())
|
val componentObject = onCreateComponentObject(configVisitor)
|
||||||
|
componentObject.onConfigLoaded(configVisitor)
|
||||||
componentObject
|
componentObject
|
||||||
} catch (e: RuntimeException) {
|
} catch (e: RuntimeException) {
|
||||||
NLog.e(TAG, "Failed to load config: ${file.absolutePath}: ${e.localizedMessage}")
|
NLog.e(TAG, "Failed to load config: ${file.absolutePath}: ${e.localizedMessage}")
|
||||||
@ -46,5 +48,5 @@ abstract class ConfigFileBasedComponent<out T : ConfigFileBasedObject> : NeoComp
|
|||||||
|
|
||||||
abstract fun onCheckComponentFiles()
|
abstract fun onCheckComponentFiles()
|
||||||
|
|
||||||
abstract fun onCreateComponentObject(): T
|
abstract fun onCreateComponentObject(configVisitor: ConfigVisitor): T
|
||||||
}
|
}
|
@ -67,20 +67,19 @@ class ShellProfile : NeoProfile() {
|
|||||||
enableWordBasedIme = NeoPreference.isWordBasedImeEnabled()
|
enableWordBasedIme = NeoPreference.isWordBasedImeEnabled()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onProfileLoaded(visitor: ConfigVisitor): Boolean {
|
override fun onConfigLoaded(configVisitor: ConfigVisitor) {
|
||||||
loginShell = visitor.getProfileString(LOGIN_SHELL, loginShell)
|
loginShell = configVisitor.getProfileString(LOGIN_SHELL, loginShell)
|
||||||
initialCommand = visitor.getProfileString(INITIAL_COMMAND, initialCommand)
|
initialCommand = configVisitor.getProfileString(INITIAL_COMMAND, initialCommand)
|
||||||
enableBell = visitor.getProfileBoolean(BELL, enableBell)
|
enableBell = configVisitor.getProfileBoolean(BELL, enableBell)
|
||||||
enableVibrate = visitor.getProfileBoolean(VIBRATE, enableVibrate)
|
enableVibrate = configVisitor.getProfileBoolean(VIBRATE, enableVibrate)
|
||||||
enableExecveWrapper = visitor.getProfileBoolean(EXECVE_WRAPPER, enableExecveWrapper)
|
enableExecveWrapper = configVisitor.getProfileBoolean(EXECVE_WRAPPER, enableExecveWrapper)
|
||||||
enableSpecialVolumeKeys = visitor.getProfileBoolean(SPECIAL_VOLUME_KEYS, enableSpecialVolumeKeys)
|
enableSpecialVolumeKeys = configVisitor.getProfileBoolean(SPECIAL_VOLUME_KEYS, enableSpecialVolumeKeys)
|
||||||
enableAutoCompletion = visitor.getProfileBoolean(AUTO_COMPLETION, enableAutoCompletion)
|
enableAutoCompletion = configVisitor.getProfileBoolean(AUTO_COMPLETION, enableAutoCompletion)
|
||||||
enableBackKeyToEscape = visitor.getProfileBoolean(BACK_KEY_TO_ESC, enableBackKeyToEscape)
|
enableBackKeyToEscape = configVisitor.getProfileBoolean(BACK_KEY_TO_ESC, enableBackKeyToEscape)
|
||||||
enableExtraKeys = visitor.getProfileBoolean(EXTRA_KEYS, enableExtraKeys)
|
enableExtraKeys = configVisitor.getProfileBoolean(EXTRA_KEYS, enableExtraKeys)
|
||||||
enableWordBasedIme = visitor.getProfileBoolean(WORD_BASED_IME, enableWordBasedIme)
|
enableWordBasedIme = configVisitor.getProfileBoolean(WORD_BASED_IME, enableWordBasedIme)
|
||||||
profileFont = visitor.getProfileString(FONT, profileFont)
|
profileFont = configVisitor.getProfileString(FONT, profileFont)
|
||||||
profileColorScheme = visitor.getProfileString(COLOR_SCHEME, profileColorScheme)
|
profileColorScheme = configVisitor.getProfileString(COLOR_SCHEME, profileColorScheme)
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ConfigVisitor.getProfileString(key: String, fallback: String): String {
|
private fun ConfigVisitor.getProfileString(key: String, fallback: String): String {
|
||||||
|
Reference in New Issue
Block a user