|
| 1 | +#include "pch.h" |
| 2 | +#include "CompositionDynamicAutomationProvider.h" |
| 3 | +#include <Fabric/ComponentView.h> |
| 4 | +#pragma warning(push) |
| 5 | +#pragma warning(disable : 4229) |
| 6 | +#define IN |
| 7 | +#define OUT |
| 8 | +#include <atlsafe.h> |
| 9 | +#pragma warning(pop) |
| 10 | +#include "RootComponentView.h" |
| 11 | +#include "UiaHelpers.h" |
| 12 | + |
| 13 | +namespace winrt::Microsoft::ReactNative::implementation { |
| 14 | + |
| 15 | +CompositionDynamicAutomationProvider::CompositionDynamicAutomationProvider( |
| 16 | + const std::shared_ptr<::Microsoft::ReactNative::CompositionBaseComponentView> &componentView) noexcept |
| 17 | + : m_view{std::static_pointer_cast<::Microsoft::ReactNative::IComponentView>(componentView)} {} |
| 18 | + |
| 19 | +HRESULT __stdcall CompositionDynamicAutomationProvider::Navigate( |
| 20 | + NavigateDirection direction, |
| 21 | + IRawElementProviderFragment **pRetVal) { |
| 22 | + if (pRetVal == nullptr) |
| 23 | + return E_POINTER; |
| 24 | + |
| 25 | + return UiaNavigateHelper(m_view, direction, *pRetVal); |
| 26 | +} |
| 27 | + |
| 28 | +// Implementations should return NULL for a top-level element that is hosted in a window. Other elements should return |
| 29 | +// an array that contains UiaAppendRuntimeId (defined in Uiautomationcoreapi.h), followed by a value that is unique |
| 30 | +// within an instance of the fragment. |
| 31 | +// |
| 32 | +// We'll use the react tag as our identifier for those situations |
| 33 | +HRESULT __stdcall CompositionDynamicAutomationProvider::GetRuntimeId(SAFEARRAY **pRetVal) { |
| 34 | + if (pRetVal == nullptr) |
| 35 | + return E_POINTER; |
| 36 | + |
| 37 | + *pRetVal = nullptr; |
| 38 | + |
| 39 | + auto strongView = m_view.view(); |
| 40 | + |
| 41 | + if (!strongView) |
| 42 | + return UIA_E_ELEMENTNOTAVAILABLE; |
| 43 | + |
| 44 | + CComSafeArray<int32_t> runtimeId; |
| 45 | + auto hr = runtimeId.Create(2); |
| 46 | + |
| 47 | + if (FAILED(hr)) |
| 48 | + return hr; |
| 49 | + |
| 50 | + runtimeId[0] = UiaAppendRuntimeId; |
| 51 | + runtimeId[1] = strongView->tag(); |
| 52 | + |
| 53 | + *pRetVal = runtimeId.Detach(); |
| 54 | + |
| 55 | + return S_OK; |
| 56 | +} |
| 57 | + |
| 58 | +HRESULT __stdcall CompositionDynamicAutomationProvider::get_BoundingRectangle(UiaRect *pRetVal) { |
| 59 | + if (pRetVal == nullptr) |
| 60 | + return E_POINTER; |
| 61 | + |
| 62 | + return S_OK; |
| 63 | +} |
| 64 | + |
| 65 | +HRESULT __stdcall CompositionDynamicAutomationProvider::GetEmbeddedFragmentRoots(SAFEARRAY **pRetVal) { |
| 66 | + if (pRetVal == nullptr) |
| 67 | + return E_POINTER; |
| 68 | + |
| 69 | + *pRetVal = nullptr; |
| 70 | + |
| 71 | + return S_OK; |
| 72 | +} |
| 73 | + |
| 74 | +HRESULT __stdcall CompositionDynamicAutomationProvider::SetFocus(void) { |
| 75 | + return S_OK; |
| 76 | +} |
| 77 | + |
| 78 | +HRESULT __stdcall CompositionDynamicAutomationProvider::get_FragmentRoot(IRawElementProviderFragmentRoot **pRetVal) { |
| 79 | + if (pRetVal == nullptr) |
| 80 | + return E_POINTER; |
| 81 | + |
| 82 | + auto strongView = m_view.view(); |
| 83 | + |
| 84 | + if (!strongView) |
| 85 | + return UIA_E_ELEMENTNOTAVAILABLE; |
| 86 | + |
| 87 | + auto rootCV = strongView->rootComponentView(); |
| 88 | + if (rootCV == nullptr) |
| 89 | + return UIA_E_ELEMENTNOTAVAILABLE; |
| 90 | + |
| 91 | + auto uiaProvider = rootCV->EnsureUiaProvider(); |
| 92 | + if (uiaProvider != nullptr) { |
| 93 | + winrt::com_ptr<IRawElementProviderFragmentRoot> spReps; |
| 94 | + uiaProvider.as(spReps); |
| 95 | + *pRetVal = spReps.detach(); |
| 96 | + } |
| 97 | + |
| 98 | + return S_OK; |
| 99 | +} |
| 100 | + |
| 101 | +HRESULT __stdcall CompositionDynamicAutomationProvider::get_ProviderOptions(ProviderOptions *pRetVal) { |
| 102 | + if (pRetVal == nullptr) |
| 103 | + return E_POINTER; |
| 104 | + |
| 105 | + *pRetVal = ProviderOptions_ServerSideProvider | ProviderOptions_UseComThreading; |
| 106 | + return S_OK; |
| 107 | +} |
| 108 | + |
| 109 | +HRESULT __stdcall CompositionDynamicAutomationProvider::GetPatternProvider(PATTERNID patternId, IUnknown **pRetVal) { |
| 110 | + if (pRetVal == nullptr) |
| 111 | + return E_POINTER; |
| 112 | + |
| 113 | + *pRetVal = nullptr; |
| 114 | + |
| 115 | + return S_OK; |
| 116 | +} |
| 117 | + |
| 118 | +long GetControlType(const std::string &role) noexcept { |
| 119 | + if (role == "adjustable") { |
| 120 | + return UIA_SliderControlTypeId; |
| 121 | + } else if (role == "group" || role == "search" || role == "radiogroup" || role == "timer" || role.empty()) { |
| 122 | + return UIA_GroupControlTypeId; |
| 123 | + } else if (role == "button" || role == "imagebutton" || role == "switch" || role == "togglebutton") { |
| 124 | + return UIA_ButtonControlTypeId; |
| 125 | + } else if (role == "checkbox") { |
| 126 | + return UIA_CheckBoxControlTypeId; |
| 127 | + } else if (role == "combobox") { |
| 128 | + return UIA_ComboBoxControlTypeId; |
| 129 | + } else if (role == "alert" || role == "header" || role == "summary" || role == "text") { |
| 130 | + return UIA_TextControlTypeId; |
| 131 | + } else if (role == "image") { |
| 132 | + return UIA_ImageControlTypeId; |
| 133 | + } else if (role == "keyboardkey") { |
| 134 | + return UIA_CustomControlTypeId; |
| 135 | + } else if (role == "link") { |
| 136 | + return UIA_HyperlinkControlTypeId; |
| 137 | + } |
| 138 | + // list and listitem were added by RNW to better support UIA Control Types |
| 139 | + else if (role == "list") { |
| 140 | + return UIA_ListControlTypeId; |
| 141 | + } else if (role == "listitem") { |
| 142 | + return UIA_ListItemControlTypeId; |
| 143 | + } else if (role == "menu") { |
| 144 | + return UIA_MenuControlTypeId; |
| 145 | + } else if (role == "menubar") { |
| 146 | + return UIA_MenuBarControlTypeId; |
| 147 | + } else if (role == "menuitem") { |
| 148 | + return UIA_MenuItemControlTypeId; |
| 149 | + } |
| 150 | + // If role is "none", remove the element from the control tree |
| 151 | + // and expose it as a plain element would in the raw tree. |
| 152 | + else if (role == "none") { |
| 153 | + return UIA_GroupControlTypeId; |
| 154 | + } else if (role == "progressbar") { |
| 155 | + return UIA_ProgressBarControlTypeId; |
| 156 | + } else if (role == "radio") { |
| 157 | + return UIA_RadioButtonControlTypeId; |
| 158 | + } else if (role == "scrollbar") { |
| 159 | + return UIA_ScrollBarControlTypeId; |
| 160 | + } else if (role == "spinbutton") { |
| 161 | + return UIA_SpinnerControlTypeId; |
| 162 | + } else if (role == "splitbutton") { |
| 163 | + return UIA_SplitButtonControlTypeId; |
| 164 | + } else if (role == "tab") { |
| 165 | + return UIA_TabItemControlTypeId; |
| 166 | + } else if (role == "tablist") { |
| 167 | + return UIA_TabControlTypeId; |
| 168 | + } else if (role == "toolbar") { |
| 169 | + return UIA_ToolBarControlTypeId; |
| 170 | + } else if (role == "tree") { |
| 171 | + return UIA_TreeControlTypeId; |
| 172 | + } else if (role == "treeitem") { |
| 173 | + return UIA_TreeItemControlTypeId; |
| 174 | + } |
| 175 | + assert(false); |
| 176 | + return UIA_GroupControlTypeId; |
| 177 | +} |
| 178 | + |
| 179 | +HRESULT __stdcall CompositionDynamicAutomationProvider::GetPropertyValue(PROPERTYID propertyId, VARIANT *pRetVal) { |
| 180 | + if (pRetVal == nullptr) |
| 181 | + return E_POINTER; |
| 182 | + |
| 183 | + auto strongView = m_view.view(); |
| 184 | + if (strongView == nullptr) |
| 185 | + return UIA_E_ELEMENTNOTAVAILABLE; |
| 186 | + |
| 187 | + auto props = std::static_pointer_cast<const facebook::react::ViewProps>(strongView->props()); |
| 188 | + if (props == nullptr) |
| 189 | + return UIA_E_ELEMENTNOTAVAILABLE; |
| 190 | + |
| 191 | + switch (propertyId) { |
| 192 | + case UIA_ControlTypePropertyId: { |
| 193 | + pRetVal->vt = VT_I4; |
| 194 | + auto role = props->accessibilityRole; |
| 195 | + pRetVal->lVal = GetControlType(role); |
| 196 | + break; |
| 197 | + } |
| 198 | + case UIA_AutomationIdPropertyId: { |
| 199 | + pRetVal->vt = VT_BSTR; |
| 200 | + auto testId = props->testId; |
| 201 | + CComBSTR temp(testId.c_str()); |
| 202 | + pRetVal->bstrVal = temp.Detach(); |
| 203 | + break; |
| 204 | + } |
| 205 | + case UIA_NamePropertyId: { |
| 206 | + pRetVal->vt = VT_BSTR; |
| 207 | + auto name = props->accessibilityLabel; |
| 208 | + CComBSTR temp(name.c_str()); |
| 209 | + pRetVal->bstrVal = temp.Detach(); |
| 210 | + break; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + return S_OK; |
| 215 | +} |
| 216 | + |
| 217 | +HRESULT __stdcall CompositionDynamicAutomationProvider::get_HostRawElementProvider( |
| 218 | + IRawElementProviderSimple **pRetVal) { |
| 219 | + if (pRetVal == nullptr) |
| 220 | + return E_POINTER; |
| 221 | + |
| 222 | + *pRetVal = nullptr; |
| 223 | + |
| 224 | + return S_OK; |
| 225 | +} |
| 226 | + |
| 227 | +} // namespace winrt::Microsoft::ReactNative::implementation |
0 commit comments