2013-02-12 12:51:25 -08:00
|
|
|
/* 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/. */
|
|
|
|
|
2013-11-23 21:32:32 -08:00
|
|
|
let Ci = Components.interfaces;
|
|
|
|
let Cc = Components.classes;
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
dump("### SelectionHandler.js loaded\n");
|
|
|
|
|
|
|
|
var SelectionHandler = {
|
|
|
|
init: function init() {
|
2013-06-17 05:46:52 -07:00
|
|
|
this.type = kContentSelector;
|
|
|
|
this.snap = true;
|
2013-10-21 10:02:02 -07:00
|
|
|
this.lastYPos = this.lastXPos = null;
|
2013-02-12 12:51:25 -08:00
|
|
|
addMessageListener("Browser:SelectionStart", this);
|
2013-02-19 17:51:02 -08:00
|
|
|
addMessageListener("Browser:SelectionAttach", this);
|
2013-02-12 12:51:25 -08:00
|
|
|
addMessageListener("Browser:SelectionEnd", this);
|
|
|
|
addMessageListener("Browser:SelectionMoveStart", this);
|
|
|
|
addMessageListener("Browser:SelectionMove", this);
|
|
|
|
addMessageListener("Browser:SelectionMoveEnd", this);
|
|
|
|
addMessageListener("Browser:SelectionUpdate", this);
|
|
|
|
addMessageListener("Browser:SelectionClose", this);
|
|
|
|
addMessageListener("Browser:SelectionCopy", this);
|
|
|
|
addMessageListener("Browser:SelectionDebug", this);
|
2013-03-20 07:40:05 -07:00
|
|
|
addMessageListener("Browser:CaretAttach", this);
|
2013-03-21 05:28:19 -07:00
|
|
|
addMessageListener("Browser:CaretMove", this);
|
2013-03-20 07:40:05 -07:00
|
|
|
addMessageListener("Browser:CaretUpdate", this);
|
2013-03-22 05:16:05 -07:00
|
|
|
addMessageListener("Browser:SelectionSwitchMode", this);
|
2013-04-05 03:33:42 -07:00
|
|
|
addMessageListener("Browser:RepositionInfoRequest", this);
|
2013-04-19 08:01:00 -07:00
|
|
|
addMessageListener("Browser:SelectionHandlerPing", this);
|
2013-10-28 21:17:45 -07:00
|
|
|
addMessageListener("Browser:ResetLastPos", this);
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
shutdown: function shutdown() {
|
|
|
|
removeMessageListener("Browser:SelectionStart", this);
|
2013-02-19 17:51:02 -08:00
|
|
|
removeMessageListener("Browser:SelectionAttach", this);
|
2013-02-12 12:51:25 -08:00
|
|
|
removeMessageListener("Browser:SelectionEnd", this);
|
|
|
|
removeMessageListener("Browser:SelectionMoveStart", this);
|
|
|
|
removeMessageListener("Browser:SelectionMove", this);
|
|
|
|
removeMessageListener("Browser:SelectionMoveEnd", this);
|
|
|
|
removeMessageListener("Browser:SelectionUpdate", this);
|
|
|
|
removeMessageListener("Browser:SelectionClose", this);
|
|
|
|
removeMessageListener("Browser:SelectionCopy", this);
|
|
|
|
removeMessageListener("Browser:SelectionDebug", this);
|
2013-03-20 07:40:05 -07:00
|
|
|
removeMessageListener("Browser:CaretAttach", this);
|
2013-03-21 05:28:19 -07:00
|
|
|
removeMessageListener("Browser:CaretMove", this);
|
2013-03-20 07:40:05 -07:00
|
|
|
removeMessageListener("Browser:CaretUpdate", this);
|
2013-03-22 05:16:05 -07:00
|
|
|
removeMessageListener("Browser:SelectionSwitchMode", this);
|
2013-04-05 03:33:42 -07:00
|
|
|
removeMessageListener("Browser:RepositionInfoRequest", this);
|
2013-04-19 08:01:00 -07:00
|
|
|
removeMessageListener("Browser:SelectionHandlerPing", this);
|
2013-10-28 21:17:45 -07:00
|
|
|
removeMessageListener("Browser:ResetLastPos", this);
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
2013-06-17 05:46:52 -07:00
|
|
|
sendAsync: function sendAsync(aMsg, aJson) {
|
|
|
|
sendAsyncMessage(aMsg, aJson);
|
2013-02-27 08:27:48 -08:00
|
|
|
},
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
/*************************************************
|
|
|
|
* Browser event handlers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Selection start event handler
|
|
|
|
*/
|
2013-08-19 02:26:17 -07:00
|
|
|
_onSelectionStart: function _onSelectionStart(aJson) {
|
2013-02-12 12:51:25 -08:00
|
|
|
// Init content window information
|
2013-08-19 02:26:17 -07:00
|
|
|
if (!this._initTargetInfo(aJson.xPos, aJson.yPos)) {
|
2013-03-20 07:40:05 -07:00
|
|
|
this._onFail("failed to get target information");
|
2013-02-12 12:51:25 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-19 02:26:17 -07:00
|
|
|
// for context menu select command, which doesn't trigger
|
|
|
|
// form input focus changes.
|
|
|
|
if (aJson.setFocus && this._targetIsEditable) {
|
|
|
|
this._targetElement.focus();
|
|
|
|
}
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
// Clear any existing selection from the document
|
|
|
|
let selection = this._contentWindow.getSelection();
|
|
|
|
selection.removeAllRanges();
|
|
|
|
|
|
|
|
// Set our initial selection, aX and aY should be in client coordinates.
|
2013-08-19 02:26:17 -07:00
|
|
|
let framePoint = this._clientPointToFramePoint({ xPos: aJson.xPos, yPos: aJson.yPos });
|
2013-02-27 08:27:48 -08:00
|
|
|
if (!this._domWinUtils.selectAtPoint(framePoint.xPos, framePoint.yPos,
|
|
|
|
Ci.nsIDOMWindowUtils.SELECT_WORDNOSPACE)) {
|
2013-02-12 12:51:25 -08:00
|
|
|
this._onFail("failed to set selection at point");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the position of our selection monocles
|
2013-06-17 05:46:52 -07:00
|
|
|
this._updateSelectionUI("start", true, true);
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
2013-02-19 17:51:02 -08:00
|
|
|
_onSelectionAttach: function _onSelectionAttach(aX, aY) {
|
|
|
|
// Init content window information
|
|
|
|
if (!this._initTargetInfo(aX, aY)) {
|
|
|
|
this._onFail("failed to get frame offset");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the position of our selection monocles
|
2013-06-17 05:46:52 -07:00
|
|
|
this._updateSelectionUI("start", true, true);
|
2013-02-19 17:51:02 -08:00
|
|
|
},
|
|
|
|
|
2013-03-22 05:16:05 -07:00
|
|
|
/*
|
|
|
|
* Switch selection modes. Currently we only support switching
|
|
|
|
* from "caret" to "selection".
|
|
|
|
*/
|
|
|
|
_onSwitchMode: function _onSwitchMode(aMode, aMarker, aX, aY) {
|
|
|
|
if (aMode != "selection") {
|
|
|
|
this._onFail("unsupported mode switch");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check to be sure we are initialized
|
|
|
|
if (!this._targetElement) {
|
|
|
|
this._onFail("not initialized");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-06 13:35:28 -08:00
|
|
|
// Only use selectAtPoint for editable content and avoid that for inputs,
|
|
|
|
// as we can expand caret to selection manually more precisely. We can use
|
|
|
|
// selectAtPoint for inputs too though, but only once bug 881938 is fully
|
|
|
|
// resolved.
|
|
|
|
if(Util.isEditableContent(this._targetElement)) {
|
|
|
|
// Similar to _onSelectionStart - we need to create initial selection
|
|
|
|
// but without the initialization bits.
|
|
|
|
let framePoint = this._clientPointToFramePoint({ xPos: aX, yPos: aY });
|
|
|
|
if (!this._domWinUtils.selectAtPoint(framePoint.xPos, framePoint.yPos,
|
|
|
|
Ci.nsIDOMWindowUtils.SELECT_CHARACTER)) {
|
|
|
|
this._onFail("failed to set selection at point");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (this._targetElement.selectionStart == 0 || aMarker == "end") {
|
|
|
|
// Expand caret forward or backward depending on direction
|
|
|
|
this._targetElement.selectionEnd++;
|
|
|
|
} else {
|
|
|
|
this._targetElement.selectionStart--;
|
2013-03-22 05:16:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We bail if things get out of sync here implying we missed a message.
|
|
|
|
this._selectionMoveActive = true;
|
|
|
|
|
|
|
|
// Update the position of the selection marker that is *not*
|
|
|
|
// being dragged.
|
2013-06-17 05:46:52 -07:00
|
|
|
this._updateSelectionUI("update", aMarker == "end", aMarker == "start");
|
2013-03-22 05:16:05 -07:00
|
|
|
},
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
/*
|
|
|
|
* Selection monocle start move event handler
|
|
|
|
*/
|
|
|
|
_onSelectionMoveStart: function _onSelectionMoveStart(aMsg) {
|
|
|
|
if (!this._contentWindow) {
|
|
|
|
this._onFail("_onSelectionMoveStart was called without proper view set up");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._selectionMoveActive) {
|
|
|
|
this._onFail("mouse is already down on drag start?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We bail if things get out of sync here implying we missed a message.
|
|
|
|
this._selectionMoveActive = true;
|
|
|
|
|
2013-03-25 13:54:49 -07:00
|
|
|
if (this._targetIsEditable) {
|
|
|
|
// If we're coming out of an out-of-bounds scroll, the node the user is
|
|
|
|
// trying to drag may be hidden (the monocle will be pegged to the edge
|
|
|
|
// of the edit). Make sure the node the user wants to move is visible
|
|
|
|
// and has focus.
|
|
|
|
this._updateInputFocus(aMsg.change);
|
|
|
|
}
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
// Update the position of our selection monocles
|
2013-06-17 05:46:52 -07:00
|
|
|
this._updateSelectionUI("update", true, true);
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Selection monocle move event handler
|
|
|
|
*/
|
|
|
|
_onSelectionMove: function _onSelectionMove(aMsg) {
|
|
|
|
if (!this._contentWindow) {
|
|
|
|
this._onFail("_onSelectionMove was called without proper view set up");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._selectionMoveActive) {
|
|
|
|
this._onFail("mouse isn't down for drag move?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-05 03:41:14 -08:00
|
|
|
this._handleSelectionPoint(aMsg, false);
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Selection monocle move finished event handler
|
|
|
|
*/
|
|
|
|
_onSelectionMoveEnd: function _onSelectionMoveComplete(aMsg) {
|
|
|
|
if (!this._contentWindow) {
|
|
|
|
this._onFail("_onSelectionMove was called without proper view set up");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._selectionMoveActive) {
|
|
|
|
this._onFail("mouse isn't down for drag move?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-05 03:41:14 -08:00
|
|
|
this._handleSelectionPoint(aMsg, true);
|
2013-02-12 12:51:25 -08:00
|
|
|
this._selectionMoveActive = false;
|
|
|
|
|
|
|
|
// _handleSelectionPoint may set a scroll timer, so this must
|
|
|
|
// be reset after the last call.
|
2013-03-21 06:55:16 -07:00
|
|
|
this._clearTimers();
|
2013-02-12 12:51:25 -08:00
|
|
|
|
|
|
|
// Update the position of our selection monocles
|
2013-06-17 05:46:52 -07:00
|
|
|
this._updateSelectionUI("end", true, true);
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
2013-03-20 07:40:05 -07:00
|
|
|
/*
|
|
|
|
* _onCaretAttach - called by SelectionHelperUI when the user taps in a
|
|
|
|
* form input. Initializes SelectionHandler, updates the location of the
|
|
|
|
* caret, and messages back with current monocle position information.
|
|
|
|
*
|
|
|
|
* @param aX, aY tap location in client coordinates.
|
|
|
|
*/
|
|
|
|
_onCaretAttach: function _onCaretAttach(aX, aY) {
|
|
|
|
// Init content window information
|
|
|
|
if (!this._initTargetInfo(aX, aY)) {
|
|
|
|
this._onFail("failed to get target information");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should never happen, but we check to make sure
|
2013-07-08 13:45:04 -07:00
|
|
|
if (!this._targetIsEditable) {
|
2013-08-01 05:31:34 -07:00
|
|
|
this._onFail("Coordiates didn't find a text input element.");
|
2013-03-20 07:40:05 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locate and sanity check the caret position
|
|
|
|
let selection = this._getSelection();
|
|
|
|
if (!selection || !selection.isCollapsed) {
|
2013-08-01 05:31:34 -07:00
|
|
|
this._onFail("No selection or selection is not collapsed.");
|
2013-03-20 07:40:05 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the position of our selection monocles
|
2013-06-17 05:46:52 -07:00
|
|
|
this._updateSelectionUI("caret", false, false, true);
|
2013-03-20 07:40:05 -07:00
|
|
|
},
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
/*
|
|
|
|
* Selection copy event handler
|
|
|
|
*
|
|
|
|
* Check to see if the incoming click was on our selection rect.
|
|
|
|
* if it was, copy to the clipboard. Incoming coordinates are
|
|
|
|
* content values.
|
|
|
|
*/
|
|
|
|
_onSelectionCopy: function _onSelectionCopy(aMsg) {
|
|
|
|
let tap = {
|
2013-02-27 08:27:48 -08:00
|
|
|
xPos: aMsg.xPos,
|
|
|
|
yPos: aMsg.yPos,
|
2013-02-12 12:51:25 -08:00
|
|
|
};
|
|
|
|
|
2013-03-20 07:40:05 -07:00
|
|
|
let tapInSelection = (tap.xPos > this._cache.selection.left &&
|
|
|
|
tap.xPos < this._cache.selection.right) &&
|
|
|
|
(tap.yPos > this._cache.selection.top &&
|
|
|
|
tap.yPos < this._cache.selection.bottom);
|
2013-02-27 08:27:48 -08:00
|
|
|
// Util.dumpLn(tapInSelection,
|
2013-03-20 07:40:05 -07:00
|
|
|
// tap.xPos, tap.yPos, "|", this._cache.selection.left,
|
|
|
|
// this._cache.selection.right, this._cache.selection.top,
|
|
|
|
// this._cache.selection.bottom);
|
2013-02-12 12:51:25 -08:00
|
|
|
let success = false;
|
|
|
|
let selectedText = this._getSelectedText();
|
|
|
|
if (tapInSelection && selectedText.length) {
|
|
|
|
let clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
|
|
|
|
.getService(Ci.nsIClipboardHelper);
|
|
|
|
clipboard.copyString(selectedText, this._contentWindow.document);
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
sendSyncMessage("Content:SelectionCopied", { succeeded: success });
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Selection close event handler
|
2013-03-21 06:55:16 -07:00
|
|
|
*
|
2013-04-05 03:33:43 -07:00
|
|
|
* @param aClearSelection requests that selection be cleared.
|
2013-02-12 12:51:25 -08:00
|
|
|
*/
|
2013-04-05 03:33:43 -07:00
|
|
|
_onSelectionClose: function _onSelectionClose(aClearSelection) {
|
|
|
|
if (aClearSelection) {
|
|
|
|
this._clearSelection();
|
2013-03-21 06:55:16 -07:00
|
|
|
}
|
2013-08-01 05:31:34 -07:00
|
|
|
this.closeSelection();
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called any time SelectionHelperUI would like us to
|
|
|
|
* recalculate the selection bounds.
|
|
|
|
*/
|
2014-01-27 11:25:04 -08:00
|
|
|
_onSelectionUpdate: function _onSelectionUpdate(aMsg) {
|
2013-02-12 12:51:25 -08:00
|
|
|
if (!this._contentWindow) {
|
|
|
|
this._onFail("_onSelectionUpdate was called without proper view set up");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-27 11:25:04 -08:00
|
|
|
if (aMsg && aMsg.isInitiatedByAPZC) {
|
|
|
|
let {offset: offset} = Content.getCurrentWindowAndOffset(
|
|
|
|
this._targetCoordinates.x, this._targetCoordinates.y);
|
|
|
|
this._contentOffset = offset;
|
|
|
|
}
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
// Update the position of our selection monocles
|
2013-06-17 05:46:52 -07:00
|
|
|
this._updateSelectionUI("update", true, true);
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called if for any reason we fail during the selection
|
|
|
|
* process. Cancels the selection.
|
|
|
|
*/
|
|
|
|
_onFail: function _onFail(aDbgMessage) {
|
|
|
|
if (aDbgMessage && aDbgMessage.length > 0)
|
|
|
|
Util.dumpLn(aDbgMessage);
|
2013-06-17 05:46:52 -07:00
|
|
|
this.sendAsync("Content:SelectionFail");
|
2013-02-12 12:51:25 -08:00
|
|
|
this._clearSelection();
|
2013-08-01 05:31:34 -07:00
|
|
|
this.closeSelection();
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
2013-04-05 03:33:42 -07:00
|
|
|
/*
|
|
|
|
* _repositionInfoRequest - fired at us by ContentAreaObserver when the
|
|
|
|
* soft keyboard is being displayed. CAO wants to make a decision about
|
|
|
|
* whether the browser deck needs repositioning.
|
|
|
|
*/
|
|
|
|
_repositionInfoRequest: function _repositionInfoRequest(aJsonMsg) {
|
|
|
|
let result = this._calcNewContentPosition(aJsonMsg.viewHeight);
|
|
|
|
|
|
|
|
// no repositioning needed
|
|
|
|
if (result == 0) {
|
2013-06-17 05:46:52 -07:00
|
|
|
this.sendAsync("Content:RepositionInfoResponse", { reposition: false });
|
2013-04-05 03:33:42 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-17 05:46:52 -07:00
|
|
|
this.sendAsync("Content:RepositionInfoResponse", {
|
2013-04-05 03:33:42 -07:00
|
|
|
reposition: true,
|
|
|
|
raiseContent: result,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-04-19 08:01:00 -07:00
|
|
|
_onPing: function _onPing(aId) {
|
2013-06-17 05:46:52 -07:00
|
|
|
this.sendAsync("Content:SelectionHandlerPong", { id: aId });
|
2013-04-19 08:01:00 -07:00
|
|
|
},
|
|
|
|
|
2013-10-21 10:02:02 -07:00
|
|
|
onClickCoords: function (xPos, yPos) {
|
|
|
|
this.lastXPos = xPos;
|
|
|
|
this.lastYPos = yPos;
|
|
|
|
},
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
/*************************************************
|
|
|
|
* Selection helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* _clearSelection
|
|
|
|
*
|
|
|
|
* Clear existing selection if it exists and reset our internla state.
|
|
|
|
*/
|
|
|
|
_clearSelection: function _clearSelection() {
|
2013-03-21 06:55:16 -07:00
|
|
|
this._clearTimers();
|
2013-02-12 12:51:25 -08:00
|
|
|
if (this._contentWindow) {
|
|
|
|
let selection = this._getSelection();
|
|
|
|
if (selection)
|
|
|
|
selection.removeAllRanges();
|
|
|
|
} else {
|
|
|
|
let selection = content.getSelection();
|
|
|
|
if (selection)
|
|
|
|
selection.removeAllRanges();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2013-08-01 05:31:34 -07:00
|
|
|
* closeSelection
|
2013-02-12 12:51:25 -08:00
|
|
|
*
|
|
|
|
* Shuts SelectionHandler down.
|
|
|
|
*/
|
2013-08-01 05:31:34 -07:00
|
|
|
closeSelection: function closeSelection() {
|
2013-03-21 06:55:16 -07:00
|
|
|
this._clearTimers();
|
2013-02-12 12:51:25 -08:00
|
|
|
this._cache = null;
|
|
|
|
this._contentWindow = null;
|
2013-03-23 09:07:18 -07:00
|
|
|
this._targetElement = null;
|
2013-02-12 12:51:25 -08:00
|
|
|
this._selectionMoveActive = false;
|
2013-03-23 09:07:18 -07:00
|
|
|
this._contentOffset = null;
|
|
|
|
this._domWinUtils = null;
|
|
|
|
this._targetIsEditable = false;
|
2014-01-27 11:25:04 -08:00
|
|
|
this._targetCoordinates = null;
|
2013-04-19 08:01:00 -07:00
|
|
|
sendSyncMessage("Content:HandlerShutdown", {});
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find content within frames - cache the target nsIDOMWindow,
|
|
|
|
* client coordinate offset, target element, and dom utils interface.
|
|
|
|
*/
|
|
|
|
_initTargetInfo: function _initTargetInfo(aX, aY) {
|
|
|
|
// getCurrentWindowAndOffset takes client coordinates
|
|
|
|
let { element: element,
|
|
|
|
contentWindow: contentWindow,
|
|
|
|
offset: offset,
|
|
|
|
utils: utils } =
|
2013-11-22 15:20:05 -08:00
|
|
|
Content.getCurrentWindowAndOffset(aX, aY);
|
2013-02-12 12:51:25 -08:00
|
|
|
if (!contentWindow) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this._targetElement = element;
|
|
|
|
this._contentWindow = contentWindow;
|
|
|
|
this._contentOffset = offset;
|
|
|
|
this._domWinUtils = utils;
|
2013-07-08 13:45:04 -07:00
|
|
|
this._targetIsEditable = Util.isEditable(this._targetElement);
|
2014-01-27 11:25:04 -08:00
|
|
|
this._targetCoordinates = {
|
|
|
|
x: aX,
|
|
|
|
y: aY
|
|
|
|
};
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2013-04-05 03:33:42 -07:00
|
|
|
/*
|
|
|
|
* _calcNewContentPosition - calculates the distance the browser should be
|
|
|
|
* raised to move the focused form input out of the way of the soft
|
|
|
|
* keyboard.
|
|
|
|
*
|
|
|
|
* @param aNewViewHeight the new content view height
|
|
|
|
* @return 0 if no positioning is required or a positive val equal to the
|
|
|
|
* distance content should be raised to center the target element.
|
|
|
|
*/
|
|
|
|
_calcNewContentPosition: function _calcNewContentPosition(aNewViewHeight) {
|
2013-10-08 13:02:52 -07:00
|
|
|
// We have no target element but the keyboard is up
|
2013-10-21 10:02:02 -07:00
|
|
|
// so lets not cover content that is below the keyboard
|
2013-10-08 13:02:52 -07:00
|
|
|
if (!this._cache || !this._cache.element) {
|
2013-10-21 10:02:02 -07:00
|
|
|
if (this.lastYPos != null && this.lastYPos > aNewViewHeight) {
|
|
|
|
return Services.metro.keyboardHeight;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-04-05 03:33:42 -07:00
|
|
|
}
|
|
|
|
|
2013-10-01 14:21:56 -07:00
|
|
|
let position = Util.centerElementInView(aNewViewHeight, this._cache.element);
|
|
|
|
if (position !== undefined) {
|
|
|
|
return position;
|
2013-04-05 03:33:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special case: we are dealing with an input that is taller than the
|
|
|
|
// desired height of content. We need to center on the caret location.
|
|
|
|
let rect =
|
2014-04-25 16:52:14 -07:00
|
|
|
this._domWinUtils.sendQueryContentEvent(
|
|
|
|
this._domWinUtils.QUERY_CARET_RECT,
|
|
|
|
this._targetElement.selectionEnd,
|
|
|
|
0, 0, 0,
|
|
|
|
this._domWinUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
|
2013-04-05 03:33:42 -07:00
|
|
|
if (!rect || !rect.succeeded) {
|
|
|
|
Util.dumpLn("no caret was present, unexpected.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note sendQueryContentEvent with QUERY_CARET_RECT is really buggy. If it
|
|
|
|
// can't find the exact location of the caret position it will "guess".
|
|
|
|
// Sometimes this can put the result in unexpected locations.
|
|
|
|
let caretLocation = Math.max(Math.min(Math.round(rect.top + (rect.height * .5)),
|
|
|
|
viewBottom), 0);
|
|
|
|
|
|
|
|
// Caret is above the bottom of the new view bounds, no need to shift.
|
|
|
|
if (caretLocation <= aNewViewHeight) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// distance from the top of the keyboard down to the caret location
|
|
|
|
return caretLocation - aNewViewHeight;
|
|
|
|
},
|
|
|
|
|
2013-06-17 05:46:52 -07:00
|
|
|
/*************************************************
|
2013-03-21 06:55:16 -07:00
|
|
|
* Events
|
|
|
|
*/
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
/*
|
|
|
|
* Scroll + selection advancement timer when the monocle is
|
|
|
|
* outside the bounds of an input control.
|
|
|
|
*/
|
|
|
|
scrollTimerCallback: function scrollTimerCallback() {
|
|
|
|
let result = SelectionHandler.updateTextEditSelection();
|
|
|
|
// Update monocle position and speed if we've dragged off to one side
|
|
|
|
if (result.trigger) {
|
2013-06-17 05:46:52 -07:00
|
|
|
SelectionHandler._updateSelectionUI("update", result.start, result.end);
|
2013-02-12 12:51:25 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function sh_receiveMessage(aMessage) {
|
|
|
|
if (this._debugEvents && aMessage.name != "Browser:SelectionMove") {
|
|
|
|
Util.dumpLn("SelectionHandler:", aMessage.name);
|
|
|
|
}
|
|
|
|
let json = aMessage.json;
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Browser:SelectionStart":
|
2013-08-19 02:26:17 -07:00
|
|
|
this._onSelectionStart(json);
|
2013-02-12 12:51:25 -08:00
|
|
|
break;
|
|
|
|
|
2013-02-19 17:51:02 -08:00
|
|
|
case "Browser:SelectionAttach":
|
|
|
|
this._onSelectionAttach(json.xPos, json.yPos);
|
2013-03-22 05:16:05 -07:00
|
|
|
break;
|
2013-02-19 17:51:02 -08:00
|
|
|
|
2013-03-20 07:40:05 -07:00
|
|
|
case "Browser:CaretAttach":
|
|
|
|
this._onCaretAttach(json.xPos, json.yPos);
|
2013-03-22 05:16:05 -07:00
|
|
|
break;
|
2013-03-20 07:40:05 -07:00
|
|
|
|
|
|
|
case "Browser:CaretMove":
|
|
|
|
this._onCaretMove(json.caret.xPos, json.caret.yPos);
|
2013-03-22 05:16:05 -07:00
|
|
|
break;
|
2013-03-20 07:40:05 -07:00
|
|
|
|
|
|
|
case "Browser:CaretUpdate":
|
|
|
|
this._onCaretPositionUpdate(json.caret.xPos, json.caret.yPos);
|
2013-03-22 05:16:05 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "Browser:SelectionSwitchMode":
|
|
|
|
this._onSwitchMode(json.newMode, json.change, json.xPos, json.yPos);
|
|
|
|
break;
|
2013-03-20 07:40:05 -07:00
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
case "Browser:SelectionClose":
|
2013-04-05 03:33:43 -07:00
|
|
|
this._onSelectionClose(json.clearSelection);
|
2013-02-12 12:51:25 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "Browser:SelectionMoveStart":
|
|
|
|
this._onSelectionMoveStart(json);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Browser:SelectionMove":
|
|
|
|
this._onSelectionMove(json);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Browser:SelectionMoveEnd":
|
|
|
|
this._onSelectionMoveEnd(json);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Browser:SelectionCopy":
|
|
|
|
this._onSelectionCopy(json);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Browser:SelectionDebug":
|
|
|
|
this._onSelectionDebug(json);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Browser:SelectionUpdate":
|
2014-01-27 11:25:04 -08:00
|
|
|
this._onSelectionUpdate(json);
|
2013-02-12 12:51:25 -08:00
|
|
|
break;
|
2013-04-05 03:33:42 -07:00
|
|
|
|
|
|
|
case "Browser:RepositionInfoRequest":
|
2013-10-21 10:02:02 -07:00
|
|
|
// This message is sent simultaneously with a tap event.
|
|
|
|
// Wait a bit to make sure we have the most up-to-date tap co-ordinates
|
|
|
|
// before a call to _calcNewContentPosition() which accesses them.
|
|
|
|
content.setTimeout (function () {
|
|
|
|
SelectionHandler._repositionInfoRequest(json);
|
|
|
|
}, 50);
|
2013-04-05 03:33:42 -07:00
|
|
|
break;
|
2013-04-19 08:01:00 -07:00
|
|
|
|
|
|
|
case "Browser:SelectionHandlerPing":
|
|
|
|
this._onPing(json.id);
|
|
|
|
break;
|
2013-10-28 21:17:45 -07:00
|
|
|
|
|
|
|
case "Browser:ResetLastPos":
|
2013-11-26 18:06:18 -08:00
|
|
|
this.onClickCoords(json.xPos, json.yPos);
|
2013-10-28 21:17:45 -07:00
|
|
|
break;
|
2013-02-12 12:51:25 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-06-17 05:46:52 -07:00
|
|
|
/*************************************************
|
2013-02-12 12:51:25 -08:00
|
|
|
* Utilities
|
|
|
|
*/
|
|
|
|
|
|
|
|
_getDocShell: function _getDocShell(aWindow) {
|
|
|
|
if (aWindow == null)
|
|
|
|
return null;
|
|
|
|
return aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShell);
|
|
|
|
},
|
|
|
|
|
|
|
|
_getSelectedText: function _getSelectedText() {
|
|
|
|
let selection = this._getSelection();
|
2013-02-27 08:27:48 -08:00
|
|
|
if (selection)
|
|
|
|
return selection.toString();
|
|
|
|
return "";
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_getSelection: function _getSelection() {
|
2013-03-20 07:40:05 -07:00
|
|
|
if (this._targetElement instanceof Ci.nsIDOMNSEditableElement) {
|
2013-02-12 12:51:25 -08:00
|
|
|
return this._targetElement
|
|
|
|
.QueryInterface(Ci.nsIDOMNSEditableElement)
|
|
|
|
.editor.selection;
|
2013-03-20 07:40:05 -07:00
|
|
|
} else if (this._contentWindow)
|
2013-02-12 12:51:25 -08:00
|
|
|
return this._contentWindow.getSelection();
|
2013-02-27 08:27:48 -08:00
|
|
|
return null;
|
2013-02-12 12:51:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_getSelectController: function _getSelectController() {
|
|
|
|
if (this._targetElement instanceof Ci.nsIDOMNSEditableElement) {
|
|
|
|
return this._targetElement
|
|
|
|
.QueryInterface(Ci.nsIDOMNSEditableElement)
|
|
|
|
.editor.selectionController;
|
|
|
|
} else {
|
|
|
|
let docShell = this._getDocShell(this._contentWindow);
|
|
|
|
if (docShell == null)
|
|
|
|
return null;
|
|
|
|
return docShell.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsISelectionDisplay)
|
|
|
|
.QueryInterface(Ci.nsISelectionController);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2013-11-23 21:32:32 -08:00
|
|
|
this.SelectionHandler = SelectionHandler;
|
2013-02-12 12:51:25 -08:00
|
|
|
|
2013-06-17 05:46:52 -07:00
|
|
|
SelectionHandler.__proto__ = new SelectionPrototype();
|
|
|
|
SelectionHandler.init();
|
|
|
|
|