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 | /* vim: se cin sw=2 ts=2 et filetype=javascript : |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | /* |
michael@0 | 6 | * This module implements the front end behavior for AeroPeek. Starting in |
michael@0 | 7 | * Windows Vista, the taskbar began showing live thumbnail previews of windows |
michael@0 | 8 | * when the user hovered over the window icon in the taskbar. Starting with |
michael@0 | 9 | * Windows 7, the taskbar allows an application to expose its tabbed interface |
michael@0 | 10 | * in the taskbar by showing thumbnail previews rather than the default window |
michael@0 | 11 | * preview. Additionally, when a user hovers over a thumbnail (tab or window), |
michael@0 | 12 | * they are shown a live preview of the window (or tab + its containing window). |
michael@0 | 13 | * |
michael@0 | 14 | * In Windows 7, a title, icon, close button and optional toolbar are shown for |
michael@0 | 15 | * each preview. This feature does not make use of the toolbar. For window |
michael@0 | 16 | * previews, the title is the window title and the icon the window icon. For |
michael@0 | 17 | * tab previews, the title is the page title and the page's favicon. In both |
michael@0 | 18 | * cases, the close button "does the right thing." |
michael@0 | 19 | * |
michael@0 | 20 | * The primary objects behind this feature are nsITaskbarTabPreview and |
michael@0 | 21 | * nsITaskbarPreviewController. Each preview has a controller. The controller |
michael@0 | 22 | * responds to the user's interactions on the taskbar and provides the required |
michael@0 | 23 | * data to the preview for determining the size of the tab and thumbnail. The |
michael@0 | 24 | * PreviewController class implements this interface. The preview will request |
michael@0 | 25 | * the controller to provide a thumbnail or preview when the user interacts with |
michael@0 | 26 | * the taskbar. To reduce the overhead of drawing the tab area, the controller |
michael@0 | 27 | * implementation caches the tab's contents in a <canvas> element. If no |
michael@0 | 28 | * previews or thumbnails have been requested for some time, the controller will |
michael@0 | 29 | * discard its cached tab contents. |
michael@0 | 30 | * |
michael@0 | 31 | * Screen real estate is limited so when there are too many thumbnails to fit |
michael@0 | 32 | * on the screen, the taskbar stops displaying thumbnails and instead displays |
michael@0 | 33 | * just the title, icon and close button in a similar fashion to previous |
michael@0 | 34 | * versions of the taskbar. If there are still too many previews to fit on the |
michael@0 | 35 | * screen, the taskbar resorts to a scroll up and scroll down button pair to let |
michael@0 | 36 | * the user scroll through the list of tabs. Since this is undoubtedly |
michael@0 | 37 | * inconvenient for users with many tabs, the AeroPeek objects turns off all of |
michael@0 | 38 | * the tab previews. This tells the taskbar to revert to one preview per window. |
michael@0 | 39 | * If the number of tabs falls below this magic threshold, the preview-per-tab |
michael@0 | 40 | * behavior returns. There is no reliable way to determine when the scroll |
michael@0 | 41 | * buttons appear on the taskbar, so a magic pref-controlled number determines |
michael@0 | 42 | * when this threshold has been crossed. |
michael@0 | 43 | */ |
michael@0 | 44 | this.EXPORTED_SYMBOLS = ["AeroPeek"]; |
michael@0 | 45 | |
michael@0 | 46 | const Cc = Components.classes; |
michael@0 | 47 | const Ci = Components.interfaces; |
michael@0 | 48 | const Cu = Components.utils; |
michael@0 | 49 | |
michael@0 | 50 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 51 | Cu.import("resource://gre/modules/NetUtil.jsm"); |
michael@0 | 52 | Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); |
michael@0 | 53 | |
michael@0 | 54 | // Pref to enable/disable preview-per-tab |
michael@0 | 55 | const TOGGLE_PREF_NAME = "browser.taskbar.previews.enable"; |
michael@0 | 56 | // Pref to determine the magic auto-disable threshold |
michael@0 | 57 | const DISABLE_THRESHOLD_PREF_NAME = "browser.taskbar.previews.max"; |
michael@0 | 58 | // Pref to control the time in seconds that tab contents live in the cache |
michael@0 | 59 | const CACHE_EXPIRATION_TIME_PREF_NAME = "browser.taskbar.previews.cachetime"; |
michael@0 | 60 | |
michael@0 | 61 | const WINTASKBAR_CONTRACTID = "@mozilla.org/windows-taskbar;1"; |
michael@0 | 62 | |
michael@0 | 63 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 64 | //// Various utility properties |
michael@0 | 65 | XPCOMUtils.defineLazyServiceGetter(this, "ioSvc", |
michael@0 | 66 | "@mozilla.org/network/io-service;1", |
michael@0 | 67 | "nsIIOService"); |
michael@0 | 68 | XPCOMUtils.defineLazyServiceGetter(this, "imgTools", |
michael@0 | 69 | "@mozilla.org/image/tools;1", |
michael@0 | 70 | "imgITools"); |
michael@0 | 71 | XPCOMUtils.defineLazyServiceGetter(this, "faviconSvc", |
michael@0 | 72 | "@mozilla.org/browser/favicon-service;1", |
michael@0 | 73 | "nsIFaviconService"); |
michael@0 | 74 | |
michael@0 | 75 | // nsIURI -> imgIContainer |
michael@0 | 76 | function _imageFromURI(uri, privateMode, callback) { |
michael@0 | 77 | let channel = ioSvc.newChannelFromURI(uri); |
michael@0 | 78 | try { |
michael@0 | 79 | channel.QueryInterface(Ci.nsIPrivateBrowsingChannel); |
michael@0 | 80 | channel.setPrivate(privateMode); |
michael@0 | 81 | } catch (e) { |
michael@0 | 82 | // Ignore channels which do not support nsIPrivateBrowsingChannel |
michael@0 | 83 | } |
michael@0 | 84 | NetUtil.asyncFetch(channel, function(inputStream, resultCode) { |
michael@0 | 85 | if (!Components.isSuccessCode(resultCode)) |
michael@0 | 86 | return; |
michael@0 | 87 | try { |
michael@0 | 88 | let out_img = { value: null }; |
michael@0 | 89 | imgTools.decodeImageData(inputStream, channel.contentType, out_img); |
michael@0 | 90 | callback(out_img.value); |
michael@0 | 91 | } catch (e) { |
michael@0 | 92 | // We failed, so use the default favicon (only if this wasn't the default |
michael@0 | 93 | // favicon). |
michael@0 | 94 | let defaultURI = faviconSvc.defaultFavicon; |
michael@0 | 95 | if (!defaultURI.equals(uri)) |
michael@0 | 96 | _imageFromURI(defaultURI, callback); |
michael@0 | 97 | } |
michael@0 | 98 | }); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | // string? -> imgIContainer |
michael@0 | 102 | function getFaviconAsImage(iconurl, privateMode, callback) { |
michael@0 | 103 | if (iconurl) |
michael@0 | 104 | _imageFromURI(NetUtil.newURI(iconurl), privateMode, callback); |
michael@0 | 105 | else |
michael@0 | 106 | _imageFromURI(faviconSvc.defaultFavicon, privateMode, callback); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | // Snaps the given rectangle to be pixel-aligned at the given scale |
michael@0 | 110 | function snapRectAtScale(r, scale) { |
michael@0 | 111 | let x = Math.floor(r.x * scale); |
michael@0 | 112 | let y = Math.floor(r.y * scale); |
michael@0 | 113 | let width = Math.ceil((r.x + r.width) * scale) - x; |
michael@0 | 114 | let height = Math.ceil((r.y + r.height) * scale) - y; |
michael@0 | 115 | |
michael@0 | 116 | r.x = x / scale; |
michael@0 | 117 | r.y = y / scale; |
michael@0 | 118 | r.width = width / scale; |
michael@0 | 119 | r.height = height / scale; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 123 | //// PreviewController |
michael@0 | 124 | |
michael@0 | 125 | /* |
michael@0 | 126 | * This class manages the behavior of the preview. |
michael@0 | 127 | * |
michael@0 | 128 | * To give greater performance when drawing, the dirty areas of the content |
michael@0 | 129 | * window are tracked and drawn on demand into a canvas of the same size. |
michael@0 | 130 | * This provides a great increase in responsiveness when drawing a preview |
michael@0 | 131 | * for unchanged (or even only slightly changed) tabs. |
michael@0 | 132 | * |
michael@0 | 133 | * @param win |
michael@0 | 134 | * The TabWindow (see below) that owns the preview that this controls |
michael@0 | 135 | * @param tab |
michael@0 | 136 | * The <tab> that this preview is associated with |
michael@0 | 137 | */ |
michael@0 | 138 | function PreviewController(win, tab) { |
michael@0 | 139 | this.win = win; |
michael@0 | 140 | this.tab = tab; |
michael@0 | 141 | this.linkedBrowser = tab.linkedBrowser; |
michael@0 | 142 | this.preview = this.win.createTabPreview(this); |
michael@0 | 143 | |
michael@0 | 144 | this.linkedBrowser.addEventListener("MozAfterPaint", this, false); |
michael@0 | 145 | this.tab.addEventListener("TabAttrModified", this, false); |
michael@0 | 146 | |
michael@0 | 147 | XPCOMUtils.defineLazyGetter(this, "canvasPreview", function () { |
michael@0 | 148 | let canvas = this.win.win.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); |
michael@0 | 149 | canvas.mozOpaque = true; |
michael@0 | 150 | return canvas; |
michael@0 | 151 | }); |
michael@0 | 152 | |
michael@0 | 153 | XPCOMUtils.defineLazyGetter(this, "dirtyRegion", |
michael@0 | 154 | function () { |
michael@0 | 155 | let dirtyRegion = Cc["@mozilla.org/gfx/region;1"] |
michael@0 | 156 | .createInstance(Ci.nsIScriptableRegion); |
michael@0 | 157 | dirtyRegion.init(); |
michael@0 | 158 | return dirtyRegion; |
michael@0 | 159 | }); |
michael@0 | 160 | |
michael@0 | 161 | XPCOMUtils.defineLazyGetter(this, "winutils", |
michael@0 | 162 | function () { |
michael@0 | 163 | let win = tab.linkedBrowser.contentWindow; |
michael@0 | 164 | return win.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 165 | .getInterface(Ci.nsIDOMWindowUtils); |
michael@0 | 166 | }); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | PreviewController.prototype = { |
michael@0 | 170 | QueryInterface: XPCOMUtils.generateQI([Ci.nsITaskbarPreviewController, |
michael@0 | 171 | Ci.nsIDOMEventListener]), |
michael@0 | 172 | destroy: function () { |
michael@0 | 173 | this.tab.removeEventListener("TabAttrModified", this, false); |
michael@0 | 174 | this.linkedBrowser.removeEventListener("MozAfterPaint", this, false); |
michael@0 | 175 | |
michael@0 | 176 | // Break cycles, otherwise we end up leaking the window with everything |
michael@0 | 177 | // attached to it. |
michael@0 | 178 | delete this.win; |
michael@0 | 179 | delete this.preview; |
michael@0 | 180 | delete this.dirtyRegion; |
michael@0 | 181 | }, |
michael@0 | 182 | get wrappedJSObject() { |
michael@0 | 183 | return this; |
michael@0 | 184 | }, |
michael@0 | 185 | |
michael@0 | 186 | get dirtyRects() { |
michael@0 | 187 | let rectstream = this.dirtyRegion.getRects(); |
michael@0 | 188 | if (!rectstream) |
michael@0 | 189 | return []; |
michael@0 | 190 | let rects = []; |
michael@0 | 191 | for (let i = 0; i < rectstream.length; i+= 4) { |
michael@0 | 192 | let r = {x: rectstream[i], |
michael@0 | 193 | y: rectstream[i+1], |
michael@0 | 194 | width: rectstream[i+2], |
michael@0 | 195 | height: rectstream[i+3]}; |
michael@0 | 196 | rects.push(r); |
michael@0 | 197 | } |
michael@0 | 198 | return rects; |
michael@0 | 199 | }, |
michael@0 | 200 | |
michael@0 | 201 | // Resizes the canvasPreview to 0x0, essentially freeing its memory. |
michael@0 | 202 | // updateCanvasPreview() will detect the size mismatch as a resize event |
michael@0 | 203 | // the next time it is called. |
michael@0 | 204 | resetCanvasPreview: function () { |
michael@0 | 205 | this.canvasPreview.width = 0; |
michael@0 | 206 | this.canvasPreview.height = 0; |
michael@0 | 207 | }, |
michael@0 | 208 | |
michael@0 | 209 | get zoom() { |
michael@0 | 210 | // Note that winutils.fullZoom accounts for "quantization" of the zoom factor |
michael@0 | 211 | // from nsIMarkupDocumentViewer due to conversion through appUnits. |
michael@0 | 212 | // We do -not- want screenPixelsPerCSSPixel here, because that would -also- |
michael@0 | 213 | // incorporate any scaling that is applied due to hi-dpi resolution options. |
michael@0 | 214 | return this.winutils.fullZoom; |
michael@0 | 215 | }, |
michael@0 | 216 | |
michael@0 | 217 | // Updates the controller's canvas with the parts of the <browser> that need |
michael@0 | 218 | // to be redrawn. |
michael@0 | 219 | updateCanvasPreview: function () { |
michael@0 | 220 | let win = this.linkedBrowser.contentWindow; |
michael@0 | 221 | let bx = this.linkedBrowser.boxObject; |
michael@0 | 222 | // Check for resize |
michael@0 | 223 | if (bx.width != this.canvasPreview.width || |
michael@0 | 224 | bx.height != this.canvasPreview.height) { |
michael@0 | 225 | // Invalidate the entire area and repaint |
michael@0 | 226 | this.onTabPaint({left:0, top:0, right:win.innerWidth, bottom:win.innerHeight}); |
michael@0 | 227 | this.canvasPreview.width = bx.width; |
michael@0 | 228 | this.canvasPreview.height = bx.height; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | // Draw dirty regions |
michael@0 | 232 | let ctx = this.canvasPreview.getContext("2d"); |
michael@0 | 233 | let scale = this.zoom; |
michael@0 | 234 | |
michael@0 | 235 | let flags = this.canvasPreviewFlags; |
michael@0 | 236 | // The dirty region may include parts that are offscreen so we clip to the |
michael@0 | 237 | // canvas area. |
michael@0 | 238 | this.dirtyRegion.intersectRect(0, 0, win.innerWidth, win.innerHeight); |
michael@0 | 239 | this.dirtyRects.forEach(function (r) { |
michael@0 | 240 | // We need to snap the rectangle to be pixel aligned in the destination |
michael@0 | 241 | // coordinate space. Otherwise natively themed widgets might not draw. |
michael@0 | 242 | snapRectAtScale(r, scale); |
michael@0 | 243 | let x = r.x; |
michael@0 | 244 | let y = r.y; |
michael@0 | 245 | let width = r.width; |
michael@0 | 246 | let height = r.height; |
michael@0 | 247 | |
michael@0 | 248 | ctx.save(); |
michael@0 | 249 | ctx.scale(scale, scale); |
michael@0 | 250 | ctx.translate(x, y); |
michael@0 | 251 | ctx.drawWindow(win, x, y, width, height, "white", flags); |
michael@0 | 252 | ctx.restore(); |
michael@0 | 253 | }); |
michael@0 | 254 | this.dirtyRegion.setToRect(0,0,0,0); |
michael@0 | 255 | |
michael@0 | 256 | // If we're updating the canvas, then we're in the middle of a peek so |
michael@0 | 257 | // don't discard the cache of previews. |
michael@0 | 258 | AeroPeek.resetCacheTimer(); |
michael@0 | 259 | }, |
michael@0 | 260 | |
michael@0 | 261 | onTabPaint: function (rect) { |
michael@0 | 262 | let x = Math.floor(rect.left), |
michael@0 | 263 | y = Math.floor(rect.top), |
michael@0 | 264 | width = Math.ceil(rect.right) - x, |
michael@0 | 265 | height = Math.ceil(rect.bottom) - y; |
michael@0 | 266 | this.dirtyRegion.unionRect(x, y, width, height); |
michael@0 | 267 | }, |
michael@0 | 268 | |
michael@0 | 269 | updateTitleAndTooltip: function () { |
michael@0 | 270 | let title = this.win.tabbrowser.getWindowTitleForBrowser(this.linkedBrowser); |
michael@0 | 271 | this.preview.title = title; |
michael@0 | 272 | this.preview.tooltip = title; |
michael@0 | 273 | }, |
michael@0 | 274 | |
michael@0 | 275 | ////////////////////////////////////////////////////////////////////////////// |
michael@0 | 276 | //// nsITaskbarPreviewController |
michael@0 | 277 | |
michael@0 | 278 | get width() { |
michael@0 | 279 | return this.win.width; |
michael@0 | 280 | }, |
michael@0 | 281 | |
michael@0 | 282 | get height() { |
michael@0 | 283 | return this.win.height; |
michael@0 | 284 | }, |
michael@0 | 285 | |
michael@0 | 286 | get thumbnailAspectRatio() { |
michael@0 | 287 | let boxObject = this.tab.linkedBrowser.boxObject; |
michael@0 | 288 | // Avoid returning 0 |
michael@0 | 289 | let tabWidth = boxObject.width || 1; |
michael@0 | 290 | // Avoid divide by 0 |
michael@0 | 291 | let tabHeight = boxObject.height || 1; |
michael@0 | 292 | return tabWidth / tabHeight; |
michael@0 | 293 | }, |
michael@0 | 294 | |
michael@0 | 295 | drawPreview: function (ctx) { |
michael@0 | 296 | let self = this; |
michael@0 | 297 | this.win.tabbrowser.previewTab(this.tab, function () self.previewTabCallback(ctx)); |
michael@0 | 298 | |
michael@0 | 299 | // We must avoid having the frame drawn around the window. See bug 520807 |
michael@0 | 300 | return false; |
michael@0 | 301 | }, |
michael@0 | 302 | |
michael@0 | 303 | previewTabCallback: function (ctx) { |
michael@0 | 304 | // This will extract the resolution-scale component of the scaling we need, |
michael@0 | 305 | // which should be applied to both chrome and content; |
michael@0 | 306 | // the page zoom component is applied (to content only) within updateCanvasPreview. |
michael@0 | 307 | let scale = this.winutils.screenPixelsPerCSSPixel / this.winutils.fullZoom; |
michael@0 | 308 | ctx.save(); |
michael@0 | 309 | ctx.scale(scale, scale); |
michael@0 | 310 | let width = this.win.width; |
michael@0 | 311 | let height = this.win.height; |
michael@0 | 312 | // Draw our toplevel window |
michael@0 | 313 | ctx.drawWindow(this.win.win, 0, 0, width, height, "transparent"); |
michael@0 | 314 | |
michael@0 | 315 | // XXX (jfkthame): Pending tabs don't seem to draw with the proper scaling |
michael@0 | 316 | // unless we use this block of code; but doing this for "normal" (loaded) tabs |
michael@0 | 317 | // results in blurry rendering on hidpi systems, so we avoid it if possible. |
michael@0 | 318 | // I don't understand why pending and loaded tabs behave differently here... |
michael@0 | 319 | // (see bug 857061). |
michael@0 | 320 | if (this.tab.hasAttribute("pending")) { |
michael@0 | 321 | // Compositor, where art thou? |
michael@0 | 322 | // Draw the tab content on top of the toplevel window |
michael@0 | 323 | this.updateCanvasPreview(); |
michael@0 | 324 | |
michael@0 | 325 | let boxObject = this.linkedBrowser.boxObject; |
michael@0 | 326 | ctx.translate(boxObject.x, boxObject.y); |
michael@0 | 327 | ctx.drawImage(this.canvasPreview, 0, 0); |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | ctx.restore(); |
michael@0 | 331 | }, |
michael@0 | 332 | |
michael@0 | 333 | drawThumbnail: function (ctx, width, height) { |
michael@0 | 334 | this.updateCanvasPreview(); |
michael@0 | 335 | |
michael@0 | 336 | let scale = width/this.linkedBrowser.boxObject.width; |
michael@0 | 337 | ctx.scale(scale, scale); |
michael@0 | 338 | ctx.drawImage(this.canvasPreview, 0, 0); |
michael@0 | 339 | |
michael@0 | 340 | // Don't draw a frame around the thumbnail |
michael@0 | 341 | return false; |
michael@0 | 342 | }, |
michael@0 | 343 | |
michael@0 | 344 | onClose: function () { |
michael@0 | 345 | this.win.tabbrowser.removeTab(this.tab); |
michael@0 | 346 | }, |
michael@0 | 347 | |
michael@0 | 348 | onActivate: function () { |
michael@0 | 349 | this.win.tabbrowser.selectedTab = this.tab; |
michael@0 | 350 | |
michael@0 | 351 | // Accept activation - this will restore the browser window |
michael@0 | 352 | // if it's minimized |
michael@0 | 353 | return true; |
michael@0 | 354 | }, |
michael@0 | 355 | |
michael@0 | 356 | //// nsIDOMEventListener |
michael@0 | 357 | handleEvent: function (evt) { |
michael@0 | 358 | switch (evt.type) { |
michael@0 | 359 | case "MozAfterPaint": |
michael@0 | 360 | if (evt.originalTarget === this.linkedBrowser.contentWindow) { |
michael@0 | 361 | let clientRects = evt.clientRects; |
michael@0 | 362 | let length = clientRects.length; |
michael@0 | 363 | for (let i = 0; i < length; i++) { |
michael@0 | 364 | let r = clientRects.item(i); |
michael@0 | 365 | this.onTabPaint(r); |
michael@0 | 366 | } |
michael@0 | 367 | } |
michael@0 | 368 | let preview = this.preview; |
michael@0 | 369 | if (preview.visible) |
michael@0 | 370 | preview.invalidate(); |
michael@0 | 371 | break; |
michael@0 | 372 | case "TabAttrModified": |
michael@0 | 373 | this.updateTitleAndTooltip(); |
michael@0 | 374 | break; |
michael@0 | 375 | } |
michael@0 | 376 | } |
michael@0 | 377 | }; |
michael@0 | 378 | |
michael@0 | 379 | XPCOMUtils.defineLazyGetter(PreviewController.prototype, "canvasPreviewFlags", |
michael@0 | 380 | function () { let canvasInterface = Ci.nsIDOMCanvasRenderingContext2D; |
michael@0 | 381 | return canvasInterface.DRAWWINDOW_DRAW_VIEW |
michael@0 | 382 | | canvasInterface.DRAWWINDOW_DRAW_CARET |
michael@0 | 383 | | canvasInterface.DRAWWINDOW_ASYNC_DECODE_IMAGES |
michael@0 | 384 | | canvasInterface.DRAWWINDOW_DO_NOT_FLUSH; |
michael@0 | 385 | }); |
michael@0 | 386 | |
michael@0 | 387 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 388 | //// TabWindow |
michael@0 | 389 | |
michael@0 | 390 | /* |
michael@0 | 391 | * This class monitors a browser window for changes to its tabs |
michael@0 | 392 | * |
michael@0 | 393 | * @param win |
michael@0 | 394 | * The nsIDOMWindow browser window |
michael@0 | 395 | */ |
michael@0 | 396 | function TabWindow(win) { |
michael@0 | 397 | this.win = win; |
michael@0 | 398 | this.tabbrowser = win.gBrowser; |
michael@0 | 399 | |
michael@0 | 400 | this.previews = new Map(); |
michael@0 | 401 | |
michael@0 | 402 | for (let i = 0; i < this.tabEvents.length; i++) |
michael@0 | 403 | this.tabbrowser.tabContainer.addEventListener(this.tabEvents[i], this, false); |
michael@0 | 404 | this.tabbrowser.addTabsProgressListener(this); |
michael@0 | 405 | |
michael@0 | 406 | for (let i = 0; i < this.winEvents.length; i++) |
michael@0 | 407 | this.win.addEventListener(this.winEvents[i], this, false); |
michael@0 | 408 | |
michael@0 | 409 | AeroPeek.windows.push(this); |
michael@0 | 410 | let tabs = this.tabbrowser.tabs; |
michael@0 | 411 | for (let i = 0; i < tabs.length; i++) |
michael@0 | 412 | this.newTab(tabs[i]); |
michael@0 | 413 | |
michael@0 | 414 | this.updateTabOrdering(); |
michael@0 | 415 | AeroPeek.checkPreviewCount(); |
michael@0 | 416 | } |
michael@0 | 417 | |
michael@0 | 418 | TabWindow.prototype = { |
michael@0 | 419 | _enabled: false, |
michael@0 | 420 | tabEvents: ["TabOpen", "TabClose", "TabSelect", "TabMove"], |
michael@0 | 421 | winEvents: ["tabviewshown", "tabviewhidden"], |
michael@0 | 422 | |
michael@0 | 423 | destroy: function () { |
michael@0 | 424 | this._destroying = true; |
michael@0 | 425 | |
michael@0 | 426 | let tabs = this.tabbrowser.tabs; |
michael@0 | 427 | |
michael@0 | 428 | this.tabbrowser.removeTabsProgressListener(this); |
michael@0 | 429 | for (let i = 0; i < this.tabEvents.length; i++) |
michael@0 | 430 | this.tabbrowser.tabContainer.removeEventListener(this.tabEvents[i], this, false); |
michael@0 | 431 | |
michael@0 | 432 | for (let i = 0; i < this.winEvents.length; i++) |
michael@0 | 433 | this.win.removeEventListener(this.winEvents[i], this, false); |
michael@0 | 434 | |
michael@0 | 435 | for (let i = 0; i < tabs.length; i++) |
michael@0 | 436 | this.removeTab(tabs[i]); |
michael@0 | 437 | |
michael@0 | 438 | let idx = AeroPeek.windows.indexOf(this.win.gTaskbarTabGroup); |
michael@0 | 439 | AeroPeek.windows.splice(idx, 1); |
michael@0 | 440 | AeroPeek.checkPreviewCount(); |
michael@0 | 441 | }, |
michael@0 | 442 | |
michael@0 | 443 | get width () { |
michael@0 | 444 | return this.win.innerWidth; |
michael@0 | 445 | }, |
michael@0 | 446 | get height () { |
michael@0 | 447 | return this.win.innerHeight; |
michael@0 | 448 | }, |
michael@0 | 449 | |
michael@0 | 450 | // Invoked when the given tab is added to this window |
michael@0 | 451 | newTab: function (tab) { |
michael@0 | 452 | let controller = new PreviewController(this, tab); |
michael@0 | 453 | // It's OK to add the preview now while the favicon still loads. |
michael@0 | 454 | this.previews.set(tab, controller.preview); |
michael@0 | 455 | AeroPeek.addPreview(controller.preview); |
michael@0 | 456 | // updateTitleAndTooltip relies on having controller.preview which is lazily resolved. |
michael@0 | 457 | // Now that we've updated this.previews, it will resolve successfully. |
michael@0 | 458 | controller.updateTitleAndTooltip(); |
michael@0 | 459 | }, |
michael@0 | 460 | |
michael@0 | 461 | createTabPreview: function (controller) { |
michael@0 | 462 | let docShell = this.win |
michael@0 | 463 | .QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 464 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 465 | .QueryInterface(Ci.nsIDocShell); |
michael@0 | 466 | let preview = AeroPeek.taskbar.createTaskbarTabPreview(docShell, controller); |
michael@0 | 467 | preview.visible = AeroPeek.enabled; |
michael@0 | 468 | preview.active = this.tabbrowser.selectedTab == controller.tab; |
michael@0 | 469 | // Grab the default favicon |
michael@0 | 470 | getFaviconAsImage(null, PrivateBrowsingUtils.isWindowPrivate(this.win), function (img) { |
michael@0 | 471 | // It is possible that we've already gotten the real favicon, so make sure |
michael@0 | 472 | // we have not set one before setting this default one. |
michael@0 | 473 | if (!preview.icon) |
michael@0 | 474 | preview.icon = img; |
michael@0 | 475 | }); |
michael@0 | 476 | |
michael@0 | 477 | return preview; |
michael@0 | 478 | }, |
michael@0 | 479 | |
michael@0 | 480 | // Invoked when the given tab is closed |
michael@0 | 481 | removeTab: function (tab) { |
michael@0 | 482 | let preview = this.previewFromTab(tab); |
michael@0 | 483 | preview.active = false; |
michael@0 | 484 | preview.visible = false; |
michael@0 | 485 | preview.move(null); |
michael@0 | 486 | preview.controller.wrappedJSObject.destroy(); |
michael@0 | 487 | |
michael@0 | 488 | this.previews.delete(tab); |
michael@0 | 489 | AeroPeek.removePreview(preview); |
michael@0 | 490 | }, |
michael@0 | 491 | |
michael@0 | 492 | get enabled () { |
michael@0 | 493 | return this._enabled; |
michael@0 | 494 | }, |
michael@0 | 495 | |
michael@0 | 496 | set enabled (enable) { |
michael@0 | 497 | this._enabled = enable; |
michael@0 | 498 | // Because making a tab visible requires that the tab it is next to be |
michael@0 | 499 | // visible, it is far simpler to unset the 'next' tab and recreate them all |
michael@0 | 500 | // at once. |
michael@0 | 501 | for (let [tab, preview] of this.previews) { |
michael@0 | 502 | preview.move(null); |
michael@0 | 503 | preview.visible = enable; |
michael@0 | 504 | } |
michael@0 | 505 | this.updateTabOrdering(); |
michael@0 | 506 | }, |
michael@0 | 507 | |
michael@0 | 508 | previewFromTab: function (tab) { |
michael@0 | 509 | return this.previews.get(tab); |
michael@0 | 510 | }, |
michael@0 | 511 | |
michael@0 | 512 | updateTabOrdering: function () { |
michael@0 | 513 | let previews = this.previews; |
michael@0 | 514 | let tabs = this.tabbrowser.tabs; |
michael@0 | 515 | |
michael@0 | 516 | // Previews are internally stored using a map, so we need to iterate the |
michael@0 | 517 | // tabbrowser's array of tabs to retrieve previews in the same order. |
michael@0 | 518 | let inorder = [previews.get(t) for (t of tabs) if (previews.has(t))]; |
michael@0 | 519 | |
michael@0 | 520 | // Since the internal taskbar array has not yet been updated we must force |
michael@0 | 521 | // on it the sorting order of our local array. To do so we must walk |
michael@0 | 522 | // the local array backwards, otherwise we would send move requests in the |
michael@0 | 523 | // wrong order. See bug 522610 for details. |
michael@0 | 524 | for (let i = inorder.length - 1; i >= 0; i--) { |
michael@0 | 525 | inorder[i].move(inorder[i + 1] || null); |
michael@0 | 526 | } |
michael@0 | 527 | }, |
michael@0 | 528 | |
michael@0 | 529 | //// nsIDOMEventListener |
michael@0 | 530 | handleEvent: function (evt) { |
michael@0 | 531 | let tab = evt.originalTarget; |
michael@0 | 532 | switch (evt.type) { |
michael@0 | 533 | case "TabOpen": |
michael@0 | 534 | this.newTab(tab); |
michael@0 | 535 | this.updateTabOrdering(); |
michael@0 | 536 | break; |
michael@0 | 537 | case "TabClose": |
michael@0 | 538 | this.removeTab(tab); |
michael@0 | 539 | this.updateTabOrdering(); |
michael@0 | 540 | break; |
michael@0 | 541 | case "TabSelect": |
michael@0 | 542 | this.previewFromTab(tab).active = true; |
michael@0 | 543 | break; |
michael@0 | 544 | case "TabMove": |
michael@0 | 545 | this.updateTabOrdering(); |
michael@0 | 546 | break; |
michael@0 | 547 | case "tabviewshown": |
michael@0 | 548 | this.enabled = false; |
michael@0 | 549 | break; |
michael@0 | 550 | case "tabviewhidden": |
michael@0 | 551 | if (!AeroPeek._prefenabled) |
michael@0 | 552 | return; |
michael@0 | 553 | this.enabled = true; |
michael@0 | 554 | break; |
michael@0 | 555 | } |
michael@0 | 556 | }, |
michael@0 | 557 | |
michael@0 | 558 | //// Browser progress listener |
michael@0 | 559 | onLinkIconAvailable: function (aBrowser, aIconURL) { |
michael@0 | 560 | let self = this; |
michael@0 | 561 | getFaviconAsImage(aIconURL, PrivateBrowsingUtils.isWindowPrivate(this.win), function (img) { |
michael@0 | 562 | let index = self.tabbrowser.browsers.indexOf(aBrowser); |
michael@0 | 563 | // Only add it if we've found the index. The tab could have closed! |
michael@0 | 564 | if (index != -1) { |
michael@0 | 565 | let tab = self.tabbrowser.tabs[index]; |
michael@0 | 566 | self.previews.get(tab).icon = img; |
michael@0 | 567 | } |
michael@0 | 568 | }); |
michael@0 | 569 | } |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 573 | //// AeroPeek |
michael@0 | 574 | |
michael@0 | 575 | /* |
michael@0 | 576 | * This object acts as global storage and external interface for this feature. |
michael@0 | 577 | * It maintains the values of the prefs. |
michael@0 | 578 | */ |
michael@0 | 579 | this.AeroPeek = { |
michael@0 | 580 | available: false, |
michael@0 | 581 | // Does the pref say we're enabled? |
michael@0 | 582 | _prefenabled: true, |
michael@0 | 583 | |
michael@0 | 584 | _enabled: true, |
michael@0 | 585 | |
michael@0 | 586 | // nsITaskbarTabPreview array |
michael@0 | 587 | previews: [], |
michael@0 | 588 | |
michael@0 | 589 | // TabWindow array |
michael@0 | 590 | windows: [], |
michael@0 | 591 | |
michael@0 | 592 | // nsIWinTaskbar service |
michael@0 | 593 | taskbar: null, |
michael@0 | 594 | |
michael@0 | 595 | // Maximum number of previews |
michael@0 | 596 | maxpreviews: 20, |
michael@0 | 597 | |
michael@0 | 598 | // Length of time in seconds that previews are cached |
michael@0 | 599 | cacheLifespan: 20, |
michael@0 | 600 | |
michael@0 | 601 | initialize: function () { |
michael@0 | 602 | if (!(WINTASKBAR_CONTRACTID in Cc)) |
michael@0 | 603 | return; |
michael@0 | 604 | this.taskbar = Cc[WINTASKBAR_CONTRACTID].getService(Ci.nsIWinTaskbar); |
michael@0 | 605 | this.available = this.taskbar.available; |
michael@0 | 606 | if (!this.available) |
michael@0 | 607 | return; |
michael@0 | 608 | |
michael@0 | 609 | this.prefs.addObserver(TOGGLE_PREF_NAME, this, false); |
michael@0 | 610 | this.prefs.addObserver(DISABLE_THRESHOLD_PREF_NAME, this, false); |
michael@0 | 611 | this.prefs.addObserver(CACHE_EXPIRATION_TIME_PREF_NAME, this, false); |
michael@0 | 612 | |
michael@0 | 613 | this.cacheLifespan = this.prefs.getIntPref(CACHE_EXPIRATION_TIME_PREF_NAME); |
michael@0 | 614 | |
michael@0 | 615 | this.maxpreviews = this.prefs.getIntPref(DISABLE_THRESHOLD_PREF_NAME); |
michael@0 | 616 | |
michael@0 | 617 | this.enabled = this._prefenabled = this.prefs.getBoolPref(TOGGLE_PREF_NAME); |
michael@0 | 618 | }, |
michael@0 | 619 | |
michael@0 | 620 | destroy: function destroy() { |
michael@0 | 621 | this._enabled = false; |
michael@0 | 622 | |
michael@0 | 623 | this.prefs.removeObserver(TOGGLE_PREF_NAME, this); |
michael@0 | 624 | this.prefs.removeObserver(DISABLE_THRESHOLD_PREF_NAME, this); |
michael@0 | 625 | this.prefs.removeObserver(CACHE_EXPIRATION_TIME_PREF_NAME, this); |
michael@0 | 626 | |
michael@0 | 627 | if (this.cacheTimer) |
michael@0 | 628 | this.cacheTimer.cancel(); |
michael@0 | 629 | }, |
michael@0 | 630 | |
michael@0 | 631 | get enabled() { |
michael@0 | 632 | return this._enabled; |
michael@0 | 633 | }, |
michael@0 | 634 | |
michael@0 | 635 | set enabled(enable) { |
michael@0 | 636 | if (this._enabled == enable) |
michael@0 | 637 | return; |
michael@0 | 638 | |
michael@0 | 639 | this._enabled = enable; |
michael@0 | 640 | |
michael@0 | 641 | this.windows.forEach(function (win) { |
michael@0 | 642 | win.enabled = enable; |
michael@0 | 643 | }); |
michael@0 | 644 | }, |
michael@0 | 645 | |
michael@0 | 646 | addPreview: function (preview) { |
michael@0 | 647 | this.previews.push(preview); |
michael@0 | 648 | this.checkPreviewCount(); |
michael@0 | 649 | }, |
michael@0 | 650 | |
michael@0 | 651 | removePreview: function (preview) { |
michael@0 | 652 | let idx = this.previews.indexOf(preview); |
michael@0 | 653 | this.previews.splice(idx, 1); |
michael@0 | 654 | this.checkPreviewCount(); |
michael@0 | 655 | }, |
michael@0 | 656 | |
michael@0 | 657 | checkPreviewCount: function () { |
michael@0 | 658 | if (this.previews.length > this.maxpreviews) |
michael@0 | 659 | this.enabled = false; |
michael@0 | 660 | else |
michael@0 | 661 | this.enabled = this._prefenabled; |
michael@0 | 662 | }, |
michael@0 | 663 | |
michael@0 | 664 | onOpenWindow: function (win) { |
michael@0 | 665 | // This occurs when the taskbar service is not available (xp, vista) |
michael@0 | 666 | if (!this.available) |
michael@0 | 667 | return; |
michael@0 | 668 | |
michael@0 | 669 | win.gTaskbarTabGroup = new TabWindow(win); |
michael@0 | 670 | }, |
michael@0 | 671 | |
michael@0 | 672 | onCloseWindow: function (win) { |
michael@0 | 673 | // This occurs when the taskbar service is not available (xp, vista) |
michael@0 | 674 | if (!this.available) |
michael@0 | 675 | return; |
michael@0 | 676 | |
michael@0 | 677 | win.gTaskbarTabGroup.destroy(); |
michael@0 | 678 | delete win.gTaskbarTabGroup; |
michael@0 | 679 | |
michael@0 | 680 | if (this.windows.length == 0) |
michael@0 | 681 | this.destroy(); |
michael@0 | 682 | }, |
michael@0 | 683 | |
michael@0 | 684 | resetCacheTimer: function () { |
michael@0 | 685 | this.cacheTimer.cancel(); |
michael@0 | 686 | this.cacheTimer.init(this, 1000*this.cacheLifespan, Ci.nsITimer.TYPE_ONE_SHOT); |
michael@0 | 687 | }, |
michael@0 | 688 | |
michael@0 | 689 | //// nsIObserver |
michael@0 | 690 | observe: function (aSubject, aTopic, aData) { |
michael@0 | 691 | switch (aTopic) { |
michael@0 | 692 | case "nsPref:changed": |
michael@0 | 693 | if (aData == CACHE_EXPIRATION_TIME_PREF_NAME) |
michael@0 | 694 | break; |
michael@0 | 695 | |
michael@0 | 696 | if (aData == TOGGLE_PREF_NAME) |
michael@0 | 697 | this._prefenabled = this.prefs.getBoolPref(TOGGLE_PREF_NAME); |
michael@0 | 698 | else if (aData == DISABLE_THRESHOLD_PREF_NAME) |
michael@0 | 699 | this.maxpreviews = this.prefs.getIntPref(DISABLE_THRESHOLD_PREF_NAME); |
michael@0 | 700 | // Might need to enable/disable ourselves |
michael@0 | 701 | this.checkPreviewCount(); |
michael@0 | 702 | break; |
michael@0 | 703 | case "timer-callback": |
michael@0 | 704 | this.previews.forEach(function (preview) { |
michael@0 | 705 | let controller = preview.controller.wrappedJSObject; |
michael@0 | 706 | controller.resetCanvasPreview(); |
michael@0 | 707 | }); |
michael@0 | 708 | break; |
michael@0 | 709 | } |
michael@0 | 710 | } |
michael@0 | 711 | }; |
michael@0 | 712 | |
michael@0 | 713 | XPCOMUtils.defineLazyGetter(AeroPeek, "cacheTimer", function () |
michael@0 | 714 | Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer) |
michael@0 | 715 | ); |
michael@0 | 716 | |
michael@0 | 717 | XPCOMUtils.defineLazyServiceGetter(AeroPeek, "prefs", |
michael@0 | 718 | "@mozilla.org/preferences-service;1", |
michael@0 | 719 | "nsIPrefBranch"); |
michael@0 | 720 | |
michael@0 | 721 | AeroPeek.initialize(); |