Skip to content

Commit 2201dde

Browse files
authored
Merge pull request #2706 from freeone3000/master
fix: Fixed clipboard issues on Wayland, using pyclip instead of wx
2 parents 3a324b9 + 84f5322 commit 2201dde

File tree

2 files changed

+9
-56
lines changed

2 files changed

+9
-56
lines changed

gui/utils/clipboard.py

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,20 @@
11
# noinspection PyPackageRequirements
2-
import wx
2+
import pyclip
33
from logbook import Logger
44

55
logger = Logger(__name__)
66

77

88
def toClipboard(text):
9-
"""
10-
Copy text to clipboard. Explicitly uses CLIPBOARD selection, not PRIMARY.
11-
12-
On X11 systems, wxPython can confuse between PRIMARY and CLIPBOARD selections,
13-
causing "already open" errors. This function ensures we always use CLIPBOARD.
14-
15-
See: https://discuss.wxpython.org/t/wx-theclipboard-pasting-different-content-on-every-second-paste/35361
16-
"""
17-
clipboard = wx.TheClipboard
18-
try:
19-
# Explicitly use CLIPBOARD selection, not PRIMARY selection
20-
# This prevents X11 confusion between the two clipboard types
21-
clipboard.UsePrimarySelection(False)
22-
23-
if clipboard.Open():
24-
try:
25-
data = wx.TextDataObject(text)
26-
clipboard.SetData(data)
27-
return True
28-
finally:
29-
clipboard.Close()
30-
else:
31-
logger.debug("Failed to open clipboard for writing")
32-
return False
33-
except Exception as e:
34-
logger.warning("Error writing to clipboard: {}", e)
35-
return False
9+
pyclip.copy(text)
3610

3711

3812
def fromClipboard():
3913
"""
40-
Read text from clipboard. Explicitly uses CLIPBOARD selection, not PRIMARY.
41-
42-
On X11 systems, wxPython can confuse between PRIMARY and CLIPBOARD selections,
43-
causing "already open" errors. This function ensures we always use CLIPBOARD.
44-
45-
See: https://discuss.wxpython.org/t/wx-theclipboard-pasting-different-content-on-every-second-paste/35361
14+
Read text from clipboard. Uses pyclip to grab in a cross-platform, reliable manner.
4615
"""
47-
clipboard = wx.TheClipboard
48-
try:
49-
# Explicitly use CLIPBOARD selection, not PRIMARY selection
50-
# This prevents X11 confusion between the two clipboard types
51-
clipboard.UsePrimarySelection(False)
52-
53-
if clipboard.Open():
54-
try:
55-
data = wx.TextDataObject()
56-
if clipboard.GetData(data):
57-
return data.GetText()
58-
else:
59-
logger.debug("Clipboard open but no CLIPBOARD data available")
60-
return None
61-
finally:
62-
clipboard.Close()
63-
else:
64-
logger.debug("Failed to open clipboard for reading")
65-
return None
66-
except Exception as e:
67-
logger.warning("Error reading from clipboard: {}", e)
68-
return None
16+
data = pyclip.paste(text=True)
17+
if not isinstance(data, str):
18+
data = data.decode('utf-8')
19+
logger.debug("Pasted data: {}", data)
20+
return data

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ beautifulsoup4==4.12.2
1313
pyyaml==6.0.1
1414
python-jose==3.3.0
1515
requests-cache==1.1.1
16+
pyclip >= 0.7.0

0 commit comments

Comments
 (0)