Skip to content
Open
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.webkit.WebViewCompat;
import com.getcapacitor.Logger;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
Expand All @@ -32,6 +33,7 @@ public class SystemBars extends Plugin {
static final String BAR_STATUS_BAR = "StatusBar";
static final String BAR_GESTURE_BAR = "NavigationBar";

// TODO: In Cap 9, add an additional option "full"
static final String INSETS_HANDLING_CSS = "css";
static final String INSETS_HANDLING_DISABLE = "disable";

Expand All @@ -53,7 +55,7 @@ function capacitorSystemBarsCheckMetaViewport() {
capacitorSystemBarsCheckMetaViewport();
""";

private boolean insetHandlingEnabled = true;
private String insetsHandling = INSETS_HANDLING_CSS;
private boolean hasViewportCover = false;

private String currentStatusBarStyle = STYLE_DEFAULT;
Expand Down Expand Up @@ -94,9 +96,15 @@ private void initSystemBars() {
String style = getConfig().getString("style", STYLE_DEFAULT).toUpperCase(Locale.US);
boolean hidden = getConfig().getBoolean("hidden", false);

String insetsHandling = getConfig().getString("insetsHandling", "css");
if (insetsHandling.equals(INSETS_HANDLING_DISABLE)) {
insetHandlingEnabled = false;
String configuredInsetsHandling = getConfig().getString("insetsHandling", INSETS_HANDLING_CSS);
if (INSETS_HANDLING_CSS.equals(configuredInsetsHandling) || INSETS_HANDLING_DISABLE.equals(configuredInsetsHandling)) {
insetsHandling = configuredInsetsHandling;
} else {
Logger.warn(
"SystemBars",
"Unknown insetsHandling value '" + configuredInsetsHandling + "'. Falling back to '" + INSETS_HANDLING_CSS + "'."
);
insetsHandling = INSETS_HANDLING_CSS;
}

initWindowInsetsListener();
Expand Down Expand Up @@ -146,13 +154,15 @@ public void setAnimation(final PluginCall call) {

@JavascriptInterface
public void onDOMReady() {
getActivity().runOnUiThread(() -> {
this.bridge.getWebView().evaluateJavascript(viewportMetaJSFunction, (res) -> {
hasViewportCover = res.equals("true");
if (INSETS_HANDLING_CSS.equals(insetsHandling)) {
getActivity().runOnUiThread(() -> {
this.bridge.getWebView().evaluateJavascript(viewportMetaJSFunction, (res) -> {
hasViewportCover = res.equals("true");

getBridge().getWebView().requestApplyInsets();
getBridge().getWebView().requestApplyInsets();
});
});
});
}
}

private Insets calcSafeAreaInsets(WindowInsetsCompat insets) {
Expand All @@ -164,22 +174,28 @@ private Insets calcSafeAreaInsets(WindowInsetsCompat insets) {
}

private void initSafeAreaCSSVariables() {
WindowInsetsCompat insets;
if (INSETS_HANDLING_CSS.equals(insetsHandling)) {
WindowInsetsCompat insets;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
View v = (View) this.getBridge().getWebView().getParent();
insets = ViewCompat.getRootWindowInsets(v);
} else {
insets = WindowInsetsCompat.CONSUMED;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
View v = (View) this.getBridge().getWebView().getParent();
insets = ViewCompat.getRootWindowInsets(v);
} else {
insets = WindowInsetsCompat.CONSUMED;
}

if (insets != null) {
Insets safeAreaInsets = calcSafeAreaInsets(insets);
injectSafeAreaCSS(safeAreaInsets.top, safeAreaInsets.right, safeAreaInsets.bottom, safeAreaInsets.left);
if (insets != null) {
Insets safeAreaInsets = calcSafeAreaInsets(insets);
injectSafeAreaCSS(safeAreaInsets.top, safeAreaInsets.right, safeAreaInsets.bottom, safeAreaInsets.left);
}
}
}

private void initWindowInsetsListener() {
if (INSETS_HANDLING_DISABLE.equals(insetsHandling)) {
return;
}

ViewCompat.setOnApplyWindowInsetsListener((View) getBridge().getWebView().getParent(), (v, insets) -> {
boolean shouldPassthroughInsets = getWebViewMajorVersion() >= WEBVIEW_VERSION_WITH_SAFE_AREA_FIX && hasViewportCover;

Expand Down
Loading