2008-07-09 11:30:32 -07:00
|
|
|
/* ***** 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 Spatial Navigation.
|
|
|
|
*
|
2009-12-21 13:50:30 -08:00
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation
|
2008-07-09 11:30:32 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Doug Turner <dougt@meer.net> (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 ***** */
|
|
|
|
|
|
|
|
/**
|
2009-02-24 13:36:19 -08:00
|
|
|
*
|
2008-07-09 11:30:32 -07:00
|
|
|
* Import this module through
|
|
|
|
*
|
|
|
|
* Components.utils.import("resource://gre/modules/SpatialNavigation.js");
|
|
|
|
*
|
2008-08-05 16:31:06 -07:00
|
|
|
* Usage: (Literal class)
|
2008-07-09 11:30:32 -07:00
|
|
|
*
|
2008-08-05 16:31:06 -07:00
|
|
|
* SpatialNavigation(browser_element, optional_callback);
|
2008-07-09 11:30:32 -07:00
|
|
|
*
|
|
|
|
* optional_callback will be called when a new element is focused.
|
|
|
|
*
|
|
|
|
* function optional_callback(element) {}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["SpatialNavigation"];
|
|
|
|
|
2008-08-05 15:44:53 -07:00
|
|
|
var SpatialNavigation = {
|
2008-07-09 11:30:32 -07:00
|
|
|
|
2008-08-05 15:44:53 -07:00
|
|
|
init: function(browser, callback) {
|
|
|
|
browser.addEventListener("keypress", function (event) { _onInputKeyPress(event, callback) }, true);
|
|
|
|
},
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-08-05 15:44:53 -07:00
|
|
|
uninit: function() {
|
|
|
|
}
|
2008-07-09 11:30:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Private stuff
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
function dump(msg)
|
|
|
|
{
|
2008-07-23 10:47:31 -07:00
|
|
|
var console = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
|
|
|
|
console.logStringMessage("*** SNAV: " + msg);
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var gDirectionalBias = 10;
|
|
|
|
var gRectFudge = 1;
|
|
|
|
|
2008-10-10 18:24:56 -07:00
|
|
|
// modifier values
|
|
|
|
const kAlt = "alt";
|
|
|
|
const kShift = "shift";
|
|
|
|
const kCtrl = "ctrl";
|
|
|
|
const kNone = "none";
|
|
|
|
|
2008-07-09 11:30:32 -07:00
|
|
|
function _onInputKeyPress (event, callback) {
|
2008-07-23 10:47:31 -07:00
|
|
|
|
2009-06-30 13:35:53 -07:00
|
|
|
// If it isn't enabled, bail.
|
|
|
|
if (!PrefObserver['enabled'])
|
|
|
|
return;
|
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
// Use whatever key value is available (either keyCode or charCode).
|
|
|
|
// It might be useful for addons or whoever wants to set different
|
|
|
|
// key to be used here (e.g. "a", "F1", "arrowUp", ...).
|
|
|
|
var key = event.which || event.keyCode;
|
|
|
|
|
|
|
|
if (key != PrefObserver['keyCodeDown'] &&
|
|
|
|
key != PrefObserver['keyCodeRight'] &&
|
|
|
|
key != PrefObserver['keyCodeUp'] &&
|
|
|
|
key != PrefObserver['keyCodeLeft'])
|
2008-10-10 18:24:56 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// If it is not using the modifiers it should, bail.
|
|
|
|
if (!event.altKey && PrefObserver['modifierAlt'])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!event.shiftKey && PrefObserver['modifierShift'])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!event.crtlKey && PrefObserver['modifierCtrl'])
|
2008-07-23 10:47:31 -07:00
|
|
|
return;
|
|
|
|
|
2009-06-30 13:35:53 -07:00
|
|
|
// In some special cases where charCode is equal to one of the default arrow keyCodes we
|
|
|
|
// should bail.
|
|
|
|
if (!event.keyCode &&
|
|
|
|
(key == Ci.nsIDOMKeyEvent.DOM_VK_LEFT || key == Ci.nsIDOMKeyEvent.DOM_VK_DOWN ||
|
|
|
|
key == Ci.nsIDOMKeyEvent.DOM_VK_RIGHT || key == Ci.nsIDOMKeyEvent.DOM_VK_UP))
|
|
|
|
return;
|
|
|
|
|
2008-08-05 15:44:53 -07:00
|
|
|
var target = event.target;
|
2008-10-10 18:24:56 -07:00
|
|
|
|
|
|
|
var doc = target.ownerDocument;
|
|
|
|
|
|
|
|
// If it is XUL content (e.g. about:config), bail.
|
|
|
|
if (!PrefObserver['xulContentEnabled'] && doc instanceof Ci.nsIDOMXULDocument)
|
|
|
|
return ;
|
|
|
|
|
2009-02-24 13:36:19 -08:00
|
|
|
// check to see if we are in a textarea or text input element, and if so,
|
2008-10-10 18:24:56 -07:00
|
|
|
// ensure that we let the arrow keys work properly.
|
2008-08-05 15:44:53 -07:00
|
|
|
if (target instanceof Ci.nsIDOMHTMLHtmlElement) {
|
2008-12-05 09:07:23 -08:00
|
|
|
_focusNextUsingCmdDispatcher(key, callback);
|
2008-08-05 15:44:53 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-04 19:40:18 -07:00
|
|
|
if ((target instanceof Ci.nsIDOMHTMLInputElement &&
|
2010-05-12 00:17:07 -07:00
|
|
|
target.mozIsTextField(false)) ||
|
|
|
|
target instanceof Ci.nsIDOMHTMLTextAreaElement) {
|
2009-02-24 13:36:19 -08:00
|
|
|
|
|
|
|
// if there is any selection at all, just ignore
|
2008-07-23 10:47:31 -07:00
|
|
|
if (target.selectionEnd - target.selectionStart > 0)
|
|
|
|
return;
|
2009-02-24 13:36:19 -08:00
|
|
|
|
|
|
|
// if there is no text, there is nothing special to do.
|
2008-07-23 10:47:31 -07:00
|
|
|
if (target.textLength > 0) {
|
2009-02-24 13:36:19 -08:00
|
|
|
if (key == PrefObserver['keyCodeRight'] ||
|
|
|
|
key == PrefObserver['keyCodeDown'] ) {
|
|
|
|
// we are moving forward into the document
|
|
|
|
if (target.textLength != target.selectionEnd)
|
2009-01-22 08:25:35 -08:00
|
|
|
return;
|
2009-01-22 07:07:27 -08:00
|
|
|
}
|
2009-02-24 13:36:19 -08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// we are at the start of the text, okay to move
|
|
|
|
if (target.selectionStart != 0)
|
|
|
|
return;
|
2009-02-23 14:07:43 -08:00
|
|
|
}
|
2008-07-23 10:47:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if we are in a select
|
|
|
|
if (target instanceof Ci.nsIDOMHTMLSelectElement)
|
|
|
|
{
|
2008-12-05 09:07:23 -08:00
|
|
|
if (key == PrefObserver['keyCodeDown']) {
|
2008-07-23 10:47:31 -07:00
|
|
|
if (target.selectedIndex + 1 < target.length)
|
2008-07-16 14:54:50 -07:00
|
|
|
return;
|
2008-07-23 10:47:31 -07:00
|
|
|
}
|
2008-10-10 18:24:56 -07:00
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
if (key == PrefObserver['keyCodeUp']) {
|
2008-07-23 10:47:31 -07:00
|
|
|
if (target.selectedIndex > 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function snavfilter(node) {
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (node instanceof Ci.nsIDOMHTMLLinkElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLAnchorElement) {
|
|
|
|
// if a anchor doesn't have a href, don't target it.
|
|
|
|
if (node.href == "")
|
2008-07-09 11:30:32 -07:00
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_SKIP;
|
2008-07-23 10:47:31 -07:00
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_ACCEPT;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if ((node instanceof Ci.nsIDOMHTMLButtonElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLInputElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLLinkElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLOptGroupElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLSelectElement ||
|
|
|
|
node instanceof Ci.nsIDOMHTMLTextAreaElement) &&
|
|
|
|
node.disabled == false)
|
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_ACCEPT;
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_SKIP;
|
|
|
|
}
|
2008-10-10 18:24:56 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var bestElementToFocus = null;
|
|
|
|
var distanceToBestElement = Infinity;
|
|
|
|
var focusedRect = _inflateRect(target.getBoundingClientRect(),
|
|
|
|
- gRectFudge);
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var treeWalker = doc.createTreeWalker(doc, Ci.nsIDOMNodeFilter.SHOW_ELEMENT, snavfilter, false);
|
|
|
|
var nextNode;
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
while ((nextNode = treeWalker.nextNode())) {
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (nextNode == target)
|
|
|
|
continue;
|
2008-07-16 10:00:27 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var nextRect = _inflateRect(nextNode.getBoundingClientRect(),
|
|
|
|
- gRectFudge);
|
2008-12-05 09:07:23 -08:00
|
|
|
|
|
|
|
if (! _isRectInDirection(key, focusedRect, nextRect))
|
2008-07-23 10:47:31 -07:00
|
|
|
continue;
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
var distance = _spatialDistance(key, focusedRect, nextRect);
|
2008-08-05 15:44:53 -07:00
|
|
|
|
|
|
|
//dump("looking at: " + nextNode + " " + distance);
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (distance <= distanceToBestElement && distance > 0) {
|
|
|
|
distanceToBestElement = distance;
|
|
|
|
bestElementToFocus = nextNode;
|
|
|
|
}
|
|
|
|
}
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (bestElementToFocus != null) {
|
2008-08-05 15:44:53 -07:00
|
|
|
//dump("focusing element " + bestElementToFocus.nodeName + " " + bestElementToFocus) + "id=" + bestElementToFocus.getAttribute("id");
|
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// Wishing we could do element.focus()
|
|
|
|
doc.defaultView.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).focus(bestElementToFocus);
|
2008-07-16 14:54:50 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// if it is a text element, select all.
|
2010-08-04 19:40:18 -07:00
|
|
|
if ((bestElementToFocus instanceof Ci.nsIDOMHTMLInputElement &&
|
2010-05-12 00:17:07 -07:00
|
|
|
bestElementToFocus.mozIsTextField(false)) ||
|
|
|
|
bestElementToFocus instanceof Ci.nsIDOMHTMLTextAreaElement) {
|
2008-07-23 10:47:31 -07:00
|
|
|
bestElementToFocus.selectionStart = 0;
|
|
|
|
bestElementToFocus.selectionEnd = bestElementToFocus.textLength;
|
2008-07-16 14:54:50 -07:00
|
|
|
}
|
2008-07-23 10:47:31 -07:00
|
|
|
|
|
|
|
if (callback != undefined)
|
|
|
|
callback(bestElementToFocus);
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
} else {
|
|
|
|
// couldn't find anything. just advance and hope.
|
2008-12-05 09:07:23 -08:00
|
|
|
_focusNextUsingCmdDispatcher(key, callback);
|
2008-08-05 15:44:53 -07:00
|
|
|
}
|
2008-12-05 09:07:23 -08:00
|
|
|
|
2008-08-05 15:44:53 -07:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
function _focusNextUsingCmdDispatcher(key, callback) {
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var windowMediator = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
|
|
|
|
var window = windowMediator.getMostRecentWindow("navigator:browser");
|
2008-08-05 15:44:53 -07:00
|
|
|
|
2008-12-05 09:11:51 -08:00
|
|
|
if (key == PrefObserver['keyCodeRight'] || key == PrefObserver['keyCodeDown']) {
|
2008-07-23 10:47:31 -07:00
|
|
|
window.document.commandDispatcher.advanceFocus();
|
2008-08-05 15:44:53 -07:00
|
|
|
} else {
|
2008-07-23 10:47:31 -07:00
|
|
|
window.document.commandDispatcher.rewindFocus();
|
2008-08-05 15:44:53 -07:00
|
|
|
}
|
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (callback != undefined)
|
|
|
|
callback(null);
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
function _isRectInDirection(key, focusedRect, anotherRect)
|
2008-07-09 11:30:32 -07:00
|
|
|
{
|
2008-12-05 09:07:23 -08:00
|
|
|
if (key == PrefObserver['keyCodeLeft']) {
|
2008-10-10 18:24:56 -07:00
|
|
|
return (anotherRect.left < focusedRect.left);
|
|
|
|
}
|
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
if (key == PrefObserver['keyCodeRight']) {
|
2008-10-10 18:24:56 -07:00
|
|
|
return (anotherRect.right > focusedRect.right);
|
|
|
|
}
|
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
if (key == PrefObserver['keyCodeUp']) {
|
2008-10-10 18:24:56 -07:00
|
|
|
return (anotherRect.top < focusedRect.top);
|
|
|
|
}
|
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
if (key == PrefObserver['keyCodeDown']) {
|
2008-10-10 18:24:56 -07:00
|
|
|
return (anotherRect.bottom > focusedRect.bottom);
|
|
|
|
}
|
2008-07-09 11:30:32 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _inflateRect(rect, value)
|
|
|
|
{
|
2008-07-23 10:47:31 -07:00
|
|
|
var newRect = new Object();
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
newRect.left = rect.left - value;
|
|
|
|
newRect.top = rect.top - value;
|
|
|
|
newRect.right = rect.right + value;
|
|
|
|
newRect.bottom = rect.bottom + value;
|
|
|
|
return newRect;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function _containsRect(a, b)
|
|
|
|
{
|
2008-07-23 10:47:31 -07:00
|
|
|
return ( (b.left <= a.right) &&
|
|
|
|
(b.right >= a.left) &&
|
|
|
|
(b.top <= a.bottom) &&
|
|
|
|
(b.bottom >= a.top) );
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
function _spatialDistance(key, a, b)
|
2008-07-09 11:30:32 -07:00
|
|
|
{
|
2008-07-23 10:47:31 -07:00
|
|
|
var inlineNavigation = false;
|
|
|
|
var mx, my, nx, ny;
|
2008-10-10 18:24:56 -07:00
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
if (key == PrefObserver['keyCodeLeft']) {
|
2008-10-10 18:24:56 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
|
|
|
// |---| |---|
|
|
|
|
// |---| |---|
|
|
|
|
//
|
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (a.top > b.bottom) {
|
|
|
|
// the b rect is above a.
|
|
|
|
mx = a.left;
|
|
|
|
my = a.top;
|
|
|
|
nx = b.right;
|
|
|
|
ny = b.bottom;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
2008-07-23 10:47:31 -07:00
|
|
|
else if (a.bottom < b.top) {
|
|
|
|
// the b rect is below a.
|
|
|
|
mx = a.left;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = b.right;
|
2009-02-24 13:36:19 -08:00
|
|
|
ny = b.top;
|
2008-07-23 10:47:31 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mx = a.left;
|
|
|
|
my = 0;
|
2008-10-10 18:24:56 -07:00
|
|
|
nx = b.right;
|
|
|
|
ny = 0;
|
2008-07-23 10:47:31 -07:00
|
|
|
}
|
2008-12-05 09:07:23 -08:00
|
|
|
} else if (key == PrefObserver['keyCodeRight']) {
|
2008-10-10 18:24:56 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
|
|
|
// |---| |---|
|
|
|
|
// |---| |---|
|
|
|
|
//
|
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (a.top > b.bottom) {
|
|
|
|
// the b rect is above a.
|
|
|
|
mx = a.right;
|
|
|
|
my = a.top;
|
|
|
|
nx = b.left;
|
|
|
|
ny = b.bottom;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
2008-07-23 10:47:31 -07:00
|
|
|
else if (a.bottom < b.top) {
|
|
|
|
// the b rect is below a.
|
|
|
|
mx = a.right;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = b.left;
|
2009-02-24 13:36:19 -08:00
|
|
|
ny = b.top;
|
2008-07-23 10:47:31 -07:00
|
|
|
} else {
|
|
|
|
mx = a.right;
|
|
|
|
my = 0;
|
2008-10-10 18:24:56 -07:00
|
|
|
nx = b.left;
|
|
|
|
ny = 0;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
2008-12-05 09:07:23 -08:00
|
|
|
} else if (key == PrefObserver['keyCodeUp']) {
|
2008-10-10 18:24:56 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// |---| |---| |---|
|
|
|
|
// |---| |---| |---|
|
|
|
|
//
|
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (a.left > b.right) {
|
|
|
|
// the b rect is to the left of a.
|
|
|
|
mx = a.left;
|
|
|
|
my = a.top;
|
|
|
|
nx = b.right;
|
|
|
|
ny = b.bottom;
|
|
|
|
} else if (a.right < b.left) {
|
|
|
|
// the b rect is to the right of a
|
|
|
|
mx = a.right;
|
|
|
|
my = a.top;
|
|
|
|
nx = b.left;
|
2009-02-24 13:36:19 -08:00
|
|
|
ny = b.bottom;
|
2008-07-23 10:47:31 -07:00
|
|
|
} else {
|
|
|
|
// both b and a share some common x's.
|
|
|
|
mx = 0;
|
|
|
|
my = a.top;
|
|
|
|
nx = 0;
|
|
|
|
ny = b.bottom;
|
|
|
|
}
|
2008-12-05 09:07:23 -08:00
|
|
|
} else if (key == PrefObserver['keyCodeDown']) {
|
2008-10-10 18:24:56 -07:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// |---|
|
|
|
|
// |---|
|
|
|
|
//
|
|
|
|
// |---| |---| |---|
|
|
|
|
// |---| |---| |---|
|
|
|
|
//
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
if (a.left > b.right) {
|
|
|
|
// the b rect is to the left of a.
|
|
|
|
mx = a.left;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = b.right;
|
|
|
|
ny = b.top;
|
|
|
|
} else if (a.right < b.left) {
|
|
|
|
// the b rect is to the right of a
|
|
|
|
mx = a.right;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = b.left;
|
2009-02-24 13:36:19 -08:00
|
|
|
ny = b.top;
|
2008-07-23 10:47:31 -07:00
|
|
|
} else {
|
|
|
|
// both b and a share some common x's.
|
|
|
|
mx = 0;
|
|
|
|
my = a.bottom;
|
|
|
|
nx = 0;
|
|
|
|
ny = b.top;
|
|
|
|
}
|
|
|
|
}
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var scopedRect = _inflateRect(a, gRectFudge);
|
2008-10-10 18:24:56 -07:00
|
|
|
|
2008-12-05 09:07:23 -08:00
|
|
|
if (key == PrefObserver['keyCodeLeft'] ||
|
|
|
|
key == PrefObserver['keyCodeRight']) {
|
2008-07-23 10:47:31 -07:00
|
|
|
scopedRect.left = 0;
|
|
|
|
scopedRect.right = Infinity;
|
|
|
|
inlineNavigation = _containsRect(scopedRect, b);
|
|
|
|
}
|
2008-12-05 09:07:23 -08:00
|
|
|
else if (key == PrefObserver['keyCodeUp'] ||
|
|
|
|
key == PrefObserver['keyCodeDown']) {
|
2008-07-23 10:47:31 -07:00
|
|
|
scopedRect.top = 0;
|
|
|
|
scopedRect.bottom = Infinity;
|
|
|
|
inlineNavigation = _containsRect(scopedRect, b);
|
|
|
|
}
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
var d = Math.pow((mx-nx), 2) + Math.pow((my-ny), 2);
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
// prefer elements directly aligned with the focused element
|
|
|
|
if (inlineNavigation)
|
|
|
|
d /= gDirectionalBias;
|
2009-02-24 13:36:19 -08:00
|
|
|
|
2008-07-23 10:47:31 -07:00
|
|
|
return d;
|
2008-07-09 11:30:32 -07:00
|
|
|
}
|
|
|
|
|
2008-10-10 18:24:56 -07:00
|
|
|
// Snav preference observer
|
|
|
|
|
|
|
|
PrefObserver = {
|
|
|
|
|
|
|
|
register: function()
|
|
|
|
{
|
|
|
|
this.prefService = Cc["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Ci.nsIPrefService);
|
|
|
|
|
|
|
|
this._branch = this.prefService.getBranch("snav.");
|
|
|
|
this._branch.QueryInterface(Ci.nsIPrefBranch2);
|
|
|
|
this._branch.addObserver("", this, false);
|
|
|
|
|
|
|
|
// set current or default pref values
|
|
|
|
this.observe(null, "nsPref:changed", "enabled");
|
|
|
|
this.observe(null, "nsPref:changed", "xulContentEnabled");
|
|
|
|
this.observe(null, "nsPref:changed", "keyCode.modifier");
|
|
|
|
this.observe(null, "nsPref:changed", "keyCode.right");
|
|
|
|
this.observe(null, "nsPref:changed", "keyCode.up");
|
|
|
|
this.observe(null, "nsPref:changed", "keyCode.down");
|
|
|
|
this.observe(null, "nsPref:changed", "keyCode.left");
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData)
|
|
|
|
{
|
|
|
|
if(aTopic != "nsPref:changed")
|
|
|
|
return;
|
|
|
|
|
|
|
|
// aSubject is the nsIPrefBranch we're observing (after appropriate QI)
|
|
|
|
// aData is the name of the pref that's been changed (relative to aSubject)
|
|
|
|
switch (aData) {
|
|
|
|
case "enabled":
|
|
|
|
try {
|
|
|
|
this.enabled = this._branch.getBoolPref("enabled");
|
|
|
|
} catch(e) {
|
|
|
|
this.enabled = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "xulContentEnabled":
|
|
|
|
try {
|
|
|
|
this.xulContentEnabled = this._branch.getBoolPref("xulContentEnabled");
|
|
|
|
} catch(e) {
|
|
|
|
this.xulContentEnabled = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "keyCode.modifier":
|
|
|
|
try {
|
|
|
|
this.keyCodeModifier = this._branch.getCharPref("keyCode.modifier");
|
|
|
|
|
|
|
|
// resetting modifiers
|
|
|
|
this.modifierAlt = false;
|
|
|
|
this.modifierShift = false;
|
|
|
|
this.modifierCtrl = false;
|
|
|
|
|
|
|
|
if (this.keyCodeModifier != this.kNone)
|
|
|
|
{
|
|
|
|
// use are using '+' as a separator in about:config.
|
|
|
|
var mods = this.keyCodeModifier.split(/\++/);
|
|
|
|
for (var i = 0; i < mods.length; i++) {
|
|
|
|
var mod = mods[i].toLowerCase();
|
|
|
|
if (mod == "")
|
|
|
|
continue;
|
|
|
|
else if (mod == kAlt)
|
|
|
|
this.modifierAlt = true;
|
|
|
|
else if (mod == kShift)
|
|
|
|
this.modifierShift = true;
|
|
|
|
else if (mod == kCtrl)
|
|
|
|
this.modifierCtrl = true;
|
|
|
|
else {
|
|
|
|
this.keyCodeModifier = kNone;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
this.keyCodeModifier = kNone;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "keyCode.up":
|
|
|
|
try {
|
|
|
|
this.keyCodeUp = this._branch.getIntPref("keyCode.up");
|
|
|
|
} catch(e) {
|
|
|
|
this.keyCodeUp = Ci.nsIDOMKeyEvent.DOM_VK_UP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "keyCode.down":
|
|
|
|
try {
|
|
|
|
this.keyCodeDown = this._branch.getIntPref("keyCode.down");
|
|
|
|
} catch(e) {
|
|
|
|
this.keyCodeDown = Ci.nsIDOMKeyEvent.DOM_VK_DOWN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "keyCode.left":
|
|
|
|
try {
|
|
|
|
this.keyCodeLeft = this._branch.getIntPref("keyCode.left");
|
|
|
|
} catch(e) {
|
|
|
|
this.keyCodeLeft = Ci.nsIDOMKeyEvent.DOM_VK_LEFT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "keyCode.right":
|
|
|
|
try {
|
|
|
|
this.keyCodeRight = this._branch.getIntPref("keyCode.right");
|
|
|
|
} catch(e) {
|
|
|
|
this.keyCodeRight = Ci.nsIDOMKeyEvent.DOM_VK_RIGHT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
PrefObserver.register();
|