StatusBarProxy.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. */
  20. /* global Windows */
  21. var _supported = null; // set to null so we can check first time
  22. function isSupported() {
  23. // if not checked before, run check
  24. if (_supported === null) {
  25. var viewMan = Windows.UI.ViewManagement;
  26. _supported = (viewMan.StatusBar && viewMan.StatusBar.getForCurrentView);
  27. }
  28. return _supported;
  29. }
  30. function getViewStatusBar() {
  31. if (!isSupported()) {
  32. throw new Error("Status bar is not supported");
  33. }
  34. return Windows.UI.ViewManagement.StatusBar.getForCurrentView();
  35. }
  36. function hexToRgb(hex) {
  37. // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  38. var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  39. hex = hex.replace(shorthandRegex, function (m, r, g, b) {
  40. return r + r + g + g + b + b;
  41. });
  42. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  43. return result ? {
  44. r: parseInt(result[1], 16),
  45. g: parseInt(result[2], 16),
  46. b: parseInt(result[3], 16)
  47. } : null;
  48. }
  49. module.exports = {
  50. _ready: function(win, fail) {
  51. if(isSupported()) {
  52. var statusBar = getViewStatusBar();
  53. win(statusBar.occludedRect.height !== 0);
  54. }
  55. },
  56. overlaysWebView: function () {
  57. // not supported
  58. },
  59. styleDefault: function () {
  60. // dark text ( to be used on a light background )
  61. if (isSupported()) {
  62. getViewStatusBar().foregroundColor = { a: 0, r: 0, g: 0, b: 0 };
  63. }
  64. },
  65. styleLightContent: function () {
  66. // light text ( to be used on a dark background )
  67. if (isSupported()) {
  68. getViewStatusBar().foregroundColor = { a: 0, r: 255, g: 255, b: 255 };
  69. }
  70. },
  71. styleBlackTranslucent: function () {
  72. // #88000000 ? Apple says to use lightContent instead
  73. return module.exports.styleLightContent();
  74. },
  75. styleBlackOpaque: function () {
  76. // #FF000000 ? Apple says to use lightContent instead
  77. return module.exports.styleLightContent();
  78. },
  79. backgroundColorByHexString: function (win, fail, args) {
  80. var rgb = hexToRgb(args[0]);
  81. if(isSupported()) {
  82. var statusBar = getViewStatusBar();
  83. statusBar.backgroundColor = { a: 0, r: rgb.r, g: rgb.g, b: rgb.b };
  84. statusBar.backgroundOpacity = 1;
  85. }
  86. },
  87. show: function (win, fail) {
  88. // added support check so no error thrown, when calling this method
  89. if (isSupported()) {
  90. getViewStatusBar().showAsync().done(win, fail);
  91. }
  92. },
  93. hide: function (win, fail) {
  94. // added support check so no error thrown, when calling this method
  95. if (isSupported()) {
  96. getViewStatusBar().hideAsync().done(win, fail);
  97. }
  98. }
  99. };
  100. require("cordova/exec/proxy").add("StatusBar", module.exports);