Skip to content

Commit cd722ac

Browse files
committed
add insetType prop
1 parent 5d72eb6 commit cd722ac

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.swmansion.rnscreens.safearea
2+
3+
enum class InsetType {
4+
ALL,
5+
SYSTEM,
6+
INTERFACE,
7+
;
8+
9+
fun containsSystem(): Boolean = this == ALL || this == SYSTEM
10+
11+
fun containsInterface(): Boolean = this == ALL || this == INTERFACE
12+
}

android/src/main/java/com/swmansion/rnscreens/safearea/SafeAreaView.kt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class SafeAreaView(
3737
private var needsInsetsUpdate = false
3838
private var stateWrapper: StateWrapper? = null
3939
private var edges: SafeAreaViewEdges? = null
40+
private var insetType: InsetType = InsetType.ALL
4041

4142
fun getStateWrapper(): StateWrapper? = stateWrapper
4243

@@ -90,7 +91,10 @@ class SafeAreaView(
9091
fun onInterfaceInsetsChange(newInterfaceInsets: EdgeInsets) {
9192
if (newInterfaceInsets != currentInterfaceInsets) {
9293
currentInterfaceInsets = newInterfaceInsets
93-
needsInsetsUpdate = true
94+
95+
if (insetType.containsInterface()) {
96+
needsInsetsUpdate = true
97+
}
9498
}
9599
}
96100

@@ -104,14 +108,20 @@ class SafeAreaView(
104108

105109
if (newSystemInsets != currentSystemInsets) {
106110
currentSystemInsets = EdgeInsets.fromInsets(newSystemInsets)
107-
needsInsetsUpdate = true
111+
112+
if (insetType.containsSystem()) {
113+
needsInsetsUpdate = true
114+
}
108115
}
109116

110117
return WindowInsetsCompat
111-
.Builder(insets)
112-
.setInsets(WindowInsetsCompat.Type.systemBars(), Insets.NONE)
113-
// .setInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars(), Insets.NONE)
114-
.setInsets(WindowInsetsCompat.Type.displayCutout(), Insets.NONE)
118+
.Builder(insets).apply {
119+
if (insetType.containsSystem()) {
120+
setInsets(WindowInsetsCompat.Type.systemBars(), Insets.NONE)
121+
// setInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars(), Insets.NONE)
122+
setInsets(WindowInsetsCompat.Type.displayCutout(), Insets.NONE)
123+
}
124+
}
115125
.build()
116126
}
117127

@@ -125,7 +135,11 @@ class SafeAreaView(
125135
}
126136

127137
private fun updateInsets() {
128-
val safeAreaInsets = EdgeInsets.max(currentInterfaceInsets, currentSystemInsets)
138+
val safeAreaInsets = EdgeInsets.max(
139+
if (insetType.containsInterface()) currentInterfaceInsets else EdgeInsets.NONE,
140+
if (insetType.containsSystem()) currentSystemInsets else EdgeInsets.NONE
141+
)
142+
129143
val stateWrapper = getStateWrapper()
130144
if (stateWrapper != null) {
131145
val insets = Arguments.createMap()
@@ -200,6 +214,16 @@ class SafeAreaView(
200214
updateInsets()
201215
}
202216

217+
fun setInsetType(insetType: InsetType) {
218+
this.insetType = insetType
219+
requestApplyInsets()
220+
221+
// We don't want to call updateInsetsIfNeeded here because system insets don't arrive
222+
// immediately after requestApplyInsets. We just set the flag to true to make sure the
223+
// update is eventually executed, even if we set insetType to `INTERFACE`.
224+
needsInsetsUpdate = true
225+
}
226+
203227
override fun onPreDraw(): Boolean {
204228
val didUpdate = updateInsetsIfNeeded()
205229
if (didUpdate) {

android/src/main/java/com/swmansion/rnscreens/safearea/SafeAreaViewManager.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
33
package com.swmansion.rnscreens.safearea
44

5+
import com.facebook.react.bridge.JSApplicationIllegalArgumentException
56
import com.facebook.react.bridge.ReactApplicationContext
67
import com.facebook.react.bridge.ReadableMap
78
import com.facebook.react.module.annotations.ReactModule
@@ -43,6 +44,21 @@ class SafeAreaViewManager :
4344
}
4445
}
4546

47+
@ReactProp(name = "insetType")
48+
override fun setInsetType(
49+
view: SafeAreaView,
50+
value: String?
51+
) {
52+
val insetType = when (value) {
53+
null, "all" -> InsetType.ALL
54+
"system" -> InsetType.SYSTEM
55+
"interface" -> InsetType.INTERFACE
56+
else -> throw JSApplicationIllegalArgumentException("Unknown inset type $value")
57+
}
58+
59+
view.setInsetType(insetType)
60+
}
61+
4662
override fun updateState(
4763
view: SafeAreaView,
4864
props: ReactStylesDiffMap?,

android/src/paper/java/com/facebook/react/viewmanagers/RNSSafeAreaViewManagerDelegate.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public void setProperty(T view, String propName, @Nullable Object value) {
2626
case "edges":
2727
mViewManager.setEdges(view, (ReadableMap) value);
2828
break;
29+
case "insetType":
30+
mViewManager.setInsetType(view, (String) value);
31+
break;
2932
default:
3033
super.setProperty(view, propName, value);
3134
}

android/src/paper/java/com/facebook/react/viewmanagers/RNSSafeAreaViewManagerInterface.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717
public interface RNSSafeAreaViewManagerInterface<T extends View> {
1818
void setEdges(T view, @Nullable ReadableMap value);
19+
void setInsetType(T view, @Nullable String value);
1920
}

src/components/safe-area/SafeAreaView.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { ViewProps } from 'react-native';
44

55
export type Edge = 'top' | 'right' | 'bottom' | 'left';
66

7+
// Android-only
8+
export type InsetType = 'all' | 'system' | 'interface';
9+
710
export interface SafeAreaViewProps extends ViewProps {
811
children?: ViewProps['children'];
912
edges?: Readonly<Partial<Record<Edge, boolean>>>;
13+
// Android-only
14+
insetType?: InsetType;
1015
}

src/fabric/safe-area/SafeAreaViewNativeComponent.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// eslint-disable-next-line @react-native/no-deep-imports
55
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
66
import { ViewProps } from 'react-native';
7+
import { WithDefault } from 'react-native/Libraries/Types/CodegenTypesNamespace';
8+
9+
type InsetType = 'all' | 'system' | 'interface';
710

811
export interface NativeProps extends ViewProps {
912
edges?: Readonly<{
@@ -12,6 +15,8 @@ export interface NativeProps extends ViewProps {
1215
bottom: boolean;
1316
left: boolean;
1417
}>;
18+
// Android-only
19+
insetType?: WithDefault<InsetType, 'all'>;
1520
}
1621

1722
export default codegenNativeComponent<NativeProps>('RNSSafeAreaView', {

0 commit comments

Comments
 (0)