|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +#include "pch.h" |
| 5 | +#include <UIAutomationCore.h> |
| 6 | +#include "TermControlAutomationPeer.h" |
| 7 | +#include "TermControl.h" |
| 8 | +#include "TermControlAutomationPeer.g.cpp" |
| 9 | + |
| 10 | +#include "XamlUiaTextRange.h" |
| 11 | + |
| 12 | +using namespace Microsoft::Console::Types; |
| 13 | +using namespace winrt::Windows::UI::Xaml::Automation::Peers; |
| 14 | + |
| 15 | +namespace UIA |
| 16 | +{ |
| 17 | + using ::ITextRangeProvider; |
| 18 | + using ::SupportedTextSelection; |
| 19 | +} |
| 20 | + |
| 21 | +namespace XamlAutomation |
| 22 | +{ |
| 23 | + using winrt::Windows::UI::Xaml::Automation::SupportedTextSelection; |
| 24 | + using winrt::Windows::UI::Xaml::Automation::Provider::IRawElementProviderSimple; |
| 25 | + using winrt::Windows::UI::Xaml::Automation::Provider::ITextRangeProvider; |
| 26 | +} |
| 27 | + |
| 28 | +namespace winrt::Microsoft::Terminal::TerminalControl::implementation |
| 29 | +{ |
| 30 | + TermControlAutomationPeer::TermControlAutomationPeer(winrt::Microsoft::Terminal::TerminalControl::implementation::TermControl const& owner) : |
| 31 | + TermControlAutomationPeerT<TermControlAutomationPeer>(owner), // pass owner to FrameworkElementAutomationPeer |
| 32 | + _uiaProvider{ owner.GetRenderData(), nullptr, std::bind(&TermControlAutomationPeer::GetBoundingRectWrapped, this) } {}; |
| 33 | + |
| 34 | + winrt::hstring TermControlAutomationPeer::GetClassNameCore() const |
| 35 | + { |
| 36 | + return L"TermControl"; |
| 37 | + } |
| 38 | + |
| 39 | + AutomationControlType TermControlAutomationPeer::GetAutomationControlTypeCore() const |
| 40 | + { |
| 41 | + return AutomationControlType::Text; |
| 42 | + } |
| 43 | + |
| 44 | + winrt::hstring TermControlAutomationPeer::GetLocalizedControlTypeCore() const |
| 45 | + { |
| 46 | + // TODO GitHub #2142: Localize string |
| 47 | + return L"TerminalControl"; |
| 48 | + } |
| 49 | + |
| 50 | + winrt::Windows::Foundation::IInspectable TermControlAutomationPeer::GetPatternCore(PatternInterface patternInterface) const |
| 51 | + { |
| 52 | + switch (patternInterface) |
| 53 | + { |
| 54 | + case PatternInterface::Text: |
| 55 | + return *this; |
| 56 | + break; |
| 57 | + default: |
| 58 | + return nullptr; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | +#pragma region ITextProvider |
| 63 | + winrt::com_array<XamlAutomation::ITextRangeProvider> TermControlAutomationPeer::GetSelection() |
| 64 | + { |
| 65 | + SAFEARRAY* pReturnVal; |
| 66 | + THROW_IF_FAILED(_uiaProvider.GetSelection(&pReturnVal)); |
| 67 | + return WrapArrayOfTextRangeProviders(pReturnVal); |
| 68 | + } |
| 69 | + |
| 70 | + winrt::com_array<XamlAutomation::ITextRangeProvider> TermControlAutomationPeer::GetVisibleRanges() |
| 71 | + { |
| 72 | + SAFEARRAY* pReturnVal; |
| 73 | + THROW_IF_FAILED(_uiaProvider.GetVisibleRanges(&pReturnVal)); |
| 74 | + return WrapArrayOfTextRangeProviders(pReturnVal); |
| 75 | + } |
| 76 | + |
| 77 | + XamlAutomation::ITextRangeProvider TermControlAutomationPeer::RangeFromChild(XamlAutomation::IRawElementProviderSimple childElement) |
| 78 | + { |
| 79 | + UIA::ITextRangeProvider* returnVal; |
| 80 | + // ScreenInfoUiaProvider doesn't actually use parameter, so just pass in nullptr |
| 81 | + THROW_IF_FAILED(_uiaProvider.RangeFromChild(/* IRawElementProviderSimple */ nullptr, |
| 82 | + &returnVal)); |
| 83 | + |
| 84 | + auto parentProvider = this->ProviderFromPeer(*this); |
| 85 | + auto xutr = winrt::make_self<XamlUiaTextRange>(returnVal, parentProvider); |
| 86 | + return xutr.as<XamlAutomation::ITextRangeProvider>(); |
| 87 | + } |
| 88 | + |
| 89 | + XamlAutomation::ITextRangeProvider TermControlAutomationPeer::RangeFromPoint(Windows::Foundation::Point screenLocation) |
| 90 | + { |
| 91 | + UIA::ITextRangeProvider* returnVal; |
| 92 | + THROW_IF_FAILED(_uiaProvider.RangeFromPoint({ screenLocation.X, screenLocation.Y }, &returnVal)); |
| 93 | + |
| 94 | + auto parentProvider = this->ProviderFromPeer(*this); |
| 95 | + auto xutr = winrt::make_self<XamlUiaTextRange>(returnVal, parentProvider); |
| 96 | + return xutr.as<XamlAutomation::ITextRangeProvider>(); |
| 97 | + } |
| 98 | + |
| 99 | + XamlAutomation::ITextRangeProvider TermControlAutomationPeer::DocumentRange() |
| 100 | + { |
| 101 | + UIA::ITextRangeProvider* returnVal; |
| 102 | + THROW_IF_FAILED(_uiaProvider.get_DocumentRange(&returnVal)); |
| 103 | + |
| 104 | + auto parentProvider = this->ProviderFromPeer(*this); |
| 105 | + auto xutr = winrt::make_self<XamlUiaTextRange>(returnVal, parentProvider); |
| 106 | + return xutr.as<XamlAutomation::ITextRangeProvider>(); |
| 107 | + } |
| 108 | + |
| 109 | + Windows::UI::Xaml::Automation::SupportedTextSelection TermControlAutomationPeer::SupportedTextSelection() |
| 110 | + { |
| 111 | + UIA::SupportedTextSelection returnVal; |
| 112 | + THROW_IF_FAILED(_uiaProvider.get_SupportedTextSelection(&returnVal)); |
| 113 | + return static_cast<XamlAutomation::SupportedTextSelection>(returnVal); |
| 114 | + } |
| 115 | + |
| 116 | +#pragma endregion |
| 117 | + |
| 118 | + RECT TermControlAutomationPeer::GetBoundingRectWrapped() |
| 119 | + { |
| 120 | + auto rect = GetBoundingRectangle(); |
| 121 | + return { |
| 122 | + gsl::narrow<LONG>(rect.X), |
| 123 | + gsl::narrow<LONG>(rect.Y), |
| 124 | + gsl::narrow<LONG>(rect.X + rect.Width), |
| 125 | + gsl::narrow<LONG>(rect.Y + rect.Height) |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + // Method Description: |
| 130 | + // - extracts the UiaTextRanges from the SAFEARRAY and converts them to Xaml ITextRangeProviders |
| 131 | + // Arguments: |
| 132 | + // - SAFEARRAY of UIA::UiaTextRange (ITextRangeProviders) |
| 133 | + // Return Value: |
| 134 | + // - com_array of Xaml Wrapped UiaTextRange (ITextRangeProviders) |
| 135 | + winrt::com_array<XamlAutomation::ITextRangeProvider> TermControlAutomationPeer::WrapArrayOfTextRangeProviders(SAFEARRAY* textRanges) |
| 136 | + { |
| 137 | + // transfer ownership of UiaTextRanges to this new vector |
| 138 | + auto providers = SafeArrayToOwningVector<::Microsoft::Console::Types::UiaTextRange>(textRanges); |
| 139 | + int count = providers.size(); |
| 140 | + |
| 141 | + std::vector<XamlAutomation::ITextRangeProvider> vec; |
| 142 | + vec.reserve(count); |
| 143 | + auto parentProvider = this->ProviderFromPeer(*this); |
| 144 | + for (int i = 0; i < count; i++) |
| 145 | + { |
| 146 | + auto xutr = winrt::make_self<XamlUiaTextRange>(providers[i].detach(), parentProvider); |
| 147 | + vec.emplace_back(xutr.as<XamlAutomation::ITextRangeProvider>()); |
| 148 | + } |
| 149 | + |
| 150 | + winrt::com_array<XamlAutomation::ITextRangeProvider> result{ vec }; |
| 151 | + |
| 152 | + return result; |
| 153 | + } |
| 154 | +} |
0 commit comments