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
6f88a60
commit f18ff93
Showing
12 changed files
with
236 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) | ||
} | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...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,51 @@ | ||
/* | ||
* 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 val delegate: ViewManagerDelegate<ReactSafeAreaView> = SafeAreaViewManagerDelegate(this) | ||
|
||
override fun getDelegate(): ViewManagerDelegate<ReactSafeAreaView> = delegate | ||
|
||
override fun createViewInstance(context: ThemedReactContext): ReactSafeAreaView = | ||
ReactSafeAreaView(context) | ||
|
||
override fun getName(): String = REACT_CLASS | ||
|
||
override fun createShadowNodeInstance(): LayoutShadowNode = ReactSafeAreaViewShadowNode() | ||
|
||
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 = "SafeAreaView" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...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,12 @@ | ||
/* | ||
* 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.uimanager.LayoutShadowNode | ||
|
||
internal class ReactSafeAreaViewShadowNode : LayoutShadowNode() {} |
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