Merge pull request #428 from ajaxorg/mouse-refactor

Mouse refactor
This commit is contained in:
Fabian Jakobs 2011-09-24 05:08:29 -07:00
commit 2c92472b8f
7 changed files with 328 additions and 97 deletions

View file

@ -47,7 +47,7 @@ var event = require("pilot/event");
var lang = require("pilot/lang");
var useragent = require("pilot/useragent");
var TextInput = require("ace/keyboard/textinput").TextInput;
var MouseHandler = require("ace/mouse_handler").MouseHandler;
var MouseHandler = require("ace/mouse/mouse_handler").MouseHandler;
//var TouchHandler = require("ace/touch_handler").TouchHandler;
var KeyBinding = require("ace/keyboard/keybinding").KeyBinding;
var EditSession = require("ace/edit_session").EditSession;

View file

@ -21,7 +21,7 @@
*
* Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org>
* Mihai Sucan <mihai DOT sucan AT gmail DOT com>
* Mike de Boer <mike AT ajax DOT org>
*
* 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
@ -41,6 +41,7 @@ define(function(require, exports, module) {
var event = require("pilot/event");
var dom = require("pilot/dom");
var EventEmitter = require("pilot/event_emitter").EventEmitter;
var BrowserFocus = require("pilot/browser_focus").BrowserFocus;
var STATE_UNKNOWN = 0;
@ -50,63 +51,30 @@ var STATE_DRAG = 2;
var DRAG_TIMER = 250; // milliseconds
var DRAG_OFFSET = 5; // pixels
var MouseHandler = function(editor) {
function DefaultHandlers(editor) {
this.editor = editor;
this.$clickSelection = null;
this.browserFocus = new BrowserFocus();
event.addListener(editor.container, "mousedown", function(e) {
editor.focus();
return event.preventDefault(e);
});
event.addListener(editor.container, "selectstart", function(e) {
return event.preventDefault(e);
});
var mouseTarget = editor.renderer.getMouseEventTarget();
event.addListener(mouseTarget, "mousedown", this.onMouseDown.bind(this));
event.addMultiMouseDownListener(mouseTarget, 0, 2, 500, this.onMouseDoubleClick.bind(this));
event.addMultiMouseDownListener(mouseTarget, 0, 3, 600, this.onMouseTripleClick.bind(this));
event.addMultiMouseDownListener(mouseTarget, 0, 4, 600, this.onMouseQuadClick.bind(this));
event.addMouseWheelListener(editor.container, this.onMouseWheel.bind(this));
};
editor.setDefaultHandler("mousedown", this.onMouseDown.bind(this));
editor.setDefaultHandler("dblclick", this.onDoubleClick.bind(this));
editor.setDefaultHandler("tripleclick", this.onTripleClick.bind(this));
editor.setDefaultHandler("quadclick", this.onQuadClick.bind(this));
editor.setDefaultHandler("scroll", this.onScroll.bind(this));
}
(function() {
this.$scrollSpeed = 1;
this.setScrollSpeed = function(speed) {
this.$scrollSpeed = speed;
};
this.getScrollSpeed = function() {
return this.$scrollSpeed;
};
this.$getEventPosition = function(e) {
var pageX = event.getDocumentX(e);
var pageY = event.getDocumentY(e);
var pos = this.editor.renderer.screenToTextCoordinates(pageX, pageY);
pos.row = Math.max(0, Math.min(pos.row, this.editor.session.getLength()-1));
return pos;
};
this.$distance = function(ax, ay, bx, by) {
return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2));
};
this.onMouseDown = function(e) {
var pageX = event.getDocumentX(e);
var pageY = event.getDocumentY(e);
var pos = this.$getEventPosition(e);
this.onMouseDown = function(ev) {
var inSelection = ev.inSelection();
var pageX = ev.pageX;
var pageY = ev.pageY;
var pos = ev.getDocumentPosition();
var editor = this.editor;
var self = this;
var _self = this;
var selectionRange = editor.getSelectionRange();
var selectionEmpty = selectionRange.isEmpty();
var inSelection = !editor.getReadOnly()
&& !selectionEmpty
&& selectionRange.contains(pos.row, pos.column);
var state = STATE_UNKNOWN;
// if this click caused the editor to be focused should not clear the
@ -115,14 +83,14 @@ var MouseHandler = function(editor) {
inSelection && (
!this.browserFocus.isFocused()
|| new Date().getTime() - this.browserFocus.lastFocus < 20
|| !this.editor.isFocused()
|| !editor.isFocused()
)
) {
this.editor.focus();
editor.focus();
return;
}
var button = event.getButton(e);
var button = ev.getButton();
if (button !== 0) {
if (selectionEmpty) {
editor.moveCursorToPosition(pos);
@ -132,7 +100,8 @@ var MouseHandler = function(editor) {
event.capture(editor.container, function(){}, editor.textInput.onContextMenuClose);
}
return;
} else {
}
else {
// Select the fold as the user clicks it.
var fold = editor.session.getFoldAt(pos.row, pos.column, 1);
if (fold) {
@ -164,7 +133,7 @@ var MouseHandler = function(editor) {
else if (state == STATE_DRAG)
onMouseDragSelectionEnd();
self.$clickSelection = null;
_self.$clickSelection = null;
state = STATE_UNKNOWN;
};
@ -172,7 +141,7 @@ var MouseHandler = function(editor) {
dom.removeCssClass(editor.container, "ace_dragging");
editor.session.removeMarker(dragSelectionMarker);
if (!self.$clickSelection) {
if (!editor.$mouseHandler.$clickSelection) {
if (!dragCursor) {
editor.moveCursorToPosition(pos);
editor.selection.clearSelection(pos.row, pos.column);
@ -202,16 +171,16 @@ var MouseHandler = function(editor) {
return;
if (state == STATE_UNKNOWN) {
var distance = self.$distance(pageX, pageY, mousePageX, mousePageY);
var distance = calcDistance(pageX, pageY, mousePageX, mousePageY);
var time = (new Date()).getTime();
if (distance > DRAG_OFFSET) {
state = STATE_SELECT;
var cursor = editor.renderer.screenToTextCoordinates(mousePageX, mousePageY);
cursor.row = Math.max(0, Math.min(cursor.row, editor.session.getLength()-1));
onStartSelect(cursor);
} else if ((time - mousedownTime) > DRAG_TIMER) {
}
else if ((time - mousedownTime) > DRAG_TIMER) {
state = STATE_DRAG;
dragRange = editor.getSelectionRange();
var style = editor.getSelectionStyle();
@ -229,10 +198,11 @@ var MouseHandler = function(editor) {
};
function onStartSelect(pos) {
if (e.shiftKey)
editor.selection.selectToPosition(pos)
if (ev.getShiftKey()) {
editor.selection.selectToPosition(pos);
}
else {
if (!self.$clickSelection) {
if (!_self.$clickSelection) {
editor.moveCursorToPosition(pos);
editor.selection.clearSelection(pos.row, pos.column);
}
@ -241,17 +211,20 @@ var MouseHandler = function(editor) {
}
var onUpdateSelectionInterval = function() {
var anchor;
var cursor = editor.renderer.screenToTextCoordinates(mousePageX, mousePageY);
cursor.row = Math.max(0, Math.min(cursor.row, editor.session.getLength()-1));
if (self.$clickSelection) {
if (self.$clickSelection.contains(cursor.row, cursor.column)) {
editor.selection.setSelectionRange(self.$clickSelection);
} else {
if (self.$clickSelection.compare(cursor.row, cursor.column) == -1) {
var anchor = self.$clickSelection.end;
} else {
var anchor = self.$clickSelection.start;
if (_self.$clickSelection) {
if (_self.$clickSelection.contains(cursor.row, cursor.column)) {
editor.selection.setSelectionRange(_self.$clickSelection);
}
else {
if (_self.$clickSelection.compare(cursor.row, cursor.column) == -1) {
anchor = _self.$clickSelection.end;
}
else {
anchor = _self.$clickSelection.start;
}
editor.selection.setSelectionAnchor(anchor.row, anchor.column);
editor.selection.selectToPosition(cursor);
@ -274,46 +247,55 @@ var MouseHandler = function(editor) {
event.capture(editor.container, onMouseSelection, onMouseSelectionEnd);
var timerId = setInterval(onSelectionInterval, 20);
return event.preventDefault(e);
return ev.preventDefault();
};
this.onMouseDoubleClick = function(e) {
this.onDoubleClick = function(ev) {
var pos = ev.getDocumentPosition();
var editor = this.editor;
var pos = this.$getEventPosition(e);
// If the user dclicked on a fold, then expand it.
var fold = editor.session.getFoldAt(pos.row, pos.column, 1);
if (fold) {
editor.session.expandFold(fold);
} else {
}
else {
editor.moveCursorToPosition(pos);
editor.selection.selectWord();
this.$clickSelection = editor.getSelectionRange();
}
};
this.onMouseTripleClick = function(e) {
var pos = this.$getEventPosition(e);
this.editor.moveCursorToPosition(pos);
this.editor.selection.selectLine();
this.$clickSelection = this.editor.getSelectionRange();
this.onTripleClick = function(ev) {
var pos = ev.getDocumentPosition();
var editor = this.editor;
editor.moveCursorToPosition(pos);
editor.selection.selectLine();
this.$clickSelection = editor.getSelectionRange();
};
this.onMouseQuadClick = function(e) {
this.editor.selectAll();
this.$clickSelection = this.editor.getSelectionRange();
this.onQuadClick = function(ev) {
var editor = this.editor;
editor.selectAll();
this.$clickSelection = editor.getSelectionRange();
};
this.onMouseWheel = function(e) {
var speed = this.$scrollSpeed * 2;
this.editor.renderer.scrollBy(e.wheelX * speed, e.wheelY * speed);
if (this.editor.renderer.isScrollableBy(e.wheelX, e.wheelY))
this.onScroll = function(ev) {
var editor = this.editor;
editor.renderer.scrollBy(e.wheelX * ev.speed, e.wheelY * speed);
if (editor.renderer.isScrollableBy(e.wheelX, e.wheelY))
return event.preventDefault(e);
};
}).call(DefaultHandlers.prototype);
exports.DefaultHandlers = DefaultHandlers;
}).call(MouseHandler.prototype);
function calcDistance(ax, ay, bx, by) {
return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2));
}
exports.MouseHandler = MouseHandler;
});

0
lib/ace/mouse/dragdrop.js vendored Normal file
View file

View file

@ -0,0 +1,133 @@
/* vim:ts=4:sts=4:sw=4:
* ***** 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 Ajax.org Code Editor (ACE).
*
* The Initial Developer of the Original Code is
* Ajax.org B.V.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org>
*
* 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 ***** */
define(function(require, exports, module) {
var event = require("pilot/event");
var dom = require("pilot/dom");
/**
* Custom Ace mouse event
*/
var MouseEvent = exports.MouseEvent = function(domEvent, editor) {
this.domEvent = domEvent;
this.editor = editor;
this.pageX = event.getDocumentX(domEvent);
this.pageY = event.getDocumentY(domEvent);
this.$pos = null;
this.$inSelection = null;
this.propagationStopped = false;
this.defaultPrevented = false;
};
(function() {
this.stopPropagation = function() {
event.stopPropagation(this.domEvent);
this.propagationStopped = true;
};
this.preventDefault = function() {
event.preventDefault(this.domEvent);
this.defaultPrevented = true;
};
/**
* Get the document position below the mouse cursor
*
* @return {Object} 'row' and 'column' of the document position
*/
this.getDocumentPosition = function() {
if (this.$pos)
return this.$pos;
var pageX = event.getDocumentX(this.domEvent);
var pageY = event.getDocumentY(this.domEvent);
this.$pos = this.editor.renderer.screenToTextCoordinates(pageX, pageY);
this.$pos.row = Math.max(0, Math.min(this.$pos.row, this.editor.session.getLength()-1));
return this.$pos;
};
/**
* Check if the mouse cursor is inside of the text selection
*
* @return {Boolean} whether the mouse cursor is inside of the selection
*/
this.inSelection = function() {
if (this.$inSelection !== null)
return this.$inSelection;
var editor = this.editor;
if (editor.getReadOnly()) {
this.$inSelection = false;
}
else {
var selectionRange = editor.getSelectionRange();
if (selectionRange.isEmpty())
this.$inSelection = false;
else {
var pos = this.getDocumentPosition();
this.$inSelection = selectionRange.contains(pos.row, pos.column);
}
}
return this.$inSelection;
};
/**
* Get the clicked mouse button
*
* @return {Number} 0 for left button, 1 for middle button, 2 for right button
*/
this.getButton = function() {
return event.getButton(this.domEvent);
};
/**
* @return {Boolean} whether the shift key was pressed when the event was emitted
*/
this.getShiftKey = function() {
return this.domEvent.shiftKey;
};
}).call(MouseEvent.prototype);
});

View file

@ -0,0 +1,113 @@
/* vim:ts=4:sts=4:sw=4:
* ***** 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 Ajax.org Code Editor (ACE).
*
* The Initial Developer of the Original Code is
* Ajax.org B.V.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org>
* Mihai Sucan <mihai DOT sucan AT gmail DOT com>
*
* 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 ***** */
define(function(require, exports, module) {
var event = require("pilot/event");
var DefaultHandlers = require("ace/mouse/default_handlers").DefaultHandlers;
var MouseEvent = require("ace/mouse/mouse_event").MouseEvent;
var MouseHandler = function(editor) {
this.editor = editor;
this.defaultHandlers = new DefaultHandlers(editor);
event.addListener(editor.container, "mousedown", function(e) {
editor.focus();
return event.preventDefault(e);
});
event.addListener(editor.container, "selectstart", function(e) {
return event.preventDefault(e);
});
var mouseTarget = editor.renderer.getMouseEventTarget();
event.addListener(mouseTarget, "mousedown", this.onMouseDown.bind(this));
event.addListener(mouseTarget, "mousemove", this.onMouseMove.bind(this));
event.addMultiMouseDownListener(mouseTarget, 0, 2, 500, this.onMouseDoubleClick.bind(this));
event.addMultiMouseDownListener(mouseTarget, 0, 3, 600, this.onMouseTripleClick.bind(this));
event.addMultiMouseDownListener(mouseTarget, 0, 4, 600, this.onMouseQuadClick.bind(this));
event.addMouseWheelListener(editor.container, this.onMouseWheel.bind(this));
};
(function() {
this.$scrollSpeed = 1;
this.setScrollSpeed = function(speed) {
this.$scrollSpeed = speed;
};
this.getScrollSpeed = function() {
return this.$scrollSpeed;
};
this.onMouseDown = function(e) {
this.editor._dispatchEvent("mousedown", new MouseEvent(e, this.editor));
};
this.onMouseMove = function(e) {
// optimization, because mousemove doesn't have a default handler.
var listeners = this.editor._eventRegistry && this.editor._eventRegistry["mousemove"];
if (!listeners || !listeners.length)
return;
this.editor._dispatchEvent("mousemove", new MouseEvent(e, this.editor));
};
this.onMouseDoubleClick = function(e) {
this.editor._dispatchEvent("dblclick", new MouseEvent(e, this.editor));
};
this.onMouseTripleClick = function(e) {
this.editor._dispatchEvent("tripleclick", new MouseEvent(e, this.editor));
};
this.onMouseQuadClick = function(e) {
this.editor._dispatchEvent("quadclick", new MouseEvent(e, this.editor));
};
this.onMouseWheel = function(e) {
var mouseEvent = new MouseEvent(e, this.editor);
mouseEvent.speed = this.$scrollSpeed * 2;
this.editor._dispatchEvent("quadclick", mouseEvent);
};
}).call(MouseHandler.prototype);
exports.MouseHandler = MouseHandler;
});

View file

@ -154,7 +154,10 @@ var WorkerClient = function(topLevelNamespaces, packagedJs, module, classname) {
};
this.emit = function(event, data) {
this.$worker.postMessage({event: event, data: data});
try {
this.$worker.postMessage({event: event, data: data});
}
catch(ex) {}
};
}).call(WorkerClient.prototype);

@ -1 +1 @@
Subproject commit f16b1438c95900416e443f6bf585562c1c94ca43
Subproject commit 3fdfcc071929aa96dff4f2d710dce10cda164fa7