Skip to content

Commit

Permalink
Uniformize all "Copied to clipboard" messages (nvaccess#6757)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienCochuyt committed Jul 2, 2020
1 parent a646fcd commit 50bf96b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
36 changes: 30 additions & 6 deletions source/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import appModuleHandler
import cursorManager
from typing import Any
import speech

#User functions

Expand Down Expand Up @@ -292,21 +293,44 @@ def processPendingEvents(processEventQueue=True):
if processEventQueue:
queueHandler.flushQueue(queueHandler.eventQueue)

def copyToClip(text):

def copyToClip(text, notify=False):
"""Copies the given text to the windows clipboard.
@returns: True if it succeeds, False otherwise.
@rtype: boolean
@param text: the text which will be copied to the clipboard
@type text: string
@param notify: whether to emit a confirmation message
@type notify: boolean
"""
if not isinstance(text,str) or len(text)==0:
return False
import gui
with winUser.openClipboard(gui.mainFrame.Handle):
winUser.emptyClipboard()
winUser.setClipboardData(winUser.CF_UNICODETEXT,text)
got=getClipData()
return got == text
try:
with winUser.openClipboard(gui.mainFrame.Handle):
winUser.emptyClipboard()
winUser.setClipboardData(winUser.CF_UNICODETEXT, text)
got = getClipData()
except ctypes.WinError:
if notify:
# Translators: Presented when unable to copy to the clipboard
# because of an error.
ui.message(_("Unable to copy"))
return False
if got == text:
if notify:
# Translators: Announced when a text has been copied to clipboard.
# %s is replaced by the copied text.
speech.speakMessage(_("Copied to clipboard: %s" % text))
# Translators: Displayed in braille when a text has been copied to clipboard.
# %s is replaced by the copied text.
braille.handler.message(_("Copied: %s" % text))
return True
if notify:
# Translators: Presented when the clipboard content did not match what was just copied.
ui.message(_("Unable to copy"))
return False


def getClipData():
"""Receives text from the windows clipboard.
Expand Down
4 changes: 1 addition & 3 deletions source/cursorManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,7 @@ def script_copyToClipboard(self,gesture):
# Translators: Reported when there is no text selected (for copying).
ui.message(_("No selection"))
return
if info.copyToClipboard():
# Translators: Message presented when text has been copied to clipboard.
ui.message(_("Copied to clipboard"))
info.copyToClipboard(notify=True)

def reportSelectionChange(self, oldTextInfo):
newInfo=self.makeTextInfo(textInfos.POSITION_SELECTION)
Expand Down
18 changes: 4 additions & 14 deletions source/globalCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,7 @@ def script_navigatorObject_current(self,gesture):
if scriptHandler.getLastScriptRepeatCount()==1:
speech.speakSpelling(text)
else:
if api.copyToClip(text):
# Translators: Indicates something has been copied to clipboard (example output: title text copied to clipboard).
speech.speakMessage(_("%s copied to clipboard")%text)
api.copyToClip(text, notify=True)
else:
speech.speakObject(curObject,reason=controlTypes.REASON_QUERY)
# Translators: Input help mode message for report current navigator object command.
Expand Down Expand Up @@ -1615,9 +1613,7 @@ def script_reportStatusLine(self,gesture):
# Translators: Reported when user attempts to copy content of the empty status line.
ui.message(_("unable to copy status bar content to clipboard"))
else:
if api.copyToClip(text):
# Translators: The message presented when the status bar is copied to the clipboard.
ui.message(_("%s copied to clipboard")%text)
api.copyToClip(text, notify=True)
# Translators: Input help mode message for report status line text command.
script_reportStatusLine.__doc__ = _("Reads the current application status bar and moves the navigator to it. If pressed twice, spells the information. If pressed three times, copies the status bar to the clipboard")
script_reportStatusLine.category=SCRCAT_FOCUS
Expand Down Expand Up @@ -1669,8 +1665,7 @@ def script_title(self,gesture):
elif repeatCount==1:
speech.speakSpelling(title)
else:
if api.copyToClip(title):
ui.message(_("%s copied to clipboard")%title)
api.copyToClip(title, notify=True)
# Translators: Input help mode message for report title bar command.
script_title.__doc__=_("Reports the title of the current application or foreground window. If pressed twice, spells the title. If pressed three times, copies the title to the clipboard")
script_title.category=SCRCAT_FOCUS
Expand Down Expand Up @@ -2218,12 +2213,7 @@ def script_review_copy(self, gesture):
return
elif scriptHandler.getLastScriptRepeatCount()==1: # the second call, try to copy the text
copyMarker = pos.obj._selectThenCopyRange
if copyMarker.copyToClipboard():
# Translators: Presented when some review text has been copied to clipboard.
ui.message(_("Review selection copied to clipboard"))
else:
# Translators: Presented when unable to copy to the clipboard because of an error.
ui.message(_("Unable to copy"))
copyMarker.copyToClipboard(notify=True)
# on the second call always clean up the start marker
api.getReviewPosition().obj._selectThenCopyRange = None
api.getReviewPosition().obj._copyStartMarker = None
Expand Down
6 changes: 4 additions & 2 deletions source/textInfos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,15 @@ def _get_clipboardText(self):
"""Text suitably formatted for copying to the clipboard. E.g. crlf characters inserted between lines."""
return convertToCrlf(self.text)

def copyToClipboard(self):
def copyToClipboard(self, notify=False):
"""Copy the content of this instance to the clipboard.
@return: C{True} if successful, C{False} otherwise.
@rtype: bool
@param notify: whether to emit a confirmation message
@type notify: boolean
"""
import api
return api.copyToClip(self.clipboardText)
return api.copyToClip(self.clipboardText, notify)

def getTextInChunks(self, unit):
"""Retrieve the text of this instance in chunks of a given unit.
Expand Down
4 changes: 2 additions & 2 deletions source/treeInterceptorHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def _set__rangeObj(self,r):
def _get_locationText(self):
return self.innerTextInfo.locationText

def copyToClipboard(self):
return self.innerTextInfo.copyToClipboard()
def copyToClipboard(self, notify=False):
return self.innerTextInfo.copyToClipboard(notify)

def find(self,text,caseSensitive=False,reverse=False):
return self.innerTextInfo.find(text,caseSensitive,reverse)
Expand Down

0 comments on commit 50bf96b

Please sign in to comment.