Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a simpler API to manipulate safe area insets #2473

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.isEqualTo
import kotlin.test.Test
import platform.CoreGraphics.CGRectMake
import kotlinx.cinterop.cValue
import platform.CoreGraphics.CGRectZero
import platform.UIKit.UIEdgeInsets
import platform.UIKit.UIEdgeInsetsMake
import platform.UIKit.UIEdgeInsetsZero
import platform.UIKit.UILabel
import platform.UIKit.UIView
import platform.UIKit.UIViewController
import platform.UIKit.UIWindow
import platform.UIKit.additionalSafeAreaInsets

class RedwoodUIViewTest {
@Test
Expand All @@ -41,31 +41,17 @@ class RedwoodUIViewTest {
assertThat(redwoodUIView.value.subviews).containsExactly(label)
}

/**
* Confirm we accept and propagates insets through [RedwoodUIView.uiConfiguration].
*
* Testing insets is tricky. We need both a [UIWindow] and a [UIViewController] to apply insets to
* a subject view.
*/
/** Confirm we accept and propagates insets through [RedwoodUIView.uiConfiguration]. */
@Test
fun viewInsets() {
val redwoodUIView = RedwoodUIView()
val viewController = object : UIViewController(null, null) {
override fun loadView() {
view = redwoodUIView.value
}
}

val window = UIWindow(
CGRectMake(0.0, 0.0, 390.0, 844.0), // iPhone 14.
)
window.setHidden(false) // Necessary to propagate additionalSafeAreaInsets.
window.rootViewController = viewController
val insetsContainer = InsetsContainer()
insetsContainer.addSubview(redwoodUIView.value)

assertThat(redwoodUIView.uiConfiguration.value.viewInsets)
.isEqualTo(Margin.Zero)

viewController.additionalSafeAreaInsets = UIEdgeInsetsMake(10.0, 20.0, 30.0, 40.0)
insetsContainer.subviewSafeAreaInsets = UIEdgeInsetsMake(10.0, 20.0, 30.0, 40.0)

assertThat(redwoodUIView.uiConfiguration.value.viewInsets)
.isEqualTo(Margin(top = 10.0.dp, start = 20.0.dp, bottom = 30.0.dp, end = 40.0.dp))
Expand All @@ -76,4 +62,16 @@ class RedwoodUIViewTest {
) : Widget<UIView> {
override var modifier: Modifier = Modifier
}

/** Override [safeAreaInsets] to propagate a test value to subviews on the next layout. */
class InsetsContainer : UIView(cValue { CGRectZero }) {
var subviewSafeAreaInsets = cValue<UIEdgeInsets> { UIEdgeInsetsZero }
set(value) {
field = value
setNeedsLayout()
layoutIfNeeded()
}

override fun safeAreaInsets() = subviewSafeAreaInsets
}
}