From 10a886292f54bce0c46e93be30f803d799d18fed Mon Sep 17 00:00:00 2001 From: Wojt Janowski Date: Thu, 12 Nov 2026 21:40:29 +0000 Subject: [PATCH] Let an h-full drawer child fill the drawer's height Both drawer hosts put their children in a scroll view, which measures them with an unbounded height, so an h-full root column hugged its content and justify-between couldn't pin a footer to the bottom. As in the scroll-view renderers, a direct child with h-full now gets the drawer's height as a minimum; taller content still grows and scrolls, and other children are unchanged. Closes #113. Co-authored-by: Wojt Janowski --- resources/android/NativeLayoutDrawerHost.kt | 46 +++++++++++-- resources/ios/NativeUIDrawerHost.swift | 32 ++++++--- .../ios/NativeUIScrollViewRenderer.swift | 2 +- tests/android/README.md | 9 ++- tests/android/build.gradle.kts | 3 +- tests/android/src/main/kotlin/NodeView.kt | 4 +- .../src/test/kotlin/DrawerFillHeightTest.kt | 67 +++++++++++++++++++ 7 files changed, 141 insertions(+), 22 deletions(-) create mode 100644 tests/android/src/test/kotlin/DrawerFillHeightTest.kt diff --git a/resources/android/NativeLayoutDrawerHost.kt b/resources/android/NativeLayoutDrawerHost.kt index aea7feb0..70aa9ccd 100644 --- a/resources/android/NativeLayoutDrawerHost.kt +++ b/resources/android/NativeLayoutDrawerHost.kt @@ -1,9 +1,12 @@ package com.nativephp.plugins.native_ui.ui import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.layout.width @@ -26,6 +29,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.nativephp.mobile.ui.nativerender.NativeUINode import com.nativephp.mobile.ui.nativerender.RenderNode +import com.nativephp.mobile.ui.nativerender.SizeMode import kotlinx.coroutines.launch /** @@ -103,13 +107,41 @@ fun NativeLayoutDrawerHost( @Composable private fun DrawerChildren(drawerNode: NativeUINode) { - Column( - modifier = Modifier - .fillMaxHeight() - .verticalScroll(rememberScrollState()) - ) { - drawerNode.children.forEach { child -> - RenderNode(child) + // A `fill` / `h-full` DIRECT child is at least as tall as the drawer, so + // `justify-between` can pin a footer to the bottom. The scroll measures + // its children with an unbounded height, so `fillMaxHeight()` alone has + // nothing to fill and the child hugs its content. The drawer's height is + // handed over as a MINIMUM, the same rule as ScrollViewRenderer: content + // taller than the drawer still grows and scrolls. + BoxWithConstraints(Modifier.fillMaxHeight()) { + val viewport = maxHeight.takeIf { it.value.isFinite() && it.value > 0f } + Column( + modifier = Modifier + .fillMaxHeight() + .verticalScroll(rememberScrollState()) + ) { + drawerNode.children.forEach { child -> + val layout = child.layout + if (viewport != null && layout?.heightMode == SizeMode.FILL) { + // Width is reproduced because the modifier replaces + // NodeView's own sizing, as in ScrollViewRenderer. + var childMod: Modifier = Modifier.heightIn(min = viewport) + when (layout.widthMode) { + SizeMode.FILL -> childMod = childMod.fillMaxWidth() + SizeMode.FIXED -> if (layout.width > 0f) { + childMod = childMod.width(layout.width.dp) + } + SizeMode.PERCENT -> if (layout.width > 0f) { + childMod = childMod.fillMaxWidth( + (layout.width / 100f).coerceIn(0f, 1f) + ) + } + } + RenderNode(child, childMod) + } else { + RenderNode(child) + } + } } } } diff --git a/resources/ios/NativeUIDrawerHost.swift b/resources/ios/NativeUIDrawerHost.swift index fa2dd53a..94e019b6 100644 --- a/resources/ios/NativeUIDrawerHost.swift +++ b/resources/ios/NativeUIDrawerHost.swift @@ -255,18 +255,30 @@ struct NativeDrawerHost: View { @ViewBuilder private func drawerView(_ drawerNode: NativeUINode, width: CGFloat) -> some View { - ScrollView { - VStack(alignment: .leading, spacing: 0) { - ForEach(drawerNode.children) { child in - NodeView(node: child).equatable() + // Nothing inside the ScrollView knows how tall the panel is, so an + // `h-full` child (a `justify-between` column pinning a footer) would + // hug its content. The panel's height, less the top inset, goes to + // those direct children as a minimum, as in the scroll-view renderer: + // taller content still grows and scrolls. + GeometryReader { geometry in + ScrollView { + VStack(alignment: .leading, spacing: 0) { + ForEach(drawerNode.children) { child in + NodeView(node: child).equatable() + .modifier(MinViewportHeightModifier( + minHeight: child.layout?.heightMode == SizeMode.fill + ? geometry.size.height - windowSafeAreaTop + : nil + )) + } } + // The panel ignores the safe area so its background reaches the top + // of the screen; without this inset the CONTENT does too, and the + // first row slides under the status bar. An app that pins + // UIUserInterfaceStyle sees this at every launch, not just on a + // notched device in landscape. + .padding(.top, windowSafeAreaTop) } - // The panel ignores the safe area so its background reaches the top - // of the screen; without this inset the CONTENT does too, and the - // first row slides under the status bar. An app that pins - // UIUserInterfaceStyle sees this at every launch, not just on a - // notched device in landscape. - .padding(.top, windowSafeAreaTop) } .frame(width: width) .frame(maxHeight: .infinity) diff --git a/resources/ios/NativeUIScrollViewRenderer.swift b/resources/ios/NativeUIScrollViewRenderer.swift index 33e53dab..d2ab4046 100644 --- a/resources/ios/NativeUIScrollViewRenderer.swift +++ b/resources/ios/NativeUIScrollViewRenderer.swift @@ -469,7 +469,7 @@ private struct BottomProbe: Equatable { /// Applies a minimum height only when there is one, so children that didn't /// ask for viewport height keep the exact modifier chain they always had. -private struct MinViewportHeightModifier: ViewModifier { +struct MinViewportHeightModifier: ViewModifier { let minHeight: CGFloat? @ViewBuilder diff --git a/tests/android/README.md b/tests/android/README.md index c42eed50..547848da 100644 --- a/tests/android/README.md +++ b/tests/android/README.md @@ -2,7 +2,8 @@ This standalone Robolectric/Compose module compiles the production filled and outlined editors, their shared dispatcher, the production Button renderer and -the production gesture area, list, virtual list, pager and bottom sheet renderers. +the production gesture area, list, virtual list, pager and bottom sheet renderers +and the layout drawer host. It records the bridge event boundary; it does not load PHP/JNI, render icons or menus, or prove device IME behaviour. The Mobile policy and node models are read from an explicit checkout, not substituted by test implementations. @@ -53,4 +54,10 @@ viewport's top, not just that a scroll was invoked. composition: when its detents change in the same render, and when a finger grabs it while it slides away. `RenderNode` here draws each child as `NodeView` does. + +`DrawerFillHeightTest` opens a modal drawer from its menu button and checks +an `h-full` child reaches the bottom of the drawer, a sibling without it +keeps its own height, and a child taller than the drawer still scrolls. +`NodeView` here wraps its box in the modifier the drawer passes, so these +check the sizes the drawer hands out, not `justify-between` itself. These are JVM checks; SwiftUI and native device behaviour need separate testing. diff --git a/tests/android/build.gradle.kts b/tests/android/build.gradle.kts index 257bed69..ee80a16c 100644 --- a/tests/android/build.gradle.kts +++ b/tests/android/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } val stageSources = tasks.register("stageSources") { from(layout.projectDirectory.dir("../../resources/android")) { - include("FilledTextInputRenderer.kt", "OutlinedTextInputRenderer.kt", "BareTextInputRenderer.kt", "TextInputShared.kt", "NativeUIFontResolver.kt", "NativeUITheme.kt", "ButtonRenderer.kt", "GestureAreaRenderer.kt", "ListRenderer.kt", "VirtualListRenderer.kt", "PagerRenderer.kt", "BottomSheetRenderer.kt") + include("FilledTextInputRenderer.kt", "OutlinedTextInputRenderer.kt", "BareTextInputRenderer.kt", "TextInputShared.kt", "NativeUIFontResolver.kt", "NativeUITheme.kt", "ButtonRenderer.kt", "GestureAreaRenderer.kt", "ListRenderer.kt", "VirtualListRenderer.kt", "PagerRenderer.kt", "BottomSheetRenderer.kt", "NativeLayoutDrawerHost.kt") } from(providers.gradleProperty("mobileRoot").map { "$it/resources/androidstudio/app/src/main/java/com/nativephp/mobile/ui/nativerender" }) { include("KeyboardFocusPolicy.kt", "NativeUINode.kt", "SharedValueStore.kt") @@ -30,6 +30,7 @@ dependencies { implementation(platform("androidx.compose:compose-bom:2025.12.00")) implementation("androidx.compose.foundation:foundation") implementation("androidx.compose.material3:material3") + implementation("androidx.compose.material:material-icons-core") implementation("androidx.compose.ui:ui") implementation("io.coil-kt.coil3:coil-compose:3.1.0") testImplementation("junit:junit:4.13.2") diff --git a/tests/android/src/main/kotlin/NodeView.kt b/tests/android/src/main/kotlin/NodeView.kt index 7faa547b..27ad74ba 100644 --- a/tests/android/src/main/kotlin/NodeView.kt +++ b/tests/android/src/main/kotlin/NodeView.kt @@ -11,11 +11,11 @@ import androidx.compose.ui.unit.dp /** * Draws a container's children for the gesture area tests: a 300x120 box * tagged `node-`, made clickable the way core makes a `pressable` when - * the node has a press handler. + * the node has a press handler. A parent's modifier wraps the box. */ @Composable fun NodeView(node: NativeUINode, overrideModifier: Modifier? = null) { - var modifier = Modifier.size(300.dp, 120.dp).testTag("node-${node.id}") + var modifier = (overrideModifier ?: Modifier).size(300.dp, 120.dp).testTag("node-${node.id}") if (node.onPress != 0) { modifier = modifier.clickable { NativeUIBridge.sendPressEvent(node.onPress, node.id) } } diff --git a/tests/android/src/test/kotlin/DrawerFillHeightTest.kt b/tests/android/src/test/kotlin/DrawerFillHeightTest.kt new file mode 100644 index 00000000..aef34bae --- /dev/null +++ b/tests/android/src/test/kotlin/DrawerFillHeightTest.kt @@ -0,0 +1,67 @@ +package com.nativephp.plugins.native_ui.ui + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.height +import androidx.compose.ui.Modifier +import androidx.compose.ui.semantics.SemanticsProperties +import androidx.compose.ui.test.* +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.height +import com.nativephp.mobile.ui.nativerender.* +import org.junit.Assert.assertEquals +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35]) +class DrawerFillHeightTest { + @get:Rule val compose = createComposeRule() + + @Test fun anHFullRootReachesTheBottomOfTheDrawer() { + open(fill(2)) + val root = compose.onNodeWithTag("node-2").getUnclippedBoundsInRoot() + assertNear(viewport().top, root.top) + assertNear(viewport().bottom, root.bottom) + } + + @Test fun aSiblingThatDoesNotFillKeepsItsOwnHeight() { + open(fill(2), plain(3)) + assertNear(viewport().height, compose.onNodeWithTag("node-2").getUnclippedBoundsInRoot().height) + compose.onNodeWithTag("node-3").assertHeightIsEqualTo(120.dp) + } + + @Test fun aRootTallerThanTheDrawerStillGrowsAndScrolls() { + open(fill(2), height = 100.dp) + compose.onNodeWithTag("node-2").assertHeightIsEqualTo(120.dp) + compose.onNode(drawerScroll).performTouchInput { swipeUp() } + assertNear(viewport().bottom, compose.onNodeWithTag("node-2").getUnclippedBoundsInRoot().bottom) + } + + private val drawerScroll = SemanticsMatcher.keyIsDefined(SemanticsProperties.VerticalScrollAxisRange) + + /** The drawer's scrolling column: the height an `h-full` child should reach. */ + private fun viewport() = compose.onNode(drawerScroll).getUnclippedBoundsInRoot() + + private fun open(vararg children: NativeUINode, height: Dp? = null) { + compose.setContent { + Box(if (height != null) Modifier.height(height) else Modifier) { + NativeLayoutDrawerHost(NativeUINode(1, "native_drawer", null, null, GenericProps(), 0, 0, children.toList())) {} + } + } + compose.onNodeWithContentDescription("Open menu").performClick() + compose.waitForIdle() + } + + private fun assertNear(expected: Dp, actual: Dp) = assertEquals(expected.value, actual.value, 0.5f) + + /** `w-full h-full`, like a drawer root column with `justify-between`. */ + private fun fill(id: Int) = NativeUINode( + id, "column", NodeLayout.EMPTY.copy(widthMode = SizeMode.FILL, heightMode = SizeMode.FILL), null, GenericProps(), 0, 0, emptyList()) + + private fun plain(id: Int) = NativeUINode(id, "column", null, null, GenericProps(), 0, 0, emptyList()) +}