Bug 1495944: Enable/Disable devtools menu items and the shortcut key. r=jdescottes

Differential Revision: https://phabricator.services.mozilla.com/D16684

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Daisuke Akatsuka 2019-01-18 10:26:53 +00:00
parent d4fcf84025
commit 6ca976f56d
2 changed files with 103 additions and 13 deletions

View File

@ -299,3 +299,51 @@ exports.removeMenus = function(doc) {
// unregistering each tool.
removeTopLevelItems(doc);
};
/**
* This is used for about:devtools-toolbox and that we are hiding the main toolbox toggle
* menu item, as well as all the tool items displayed on the menu. But we keep the
* non-toolbox menu items such as Scratchpad, Browser Console etc.
*
* @param {XULDocument} doc
* @param {boolean} isEnabled
*/
function setDevtoolsMenuItemsEnabled(doc, isEnabled) {
setMenuItemEnabled(doc, "menu_devToolbox", isEnabled);
for (const toolDefinition of gDevTools.getToolDefinitionArray()) {
if (!toolDefinition.inMenu) {
continue;
}
setMenuItemEnabled(doc, "menuitem_" + toolDefinition.id, isEnabled);
}
}
function setMenuItemEnabled(doc, menuItemId, isEnabled) {
const menuItem = doc.getElementById(menuItemId);
if (menuItem) {
if (isEnabled) {
menuItem.removeAttribute("hidden");
} else {
menuItem.setAttribute("hidden", true);
}
}
}
/**
* Enable all devtools menu items.
*
* @param {XULDocument} doc
*/
exports.enableDevtoolsMenuItems = function(doc) {
setDevtoolsMenuItemsEnabled(doc, true);
};
/**
* Disable all devtools menu items.
*
* @param {XULDocument} doc
*/
exports.disableDevtoolsMenuItems = function(doc) {
setDevtoolsMenuItemsEnabled(doc, false);
};

View File

@ -242,6 +242,16 @@ var gDevToolsBrowser = exports.gDevToolsBrowser = {
* `Cu.now()` timing.
*/
async onKeyShortcut(window, key, startTime) {
// Avoid to open devtools when the about:devtools-toolbox page is showing
// on the window now.
if (gDevToolsBrowser._isAboutDevtoolsToolbox(window) &&
(key.toolId ||
key.id === "toggleToolbox" ||
key.id === "toggleToolboxF12" ||
key.id === "inspectorMac")) {
return;
}
// If this is a toolbox's panel key shortcut, delegate to selectToolCommand
if (key.toolId) {
await gDevToolsBrowser.selectToolCommand(window.gBrowser, key.toolId, startTime);
@ -589,6 +599,10 @@ var gDevToolsBrowser = exports.gDevToolsBrowser = {
for (const win of gDevToolsBrowser._trackedBrowserWindows) {
BrowserMenus.insertToolMenuElements(win.document, toolDefinition, prevDef);
// If we are on a page where devtools menu items are hidden such as
// about:devtools-toolbox, we need to call _updateMenuItems to update the
// visibility of the newly created menu item.
gDevToolsBrowser._updateMenuItems(win);
}
if (toolDefinition.id === "jsdebugger") {
@ -607,22 +621,50 @@ var gDevToolsBrowser = exports.gDevToolsBrowser = {
},
/**
* Update the "Toggle Tools" checkbox in the developer tools menu. This is
* Update developer tools menu items and the "Toggle Tools" checkbox. This is
* called when a toolbox is created or destroyed.
*/
_updateMenuCheckbox() {
_updateMenu() {
for (const win of gDevToolsBrowser._trackedBrowserWindows) {
const hasToolbox = gDevToolsBrowser.hasToolboxOpened(win);
const menu = win.document.getElementById("menu_devToolbox");
if (hasToolbox) {
menu.setAttribute("checked", "true");
} else {
menu.removeAttribute("checked");
}
gDevToolsBrowser._updateMenuItems(win);
}
},
/**
* Update developer tools menu items and the "Toggle Tools" checkbox of XULWindow.
*
* @param {XULWindow} win
*/
_updateMenuItems(win) {
if (gDevToolsBrowser._isAboutDevtoolsToolbox(win)) {
BrowserMenus.disableDevtoolsMenuItems(win.document);
return;
}
BrowserMenus.enableDevtoolsMenuItems(win.document);
const hasToolbox = gDevToolsBrowser.hasToolboxOpened(win);
const menu = win.document.getElementById("menu_devToolbox");
if (hasToolbox) {
menu.setAttribute("checked", "true");
} else {
menu.removeAttribute("checked");
}
},
/**
* Check whether the window is showing about:devtools-toolbox page or not.
*
* @param {XULWindow} win
* @return {boolean} true: about:devtools-toolbox is showing
* false: otherwise
*/
_isAboutDevtoolsToolbox(win) {
const currentURI = win.gBrowser.currentURI;
return currentURI.scheme === "about" && currentURI.filePath === "devtools-toolbox";
},
/**
* Remove the menuitem for a tool to all open browser windows.
*
@ -675,7 +717,7 @@ var gDevToolsBrowser = exports.gDevToolsBrowser = {
handleEvent(event) {
switch (event.type) {
case "TabSelect":
gDevToolsBrowser._updateMenuCheckbox();
gDevToolsBrowser._updateMenu();
break;
case "unload":
// top-level browser window unload
@ -727,8 +769,8 @@ gDevTools.on("tool-unregistered", function(toolId) {
gDevToolsBrowser._removeToolFromWindows(toolId);
});
gDevTools.on("toolbox-ready", gDevToolsBrowser._updateMenuCheckbox);
gDevTools.on("toolbox-destroyed", gDevToolsBrowser._updateMenuCheckbox);
gDevTools.on("toolbox-ready", gDevToolsBrowser._updateMenu);
gDevTools.on("toolbox-destroyed", gDevToolsBrowser._updateMenu);
Services.obs.addObserver(gDevToolsBrowser, "quit-application");
Services.obs.addObserver(gDevToolsBrowser, "browser-delayed-startup-finished");