Skip to content

Commit 4f4498d

Browse files
committed
IsAnyClassName
1 parent fb9461d commit 4f4498d

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

src/uia.cc

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <uiautomation.h>
44
#include <wrl/client.h>
55

6+
#include <initializer_list>
67
#include <optional>
78
#include <string_view>
89

@@ -170,20 +171,24 @@ ComPtr<IUIAutomationCondition> CreateClassCondition(
170171
}
171172

172173
bool IsClassName(const ComPtr<IUIAutomationElement>& element,
173-
std::wstring_view class_name_target) {
174-
if (!element) {
174+
std::wstring_view expected_class_name) {
175+
const auto actual_class_name =
176+
GetCurrentStringProperty(element, UIA_ClassNamePropertyId);
177+
if (!actual_class_name.has_value()) {
175178
return false;
176179
}
180+
return actual_class_name.value() == expected_class_name;
181+
}
177182

178-
BSTR class_name = nullptr;
179-
if (FAILED(element->get_CurrentClassName(&class_name)) || !class_name) {
183+
bool IsAnyClassName(
184+
const ComPtr<IUIAutomationElement>& element,
185+
std::initializer_list<std::wstring_view> expected_class_names) {
186+
const auto actual_class_name =
187+
GetCurrentStringProperty(element, UIA_ClassNamePropertyId);
188+
if (!actual_class_name.has_value()) {
180189
return false;
181190
}
182-
183-
const std::wstring_view class_name_view(class_name);
184-
const bool is_match = (class_name_view == class_name_target);
185-
SysFreeString(class_name);
186-
return is_match;
191+
return std::ranges::contains(expected_class_names, actual_class_name.value());
187192
}
188193

189194
ComPtr<IUIAutomationElement> FindFirstInSubtree(
@@ -365,11 +370,10 @@ bool IsOnTabBar(POINT pt) {
365370
}
366371

367372
// Fast path to avoid `GetParentElement` calls for common tab bar elements.
368-
if (IsClassName(pointed, L"HorizontalTabStripRegionView") ||
369-
IsClassName(pointed, L"TabStrip::TabDragContextImpl") ||
370-
IsClassName(pointed, L"TabStripControlButton") ||
371-
IsClassName(pointed, L"FrameGrabHandle") ||
372-
IsClassName(pointed, L"ScrollView")) {
373+
if (IsAnyClassName(
374+
pointed,
375+
{L"HorizontalTabStripRegionView", L"TabStrip::TabDragContextImpl",
376+
L"TabStripControlButton", L"FrameGrabHandle", L"ScrollView"})) {
373377
return true;
374378
}
375379
return FindParentByClass(automation, pointed,
@@ -401,9 +405,7 @@ bool IsOnBookmark(POINT pt) {
401405
if (is_blocked_scheme || !looks_like_url) {
402406
return false;
403407
}
404-
405-
return IsClassName(pointed, L"BookmarkButton") ||
406-
IsClassName(pointed, L"MenuItemView");
408+
return IsAnyClassName(pointed, {L"BookmarkButton", L"MenuItemView"});
407409
}
408410

409411
bool IsOmniboxFocused() {
@@ -416,6 +418,5 @@ bool IsOmniboxFocused() {
416418
if (!focused) {
417419
return false;
418420
}
419-
return IsClassName(focused, L"OmniboxViewViews") ||
420-
IsClassName(focused, L"OmniboxResultView");
421+
return IsAnyClassName(focused, {L"OmniboxViewViews", L"OmniboxResultView"});
421422
}

0 commit comments

Comments
 (0)