Bug 1292592 - isolate xul dependency in sourceeditor and theme-switching;r=bgrins draft
authorFred Lin <gasolin@mozilla.com>
Tue, 20 Sep 2016 14:20:46 +0800
changeset 420487 32d3ce69e604298d4fc9ae560af66252e013b453
parent 420486 5b26d0506935a01b662dc68d9691ae9ad2897149
child 420488 c09f121df4198266e10edc3b979dc2ea66237673
push id31211
push userbmo:gasolin@mozilla.com
push dateTue, 04 Oct 2016 07:17:08 +0000
reviewersbgrins
bugs1292592
milestone52.0a1
Bug 1292592 - isolate xul dependency in sourceeditor and theme-switching;r=bgrins MozReview-Commit-ID: 95ylSC8MpwM
devtools/client/shared/theme-switching.js
devtools/client/sourceeditor/editor.js
--- a/devtools/client/shared/theme-switching.js
+++ b/devtools/client/shared/theme-switching.js
@@ -1,16 +1,20 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* eslint-env browser */
 "use strict";
 (function () {
-  const SCROLLBARS_URL = "chrome://devtools/skin/floating-scrollbars-dark-theme.css";
+  const { utils: Cu } = Components;
+  const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
+  const Services = require("Services");
+  const { gDevTools } = require("devtools/client/framework/devtools");
+  const { watchCSS } = require("devtools/client/shared/css-reload");
   let documentElement = document.documentElement;
 
   let os;
   let platform = navigator.platform;
   if (platform.startsWith("Win")) {
     os = "win";
   } else if (platform.startsWith("Mac")) {
     os = "mac";
@@ -108,29 +112,32 @@
 
     let loadEvents = [];
     for (let url of newThemeDef.stylesheets) {
       let {styleSheet, loadPromise} = appendStyleSheet(url);
       devtoolsStyleSheets.get(newThemeDef).push(styleSheet);
       loadEvents.push(loadPromise);
     }
 
-    // Floating scroll-bars like in OSX
-    let hiddenDOMWindow = Cc["@mozilla.org/appshell/appShellService;1"]
-                 .getService(Ci.nsIAppShellService)
-                 .hiddenDOMWindow;
+    try {
+      const StylesheetUtils = require("sdk/stylesheet/utils");
+      const SCROLLBARS_URL = "chrome://devtools/skin/floating-scrollbars-dark-theme.css";
 
-    // TODO: extensions might want to customize scrollbar styles too.
-    if (!hiddenDOMWindow.matchMedia("(-moz-overlay-scrollbars)").matches) {
-      if (newTheme == "dark") {
-        StylesheetUtils.loadSheet(window, SCROLLBARS_URL, "agent");
-      } else if (oldTheme == "dark") {
-        StylesheetUtils.removeSheet(window, SCROLLBARS_URL, "agent");
+      // TODO: extensions might want to customize scrollbar styles too.
+      if (!Services.appShell.hiddenDOMWindow
+        .matchMedia("(-moz-overlay-scrollbars)").matches) {
+        if (newTheme == "dark") {
+          StylesheetUtils.loadSheet(window, SCROLLBARS_URL, "agent");
+        } else if (oldTheme == "dark") {
+          StylesheetUtils.removeSheet(window, SCROLLBARS_URL, "agent");
+        }
+        forceStyle();
       }
-      forceStyle();
+    } catch (e) {
+      console.warn("customize scrollbar styles is only supported in firefox");
     }
 
     Promise.all(loadEvents).then(() => {
       // Unload all stylesheets and classes from the old theme.
       if (oldThemeDef) {
         for (let name of oldThemeDef.classList) {
           documentElement.classList.remove(name);
         }
@@ -158,23 +165,16 @@
       notifyWindow();
     }, console.error.bind(console));
   }
 
   function handlePrefChange() {
     switchTheme(Services.prefs.getCharPref("devtools.theme"));
   }
 
-  const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
-  const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
-  const Services = require("Services");
-  const { gDevTools } = require("devtools/client/framework/devtools");
-  const StylesheetUtils = require("sdk/stylesheet/utils");
-  const { watchCSS } = require("devtools/client/shared/css-reload");
-
   if (documentElement.hasAttribute("force-theme")) {
     switchTheme(documentElement.getAttribute("force-theme"));
   } else {
     switchTheme(Services.prefs.getCharPref("devtools.theme"));
 
     Services.prefs.addObserver("devtools.theme", handlePrefChange, false);
     window.addEventListener("unload", function () {
       Services.prefs.removeObserver("devtools.theme", handlePrefChange);
--- a/devtools/client/sourceeditor/editor.js
+++ b/devtools/client/sourceeditor/editor.js
@@ -463,17 +463,21 @@ Editor.prototype = {
 
       this.emit("gutterClick", line, ev.button);
     });
 
     win.CodeMirror.defineExtension("l10n", (name) => {
       return L10N.getStr(name);
     });
 
-    cm.getInputField().controllers.insertControllerAt(0, controller(this));
+    try {
+      cm.getInputField().controllers.insertControllerAt(0, controller(this));
+    } catch (e) {
+      console.warn("controller command is only supported in XUL");
+    }
 
     editors.set(this, cm);
 
     this.reloadPreferences = this.reloadPreferences.bind(this);
     this._prefObserver = new PrefObserver("devtools.editor.");
     this._prefObserver.on(TAB_SIZE, this.reloadPreferences);
     this._prefObserver.on(EXPAND_TAB, this.reloadPreferences);
     this._prefObserver.on(KEYMAP, this.reloadPreferences);