forked from nvaccess/nvda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinConsoleUIA.py
More file actions
196 lines (181 loc) · 5.69 KB
/
winConsoleUIA.py
File metadata and controls
196 lines (181 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# NVDAObjects/UIA/winConsoleUIA.py
# A part of NonVisual Desktop Access (NVDA)
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.
# Copyright (C) 2019 Bill Dengler
import ctypes
import NVDAHelper
import speech
import time
import textInfos
import UIAHandler
from scriptHandler import script
from winVersion import isAtLeastWin10
from . import UIATextInfo
from ..behaviors import Terminal
class consoleUIATextInfo(UIATextInfo):
_expandCollapseBeforeReview = False
def __init__(self, obj, position, _rangeObj=None):
super(consoleUIATextInfo, self).__init__(obj, position, _rangeObj)
if position == textInfos.POSITION_CARET and isAtLeastWin10(1903):
# The UIA implementation in 1903 causes the caret to be
# off-by-one, so move it one position to the right
# to compensate.
self._rangeObj.MoveEndpointByUnit(
UIAHandler.TextPatternRangeEndpoint_Start,
UIAHandler.NVDAUnitsToUIAUnits[textInfos.UNIT_CHARACTER],
1
)
def move(self, unit, direction, endPoint=None):
oldRange=None
if self.basePosition != textInfos.POSITION_CARET:
# Insure we haven't gone beyond the visible text.
# UIA adds thousands of blank lines to the end of the console.
visiRanges = self.obj.UIATextPattern.GetVisibleRanges()
if visiRanges.length > 0:
firstVisiRange = visiRanges.GetElement(0)
lastVisiRange = visiRanges.GetElement(visiRanges.length - 1)
oldRange=self._rangeObj.clone()
if unit == textInfos.UNIT_WORD and direction != 0:
# UIA doesn't implement word movement, so we need to do it manually.
lineInfo = self.copy()
lineInfo.expand(textInfos.UNIT_LINE)
offset = self._getCurrentOffsetInThisLine(lineInfo)
index = 1 if direction > 0 else 0
start, end = self._getWordOffsetsInThisLine(offset, lineInfo)
wordMoveDirections = (
(offset - start) * -1,
end - offset
)
res = self.move(
textInfos.UNIT_CHARACTER,
wordMoveDirections[index],
endPoint=endPoint
)
if res != 0:
return direction
else:
if self.move(textInfos.UNIT_CHARACTER, -1): # Reset word boundaries to move to the previous word
return self.move(unit, direction, endPoint=endPoint)
else:
return res
res = super(consoleUIATextInfo, self).move(unit, direction, endPoint)
if oldRange and (
self._rangeObj.CompareEndPoints(
UIAHandler.TextPatternRangeEndpoint_Start,
firstVisiRange,
UIAHandler.TextPatternRangeEndpoint_Start
) < 0
or self._rangeObj.CompareEndPoints(
UIAHandler.TextPatternRangeEndpoint_Start,
lastVisiRange,
UIAHandler.TextPatternRangeEndpoint_End
) >= 0
):
self._rangeObj = oldRange
return 0
return res
def expand(self, unit):
if unit == textInfos.UNIT_WORD:
# UIA doesn't implement word movement, so we need to do it manually.
lineInfo = self.copy()
lineInfo.expand(textInfos.UNIT_LINE)
offset = self._getCurrentOffsetInThisLine(lineInfo)
start, end = self._getWordOffsetsInThisLine(offset, lineInfo)
wordEndPoints = (
(offset - start) * -1,
end - offset - 1
)
if wordEndPoints[0]:
self._rangeObj.MoveEndpointByUnit(
UIAHandler.TextPatternRangeEndpoint_Start,
UIAHandler.NVDAUnitsToUIAUnits[textInfos.UNIT_CHARACTER],
wordEndPoints[0]
)
if wordEndPoints[1]:
self._rangeObj.MoveEndpointByUnit(
UIAHandler.TextPatternRangeEndpoint_End,
UIAHandler.NVDAUnitsToUIAUnits[textInfos.UNIT_CHARACTER],
wordEndPoints[1]
)
else:
return super(consoleUIATextInfo, self).expand(unit)
def _getCurrentOffsetInThisLine(self, lineInfo):
charInfo = self.copy()
res = 0
chars = None
while charInfo.compareEndPoints(
lineInfo,
"startToEnd"
) <= 0:
charInfo.expand(textInfos.UNIT_CHARACTER)
chars = charInfo.move(textInfos.UNIT_CHARACTER, -1) * -1
if chars != 0 and charInfo.compareEndPoints(
lineInfo,
"startToStart"
) >= 0:
res += chars
else:
break
return res
def _getWordOffsetsInThisLine(self, offset, lineInfo):
lineText = lineInfo.text
# Convert NULL and non-breaking space to space to make sure
# that words will break on them
lineText = lineText.translate({0: u' ', 0xa0: u' '})
start = ctypes.c_int()
end = ctypes.c_int()
# Uniscribe does some strange things when you give it a string with
# not more than two alphanumeric chars in a row.
# Inject two alphanumeric characters at the end to fix this.
lineText += "xx"
NVDAHelper.localLib.calculateWordOffsets(
lineText,
len(lineText),
offset,
ctypes.byref(start),
ctypes.byref(end)
)
return (
start.value,
min(end.value, len(lineText) - 2)
)
class winConsoleUIA(Terminal):
STABILIZE_DELAY = 0.03
_TextInfo = consoleUIATextInfo
_isTyping = False
_lastCharTime = 0
_TYPING_TIMEOUT = 1
def _reportNewText(self, line):
# Additional typed character filtering beyond that in LiveText
if (
self._isTyping
and time.time() - self._lastCharTime <= self._TYPING_TIMEOUT
):
return
super(winConsoleUIA, self)._reportNewText(line)
def event_typedCharacter(self, ch):
if not ch.isspace():
self._isTyping = True
if ch in ('\n', '\r', '\t'):
# Clear the typed word buffer for tab and return.
# This will need to be changed once #8110 is merged.
speech.curWordChars = []
self._lastCharTime = time.time()
super(winConsoleUIA, self).event_typedCharacter(ch)
@script(gestures=[
"kb:enter",
"kb:numpadEnter",
"kb:tab",
"kb:control+c",
"kb:control+d",
"kb:control+pause"
])
def script_clear_isTyping(self, gesture):
gesture.send()
self._isTyping = False
def _getTextLines(self):
# Filter out extraneous empty lines from UIA
ptr = self.UIATextPattern.GetVisibleRanges()
res = [ptr.GetElement(i).GetText(-1) for i in range(ptr.length)]
return res