Skip to content

Commit cb10017

Browse files
committed
Add a demo project and extract common Gradle conventions, meanwhile improve TopAppBarScaffold and Material icons
1 parent 07a25f9 commit cb10017

File tree

19 files changed

+247
-73
lines changed

19 files changed

+247
-73
lines changed

buildSrc/build.gradle.kts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
`kotlin-dsl`
3+
}
4+
5+
repositories {
6+
gradlePluginPortal()
7+
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
8+
google()
9+
}
10+
11+
dependencies {
12+
implementation(kotlin("gradle-plugin", "1.7.20"))
13+
implementation("org.jetbrains.compose:compose-gradle-plugin:1.2.1")
14+
implementation("com.huanshankeji.team:gradle-plugins:0.3.2") {
15+
exclude("org.jetbrains.kotlin")
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
kotlin("multiplatform")
3+
id("org.jetbrains.compose")
4+
}
5+
6+
repositories {
7+
mavenCentral()
8+
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
9+
google()
10+
}
11+
12+
kotlin {
13+
jvm() // TODO: `jvm("desktop")`?
14+
// TODO: `android()`?
15+
js(IR)
16+
}

compose-multiplatform-material/build.gradle.kts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
plugins {
2-
kotlin("multiplatform")
3-
id("org.jetbrains.compose") version "1.2.1"
2+
`common-conventions`
43
// TODO: `id("com.android.library") version "7.2.2"`?
54
id("com.huanshankeji.kotlin-multiplatform-jvm-and-js-browser-sonatype-ossrh-publish-conventions")
65
}
76

87
group = "com.huanshankeji"
98
version = "0.1.0-SNAPSHOT"
109

11-
repositories {
12-
mavenCentral()
13-
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
14-
google()
15-
}
16-
1710
kotlin {
18-
jvm() // TODO: `jvm("desktop")`?
19-
// TODO: `android()`?
20-
js(IR) {
21-
browser()
22-
}
23-
2411
sourceSets {
2512
val commonMain by getting {
2613
dependencies {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.huanshankeji.compose
2+
3+
import androidx.compose.runtime.Composable
4+
5+
@Composable
6+
expect fun Text(text: String)
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.huanshankeji.compose.material
22

33
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.material.icon.MaterialIcon
45

56
expect /*value*/ class ButtonScope {
67
@Composable
@@ -11,11 +12,34 @@ expect /*value*/ class ButtonScope {
1112
expect fun Button(onClick: () -> Unit, content: @Composable ButtonScope.() -> Unit)
1213

1314

14-
//expect class TopAppBarActionsScope
15+
@Composable
16+
expect fun Icon(materialIcon: MaterialIcon)
17+
18+
19+
@Composable
20+
expect fun IconButton(onClick: () -> Unit, content: @Composable () -> Unit)
21+
22+
23+
expect class NavigationIconScope {
24+
@Composable
25+
fun NavButton(onClick: () -> Unit, content: @Composable () -> Unit)
26+
27+
@Composable
28+
fun MaterialIconNavButton(onClick: () -> Unit, materialIcon: MaterialIcon)
29+
}
30+
31+
expect class TopAppBarActionsScope {
32+
@Composable
33+
fun ActionButton(onClick: () -> Unit, content: @Composable () -> Unit)
34+
35+
@Composable
36+
fun MaterialIconActionButton(onClick: () -> Unit, materialIcon: MaterialIcon)
37+
}
38+
1539
@Composable
1640
expect fun TopAppBarScaffold(
1741
title: @Composable () -> Unit,
18-
navigationIcon: @Composable (() -> Unit)? = null,
19-
actions: @Composable /*TopAppBarActionsScope.*/() -> Unit = {},
42+
navigationIcon: @Composable (NavigationIconScope.() -> Unit)? = null,
43+
actions: @Composable TopAppBarActionsScope.() -> Unit = {},
2044
content: @Composable () -> Unit
2145
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.huanshankeji.compose.material.icon
2+
3+
expect class MaterialIcon
4+
5+
expect object MaterialIcons {
6+
val Menu : MaterialIcon
7+
val Search : MaterialIcon
8+
}

compose-multiplatform-material/src/commonMain/kotlin/com/huanshankeji/compose/material/icons/Icons.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.huanshankeji.compose
2+
3+
import androidx.compose.runtime.Composable
4+
5+
@Composable
6+
actual fun Text(text: String) =
7+
org.jetbrains.compose.web.dom.Text(text)

compose-multiplatform-material/src/jsMain/kotlin/com/huanshankeji/compose/material/Components.kt

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.huanshankeji.compose.material
22

33
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.material.icon.MaterialIcon
45
import dev.petuska.kmdc.button.Label
56
import dev.petuska.kmdc.button.MDCButton
67
import dev.petuska.kmdc.button.MDCButtonScope
8+
import dev.petuska.kmdc.icon.button.MDCIconButton
79
import dev.petuska.kmdc.top.app.bar.*
10+
import dev.petuska.kmdcx.icons.MDCIconSpan
11+
import dev.petuska.kmdcx.icons.mdcIcon
12+
import org.jetbrains.compose.web.dom.Text
813
import org.w3c.dom.HTMLButtonElement
914

1015
actual class ButtonScope(val mdcButtonScope: MDCButtonScope<HTMLButtonElement>) {
@@ -18,18 +23,56 @@ actual fun Button(onClick: () -> Unit, content: @Composable ButtonScope.() -> Un
1823
MDCButton(attrs = { onClick { onClick() } }) { ButtonScope(this).content() }
1924

2025

26+
/**
27+
* There is often a better alternative of adding the CSS rule to the parent element to using this composable directly.
28+
*/
29+
@Composable
30+
actual fun Icon(materialIcon: MaterialIcon) =
31+
MDCIconSpan(materialIcon.mdcIcon)
32+
33+
34+
@Composable
35+
actual fun IconButton(onClick: () -> Unit, content: @Composable () -> Unit) =
36+
MDCIconButton(attrs = {
37+
onClick { onClick() }
38+
}) { content() }
39+
40+
41+
actual class NavigationIconScope(val mdcTopAppBarSectionScope: MDCTopAppBarSectionScope) {
42+
@Composable
43+
actual fun NavButton(onClick: () -> Unit, content: @Composable () -> Unit) =
44+
mdcTopAppBarSectionScope.NavButton(attrs = { onClick { onClick() } }) { content() }
45+
46+
@Composable
47+
actual fun MaterialIconNavButton(onClick: () -> Unit, materialIcon: MaterialIcon) =
48+
mdcTopAppBarSectionScope.NavButton(attrs = { mdcIcon() }) { Text(materialIcon.mdcIcon.type) }
49+
}
50+
51+
actual class TopAppBarActionsScope(val mdcTopAppBarSectionScope: MDCTopAppBarSectionScope) {
52+
@Composable
53+
actual fun ActionButton(onClick: () -> Unit, content: @Composable () -> Unit) =
54+
mdcTopAppBarSectionScope.ActionButton(attrs = { onClick { onClick() } }) { content() }
55+
56+
@Composable
57+
actual fun MaterialIconActionButton(
58+
onClick: () -> Unit,
59+
materialIcon: MaterialIcon
60+
) =
61+
mdcTopAppBarSectionScope.ActionButton(attrs = { mdcIcon() }) { Text(materialIcon.mdcIcon.type) }
62+
}
63+
2164
@Composable
2265
actual fun TopAppBarScaffold(
2366
title: @Composable () -> Unit,
24-
navigationIcon: @Composable (() -> Unit)?,
25-
actions: @Composable () -> Unit,
67+
navigationIcon: @Composable (NavigationIconScope.() -> Unit)?,
68+
actions: @Composable TopAppBarActionsScope.() -> Unit,
2669
content: @Composable () -> Unit
2770
) =
2871
MDCTopAppBar {
2972
TopAppBar {
3073
Row {
3174
Section(align = MDCTopAppBarSectionAlign.Start) {
32-
navigationIcon?.let { NavButton { it() } }
75+
navigationIcon?.let { NavButton { NavigationIconScope(this@Section).it() } }
3376
Title { title() }
3477
}
3578
Section(
@@ -38,7 +81,7 @@ actual fun TopAppBarScaffold(
3881
attr("role", "toolbar")
3982
}
4083
) {
41-
actions()
84+
TopAppBarActionsScope(this).actions()
4285
}
4386
}
4487
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.huanshankeji.compose.material.icon
2+
3+
import dev.petuska.kmdcx.icons.MDCIcon
4+
5+
actual class MaterialIcon(val mdcIcon: MDCIcon)
6+
7+
actual object MaterialIcons {
8+
actual val Menu = MaterialIcon(MDCIcon.Menu)
9+
actual val Search = MaterialIcon(MDCIcon.Search)
10+
}

0 commit comments

Comments
 (0)