Skip to content

Commit d035939

Browse files
authored
feat(android): Add Statusbar.setOverlaysWebView method (#2597)
1 parent 52069b7 commit d035939

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/StatusBar.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
@NativePlugin()
1717
public class StatusBar extends Plugin {
1818

19+
private int currentStatusbarColor;
20+
21+
public void load() {
22+
// save initial color of the status bar
23+
currentStatusbarColor = getActivity().getWindow().getStatusBarColor();
24+
}
25+
1926
@PluginMethod()
2027
public void setStyle(final PluginCall call) {
2128
final String style = call.getString("style");
@@ -57,7 +64,10 @@ public void run() {
5764
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
5865
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
5966
try {
60-
window.setStatusBarColor(Color.parseColor(color.toUpperCase()));
67+
final int parsedColor = Color.parseColor(color.toUpperCase());
68+
window.setStatusBarColor(parsedColor);
69+
// update the local color field as well
70+
currentStatusbarColor = parsedColor;
6171
call.success();
6272
} catch (IllegalArgumentException ex) {
6373
call.error("Invalid color provided. Must be a hex string (ex: #ff0000");
@@ -114,6 +124,38 @@ public void getInfo(final PluginCall call) {
114124
data.put("visible", (decorView.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN) != View.SYSTEM_UI_FLAG_FULLSCREEN);
115125
data.put("style", style);
116126
data.put("color", String.format("#%06X", (0xFFFFFF & window.getStatusBarColor())));
127+
data.put("overlays", (decorView.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) == View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
117128
call.resolve(data);
118129
}
130+
131+
@PluginMethod()
132+
public void setOverlaysWebView(final PluginCall call) {
133+
final Boolean overlays = call.getBoolean("overlay", true);
134+
getBridge().executeOnMainThread(new Runnable() {
135+
@Override
136+
public void run() {
137+
if (overlays) {
138+
// Sets the layout to a fullscreen one that does not hide the actual status bar, so the webview is displayed behind it.
139+
View decorView = getActivity().getWindow().getDecorView();
140+
int uiOptions = decorView.getSystemUiVisibility();
141+
uiOptions = uiOptions | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
142+
decorView.setSystemUiVisibility(uiOptions);
143+
currentStatusbarColor = getActivity().getWindow().getStatusBarColor();
144+
getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
145+
146+
call.success();
147+
} else {
148+
// Sets the layout to a normal one that displays the webview below the status bar.
149+
View decorView = getActivity().getWindow().getDecorView();
150+
int uiOptions = decorView.getSystemUiVisibility();
151+
uiOptions = uiOptions & ~View.SYSTEM_UI_FLAG_LAYOUT_STABLE & ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
152+
decorView.setSystemUiVisibility(uiOptions);
153+
// recover the previous color of the status bar
154+
getActivity().getWindow().setStatusBarColor(currentStatusbarColor);
155+
156+
call.success();
157+
}
158+
}
159+
});
160+
}
119161
}

core/src/core-plugin-definitions.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,11 @@ export interface StatusBarPlugin extends Plugin {
16081608
* Get info about the current state of the status bar
16091609
*/
16101610
getInfo(): Promise<StatusBarInfoResult>;
1611+
/**
1612+
* Set whether or not the status bar should overlay the webview to allow usage of the space
1613+
* around a device "notch"
1614+
*/
1615+
setOverlaysWebView(options: StatusBarOverlaysWebviewOptions): Promise<void>;
16111616
}
16121617

16131618
export interface StatusBarStyleOptions {
@@ -1655,6 +1660,11 @@ export interface StatusBarInfoResult {
16551660
visible: boolean;
16561661
style: StatusBarStyle;
16571662
color?: string;
1663+
overlays?: boolean;
1664+
}
1665+
1666+
export interface StatusBarOverlaysWebviewOptions {
1667+
overlay: boolean;
16581668
}
16591669

16601670
export interface StoragePlugin extends Plugin {

ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
CAP_PLUGIN_METHOD(show, CAPPluginReturnPromise);
140140
CAP_PLUGIN_METHOD(hide, CAPPluginReturnPromise);
141141
CAP_PLUGIN_METHOD(getInfo, CAPPluginReturnPromise);
142+
CAP_PLUGIN_METHOD(setOverlaysWebView, CAPPluginReturnPromise);
142143
)
143144

144145
CAP_PLUGIN(CAPStoragePlugin, "Storage",

ios/Capacitor/Capacitor/Plugins/StatusBar.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,9 @@ public class CAPStatusBarPlugin: CAPPlugin {
8888
])
8989
}
9090
}
91+
92+
@objc func setOverlaysWebView(_ call: CAPPluginCall) {
93+
call.unimplemented()
94+
}
9195
}
9296

site/docs-md/apis/status-bar/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ export class StatusBarExample {
4949
style: this.isStatusBarLight ? StatusBarStyle.Dark : StatusBarStyle.Light
5050
});
5151
this.isStatusBarLight = !this.isStatusBarLight;
52+
53+
// Display content under transparent status bar (Android only)
54+
Statusbar.setOverlaysWebView({
55+
overlay: true
56+
});
5257
}
5358

5459
hideStatusBar() {

0 commit comments

Comments
 (0)