forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Android version of SafeAreaView (facebook#46246)
Summary: Pull Request resolved: facebook#46246 Add Android implementation for 'SafeAreaView' (native implementation for [RCTSafeAreaViewNativeComponent](https://www.internalfb.com/code/fbsource/[cace8a68d2323612b199982f6784b32c9bd0ef21]/xplat/js/react-native-github/packages/react-native/src/private/specs/components/RCTSafeAreaViewNativeComponent.js)) which is intended to be used in RN core and RN Tester. This is needed for forced edge-to-edge w/ targetSdk 35 in Android 15. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D61673059
- Loading branch information
1 parent
f00e8ba
commit 14134b7
Showing
12 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ive/ReactAndroid/src/main/java/com/facebook/react/views/safeareaview/ReactSafeAreaView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.views.safeareaview | ||
|
||
import android.content.Context | ||
import android.view.ViewGroup | ||
import androidx.annotation.UiThread | ||
import androidx.core.graphics.Insets | ||
import androidx.core.view.ViewCompat | ||
import androidx.core.view.WindowInsetsCompat | ||
import androidx.core.view.WindowInsetsCompat.CONSUMED | ||
import com.facebook.react.bridge.WritableNativeMap | ||
import com.facebook.react.uimanager.PixelUtil.pxToDp | ||
import com.facebook.react.uimanager.StateWrapper | ||
|
||
public class ReactSafeAreaView(context: Context) : ViewGroup(context) { | ||
internal var stateWrapper: StateWrapper? = null | ||
|
||
override fun onAttachedToWindow() { | ||
super.onAttachedToWindow() | ||
|
||
ViewCompat.setOnApplyWindowInsetsListener(this) { _, windowInsets -> | ||
val insets = | ||
windowInsets.getInsets( | ||
WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()) | ||
updateState(insets) | ||
CONSUMED | ||
} | ||
requestApplyInsets() | ||
} | ||
|
||
override fun onLayout(p0: Boolean, p1: Int, p2: Int, p3: Int, p4: Int): Unit = Unit | ||
|
||
@UiThread | ||
private fun updateState(insets: Insets) { | ||
stateWrapper?.let { stateWrapper -> | ||
WritableNativeMap().apply { | ||
putDouble("left", insets.left.toFloat().pxToDp().toDouble()) | ||
putDouble("top", insets.top.toFloat().pxToDp().toDouble()) | ||
putDouble("bottom", insets.bottom.toFloat().pxToDp().toDouble()) | ||
putDouble("right", insets.right.toFloat().pxToDp().toDouble()) | ||
|
||
stateWrapper.updateState(this) | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ctAndroid/src/main/java/com/facebook/react/views/safeareaview/ReactSafeAreaViewManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.views.safeareaview | ||
|
||
import com.facebook.react.module.annotations.ReactModule | ||
import com.facebook.react.uimanager.LayoutShadowNode | ||
import com.facebook.react.uimanager.ReactStylesDiffMap | ||
import com.facebook.react.uimanager.StateWrapper | ||
import com.facebook.react.uimanager.ThemedReactContext | ||
import com.facebook.react.uimanager.ViewGroupManager | ||
import com.facebook.react.uimanager.ViewManagerDelegate | ||
import com.facebook.react.viewmanagers.SafeAreaViewManagerDelegate | ||
import com.facebook.react.viewmanagers.SafeAreaViewManagerInterface | ||
|
||
/** View manager for [ReactSafeAreaView] components. */ | ||
@ReactModule(name = ReactSafeAreaViewManager.REACT_CLASS) | ||
public class ReactSafeAreaViewManager() : | ||
ViewGroupManager<ReactSafeAreaView>(), SafeAreaViewManagerInterface<ReactSafeAreaView> { | ||
private var shadowNode: ReactSafeAreaViewShadowNode? = null | ||
|
||
private val delegate: ViewManagerDelegate<ReactSafeAreaView> = SafeAreaViewManagerDelegate(this) | ||
|
||
override fun getDelegate(): ViewManagerDelegate<ReactSafeAreaView> = delegate | ||
|
||
override fun createViewInstance(context: ThemedReactContext): ReactSafeAreaView = | ||
ReactSafeAreaView(context).also { shadowNode?.setInsetPadding() } | ||
|
||
override fun getName(): String = REACT_CLASS | ||
|
||
override fun createShadowNodeInstance(): LayoutShadowNode = | ||
ReactSafeAreaViewShadowNode().also { shadowNode = it } | ||
|
||
public override fun getShadowNodeClass(): Class<out LayoutShadowNode> = | ||
ReactSafeAreaViewShadowNode::class.java | ||
|
||
public override fun updateState( | ||
view: ReactSafeAreaView, | ||
props: ReactStylesDiffMap, | ||
stateWrapper: StateWrapper | ||
): Any? { | ||
view.stateWrapper = stateWrapper | ||
return null | ||
} | ||
|
||
public companion object { | ||
public const val REACT_CLASS: String = "RCTSafeAreaView" | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ndroid/src/main/java/com/facebook/react/views/safeareaview/ReactSafeAreaViewShadowNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.views.safeareaview | ||
|
||
import androidx.core.view.ViewCompat | ||
import androidx.core.view.WindowInsetsCompat | ||
import com.facebook.react.uimanager.LayoutShadowNode | ||
import com.facebook.react.uimanager.Spacing | ||
|
||
internal class ReactSafeAreaViewShadowNode() : LayoutShadowNode() { | ||
|
||
fun setInsetPadding() { | ||
themedContext.currentActivity?.window?.decorView?.let { decorView -> | ||
ViewCompat.setOnApplyWindowInsetsListener(decorView) { _, windowInsets -> | ||
val insetsType = | ||
WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout() | ||
val insets = windowInsets.getInsets(insetsType) | ||
|
||
setDefaultPadding(Spacing.START, insets.left.toFloat()) | ||
setDefaultPadding(Spacing.TOP, insets.top.toFloat()) | ||
setDefaultPadding(Spacing.END, insets.right.toFloat()) | ||
setDefaultPadding(Spacing.BOTTOM, insets.bottom.toFloat()) | ||
|
||
WindowInsetsCompat.CONSUMED | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/react-native/ReactCommon/react/renderer/components/safeareaview/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
cmake_minimum_required(VERSION 3.13) | ||
set(CMAKE_VERBOSE_MAKEFILE on) | ||
|
||
file(GLOB rrc_safeareaview_SRCS CONFIGURE_DEPENDS *.cpp) | ||
|
||
add_library( | ||
rrc_safeareaview | ||
STATIC | ||
${rrc_safeareaview_SRCS} | ||
) | ||
|
||
target_include_directories(rrc_safeareaview PUBLIC ${REACT_COMMON_DIR}) | ||
|
||
target_link_libraries( | ||
rrc_safeareaview | ||
glog | ||
fbjni | ||
folly_runtime | ||
glog_init | ||
react_codegen_rncore | ||
react_debug | ||
react_render_componentregistry | ||
react_render_core | ||
react_render_debug | ||
react_render_graphics | ||
react_render_uimanager | ||
reactnativejni | ||
rrc_view | ||
yoga | ||
) | ||
|
||
target_compile_options( | ||
rrc_safeareaview | ||
PRIVATE | ||
-DLOG_TAG=\"Fabric\" | ||
-fexceptions | ||
-frtti | ||
-std=c++20 | ||
-Wall | ||
-Wpedantic | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters