theme-switching.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /* eslint-env browser */
  5. "use strict";
  6. (function () {
  7. const { utils: Cu } = Components;
  8. const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
  9. const Services = require("Services");
  10. const { gDevTools } = require("devtools/client/framework/devtools");
  11. const { watchCSS } = require("devtools/client/shared/css-reload");
  12. let documentElement = document.documentElement;
  13. let os;
  14. let platform = navigator.platform;
  15. if (platform.startsWith("Win")) {
  16. os = "win";
  17. } else if (platform.startsWith("Mac")) {
  18. os = "mac";
  19. } else {
  20. os = "linux";
  21. }
  22. documentElement.setAttribute("platform", os);
  23. // no-theme attributes allows to just est the platform attribute
  24. // to have per-platform CSS working correctly.
  25. if (documentElement.getAttribute("no-theme") === "true") {
  26. return;
  27. }
  28. let devtoolsStyleSheets = new WeakMap();
  29. let gOldTheme = "";
  30. function forceStyle() {
  31. let computedStyle = window.getComputedStyle(documentElement);
  32. if (!computedStyle) {
  33. // Null when documentElement is not ready. This method is anyways not
  34. // required then as scrollbars would be in their state without flushing.
  35. return;
  36. }
  37. // Save display value
  38. let display = computedStyle.display;
  39. documentElement.style.display = "none";
  40. // Flush
  41. window.getComputedStyle(documentElement).display;
  42. // Restore
  43. documentElement.style.display = display;
  44. }
  45. /*
  46. * Append a new processing instruction and return an object with
  47. * - styleSheet: DOMNode
  48. * - loadPromise: Promise that resolves once the sheets loads or errors
  49. */
  50. function appendStyleSheet(url) {
  51. let styleSheetAttr = `href="${url}" type="text/css"`;
  52. let styleSheet = document.createProcessingInstruction(
  53. "xml-stylesheet", styleSheetAttr);
  54. let loadPromise = new Promise((resolve, reject) => {
  55. function onload() {
  56. styleSheet.removeEventListener("load", onload);
  57. styleSheet.removeEventListener("error", onerror);
  58. resolve();
  59. }
  60. function onerror() {
  61. styleSheet.removeEventListener("load", onload);
  62. styleSheet.removeEventListener("error", onerror);
  63. reject("Failed to load theme file " + url);
  64. }
  65. styleSheet.addEventListener("load", onload);
  66. styleSheet.addEventListener("error", onerror);
  67. });
  68. document.insertBefore(styleSheet, documentElement);
  69. return {styleSheet, loadPromise};
  70. }
  71. /*
  72. * Notify the window that a theme switch finished so tests can check the DOM
  73. */
  74. function notifyWindow() {
  75. window.dispatchEvent(new CustomEvent("theme-switch-complete", {}));
  76. }
  77. /*
  78. * Apply all the sheets from `newTheme` and remove all of the sheets
  79. * from `oldTheme`
  80. */
  81. function switchTheme(newTheme) {
  82. if (newTheme === gOldTheme) {
  83. return;
  84. }
  85. let oldTheme = gOldTheme;
  86. gOldTheme = newTheme;
  87. let oldThemeDef = gDevTools.getThemeDefinition(oldTheme);
  88. let newThemeDef = gDevTools.getThemeDefinition(newTheme);
  89. // The theme might not be available anymore (e.g. uninstalled)
  90. // Use the default one.
  91. if (!newThemeDef) {
  92. newThemeDef = gDevTools.getThemeDefinition("light");
  93. }
  94. // Store the sheets in a WeakMap for access later when the theme gets
  95. // unapplied. It's hard to query for processing instructions so this
  96. // is an easy way to access them later without storing a property on
  97. // the window
  98. devtoolsStyleSheets.set(newThemeDef, []);
  99. let loadEvents = [];
  100. for (let url of newThemeDef.stylesheets) {
  101. let {styleSheet, loadPromise} = appendStyleSheet(url);
  102. devtoolsStyleSheets.get(newThemeDef).push(styleSheet);
  103. loadEvents.push(loadPromise);
  104. }
  105. try {
  106. const StylesheetUtils = require("sdk/stylesheet/utils");
  107. const SCROLLBARS_URL = "chrome://devtools/skin/floating-scrollbars-dark-theme.css";
  108. // TODO: extensions might want to customize scrollbar styles too.
  109. if (!Services.appShell.hiddenDOMWindow
  110. .matchMedia("(-moz-overlay-scrollbars)").matches) {
  111. if (newTheme == "dark") {
  112. StylesheetUtils.loadSheet(window, SCROLLBARS_URL, "agent");
  113. } else if (oldTheme == "dark") {
  114. StylesheetUtils.removeSheet(window, SCROLLBARS_URL, "agent");
  115. }
  116. forceStyle();
  117. }
  118. } catch (e) {
  119. console.warn("customize scrollbar styles is only supported in firefox");
  120. }
  121. Promise.all(loadEvents).then(() => {
  122. // Unload all stylesheets and classes from the old theme.
  123. if (oldThemeDef) {
  124. for (let name of oldThemeDef.classList) {
  125. documentElement.classList.remove(name);
  126. }
  127. for (let sheet of devtoolsStyleSheets.get(oldThemeDef) || []) {
  128. sheet.remove();
  129. }
  130. if (oldThemeDef.onUnapply) {
  131. oldThemeDef.onUnapply(window, newTheme);
  132. }
  133. }
  134. // Load all stylesheets and classes from the new theme.
  135. for (let name of newThemeDef.classList) {
  136. documentElement.classList.add(name);
  137. }
  138. if (newThemeDef.onApply) {
  139. newThemeDef.onApply(window, oldTheme);
  140. }
  141. // Final notification for further theme-switching related logic.
  142. gDevTools.emit("theme-switched", window, newTheme, oldTheme);
  143. notifyWindow();
  144. }, console.error.bind(console));
  145. }
  146. function handlePrefChange() {
  147. switchTheme(Services.prefs.getCharPref("devtools.theme"));
  148. }
  149. if (documentElement.hasAttribute("force-theme")) {
  150. switchTheme(documentElement.getAttribute("force-theme"));
  151. } else {
  152. switchTheme(Services.prefs.getCharPref("devtools.theme"));
  153. Services.prefs.addObserver("devtools.theme", handlePrefChange, false);
  154. window.addEventListener("unload", function () {
  155. Services.prefs.removeObserver("devtools.theme", handlePrefChange);
  156. }, { once: true });
  157. }
  158. watchCSS(window);
  159. })();