Skip to content

Commit 4571d42

Browse files
committed
Polish non-testing area. Up next: testing polish
1 parent e44db54 commit 4571d42

File tree

6 files changed

+80
-73
lines changed

6 files changed

+80
-73
lines changed

src/buffer/out/textBuffer.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,6 @@ const COORD TextBuffer::GetWordEnd(const COORD target, const std::wstring_view w
11051105
}
11061106
}
11071107

1108-
11091108
// Method Description:
11101109
// - Helper method for GetWordEnd(). Get the COORD for the beginning of the next READABLE word
11111110
// Arguments:

src/cascadia/TerminalControl/XamlUiaTextRange.cpp

+6-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include "XamlUiaTextRange.h"
66
#include "UiaTextRange.hpp"
77

8+
// the same as COR_E_NOTSUPPORTED
9+
// we don't want to import the CLR headers to get it
10+
#define XAML_E_NOT_SUPPORTED 0x80131515L
11+
812
namespace UIA
913
{
1014
using ::ITextRangeProvider;
@@ -24,18 +28,6 @@ namespace XamlAutomation
2428

2529
namespace winrt::Microsoft::Terminal::TerminalControl::implementation
2630
{
27-
// EmptyObject is our equivalent of UIAutomationCore::UiaGetReservedNotSupportedValue()
28-
// This retrieves a value that is interpreted as "not supported".
29-
class EmptyObject : public winrt::implements<EmptyObject, IInspectable>
30-
{
31-
public:
32-
static IInspectable GetInstance()
33-
{
34-
static auto empty = make_self<EmptyObject>();
35-
return *empty;
36-
}
37-
};
38-
3931
XamlAutomation::ITextRangeProvider XamlUiaTextRange::Clone() const
4032
{
4133
UIA::ITextRangeProvider* pReturn;
@@ -100,9 +92,9 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
10092
}
10193
else
10294
{
103-
// We _need_ to return an empty object here.
95+
// We _need_ to return XAML_E_NOT_SUPPORTED here.
10496
// Returning nullptr is an improper implementation of it being unsupported.
105-
return EmptyObject::GetInstance();
97+
winrt::throw_hresult(XAML_E_NOT_SUPPORTED);
10698
}
10799
}
108100

src/inc/LibraryIncludes.h

+3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@
7070
#include <CppCoreCheck/Warnings.h>
7171

7272
// Chromium Numerics (safe math)
73+
#pragma warning(push)
74+
#pragma warning(disable:4100) // unreferenced parameter
7375
#include <base/numerics/safe_math.h>
76+
#pragma warning(pop)
7477

7578
// IntSafe
7679
#define ENABLE_INTSAFE_SIGNED_FUNCTIONS

src/types/ScreenInfoUiaProviderBase.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ IFACEMETHODIMP ScreenInfoUiaProviderBase::GetVisibleRanges(_Outptr_result_mayben
352352
}
353353

354354
// stuff each visible line in the safearray
355-
for (SHORT i = 0; i < rowCount; ++i)
355+
for (short i = 0; i < rowCount; ++i)
356356
{
357357
// end is exclusive so add 1
358-
const COORD start { viewport.Left(), viewport.Top() + i };
359-
const COORD end { start.X, start.Y + 1 };
358+
const COORD start{ viewport.Left(), viewport.Top() + i };
359+
const COORD end{ start.X, start.Y + 1 };
360360

361361
HRESULT hr = S_OK;
362362
WRL::ComPtr<UiaTextRangeBase> range;

src/types/UiaTextRangeBase.cpp

+35-34
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ IFACEMETHODIMP UiaTextRangeBase::Compare(_In_opt_ ITextRangeProvider* pRange, _O
239239
if (other)
240240
{
241241
*pRetVal = (_start == other->GetEndpoint(TextPatternRangeEndpoint_Start) &&
242-
_end == other->GetEndpoint(TextPatternRangeEndpoint_End) &&
243-
IsDegenerate() == other->IsDegenerate());
242+
_end == other->GetEndpoint(TextPatternRangeEndpoint_End) &&
243+
IsDegenerate() == other->IsDegenerate());
244244
}
245245
// TODO GitHub #1914: Re-attach Tracing to UIA Tree
246246
// tracing
@@ -314,14 +314,15 @@ IFACEMETHODIMP UiaTextRangeBase::ExpandToEnclosingUnit(_In_ TextUnit unit)
314314
else if (unit <= TextUnit::TextUnit_Word)
315315
{
316316
// expand to word
317-
_start = buffer.GetWordStart(_start, _wordDelimiters, /*accessibilityMode*/ true);
318-
_end = buffer.GetWordEnd(_start, _wordDelimiters, /*accessibilityMode*/ true);
317+
_start = buffer.GetWordStart(_start, _wordDelimiters, true);
318+
_end = buffer.GetWordEnd(_start, _wordDelimiters, true);
319319
}
320320
else if (unit <= TextUnit::TextUnit_Line)
321321
{
322322
// expand to line
323323
_start.X = 0;
324-
_end.X = base::ClampAdd(_start.Y, 1);
324+
_end.X = 0;
325+
_end.Y = base::ClampAdd(_start.Y, 1);
325326
}
326327
else
327328
{
@@ -411,12 +412,11 @@ IFACEMETHODIMP UiaTextRangeBase::GetBoundingRectangles(_Outptr_result_maybenull_
411412
// _end is exclusive, let's be inclusive so we don't have to think about it anymore for bounding rects
412413
bufferSize.DecrementInBounds(endAnchor, true);
413414

414-
if (IsDegenerate())
415-
{
416-
_getBoundingRect(_start, _start, coords);
417-
}
418-
else if (bufferSize.CompareInBounds(_start, viewportEnd, true) > 0 || bufferSize.CompareInBounds(_end, viewportOrigin, true) < 0)
415+
if (IsDegenerate() || bufferSize.CompareInBounds(_start, viewportEnd, true) > 0 || bufferSize.CompareInBounds(_end, viewportOrigin, true) < 0)
419416
{
417+
// An empty array is returned for a degenerate (empty) text range or for a text range
418+
// reference: https://docs.microsoft.com/en-us/windows/win32/api/uiautomationclient/nf-uiautomationclient-iuiautomationtextrange-getboundingrectangles
419+
420420
// Remember, start cannot be past end, so
421421
// if start is past the viewport end,
422422
// or end is past the viewport origin
@@ -515,8 +515,8 @@ IFACEMETHODIMP UiaTextRangeBase::GetText(_In_ int maxLength, _Out_ BSTR* pRetVal
515515
// if _end is at 0, we ignore that row because _end is exclusive
516516
const auto& buffer = _pData->GetTextBuffer();
517517
const SHORT totalRowsInRange = (_end.X == buffer.GetSize().Left()) ?
518-
_end.Y - _start.Y :
519-
_end.Y - _start.Y + static_cast<SHORT>(1);
518+
_end.Y - _start.Y :
519+
_end.Y - _start.Y + static_cast<SHORT>(1);
520520
const SHORT lastRowInRange = _start.Y + totalRowsInRange - static_cast<SHORT>(1);
521521

522522
SHORT currentScreenInfoRow = 0;
@@ -811,11 +811,11 @@ IFACEMETHODIMP UiaTextRangeBase::ScrollIntoView(_In_ BOOL alignToTop)
811811
const auto oldViewport = _pData->GetViewport().ToInclusive();
812812
const auto viewportHeight = _getViewportHeight(oldViewport);
813813
// range rows
814-
const auto startScreenInfoRow = _start.Y;
815-
const auto endScreenInfoRow = _end.Y;
814+
const base::ClampedNumeric<short> startScreenInfoRow = _start.Y;
815+
const base::ClampedNumeric<short> endScreenInfoRow = _end.Y;
816816
// screen buffer rows
817-
const auto topRow = 0;
818-
const auto bottomRow = _pData->GetTextBuffer().TotalRowCount() - 1;
817+
const base::ClampedNumeric<short> topRow = 0;
818+
const base::ClampedNumeric<short> bottomRow = _pData->GetTextBuffer().TotalRowCount() - 1;
819819

820820
SMALL_RECT newViewport = oldViewport;
821821

@@ -827,15 +827,15 @@ IFACEMETHODIMP UiaTextRangeBase::ScrollIntoView(_In_ BOOL alignToTop)
827827
if (startScreenInfoRow + viewportHeight <= bottomRow)
828828
{
829829
// we can align to the top
830-
newViewport.Top = gsl::narrow<SHORT>(startScreenInfoRow);
831-
newViewport.Bottom = gsl::narrow<SHORT>(startScreenInfoRow + viewportHeight - 1);
830+
newViewport.Top = startScreenInfoRow;
831+
newViewport.Bottom = startScreenInfoRow + viewportHeight - 1;
832832
}
833833
else
834834
{
835835
// we can align to the top so we'll just move the viewport
836836
// to the bottom of the screen buffer
837-
newViewport.Bottom = gsl::narrow<SHORT>(bottomRow);
838-
newViewport.Top = gsl::narrow<SHORT>(bottomRow - viewportHeight + 1);
837+
newViewport.Bottom = bottomRow;
838+
newViewport.Top = bottomRow - viewportHeight + 1;
839839
}
840840
}
841841
else
@@ -845,20 +845,20 @@ IFACEMETHODIMP UiaTextRangeBase::ScrollIntoView(_In_ BOOL alignToTop)
845845
if (static_cast<unsigned int>(endScreenInfoRow) >= viewportHeight)
846846
{
847847
// we can align to bottom
848-
newViewport.Bottom = gsl::narrow<SHORT>(endScreenInfoRow);
849-
newViewport.Top = gsl::narrow<SHORT>(endScreenInfoRow - viewportHeight + 1);
848+
newViewport.Bottom = endScreenInfoRow;
849+
newViewport.Top = endScreenInfoRow - viewportHeight + 1;
850850
}
851851
else
852852
{
853853
// we can't align to bottom so we'll move the viewport to
854854
// the top of the screen buffer
855-
newViewport.Top = gsl::narrow<SHORT>(topRow);
856-
newViewport.Bottom = gsl::narrow<SHORT>(topRow + viewportHeight - 1);
855+
newViewport.Top = topRow;
856+
newViewport.Bottom = topRow + viewportHeight - 1;
857857
}
858858
}
859859

860-
FAIL_FAST_IF(!(newViewport.Top >= gsl::narrow<SHORT>(topRow)));
861-
FAIL_FAST_IF(!(newViewport.Bottom <= gsl::narrow<SHORT>(bottomRow)));
860+
FAIL_FAST_IF(!(newViewport.Top >= topRow));
861+
FAIL_FAST_IF(!(newViewport.Bottom <= bottomRow));
862862
FAIL_FAST_IF(!(_getViewportHeight(oldViewport) == _getViewportHeight(newViewport)));
863863

864864
_ChangeViewport(newViewport);
@@ -938,30 +938,31 @@ void UiaTextRangeBase::_getBoundingRect(_In_ const COORD startAnchor, _In_ const
938938
// startAnchor is converted to the viewport coordinate space
939939
auto startCoord = startAnchor;
940940
viewport.ConvertToOrigin(&startCoord);
941-
topLeft.x = startCoord.X * currentFontSize.X;
942-
topLeft.y = startCoord.Y * currentFontSize.Y;
941+
topLeft.x = base::ClampMul<long, long>(startCoord.X, currentFontSize.X);
942+
topLeft.y = base::ClampMul<long, long>(startCoord.Y, currentFontSize.Y);
943943

944944
if (IsDegenerate())
945945
{
946-
bottomRight.x = (startCoord.X) * currentFontSize.X;
947-
bottomRight.y = (startCoord.Y + 1) * currentFontSize.Y;
946+
// An empty array is returned for a degenerate (empty) text range or for a text range
947+
// reference: https://docs.microsoft.com/en-us/windows/win32/api/uiautomationclient/nf-uiautomationclient-iuiautomationtextrange-getboundingrectangles
948+
return;
948949
}
949950
else
950951
{
951952
// endAnchor is converted to the viewport coordinate space
952953
auto endCoord = endAnchor;
953954
viewport.ConvertToOrigin(&endCoord);
954-
bottomRight.x = (endCoord.X + 1) * currentFontSize.X;
955-
bottomRight.y = (endCoord.Y + 1) * currentFontSize.Y;
955+
bottomRight.x = base::ClampMul<long, long>(endCoord.X, currentFontSize.X);
956+
bottomRight.y = base::ClampMul<long, long>(base::ClampAdd(endCoord.Y, 1), currentFontSize.Y);
956957
}
957958

958959
// convert the coords to be relative to the screen instead of
959960
// the client window
960961
_TranslatePointToScreen(&topLeft);
961962
_TranslatePointToScreen(&bottomRight);
962963

963-
const LONG width = bottomRight.x - topLeft.x;
964-
const LONG height = bottomRight.y - topLeft.y;
964+
const long width = base::ClampSub(bottomRight.x, topLeft.x);
965+
const long height = base::ClampSub(bottomRight.y, topLeft.y);
965966

966967
// insert the coords
967968
coords.push_back(topLeft.x);

src/types/viewport.cpp

+33-21
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ bool Viewport::IsInBounds(const Viewport& other) const noexcept
179179
// - Determines if the given coordinate position lies within this viewport.
180180
// Arguments:
181181
// - pos - Coordinate position
182-
// - allowBottomExclusive - if true, allow the X position to be on the BottomExclusive boundary
182+
// - allowEndExclusive - if true, allow the EndExclusive COORD as a valid position.
183+
// Used in accessibility to signify that the exclusive end
184+
// includes the last COORD in a given viewport.
183185
// Return Value:
184186
// - True if it lies inside the viewport. False otherwise.
185-
bool Viewport::IsInBounds(const COORD& pos, bool allowBottomExclusive) const noexcept
187+
bool Viewport::IsInBounds(const COORD& pos, bool allowEndExclusive) const noexcept
186188
{
187-
if (allowBottomExclusive && pos.X == Left() && pos.Y == BottomExclusive())
189+
if (allowEndExclusive && pos == EndExclusive())
188190
{
189191
return true;
190192
}
@@ -274,12 +276,14 @@ bool Viewport::MoveInBounds(const ptrdiff_t move, COORD& pos) const noexcept
274276
// - Increments the given coordinate within the bounds of this viewport.
275277
// Arguments:
276278
// - pos - Coordinate position that will be incremented, if it can be.
277-
// - allowBottomExclusive - if true, allow the X position to be on the BottomExclusive boundary
279+
// - allowEndExclusive - if true, allow the EndExclusive COORD as a valid position.
280+
// Used in accessibility to signify that the exclusive end
281+
// includes the last COORD in a given viewport.
278282
// Return Value:
279283
// - True if it could be incremented. False if it would move outside.
280-
bool Viewport::IncrementInBounds(COORD& pos, bool allowBottomExclusive) const noexcept
284+
bool Viewport::IncrementInBounds(COORD& pos, bool allowEndExclusive) const noexcept
281285
{
282-
return WalkInBounds(pos, { XWalk::LeftToRight, YWalk::TopToBottom }, allowBottomExclusive);
286+
return WalkInBounds(pos, { XWalk::LeftToRight, YWalk::TopToBottom }, allowEndExclusive);
283287
}
284288

285289
// Method Description:
@@ -299,12 +303,14 @@ bool Viewport::IncrementInBoundsCircular(COORD& pos) const noexcept
299303
// - Decrements the given coordinate within the bounds of this viewport.
300304
// Arguments:
301305
// - pos - Coordinate position that will be incremented, if it can be.
302-
// - allowBottomExclusive - if true, allow the X position to be on the BottomExclusive boundary
306+
// - allowEndExclusive - if true, allow the EndExclusive COORD as a valid position.
307+
// Used in accessibility to signify that the exclusive end
308+
// includes the last COORD in a given viewport.
303309
// Return Value:
304310
// - True if it could be incremented. False if it would move outside.
305-
bool Viewport::DecrementInBounds(COORD& pos, bool allowBottomExclusive) const noexcept
311+
bool Viewport::DecrementInBounds(COORD& pos, bool allowEndExclusive) const noexcept
306312
{
307-
return WalkInBounds(pos, { XWalk::RightToLeft, YWalk::BottomToTop }, allowBottomExclusive);
313+
return WalkInBounds(pos, { XWalk::RightToLeft, YWalk::BottomToTop }, allowEndExclusive);
308314
}
309315

310316
// Method Description:
@@ -325,19 +331,21 @@ bool Viewport::DecrementInBoundsCircular(COORD& pos) const noexcept
325331
// Arguments:
326332
// - first- The first coordinate position
327333
// - second - The second coordinate position
328-
// - allowBottomExclusive - if true, allow the COORD's Y value to be BottomExclusive when X = Left
334+
// - allowEndExclusive - if true, allow the EndExclusive COORD as a valid position.
335+
// Used in accessibility to signify that the exclusive end
336+
// includes the last COORD in a given viewport.
329337
// Return Value:
330338
// - Negative if First is to the left of the Second.
331339
// - 0 if First and Second are the same coordinate.
332340
// - Positive if First is to the right of the Second.
333341
// - This is so you can do s_CompareCoords(first, second) <= 0 for "first is left or the same as second".
334342
// (the < looks like a left arrow :D)
335343
// - The magnitude of the result is the distance between the two coordinates when typing characters into the buffer (left to right, top to bottom)
336-
int Viewport::CompareInBounds(const COORD& first, const COORD& second, bool allowBottomExclusive) const noexcept
344+
int Viewport::CompareInBounds(const COORD& first, const COORD& second, bool allowEndExclusive) const noexcept
337345
{
338346
// Assert that our coordinates are within the expected boundaries
339-
FAIL_FAST_IF(!IsInBounds(first, allowBottomExclusive));
340-
FAIL_FAST_IF(!IsInBounds(second, allowBottomExclusive));
347+
FAIL_FAST_IF(!IsInBounds(first, allowEndExclusive));
348+
FAIL_FAST_IF(!IsInBounds(second, allowEndExclusive));
341349

342350
// First set the distance vertically
343351
// If first is on row 4 and second is on row 6, first will be -2 rows behind second * an 80 character row would be -160.
@@ -364,13 +372,15 @@ int Viewport::CompareInBounds(const COORD& first, const COORD& second, bool allo
364372
// Arguments:
365373
// - pos - Coordinate position that will be adjusted, if it can be.
366374
// - dir - Walking direction specifying which direction to go when reaching the end of a row/column
367-
// - allowBottomExclusive - if true, allow the X position to be on the BottomExclusive boundary
375+
// - allowEndExclusive - if true, allow the EndExclusive COORD as a valid position.
376+
// Used in accessibility to signify that the exclusive end
377+
// includes the last COORD in a given viewport.
368378
// Return Value:
369379
// - True if it could be adjusted as specified and remain in bounds. False if it would move outside.
370-
bool Viewport::WalkInBounds(COORD& pos, const WalkDir dir, bool allowBottomExclusive) const noexcept
380+
bool Viewport::WalkInBounds(COORD& pos, const WalkDir dir, bool allowEndExclusive) const noexcept
371381
{
372382
auto copy = pos;
373-
if (WalkInBoundsCircular(copy, dir, allowBottomExclusive))
383+
if (WalkInBoundsCircular(copy, dir, allowEndExclusive))
374384
{
375385
pos = copy;
376386
return true;
@@ -388,19 +398,21 @@ bool Viewport::WalkInBounds(COORD& pos, const WalkDir dir, bool allowBottomExclu
388398
// Arguments:
389399
// - pos - Coordinate position that will be adjusted.
390400
// - dir - Walking direction specifying which direction to go when reaching the end of a row/column
391-
// - allowBottomExclusive - if true, allow the X position to be on the BottomExclusive boundary
401+
// - allowEndExclusive - if true, allow the EndExclusive COORD as a valid position.
402+
// Used in accessibility to signify that the exclusive end
403+
// includes the last COORD in a given viewport.
392404
// Return Value:
393405
// - True if it could be adjusted inside the viewport.
394406
// - False if it rolled over from the final corner back to the initial corner
395407
// for the specified walk direction.
396-
bool Viewport::WalkInBoundsCircular(COORD& pos, const WalkDir dir, bool allowBottomExclusive) const noexcept
408+
bool Viewport::WalkInBoundsCircular(COORD& pos, const WalkDir dir, bool allowEndExclusive) const noexcept
397409
{
398410
// Assert that the position given fits inside this viewport.
399-
FAIL_FAST_IF(!IsInBounds(pos, allowBottomExclusive));
411+
FAIL_FAST_IF(!IsInBounds(pos, allowEndExclusive));
400412

401413
if (dir.x == XWalk::LeftToRight)
402414
{
403-
if (allowBottomExclusive && pos.X == Left() && pos.Y == BottomExclusive())
415+
if (allowEndExclusive && pos.X == Left() && pos.Y == BottomExclusive())
404416
{
405417
pos.Y = Top();
406418
return false;
@@ -412,7 +424,7 @@ bool Viewport::WalkInBoundsCircular(COORD& pos, const WalkDir dir, bool allowBot
412424
if (dir.y == YWalk::TopToBottom)
413425
{
414426
pos.Y++;
415-
if (allowBottomExclusive && pos.Y == BottomExclusive())
427+
if (allowEndExclusive && pos.Y == BottomExclusive())
416428
{
417429
return true;
418430
}

0 commit comments

Comments
 (0)