Node.kt (1555B) - raw


      1 package me.rhunk.snapenhance.common.scripting.ui.components
      2 
      3 @Suppress("MemberVisibilityCanBePrivate")
      4 open class Node(
      5     val type: NodeType,
      6 ) {
      7     lateinit var uiChangeDetection: (key: String, value: Any?) -> Unit
      8 
      9     val children = mutableListOf<Node>()
     10     val attributes = object: HashMap<String, Any?>() {
     11         override fun put(key: String, value: Any?): Any? {
     12             return super.put(key, value).also {
     13                 if (::uiChangeDetection.isInitialized) {
     14                     uiChangeDetection(key, value)
     15                 }
     16             }
     17         }
     18     }
     19 
     20     init {
     21         visibility("visible")
     22     }
     23 
     24     fun setAttribute(key: String, value: Any?) {
     25         attributes[key] = value
     26     }
     27 
     28     fun fillMaxWidth(): Node {
     29         attributes["fillMaxWidth"] = true
     30         return this
     31     }
     32 
     33     fun fillMaxHeight(): Node {
     34         attributes["fillMaxHeight"] = true
     35         return this
     36     }
     37 
     38     fun label(text: String): Node {
     39         attributes["label"] = text
     40         return this
     41     }
     42 
     43     fun padding(padding: Int): Node {
     44         attributes["padding"] = padding
     45         return this
     46     }
     47 
     48     fun fontSize(size: Int): Node {
     49         attributes["fontSize"] = size
     50         return this
     51     }
     52 
     53     fun color(color: Long): Node {
     54         attributes["color"] = color
     55         return this
     56     }
     57 
     58     fun visibility(state: String) {
     59         assert(state == "visible" || state == "invisible" || state == "gone") { "Invalid visibility state. Must be one of: visible, invisible, gone" }
     60         attributes["visibility"] = state
     61     }
     62 }