зеркало из https://github.com/mozilla/pjs.git
Bug 474056 - Implement optional preview per tab r=dao,rflint,mconnor
This commit is contained in:
Родитель
b5cedbcd46
Коммит
1d6ec1768e
|
@ -48,6 +48,12 @@
|
|||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef XP_WIN
|
||||
#ifndef WINCE
|
||||
#define WINNT
|
||||
#endif
|
||||
#endif
|
||||
|
||||
pref("general.startup.browser", true);
|
||||
|
||||
pref("browser.chromeURL","chrome://browser/content/");
|
||||
|
@ -900,3 +906,9 @@ pref("toolbar.customization.usesheet", true);
|
|||
#else
|
||||
pref("toolbar.customization.usesheet", false);
|
||||
#endif
|
||||
|
||||
#ifdef WINNT
|
||||
pref("aeropeek.enable", true);
|
||||
pref("aeropeek.maxpreviews", 20);
|
||||
pref("aeropeek.cachetime", 20);
|
||||
#endif
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
# Edward Lee <edward.lee@engineering.uiuc.edu>
|
||||
# Paul O’Shannessy <paul@oshannessy.com>
|
||||
# Nils Maier <maierman@web.de>
|
||||
# Rob Arnold <robarnold@cmu.edu>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
|
@ -135,6 +136,29 @@ let gInitialPages = [
|
|||
#include browser-places.js
|
||||
#include browser-tabPreviews.js
|
||||
|
||||
#ifdef XP_WIN
|
||||
#ifndef WINCE
|
||||
#define WIN7_FEATURES
|
||||
XPCOMUtils.defineLazyGetter(this, "Win7Features", function () {
|
||||
const WINTASKBAR_CONTRACTID = "@mozilla.org/windows-taskbar;1";
|
||||
let taskbar = Cc[WINTASKBAR_CONTRACTID].getService(Ci.nsIWinTaskbar);
|
||||
if (taskbar.available) {
|
||||
Cu.import("resource://gre/modules/wintaskbar/preview-per-tab.jsm");
|
||||
return {
|
||||
onOpenWindow: function () {
|
||||
AeroPeek.onOpenWindow(window);
|
||||
},
|
||||
onCloseWindow: function () {
|
||||
AeroPeek.onCloseWindow(window);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return { onOpenWindow: function () {}, onCloseWindow: function () {} };
|
||||
}
|
||||
});
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/**
|
||||
* We can avoid adding multiple load event listeners and save some time by adding
|
||||
* one listener that calls all real handlers.
|
||||
|
@ -1341,10 +1365,17 @@ function delayedStartup(isLoadingBlank, mustLoadSidebar) {
|
|||
gBrowser.mPanelContainer.addEventListener("InstallBrowserTheme", LightWeightThemeWebInstaller, false, true);
|
||||
gBrowser.mPanelContainer.addEventListener("PreviewBrowserTheme", LightWeightThemeWebInstaller, false, true);
|
||||
gBrowser.mPanelContainer.addEventListener("ResetBrowserThemePreview", LightWeightThemeWebInstaller, false, true);
|
||||
|
||||
#ifdef WIN7_FEATURES
|
||||
Win7Features.onOpenWindow();
|
||||
#endif
|
||||
}
|
||||
|
||||
function BrowserShutdown()
|
||||
{
|
||||
#ifdef WIN7_FEATURES
|
||||
Win7Features.onCloseWindow();
|
||||
#endif
|
||||
gPrefService.removeObserver(ctrlTab.prefName, ctrlTab);
|
||||
gPrefService.removeObserver(allTabs.prefName, allTabs);
|
||||
tabPreviews.uninit();
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
- Mark Pilgrim <pilgrim@gmail.com>
|
||||
- Dão Gottwald <dao@mozilla.com>
|
||||
- Paul O’Shannessy <paul@oshannessy.com>
|
||||
- Rob Arnold <tellrob@gmail.com>
|
||||
-
|
||||
- Alternatively, the contents of this file may be used under the terms of
|
||||
- either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
|
@ -236,6 +237,29 @@
|
|||
null
|
||||
</field>
|
||||
|
||||
<field name="_previewMode">
|
||||
false
|
||||
</field>
|
||||
|
||||
<method name="previewTab">
|
||||
<parameter name="aTab"/>
|
||||
<parameter name="aCallback"/>
|
||||
<body>
|
||||
<![CDATA[
|
||||
let currentTab = this.selectedTab;
|
||||
try {
|
||||
// Suppress focus, ownership and selected tab changes
|
||||
this._previewMode = true;
|
||||
this.selectedTab = aTab;
|
||||
aCallback();
|
||||
} finally {
|
||||
this.selectedTab = currentTab;
|
||||
this._previewMode = false;
|
||||
}
|
||||
]]>
|
||||
</body>
|
||||
</method>
|
||||
|
||||
<method name="getBrowserAtIndex">
|
||||
<parameter name="aIndex"/>
|
||||
<body>
|
||||
|
@ -843,7 +867,8 @@
|
|||
if (this.mCurrentBrowser == newBrowser && !aForceUpdate)
|
||||
return;
|
||||
|
||||
if (this.mCurrentTab != this.selectedTab)
|
||||
// Preview mode should not reset the owner
|
||||
if (!this._previewMode && this.mCurrentTab != this.selectedTab)
|
||||
this.mCurrentTab.owner = null;
|
||||
|
||||
this._lastRelatedTab = null;
|
||||
|
@ -897,7 +922,9 @@
|
|||
}
|
||||
}
|
||||
|
||||
this._fastFind.setDocShell(this.mCurrentBrowser.docShell);
|
||||
// Don't switch the fast find - this tab switch is temporary
|
||||
if (!this._previewMode)
|
||||
this._fastFind.setDocShell(this.mCurrentBrowser.docShell);
|
||||
|
||||
// Update the window title.
|
||||
this.updateTitlebar();
|
||||
|
@ -935,18 +962,23 @@
|
|||
}
|
||||
}
|
||||
|
||||
// We've selected the new tab, so go ahead and notify listeners.
|
||||
var event = document.createEvent("Events");
|
||||
event.initEvent("TabSelect", true, false);
|
||||
this.mCurrentTab.dispatchEvent(event);
|
||||
// TabSelect events are suppressed during preview mode to avoid confusing extensions and other bits of code
|
||||
// that might rely upon the other changes suppressed.
|
||||
// Focus is suppressed in the event that the main browser window is minimized - focusing a tab would restore the window
|
||||
if (!this._previewMode) {
|
||||
// We've selected the new tab, so go ahead and notify listeners.
|
||||
var event = document.createEvent("Events");
|
||||
event.initEvent("TabSelect", true, false);
|
||||
this.mCurrentTab.dispatchEvent(event);
|
||||
|
||||
// change focus to the new tab if nothing is focused, the old tab
|
||||
// is focused or there is something in the new tab to focus. One
|
||||
// specific case where focus is not changed is when the new tab
|
||||
// has no focused element and a chrome element is focused.
|
||||
if (!focusedChromeElement || focusedChromeElement == oldBrowser ||
|
||||
fm.getFocusedElementForWindow(window.content, true, {}))
|
||||
fm.setFocus(newBrowser, fm.FLAG_NOSCROLL);
|
||||
// change focus to the new tab if nothing is focused, the old tab
|
||||
// is focused or there is something in the new tab to focus. One
|
||||
// specific case where focus is not changed is when the new tab
|
||||
// has no focused element and a chrome element is focused.
|
||||
if ((!focusedChromeElement || focusedChromeElement == oldBrowser ||
|
||||
fm.getFocusedElementForWindow(window.content, true, {})))
|
||||
fm.setFocus(newBrowser, fm.FLAG_NOSCROLL);
|
||||
}
|
||||
]]>
|
||||
</body>
|
||||
</method>
|
||||
|
|
|
@ -76,6 +76,12 @@ ifndef WINCE
|
|||
DIRS += migration
|
||||
endif
|
||||
|
||||
ifeq ($(MOZ_WIDGET_TOOLKIT),windows)
|
||||
ifndef WINCE
|
||||
DIRS += wintaskbar
|
||||
endif
|
||||
endif
|
||||
|
||||
ifdef MOZ_SAFE_BROWSING
|
||||
DIRS += safebrowsing
|
||||
endif
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is Aero Peek
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Mozilla Corporation
|
||||
# Portions created by the Initial Developer are Copyright (C) 2009
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Rob Arnold <tellrob@gmail.com>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
ifdef ENABLE_TESTS
|
||||
DIRS += test
|
||||
endif
|
||||
|
||||
MODULE = wintaskbar
|
||||
|
||||
_WINTASKBAR_FILES = \
|
||||
preview-per-tab.jsm \
|
||||
$(NULL)
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
||||
libs:: $(_WINTASKBAR_FILES)
|
||||
$(NSINSTALL) -D $(DIST)/bin/modules/wintaskbar;
|
||||
$(INSTALL) $(foreach f,$^,"$f") $(DIST)/bin/modules/wintaskbar
|
||||
|
|
@ -0,0 +1,647 @@
|
|||
/* vim: se cin sw=2 ts=2 et :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Corporation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Rob Arnold <robarnold@cmu.edu> (original author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
/*
|
||||
* This module implements the front end behavior for AeroPeek. Starting in
|
||||
* Windows Vista, the taskbar began showing live thumbnail previews of windows
|
||||
* when the user hovered over the window icon in the taskbar. Starting with
|
||||
* Windows 7, the taskbar allows an application to expose its tabbed interface
|
||||
* 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).
|
||||
*
|
||||
* In Windows 7, a title, icon, close button and optional toolbar are shown for
|
||||
* each preview. This feature does not make use of the toolbar. For window
|
||||
* previews, the title is the window title and the icon the window icon. For
|
||||
* tab previews, the title is the page title and the page's favicon. In both
|
||||
* cases, the close button "does the right thing."
|
||||
*
|
||||
* The primary objects behind this feature are nsITaskbarTabPreview and
|
||||
* nsITaskbarPreviewController. Each preview has a controller. The controller
|
||||
* responds to the user's interactions on the taskbar and provides the required
|
||||
* data to the preview for determining the size of the tab and thumbnail. The
|
||||
* PreviewController class implements this interface. The preview will request
|
||||
* the controller to provide a thumbnail or preview when the user interacts with
|
||||
* the taskbar. To reduce the overhead of drawing the tab area, the controller
|
||||
* implementation caches the tab's contents in a <canvas> element. If no
|
||||
* previews or thumbnails have been requested for some time, the controller will
|
||||
* discard its cached tab contents.
|
||||
*
|
||||
* 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
|
||||
* 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
|
||||
* 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
|
||||
* inconvenient for users with many tabs, the AeroPeek objects turns off all of
|
||||
* the tab previews. This tells the taskbar to revert to one preview per window.
|
||||
* If the number of tabs falls below this magic threshold, the preview-per-tab
|
||||
* behavior returns. There is no reliable way to determine when the scroll
|
||||
* buttons appear on the taskbar, so a magic pref-controlled number determines
|
||||
* when this threshold has been crossed.
|
||||
*/
|
||||
var EXPORTED_SYMBOLS = ["AeroPeek"];
|
||||
|
||||
const Cc = Components.classes;
|
||||
const Ci = Components.interfaces;
|
||||
const Cu = Components.utils;
|
||||
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
Cu.import("resource://gre/modules/NetUtil.jsm");
|
||||
|
||||
// Pref to enable/disable preview-per-tab
|
||||
const TOGGLE_PREF_NAME = "aeropeek.enable";
|
||||
// Pref to determine the magic auto-disable threshold
|
||||
const DISABLE_THRESHOLD_PREF_NAME = "aeropeek.maxpreviews";
|
||||
// Pref to control the time in seconds that tab contents live in the cache
|
||||
const CACHE_EXPIRATION_TIME_PREF_NAME = "aeropeek.cachetime";
|
||||
|
||||
const WINTASKBAR_CONTRACTID = "@mozilla.org/windows-taskbar;1";
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//// Various utility properties
|
||||
XPCOMUtils.defineLazyServiceGetter(this, "ioSvc",
|
||||
"@mozilla.org/network/io-service;1",
|
||||
"nsIIOService");
|
||||
XPCOMUtils.defineLazyServiceGetter(this, "imgTools",
|
||||
"@mozilla.org/image/tools;1",
|
||||
"imgITools");
|
||||
XPCOMUtils.defineLazyServiceGetter(this, "faviconSvc",
|
||||
"@mozilla.org/browser/favicon-service;1",
|
||||
"nsIFaviconService");
|
||||
|
||||
// nsIURI -> imgIContainer
|
||||
function _imageFromURI(uri) {
|
||||
let channel = ioSvc.newChannelFromURI(uri);
|
||||
|
||||
let out_img = { value: null };
|
||||
let inputStream = channel.open();
|
||||
try {
|
||||
imgTools.decodeImageData(inputStream, channel.contentType, out_img);
|
||||
return out_img.value;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// string -> imgIContainer
|
||||
function _imageFromURL(url) {
|
||||
return _imageFromURI(NetUtil.newURI(url));
|
||||
}
|
||||
|
||||
// string? -> imgIContainer
|
||||
function getFaviconAsImage(iconurl) {
|
||||
return (iconurl ? _imageFromURL(iconurl) : false) ||
|
||||
_imageFromURI(faviconSvc.defaultFavicon);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//// PreviewController
|
||||
|
||||
/*
|
||||
* This class manages the behavior of the preview.
|
||||
*
|
||||
* To give greater performance when drawing, the dirty areas of the content
|
||||
* window are tracked and drawn on demand into a canvas of the same size.
|
||||
* This provides a great increase in responsiveness when drawing a preview
|
||||
* for unchanged (or even only slightly changed) tabs.
|
||||
*
|
||||
* @param win
|
||||
* The TabWindow (see below) that owns the preview that this controls
|
||||
* @param tab
|
||||
* The <tab> that this preview is associated with
|
||||
*/
|
||||
function PreviewController(win, tab) {
|
||||
this.win = win;
|
||||
this.tab = tab;
|
||||
this.linkedBrowser = tab.linkedBrowser;
|
||||
|
||||
this.linkedBrowser.addEventListener("MozAfterPaint", this, false);
|
||||
this.linkedBrowser.addEventListener("DOMTitleChanged", this, false);
|
||||
// pageshow is needed for when a tab is dragged across windows.
|
||||
this.linkedBrowser.addEventListener("pageshow", this, false);
|
||||
|
||||
// Cannot perform the lookup during construction. See TabWindow.newTab
|
||||
XPCOMUtils.defineLazyGetter(this, "preview", function () this.win.previewFromTab(this.tab));
|
||||
|
||||
XPCOMUtils.defineLazyGetter(this, "canvasPreview", function ()
|
||||
this.win.win.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"));
|
||||
|
||||
XPCOMUtils.defineLazyGetter(this, "dirtyRegion",
|
||||
function () {
|
||||
let dirtyRegion = Cc["@mozilla.org/gfx/region;1"]
|
||||
.createInstance(Ci.nsIScriptableRegion);
|
||||
dirtyRegion.init();
|
||||
return dirtyRegion;
|
||||
});
|
||||
}
|
||||
|
||||
PreviewController.prototype = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsITaskbarPreviewController,
|
||||
Ci.nsIDOMEventListener]),
|
||||
destroy: function () {
|
||||
this.linkedBrowser.removeEventListener("pageshow", this, false);
|
||||
this.linkedBrowser.removeEventListener("DOMTitleChanged", this, false);
|
||||
this.linkedBrowser.removeEventListener("MozAfterPaint", this, false);
|
||||
},
|
||||
get wrappedJSObject() {
|
||||
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.
|
||||
// updateCanvasPreview() will detect the size mismatch as a resize event
|
||||
// the next time it is called.
|
||||
resetCanvasPreview: function () {
|
||||
this.canvasPreview.width = 0;
|
||||
this.canvasPreview.height = 0;
|
||||
},
|
||||
|
||||
// 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, width:bx.width, height:bx.height});
|
||||
this.canvasPreview.width = bx.width;
|
||||
this.canvasPreview.height = bx.height;
|
||||
}
|
||||
|
||||
// Draw dirty regions
|
||||
let ctx = this.canvasPreview.getContext("2d");
|
||||
let flags = this.canvasPreviewFlags;
|
||||
// width/height are occasionally bogus and too large for drawWindow
|
||||
// so we clip to the canvas region
|
||||
this.dirtyRegion.intersectRect(0, 0, bx.width, bx.height);
|
||||
for each (let r in this.dirtyRects) {
|
||||
let x = r.x;
|
||||
let y = r.y;
|
||||
let width = r.width;
|
||||
let height = r.height;
|
||||
ctx.save();
|
||||
ctx.translate(x, y);
|
||||
ctx.drawWindow(win, x, y, width, height, "white", flags);
|
||||
ctx.restore();
|
||||
}
|
||||
this.dirtyRegion.setToRect(0,0,0,0);
|
||||
|
||||
// If we're updating the canvas, then we're in the middle of a peek so
|
||||
// don't discard the cache of previews.
|
||||
AeroPeek.resetCacheTimer();
|
||||
},
|
||||
|
||||
onTabPaint: function (rect) {
|
||||
// Ignore spurious dirty rects
|
||||
if (!rect.width || !rect.height)
|
||||
return;
|
||||
|
||||
let r = { x: Math.floor(rect.left),
|
||||
y: Math.floor(rect.top),
|
||||
width: Math.ceil(rect.width),
|
||||
height: Math.ceil(rect.height)
|
||||
};
|
||||
this.dirtyRegion.unionRect(r.x, r.y, r.width, r.height);
|
||||
},
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//// nsITaskbarPreviewController
|
||||
|
||||
get width() {
|
||||
return this.win.width;
|
||||
},
|
||||
|
||||
get height() {
|
||||
return this.win.height;
|
||||
},
|
||||
|
||||
get thumbnailAspectRatio() {
|
||||
let boxObject = this.tab.linkedBrowser.boxObject;
|
||||
// Avoid returning 0
|
||||
let tabWidth = boxObject.width || 1;
|
||||
// Avoid divide by 0
|
||||
let tabHeight = boxObject.height || 1;
|
||||
return tabWidth / tabHeight;
|
||||
},
|
||||
|
||||
drawPreview: function (ctx) {
|
||||
let self = this;
|
||||
this.win.tabbrowser.previewTab(this.tab, function () self.previewTabCallback(ctx));
|
||||
|
||||
// We want a frame drawn around the preview
|
||||
return true;
|
||||
},
|
||||
|
||||
previewTabCallback: function (ctx) {
|
||||
let width = this.win.width;
|
||||
let height = this.win.height;
|
||||
// Draw our toplevel window
|
||||
ctx.drawWindow(this.win.win, 0, 0, width, height, "transparent");
|
||||
|
||||
// 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);
|
||||
},
|
||||
|
||||
drawThumbnail: function (ctx, width, height) {
|
||||
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 () {
|
||||
this.win.tabbrowser.removeTab(this.tab);
|
||||
},
|
||||
|
||||
onActivate: function () {
|
||||
this.win.tabbrowser.selectedTab = this.tab;
|
||||
|
||||
// Accept activation - this will restore the browser window
|
||||
// if it's minimized
|
||||
return true;
|
||||
},
|
||||
|
||||
//// nsIDOMEventListener
|
||||
handleEvent: function (evt) {
|
||||
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 "pageshow":
|
||||
case "DOMTitleChanged":
|
||||
// The tab's label is sometimes empty when dragging tabs between windows
|
||||
// so we force the tab title to be updated (see bug 520579)
|
||||
this.win.tabbrowser.setTabTitle(this.tab);
|
||||
let title = this.tab.label;
|
||||
this.preview.title = title;
|
||||
this.preview.tooltip = title;
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
XPCOMUtils.defineLazyGetter(PreviewController.prototype, "canvasPreviewFlags",
|
||||
function () { let canvasInterface = Ci.nsIDOMCanvasRenderingContext2D;
|
||||
return canvasInterface.DRAWWINDOW_DRAW_VIEW
|
||||
| canvasInterface.DRAWWINDOW_DRAW_CARET
|
||||
| canvasInterface.DRAWWINDOW_DO_NOT_FLUSH;
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//// TabWindow
|
||||
|
||||
/*
|
||||
* This class monitors a browser window for changes to its tabs
|
||||
*
|
||||
* @param win
|
||||
* The nsIDOMWindow browser window
|
||||
*/
|
||||
function TabWindow(win) {
|
||||
this.win = win;
|
||||
this.tabbrowser = win.gBrowser;
|
||||
|
||||
this.previews = [];
|
||||
|
||||
for each (let evtName in this.events)
|
||||
this.tabbrowser.tabContainer.addEventListener(evtName, this, false);
|
||||
this.tabbrowser.addTabsProgressListener(this);
|
||||
|
||||
|
||||
AeroPeek.windows.push(this);
|
||||
let tabs = this.tabbrowser.mTabs;
|
||||
for (let i = 0; i < tabs.length; i++)
|
||||
this.newTab(tabs[i]);
|
||||
|
||||
this.updateTabOrdering();
|
||||
AeroPeek.checkPreviewCount();
|
||||
}
|
||||
|
||||
TabWindow.prototype = {
|
||||
_enabled: false,
|
||||
events: ["TabOpen", "TabClose", "TabSelect", "TabMove"],
|
||||
|
||||
destroy: function () {
|
||||
let tabs = this.tabbrowser.mTabs;
|
||||
|
||||
for (let i = 0; i < tabs.length; i++)
|
||||
this.removeTab(tabs[i]);
|
||||
|
||||
let idx = AeroPeek.windows.indexOf(win.gTaskbarTabGroup);
|
||||
AeroPeek.windows.splice(idx, 1);
|
||||
AeroPeek.checkPreviewCount();
|
||||
|
||||
for each (let evtName in this.events)
|
||||
this.tabbrowser.tabcontainer.removeEventListener(evtName, this, false);
|
||||
},
|
||||
|
||||
get width () {
|
||||
return this.win.innerWidth;
|
||||
},
|
||||
get height () {
|
||||
return this.win.innerHeight;
|
||||
},
|
||||
|
||||
// Invoked when the given tab is added to this window
|
||||
newTab: function (tab) {
|
||||
let controller = new PreviewController(this, tab);
|
||||
let preview = AeroPeek.taskbar.createTaskbarTabPreview(this.tabbrowser.docShell, controller);
|
||||
preview.title = tab.label;
|
||||
preview.tooltip = tab.label;
|
||||
preview.visible = AeroPeek.enabled;
|
||||
preview.active = this.tabbrowser.selectedTab == tab;
|
||||
// Grab the default favicon
|
||||
preview.icon = getFaviconAsImage(null);
|
||||
|
||||
this.previews.splice(tab._tPos, 0, preview);
|
||||
AeroPeek.addPreview(preview);
|
||||
},
|
||||
|
||||
// Invoked when the given tab is closed
|
||||
removeTab: function (tab) {
|
||||
let preview = this.previewFromTab(tab);
|
||||
preview.active = false;
|
||||
preview.visible = false;
|
||||
preview.move(null);
|
||||
preview.controller.wrappedJSObject.destroy();
|
||||
|
||||
|
||||
this.previews.splice(tab._tPos, 1);
|
||||
AeroPeek.removePreview(preview);
|
||||
},
|
||||
|
||||
get enabled () {
|
||||
return this._enabled;
|
||||
},
|
||||
|
||||
set enabled(enable) {
|
||||
this._enabled = enable;
|
||||
// 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
|
||||
// at once.
|
||||
for each (let preview in this.previews) {
|
||||
preview.move(null);
|
||||
preview.visible = enable;
|
||||
}
|
||||
this.updateTabOrdering();
|
||||
},
|
||||
|
||||
previewFromTab: function (tab) {
|
||||
return this.previews[tab._tPos];
|
||||
},
|
||||
|
||||
updateTabOrdering: function () {
|
||||
for (let i = 0; i < this.previews.length; i++) {
|
||||
let p = this.previews[i];
|
||||
let next = i == this.previews.length - 1 ? null : this.previews[i+1];
|
||||
p.move(next);
|
||||
}
|
||||
},
|
||||
|
||||
//// nsIDOMEventListener
|
||||
handleEvent: function (evt) {
|
||||
let tab = evt.originalTarget;
|
||||
switch (evt.type) {
|
||||
case "TabOpen":
|
||||
this.newTab(tab);
|
||||
this.updateTabOrdering();
|
||||
break;
|
||||
case "TabClose":
|
||||
this.removeTab(tab);
|
||||
this.updateTabOrdering();
|
||||
break;
|
||||
case "TabSelect":
|
||||
this.previewFromTab(tab).active = true;
|
||||
break;
|
||||
case "TabMove":
|
||||
let oldPos = evt.detail;
|
||||
let newPos = tab._tPos;
|
||||
let preview = this.previews[oldPos];
|
||||
this.previews.splice(oldPos, 1);
|
||||
this.previews.splice(newPos, 0, preview);
|
||||
this.updateTabOrdering();
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
//// Browser progress listener
|
||||
onLocationChange: function () {
|
||||
},
|
||||
onProgressChange: function () {
|
||||
},
|
||||
onSecurityChange: function () {
|
||||
},
|
||||
onStateChange: function () {
|
||||
},
|
||||
onStatusChange: function () {
|
||||
},
|
||||
onLinkIconAvailable: function (aBrowser) {
|
||||
let img = getFaviconAsImage(aBrowser.mIconURL);
|
||||
for each (let tab in this.tabbrowser.mTabs)
|
||||
if (tab.linkedBrowser == aBrowser)
|
||||
this.previewFromTab(tab).icon = img;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//// AeroPeek
|
||||
|
||||
/*
|
||||
* This object acts as global storage and external interface for this feature.
|
||||
* It maintains the values of the prefs.
|
||||
*/
|
||||
var AeroPeek = {
|
||||
available: false,
|
||||
// Does the pref say we're enabled?
|
||||
_prefenabled: true,
|
||||
|
||||
_enabled: true,
|
||||
|
||||
// nsITaskbarTabPreview array
|
||||
previews: [],
|
||||
|
||||
// TabWindow array
|
||||
windows: [],
|
||||
|
||||
// nsIWinTaskbar service
|
||||
taskbar: null,
|
||||
|
||||
// Maximum number of previews
|
||||
maxpreviews: 20,
|
||||
|
||||
// Length of time in seconds that previews are cached
|
||||
cacheLifespan: 20,
|
||||
|
||||
initialize: function () {
|
||||
if (!(WINTASKBAR_CONTRACTID in Cc))
|
||||
return;
|
||||
this.taskbar = Cc[WINTASKBAR_CONTRACTID].getService(Ci.nsIWinTaskbar);
|
||||
this.available = this.taskbar.available;
|
||||
if (!this.available)
|
||||
return;
|
||||
|
||||
this.prefs.addObserver(TOGGLE_PREF_NAME, this, false);
|
||||
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);
|
||||
},
|
||||
|
||||
get enabled() {
|
||||
return this._enabled;
|
||||
},
|
||||
|
||||
set enabled(enable) {
|
||||
if (this._enabled == enable) return;
|
||||
this._enabled = enable;
|
||||
|
||||
for each (let win in this.windows)
|
||||
win.enabled = enable;
|
||||
},
|
||||
|
||||
addPreview: function (preview) {
|
||||
this.previews.push(preview);
|
||||
this.checkPreviewCount();
|
||||
},
|
||||
|
||||
removePreview: function (preview) {
|
||||
let idx = this.previews.indexOf(preview);
|
||||
this.previews.splice(idx, 1);
|
||||
this.checkPreviewCount();
|
||||
},
|
||||
|
||||
checkPreviewCount: function () {
|
||||
if (this.previews.length > this.maxpreviews)
|
||||
this.enabled = false;
|
||||
else
|
||||
this.enabled = this._prefenabled;
|
||||
},
|
||||
|
||||
onOpenWindow: function (win) {
|
||||
// This occurs when the taskbar service is not available (xp, vista)
|
||||
if (!this.available)
|
||||
return;
|
||||
|
||||
win.gTaskbarTabGroup = new TabWindow(win);
|
||||
},
|
||||
|
||||
onCloseWindow: function (win) {
|
||||
// This occurs when the taskbar service is not available (xp, vista)
|
||||
if (!this.available)
|
||||
return;
|
||||
|
||||
win.gTaskbarTabGroup.destroy();
|
||||
win.gTaskbarTabGroup = null;
|
||||
},
|
||||
|
||||
resetCacheTimer: function () {
|
||||
this.cacheTimer.cancel();
|
||||
this.cacheTimer.init(this, 1000*this.cacheLifespan, Ci.nsITimer.TYPE_ONE_SHOT);
|
||||
},
|
||||
|
||||
//// nsIObserver
|
||||
observe: function (aSubject, aTopic, aData) {
|
||||
switch (aTopic) {
|
||||
case "nsPref:changed":
|
||||
if (aData == CACHE_EXPIRATION_TIME_PREF_NAME) {
|
||||
break;
|
||||
}
|
||||
if (aData == TOGGLE_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);
|
||||
// Might need to enable/disable ourselves
|
||||
this.checkPreviewCount();
|
||||
break;
|
||||
case "timer-callback":
|
||||
for each (let preview in this.previews) {
|
||||
let controller = preview.controller.wrappedJSObject;
|
||||
controller.resetCanvasPreview();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
XPCOMUtils.defineLazyGetter(AeroPeek, "cacheTimer", function ()
|
||||
Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer)
|
||||
);
|
||||
|
||||
XPCOMUtils.defineLazyServiceGetter(AeroPeek, "prefs",
|
||||
"@mozilla.org/preferences-service;1",
|
||||
"nsIPrefBranch2");
|
||||
|
||||
AeroPeek.initialize();
|
|
@ -0,0 +1,54 @@
|
|||
#
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License Version
|
||||
# 1.1 (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
#
|
||||
# The Original Code is mozilla.org code.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Mozilla Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 2009
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Rob Arnold <robarnold@cmu.edu>
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either of the GNU General Public License Version 2 or later (the "GPL"),
|
||||
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
# in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
# of those above. If you wish to allow use of your version of this file only
|
||||
# under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
# use your version of this file under the terms of the MPL, indicate your
|
||||
# decision by deleting the provisions above and replace them with the notice
|
||||
# and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
# the provisions above, a recipient may use your version of this file under
|
||||
# the terms of any one of the MPL, the GPL or the LGPL.
|
||||
#
|
||||
# ***** END LICENSE BLOCK *****
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
relativesrcdir = browser/components/wintaskbar/test
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
||||
_BROWSER_FILES = browser_taskbar_preview.js \
|
||||
$(NULL)
|
||||
|
||||
libs:: $(_BROWSER_FILES)
|
||||
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)
|
||||
|
||||
|
|
@ -0,0 +1,137 @@
|
|||
function test() {
|
||||
waitForExplicitFinish();
|
||||
|
||||
// Cannot do anything if the taskbar service is not available
|
||||
ok(AeroPeek.available == isWin7OrHigher(), "AeroPeek initialized when it should be");
|
||||
|
||||
// Entire feature is disabled
|
||||
if (!AeroPeek.available)
|
||||
return;
|
||||
|
||||
gPrefService.setBoolPref("aeropeek.enable", true);
|
||||
|
||||
is(1, AeroPeek.windows.length, "Got the expected number of windows");
|
||||
|
||||
checkPreviews(1, "Browser starts with one preview");
|
||||
|
||||
gBrowser.addTab();
|
||||
gBrowser.addTab();
|
||||
gBrowser.addTab();
|
||||
|
||||
checkPreviews(4, "Correct number of previews after adding");
|
||||
|
||||
for each (let preview in AeroPeek.previews)
|
||||
ok(preview.visible, "Preview is shown as expected");
|
||||
|
||||
gPrefService.setBoolPref("aeropeek.enable", false);
|
||||
checkPreviews(4, "Previews are unchanged when disabling");
|
||||
|
||||
for each (let preview in AeroPeek.previews)
|
||||
ok(!preview.visible, "Preview is not shown as expected after disabling");
|
||||
|
||||
gPrefService.setBoolPref("aeropeek.enable", true);
|
||||
checkPreviews(4, "Previews are unchanged when re-enabling");
|
||||
for each (let preview in AeroPeek.previews)
|
||||
ok(preview.visible, "Preview is shown as expected after re-enabling");
|
||||
|
||||
[1,2,3,4].forEach(function (idx) {
|
||||
gBrowser.selectedTab = gBrowser.mTabs[idx];
|
||||
ok(checkSelectedTab(), "Current tab is correctly selected");
|
||||
});
|
||||
|
||||
let currentSelectedTab = gBrowser.selectedTab;
|
||||
for each (let idx in [1,2,3,4]) {
|
||||
let preview = getPreviewForTab(gBrowser.mTabs[0]);
|
||||
let canvas = createThumbnailSurface(preview);
|
||||
let ctx = canvas.getContext("2d");
|
||||
preview.controller.drawThumbnail(ctx, canvas.width, canvas.height);
|
||||
ok(currentSelectedTab == gBrowser.selectedTab, "Drawing thumbnail does not change selection");
|
||||
canvas = getCanvas(preview.controller.width, preview.controller.height);
|
||||
ctx = canvas.getContext("2d");
|
||||
preview.controller.drawPreview(ctx);
|
||||
ok(currentSelectedTab == gBrowser.selectedTab, "Drawing preview does not change selection");
|
||||
}
|
||||
|
||||
// Close #4
|
||||
getPreviewForTab(gBrowser.selectedTab).controller.onClose();
|
||||
checkPreviews(3, "Expected number of previews after closing selected tab via controller");
|
||||
ok(gBrowser.mTabs.length == 3, "Successfully closed a tab");
|
||||
|
||||
// Select #1
|
||||
ok(getPreviewForTab(gBrowser.mTabs[0]).controller.onActivate(), "Activation was accepted");
|
||||
ok(gBrowser.mTabs[0] == gBrowser.selectedTab, "Correct tab was selected");
|
||||
checkSelectedTab();
|
||||
|
||||
// Remove #3 (non active)
|
||||
gBrowser.removeTab(gBrowser.tabContainer.lastChild);
|
||||
checkPreviews(2, "Expected number of previews after closing unselected via browser");
|
||||
|
||||
// Remove #1 (active)
|
||||
gBrowser.removeTab(gBrowser.tabContainer.firstChild);
|
||||
checkPreviews(1, "Expected number of previews after closing selected tab via browser");
|
||||
|
||||
// Add a new tab
|
||||
gBrowser.addTab();
|
||||
checkPreviews(2);
|
||||
// Check default selection
|
||||
checkSelectedTab();
|
||||
|
||||
// Change selection
|
||||
gBrowser.selectedTab = gBrowser.mTabs[0];
|
||||
checkSelectedTab();
|
||||
// Close nonselected tab via controller
|
||||
getPreviewForTab(gBrowser.mTabs[1]).controller.onClose();
|
||||
checkPreviews(1);
|
||||
|
||||
gPrefService.clearUserPref("aeropeek.enable");
|
||||
|
||||
finish();
|
||||
|
||||
function checkPreviews(aPreviews, msg) {
|
||||
let nPreviews = AeroPeek.previews.length;
|
||||
is(aPreviews, gBrowser.mTabs.length, "Browser has expected number of tabs");
|
||||
is(nPreviews, gBrowser.mTabs.length, "Browser has one preview per tab");
|
||||
is(nPreviews, aPreviews, msg || "Got expected number of previews");
|
||||
}
|
||||
|
||||
function isWin7OrHigher() {
|
||||
try {
|
||||
var sysInfo = Cc["@mozilla.org/system-info;1"].
|
||||
getService(Ci.nsIPropertyBag2);
|
||||
var ver = parseFloat(sysInfo.getProperty("version"));
|
||||
if (ver >= 6.1)
|
||||
return true;
|
||||
} catch (ex) { }
|
||||
return false;
|
||||
}
|
||||
function getPreviewForTab(tab)
|
||||
window.gTaskbarTabGroup.previewFromTab(tab);
|
||||
|
||||
function checkSelectedTab()
|
||||
getPreviewForTab(gBrowser.selectedTab).active;
|
||||
|
||||
function isTabSelected(idx)
|
||||
gBrowser.selectedTab == gBrowser.mTabs[idx];
|
||||
|
||||
function createThumbnailSurface(p) {
|
||||
let thumbnailWidth = 200,
|
||||
thumbnailHeight = 120;
|
||||
let ratio = p.controller.thumbnailAspectRatio;
|
||||
|
||||
if (thumbnailWidth/thumbnailHeight > ratio)
|
||||
thumbnailWidth = thumbnailHeight * ratio;
|
||||
else
|
||||
thumbnailHeight = thumbnailWidth / ratio;
|
||||
|
||||
return getCanvas(thumbnailWidth, thumbnailHeight);
|
||||
}
|
||||
|
||||
function getCanvas(width, height) {
|
||||
let win = window.QueryInterface(Ci.nsIDOMWindowInternal);
|
||||
let doc = win.document;
|
||||
let canvas = doc.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
return canvas;
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче