[PALEMOON] Update the taskbar preview per tab module for UXP.

- Use placesutils for favicons
- Switch to the PageThumbs service for thumbnails (like QuickDial)
- Make thumb requests asynchronous
- Update canvas handling

This resolves #731
This commit is contained in:
wolfbeast 2018-09-02 09:09:45 +02:00 • committed by Roy Tam
commit 51025a5a81

View file

@ -3,12 +3,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* /*
* This module implements the front end behavior for AeroPeek. Starting in * This module implements the front end behavior for AeroPeek. The taskbar
* Windows Vista, the taskbar began showing live thumbnail previews of windows * allows an application to expose its tabbed interface by showing thumbnail
* when the user hovered over the window icon in the taskbar. Starting with * previews rather than the default window preview.
* Windows 7, the taskbar allows an application to expose its tabbed interface * Additionally, when a user hovers over a thumbnail (tab or window),
* in the taskbar by showing thumbnail previews rather than the default window
* preview. Additionally, when a user hovers over a thumbnail (tab or window),
* they are shown a live preview of the window (or tab + its containing window). * they are shown a live preview of the window (or tab + its containing window).
* *
* In Windows 7, a title, icon, close button and optional toolbar are shown for * In Windows 7, a title, icon, close button and optional toolbar are shown for
@ -31,7 +29,7 @@
* Screen real estate is limited so when there are too many thumbnails to fit * Screen real estate is limited so when there are too many thumbnails to fit
* on the screen, the taskbar stops displaying thumbnails and instead displays * on the screen, the taskbar stops displaying thumbnails and instead displays
* just the title, icon and close button in a similar fashion to previous * just the title, icon and close button in a similar fashion to previous
* versions of the taskbar. If there are still too many previews to fit on the * versions of the taskbar. If there are still too many previews to fit on the
* screen, the taskbar resorts to a scroll up and scroll down button pair to let * screen, the taskbar resorts to a scroll up and scroll down button pair to let
* the user scroll through the list of tabs. Since this is undoubtedly * the user scroll through the list of tabs. Since this is undoubtedly
* inconvenient for users with many tabs, the AeroPeek objects turns off all of * inconvenient for users with many tabs, the AeroPeek objects turns off all of
@ -47,9 +45,11 @@ const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm"); Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/PlacesUtils.jsm");
Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
// Pref to enable/disable preview-per-tab // Pref to enable/disable preview-per-tab
const TOGGLE_PREF_NAME = "browser.taskbar.previews.enable"; const TOGGLE_PREF_NAME = "browser.taskbar.previews.enable";
@ -60,17 +60,15 @@ const CACHE_EXPIRATION_TIME_PREF_NAME = "browser.taskbar.previews.cachetime";
const WINTASKBAR_CONTRACTID = "@mozilla.org/windows-taskbar;1"; const WINTASKBAR_CONTRACTID = "@mozilla.org/windows-taskbar;1";
//////////////////////////////////////////////////////////////////////////////// // Various utility properties
//// Various utility properties
XPCOMUtils.defineLazyServiceGetter(this, "imgTools", XPCOMUtils.defineLazyServiceGetter(this, "imgTools",
"@mozilla.org/image/tools;1", "@mozilla.org/image/tools;1",
"imgITools"); "imgITools");
XPCOMUtils.defineLazyServiceGetter(this, "faviconSvc", XPCOMUtils.defineLazyModuleGetter(this, "PageThumbs",
"@mozilla.org/browser/favicon-service;1", "resource://gre/modules/PageThumbs.jsm");
"nsIFaviconService");
// nsIURI -> imgIContainer // nsIURI -> imgIContainer
function _imageFromURI(doc, uri, privateMode, callback) { function _imageFromURI(uri, privateMode, callback) {
let channel = NetUtil.newChannel({ let channel = NetUtil.newChannel({
uri: uri, uri: uri,
loadUsingSystemPrincipal: true, loadUsingSystemPrincipal: true,
@ -93,19 +91,20 @@ function _imageFromURI(doc, uri, privateMode, callback) {
} catch (e) { } catch (e) {
// We failed, so use the default favicon (only if this wasn't the default // We failed, so use the default favicon (only if this wasn't the default
// favicon). // favicon).
let defaultURI = faviconSvc.defaultFavicon; let defaultURI = PlacesUtils.favicons.defaultFavicon;
if (!defaultURI.equals(uri)) if (!defaultURI.equals(uri))
_imageFromURI(doc, defaultURI, privateMode, callback); _imageFromURI(defaultURI, privateMode, callback);
} }
}); });
} }
// string? -> imgIContainer // string? -> imgIContainer
function getFaviconAsImage(doc, iconurl, privateMode, callback) { function getFaviconAsImage(iconurl, privateMode, callback) {
if (iconurl) if (iconurl) {
_imageFromURI(doc, NetUtil.newURI(iconurl), privateMode, callback); _imageFromURI(NetUtil.newURI(iconurl), privateMode, callback);
else } else {
_imageFromURI(doc, faviconSvc.defaultFavicon, privateMode, callback); _imageFromURI(PlacesUtils.favicons.defaultFavicon, privateMode, callback);
}
} }
// Snaps the given rectangle to be pixel-aligned at the given scale // Snaps the given rectangle to be pixel-aligned at the given scale
@ -121,16 +120,17 @@ function snapRectAtScale(r, scale) {
r.height = height / scale; r.height = height / scale;
} }
//////////////////////////////////////////////////////////////////////////////// // PreviewController
//// PreviewController
/* /*
* This class manages the behavior of the preview. * This class manages the behavior of thumbnails and previews. It has the following
* * responsibilities:
* To give greater performance when drawing, the dirty areas of the content * 1) Responding to requests from Windows taskbar for a thumbnail or window
* window are tracked and drawn on demand into a canvas of the same size. * preview.
* This provides a great increase in responsiveness when drawing a preview * 2) Listening for DOM events that result in a thumbnail or window preview needing
* for unchanged (or even only slightly changed) tabs. * to be refreshed, and communicating this to the taskbar.
* 3) Handling queryies and returning new thumbnail or window preview images to the
* taskbar through PageThumbs.
* *
* @param win * @param win
* The TabWindow (see below) that owns the preview that this controls * The TabWindow (see below) that owns the preview that this controls
@ -143,205 +143,181 @@ function PreviewController(win, tab) {
this.linkedBrowser = tab.linkedBrowser; this.linkedBrowser = tab.linkedBrowser;
this.preview = this.win.createTabPreview(this); this.preview = this.win.createTabPreview(this);
this.linkedBrowser.addEventListener("MozAfterPaint", this, false);
this.tab.addEventListener("TabAttrModified", this, false); this.tab.addEventListener("TabAttrModified", this, false);
XPCOMUtils.defineLazyGetter(this, "canvasPreview", function () { XPCOMUtils.defineLazyGetter(this, "canvasPreview", function () {
let canvas = this.win.win.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); let canvas = PageThumbs.createCanvas();
canvas.mozOpaque = true; canvas.mozOpaque = true;
return canvas; return canvas;
}); });
XPCOMUtils.defineLazyGetter(this, "dirtyRegion",
function () {
let dirtyRegion = Cc["@mozilla.org/gfx/region;1"]
.createInstance(Ci.nsIScriptableRegion);
dirtyRegion.init();
return dirtyRegion;
});
XPCOMUtils.defineLazyGetter(this, "winutils",
function () {
let win = tab.linkedBrowser.contentWindow;
return win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
});
} }
PreviewController.prototype = { PreviewController.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsITaskbarPreviewController, QueryInterface: XPCOMUtils.generateQI([Ci.nsITaskbarPreviewController,
Ci.nsIDOMEventListener]), Ci.nsIDOMEventListener]),
destroy: function () { destroy: function () {
this.tab.removeEventListener("TabAttrModified", this, false); this.tab.removeEventListener("TabAttrModified", this, false);
this.linkedBrowser.removeEventListener("MozAfterPaint", this, false);
// Break cycles, otherwise we end up leaking the window with everything // Break cycles, otherwise we end up leaking the window with everything
// attached to it. // attached to it.
delete this.win; delete this.win;
delete this.preview; delete this.preview;
delete this.dirtyRegion;
}, },
get wrappedJSObject() { get wrappedJSObject() {
return this; return this;
}, },
get dirtyRects() {
let rectstream = this.dirtyRegion.getRects();
if (!rectstream)
return [];
let rects = [];
for (let i = 0; i < rectstream.length; i+= 4) {
let r = {x: rectstream[i],
y: rectstream[i+1],
width: rectstream[i+2],
height: rectstream[i+3]};
rects.push(r);
}
return rects;
},
// Resizes the canvasPreview to 0x0, essentially freeing its memory. // Resizes the canvasPreview to 0x0, essentially freeing its memory.
// updateCanvasPreview() will detect the size mismatch as a resize event
// the next time it is called.
resetCanvasPreview: function () { resetCanvasPreview: function () {
this.canvasPreview.width = 0; this.canvasPreview.width = 0;
this.canvasPreview.height = 0; this.canvasPreview.height = 0;
}, },
get zoom() { /**
// Note that winutils.fullZoom accounts for "quantization" of the zoom factor * Set the canvas dimensions.
// from nsIMarkupDocumentViewer due to conversion through appUnits. */
// We do -not- want screenPixelsPerCSSPixel here, because that would -also- resizeCanvasPreview: function (aRequestedWidth, aRequestedHeight) {
// incorporate any scaling that is applied due to hi-dpi resolution options. this.canvasPreview.width = aRequestedWidth;
return this.winutils.fullZoom; this.canvasPreview.height = aRequestedHeight;
}, },
// Updates the controller's canvas with the parts of the <browser> that need
// to be redrawn.
updateCanvasPreview: function () {
let win = this.linkedBrowser.contentWindow;
let bx = this.linkedBrowser.boxObject;
// Check for resize
if (bx.width != this.canvasPreview.width ||
bx.height != this.canvasPreview.height) {
// Invalidate the entire area and repaint
this.onTabPaint({left:0, top:0, right:win.innerWidth, bottom:win.innerHeight});
this.canvasPreview.width = bx.width;
this.canvasPreview.height = bx.height;
}
// Draw dirty regions get zoom() {
let ctx = this.canvasPreview.getContext("2d"); // Note that winutils.fullZoom accounts for "quantization" of the zoom factor
let scale = this.zoom; // from nsIContentViewer due to conversion through appUnits.
// We do -not- want screenPixelsPerCSSPixel here, because that would -also-
// incorporate any scaling that is applied due to hi-dpi resolution options.
return this.tab.linkedBrowser.fullZoom;
},
let flags = this.canvasPreviewFlags; get screenPixelsPerCSSPixel() {
// The dirty region may include parts that are offscreen so we clip to the let chromeWin = this.tab.ownerGlobal;
// canvas area. let windowUtils = chromeWin.getInterface(Ci.nsIDOMWindowUtils);
this.dirtyRegion.intersectRect(0, 0, win.innerWidth, win.innerHeight); return windowUtils.screenPixelsPerCSSPixel;
this.dirtyRects.forEach(function (r) { },
// We need to snap the rectangle to be pixel aligned in the destination
// coordinate space. Otherwise natively themed widgets might not draw.
snapRectAtScale(r, scale);
let x = r.x;
let y = r.y;
let width = r.width;
let height = r.height;
ctx.save(); get browserDims() {
ctx.scale(scale, scale); return this.tab.linkedBrowser.getBoundingClientRect();
ctx.translate(x, y); },
ctx.drawWindow(win, x, y, width, height, "white", flags);
ctx.restore();
});
this.dirtyRegion.setToRect(0,0,0,0);
cacheBrowserDims: function () {
let dims = this.browserDims;
this._cachedWidth = dims.width;
this._cachedHeight = dims.height;
},
testCacheBrowserDims: function () {
let dims = this.browserDims;
return this._cachedWidth == dims.width &&
this._cachedHeight == dims.height;
},
/**
* Capture a new thumbnail image for this preview. Called by the controller
* in response to a request for a new thumbnail image.
*/
updateCanvasPreview: function (aFullScale, aCallback) {
// Update our cached browser dims so that delayed resize
// events don't trigger another invalidation if this tab becomes active.
this.cacheBrowserDims();
PageThumbs.captureToCanvas(this.linkedBrowser, this.canvasPreview,
aCallback, { fullScale: aFullScale });
// If we're updating the canvas, then we're in the middle of a peek so // If we're updating the canvas, then we're in the middle of a peek so
// don't discard the cache of previews. // don't discard the cache of previews.
AeroPeek.resetCacheTimer(); AeroPeek.resetCacheTimer();
}, },
onTabPaint: function (rect) {
let x = Math.floor(rect.left),
y = Math.floor(rect.top),
width = Math.ceil(rect.right) - x,
height = Math.ceil(rect.bottom) - y;
this.dirtyRegion.unionRect(x, y, width, height);
},
updateTitleAndTooltip: function () { updateTitleAndTooltip: function () {
let title = this.win.tabbrowser.getWindowTitleForBrowser(this.linkedBrowser); let title = this.win.tabbrowser.getWindowTitleForBrowser(this.linkedBrowser);
this.preview.title = title; this.preview.title = title;
this.preview.tooltip = title; this.preview.tooltip = title;
}, },
////////////////////////////////////////////////////////////////////////////// // nsITaskbarPreviewController
//// nsITaskbarPreviewController
// window width and height, not browser
get width() { get width() {
return this.win.width; return this.win.width;
}, },
// window width and height, not browser
get height() { get height() {
return this.win.height; return this.win.height;
}, },
get thumbnailAspectRatio() { get thumbnailAspectRatio() {
let boxObject = this.tab.linkedBrowser.boxObject; let browserDims = this.browserDims;
// Avoid returning 0 // Avoid returning 0
let tabWidth = boxObject.width || 1; let tabWidth = browserDims.width || 1;
// Avoid divide by 0 // Avoid divide by 0
let tabHeight = boxObject.height || 1; let tabHeight = browserDims.height || 1;
return tabWidth / tabHeight; return tabWidth / tabHeight;
}, },
drawPreview: function (ctx) { /**
let self = this; * Responds to taskbar requests for window previews. Returns the results asynchronously
this.win.tabbrowser.previewTab(this.tab, function () self.previewTabCallback(ctx)); * through updateCanvasPreview.
*
* @param aTaskbarCallback nsITaskbarPreviewCallback results callback
*/
requestPreview: function (aTaskbarCallback) {
// Grab a high res content preview
this.resetCanvasPreview();
this.updateCanvasPreview(true, (aPreviewCanvas) => {
let winWidth = this.win.width;
let winHeight = this.win.height;
// We must avoid having the frame drawn around the window. See bug 520807 let composite = PageThumbs.createCanvas();
return false;
// Use transparency, Aero glass is drawn black without it.
composite.mozOpaque = false;
let ctx = composite.getContext('2d');
let scale = this.screenPixelsPerCSSPixel / this.zoom;
composite.width = winWidth * scale;
composite.height = winHeight * scale;
ctx.save();
ctx.scale(scale, scale);
// Draw chrome. Note we currently do not get scrollbars for remote frames
// in the image above.
ctx.drawWindow(this.win.win, 0, 0, winWidth, winHeight, "rgba(0,0,0,0)");
// Draw the content are into the composite canvas at the right location.
ctx.drawImage(aPreviewCanvas, this.browserDims.x, this.browserDims.y,
aPreviewCanvas.width, aPreviewCanvas.height);
ctx.restore();
// Deliver the resulting composite canvas to Windows
this.win.tabbrowser.previewTab(this.tab, function () {
aTaskbarCallback.done(composite, false);
});
});
}, },
previewTabCallback: function (ctx) { /**
// This will extract the resolution-scale component of the scaling we need, * Responds to taskbar requests for tab thumbnails. Returns the results asynchronously
// which should be applied to both chrome and content; * through updateCanvasPreview.
// the page zoom component is applied (to content only) within updateCanvasPreview. *
let scale = this.winutils.screenPixelsPerCSSPixel / this.winutils.fullZoom; * Note Windows requests a specific width and height here, if the resulting thumbnail
ctx.save(); * does not match these dimensions thumbnail display will fail.
ctx.scale(scale, scale); *
let width = this.win.width; * @param aTaskbarCallback nsITaskbarPreviewCallback results callback
let height = this.win.height; * @param aRequestedWidth width of the requested thumbnail
// Draw our toplevel window * @param aRequestedHeight height of the requested thumbnail
ctx.drawWindow(this.win.win, 0, 0, width, height, "transparent"); */
requestThumbnail: function (aTaskbarCallback, aRequestedWidth, aRequestedHeight) {
// XXX (jfkthame): Pending tabs don't seem to draw with the proper scaling this.resizeCanvasPreview(aRequestedWidth, aRequestedHeight);
// unless we use this block of code; but doing this for "normal" (loaded) tabs this.updateCanvasPreview(false, (aThumbnailCanvas) => {
// results in blurry rendering on hidpi systems, so we avoid it if possible. aTaskbarCallback.done(aThumbnailCanvas, false);
// I don't understand why pending and loaded tabs behave differently here... });
// (see bug 857061).
if (this.tab.hasAttribute("pending")) {
// Compositor, where art thou?
// Draw the tab content on top of the toplevel window
this.updateCanvasPreview();
let boxObject = this.linkedBrowser.boxObject;
ctx.translate(boxObject.x, boxObject.y);
ctx.drawImage(this.canvasPreview, 0, 0);
}
ctx.restore();
}, },
drawThumbnail: function (ctx, width, height) { // Event handling
this.updateCanvasPreview();
let scale = width/this.linkedBrowser.boxObject.width;
ctx.scale(scale, scale);
ctx.drawImage(this.canvasPreview, 0, 0);
// Don't draw a frame around the thumbnail
return false;
},
onClose: function () { onClose: function () {
this.win.tabbrowser.removeTab(this.tab); this.win.tabbrowser.removeTab(this.tab);
@ -355,22 +331,9 @@ PreviewController.prototype = {
return true; return true;
}, },
//// nsIDOMEventListener // nsIDOMEventListener
handleEvent: function (evt) { handleEvent: function (evt) {
switch (evt.type) { switch (evt.type) {
case "MozAfterPaint":
if (evt.originalTarget === this.linkedBrowser.contentWindow) {
let clientRects = evt.clientRects;
let length = clientRects.length;
for (let i = 0; i < length; i++) {
let r = clientRects.item(i);
this.onTabPaint(r);
}
}
let preview = this.preview;
if (preview.visible)
preview.invalidate();
break;
case "TabAttrModified": case "TabAttrModified":
this.updateTitleAndTooltip(); this.updateTitleAndTooltip();
break; break;
@ -386,14 +349,13 @@ XPCOMUtils.defineLazyGetter(PreviewController.prototype, "canvasPreviewFlags",
| canvasInterface.DRAWWINDOW_DO_NOT_FLUSH; | canvasInterface.DRAWWINDOW_DO_NOT_FLUSH;
}); });
//////////////////////////////////////////////////////////////////////////////// // TabWindow
//// TabWindow
/* /*
* This class monitors a browser window for changes to its tabs * This class monitors a browser window for changes to its tabs
* *
* @param win * @param win
* The nsIDOMWindow browser window * The nsIDOMWindow browser window
*/ */
function TabWindow(win) { function TabWindow(win) {
this.win = win; this.win = win;
@ -403,6 +365,10 @@ function TabWindow(win) {
for (let i = 0; i < this.tabEvents.length; i++) for (let i = 0; i < this.tabEvents.length; i++)
this.tabbrowser.tabContainer.addEventListener(this.tabEvents[i], this, false); this.tabbrowser.tabContainer.addEventListener(this.tabEvents[i], this, false);
for (let i = 0; i < this.winEvents.length; i++)
this.win.addEventListener(this.winEvents[i], this, false);
this.tabbrowser.addTabsProgressListener(this); this.tabbrowser.addTabsProgressListener(this);
AeroPeek.windows.push(this); AeroPeek.windows.push(this);
@ -416,7 +382,10 @@ function TabWindow(win) {
TabWindow.prototype = { TabWindow.prototype = {
_enabled: false, _enabled: false,
_cachedWidth: 0,
_cachedHeight: 0,
tabEvents: ["TabOpen", "TabClose", "TabSelect", "TabMove"], tabEvents: ["TabOpen", "TabClose", "TabSelect", "TabMove"],
winEvents: ["resize"],
destroy: function () { destroy: function () {
this._destroying = true; this._destroying = true;
@ -424,6 +393,10 @@ TabWindow.prototype = {
let tabs = this.tabbrowser.tabs; let tabs = this.tabbrowser.tabs;
this.tabbrowser.removeTabsProgressListener(this); this.tabbrowser.removeTabsProgressListener(this);
for (let i = 0; i < this.winEvents.length; i++)
this.win.removeEventListener(this.winEvents[i], this, false);
for (let i = 0; i < this.tabEvents.length; i++) for (let i = 0; i < this.tabEvents.length; i++)
this.tabbrowser.tabContainer.removeEventListener(this.tabEvents[i], this, false); this.tabbrowser.tabContainer.removeEventListener(this.tabEvents[i], this, false);
@ -442,6 +415,15 @@ TabWindow.prototype = {
return this.win.innerHeight; return this.win.innerHeight;
}, },
cacheDims: function () {
this._cachedWidth = this.width;
this._cachedHeight = this.height;
},
testCacheDims: function () {
return this._cachedWidth == this.width && this._cachedHeight == this.height;
},
// Invoked when the given tab is added to this window // Invoked when the given tab is added to this window
newTab: function (tab) { newTab: function (tab) {
let controller = new PreviewController(this, tab); let controller = new PreviewController(this, tab);
@ -461,18 +443,8 @@ TabWindow.prototype = {
let preview = AeroPeek.taskbar.createTaskbarTabPreview(docShell, controller); let preview = AeroPeek.taskbar.createTaskbarTabPreview(docShell, controller);
preview.visible = AeroPeek.enabled; preview.visible = AeroPeek.enabled;
preview.active = this.tabbrowser.selectedTab == controller.tab; preview.active = this.tabbrowser.selectedTab == controller.tab;
// Grab the default favicon this.onLinkIconAvailable(controller.tab.linkedBrowser,
getFaviconAsImage( controller.tab.getAttribute("image"));
controller.linkedBrowser.contentWindow.document,
null,
PrivateBrowsingUtils.isWindowPrivate(this.win),
function (img) {
// It is possible that we've already gotten the real favicon, so make sure
// we have not set one before setting this default one.
if (!preview.icon)
preview.icon = img;
});
return preview; return preview;
}, },
@ -484,8 +456,6 @@ TabWindow.prototype = {
preview.move(null); preview.move(null);
preview.controller.wrappedJSObject.destroy(); preview.controller.wrappedJSObject.destroy();
// We don't want to splice from the array if the tabs aren't being removed
// from the tab bar as well (as is the case when the window closes).
this.previews.delete(tab); this.previews.delete(tab);
AeroPeek.removePreview(preview); AeroPeek.removePreview(preview);
}, },
@ -499,7 +469,7 @@ TabWindow.prototype = {
// Because making a tab visible requires that the tab it is next to be // Because making a tab visible requires that the tab it is next to be
// visible, it is far simpler to unset the 'next' tab and recreate them all // visible, it is far simpler to unset the 'next' tab and recreate them all
// at once. // at once.
for (let [tab, preview] of this.previews) { for (let [, preview] of this.previews) {
preview.move(null); preview.move(null);
preview.visible = enable; preview.visible = enable;
} }
@ -514,27 +484,25 @@ TabWindow.prototype = {
let previews = this.previews; let previews = this.previews;
let tabs = this.tabbrowser.tabs; let tabs = this.tabbrowser.tabs;
// Previews are internally stored using a map, so we need to iterate over // Previews are internally stored using a map, so we need to iterate the
// the tabbrowser's array of tabs to retrieve previews in the same order. // tabbrowser's array of tabs to retrieve previews in the same order.
// Tycho: let inorder = [previews.get(t) for (t of tabs) if (previews.has(t))];
let inorder = []; let inorder = [];
for (let t of tabs) { for (let t of tabs) {
if (previews.has(t)) { if (previews.has(t)) {
inorder.push(previews.get(t)); inorder.push(previews.get(t));
} }
} }
// Since the internal taskbar array has not yet been updated, we must force // Since the internal taskbar array has not yet been updated we must force
// the sorting order of our local array on it. To do so, we must walk // on it the sorting order of our local array. To do so we must walk
// the local array backwards, because otherwise we would send move requests // the local array backwards, otherwise we would send move requests in the
// in the wrong order. See bug 522610 for details. // wrong order. See bug 522610 for details.
for (let i = inorder.length - 1; i >= 0; i--) { for (let i = inorder.length - 1; i >= 0; i--) {
inorder[i].move(inorder[i + 1] || null); inorder[i].move(inorder[i + 1] || null);
} }
}, },
//// nsIDOMEventListener // nsIDOMEventListener
handleEvent: function (evt) { handleEvent: function (evt) {
let tab = evt.originalTarget; let tab = evt.originalTarget;
switch (evt.type) { switch (evt.type) {
@ -552,28 +520,117 @@ TabWindow.prototype = {
case "TabMove": case "TabMove":
this.updateTabOrdering(); this.updateTabOrdering();
break; break;
case "resize":
if (!AeroPeek._prefenabled)
return;
this.onResize();
break;
} }
}, },
//// Browser progress listener // Set or reset a timer that will invalidate visible thumbnails soon.
onLinkIconAvailable: function (aBrowser, aIconURL) { setInvalidationTimer: function () {
let self = this; if (!this.invalidateTimer) {
getFaviconAsImage( this.invalidateTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
aBrowser.contentWindow.document, }
aIconURL,PrivateBrowsingUtils.isWindowPrivate(this.win), this.invalidateTimer.cancel();
function (img) {
let index = self.tabbrowser.browsers.indexOf(aBrowser); // delay 1 second before invalidating
// Only add it if we've found the index. The tab could have closed! this.invalidateTimer.initWithCallback(() => {
if (index != -1) { // invalidate every preview. note the internal implementation of
let tab = self.tabbrowser.tabs[index]; // invalidate ignores thumbnails that aren't visible.
self.previews.get(tab).icon = img; this.previews.forEach(function (aPreview) {
let controller = aPreview.controller.wrappedJSObject;
if (!controller.testCacheBrowserDims()) {
controller.cacheBrowserDims();
aPreview.invalidate();
} }
}); });
}, 1000, Ci.nsITimer.TYPE_ONE_SHOT);
},
onResize: function () {
// Specific to a window.
// Call invalidate on each tab thumbnail so that Windows will request an
// updated image. However don't do this repeatedly across multiple resize
// events triggered during window border drags.
if (this.testCacheDims()) {
return;
}
// update the window dims on our TabWindow object.
this.cacheDims();
// invalidate soon
this.setInvalidationTimer();
},
invalidateTabPreview: function(aBrowser) {
for (let [tab, preview] of this.previews) {
if (aBrowser == tab.linkedBrowser) {
preview.invalidate();
break;
}
}
},
// Browser progress listener
onLocationChange: function (aBrowser) {
// I'm not sure we need this, onStateChange does a really good job
// of picking up page changes.
// this.invalidateTabPreview(aBrowser);
},
onStateChange: function (aBrowser, aWebProgress, aRequest, aStateFlags, aStatus) {
if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) {
this.invalidateTabPreview(aBrowser);
}
},
directRequestProtocols: new Set([
"file", "chrome", "resource", "about"
]),
onLinkIconAvailable: function (aBrowser, aIconURL) {
let requestURL = null;
if (aIconURL) {
let shouldRequestFaviconURL = true;
try {
let urlObject = NetUtil.newURI(aIconURL);
shouldRequestFaviconURL =
!this.directRequestProtocols.has(urlObject.scheme);
} catch (ex) {}
requestURL = shouldRequestFaviconURL ?
"moz-anno:favicon:" + aIconURL :
aIconURL;
}
let isDefaultFavicon = !requestURL;
getFaviconAsImage(
requestURL,
PrivateBrowsingUtils.isWindowPrivate(this.win),
img => {
let index = this.tabbrowser.browsers.indexOf(aBrowser);
// Only add it if we've found the index and the URI is still the same.
// The tab could have closed, and there's no guarantee the icons
// will have finished fetching 'in order'.
if (index != -1) {
let tab = this.tabbrowser.tabs[index];
let preview = this.previews.get(tab);
if (tab.getAttribute("image") == aIconURL ||
(!preview.icon && isDefaultFavicon)) {
preview.icon = img;
}
}
}
);
} }
} }
//////////////////////////////////////////////////////////////////////////////// // AeroPeek
//// AeroPeek
/* /*
* This object acts as global storage and external interface for this feature. * This object acts as global storage and external interface for this feature.
@ -582,10 +639,12 @@ TabWindow.prototype = {
this.AeroPeek = { this.AeroPeek = {
available: false, available: false,
// Does the pref say we're enabled? // Does the pref say we're enabled?
_prefenabled: true, __prefenabled: false,
_enabled: true, _enabled: true,
initialized: false,
// nsITaskbarTabPreview array // nsITaskbarTabPreview array
previews: [], previews: [],
@ -609,24 +668,14 @@ this.AeroPeek = {
if (!this.available) if (!this.available)
return; return;
this.prefs.addObserver(TOGGLE_PREF_NAME, this, false); this.prefs.addObserver(TOGGLE_PREF_NAME, this, true);
this.prefs.addObserver(DISABLE_THRESHOLD_PREF_NAME, this, false);
this.prefs.addObserver(CACHE_EXPIRATION_TIME_PREF_NAME, this, false);
this.cacheLifespan = this.prefs.getIntPref(CACHE_EXPIRATION_TIME_PREF_NAME);
this.maxpreviews = this.prefs.getIntPref(DISABLE_THRESHOLD_PREF_NAME);
this.enabled = this._prefenabled = this.prefs.getBoolPref(TOGGLE_PREF_NAME); this.enabled = this._prefenabled = this.prefs.getBoolPref(TOGGLE_PREF_NAME);
this.initialized = true;
}, },
destroy: function destroy() { destroy: function destroy() {
this._enabled = false; this._enabled = false;
this.prefs.removeObserver(TOGGLE_PREF_NAME, this);
this.prefs.removeObserver(DISABLE_THRESHOLD_PREF_NAME, this);
this.prefs.removeObserver(CACHE_EXPIRATION_TIME_PREF_NAME, this);
if (this.cacheTimer) if (this.cacheTimer)
this.cacheTimer.cancel(); this.cacheTimer.cancel();
}, },
@ -646,6 +695,61 @@ this.AeroPeek = {
}); });
}, },
get _prefenabled() {
return this.__prefenabled;
},
set _prefenabled(enable) {
if (enable == this.__prefenabled) {
return;
}
this.__prefenabled = enable;
if (enable) {
this.enable();
} else {
this.disable();
}
},
_observersAdded: false,
enable() {
if (!this._observersAdded) {
this.prefs.addObserver(DISABLE_THRESHOLD_PREF_NAME, this, true);
this.prefs.addObserver(CACHE_EXPIRATION_TIME_PREF_NAME, this, true);
PlacesUtils.history.addObserver(this, true);
this._observersAdded = true;
}
this.cacheLifespan = this.prefs.getIntPref(CACHE_EXPIRATION_TIME_PREF_NAME);
this.maxpreviews = this.prefs.getIntPref(DISABLE_THRESHOLD_PREF_NAME);
// If the user toggled us on/off while the browser was already up
// (rather than this code running on startup because the pref was
// already set to true), we must initialize previews for open windows:
if (this.initialized) {
let browserWindows = Services.wm.getEnumerator("navigator:browser");
while (browserWindows.hasMoreElements()) {
let win = browserWindows.getNext();
if (!win.closed) {
this.onOpenWindow(win);
}
}
}
},
disable() {
while (this.windows.length) {
// We can't call onCloseWindow here because it'll bail if we're not
// enabled.
let tabWinObject = this.windows[0];
tabWinObject.destroy(); // This will remove us from the array.
delete tabWinObject.win.gTaskbarTabGroup; // Tidy up the window.
}
},
addPreview: function (preview) { addPreview: function (preview) {
this.previews.push(preview); this.previews.push(preview);
this.checkPreviewCount(); this.checkPreviewCount();
@ -658,15 +762,15 @@ this.AeroPeek = {
}, },
checkPreviewCount: function () { checkPreviewCount: function () {
if (this.previews.length > this.maxpreviews) if (!this._prefenabled) {
this.enabled = false; return;
else }
this.enabled = this._prefenabled; this.enabled = this.previews.length <= this.maxpreviews;
}, },
onOpenWindow: function (win) { onOpenWindow: function (win) {
// This occurs when the taskbar service is not available (xp, vista) // This occurs when the taskbar service is not available (xp, vista)
if (!this.available) if (!this.available || !this._prefenabled)
return; return;
win.gTaskbarTabGroup = new TabWindow(win); win.gTaskbarTabGroup = new TabWindow(win);
@ -674,7 +778,7 @@ this.AeroPeek = {
onCloseWindow: function (win) { onCloseWindow: function (win) {
// This occurs when the taskbar service is not available (xp, vista) // This occurs when the taskbar service is not available (xp, vista)
if (!this.available) if (!this.available || !this._prefenabled)
return; return;
win.gTaskbarTabGroup.destroy(); win.gTaskbarTabGroup.destroy();
@ -689,16 +793,20 @@ this.AeroPeek = {
this.cacheTimer.init(this, 1000*this.cacheLifespan, Ci.nsITimer.TYPE_ONE_SHOT); this.cacheTimer.init(this, 1000*this.cacheLifespan, Ci.nsITimer.TYPE_ONE_SHOT);
}, },
//// nsIObserver // nsIObserver
observe: function (aSubject, aTopic, aData) { observe: function (aSubject, aTopic, aData) {
if (aTopic == "nsPref:changed" && aData == TOGGLE_PREF_NAME) {
this._prefenabled = this.prefs.getBoolPref(TOGGLE_PREF_NAME);
}
if (!this._prefenabled) {
return;
}
switch (aTopic) { switch (aTopic) {
case "nsPref:changed": case "nsPref:changed":
if (aData == CACHE_EXPIRATION_TIME_PREF_NAME) if (aData == CACHE_EXPIRATION_TIME_PREF_NAME)
break; break;
if (aData == TOGGLE_PREF_NAME) if (aData == DISABLE_THRESHOLD_PREF_NAME)
this._prefenabled = this.prefs.getBoolPref(TOGGLE_PREF_NAME);
else if (aData == DISABLE_THRESHOLD_PREF_NAME)
this.maxpreviews = this.prefs.getIntPref(DISABLE_THRESHOLD_PREF_NAME); this.maxpreviews = this.prefs.getIntPref(DISABLE_THRESHOLD_PREF_NAME);
// Might need to enable/disable ourselves // Might need to enable/disable ourselves
this.checkPreviewCount(); this.checkPreviewCount();
@ -710,10 +818,38 @@ this.AeroPeek = {
}); });
break; break;
} }
} },
/* nsINavHistoryObserver implementation */
onBeginUpdateBatch() {},
onEndUpdateBatch() {},
onVisit() {},
onTitleChanged() {},
onFrecencyChanged() {},
onManyFrecenciesChanged() {},
onDeleteURI() {},
onClearHistory() {},
onDeleteVisits() {},
onPageChanged(uri, changedConst, newValue) {
if (this.enabled && changedConst == Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON) {
for (let win of this.windows) {
for (let [tab, ] of win.previews) {
if (tab.getAttribute("image") == newValue) {
win.onLinkIconAvailable(tab.linkedBrowser, newValue);
}
}
}
}
},
QueryInterface: XPCOMUtils.generateQI([
Ci.nsISupportsWeakReference,
Ci.nsINavHistoryObserver,
Ci.nsIObserver
]),
}; };
XPCOMUtils.defineLazyGetter(AeroPeek, "cacheTimer", function () XPCOMUtils.defineLazyGetter(AeroPeek, "cacheTimer", () =>
Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer) Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer)
); );