Browse Source

CSSUnitValue is an interface

pull/761/head
Shagen Ogandzhanian 4 years ago
parent
commit
c7223bdcb1
  1. 8
      web/core/src/jsMain/kotlin/androidx/compose/web/css/CSS.kt
  2. 17
      web/core/src/jsMain/kotlin/androidx/compose/web/css/CSSProperties.kt
  3. 204
      web/core/src/jsMain/kotlin/androidx/compose/web/css/CSSUnits.kt
  4. 3
      web/core/src/jsTest/kotlin/CSSUnitApiTests.kt

8
web/core/src/jsMain/kotlin/androidx/compose/web/css/CSS.kt

@ -142,8 +142,10 @@ external interface CSSNumericValue : CSSStyleValue {
// } // }
} }
open class CSSUnitValue(val value: Number, val unit: String) : CSSNumericValue { interface CSSUnitValue : CSSNumericValue {
override fun toString(): String = "${value}${unit}" val value: Number
val unit: String
fun asString(): String = "${value}${unit}"
} }
// declare enum CSSMathOperator { // declare enum CSSMathOperator {
@ -329,7 +331,7 @@ external interface StylePropertyValue
fun String.asStylePropertyValue() = unsafeCast<StylePropertyValue>() fun String.asStylePropertyValue() = unsafeCast<StylePropertyValue>()
fun Number.asStylePropertyValue() = unsafeCast<StylePropertyValue>() fun Number.asStylePropertyValue() = unsafeCast<StylePropertyValue>()
fun CSSStyleValue.asStylePropertyValue() = unsafeCast<StylePropertyValue>() fun CSSStyleValue.asStylePropertyValue() = unsafeCast<StylePropertyValue>()
fun CSSUnitValue.asStylePropertyValue() = toString().asStylePropertyValue() fun CSSUnitValue.asStylePropertyValue() = asString().asStylePropertyValue()
external class StylePropertyMap : StylePropertyMapReadOnly { external class StylePropertyMap : StylePropertyMapReadOnly {
fun set(property: String, vararg values: StylePropertyValue) fun set(property: String, vararg values: StylePropertyValue)

17
web/core/src/jsMain/kotlin/androidx/compose/web/css/CSSProperties.kt

@ -296,7 +296,7 @@ fun StyleBuilder.borderRadius(r: CSSUnitValue) {
} }
fun StyleBuilder.borderRadius(topLeft: CSSUnitValue, bottomRight: CSSUnitValue) { fun StyleBuilder.borderRadius(topLeft: CSSUnitValue, bottomRight: CSSUnitValue) {
property("border-radius", "$topLeft $bottomRight".asStylePropertyValue()) property("border-radius", "${topLeft.asString()} ${bottomRight.asString()}".asStylePropertyValue())
} }
fun StyleBuilder.borderRadius( fun StyleBuilder.borderRadius(
@ -304,7 +304,7 @@ fun StyleBuilder.borderRadius(
topRightAndBottomLeft: CSSUnitValue, topRightAndBottomLeft: CSSUnitValue,
bottomRight: CSSUnitValue bottomRight: CSSUnitValue
) { ) {
property("border-radius", "$topLeft $topRightAndBottomLeft $bottomRight".asStylePropertyValue()) property("border-radius", "${topLeft.asString()} ${topRightAndBottomLeft.asString()} ${bottomRight.asString()}".asStylePropertyValue())
} }
fun StyleBuilder.borderRadius( fun StyleBuilder.borderRadius(
@ -313,7 +313,10 @@ fun StyleBuilder.borderRadius(
bottomRight: CSSUnitValue, bottomRight: CSSUnitValue,
bottomLeft: CSSUnitValue bottomLeft: CSSUnitValue
) { ) {
property("border-radius", "$topLeft $topRight $bottomRight $bottomLeft".asStylePropertyValue()) property(
"border-radius",
"${topLeft.asString()} ${topRight.asString()} ${bottomRight.asString()} ${bottomLeft.asString()}".asStylePropertyValue()
)
} }
fun StyleBuilder.width(value: CSSUnitValue) { fun StyleBuilder.width(value: CSSUnitValue) {
@ -370,18 +373,18 @@ fun StyleBuilder.fontSize(value: CSSUnitValue) {
fun StyleBuilder.margin(value: CSSUnitValue) { fun StyleBuilder.margin(value: CSSUnitValue) {
// marign hasn't Typed OM yet // marign hasn't Typed OM yet
property("margin", value(value.toString())) property("margin", value(value.asString()))
} }
fun StyleBuilder.marginLeft(value: CSSUnitValue) { fun StyleBuilder.marginLeft(value: CSSUnitValue) {
property("margin-left", value(value.toString())) property("margin-left", value(value.asString()))
} }
fun StyleBuilder.marginTop(value: CSSUnitValue) { fun StyleBuilder.marginTop(value: CSSUnitValue) {
property("margin-top", value(value.toString())) property("margin-top", value(value.asString()))
} }
fun StyleBuilder.padding(value: CSSUnitValue) { fun StyleBuilder.padding(value: CSSUnitValue) {
// padding hasn't Typed OM yet // padding hasn't Typed OM yet
property("padding", value(value.toString())) property("padding", value(value.asString()))
} }

204
web/core/src/jsMain/kotlin/androidx/compose/web/css/CSSUnits.kt

@ -8,47 +8,173 @@ interface CSSFrequencyValue
interface CSSResolutionValue interface CSSResolutionValue
interface CSSFlexValue interface CSSFlexValue
class CSSpercentValue(value: Number) : CSSUnitValue(value, "percent"), CSSRelValue class CSSpercentValue(override val value: Number) : CSSUnitValue, CSSRelValue {
class CSSemValue(value: Number) : CSSUnitValue(value, "em"), CSSRelValue override val unit: String
class CSSexValue(value: Number) : CSSUnitValue(value, "ex"), CSSRelValue get() = "percent"
class CSSchValue(value: Number) : CSSUnitValue(value, "ch"), CSSRelValue }
class CSSicValue(value: Number) : CSSUnitValue(value, "ic"), CSSRelValue
class CSSremValue(value: Number) : CSSUnitValue(value, "rem"), CSSRelValue class CSSemValue(override val value: Number) : CSSUnitValue, CSSRelValue {
class CSSlhValue(value: Number) : CSSUnitValue(value, "lh"), CSSRelValue override val unit: String
class CSSrlhValue(value: Number) : CSSUnitValue(value, "rlh"), CSSRelValue get() = "em"
class CSSvwValue(value: Number) : CSSUnitValue(value, "vw"), CSSRelValue }
class CSSvhValue(value: Number) : CSSUnitValue(value, "vh"), CSSRelValue
class CSSviValue(value: Number) : CSSUnitValue(value, "vi"), CSSRelValue class CSSexValue(override val value: Number) : CSSUnitValue, CSSRelValue {
class CSSvbValue(value: Number) : CSSUnitValue(value, "vb"), CSSRelValue override val unit: String
class CSSvminValue(value: Number) : CSSUnitValue(value, "vmin"), CSSRelValue get() = "ex"
class CSSvmaxValue(value: Number) : CSSUnitValue(value, "vmax"), CSSRelValue }
class CSScmValue(value: Number) : CSSUnitValue(value, "cm"), CSSRelValue
class CSSmmValue(value: Number) : CSSUnitValue(value, "mm"), CSSRelValue class CSSchValue(override val value: Number) : CSSUnitValue, CSSRelValue {
class CSSQValue(value: Number) : CSSUnitValue(value, "q"), CSSRelValue override val unit: String
get() = "ch"
class CSSptValue(value: Number) : CSSUnitValue(value, "pt"), CSSAbsValue }
class CSSpcValue(value: Number) : CSSUnitValue(value, "pc"), CSSAbsValue
class CSSpxValue(value: Number) : CSSUnitValue(value, "px"), CSSAbsValue class CSSicValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
class CSSdegValue(value: Number) : CSSUnitValue(value, "deg"), CSSAngleValue get() = "ic"
class CSSgradValue(value: Number) : CSSUnitValue(value, "grad"), CSSAngleValue }
class CSSradValue(value: Number) : CSSUnitValue(value, "rad"), CSSAngleValue
class CSSturnValue(value: Number) : CSSUnitValue(value, "turn"), CSSAngleValue class CSSremValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
class CSSsValue(value: Number) : CSSUnitValue(value, "s"), CSSTimeValue get() = "rem"
class CSSmsValue(value: Number) : CSSUnitValue(value, "ms"), CSSTimeValue }
class CSSHzValue(value: Number) : CSSUnitValue(value, "hz"), CSSFrequencyValue class CSSlhValue(override val value: Number) : CSSUnitValue, CSSRelValue {
class CSSkHzValue(value: Number) : CSSUnitValue(value, "khz"), CSSFrequencyValue override val unit: String
get() = "lh"
class CSSdpiValue(value: Number) : CSSUnitValue(value, "dpi"), CSSResolutionValue }
class CSSdpcmValue(value: Number) : CSSUnitValue(value, "dpcm"), CSSResolutionValue
class CSSdppxValue(value: Number) : CSSUnitValue(value, "dppx"), CSSResolutionValue class CSSrlhValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
class CSSfrValue(value: Number) : CSSUnitValue(value, "fr"), CSSFlexValue get() = "rlh"
}
class CSSvwValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
get() = "vw"
}
class CSSvhValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
get() = "vh"
}
class CSSviValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
get() = "vi"
}
class CSSvbValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
get() = "vb"
}
class CSSvminValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
get() = "vmin"
}
class CSSvmaxValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
get() = "vmax"
}
class CSScmValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
get() = "cm"
}
class CSSmmValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
get() = "mm"
}
class CSSQValue(override val value: Number) : CSSUnitValue, CSSRelValue {
override val unit: String
get() = "q"
}
class CSSptValue(override val value: Number) : CSSUnitValue, CSSAbsValue {
override val unit: String
get() = "pt"
}
class CSSpcValue(override val value: Number) : CSSUnitValue, CSSAbsValue {
override val unit: String
get() = "pc"
}
class CSSpxValue(override val value: Number) : CSSUnitValue, CSSAbsValue {
override val unit: String
get() = "px"
}
class CSSdegValue(override val value: Number) : CSSUnitValue, CSSAngleValue {
override val unit: String
get() = "deg"
}
class CSSgradValue(override val value: Number) : CSSUnitValue, CSSAngleValue {
override val unit: String
get() = "grad"
}
class CSSradValue(override val value: Number) : CSSUnitValue, CSSAngleValue {
override val unit: String
get() = "rad"
}
class CSSturnValue(override val value: Number) : CSSUnitValue, CSSAngleValue {
override val unit: String
get() = "turn"
}
class CSSsValue(override val value: Number) : CSSUnitValue, CSSTimeValue {
override val unit: String
get() = "s"
}
class CSSmsValue(override val value: Number) : CSSUnitValue, CSSTimeValue {
override val unit: String
get() = "ms"
}
class CSSHzValue(override val value: Number) : CSSUnitValue, CSSFrequencyValue {
override val unit: String
get() = "hz"
}
class CSSkHzValue(override val value: Number) : CSSUnitValue, CSSFrequencyValue {
override val unit: String
get() = "khz"
}
class CSSdpiValue(override val value: Number) : CSSUnitValue, CSSResolutionValue {
override val unit: String
get() = "dpi"
}
class CSSdpcmValue(override val value: Number) : CSSUnitValue, CSSResolutionValue {
override val unit: String
get() = "dpcm"
}
class CSSdppxValue(override val value: Number) : CSSUnitValue, CSSResolutionValue {
override val unit: String
get() = "dppx"
}
class CSSfrValue(override val value: Number) : CSSUnitValue, CSSFlexValue {
override val unit: String
get() = "fr"
}
class CSSnumberValue(override val value: Number) : CSSUnitValue, CSSFlexValue {
override val unit: String
get() = "number"
}
val Number.number val Number.number
get(): CSSUnitValue = CSSUnitValue(this, "number") get(): CSSUnitValue = CSSnumberValue(this)
val Number.percent val Number.percent
get(): CSSpercentValue = CSSpercentValue(this) get(): CSSpercentValue = CSSpercentValue(this)

3
web/core/src/jsTest/kotlin/CSSUnitApiTests.kt

@ -18,6 +18,7 @@ import org.jetbrains.compose.web.css.CSSfrValue
import org.jetbrains.compose.web.css.CSSgradValue import org.jetbrains.compose.web.css.CSSgradValue
import org.jetbrains.compose.web.css.CSSmmValue import org.jetbrains.compose.web.css.CSSmmValue
import org.jetbrains.compose.web.css.CSSmsValue import org.jetbrains.compose.web.css.CSSmsValue
import org.jetbrains.compose.web.css.CSSnumberValue
import org.jetbrains.compose.web.css.CSSpcValue import org.jetbrains.compose.web.css.CSSpcValue
import org.jetbrains.compose.web.css.CSSpercentValue import org.jetbrains.compose.web.css.CSSpercentValue
import org.jetbrains.compose.web.css.CSSptValue import org.jetbrains.compose.web.css.CSSptValue
@ -109,7 +110,7 @@ class CSSUnitApiTests {
@Test @Test
fun postfixInvocation() { fun postfixInvocation() {
4.number.assertStructure(CSSUnitValue(4, "number"), "number postfix") 4.number.assertStructure(CSSnumberValue(4), "number postfix")
4.percent.assertStructure(CSSpercentValue(4), "percent posfix") 4.percent.assertStructure(CSSpercentValue(4), "percent posfix")
4.em.assertStructure(CSSemValue(4), "em postfix") 4.em.assertStructure(CSSemValue(4), "em postfix")

Loading…
Cancel
Save