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

feat(Android): added navigationBarTranslucent option #2152

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 15 additions & 1 deletion android/src/main/java/com/swmansion/rnscreens/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ class Screen(context: ReactContext?) : FabricEnabledViewGroup(context) {
fragmentWrapper?.let { ScreenWindowTraits.setNavigationBarColor(this, it.tryGetActivity()) }
}

var isNavigationBarTranslucent: Boolean? = null
set(navigationBarTranslucent) {
if (navigationBarTranslucent != null) {
ScreenWindowTraits.applyDidSetNavigationBarAppearance()
}
field = navigationBarTranslucent
fragmentWrapper?.let {
ScreenWindowTraits.setNavigationBarTranslucent(
this,
it.tryGetActivity(),
)
}
}

var isNavigationBarHidden: Boolean? = null
set(navigationBarHidden) {
if (navigationBarHidden != null) {
Expand Down Expand Up @@ -276,6 +290,6 @@ class Screen(context: ReactContext?) : FabricEnabledViewGroup(context) {
}

enum class WindowTraits {
ORIENTATION, COLOR, STYLE, TRANSLUCENT, HIDDEN, ANIMATED, NAVIGATION_BAR_COLOR, NAVIGATION_BAR_HIDDEN
ORIENTATION, COLOR, STYLE, TRANSLUCENT, HIDDEN, ANIMATED, NAVIGATION_BAR_COLOR, NAVIGATION_BAR_TRANSLUCENT, NAVIGATION_BAR_HIDDEN
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ open class ScreenViewManager : ViewGroupManager<Screen>(), RNSScreenManagerInter
view.navigationBarColor = navigationBarColor
}

@ReactProp(name = "navigationBarTranslucent")
override fun setNavigationBarTranslucent(view: Screen, navigationBarTranslucent: Boolean) {
view.isNavigationBarTranslucent = navigationBarTranslucent
}

@ReactProp(name = "navigationBarHidden")
override fun setNavigationBarHidden(view: Screen, navigationBarHidden: Boolean) {
view.isNavigationBarHidden = navigationBarHidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import android.app.Activity
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Build
import android.util.Log
alduzy marked this conversation as resolved.
Show resolved Hide resolved
import android.view.ViewParent
import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.facebook.react.bridge.GuardedRunnable
Expand Down Expand Up @@ -185,6 +187,20 @@ object ScreenWindowTraits {
window.navigationBarColor = color
}

internal fun setNavigationBarTranslucent(screen: Screen, activity: Activity?) {
if (activity == null) {
return
}

val window = activity.window

val screenForNavBarTranslucent = findScreenForTrait(screen, WindowTraits.NAVIGATION_BAR_TRANSLUCENT)
val translucent = screenForNavBarTranslucent?.isNavigationBarTranslucent ?: false

// Following method controls whether to display edge-to-edge content that draws behind the navigation bar
WindowCompat.setDecorFitsSystemWindows(window, !translucent)
alduzy marked this conversation as resolved.
Show resolved Hide resolved
}

internal fun setNavigationBarHidden(screen: Screen, activity: Activity?) {
if (activity == null) {
return
Expand Down Expand Up @@ -221,6 +237,7 @@ object ScreenWindowTraits {
}
if (didSetNavigationBarAppearance) {
setNavigationBarColor(screen, activity)
setNavigationBarTranslucent(screen, activity)
setNavigationBarHidden(screen, activity)
}
}
Expand Down Expand Up @@ -281,6 +298,7 @@ object ScreenWindowTraits {
WindowTraits.HIDDEN -> screen.isStatusBarHidden != null
WindowTraits.ANIMATED -> screen.isStatusBarAnimated != null
WindowTraits.NAVIGATION_BAR_COLOR -> screen.navigationBarColor != null
WindowTraits.NAVIGATION_BAR_TRANSLUCENT -> screen.isNavigationBarTranslucent != null
WindowTraits.NAVIGATION_BAR_HIDDEN -> screen.isNavigationBarHidden != null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public void setProperty(T view, String propName, @Nullable Object value) {
case "navigationBarColor":
mViewManager.setNavigationBarColor(view, ColorPropConverter.getColor(value, view.getContext()));
break;
case "navigationBarTranslucent":
mViewManager.setNavigationBarTranslucent(view, value == null ? false : (boolean) value);
break;
case "navigationBarHidden":
mViewManager.setNavigationBarHidden(view, value == null ? false : (boolean) value);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public interface RNSScreenManagerInterface<T extends View> {
void setHideKeyboardOnSwipe(T view, boolean value);
void setActivityState(T view, float value);
void setNavigationBarColor(T view, @Nullable Integer value);
void setNavigationBarTranslucent(T view, boolean value);
void setNavigationBarHidden(T view, boolean value);
void setNativeBackButtonDismissalEnabled(T view, boolean value);
}
1 change: 1 addition & 0 deletions src/fabric/ModalScreenNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface NativeProps extends ViewProps {
hideKeyboardOnSwipe?: boolean;
activityState?: WithDefault<Float, -1.0>;
navigationBarColor?: ColorValue;
navigationBarTranslucent?: boolean;
navigationBarHidden?: boolean;
nativeBackButtonDismissalEnabled?: boolean;
}
Expand Down
1 change: 1 addition & 0 deletions src/fabric/ScreenNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface NativeProps extends ViewProps {
hideKeyboardOnSwipe?: boolean;
activityState?: WithDefault<Float, -1.0>;
navigationBarColor?: ColorValue;
navigationBarTranslucent?: boolean;
navigationBarHidden?: boolean;
nativeBackButtonDismissalEnabled?: boolean;
}
Expand Down
6 changes: 6 additions & 0 deletions src/native-stack/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ export type NativeStackNavigationOptions = {
* @platform android
*/
navigationBarColor?: ColorValue;
/**
* Boolean indicating whether the content should be visible behind the navigation bar. Defaults to `false`.
*
* @platform android
*/
navigationBarTranslucent?: boolean;
/**
* Sets the visibility of the navigation bar. Defaults to `false`.
*
Expand Down
2 changes: 2 additions & 0 deletions src/native-stack/views/NativeStackView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ const RouteView = ({
sheetExpandsWhenScrolledToEdge = true,
nativeBackButtonDismissalEnabled = false,
navigationBarColor,
navigationBarTranslucent,
navigationBarHidden,
replaceAnimation = 'pop',
screenOrientation,
Expand Down Expand Up @@ -299,6 +300,7 @@ const RouteView = ({
gestureResponseDistance={gestureResponseDistance}
nativeBackButtonDismissalEnabled={nativeBackButtonDismissalEnabled}
navigationBarColor={navigationBarColor}
navigationBarTranslucent={navigationBarTranslucent}
navigationBarHidden={navigationBarHidden}
replaceAnimation={replaceAnimation}
screenOrientation={screenOrientation}
Expand Down
6 changes: 6 additions & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ export interface ScreenProps extends ViewProps {
* @platform android
*/
navigationBarColor?: ColorValue;
/**
* Boolean indicating whether the content should be visible behind the navigation bar. Defaults to `false`.
*
* @platform android
*/
navigationBarTranslucent?: boolean;
/**
* Sets the visibility of the navigation bar. Defaults to `false`.
*
Expand Down
Loading