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