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.
Refactoring: extract helper functions from StatusBarModule (facebook#…
…46224) Summary: Pull Request resolved: facebook#46224 - Extract functions from StatusBarModule so they can be reused - WindowUtil was created as a container for the extracted Window related helper functions Changelog: [Internal] Differential Revision: D61834841 Reviewed By: tdn120
- Loading branch information
1 parent
7bc9244
commit 5116c35
Showing
3 changed files
with
72 additions
and
39 deletions.
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
63 changes: 63 additions & 0 deletions
63
packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/WindowUtil.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,63 @@ | ||
/* | ||
* 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.view | ||
|
||
import android.os.Build | ||
import android.view.Window | ||
import android.view.WindowManager | ||
import androidx.core.view.ViewCompat | ||
|
||
@Suppress("DEPRECATION") | ||
public fun Window.setStatusBarTranslucency(isTranslucent: Boolean) { | ||
// If the status bar is translucent hook into the window insets calculations | ||
// and consume all the top insets so no padding will be added under the status bar. | ||
if (isTranslucent) { | ||
decorView.setOnApplyWindowInsetsListener { v, insets -> | ||
val defaultInsets = v.onApplyWindowInsets(insets) | ||
defaultInsets.replaceSystemWindowInsets( | ||
defaultInsets.systemWindowInsetLeft, | ||
0, | ||
defaultInsets.systemWindowInsetRight, | ||
defaultInsets.systemWindowInsetBottom) | ||
} | ||
} else { | ||
decorView.setOnApplyWindowInsetsListener(null) | ||
} | ||
ViewCompat.requestApplyInsets(decorView) | ||
} | ||
|
||
public fun Window.setStatusBarVisibility(isHidden: Boolean) { | ||
if (isHidden) { | ||
this.statusBarHide() | ||
} else { | ||
this.statusBarShow() | ||
} | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
private fun Window.statusBarHide() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
// Ensure the content extends into the cutout area | ||
attributes.layoutInDisplayCutoutMode = | ||
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES | ||
setDecorFitsSystemWindows(false) | ||
} | ||
addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) | ||
clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN) | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
private fun Window.statusBarShow() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
attributes.layoutInDisplayCutoutMode = | ||
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT | ||
setDecorFitsSystemWindows(true) | ||
} | ||
addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN) | ||
clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) | ||
} |