Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const { classes: Cc, interfaces: Ci, utils: Cu } = Components; |
michael@0 | 8 | |
michael@0 | 9 | this.EXPORTED_SYMBOLS = [ "switchToFloatingScrollbars", "switchToNativeScrollbars" ]; |
michael@0 | 10 | |
michael@0 | 11 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | let URL = Services.io.newURI("chrome://browser/skin/devtools/floating-scrollbars.css", null, null); |
michael@0 | 14 | |
michael@0 | 15 | let trackedTabs = new WeakMap(); |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * Switch to floating scrollbars, à la mobile. |
michael@0 | 19 | * |
michael@0 | 20 | * @param aTab the targeted tab. |
michael@0 | 21 | * |
michael@0 | 22 | */ |
michael@0 | 23 | this.switchToFloatingScrollbars = function switchToFloatingScrollbars(aTab) { |
michael@0 | 24 | let mgr = trackedTabs.get(aTab); |
michael@0 | 25 | if (!mgr) { |
michael@0 | 26 | mgr = new ScrollbarManager(aTab); |
michael@0 | 27 | } |
michael@0 | 28 | mgr.switchToFloating(); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | /** |
michael@0 | 32 | * Switch to original native scrollbars. |
michael@0 | 33 | * |
michael@0 | 34 | * @param aTab the targeted tab. |
michael@0 | 35 | * |
michael@0 | 36 | */ |
michael@0 | 37 | this.switchToNativeScrollbars = function switchToNativeScrollbars(aTab) { |
michael@0 | 38 | let mgr = trackedTabs.get(aTab); |
michael@0 | 39 | if (mgr) { |
michael@0 | 40 | mgr.reset(); |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | function ScrollbarManager(aTab) { |
michael@0 | 45 | trackedTabs.set(aTab, this); |
michael@0 | 46 | |
michael@0 | 47 | this.attachedTab = aTab; |
michael@0 | 48 | this.attachedBrowser = aTab.linkedBrowser; |
michael@0 | 49 | |
michael@0 | 50 | this.reset = this.reset.bind(this); |
michael@0 | 51 | this.switchToFloating = this.switchToFloating.bind(this); |
michael@0 | 52 | |
michael@0 | 53 | this.attachedTab.addEventListener("TabClose", this.reset, true); |
michael@0 | 54 | this.attachedBrowser.addEventListener("DOMContentLoaded", this.switchToFloating, true); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | ScrollbarManager.prototype = { |
michael@0 | 58 | get win() { |
michael@0 | 59 | return this.attachedBrowser.contentWindow; |
michael@0 | 60 | }, |
michael@0 | 61 | |
michael@0 | 62 | /* |
michael@0 | 63 | * Change the look of the scrollbars. |
michael@0 | 64 | */ |
michael@0 | 65 | switchToFloating: function() { |
michael@0 | 66 | let windows = this.getInnerWindows(this.win); |
michael@0 | 67 | windows.forEach(this.injectStyleSheet); |
michael@0 | 68 | this.forceStyle(); |
michael@0 | 69 | }, |
michael@0 | 70 | |
michael@0 | 71 | |
michael@0 | 72 | /* |
michael@0 | 73 | * Reset the look of the scrollbars. |
michael@0 | 74 | */ |
michael@0 | 75 | reset: function() { |
michael@0 | 76 | let windows = this.getInnerWindows(this.win); |
michael@0 | 77 | windows.forEach(this.removeStyleSheet); |
michael@0 | 78 | this.forceStyle(this.attachedBrowser); |
michael@0 | 79 | this.attachedBrowser.removeEventListener("DOMContentLoaded", this.switchToFloating, true); |
michael@0 | 80 | this.attachedTab.removeEventListener("TabClose", this.reset, true); |
michael@0 | 81 | trackedTabs.delete(this.attachedTab); |
michael@0 | 82 | }, |
michael@0 | 83 | |
michael@0 | 84 | /* |
michael@0 | 85 | * Toggle the display property of the window to force the style to be applied. |
michael@0 | 86 | */ |
michael@0 | 87 | forceStyle: function() { |
michael@0 | 88 | let parentWindow = this.attachedBrowser.ownerDocument.defaultView; |
michael@0 | 89 | let display = parentWindow.getComputedStyle(this.attachedBrowser).display; // Save display value |
michael@0 | 90 | this.attachedBrowser.style.display = "none"; |
michael@0 | 91 | parentWindow.getComputedStyle(this.attachedBrowser).display; // Flush |
michael@0 | 92 | this.attachedBrowser.style.display = display; // Restore |
michael@0 | 93 | }, |
michael@0 | 94 | |
michael@0 | 95 | /* |
michael@0 | 96 | * return all the window objects present in the hiearchy of a window. |
michael@0 | 97 | */ |
michael@0 | 98 | getInnerWindows: function(win) { |
michael@0 | 99 | let iframes = win.document.querySelectorAll("iframe"); |
michael@0 | 100 | let innerWindows = []; |
michael@0 | 101 | for (let iframe of iframes) { |
michael@0 | 102 | innerWindows = innerWindows.concat(this.getInnerWindows(iframe.contentWindow)); |
michael@0 | 103 | } |
michael@0 | 104 | return [win].concat(innerWindows); |
michael@0 | 105 | }, |
michael@0 | 106 | |
michael@0 | 107 | /* |
michael@0 | 108 | * Append the new scrollbar style. |
michael@0 | 109 | */ |
michael@0 | 110 | injectStyleSheet: function(win) { |
michael@0 | 111 | let winUtils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils); |
michael@0 | 112 | try { |
michael@0 | 113 | winUtils.loadSheet(URL, win.AGENT_SHEET); |
michael@0 | 114 | }catch(e) {} |
michael@0 | 115 | }, |
michael@0 | 116 | |
michael@0 | 117 | /* |
michael@0 | 118 | * Remove the injected stylesheet. |
michael@0 | 119 | */ |
michael@0 | 120 | removeStyleSheet: function(win) { |
michael@0 | 121 | let winUtils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils); |
michael@0 | 122 | try { |
michael@0 | 123 | winUtils.removeSheet(URL, win.AGENT_SHEET); |
michael@0 | 124 | }catch(e) {} |
michael@0 | 125 | }, |
michael@0 | 126 | } |