Skip to content

Commit

Permalink
Add new styleDarkContent (apache#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Zakharov committed Mar 20, 2020
1 parent 24d5f27 commit 0140622
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 8 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Although in the global scope, it is not available until after the `deviceready`
- StatusBar.overlaysWebView
- StatusBar.styleDefault
- StatusBar.styleLightContent
- StatusBar.styleDarkContent
- StatusBar.styleBlackTranslucent
- StatusBar.styleBlackOpaque
- StatusBar.backgroundColorByName
Expand Down Expand Up @@ -168,6 +169,7 @@ StatusBar.styleDefault
=================

Use the default statusbar (dark text, for light backgrounds).
For iOS - dark or light text depending on current device theme.

StatusBar.styleDefault();

Expand All @@ -187,6 +189,21 @@ Use the lightContent statusbar (light text, for dark backgrounds).
StatusBar.styleLightContent();


Supported Platforms
-------------------

- iOS
- Android 6+
- Windows

StatusBar.styleDarkContent
=================

Use the darkContent statusbar (dark text, for light backgrounds).

StatusBar.styleDarkContent();


Supported Platforms
-------------------

Expand Down
13 changes: 12 additions & 1 deletion src/android/StatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ public void run() {
return true;
}

if ("styleDarkContent".equals(action)) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setStatusBarStyle("darkcontent");
}
});
return true;
}

if ("styleBlackTranslucent".equals(action)) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
Expand Down Expand Up @@ -251,6 +261,7 @@ private void setStatusBarStyle(final String style) {

String[] darkContentStyles = {
"default",
"darkcontent"
};

String[] lightContentStyles = {
Expand All @@ -269,7 +280,7 @@ private void setStatusBarStyle(final String style) {
return;
}

LOG.e(TAG, "Invalid style, must be either 'default', 'lightcontent' or the deprecated 'blacktranslucent' and 'blackopaque'");
LOG.e(TAG, "Invalid style, must be either 'default', 'lightcontent', 'darkcontent' or the deprecated 'blacktranslucent' and 'blackopaque'");
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/browser/StatusBarProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
styleBlackTranslucent:notSupported,
styleDefault:notSupported,
styleLightContent:notSupported,
styleDarkContent: notSupported,
styleBlackOpaque:notSupported,
overlaysWebView:notSupported,
styleLightContect: notSupported,
Expand Down
1 change: 1 addition & 0 deletions src/ios/CDVStatusBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

- (void) styleDefault:(CDVInvokedUrlCommand*)command;
- (void) styleLightContent:(CDVInvokedUrlCommand*)command;
- (void) styleDarkContent:(CDVInvokedUrlCommand*)command;
- (void) styleBlackTranslucent:(CDVInvokedUrlCommand*)command;
- (void) styleBlackOpaque:(CDVInvokedUrlCommand*)command;

Expand Down
16 changes: 10 additions & 6 deletions src/ios/CDVStatusBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,23 @@ - (void) setStatusBarStyle:(NSString*)statusBarStyle

- (void) styleDefault:(CDVInvokedUrlCommand*)command
{
if (@available(iOS 13.0, *)) {
// TODO - Replace with UIStatusBarStyleDarkContent once Xcode 10 support is dropped
[self setStyleForStatusBar:3];
} else {
[self setStyleForStatusBar:UIStatusBarStyleDefault];
}
[self setStyleForStatusBar:UIStatusBarStyleDefault];
}

- (void) styleLightContent:(CDVInvokedUrlCommand*)command
{
[self setStyleForStatusBar:UIStatusBarStyleLightContent];
}

- (void) styleDarkContent:(CDVInvokedUrlCommand*)command
{
if (@available(iOS 13.0, *)) {
[self setStyleForStatusBar:UIStatusBarStyleDarkContent];
} else {
[self styleDefault: command];
}
}

- (void) styleBlackTranslucent:(CDVInvokedUrlCommand*)command
{
[self setStyleForStatusBar:UIStatusBarStyleLightContent];
Expand Down
7 changes: 7 additions & 0 deletions src/windows/StatusBarProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ module.exports = {
}
},

styleDarkContent: function () {
// dark text ( to be used on a light background )
if (isSupported()) {
getViewStatusBar().foregroundColor = { a: 0, r: 0, g: 0, b: 0 };
}
},

styleBlackTranslucent: function () {
// #88000000 ? Apple says to use lightContent instead
return module.exports.styleLightContent();
Expand Down
14 changes: 14 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ exports.defineAutoTests = function () {
expect(window.StatusBar.styleLightContent).toBeDefined();
expect(typeof window.StatusBar.styleLightContent).toBe("function");

expect(window.StatusBar.styleDarkContent).toBeDefined();
expect(typeof window.StatusBar.styleDarkContent).toBe("function");

expect(window.StatusBar.styleBlackOpaque).toBeDefined();
expect(typeof window.StatusBar.styleBlackOpaque).toBe("function");

Expand Down Expand Up @@ -96,6 +99,11 @@ exports.defineManualTests = function (contentEl, createActionButton) {
StatusBar.styleDefault();
}

function doColor4() {
log('set style=dark');
StatusBar.styleDarkContent();
}

var showOverlay = true;
function doOverlay() {
showOverlay = !showOverlay;
Expand All @@ -114,6 +122,8 @@ exports.defineManualTests = function (contentEl, createActionButton) {
'</p> <div id="action-color2"></div>' +
'Expected result: Status bar text will be a light (white) color' +
'</p> <div id="action-color3"></div>' +
'Expected result: Status bar text will be a dark (black) color<br>for iOS - a device theme depending (black or white) color' +
'</p> <div id="action-color4"></div>' +
'Expected result: Status bar text will be a dark (black) color' +
'</p> <div id="action-overlays"></div>' +
'Expected result:<br>Overlay true = status bar will lay on top of web view content<br>Overlay false = status bar will be separate from web view and will not cover content' +
Expand Down Expand Up @@ -145,6 +155,10 @@ exports.defineManualTests = function (contentEl, createActionButton) {
doColor3();
}, 'action-color3');

createActionButton("Style=dark", function () {
doColor4();
}, 'action-color4');

createActionButton("Toggle Overlays", function () {
doOverlay();
}, 'action-overlays');
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface StatusBar {

/**
* Use the default statusbar (dark text, for light backgrounds).
* For iOS - dark or light text depending on a device configuration.
*/
styleDefault(): void;

Expand All @@ -34,6 +35,11 @@ interface StatusBar {
*/
styleLightContent(): void;

/**
* Use the darkContent statusbar (dark text, for light backgrounds).
*/
styleDarkContent(): void;

/**
* Use the blackTranslucent statusbar (light text, for dark backgrounds).
*/
Expand Down
7 changes: 6 additions & 1 deletion www/statusbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var StatusBar = {
},

styleDefault: function () {
// dark text ( to be used on a light background )
// dark text ( to be used on a light background and on iOS depending on a device configuration )
exec(null, null, "StatusBar", "styleDefault", []);
},

Expand All @@ -58,6 +58,11 @@ var StatusBar = {
exec(null, null, "StatusBar", "styleLightContent", []);
},

styleDarkContent: function () {
// dark text ( to be used on a light background )
exec(null, null, "StatusBar", "styleDarkContent", []);
},

styleBlackTranslucent: function () {
// #88000000 ? Apple says to use lightContent instead
exec(null, null, "StatusBar", "styleBlackTranslucent", []);
Expand Down

0 comments on commit 0140622

Please sign in to comment.