All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.woojiahao.style.elements.Element.kt Maven / Gradle / Ivy

There is a newer version: 0.2.3
Show newest version
package com.github.woojiahao.style.elements

import com.github.woojiahao.style.Settings
import com.github.woojiahao.style.css.CssProperty
import com.github.woojiahao.style.css.CssSelector
import com.github.woojiahao.style.css.cssSelector
import com.github.woojiahao.style.utility.*
import com.github.woojiahao.utility.c
import com.github.woojiahao.utility.cssColor
import java.awt.Color

open class Element(private val elementName: String) {

  enum class FontWeight { NORMAL, BOLD, BOLDER, LIGHTER }

  enum class TextDecoration {
    OVERLINE, LINE_THROUGH, UNDERLINE, NONE;

    fun toCss() = name.replace("_", "-").toLowerCase()
  }

  var fontSize by CssProperty(fallback = Settings.fontSize)
  var fontFamily by CssProperty(fallback = Settings.font)
  var textColor by CssProperty(c("21"), c("EE"))
  var backgroundColor by CssProperty()
  var fontWeight by CssProperty()
  var textDecoration by CssProperty()
  var border by CssProperty(fallback = BorderBox(Border()))
  var borderRadius by CssProperty(fallback = Box(0.0.px))
  var padding by CssProperty>?>()
  var margin by CssProperty>?>()

  val globalCss
    get() = cssSelector(elementName) {
      attributes {
        "font-size" to fontSize?.let { it }
        "font-family" to fontFamily
        "color" to textColor?.cssColor()
        "background-color" to backgroundColor?.cssColor()
        "font-weight" to fontWeight?.name?.toLowerCase()
        "text-decoration" to textDecoration?.toCss()
        "border-radius" to borderRadius?.toCss { it.toString() }
        "padding" to padding?.toCss { it.toString() }
        "margin" to margin?.toCss { it.toString() }
        "border-top" to border?.top
        "border-right" to border?.right
        "border-bottom" to border?.bottom
        "border-left" to border?.left
      }
    }

  protected val css = mutableListOf()

  fun fontFamily(load: FontFamily.() -> Unit) {
    fontFamily?.clear()
    fontFamily?.load()
  }

  fun border(load: BorderBox.() -> Unit) = border?.load()

  open fun toCss(): String {
    css.add(globalCss)
    return css.joinToString("\n\n") { it.toCss() }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy