Fallback to clipboard when primary selection is unsupported
This commit is contained in:
parent
64feb62fb1
commit
1eda2b0ea4
@ -1542,10 +1542,7 @@ class CommandDispatcher:
|
||||
backend=usertypes.Backend.QtWebKit)
|
||||
def paste_primary(self):
|
||||
"""Paste the primary selection at cursor position."""
|
||||
try:
|
||||
self.insert_text(utils.get_clipboard(selection=True))
|
||||
except utils.SelectionUnsupportedError:
|
||||
self.insert_text(utils.get_clipboard())
|
||||
self.insert_text(utils.get_clipboard(selection=True, fallback=True))
|
||||
|
||||
@cmdutils.register(instance='command-dispatcher', maxsplit=0,
|
||||
scope='window')
|
||||
|
@ -437,13 +437,13 @@ class LineEdit(QLineEdit):
|
||||
"""Override keyPressEvent to paste primary selection on Shift + Ins."""
|
||||
if e.key() == Qt.Key_Insert and e.modifiers() == Qt.ShiftModifier:
|
||||
try:
|
||||
text = utils.get_clipboard(selection=True)
|
||||
text = utils.get_clipboard(selection=True, fallback=True)
|
||||
except utils.ClipboardError: # pragma: no cover
|
||||
pass
|
||||
e.ignore()
|
||||
else:
|
||||
e.accept()
|
||||
self.insert(text)
|
||||
return
|
||||
return
|
||||
super().keyPressEvent(e)
|
||||
|
||||
def __repr__(self):
|
||||
|
@ -46,11 +46,12 @@ class MinimalLineEditMixin:
|
||||
"""Override keyPressEvent to paste primary selection on Shift + Ins."""
|
||||
if e.key() == Qt.Key_Insert and e.modifiers() == Qt.ShiftModifier:
|
||||
try:
|
||||
text = utils.get_clipboard(selection=True)
|
||||
text = utils.get_clipboard(selection=True, fallback=True)
|
||||
except utils.ClipboardError:
|
||||
text = utils.get_clipboard()
|
||||
e.accept()
|
||||
self.insert(text)
|
||||
e.ignore()
|
||||
else:
|
||||
e.accept()
|
||||
self.insert(text)
|
||||
return
|
||||
super().keyPressEvent(e)
|
||||
|
||||
|
@ -767,11 +767,23 @@ def set_clipboard(data, selection=False):
|
||||
QApplication.clipboard().setText(data, mode=mode)
|
||||
|
||||
|
||||
def get_clipboard(selection=False):
|
||||
"""Get data from the clipboard."""
|
||||
def get_clipboard(selection=False, fallback=False):
|
||||
"""Get data from the clipboard.
|
||||
|
||||
Args:
|
||||
selection: Use the primary selection.
|
||||
fallback: Fall back to the clipboard if primary selection is
|
||||
unavailable.
|
||||
"""
|
||||
global fake_clipboard
|
||||
if fallback and not selection:
|
||||
raise ValueError("fallback given without selection!")
|
||||
|
||||
if selection and not supports_selection():
|
||||
raise SelectionUnsupportedError
|
||||
if fallback:
|
||||
selection = False
|
||||
else:
|
||||
raise SelectionUnsupportedError
|
||||
|
||||
if fake_clipboard is not None:
|
||||
data = fake_clipboard
|
||||
|
@ -836,6 +836,11 @@ class TestGetSetClipboard:
|
||||
with pytest.raises(utils.SelectionUnsupportedError):
|
||||
utils.get_clipboard(selection=True)
|
||||
|
||||
def test_get_unsupported_selection_fallback(self, clipboard_mock):
|
||||
clipboard_mock.supportsSelection.return_value = False
|
||||
clipboard_mock.text.return_value = 'text'
|
||||
assert utils.get_clipboard(selection=True, fallback=True) == 'text'
|
||||
|
||||
@pytest.mark.parametrize('selection', [True, False])
|
||||
def test_get_fake_clipboard(self, selection):
|
||||
utils.fake_clipboard = 'fake clipboard text'
|
||||
@ -847,6 +852,10 @@ class TestGetSetClipboard:
|
||||
clipboard_mock.supportsSelection.return_value = selection
|
||||
assert utils.supports_selection() == selection
|
||||
|
||||
def test_fallback_without_selection(self):
|
||||
with pytest.raises(ValueError):
|
||||
utils.get_clipboard(fallback=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('keystr, expected', [
|
||||
('<Control-x>', True),
|
||||
|
Loading…
Reference in New Issue
Block a user