From b9b6f357da58f3bbd21290385b4f363753806b34 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 8 May 2016 22:06:00 +0200 Subject: [PATCH] Add utils.supports_selection() --- qutebrowser/browser/commands.py | 6 +++--- qutebrowser/browser/hints.py | 3 +-- qutebrowser/utils/utils.py | 15 +++++++++------ tests/unit/utils/test_utils.py | 5 +++++ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index b9e189825..84955191d 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -688,7 +688,7 @@ class CommandDispatcher: s = self._current_url().toString(flags) what = 'URL' - if sel and QApplication.clipboard().supportsSelection(): + if sel and utils.supports_selection(): target = "primary selection" else: sel = False @@ -833,7 +833,7 @@ class CommandDispatcher: bg: Open in a background tab. window: Open in new window. """ - if sel and QApplication.clipboard().supportsSelection(): + if sel and utils.supports_selection(): target = "Primary selection" else: sel = False @@ -1756,7 +1756,7 @@ class CommandDispatcher: message.info(self._win_id, "Nothing to yank") return - if sel and QApplication.clipboard().supportsSelection(): + if sel and utils.supports_selection(): target = "primary selection" else: sel = False diff --git a/qutebrowser/browser/hints.py b/qutebrowser/browser/hints.py index 9590fa131..8ba1bc45c 100644 --- a/qutebrowser/browser/hints.py +++ b/qutebrowser/browser/hints.py @@ -28,7 +28,6 @@ import string from PyQt5.QtCore import (pyqtSignal, pyqtSlot, QObject, QEvent, Qt, QUrl, QTimer) from PyQt5.QtGui import QMouseEvent -from PyQt5.QtWidgets import QApplication from PyQt5.QtWebKit import QWebElement from PyQt5.QtWebKitWidgets import QWebPage @@ -492,7 +491,7 @@ class HintManager(QObject): context: The HintContext to use. """ if (context.target == Target.yank_primary and - QApplication.clipboard().supportsSelection()): + utils.supports_selection()): sel = True else: sel = False diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index bfd6bbf5c..c04516d36 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -757,22 +757,20 @@ def newest_slice(iterable, count): def set_clipboard(data, selection=False): """Set the clipboard to some given data.""" - clipboard = QApplication.clipboard() - if selection and not clipboard.supportsSelection(): + if selection and not supports_selection(): raise SelectionUnsupportedError if log_clipboard: what = 'primary selection' if selection else 'clipboard' log.misc.debug("Setting fake {}: {}".format(what, json.dumps(data))) else: mode = QClipboard.Selection if selection else QClipboard.Clipboard - clipboard.setText(data, mode=mode) + QApplication.clipboard().setText(data, mode=mode) def get_clipboard(selection=False): """Get data from the clipboard.""" global fake_clipboard - clipboard = QApplication.clipboard() - if selection and not clipboard.supportsSelection(): + if selection and not supports_selection(): raise SelectionUnsupportedError if fake_clipboard is not None: @@ -780,6 +778,11 @@ def get_clipboard(selection=False): fake_clipboard = None else: mode = QClipboard.Selection if selection else QClipboard.Clipboard - data = clipboard.text(mode=mode) + data = QApplication.clipboard().text(mode=mode) return data + + +def supports_selection(): + """Check if the OS supports primary selection.""" + return QApplication.clipboard().supportsSelection() diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index 0c2ed5cec..239df79df 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -979,3 +979,8 @@ class TestGetSetClipboard: utils.fake_clipboard = 'fake clipboard text' utils.get_clipboard(selection=selection) assert utils.fake_clipboard is None + + @pytest.mark.parametrize('selection', [True, False]) + def test_supports_selection(self, clipboard_mock, selection): + clipboard_mock.supportsSelection.return_value = selection + assert utils.supports_selection == selection