diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 724699c0b..3fa6fd772 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -849,14 +849,14 @@ class CommandDispatcher: s = self._yank_url(what) what = 'URL' # For printing elif what == 'selection': - def yank_callback(s): + def _selection_callback(s): if not self._current_widget().caret.has_selection() or not s: message.info("Nothing to yank") return self._yank_to_target(s, sel, what, keep) caret = self._current_widget().caret - caret.selection(callback=yank_callback) + caret.selection(callback=_selection_callback) return else: # pragma: no cover raise ValueError("Invalid value {!r} for `what'.".format(what)) @@ -1212,7 +1212,7 @@ class CommandDispatcher: log.procs.debug("Executing {} with args {}, userscript={}".format( cmd, args, userscript)) if userscript: - def selection_callback(s): + def _selection_callback(s): try: self._run_userscript(s, cmd, args, verbose) except cmdexc.CommandError as e: @@ -1224,7 +1224,7 @@ class CommandDispatcher: # until it fixed or blocked async call implemented: # https://github.com/qutebrowser/qutebrowser/issues/3327 caret = self._current_widget().caret - caret.selection(callback=selection_callback) + caret.selection(callback=_selection_callback) else: cmd = os.path.expanduser(cmd) proc = guiprocess.GUIProcess(what='command', verbose=verbose, diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 38a16b323..efc4c3035 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -203,12 +203,13 @@ class WebEngineCaret(browsertab.AbstractCaret): @pyqtSlot(usertypes.KeyMode) def _on_mode_entered(self, mode): self._tab.run_js_async( - javascript.assemble('caret', 'setInitialCursor', platform.platform())) + javascript.assemble('caret', 'setInitialCursor', + platform.platform())) @pyqtSlot(usertypes.KeyMode) def _on_mode_left(self): self.drop_selection() - self._js_call('toggle') + self._js_call('disableCaret') def move_to_next_line(self, count=1): for _ in range(count): diff --git a/qutebrowser/javascript/webengine_caret.js b/qutebrowser/javascript/webengine_caret.js index aeca235c9..5daa4aae4 100644 --- a/qutebrowser/javascript/webengine_caret.js +++ b/qutebrowser/javascript/webengine_caret.js @@ -816,15 +816,15 @@ window._qutebrowser.caret = (function() { */ CaretBrowsing.setInitialCursor = function(platform) { CaretBrowsing.isWindows = platform === "Windows"; - if (window.getSelection().rangeCount > 0) { - return; + const selectionRange = window.getSelection().toString().length; + if (selectionRange === 0) { + positionCaret(); } - positionCaret(); CaretBrowsing.injectCaretStyles(); CaretBrowsing.toggle(); CaretBrowsing.initiated = true; - CaretBrowsing.selectionEnabled = false; + CaretBrowsing.selectionEnabled = selectionRange > 0; }; /** @@ -1193,15 +1193,17 @@ window._qutebrowser.caret = (function() { }, 0); }; - CaretBrowsing.toggle = function() { + CaretBrowsing.toggle = function(value) { if (CaretBrowsing.forceEnabled) { CaretBrowsing.recreateCaretElement(); return; } - CaretBrowsing.isEnabled = !CaretBrowsing.isEnabled; - const obj = {}; - obj.enabled = CaretBrowsing.isEnabled; + if (value === undefined) { + CaretBrowsing.isEnabled = !CaretBrowsing.isEnabled; + } else { + CaretBrowsing.isEnabled = value; + } CaretBrowsing.updateIsCaretVisible(); }; @@ -1306,12 +1308,16 @@ window._qutebrowser.caret = (function() { return; } - if (!window.getSelection().toString()) { + if (window.getSelection().toString().length === 0) { positionCaret(); } CaretBrowsing.toggle(); }; + funcs.disableCaret = () => { + CaretBrowsing.toggle(false); + }; + funcs.toggle = () => { CaretBrowsing.toggle(); };