Bug 730434 - remove 'home' event and other non-standard event types r=fabrice

This commit is contained in:
Vivien Nicolas 2012-03-01 17:04:00 +01:00
parent 63bb9608a8
commit e3fa0a787d
4 changed files with 77 additions and 263 deletions

View File

@ -1,165 +0,0 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
/**
* Command Updater
*/
let CommandUpdater = {
/**
* Gets a controller that can handle a particular command.
* @param {string} command
* A command to locate a controller for, preferring controllers that
* show the command as enabled.
* @return {object} In this order of precedence:
* - the first controller supporting the specified command
* associated with the focused element that advertises the
* command as ENABLED.
* - the first controller supporting the specified command
* associated with the global window that advertises the
* command as ENABLED.
* - the first controller supporting the specified command
* associated with the focused element.
* - the first controller supporting the specified command
* associated with the global window.
*/
_getControllerForCommand: function(command) {
try {
let commandDispatcher = top.document.commandDispatcher;
let controller = commandDispatcher.getControllerForCommand(command);
if (controller && controller.isCommandEnabled(command))
return controller;
}
catch (e) { }
let controllerCount = window.controllers.getControllerCount();
for (let i = 0; i < controllerCount; ++i) {
let current = window.controllers.getControllerAt(i);
try {
if (current.supportsCommand(command) &&
current.isCommandEnabled(command))
return current;
}
catch (e) { }
}
return controller || window.controllers.getControllerForCommand(command);
},
/**
* Updates the state of a XUL <command> element for the specified command
* depending on its state.
* @param {string} command
* The name of the command to update the XUL <command> element for.
*/
updateCommand: function(command) {
let enabled = false;
try {
let controller = this._getControllerForCommand(command);
if (controller) {
enabled = controller.isCommandEnabled(command);
}
}
catch (ex) { }
this.enableCommand(command, enabled);
},
/**
* Updates the state of a XUL <command> element for the specified command
* depending on its state.
* @param {string} command
* The name of the command to update the XUL <command> element for.
*/
updateCommands: function(_commands) {
let commands = _commands.split(',');
for (let command in commands) {
this.updateCommand(commands[command]);
}
},
/**
* Enables or disables a XUL <command> element.
* @param {string} command
* The name of the command to enable or disable.
* @param {bool} enabled
* true if the command should be enabled, false otherwise.
*/
enableCommand: function(command, enabled) {
let element = document.getElementById(command);
if (!element)
return;
if (enabled)
element.removeAttribute('disabled');
else
element.setAttribute('disabled', 'true');
},
/**
* Performs the action associated with a specified command using the most
* relevant controller.
* @param {string} command
* The command to perform.
*/
doCommand: function(command) {
let controller = this._getControllerForCommand(command);
if (!controller)
return;
controller.doCommand(command);
},
/**
* Changes the label attribute for the specified command.
* @param {string} command
* The command to update.
* @param {string} labelAttribute
* The label value to use.
*/
setMenuValue: function(command, labelAttribute) {
let commandNode = top.document.getElementById(command);
if (commandNode) {
let label = commandNode.getAttribute(labelAttribute);
if (label)
commandNode.setAttribute('label', label);
}
},
/**
* Changes the accesskey attribute for the specified command.
* @param {string} command
* The command to update.
* @param {string} valueAttribute
* The value attribute to use.
*/
setAccessKey: function(command, valueAttribute) {
let commandNode = top.document.getElementById(command);
if (commandNode) {
let value = commandNode.getAttribute(valueAttribute);
if (value)
commandNode.setAttribute('accesskey', value);
}
},
/**
* Inform all the controllers attached to a node that an event has occurred
* (e.g. the tree controllers need to be informed of blur events so that they
* can change some of the menu items back to their default values)
* @param {node} node
* The node receiving the event.
* @param {event} event
* The event.
*/
onEvent: function(node, event) {
let numControllers = node.controllers.getControllerCount();
let controller;
for (let i = 0; i < numControllers; i++) {
controller = node.controllers.getControllerAt(i);
if (controller)
controller.onEvent(event);
}
}
};

View File

@ -33,11 +33,15 @@ XPCOMUtils.defineLazyGetter(Services, 'idle', function() {
.getService(Ci.nsIIdleService);
});
XPCOMUtils.defineLazyServiceGetter(Services, 'fm', function(){
return Cc['@mozilla.org/focus-managr;1']
.getService(Ci.nsFocusManager);
XPCOMUtils.defineLazyGetter(Services, 'audioManager', function() {
return Cc['@mozilla.org/telephony/audiomanager;1']
.getService(Ci.nsIAudioManager);
});
XPCOMUtils.defineLazyServiceGetter(Services, 'fm', function() {
return Cc['@mozilla.org/focus-manager;1']
.getService(Ci.nsFocusManager);
});
#ifndef MOZ_WIDGET_GONK
// In order to use http:// scheme instead of file:// scheme
@ -74,9 +78,6 @@ function addPermissions(urls) {
}
var shell = {
// FIXME/bug 678695: this should be a system setting
preferredScreenBrightness: 1.0,
isDebug: false,
get contentBrowser() {
@ -111,13 +112,23 @@ var shell = {
return alert(msg);
}
window.controllers.appendController(this);
window.addEventListener('keypress', this);
['keydown', 'keypress', 'keyup'].forEach((function listenKey(type) {
window.addEventListener(type, this, false, true);
window.addEventListener(type, this, true, true);
}).bind(this));
window.addEventListener('MozApplicationManifest', this);
window.addEventListener("AppCommand", this);
window.addEventListener('mozfullscreenchange', this);
this.contentBrowser.addEventListener('load', this, true);
// Until the volume can be set from the content side, set it to a
// a specific value when the device starts. This way the front-end
// can display a notification when the volume change and show a volume
// level modified from this point.
try {
Services.audioManager.masterVolume = 0.5;
} catch(e) {}
try {
Services.io.offline = false;
@ -159,35 +170,8 @@ var shell = {
},
stop: function shell_stop() {
window.controllers.removeController(this);
window.removeEventListener('keypress', this);
window.removeEventListener('MozApplicationManifest', this);
window.removeEventListener('AppCommand', this);
},
supportsCommand: function shell_supportsCommand(cmd) {
let isSupported = false;
switch (cmd) {
case 'cmd_close':
isSupported = true;
break;
default:
isSupported = false;
break;
}
return isSupported;
},
isCommandEnabled: function shell_isCommandEnabled(cmd) {
return true;
},
doCommand: function shell_doCommand(cmd) {
switch (cmd) {
case 'cmd_close':
content.postMessage('appclose', '*');
break;
}
window.removeEventListener('mozfullscreenchange', this);
},
toggleDebug: function shell_toggleDebug() {
@ -202,9 +186,7 @@ var shell = {
}
},
changeVolume: function shell_changeVolume(aDelta) {
let audioManager = Cc["@mozilla.org/telephony/audiomanager;1"].getService(Ci.nsIAudioManager);
changeVolume: function shell_changeVolume(delta) {
let steps = 10;
try {
steps = Services.prefs.getIntPref("media.volume.steps");
@ -212,7 +194,11 @@ var shell = {
steps = 1;
} catch(e) {}
let volume = audioManager.masterVolume + aDelta / steps;
let audioManager = Services.audioManager;
if (!audioManager)
return;
let volume = audioManager.masterVolume + delta / steps;
if (volume > 1)
volume = 1;
if (volume < 0)
@ -220,44 +206,58 @@ var shell = {
audioManager.masterVolume = volume;
},
forwardKeyToHomescreen: function shell_forwardKeyToHomescreen(evt) {
let generatedEvent = content.document.createEvent('KeyboardEvent');
generatedEvent.initKeyEvent(evt.type, true, true, evt.view, evt.ctrlKey,
evt.altKey, evt.shiftKey, evt.metaKey,
evt.keyCode, evt.charCode);
content.dispatchEvent(generatedEvent);
},
handleEvent: function shell_handleEvent(evt) {
switch (evt.type) {
case 'keydown':
case 'keyup':
case 'keypress':
switch (evt.keyCode) {
case evt.DOM_VK_HOME:
this.sendEvent(content, 'home');
break;
case evt.DOM_VK_SLEEP:
this.toggleScreen();
let details = {
'enabled': screen.mozEnabled
};
this.sendEvent(content, 'sleep', details);
break;
case evt.DOM_VK_ESCAPE:
if (evt.defaultPrevented)
return;
this.doCommand('cmd_close');
break;
// If the home key is pressed, always forward it to the homescreen
if (evt.eventPhase == evt.CAPTURING_PHASE) {
if (evt.keyCode == evt.VK_DOM_HOME) {
window.setTimeout(this.forwardKeyToHomescreen, 0, evt);
evt.preventDefault();
evt.stopPropagation();
}
return;
}
break;
case 'AppCommand':
switch (evt.command) {
case 'Menu':
if (Services.prefs.getBoolPref('b2g.keys.menu.enabled'))
this.sendEvent(content, 'menu');
break;
case 'Search':
if (Services.prefs.getBoolPref('b2g.keys.search.enabled'))
this.toggleDebug();
break;
case 'VolumeUp':
this.changeVolume(1);
break;
case 'VolumeDown':
this.changeVolume(-1);
break;
// If one of the other keys is used in an application and is
// cancelled via preventDefault, do nothing.
let homescreen = (evt.target.ownerDocument.defaultView == content);
if (!homescreen && evt.defaultPrevented)
return;
// If one of the other keys is used in an application and is
// not used forward it to the homescreen
if (!homescreen)
window.setTimeout(this.forwardKeyToHomescreen, 0, evt);
// For debug purposes and because some of the APIs are not yet exposed
// to the content, let's react on some of the keyup events.
if (evt.type == 'keyup') {
switch (evt.keyCode) {
case evt.DOM_VK_F5:
if (Services.prefs.getBoolPref('b2g.keys.search.enabled'))
this.toggleDebug();
break;
case evt.DOM_VK_PAGE_DOWN:
this.changeVolume(-1);
break;
case evt.DOM_VK_PAGE_UP:
this.changeVolume(1);
break;
}
}
break;
@ -270,7 +270,6 @@ var shell = {
break;
case 'load':
this.contentBrowser.removeEventListener('load', this, true);
this.turnScreenOn();
let chromeWindow = window.QueryInterface(Ci.nsIDOMChromeWindow);
chromeWindow.browserDOMWindow = new nsBrowserAccess();
@ -317,20 +316,6 @@ var shell = {
let event = content.document.createEvent('CustomEvent');
event.initCustomEvent(type, true, true, details ? details : {});
content.dispatchEvent(event);
},
toggleScreen: function shell_toggleScreen() {
if (screen.mozEnabled)
this.turnScreenOff();
else
this.turnScreenOn();
},
turnScreenOff: function shell_turnScreenOff() {
screen.mozEnabled = false;
screen.mozBrightness = 0.0;
},
turnScreenOn: function shell_turnScreenOn() {
screen.mozEnabled = true;
screen.mozBrightness = this.preferredScreenBrightness;
}
};
@ -339,7 +324,7 @@ var shell = {
observe: function(subject, topic, time) {
if (topic === "idle") {
// TODO: Check wakelock status. See bug 697132.
shell.turnScreenOff();
screen.mozEnabled = false;
}
},
}

View File

@ -15,7 +15,6 @@
onload="shell.start();"
onunload="shell.stop();">
<script type="application/javascript" src="chrome://browser/content/commandUtil.js"/>
<script type="application/javascript" src="chrome://browser/content/shell.js"/>
#ifndef MOZ_TOUCH
<script type="application/javascript" src="chrome://browser/content/touch.js"/>
@ -24,10 +23,6 @@
<script type="application/javascript" src="chrome://browser/content/httpd.js"/>
#endif
<commandset id="mainCommandSet">
<command id="cmd_close" oncommand="CommandUpdater.doCommand(this.id);"/>
</commandset>
<browser id="homescreen"
type="content-primary"
flex="1"

View File

@ -9,7 +9,6 @@ chrome.jar:
#ifndef MOZ_TOUCH
content/touch.js (content/touch.js)
#endif
content/commandUtil.js (content/commandUtil.js)
#ifndef MOZ_WIDGET_GONK
content/httpd.js (content/httpd.js)
#endif
@ -22,4 +21,4 @@ chrome.jar:
content/netError.css (content/netError.css)
content/images/errorpage-larry-black.png (content/images/errorpage-larry-black.png)
content/images/errorpage-larry-white.png (content/images/errorpage-larry-white.png)
content/images/errorpage-warning.png (content/images/errorpage-warning.png)
content/images/errorpage-warning.png (content/images/errorpage-warning.png)