Made StyledConfig inherit config

This commit is contained in:
Alexander Nozik 2018-09-17 19:48:23 +03:00
parent 7b83e17fcb
commit ad9002530d
3 changed files with 33 additions and 21 deletions

View File

@ -7,7 +7,7 @@ import hep.dataforge.names.Name
/** /**
* Mutable meta representing object state * Mutable meta representing object state
*/ */
class Config : MutableMetaNode<Config>() { open class Config : MutableMetaNode<Config>() {
/** /**
* Attach configuration node instead of creating one * Attach configuration node instead of creating one

View File

@ -41,7 +41,7 @@ abstract class MutableMetaNode<M : MutableMetaNode<M>> : MetaNode<M>(), MutableM
override val items: Map<String, MetaItem<M>> override val items: Map<String, MetaItem<M>>
get() = _items get() = _items
private fun itemChanged(name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?) { protected fun itemChanged(name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?) {
listeners.forEach { it(name, oldItem, newItem) } listeners.forEach { it(name, oldItem, newItem) }
} }

View File

@ -1,21 +1,29 @@
package hep.dataforge.meta package hep.dataforge.meta
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.toName
/** /**
* A configuration decorator with applied style * A configuration decorator with applied style
*/ */
class StyledConfig(val config: Config, val style: Meta = EmptyMeta) : MutableMeta<StyledConfig> { class StyledConfig(val config: Config, style: Meta = EmptyMeta) : Config() {
override fun onChange(owner: Any?, action: (Name, MetaItem<*>?, MetaItem<*>?) -> Unit) { var style: Meta = style
config.onChange(owner, action) set(value) {
field.items.forEach {
itemChanged(it.key.toName(), it.value, null)
}
field = value
value.items.forEach {
itemChanged(it.key.toName(), null, it.value)
}
}
init {
config.onChange { name, oldItem, newItem -> this.itemChanged(name, oldItem, newItem) }
} }
override fun removeListener(owner: Any) { override fun set(name: Name, item: MetaItem<Config>?) {
config.removeListener(owner)
}
override fun set(name: Name, item: MetaItem<StyledConfig>?) {
when (item) { when (item) {
null -> config.remove(name) null -> config.remove(name)
is MetaItem.ValueItem -> config[name] = item.value is MetaItem.ValueItem -> config[name] = item.value
@ -24,16 +32,16 @@ class StyledConfig(val config: Config, val style: Meta = EmptyMeta) : MutableMet
} }
} }
override val items: Map<String, MetaItem<StyledConfig>> override val items: Map<String, MetaItem<Config>>
get() = (config.items.keys + style.items.keys).associate { key -> get() = (config.items.keys + style.items.keys).associate { key ->
val value = config.items[key] val value = config.items[key]
val styleValue = style[key] val styleValue = style[key]
val item: MetaItem<StyledConfig> = when (value) { val item: MetaItem<Config> = when (value) {
null -> when (styleValue) { null -> when (styleValue) {
null -> error("Should be unreachable") null -> error("Should be unreachable")
is MetaItem.ValueItem -> MetaItem.ValueItem(styleValue.value) is MetaItem.ValueItem -> MetaItem.ValueItem(styleValue.value)
is MetaItem.SingleNodeItem -> MetaItem.SingleNodeItem(StyledConfig(config.empty(), styleValue.node)) is MetaItem.SingleNodeItem -> MetaItem.SingleNodeItem<Config>(StyledConfig(config.empty(), styleValue.node))
is MetaItem.MultiNodeItem -> MetaItem.MultiNodeItem(styleValue.nodes.map { StyledConfig(config.empty(), it) }) is MetaItem.MultiNodeItem -> MetaItem.MultiNodeItem<Config>(styleValue.nodes.map { StyledConfig(config.empty(), it) })
} }
is MetaItem.ValueItem -> MetaItem.ValueItem(value.value) is MetaItem.ValueItem -> MetaItem.ValueItem(value.value)
is MetaItem.SingleNodeItem -> MetaItem.SingleNodeItem( is MetaItem.SingleNodeItem -> MetaItem.SingleNodeItem(
@ -47,14 +55,18 @@ class StyledConfig(val config: Config, val style: Meta = EmptyMeta) : MutableMet
} }
} }
fun Config.withStyle(style: Meta = EmptyMeta) = StyledConfig(this, style) fun Config.withStyle(style: Meta = EmptyMeta) = if (this is StyledConfig) {
StyledConfig(this.config, style)
} else {
StyledConfig(this, style)
}
interface Styleable : Configurable { interface Styleable : Configurable {
val styledConfig: StyledConfig override val config: StyledConfig
override val config var style
get() = styledConfig.config get() = config.style
set(value) {
val style config.style = value
get() = styledConfig.style }
} }