Add some movement caret actions

This commit is contained in:
Artur Shaikhullin 2017-11-20 17:49:42 +06:00
parent b184d2f94d
commit 82b1bd10ec
2 changed files with 114 additions and 36 deletions

View File

@ -179,39 +179,56 @@ class WebEngineCaret(browsertab.AbstractCaret):
@pyqtSlot(usertypes.KeyMode)
def _on_mode_entered(self, mode):
js_code = javascript.assemble('caret', 'setInitialCursor')
self._tab.run_js_async(js_code)
self._tab.run_js_async(
javascript.assemble('caret', 'setInitialCursor'))
@pyqtSlot(usertypes.KeyMode)
def _on_mode_left(self):
pass
self._tab.run_js_async(
javascript.assemble('caret', 'toggle'))
def move_to_next_line(self, count=1):
log.stub()
for _ in range(count):
self._tab.run_js_async(
javascript.assemble('caret', 'moveDown'))
def move_to_prev_line(self, count=1):
log.stub()
for _ in range(count):
self._tab.run_js_async(
javascript.assemble('caret', 'moveUp'))
def move_to_next_char(self, count=1):
log.stub()
for _ in range(count):
self._tab.run_js_async(
javascript.assemble('caret', 'moveRight'))
def move_to_prev_char(self, count=1):
log.stub()
for _ in range(count):
self._tab.run_js_async(
javascript.assemble('caret', 'moveLeft'))
def move_to_end_of_word(self, count=1):
log.stub()
for _ in range(count):
self._tab.run_js_async(
javascript.assemble('caret', 'moveToEndOfWord'))
def move_to_next_word(self, count=1):
log.stub()
for _ in range(count):
self._tab.run_js_async(
javascript.assemble('caret', 'moveToNextWord'))
def move_to_prev_word(self, count=1):
log.stub()
for _ in range(count):
self._tab.run_js_async(
javascript.assemble('caret', 'moveToPreviousWord'))
def move_to_start_of_line(self):
log.stub()
self._tab.run_js_async(
javascript.assemble('caret', 'moveToStartOfLine'))
def move_to_end_of_line(self):
log.stub()
self._tab.run_js_async(
javascript.assemble('caret', 'moveToEndOfLine'))
def move_to_start_of_next_block(self, count=1):
log.stub()
@ -232,7 +249,8 @@ class WebEngineCaret(browsertab.AbstractCaret):
log.stub()
def toggle_selection(self):
log.stub()
self._tab.run_js_async(
javascript.assemble('caret', 'toggleSelection'))
def drop_selection(self):
log.stub()
@ -287,7 +305,6 @@ class WebEngineCaret(browsertab.AbstractCaret):
self._tab.run_js_async(js_code, lambda jsret:
self._follow_selected_cb(jsret, tab))
class WebEngineScroller(browsertab.AbstractScroller):
"""QtWebEngine implementations related to scrolling."""

View File

@ -1076,7 +1076,15 @@ window._qutebrowser.caret = (function() {
* first text character in the document.
*/
funcs.setInitialCursor = () => {
CaretBrowsing.setInitialCursor();
if (!CaretBrowsing.initiated) {
CaretBrowsing.setInitialCursor();
} else {
CaretBrowsing.toggle();
}
}
funcs.toggle = () => {
CaretBrowsing.toggle();
}
CaretBrowsing.setInitialCursor = function() {
@ -1094,6 +1102,8 @@ window._qutebrowser.caret = (function() {
}
CaretBrowsing.setAndValidateSelection(start, start);
CaretBrowsing.toggle();
CaretBrowsing.initiated = true;
CaretBrowsing.selectionEnabled = false;
};
/**
@ -1534,11 +1544,7 @@ window._qutebrowser.caret = (function() {
* @return {boolean} True if the cursor should move by word.
*/
CaretBrowsing.isMoveByWordEvent = function(evt) {
if (CaretBrowsing.isMac) {
return evt.altKey;
} else {
return evt.ctrlKey;
}
return evt.ctrlKey;
};
/**
@ -1589,6 +1595,75 @@ window._qutebrowser.caret = (function() {
return result;
};
funcs.moveRight = () => {
var evt = Object.assign({}, document.activeElement);
evt.keyCode = 39;
CaretBrowsing.onKeyDown(evt);
}
funcs.moveLeft = () => {
var evt = Object.assign({}, document.activeElement);
evt.keyCode = 37;
CaretBrowsing.onKeyDown(evt);
}
funcs.moveDown = () => {
var evt = Object.assign({}, document.activeElement);
evt.keyCode = 40;
CaretBrowsing.onKeyDown(evt);
}
funcs.moveUp = () => {
var evt = Object.assign({}, document.activeElement);
evt.keyCode = 38;
CaretBrowsing.onKeyDown(evt);
}
funcs.moveToEndOfWord = () => {
funcs.moveToNextWord();
funcs.moveLeft();
}
funcs.moveToNextWord = () => {
var evt = Object.assign({}, document.activeElement);
evt.keyCode = 39;
evt.ctrlKey = true;
CaretBrowsing.onKeyDown(evt);
funcs.moveRight();
}
funcs.moveToPreviousWord = () => {
var evt = Object.assign({}, document.activeElement);
evt.keyCode = 37;
evt.ctrlKey = true;
CaretBrowsing.onKeyDown(evt);
}
funcs.moveToStartOfLine = () => {
CaretBrowsing.moveToLineBoundary('left');
}
funcs.moveToEndOfLine = () => {
CaretBrowsing.moveToLineBoundary('right');
}
CaretBrowsing.moveToLineBoundary = function(side) {
window
.getSelection()
.modify(
CaretBrowsing.selectionEnabled ? 'extend' : 'move',
side,
'lineboundary');
window.setTimeout(function() {
CaretBrowsing.updateCaretOrSelection(true);
}, 0);
};
funcs.toggleSelection = () => {
CaretBrowsing.selectionEnabled = !CaretBrowsing.selectionEnabled;
}
/**
* Called when the user presses the right arrow. If there's a selection,
* moves the cursor to the end of the selection range. If it's a cursor,
@ -2002,24 +2077,15 @@ window._qutebrowser.caret = (function() {
* @return {boolean} True if the default action should be performed.
*/
CaretBrowsing.onKeyDown = function(evt) {
if (evt.defaultPrevented) {
return;
}
if (evt.keyCode == 118) { // F7
CaretBrowsing.toggle();
}
if (!CaretBrowsing.isEnabled) {
return true;
}
evt.shiftKey = CaretBrowsing.selectionEnabled;
if (evt.target && CaretBrowsing.isControlThatNeedsArrowKeys(
/** @type (Node) */(evt.target))) {
if (evt.keyCode == 27) {
CaretBrowsing.escapeFromControl(/** @type {Node} */(evt.target));
evt.preventDefault();
evt.stopPropagation();
return false;
} else {
return true;
@ -2062,11 +2128,6 @@ window._qutebrowser.caret = (function() {
break;
}
if (result == false) {
evt.preventDefault();
evt.stopPropagation();
}
window.setTimeout(function() {
CaretBrowsing.updateCaretOrSelection(result == false);
}, 0);
@ -2161,7 +2222,7 @@ window._qutebrowser.caret = (function() {
CaretBrowsing.init = function() {
CaretBrowsing.isWindowFocused = document.hasFocus();
document.addEventListener('keydown', CaretBrowsing.onKeyDown, false);
// document.addEventListener('keydown', CaretBrowsing.onKeyDown, false);
document.addEventListener('click', CaretBrowsing.onClick, false);
window.addEventListener('focus', CaretBrowsing.onWindowFocus, false);
window.addEventListener('blur', CaretBrowsing.onWindowBlur, false);