Skip to content
Draft
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
42 changes: 28 additions & 14 deletions android/src/main/java/com/swmansion/rnscreens/CustomToolbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.Choreographer
import android.view.WindowInsets
import android.view.WindowManager
import androidx.appcompat.widget.Toolbar
import androidx.core.graphics.Insets
import androidx.core.view.WindowInsetsCompat
import com.facebook.react.modules.core.ReactChoreographer
import com.facebook.react.uimanager.ThemedReactContext
Expand Down Expand Up @@ -81,9 +82,8 @@ open class CustomToolbar(
}
}

override fun onApplyWindowInsets(insets: WindowInsets?): WindowInsets? {
val unhandledInsets = super.onApplyWindowInsets(insets)

fun applyWindowInsets(insets: WindowInsets?): WindowInsets? {
println("CustomToolbar this=$this")
// There are few UI modes we could be running in
//
// 1. legacy non edge-to-edge mode,
Expand All @@ -95,17 +95,10 @@ open class CustomToolbar(
// We use rootWindowInsets in lieu of insets or unhandledInsets here,
// because cutout sometimes (only in certain scenarios, e.g. with headerLeft view present)
// happen to be Insets.ZERO and is not reliable.
val rootWindowInsets = rootWindowInsets
val cutoutInsets =
resolveInsetsOrZero(WindowInsetsCompat.Type.displayCutout(), rootWindowInsets)
resolveInsetsOrZero(WindowInsetsCompat.Type.displayCutout(), insets)
val systemBarInsets =
resolveInsetsOrZero(WindowInsetsCompat.Type.systemBars(), rootWindowInsets)
val statusBarInsetsStable =
resolveInsetsOrZero(
WindowInsetsCompat.Type.systemBars(),
rootWindowInsets,
ignoreVisibility = true,
)
resolveInsetsOrZero(WindowInsetsCompat.Type.systemBars(), insets)

// This seems to work fine in all tested configurations, because cutout & system bars overlap
// only in portrait mode & top inset is controlled separately, therefore we don't count
Expand All @@ -124,7 +117,7 @@ open class CustomToolbar(
val verticalInsets =
InsetsCompat.of(
0,
max(cutoutInsets.top, if (shouldApplyTopInset) statusBarInsetsStable.top else 0),
max(cutoutInsets.top, if (shouldApplyTopInset) systemBarInsets.top else 0),
0,
max(cutoutInsets.bottom, 0),
)
Expand All @@ -141,7 +134,28 @@ open class CustomToolbar(
)
}

return unhandledInsets
return insets?.let {
WindowInsetsCompat
.Builder(WindowInsetsCompat.toWindowInsetsCompat(insets))
.setInsets(
WindowInsetsCompat.Type.systemBars(),
Insets.of(
systemBarInsets.left - lastInsets.left,
systemBarInsets.top - lastInsets.top,
systemBarInsets.right - lastInsets.right,
systemBarInsets.bottom - lastInsets.bottom,
),
).setInsets(
WindowInsetsCompat.Type.displayCutout(),
Insets.of(
cutoutInsets.left - lastInsets.left,
cutoutInsets.top - lastInsets.top,
cutoutInsets.right - lastInsets.right,
cutoutInsets.bottom - lastInsets.bottom,
),
).build()
.toWindowInsets()
}
}

override fun onLayout(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsets
import android.view.animation.Animation
import android.widget.LinearLayout
import androidx.appcompat.widget.Toolbar
Expand Down Expand Up @@ -126,6 +127,10 @@ class ScreenStackFragment :
}
}

fun hasToolbar() = toolbar !== null

fun applyInsetsOnToolbar(insets: WindowInsets?) = (this.toolbar as CustomToolbar?)?.applyWindowInsets(insets);

override fun onContainerUpdate() {
super.onContainerUpdate()
screen.headerConfig?.onUpdate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ class ScreenStackHeaderConfig(
this.title = title
}

fun getTitle() = title

fun setTitleFontFamily(titleFontFamily: String?) {
this.titleFontFamily = titleFontFamily
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ internal class ScreensCoordinatorLayout(
PointerEventsBoxNoneImpl(),
)

override fun onApplyWindowInsets(insets: WindowInsets?): WindowInsets = super.onApplyWindowInsets(insets)
override fun onApplyWindowInsets(insets: WindowInsets?): WindowInsets? {
println("SCL name=${fragment.screen.headerConfig?.getTitle()} inset pre = ${insets}")
if (fragment.hasToolbar()) {
val after = fragment.applyInsetsOnToolbar(insets)
println("SCL inset post = ${after}")
return after
}

println("SCL default")
return super.onApplyWindowInsets(insets)
}

private val animationListener: Animation.AnimationListener =
object : Animation.AnimationListener {
Expand Down
Loading