fix: Fixed clipboard issues on Wayland, using pyclip instead of wx

This commit is contained in:
Jasmine Moore
2025-12-17 12:26:36 -05:00
parent c7074f499f
commit ccbe88d3bf
3 changed files with 10 additions and 56 deletions

View File

@@ -151,6 +151,7 @@ class CopySelectDialog(wx.Dialog):
def cb(text): def cb(text):
if self.waitDialog: if self.waitDialog:
del self.waitDialog del self.waitDialog
print("cb called. Text: {}", text) # TODO REMOVE DEBUG
toClipboard(text) toClipboard(text)
self.EndModal(wx.ID_OK) self.EndModal(wx.ID_OK)

View File

@@ -1,68 +1,20 @@
# noinspection PyPackageRequirements # noinspection PyPackageRequirements
import wx import pyclip
from logbook import Logger from logbook import Logger
logger = Logger(__name__) logger = Logger(__name__)
def toClipboard(text): def toClipboard(text):
""" pyclip.copy(text)
Copy text to clipboard. Explicitly uses CLIPBOARD selection, not PRIMARY.
On X11 systems, wxPython can confuse between PRIMARY and CLIPBOARD selections,
causing "already open" errors. This function ensures we always use CLIPBOARD.
See: https://discuss.wxpython.org/t/wx-theclipboard-pasting-different-content-on-every-second-paste/35361
"""
clipboard = wx.TheClipboard
try:
# Explicitly use CLIPBOARD selection, not PRIMARY selection
# This prevents X11 confusion between the two clipboard types
clipboard.UsePrimarySelection(False)
if clipboard.Open():
try:
data = wx.TextDataObject(text)
clipboard.SetData(data)
return True
finally:
clipboard.Close()
else:
logger.debug("Failed to open clipboard for writing")
return False
except Exception as e:
logger.warning("Error writing to clipboard: {}", e)
return False
def fromClipboard(): def fromClipboard():
""" """
Read text from clipboard. Explicitly uses CLIPBOARD selection, not PRIMARY. Read text from clipboard. Uses pyclip to grab in a cross-platform, reliable manner.
On X11 systems, wxPython can confuse between PRIMARY and CLIPBOARD selections,
causing "already open" errors. This function ensures we always use CLIPBOARD.
See: https://discuss.wxpython.org/t/wx-theclipboard-pasting-different-content-on-every-second-paste/35361
""" """
clipboard = wx.TheClipboard data = pyclip.paste(text=True)
try: if not isinstance(data, str):
# Explicitly use CLIPBOARD selection, not PRIMARY selection data = data.decode('utf-8')
# This prevents X11 confusion between the two clipboard types logger.debug("Pasted data: {}", data)
clipboard.UsePrimarySelection(False) return data
if clipboard.Open():
try:
data = wx.TextDataObject()
if clipboard.GetData(data):
return data.GetText()
else:
logger.debug("Clipboard open but no CLIPBOARD data available")
return None
finally:
clipboard.Close()
else:
logger.debug("Failed to open clipboard for reading")
return None
except Exception as e:
logger.warning("Error reading from clipboard: {}", e)
return None

View File

@@ -13,3 +13,4 @@ beautifulsoup4==4.12.2
pyyaml==6.0.1 pyyaml==6.0.1
python-jose==3.3.0 python-jose==3.3.0
requests-cache==1.1.1 requests-cache==1.1.1
pyclip >= 0.7.0