Skip to content

Commit b92913c

Browse files
committed
Fixed bug with Clipboard on Wayland
1 parent 1af2e7f commit b92913c

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

gui/utils/clipboard.py

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,69 @@
11
# noinspection PyPackageRequirements
22
import wx
3+
from logbook import Logger
4+
5+
logger = Logger(__name__)
36

47

58
def toClipboard(text):
6-
clip = wx.TheClipboard
7-
clip.Open()
8-
data = wx.TextDataObject(text)
9-
clip.SetData(data)
10-
clip.Close()
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.Clipboard()
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+
clipboard.Flush() # Ensure clipboard manager gets the data
28+
return True
29+
finally:
30+
clipboard.Close()
31+
else:
32+
logger.debug("Failed to open clipboard for writing")
33+
return False
34+
except Exception as e:
35+
logger.warning("Error writing to clipboard: {}", e)
36+
return False
1137

1238

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

0 commit comments

Comments
 (0)