Skip to content

refactor(Android): add isTranslucent to ScreenFragmentWrapper & align method naming on Screen #2818

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

Merged
merged 5 commits into from
Mar 31, 2025
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
5 changes: 4 additions & 1 deletion android/src/main/java/com/swmansion/rnscreens/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ class Screen(
)
}

fun isTransparent(): Boolean =
/**
* Whether this screen allows to see the content underneath it.
*/
fun isTranslucent(): Boolean =
when (stackPresentation) {
StackPresentation.TRANSPARENT_MODAL,
StackPresentation.FORM_SHEET,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ open class ScreenFragment :
ScreenWindowTraits.trySetWindowTraits(screen, activity, tryGetContext())
}

// Plain ScreenFragments can not be translucent
override fun isTranslucent() = false

override fun tryGetActivity(): Activity? {
activity?.let { return it }
val context = screen.context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ interface ScreenFragmentWrapper :

fun onViewAnimationEnd()

// Fragment information

/**
* Whether this screen fragment makes it possible to see content underneath it
* (not fully opaque or does not fill full screen).
*/
fun isTranslucent(): Boolean


// Helpers
fun tryGetActivity(): Activity?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class ScreenModalFragment :
savedInstanceState: Bundle?,
): View? = null

override fun isTranslucent(): Boolean = true

override fun dismissFromContainer() {
check(container is ScreenStack)
val container = container as ScreenStack
Expand Down
10 changes: 5 additions & 5 deletions android/src/main/java/com/swmansion/rnscreens/ScreenStack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class ScreenStack(
newTop = notDismissedWrappers.firstOrNull()
visibleBottom =
notDismissedWrappers
.dropWhile { it.screen.isTransparent() }
.dropWhile { it.isTranslucent() }
.firstOrNull()
?.takeUnless { it === newTop }

Expand Down Expand Up @@ -238,8 +238,8 @@ class ScreenStack(
childDrawingOrderStrategy = SwapLastTwo()
} else if (newTop != null &&
newTopAlreadyInStack &&
topScreenWrapper?.screen?.isTransparent() == true &&
newTop.screen.isTransparent() == false
topScreenWrapper?.isTranslucent() == true &&
newTop.isTranslucent() == false
) {
// In case where we dismiss multiple transparent views we want to ensure
// that they are drawn in correct order - Android swaps them by default,
Expand All @@ -250,7 +250,7 @@ class ScreenStack(
.asSequence()
.takeWhile {
it !== newTop &&
it.screen.isTransparent()
it.isTranslucent()
}.count()
if (dismissedTransparentScreenApproxCount > 1) {
childDrawingOrderStrategy =
Expand Down Expand Up @@ -311,7 +311,7 @@ class ScreenStack(
private fun turnOffA11yUnderTransparentScreen(visibleBottom: ScreenFragmentWrapper?) {
if (screenWrappers.size > 1 && visibleBottom != null) {
topScreenWrapper?.let {
if (it.screen.isTransparent()) {
if (it.isTranslucent()) {
val screenFragmentsBeneathTop = screenWrappers.slice(0 until screenWrappers.size - 1).asReversed()
// go from the top of the stack excluding the top screen
for (fragmentWrapper in screenFragmentsBeneathTop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class ScreenStackFragment :
)
}

override fun isTranslucent(): Boolean = screen.isTranslucent()

override fun removeToolbar() {
appBarLayout?.let {
toolbar?.let { toolbar ->
Expand Down Expand Up @@ -380,7 +382,7 @@ class ScreenStackFragment :
// If the screen is a transparent modal with hidden header we don't want to update the toolbar
// menu because it may erase the menu of the previous screen (which is still visible in these
// circumstances). See here: https://github.com/software-mansion/react-native-screens/issues/2271
if (!screen.isTransparent() || screen.headerConfig?.isHeaderHidden == false) {
if (!screen.isTranslucent() || screen.headerConfig?.isHeaderHidden == false) {
updateToolbarMenu(menu)
}
return super.onPrepareOptionsMenu(menu)
Expand Down
Loading