Browse Source

StyleBuilder

STYLEBUILDER_SIMPLIFICATION
Shagen Ogandzhanian 3 years ago
parent
commit
d2a4ac238d
  1. 2
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSBuilder.kt
  2. 8
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSKeyframeRule.kt
  3. 5
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSRules.kt
  4. 3
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StylePropertyBuilder.kt
  5. 2
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleSheet.kt
  6. 6
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleSheetBuilder.kt

2
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSBuilder.kt

@ -2,7 +2,7 @@ package org.jetbrains.compose.web.css
import org.jetbrains.compose.web.css.selectors.CSSSelector import org.jetbrains.compose.web.css.selectors.CSSSelector
interface CSSBuilder : CSSStyleRuleBuilder, GenericStyleSheetBuilder<CSSBuilder> { interface CSSBuilder : StyleBuilder, GenericStyleSheetBuilder<CSSBuilder> {
val root: CSSSelector val root: CSSSelector
val self: CSSSelector val self: CSSSelector
} }

8
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSKeyframeRule.kt

@ -50,19 +50,19 @@ class CSSKeyframesBuilder() {
} }
val frames: MutableList<CSSKeyframeRuleDeclaration> = mutableListOf() val frames: MutableList<CSSKeyframeRuleDeclaration> = mutableListOf()
fun from(style: CSSStyleRuleBuilder.() -> Unit) { fun from(style: StyleBuilder.() -> Unit) {
frames += CSSKeyframeRuleDeclaration(CSSKeyframe.From, buildCSSStyleRule(style)) frames += CSSKeyframeRuleDeclaration(CSSKeyframe.From, buildCSSStyleRule(style))
} }
fun to(style: CSSStyleRuleBuilder.() -> Unit) { fun to(style: StyleBuilder.() -> Unit) {
frames += CSSKeyframeRuleDeclaration(CSSKeyframe.To, buildCSSStyleRule(style)) frames += CSSKeyframeRuleDeclaration(CSSKeyframe.To, buildCSSStyleRule(style))
} }
fun each(vararg keys: CSSSizeValue<CSSUnit.percent>, style: CSSStyleRuleBuilder.() -> Unit) { fun each(vararg keys: CSSSizeValue<CSSUnit.percent>, style: StyleBuilder.() -> Unit) {
frames += CSSKeyframeRuleDeclaration(CSSKeyframe.Combine(keys.toList()), buildCSSStyleRule(style)) frames += CSSKeyframeRuleDeclaration(CSSKeyframe.Combine(keys.toList()), buildCSSStyleRule(style))
} }
operator fun CSSSizeValue<CSSUnit.percent>.invoke(style: CSSStyleRuleBuilder.() -> Unit) { operator fun CSSSizeValue<CSSUnit.percent>.invoke(style: StyleBuilder.() -> Unit) {
frames += CSSKeyframeRuleDeclaration(CSSKeyframe.Percentage(this), buildCSSStyleRule(style)) frames += CSSKeyframeRuleDeclaration(CSSKeyframe.Percentage(this), buildCSSStyleRule(style))
} }
} }

5
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSRules.kt

@ -2,9 +2,8 @@ package org.jetbrains.compose.web.css
import org.jetbrains.compose.web.css.selectors.CSSSelector import org.jetbrains.compose.web.css.selectors.CSSSelector
interface CSSStyleRuleBuilder : StylePropertyBuilder, StyleVariableBuilder
open class CSSRuleBuilderImpl : CSSStyleRuleBuilder, StyleBuilderImpl() open class CSSRuleBuilderImpl : StyleBuilderImpl()
@Suppress("EqualsOrHashCode") @Suppress("EqualsOrHashCode")
interface CSSRuleDeclaration { interface CSSRuleDeclaration {
@ -32,7 +31,7 @@ interface CSSGroupingRuleDeclaration: CSSRuleDeclaration {
typealias CSSRuleDeclarationList = List<CSSRuleDeclaration> typealias CSSRuleDeclarationList = List<CSSRuleDeclaration>
typealias MutableCSSRuleDeclarationList = MutableList<CSSRuleDeclaration> typealias MutableCSSRuleDeclarationList = MutableList<CSSRuleDeclaration>
fun buildCSSStyleRule(cssRule: CSSStyleRuleBuilder.() -> Unit): StyleHolder { fun buildCSSStyleRule(cssRule: StyleBuilder.() -> Unit): StyleHolder {
val builder = CSSRuleBuilderImpl() val builder = CSSRuleBuilderImpl()
builder.cssRule() builder.cssRule()
return builder return builder

3
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StylePropertyBuilder.kt

@ -34,6 +34,7 @@ interface StyleVariableBuilder {
} }
} }
interface StyleBuilder : StylePropertyBuilder, StyleVariableBuilder
inline fun variableValue(variableName: String, fallback: StylePropertyValue? = null) = inline fun variableValue(variableName: String, fallback: StylePropertyValue? = null) =
"var(--$variableName${fallback?.let { ", $it" } ?: ""})" "var(--$variableName${fallback?.let { ", $it" } ?: ""})"
@ -91,7 +92,7 @@ interface StyleHolder {
} }
@Suppress("EqualsOrHashCode") @Suppress("EqualsOrHashCode")
open class StyleBuilderImpl : StylePropertyBuilder, CSSStyleRuleBuilder, StyleHolder { open class StyleBuilderImpl : StyleBuilder, StyleHolder {
override val properties: MutableStylePropertyList = mutableListOf() override val properties: MutableStylePropertyList = mutableListOf()
override val variables: MutableStylePropertyList = mutableListOf() override val variables: MutableStylePropertyList = mutableListOf()

2
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleSheet.kt

@ -115,7 +115,7 @@ open class StyleSheet(
} }
} }
override fun buildRules(rulesBuild: GenericStyleSheetBuilder<CSSStyleRuleBuilder>.() -> Unit) = override fun buildRules(rulesBuild: GenericStyleSheetBuilder<StyleBuilder>.() -> Unit) =
StyleSheet().apply(rulesBuild).cssRules StyleSheet().apply(rulesBuild).cssRules
} }

6
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleSheetBuilder.kt

@ -34,8 +34,8 @@ interface GenericStyleSheetBuilder<TBuilder> : CSSRulesHolder {
} }
} }
interface StyleSheetBuilder : CSSRulesHolder, GenericStyleSheetBuilder<CSSStyleRuleBuilder> { interface StyleSheetBuilder : CSSRulesHolder, GenericStyleSheetBuilder<StyleBuilder> {
override fun style(selector: CSSSelector, cssRule: CSSStyleRuleBuilder.() -> Unit) { override fun style(selector: CSSSelector, cssRule: StyleBuilder.() -> Unit) {
add(selector, buildCSSStyleRule(cssRule)) add(selector, buildCSSStyleRule(cssRule))
} }
} }
@ -47,6 +47,6 @@ open class StyleSheetBuilderImpl : StyleSheetBuilder {
cssRules.add(cssRule) cssRules.add(cssRule)
} }
override fun buildRules(rulesBuild: GenericStyleSheetBuilder<CSSStyleRuleBuilder>.() -> Unit) = override fun buildRules(rulesBuild: GenericStyleSheetBuilder<StyleBuilder>.() -> Unit) =
StyleSheetBuilderImpl().apply(rulesBuild).cssRules StyleSheetBuilderImpl().apply(rulesBuild).cssRules
} }

Loading…
Cancel
Save