Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ repositories {
}

dependencies {
testImplementation 'junit:junit:4.13.2'
implementation 'androidx.window:window:1.5.1'
implementation 'com.facebook.react:react-native:+'
implementation 'androidx.appcompat:appcompat:1.7.1'
implementation 'androidx.fragment:fragment-ktx:1.8.9'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.swmansion.rnscreens.core.modals.formsheet

internal data class FormSheetPlacement(
val width: Int,
val left: Int,
) {
companion object {
fun resolve(
windowWidth: Int,
maxWidth: Int,
hingeLeft: Int?,
): FormSheetPlacement {
val availableWidth =
if (hingeLeft != null && hingeLeft in 1 until windowWidth) hingeLeft else windowWidth
val width = minOf(availableWidth, maxWidth)
return FormSheetPlacement(width, (availableWidth - width) / 2)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.swmansion.rnscreens.core.modals.formsheet

import android.app.Activity
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Build
import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowInfoTracker
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

/** The same vertical book posture used by the workspace, shared by both sheet implementations. */
internal class FormSheetPostureObserver(
private val activityProvider: () -> Activity?,
private val onChange: () -> Unit,
) : SensorEventListener {
private var scope: CoroutineScope? = null
private var fold: FoldingFeature? = null
private var sensorManager: SensorManager? = null
private var hingeSensor: Sensor? = null
private var bent: Boolean? = null
private var listening = false
internal var hingeLeft: Int? = null
private set

fun start() {
if (scope != null) return
val activity = activityProvider() ?: return
sensorManager = activity.getSystemService(SensorManager::class.java)
hingeSensor =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) sensorManager?.getDefaultSensor(Sensor.TYPE_HINGE_ANGLE) else null
scope =
MainScope().also { scope ->
scope.launch {
WindowInfoTracker.getOrCreate(activity).windowLayoutInfo(activity).collect { info ->
fold =
info.displayFeatures.filterIsInstance<FoldingFeature>().firstOrNull {
it.orientation == FoldingFeature.Orientation.VERTICAL
}
listen(fold != null)
report()
}
}
}
}

fun stop() {
scope?.cancel()
scope = null
listen(false)
fold = null
hingeLeft = null
}

override fun onSensorChanged(event: SensorEvent) {
// Match workspace hysteresis: nearly flat Pixel hinges rest at 175 degrees.
bent = if (bent == true) event.values[0] < 175f else event.values[0] <= 170f
report()
}

override fun onAccuracyChanged(
sensor: Sensor?,
accuracy: Int,
) = Unit

private fun listen(enabled: Boolean) {
if (enabled == listening || hingeSensor == null) return
listening = enabled
if (enabled) {
sensorManager?.registerListener(this, hingeSensor, SensorManager.SENSOR_DELAY_UI)
} else {
sensorManager?.unregisterListener(this)
bent = null
}
}

private fun report() {
val feature = fold
val next = feature?.takeIf { bent ?: (it.state == FoldingFeature.State.HALF_OPENED) }?.bounds?.left
if (hingeLeft == next) return
hingeLeft = next
onChange()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ internal class FormSheetAvailableHeightProvider(
fun onAvailableHeightMeasured(height: Int)
}

internal var onAvailableWidthMeasured: ((Int) -> Unit)? = null

internal var availableHeightListener: OnAvailableHeightMeasuredListener? = null

init {
Expand All @@ -35,6 +37,10 @@ internal class FormSheetAvailableHeightProvider(
widthMeasureSpec: Int,
heightMeasureSpec: Int,
) {
val width = MeasureSpec.getSize(widthMeasureSpec)
if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED && width > 0) {
onAvailableWidthMeasured?.invoke(width)
}
val height = MeasureSpec.getSize(heightMeasureSpec)
if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.UNSPECIFIED && height > 0) {
availableHeightListener?.onAvailableHeightMeasured(height)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.swmansion.rnscreens.core.modals.formsheet.dialog

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.WindowCompat
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.swmansion.rnscreens.core.modals.formsheet.FormSheetPlacement
import com.swmansion.rnscreens.core.modals.formsheet.FormSheetPostureObserver

/**
* A custom BottomSheetDialog override used to render the FormSheet native component.
Expand All @@ -20,7 +25,15 @@ import com.google.android.material.bottomsheet.BottomSheetDialog
*/
internal class FormSheetDialog(
context: Context,
activityProvider: () -> Activity?,
) : BottomSheetDialog(context) {
private val posture =
FormSheetPostureObserver(activityProvider) {
// A posture change keeps the window bounds, so invalidate both measurement branches.
availableHeightProvider.requestLayout()
findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)?.requestLayout()
}

internal fun interface CancelRequestInterceptor {
/**
* Invoked in place of the dialog's default cancellation whenever an interceptor is attached.
Expand Down Expand Up @@ -53,9 +66,15 @@ internal class FormSheetDialog(
override fun onAttachedToWindow() {
super.onAttachedToWindow()

posture.start()
forceEdgeToEdge()
}

override fun onDetachedFromWindow() {
posture.stop()
super.onDetachedFromWindow()
}

override fun cancel() {
val interceptor = cancelRequestInterceptor
if (interceptor == null) {
Expand Down Expand Up @@ -87,7 +106,23 @@ internal class FormSheetDialog(

private fun setupBottomSheetHeight() {
val bottomSheetView = findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
bottomSheetView?.layoutParams?.height = ViewGroup.LayoutParams.MATCH_PARENT
bottomSheetView?.let { sheet ->
sheet.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
availableHeightProvider.onAvailableWidthMeasured = { width ->
if (isShowing) posture.start()
val placement =
FormSheetPlacement.resolve(
width,
context.resources.getDimensionPixelSize(com.google.android.material.R.dimen.material_bottom_sheet_max_width),
posture.hingeLeft,
)
val params = sheet.layoutParams as CoordinatorLayout.LayoutParams
params.width = placement.width
params.leftMargin = placement.left
params.gravity = Gravity.LEFT
behavior.maxWidth = placement.width
}
}
}

private fun installAvailableHeightProvider() {
Expand All @@ -105,7 +140,7 @@ internal class FormSheetDialog(
contentParent.addView(
availableHeightProvider,
0,
FrameLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT),
FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import kotlin.properties.Delegates
class FormSheetDialogManager(
context: Context,
private val contentView: View,
activityProvider: () -> Activity? = { context.findActivity() },
private val activityProvider: () -> Activity? = { context.findActivity() },
) {
private var formSheetConfig = FormSheetConfig()

Expand Down Expand Up @@ -75,7 +75,7 @@ class FormSheetDialogManager(
}

private fun createPresentation(): FormSheetPresentation =
FormSheetPresentation(themedContext, container, presentationCallbacks).also {
FormSheetPresentation(themedContext, container, presentationCallbacks, activityProvider).also {
it.applyInitialConfig(formSheetConfig, lastContentHeight)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.swmansion.rnscreens.core.modals.formsheet.presentation

import android.app.Activity
import android.content.Context
import android.util.Log
import android.view.ViewGroup
Expand All @@ -19,6 +20,7 @@ internal class FormSheetPresentation(
themedContext: Context,
private val container: FormSheetContainer,
private val callbacks: Callbacks,
activityProvider: () -> Activity?,
) {
internal interface Callbacks {
fun onDetentChanged(index: Int)
Expand All @@ -29,7 +31,7 @@ internal class FormSheetPresentation(
}

internal val dialog =
FormSheetDialog(themedContext).apply {
FormSheetDialog(themedContext, activityProvider).apply {
setContentView(container)
// Backdrop taps are handled by Material's `touch_outside` view, which calls `cancel()`,
// intercepted by FormSheetDialog.cancelRequestInterceptor, that's responsible for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.swmansion.rnscreens.legacy.stack.views

import android.content.Context
import android.view.Gravity
import android.view.WindowInsets
import android.view.animation.Animation
import android.view.animation.AnimationSet
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.facebook.react.uimanager.ReactPointerEventsView
import com.swmansion.rnscreens.core.modals.formsheet.FormSheetPlacement
import com.swmansion.rnscreens.core.modals.formsheet.FormSheetPostureObserver
import com.swmansion.rnscreens.legacy.PointerEventsBoxNoneImpl
import com.swmansion.rnscreens.legacy.ScreenStackFragment
import com.swmansion.rnscreens.legacy.bottomsheet.usesFormSheetPresentation
Expand All @@ -23,6 +26,20 @@ internal class ScreensCoordinatorLayout(
PointerEventsBoxNoneImpl(),
)

private var lastSheetWidth = 0

private val posture = FormSheetPostureObserver({ fragment.screen.reactContext.currentActivity }) { requestLayout() }

override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (fragment.screen.usesFormSheetPresentation()) posture.start()
}

override fun onDetachedFromWindow() {
posture.stop()
super.onDetachedFromWindow()
}

internal var blockFrameworkTransitionFinalization = false
internal var needsTransitionFinalization = false

Expand Down Expand Up @@ -113,6 +130,27 @@ internal class ScreensCoordinatorLayout(

override fun shouldDelayChildPressedState(): Boolean = fragment.screen.usesFormSheetPresentation()

override fun onMeasure(
widthMeasureSpec: Int,
heightMeasureSpec: Int,
) {
if (fragment.screen.usesFormSheetPresentation()) {
if (isAttachedToWindow) posture.start()
val origin = IntArray(2).also { getLocationInWindow(it) }
val placement =
FormSheetPlacement.resolve(
MeasureSpec.getSize(widthMeasureSpec),
resources.getDimensionPixelSize(com.google.android.material.R.dimen.material_bottom_sheet_max_width),
posture.hingeLeft?.minus(origin[0]),
)
val params = fragment.screen.layoutParams as LayoutParams
params.width = placement.width
params.leftMargin = placement.left
params.gravity = Gravity.LEFT
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}

override fun onLayout(
changed: Boolean,
l: Int,
Expand All @@ -123,7 +161,10 @@ internal class ScreensCoordinatorLayout(
super.onLayout(changed, l, t, r, b)

if (fragment.screen.usesFormSheetPresentation()) {
fragment.screen.onBottomSheetBehaviorDidLayout(changed)
val widthChanged = fragment.screen.width != lastSheetWidth
lastSheetWidth = fragment.screen.width
// The hinge can resize the sheet without resizing its workspace-sized coordinator.
fragment.screen.onBottomSheetBehaviorDidLayout(changed || widthChanged)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.swmansion.rnscreens.core.modals.formsheet

import org.junit.Assert.assertEquals
import org.junit.Test

class FormSheetPlacementTest {
@Test fun flatAndCoverWindowsKeepMaterialSizing() {
assertEquals(FormSheetPlacement(1560, 258), FormSheetPlacement.resolve(2076, 1560, null))
assertEquals(FormSheetPlacement(1080, 0), FormSheetPlacement.resolve(1080, 1560, null))
}

@Test fun bookStaysBeforeThePhysicalHinge() {
assertEquals(FormSheetPlacement(1038, 0), FormSheetPlacement.resolve(2076, 1560, 1038))
assertEquals(FormSheetPlacement(1560, 220), FormSheetPlacement.resolve(4000, 1560, 2000))
// Wide inner displays, such as the Galaxy Z Fold 8's, report book posture as landscape.
assertEquals(FormSheetPlacement(1224, 0), FormSheetPlacement.resolve(2448, 1560, 1224))
}

@Test fun invalidHingesKeepTheFullWindow() {
for (hinge in listOf(0, 2076, 3000)) {
assertEquals(FormSheetPlacement(1560, 258), FormSheetPlacement.resolve(2076, 1560, hinge))
}
}

@Test fun placementRestoresWhenTheHingeReleasesOrWindowFolds() {
val frames =
listOf(
FormSheetPlacement.resolve(2076, 1560, null),
FormSheetPlacement.resolve(2076, 1560, 1038),
FormSheetPlacement.resolve(1080, 1560, null),
FormSheetPlacement.resolve(2076, 1560, null),
)
assertEquals(listOf(258, 0, 0, 258), frames.map { it.left })
assertEquals(listOf(1560, 1038, 1080, 1560), frames.map { it.width })
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-screens",
"version": "5.0.0-t3.45",
"version": "5.0.0-t3.50",
"description": "Native navigation primitives for your React Native app.",
"scripts": {
"submodules": "git submodule update --init --recursive && (cd react-navigation && yarn && yarn build && cd ../)",
Expand Down
2 changes: 1 addition & 1 deletion scripts/pack-t3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (run('git', ['status', '--porcelain']).trim()) {
}
run('git', ['merge-base', '--is-ancestor', pkg.t3Fork.upstreamCommit, 'HEAD']);
const provenance = {
repository: 'https://github.com/juliusmarminge/react-native-screens',
repository: 'https://github.com/pingdotgg/react-native-screens',
commit: run('git', ['rev-parse', 'HEAD']).trim(),
upstreamCommit: pkg.t3Fork.upstreamCommit,
version: pkg.version,
Expand Down