Skip to content

Commit 047cb82

Browse files
committed
Refactor the LazyColumn composable to use modifiers and refactor the related scopes to be akin to those in androidx.compose
1 parent 1e7bab2 commit 047cb82

File tree

7 files changed

+106
-113
lines changed

7 files changed

+106
-113
lines changed

compose-multiplatform-material/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ kotlin {
1515
commonMain {
1616
dependencies {
1717
api(compose.runtime)
18+
implementation("org.jetbrains.compose.annotation-internal:annotation:${DependencyVersions.composeMultiplatform}")
1819
api(project(":compose-multiplatform-common:compose-multiplatform-common-legacy")) // TODO remove
1920
api(project(":compose-multiplatform-common"))
2021
//compileOnly(compose.material) // for KDoc element links only

compose-multiplatform-material/src/androidxCommonMain/kotlin/com/huanshankeji/compose/material/ListAndLazyColumn.androidxCommon.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.huanshankeji.compose.material.lazy.ext
2+
3+
import androidx.compose.foundation.ExperimentalFoundationApi
4+
import androidx.compose.runtime.Composable
5+
import com.huanshankeji.compose.ui.Modifier
6+
import androidx.compose.foundation.lazy.LazyItemScope as PlatformLazyItemScope
7+
import androidx.compose.foundation.lazy.LazyListScope as PlatformLazyListScope
8+
9+
actual class LazyListScope(val platformValue: PlatformLazyListScope) {
10+
actual fun item(key: Any?, contentType: Any?, content: @Composable LazyItemScope.() -> Unit) =
11+
platformValue.item(key, contentType) { LazyItemScope(this).content() }
12+
13+
actual fun items(
14+
count: Int,
15+
key: ((index: Int) -> Any)?,
16+
contentType: (index: Int) -> Any?,
17+
itemContent: @Composable LazyItemScope.(index: Int) -> Unit
18+
) =
19+
platformValue.items(count, key, contentType) { LazyItemScope(this).itemContent(it) }
20+
21+
@OptIn(ExperimentalFoundationApi::class)
22+
actual fun group(
23+
key: Any?,
24+
contentType: Any?,
25+
headerContent: @Composable HeaderScope.() -> Unit,
26+
content: LazyListScope.() -> Unit
27+
) {
28+
platformValue.stickyHeader(key, contentType) { HeaderScope(this).headerContent() }
29+
content()
30+
}
31+
}
32+
33+
actual class LazyItemScope(val platformValue: PlatformLazyItemScope)
34+
actual typealias HeaderScope = LazyItemScope
35+
36+
@Composable
37+
actual fun LazyColumn(modifier: Modifier, content: LazyListScope.() -> Unit) =
38+
// Note that it's actually in the `foundation` package.
39+
androidx.compose.foundation.lazy.LazyColumn(modifier.platformModifier) {
40+
LazyListScope(this).content()
41+
}

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

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.huanshankeji.compose.material.lazy.ext
2+
3+
//import androidx.compose.foundation.lazy.LazyListScope
4+
import androidx.compose.runtime.Composable
5+
import com.huanshankeji.compose.ui.Modifier
6+
7+
/** @see LazyListScope */
8+
expect class LazyListScope {
9+
fun item(key: Any? = null, contentType: Any? = null, content: @Composable LazyItemScope.() -> Unit)
10+
11+
fun items(
12+
count: Int,
13+
key: ((index: Int) -> Any)? = null,
14+
contentType: (index: Int) -> Any? = { null },
15+
itemContent: @Composable LazyItemScope.(index: Int) -> Unit
16+
)
17+
18+
fun group(
19+
key: Any? = null,
20+
contentType: Any? = null,
21+
headerContent: @Composable HeaderScope.() -> Unit,
22+
content: LazyListScope.() -> Unit
23+
)
24+
}
25+
26+
expect class LazyItemScope /*{
27+
fun Modifier.fillParentMaxSize(@FloatRange(from = 0.0, to = 1.0) fraction: Float = 1f): Modifier
28+
fun Modifier.fillParentMaxWidth(@FloatRange(from = 0.0, to = 1.0) fraction: Float = 1f): Modifier
29+
fun Modifier.fillParentMaxHeight(@FloatRange(from = 0.0, to = 1.0) fraction: Float = 1f): Modifier
30+
}*/
31+
32+
expect class HeaderScope
33+
34+
/**
35+
* The current implementation is not actually lazy on web, but it seems not necessary to be.
36+
*/
37+
@Composable
38+
expect fun LazyColumn(modifier: Modifier = Modifier, content: LazyListScope.() -> Unit)
39+
40+
/**
41+
* An alias for [LazyColumn] that is more conventional for Web/HTML/JS.
42+
*/
43+
@Composable
44+
fun ScrollableList(modifier: Modifier = Modifier, content: LazyListScope.() -> Unit) =
45+
LazyColumn(modifier, content)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
package com.huanshankeji.compose.material
1+
package com.huanshankeji.compose.material.lazy.ext
22

33
import androidx.compose.runtime.Composable
4-
import com.huanshankeji.compose.ui.ModifierOrAttrs
5-
import com.huanshankeji.compose.ui.toAttrs
6-
import com.huanshankeji.compose.web.attributes.attrs
7-
import com.huanshankeji.compose.web.attributes.plus
4+
import com.huanshankeji.compose.ui.Modifier
5+
import com.varabyte.kobweb.compose.ui.toAttrs
86
import dev.petuska.kmdc.list.MDCList
97
import dev.petuska.kmdc.list.MDCListGroup
108
import dev.petuska.kmdc.list.MDCListScope
@@ -17,13 +15,11 @@ import org.w3c.dom.HTMLHeadingElement
1715
import org.w3c.dom.HTMLLIElement
1816
import org.w3c.dom.HTMLUListElement
1917

20-
actual typealias ListElement = HTMLUListElement
21-
2218
/**
2319
* This class contains mutable fields.
2420
* @see androidx.compose.foundation.lazy.LazyListScopeImpl
2521
*/
26-
actual class ListScope(
22+
actual class LazyListScope(
2723
val mdcListScope: MDCListScope<HTMLUListElement>
2824
) {
2925
private var composables: MutableList<@Composable () -> Unit>? = null
@@ -33,7 +29,7 @@ actual class ListScope(
3329

3430
/** Add the composable functions with the non-composable functions and then invoke them. */
3531
@Composable
36-
fun ComposableRun(content: ListScope.() -> Unit) {
32+
fun ComposableRun(content: LazyListScope.() -> Unit) {
3733
composables = mutableListOf()
3834
content()
3935
for (composable in composables!!)
@@ -42,48 +38,48 @@ actual class ListScope(
4238
}
4339

4440

45-
actual fun item(key: Any?, contentType: Any?, content: @Composable ItemScope.() -> Unit) = addComposable {
46-
mdcListScope.ListItem { ItemScope(this).content() }
41+
actual fun item(key: Any?, contentType: Any?, content: @Composable LazyItemScope.() -> Unit) = addComposable {
42+
mdcListScope.ListItem { LazyItemScope(this).content() }
4743
}
4844

4945
actual fun items(
5046
count: Int,
5147
key: ((index: Int) -> Any)?,
5248
contentType: (index: Int) -> Any?,
53-
itemContent: @Composable ItemScope.(index: Int) -> Unit
49+
itemContent: @Composable LazyItemScope.(index: Int) -> Unit
5450
) = addComposable {
5551
repeat(count) { i ->
56-
mdcListScope.ListItem { ItemScope(this).itemContent(i) }
52+
mdcListScope.ListItem { LazyItemScope(this).itemContent(i) }
5753
}
5854
}
5955

6056
actual fun group(
6157
key: Any?,
6258
contentType: Any?,
6359
headerContent: @Composable HeaderScope.() -> Unit,
64-
content: ListScope.() -> Unit
60+
content: LazyListScope.() -> Unit
6561
) = addComposable {
6662
MDCListGroup {
6763
Subheader {
6864
HeaderScope(this).headerContent()
6965
}
7066
MDCList {
71-
ListScope(this).ComposableRun(content)
67+
LazyListScope(this).ComposableRun(content)
7268
}
7369
}
7470
}
7571
}
7672

77-
actual class ItemScope(val mdcListItemScope: MDCListItemScope<HTMLLIElement>)
78-
actual class HeaderScope(val elementScope: ElementScope<HTMLHeadingElement>)
73+
actual class LazyItemScope(val mdcListItemScope: MDCListItemScope<HTMLLIElement>)
74+
actual class HeaderScope(val headingElementScope: ElementScope<HTMLHeadingElement>)
7975

8076
@Composable
81-
actual fun ScrollableList(modifierOrAttrs: ModifierOrAttrs<ListElement>, content: ListScope.() -> Unit) =
82-
MDCList(attrs = attrs<ListElement> {
77+
actual fun LazyColumn(modifier: Modifier, content: LazyListScope.() -> Unit) =
78+
MDCList(attrs = modifier.platformModifier.toAttrs {
8379
style {
8480
//overflowY("scroll")
8581
overflowY("auto")
8682
}
87-
} + modifierOrAttrs.toAttrs()) {
88-
ListScope(this).ComposableRun(content)
83+
}) {
84+
LazyListScope(this).ComposableRun(content)
8985
}

demo/src/commonMain/kotlin/com/huanshankeji/compose/material/demo/App.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import com.huanshankeji.compose.material.icons.Icons
2323
import com.huanshankeji.compose.material.icons.filled.Add
2424
import com.huanshankeji.compose.material.icons.filled.Menu
2525
import com.huanshankeji.compose.material.icons.filled.Search
26+
import com.huanshankeji.compose.material.lazy.ext.LazyColumn
2627
import com.huanshankeji.compose.ui.Modifier
2728
import com.huanshankeji.compose.ui.graphics.Color
28-
import com.huanshankeji.compose.ui.height
29-
import com.huanshankeji.compose.ui.unit.dpOrPx
3029
import com.huanshankeji.compose.material.ext.Button as ExtButton
3130

3231
@Composable
@@ -65,11 +64,7 @@ fun App() {
6564
}
6665

6766
Box(Modifier.padding(16.dp)) {
68-
ScrollableList({
69-
style {
70-
height(100.dpOrPx)
71-
}
72-
}) {
67+
LazyColumn(Modifier.height(100.dp)) {
7368
item {
7469
Text("Ungrouped item")
7570
}

0 commit comments

Comments
 (0)