Bug 820735 - [toolbox] Tools shortcuts should work from the toolbox window, r=paul

This commit is contained in:
Girish Sharma 2013-01-13 14:22:03 +05:30
parent 1f667ab6c7
commit efca1f88c5
3 changed files with 119 additions and 0 deletions

View File

@ -256,6 +256,7 @@ Toolbox.prototype = {
this._buildDockButtons();
this._buildTabs();
this._buildButtons();
this._addKeysToWindow();
this.selectTool(this._defaultToolId).then(function(panel) {
this.emit("ready");
@ -270,6 +271,39 @@ Toolbox.prototype = {
return deferred.promise;
},
/**
* Adds the keys and commands to the Toolbox Window in window mode.
*/
_addKeysToWindow: function TBOX__addKeysToWindow() {
if (this.hostType != Toolbox.HostType.WINDOW) {
return;
}
let doc = this.doc.defaultView.parent.document;
for (let [id, toolDefinition] of gDevTools._tools) {
if (toolDefinition.key) {
// Prevent multiple entries for the same tool.
if (doc.getElementById("key_" + id)) {
continue;
}
let key = doc.createElement("key");
key.id = "key_" + id;
if (toolDefinition.key.startsWith("VK_")) {
key.setAttribute("keycode", toolDefinition.key);
} else {
key.setAttribute("key", toolDefinition.key);
}
key.setAttribute("modifiers", toolDefinition.modifiers);
key.setAttribute("oncommand", "void(0);"); // needed. See bug 371900
key.addEventListener("command", function(toolId) {
this.selectTool(toolId);
}.bind(this, id), true);
doc.getElementById("toolbox-keyset").appendChild(key);
}
}
},
/**
* Build the buttons for changing hosts. Called every time
* the host changes.
@ -371,6 +405,8 @@ Toolbox.prototype = {
tabs.appendChild(radio);
deck.appendChild(vbox);
this._addKeysToWindow();
},
/**
@ -520,6 +556,7 @@ Toolbox.prototype = {
Services.prefs.setCharPref(this._prefs.LAST_HOST, this._host.type);
this._buildDockButtons();
this._addKeysToWindow();
this.emit("host-changed");
}.bind(this));
@ -570,6 +607,14 @@ Toolbox.prototype = {
panel.parentNode.removeChild(panel);
}
if (this.hostType == Toolbox.HostType.WINDOW) {
let doc = this.doc.defaultView.parent.document;
let key = doc.getElementById("key_" + id);
if (key) {
key.parentNode.removeChild(key);
}
}
if (this._toolPanels.has(toolId)) {
let instance = this._toolPanels.get(toolId);
instance.destroy();

View File

@ -21,6 +21,7 @@ MOCHITEST_BROWSER_FILES = \
browser_target_events.js \
browser_toolbox_tool_ready.js \
browser_toolbox_sidebar.js \
browser_toolbox_window_shortcuts.js \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,73 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
let temp = {};
Cu.import("resource:///modules/devtools/Toolbox.jsm", temp);
let Toolbox = temp.Toolbox;
temp = null;
let toolbox, toolIDs, idIndex;
function test() {
waitForExplicitFinish();
addTab("about:blank", function() {
toolIDs = [];
for (let [id, definition] of gDevTools._tools) {
if (definition.key) {
toolIDs.push(id);
}
}
let target = TargetFactory.forTab(gBrowser.selectedTab);
idIndex = 0;
gDevTools.showToolbox(target, toolIDs[0], Toolbox.HostType.WINDOW)
.then(testShortcuts);
});
}
function testShortcuts(aToolbox, aIndex) {
if (aIndex == toolIDs.length) {
tidyUp();
return;
}
toolbox = aToolbox;
info("Toolbox fired a `ready` event");
toolbox.once("select", selectCB);
if (aIndex != null) {
// This if block is to allow the call of selectCB without shortcut press for
// the first time. That happens because on opening of toolbox, one tool gets
// selected atleast.
let key = gDevTools._tools.get(toolIDs[aIndex]).key;
let toolModifiers = gDevTools._tools.get(toolIDs[aIndex]).modifiers;
let modifiers = {
accelKey: toolModifiers.contains("accel"),
altKey: toolModifiers.contains("alt"),
shiftKey: toolModifiers.contains("shift"),
};
idIndex = aIndex;
info("Testing shortcut for tool " + aIndex + ":" + toolIDs[aIndex] +
" using key " + key);
EventUtils.synthesizeKey(key, modifiers, toolbox.doc.defaultView.parent);
}
}
function selectCB(event, id) {
info("toolbox-select event from " + id);
is(toolIDs.indexOf(id), idIndex,
"Correct tool is selected on pressing the shortcut for " + id);
testShortcuts(toolbox, idIndex + 1);
}
function tidyUp() {
toolbox.destroy();
gBrowser.removeCurrentTab();
toolbox = toolIDs = idIndex = Toolbox = null;
finish();
}