Component: Refactor color scheme
This commit is contained in:
@ -5,41 +5,66 @@ import io.neoterm.App
|
|||||||
import io.neoterm.R
|
import io.neoterm.R
|
||||||
import io.neoterm.component.codegen.CodeGenComponent
|
import io.neoterm.component.codegen.CodeGenComponent
|
||||||
import io.neoterm.frontend.component.ComponentManager
|
import io.neoterm.frontend.component.ComponentManager
|
||||||
|
import io.neoterm.frontend.component.helper.FileBasedComponent
|
||||||
import io.neoterm.frontend.config.NeoPreference
|
import io.neoterm.frontend.config.NeoPreference
|
||||||
import io.neoterm.frontend.config.NeoTermPath
|
import io.neoterm.frontend.config.NeoTermPath
|
||||||
import io.neoterm.frontend.component.NeoComponent
|
|
||||||
import io.neoterm.frontend.logging.NLog
|
import io.neoterm.frontend.logging.NLog
|
||||||
import io.neoterm.utils.AssetsUtils
|
|
||||||
import io.neoterm.frontend.terminal.TerminalView
|
import io.neoterm.frontend.terminal.TerminalView
|
||||||
import io.neoterm.frontend.terminal.extrakey.ExtraKeysView
|
import io.neoterm.frontend.terminal.extrakey.ExtraKeysView
|
||||||
|
import io.neoterm.utils.AssetsUtils
|
||||||
import io.neoterm.utils.FileUtils
|
import io.neoterm.utils.FileUtils
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kiva
|
* @author kiva
|
||||||
*/
|
*/
|
||||||
class ColorSchemeComponent : NeoComponent {
|
class ColorSchemeComponent : FileBasedComponent<NeoColorScheme>() {
|
||||||
companion object {
|
companion object {
|
||||||
fun colorFile(colorName: String): File {
|
fun colorFile(colorName: String): File {
|
||||||
return File("${NeoTermPath.COLORS_PATH}/$colorName.nl")
|
return File("${NeoTermPath.COLORS_PATH}/$colorName.nl")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override val checkComponentFileWhenObtained = true
|
||||||
|
|
||||||
private lateinit var DEFAULT_COLOR: NeoColorScheme
|
private lateinit var DEFAULT_COLOR: NeoColorScheme
|
||||||
private lateinit var colors: MutableMap<String, NeoColorScheme>
|
private lateinit var colors: MutableMap<String, NeoColorScheme>
|
||||||
|
|
||||||
|
override fun onCheckComponentFiles() {
|
||||||
|
File(NeoTermPath.COLORS_PATH).mkdirs()
|
||||||
|
colors = mutableMapOf()
|
||||||
|
|
||||||
|
val defaultColorFile = colorFile(DefaultColorScheme.colorName)
|
||||||
|
if (!defaultColorFile.exists()) {
|
||||||
|
if (!extractDefaultColor(App.get())) {
|
||||||
|
DEFAULT_COLOR = DefaultColorScheme
|
||||||
|
colors[DEFAULT_COLOR.colorName] = DEFAULT_COLOR
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!reloadColorSchemes()) {
|
||||||
|
DEFAULT_COLOR = DefaultColorScheme
|
||||||
|
colors[DEFAULT_COLOR.colorName] = DEFAULT_COLOR
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateComponentObject(): NeoColorScheme {
|
||||||
|
return NeoColorScheme()
|
||||||
|
}
|
||||||
|
|
||||||
fun reloadColorSchemes(): Boolean {
|
fun reloadColorSchemes(): Boolean {
|
||||||
colors.clear()
|
colors.clear()
|
||||||
val colorDir = File(NeoTermPath.COLORS_PATH)
|
|
||||||
for (file in colorDir.listFiles({ pathname ->
|
|
||||||
pathname.name.endsWith(".color") || pathname.name.endsWith(".nl")
|
|
||||||
})) {
|
|
||||||
val color = NeoColorScheme()
|
|
||||||
|
|
||||||
if (color.loadConfigure(file)) {
|
File(NeoTermPath.COLORS_PATH).listFiles { pathname ->
|
||||||
colors.put(color.colorName, color)
|
pathname.name.endsWith(".color") || pathname.name.endsWith(".nl")
|
||||||
|
}.forEach {
|
||||||
|
val colorScheme = this.loadConfigure(it)
|
||||||
|
if (colorScheme != null) {
|
||||||
|
colors.put(colorScheme.colorName, colorScheme)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (colors.containsKey(DefaultColorScheme.colorName)) {
|
if (colors.containsKey(DefaultColorScheme.colorName)) {
|
||||||
DEFAULT_COLOR = colors[DefaultColorScheme.colorName]!!
|
DEFAULT_COLOR = colors[DefaultColorScheme.colorName]!!
|
||||||
return true
|
return true
|
||||||
@ -82,17 +107,6 @@ class ColorSchemeComponent : NeoComponent {
|
|||||||
setCurrentColorScheme(color.colorName)
|
setCurrentColorScheme(color.colorName)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onServiceObtained() {
|
|
||||||
checkForFiles()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onServiceInit() {
|
|
||||||
checkForFiles()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onServiceDestroy() {
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun extractDefaultColor(context: Context): Boolean {
|
private fun extractDefaultColor(context: Context): Boolean {
|
||||||
try {
|
try {
|
||||||
AssetsUtils.extractAssetsDir(context, "colors", NeoTermPath.COLORS_PATH)
|
AssetsUtils.extractAssetsDir(context, "colors", NeoTermPath.COLORS_PATH)
|
||||||
@ -103,25 +117,6 @@ class ColorSchemeComponent : NeoComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkForFiles() {
|
|
||||||
File(NeoTermPath.COLORS_PATH).mkdirs()
|
|
||||||
colors = mutableMapOf()
|
|
||||||
|
|
||||||
val defaultColorFile = colorFile(DefaultColorScheme.colorName)
|
|
||||||
if (!defaultColorFile.exists()) {
|
|
||||||
if (!extractDefaultColor(App.get())) {
|
|
||||||
DEFAULT_COLOR = DefaultColorScheme
|
|
||||||
colors[DEFAULT_COLOR.colorName] = DEFAULT_COLOR
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!reloadColorSchemes()) {
|
|
||||||
DEFAULT_COLOR = DefaultColorScheme
|
|
||||||
colors[DEFAULT_COLOR.colorName] = DEFAULT_COLOR
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun saveColorScheme(colorScheme: NeoColorScheme) {
|
fun saveColorScheme(colorScheme: NeoColorScheme) {
|
||||||
val colorFile = colorFile(colorScheme.colorName)
|
val colorFile = colorFile(colorScheme.colorName)
|
||||||
if (colorFile.exists()) {
|
if (colorFile.exists()) {
|
||||||
|
@ -14,6 +14,7 @@ import io.neoterm.frontend.config.NeoConfigureFile
|
|||||||
import io.neoterm.frontend.logging.NLog
|
import io.neoterm.frontend.logging.NLog
|
||||||
import io.neoterm.frontend.terminal.TerminalView
|
import io.neoterm.frontend.terminal.TerminalView
|
||||||
import io.neoterm.frontend.terminal.extrakey.ExtraKeysView
|
import io.neoterm.frontend.terminal.extrakey.ExtraKeysView
|
||||||
|
import org.jetbrains.annotations.TestOnly
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -136,26 +137,6 @@ open class NeoColorScheme : CodeGenObject, FileBasedComponentObject {
|
|||||||
validateColors()
|
validateColors()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Use ColorSchemeComponent#loadConfigure() instead")
|
|
||||||
fun loadConfigure(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
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun applyColorScheme(view: TerminalView?, extraKeysView: ExtraKeysView?) {
|
internal fun applyColorScheme(view: TerminalView?, extraKeysView: ExtraKeysView?) {
|
||||||
validateColors()
|
validateColors()
|
||||||
|
|
||||||
@ -192,4 +173,24 @@ open class NeoColorScheme : CodeGenObject, FileBasedComponentObject {
|
|||||||
private fun getColorByVisitor(visitor: ConfigVisitor, colorName: String): String? {
|
private fun getColorByVisitor(visitor: ConfigVisitor, colorName: String): String? {
|
||||||
return visitor.getStringValue(COLOR_PATH, colorName)
|
return visitor.getStringValue(COLOR_PATH, colorName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestOnly
|
||||||
|
fun loadConfigure(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
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user