Skip to content

Commit 0ea9170

Browse files
committed
Create the :compose-multiplatform-common subproject and add Box, ModifierOrAttrs, and SizeValue
1 parent 88fa4ac commit 0ea9170

File tree

14 files changed

+183
-10
lines changed

14 files changed

+183
-10
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugins {
2+
id("common-conventions")
3+
// TODO: `id("com.android.library") version "7.2.2"`?
4+
id("com.huanshankeji.kotlin-multiplatform-jvm-and-js-browser-sonatype-ossrh-publish-conventions")
5+
}
6+
7+
group = "com.huanshankeji"
8+
version = "0.1.0-SNAPSHOT"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
`lib-conventions`
3+
}
4+
5+
kotlin {
6+
sourceSets {
7+
named("commonMain") {
8+
dependencies {
9+
implementation(compose.runtime)
10+
}
11+
}
12+
named("jvmMain") {
13+
dependencies {
14+
implementation(compose.foundation)
15+
}
16+
}
17+
named("jsMain") {
18+
dependencies {
19+
implementation(compose.web.core)
20+
}
21+
}
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.huanshankeji.compose.layout
2+
3+
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.Element
5+
import com.huanshankeji.compose.ui.ModifierOrAttrs
6+
7+
expect abstract class BoxElement : Element
8+
9+
@Composable
10+
expect fun Box(modifierOrAttrs: ModifierOrAttrs<BoxElement> = null, content: @Composable () -> Unit)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.huanshankeji.compose.ui
2+
3+
import com.huanshankeji.compose.ui.unit.SizeValue
4+
5+
typealias ModifierOrAttrs<TElement> = (ModifierOrAttrsScope<TElement>.() -> Unit)?
6+
7+
expect abstract class Element
8+
expect class ModifierOrAttrsScope<TElement : Element> {
9+
fun style(builder: StyleScope.() -> Unit)
10+
}
11+
12+
expect class StyleScope {
13+
fun padding(value: SizeValue)
14+
}
15+
16+
/*
17+
/** An alternative immutable design like `Modifier`. */
18+
expect class ModifierOrAttrsImmutable<T : Element> {
19+
fun padding(): ModifierOrAttrsImmutable<T>
20+
}
21+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.huanshankeji.compose.ui.unit
2+
3+
expect abstract class SizeValue
4+
expect class DpOrPxValue : SizeValue
5+
6+
expect val Int.dpOrPx: DpOrPxValue
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.huanshankeji.compose.layout
2+
3+
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.ModifierOrAttrs
5+
import com.huanshankeji.compose.ui.toAttrs
6+
import org.jetbrains.compose.web.dom.Div
7+
import org.w3c.dom.HTMLDivElement
8+
9+
actual typealias BoxElement = HTMLDivElement
10+
11+
@Composable
12+
actual fun Box(modifierOrAttrs: ModifierOrAttrs<BoxElement>, content: @Composable () -> Unit) =
13+
Div(modifierOrAttrs.toAttrs()) { content() }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.huanshankeji.compose.ui
2+
3+
import com.huanshankeji.compose.ui.unit.SizeValue
4+
import org.jetbrains.compose.web.attributes.AttrsScope
5+
import org.jetbrains.compose.web.css.padding
6+
import org.jetbrains.compose.web.dom.AttrBuilderContext
7+
import org.w3c.dom.HTMLElement
8+
9+
fun <TElement : Element> ModifierOrAttrs<TElement>.toAttrs(): AttrBuilderContext<TElement>? =
10+
this?.let { { ModifierOrAttrsScope(this).it() } }
11+
12+
actual typealias Element = HTMLElement
13+
14+
actual class ModifierOrAttrsScope<TElement : Element>(val attrsScope: AttrsScope<TElement>) {
15+
actual fun style(builder: StyleScope.() -> Unit) =
16+
attrsScope.style { StyleScope(this).builder() }
17+
}
18+
19+
actual class StyleScope(val styleScope: org.jetbrains.compose.web.css.StyleScope) {
20+
actual fun padding(value: SizeValue) =
21+
styleScope.padding(value.platformValue)
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.huanshankeji.compose.ui.unit
2+
3+
import org.jetbrains.compose.web.css.CSSLengthOrPercentageValue
4+
import org.jetbrains.compose.web.css.CSSSizeValue
5+
import org.jetbrains.compose.web.css.CSSUnit
6+
import org.jetbrains.compose.web.css.px
7+
8+
actual abstract class SizeValue(val platformValue: CSSLengthOrPercentageValue)
9+
actual class DpOrPxValue(platformValue: CSSSizeValue<CSSUnit.px>) : SizeValue(platformValue)
10+
11+
actual val Int.dpOrPx: DpOrPxValue
12+
get() = DpOrPxValue(this.px)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.huanshankeji.compose.layout
2+
3+
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.Element
5+
import com.huanshankeji.compose.ui.ModifierOrAttrs
6+
import com.huanshankeji.compose.ui.toModifier
7+
8+
actual abstract class BoxElement : Element()
9+
10+
@Composable
11+
actual fun Box(modifierOrAttrs: ModifierOrAttrs<BoxElement>, content: @Composable () -> Unit) =
12+
androidx.compose.foundation.layout.Box(modifierOrAttrs.toModifier()) { content() }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.huanshankeji.compose.ui
2+
3+
import androidx.compose.foundation.layout.padding
4+
import androidx.compose.ui.Modifier
5+
import com.huanshankeji.compose.ui.unit.SizeValue
6+
7+
fun <TElement : Element> ModifierOrAttrs<TElement>.toModifier(): Modifier =
8+
this?.let {
9+
ModifierOrAttrsScope<TElement>(Modifier).apply(it).modifier
10+
} ?: Modifier
11+
12+
actual abstract class Element
13+
14+
actual class ModifierOrAttrsScope<TElement : Element>(modifier: Modifier) {
15+
var modifier: Modifier = modifier
16+
private set
17+
18+
fun modify(block: Modifier.() -> Modifier) {
19+
modifier = modifier.block()
20+
}
21+
22+
actual fun style(builder: StyleScope.() -> Unit) {
23+
StyleScope(this).builder()
24+
}
25+
}
26+
27+
actual class StyleScope(val modifierOrAttrsScope: ModifierOrAttrsScope<*>) {
28+
actual fun padding(value: SizeValue) = modifierOrAttrsScope.modify {
29+
padding(value.platformValue)
30+
}
31+
}

0 commit comments

Comments
 (0)