toolbox-hosts.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. const EventEmitter = require("devtools/shared/event-emitter");
  7. const promise = require("promise");
  8. const defer = require("devtools/shared/defer");
  9. const Services = require("Services");
  10. const {DOMHelpers} = require("resource://devtools/client/shared/DOMHelpers.jsm");
  11. loader.lazyRequireGetter(this, "system", "devtools/shared/system");
  12. /* A host should always allow this much space for the page to be displayed.
  13. * There is also a min-height on the browser, but we still don't want to set
  14. * frame.height to be larger than that, since it can cause problems with
  15. * resizing the toolbox and panel layout. */
  16. const MIN_PAGE_SIZE = 25;
  17. /**
  18. * A toolbox host represents an object that contains a toolbox (e.g. the
  19. * sidebar or a separate window). Any host object should implement the
  20. * following functions:
  21. *
  22. * create() - create the UI and emit a 'ready' event when the UI is ready to use
  23. * destroy() - destroy the host's UI
  24. */
  25. exports.Hosts = {
  26. "bottom": BottomHost,
  27. "side": SidebarHost,
  28. "window": WindowHost,
  29. "custom": CustomHost
  30. };
  31. /**
  32. * Host object for the dock on the bottom of the browser
  33. */
  34. function BottomHost(hostTab) {
  35. this.hostTab = hostTab;
  36. EventEmitter.decorate(this);
  37. }
  38. BottomHost.prototype = {
  39. type: "bottom",
  40. heightPref: "devtools.toolbox.footer.height",
  41. /**
  42. * Create a box at the bottom of the host tab.
  43. */
  44. create: function () {
  45. let deferred = defer();
  46. let gBrowser = this.hostTab.ownerDocument.defaultView.gBrowser;
  47. let ownerDocument = gBrowser.ownerDocument;
  48. this._nbox = gBrowser.getNotificationBox(this.hostTab.linkedBrowser);
  49. this._splitter = ownerDocument.createElement("splitter");
  50. this._splitter.setAttribute("class", "devtools-horizontal-splitter");
  51. // Avoid resizing notification containers
  52. this._splitter.setAttribute("resizebefore", "flex");
  53. this.frame = ownerDocument.createElement("iframe");
  54. this.frame.className = "devtools-toolbox-bottom-iframe";
  55. this.frame.height = Math.min(
  56. Services.prefs.getIntPref(this.heightPref),
  57. this._nbox.clientHeight - MIN_PAGE_SIZE
  58. );
  59. this._nbox.appendChild(this._splitter);
  60. this._nbox.appendChild(this.frame);
  61. let frameLoad = () => {
  62. this.emit("ready", this.frame);
  63. deferred.resolve(this.frame);
  64. };
  65. this.frame.tooltip = "aHTMLTooltip";
  66. // we have to load something so we can switch documents if we have to
  67. this.frame.setAttribute("src", "about:blank");
  68. let domHelper = new DOMHelpers(this.frame.contentWindow);
  69. domHelper.onceDOMReady(frameLoad);
  70. focusTab(this.hostTab);
  71. return deferred.promise;
  72. },
  73. /**
  74. * Raise the host.
  75. */
  76. raise: function () {
  77. focusTab(this.hostTab);
  78. },
  79. /**
  80. * Minimize this host so that only the toolbox tabbar remains visible.
  81. * @param {Number} height The height to minimize to. Defaults to 0, which
  82. * means that the toolbox won't be visible at all once minimized.
  83. */
  84. minimize: function (height = 0) {
  85. if (this.isMinimized) {
  86. return;
  87. }
  88. this.isMinimized = true;
  89. let onTransitionEnd = event => {
  90. if (event.propertyName !== "margin-bottom") {
  91. // Ignore transitionend on unrelated properties.
  92. return;
  93. }
  94. this.frame.removeEventListener("transitionend", onTransitionEnd);
  95. this.emit("minimized");
  96. };
  97. this.frame.addEventListener("transitionend", onTransitionEnd);
  98. this.frame.style.marginBottom = -this.frame.height + height + "px";
  99. this._splitter.classList.add("disabled");
  100. },
  101. /**
  102. * If the host was minimized before, maximize it again (the host will be
  103. * maximized to the height it previously had).
  104. */
  105. maximize: function () {
  106. if (!this.isMinimized) {
  107. return;
  108. }
  109. this.isMinimized = false;
  110. let onTransitionEnd = event => {
  111. if (event.propertyName !== "margin-bottom") {
  112. // Ignore transitionend on unrelated properties.
  113. return;
  114. }
  115. this.frame.removeEventListener("transitionend", onTransitionEnd);
  116. this.emit("maximized");
  117. };
  118. this.frame.addEventListener("transitionend", onTransitionEnd);
  119. this.frame.style.marginBottom = "0";
  120. this._splitter.classList.remove("disabled");
  121. },
  122. /**
  123. * Toggle the minimize mode.
  124. * @param {Number} minHeight The height to minimize to.
  125. */
  126. toggleMinimizeMode: function (minHeight) {
  127. this.isMinimized ? this.maximize() : this.minimize(minHeight);
  128. },
  129. /**
  130. * Set the toolbox title.
  131. * Nothing to do for this host type.
  132. */
  133. setTitle: function () {},
  134. /**
  135. * Destroy the bottom dock.
  136. */
  137. destroy: function () {
  138. if (!this._destroyed) {
  139. this._destroyed = true;
  140. Services.prefs.setIntPref(this.heightPref, this.frame.height);
  141. this._nbox.removeChild(this._splitter);
  142. this._nbox.removeChild(this.frame);
  143. this.frame = null;
  144. this._nbox = null;
  145. this._splitter = null;
  146. }
  147. return promise.resolve(null);
  148. }
  149. };
  150. /**
  151. * Host object for the in-browser sidebar
  152. */
  153. function SidebarHost(hostTab) {
  154. this.hostTab = hostTab;
  155. EventEmitter.decorate(this);
  156. }
  157. SidebarHost.prototype = {
  158. type: "side",
  159. widthPref: "devtools.toolbox.sidebar.width",
  160. /**
  161. * Create a box in the sidebar of the host tab.
  162. */
  163. create: function () {
  164. let deferred = defer();
  165. let gBrowser = this.hostTab.ownerDocument.defaultView.gBrowser;
  166. let ownerDocument = gBrowser.ownerDocument;
  167. this._sidebar = gBrowser.getSidebarContainer(this.hostTab.linkedBrowser);
  168. this._splitter = ownerDocument.createElement("splitter");
  169. this._splitter.setAttribute("class", "devtools-side-splitter");
  170. this.frame = ownerDocument.createElement("iframe");
  171. this.frame.className = "devtools-toolbox-side-iframe";
  172. this.frame.width = Math.min(
  173. Services.prefs.getIntPref(this.widthPref),
  174. this._sidebar.clientWidth - MIN_PAGE_SIZE
  175. );
  176. this._sidebar.appendChild(this._splitter);
  177. this._sidebar.appendChild(this.frame);
  178. let frameLoad = () => {
  179. this.emit("ready", this.frame);
  180. deferred.resolve(this.frame);
  181. };
  182. this.frame.tooltip = "aHTMLTooltip";
  183. this.frame.setAttribute("src", "about:blank");
  184. let domHelper = new DOMHelpers(this.frame.contentWindow);
  185. domHelper.onceDOMReady(frameLoad);
  186. focusTab(this.hostTab);
  187. return deferred.promise;
  188. },
  189. /**
  190. * Raise the host.
  191. */
  192. raise: function () {
  193. focusTab(this.hostTab);
  194. },
  195. /**
  196. * Set the toolbox title.
  197. * Nothing to do for this host type.
  198. */
  199. setTitle: function () {},
  200. /**
  201. * Destroy the sidebar.
  202. */
  203. destroy: function () {
  204. if (!this._destroyed) {
  205. this._destroyed = true;
  206. Services.prefs.setIntPref(this.widthPref, this.frame.width);
  207. this._sidebar.removeChild(this._splitter);
  208. this._sidebar.removeChild(this.frame);
  209. }
  210. return promise.resolve(null);
  211. }
  212. };
  213. /**
  214. * Host object for the toolbox in a separate window
  215. */
  216. function WindowHost() {
  217. this._boundUnload = this._boundUnload.bind(this);
  218. EventEmitter.decorate(this);
  219. }
  220. WindowHost.prototype = {
  221. type: "window",
  222. WINDOW_URL: "chrome://devtools/content/framework/toolbox-window.xul",
  223. /**
  224. * Create a new xul window to contain the toolbox.
  225. */
  226. create: function () {
  227. let deferred = defer();
  228. let flags = "chrome,centerscreen,resizable,dialog=no";
  229. let win = Services.ww.openWindow(null, this.WINDOW_URL, "_blank",
  230. flags, null);
  231. let frameLoad = () => {
  232. win.removeEventListener("load", frameLoad, true);
  233. win.focus();
  234. let key;
  235. if (system.constants.platform === "macosx") {
  236. key = win.document.getElementById("toolbox-key-toggle-osx");
  237. } else {
  238. key = win.document.getElementById("toolbox-key-toggle");
  239. }
  240. key.removeAttribute("disabled");
  241. this.frame = win.document.getElementById("toolbox-iframe");
  242. this.emit("ready", this.frame);
  243. deferred.resolve(this.frame);
  244. };
  245. win.addEventListener("load", frameLoad, true);
  246. win.addEventListener("unload", this._boundUnload);
  247. this._window = win;
  248. return deferred.promise;
  249. },
  250. /**
  251. * Catch the user closing the window.
  252. */
  253. _boundUnload: function (event) {
  254. if (event.target.location != this.WINDOW_URL) {
  255. return;
  256. }
  257. this._window.removeEventListener("unload", this._boundUnload);
  258. this.emit("window-closed");
  259. },
  260. /**
  261. * Raise the host.
  262. */
  263. raise: function () {
  264. this._window.focus();
  265. },
  266. /**
  267. * Set the toolbox title.
  268. */
  269. setTitle: function (title) {
  270. this._window.document.title = title;
  271. },
  272. /**
  273. * Destroy the window.
  274. */
  275. destroy: function () {
  276. if (!this._destroyed) {
  277. this._destroyed = true;
  278. this._window.removeEventListener("unload", this._boundUnload);
  279. this._window.close();
  280. }
  281. return promise.resolve(null);
  282. }
  283. };
  284. /**
  285. * Host object for the toolbox in its own tab
  286. */
  287. function CustomHost(hostTab, options) {
  288. this.frame = options.customIframe;
  289. this.uid = options.uid;
  290. EventEmitter.decorate(this);
  291. }
  292. CustomHost.prototype = {
  293. type: "custom",
  294. _sendMessageToTopWindow: function (msg, data) {
  295. // It's up to the custom frame owner (parent window) to honor
  296. // "close" or "raise" instructions.
  297. let topWindow = this.frame.ownerDocument.defaultView;
  298. if (!topWindow) {
  299. return;
  300. }
  301. let json = {name: "toolbox-" + msg, uid: this.uid};
  302. if (data) {
  303. json.data = data;
  304. }
  305. topWindow.postMessage(JSON.stringify(json), "*");
  306. },
  307. /**
  308. * Create a new xul window to contain the toolbox.
  309. */
  310. create: function () {
  311. return promise.resolve(this.frame);
  312. },
  313. /**
  314. * Raise the host.
  315. */
  316. raise: function () {
  317. this._sendMessageToTopWindow("raise");
  318. },
  319. /**
  320. * Set the toolbox title.
  321. */
  322. setTitle: function (title) {
  323. this._sendMessageToTopWindow("title", { value: title });
  324. },
  325. /**
  326. * Destroy the window.
  327. */
  328. destroy: function () {
  329. if (!this._destroyed) {
  330. this._destroyed = true;
  331. this._sendMessageToTopWindow("close");
  332. }
  333. return promise.resolve(null);
  334. }
  335. };
  336. /**
  337. * Switch to the given tab in a browser and focus the browser window
  338. */
  339. function focusTab(tab) {
  340. let browserWindow = tab.ownerDocument.defaultView;
  341. browserWindow.focus();
  342. browserWindow.gBrowser.selectedTab = tab;
  343. }