Skip to content

Commit ab5a2f1

Browse files
committed
use GetFirstChildElement when need_count = false
1 parent e426e69 commit ab5a2f1

1 file changed

Lines changed: 51 additions & 16 deletions

File tree

src/uia.cc

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ std::optional<TabContainer> FindTabContainer(
304304

305305
} // namespace
306306

307+
// TODO: make it shorter
307308
std::optional<TabHitResult> FindTabHitResult(POINT pt,
308309
bool need_count,
309310
bool need_close_button) {
@@ -317,6 +318,55 @@ std::optional<TabHitResult> FindTabHitResult(POINT pt,
317318
return std::nullopt;
318319
}
319320

321+
auto build_result = [&](const ComPtr<IUIAutomationElement>& tab_element,
322+
int tab_count) {
323+
TabHitResult result;
324+
result.tab = tab_element;
325+
result.tab_count = tab_count;
326+
327+
if (need_close_button) {
328+
const auto close_btn = FindFirstInSubtree(
329+
tab_element, CreateClassCondition(automation, L"TabCloseButton"));
330+
if (close_btn) {
331+
RECT close_rect;
332+
if (SUCCEEDED(close_btn->get_CurrentBoundingRectangle(&close_rect))) {
333+
result.on_close_button = PtInRect(&close_rect, pt) != FALSE;
334+
}
335+
}
336+
}
337+
return result;
338+
};
339+
340+
if (!need_count) {
341+
ComPtr<IUIAutomationTreeWalker> walker;
342+
if (FAILED(automation->get_ControlViewWalker(&walker)) || !walker) {
343+
return std::nullopt;
344+
}
345+
346+
ComPtr<IUIAutomationElement> child;
347+
if (FAILED(walker->GetFirstChildElement(tab_container->container.Get(),
348+
child.ReleaseAndGetAddressOf()))) {
349+
return std::nullopt;
350+
}
351+
while (child) {
352+
if (IsClassName(child, tab_container->tab_class)) {
353+
RECT rect;
354+
if (SUCCEEDED(child->get_CurrentBoundingRectangle(&rect)) &&
355+
PtInRect(&rect, pt)) {
356+
return build_result(child, 0);
357+
}
358+
}
359+
360+
ComPtr<IUIAutomationElement> next;
361+
if (FAILED(walker->GetNextSiblingElement(
362+
child.Get(), next.ReleaseAndGetAddressOf()))) {
363+
break;
364+
}
365+
child = std::move(next);
366+
}
367+
return std::nullopt;
368+
}
369+
320370
const auto condition =
321371
CreateClassCondition(automation, tab_container->tab_class);
322372
ComPtr<IUIAutomationElementArray> arr;
@@ -344,22 +394,7 @@ std::optional<TabHitResult> FindTabHitResult(POINT pt,
344394
continue;
345395
}
346396

347-
TabHitResult result;
348-
result.tab = tab_element;
349-
result.tab_count = need_count ? count : 0;
350-
351-
if (need_close_button) {
352-
const auto close_btn = FindFirstInSubtree(
353-
tab_element, CreateClassCondition(automation, L"TabCloseButton"));
354-
if (close_btn) {
355-
RECT close_rect;
356-
if (SUCCEEDED(close_btn->get_CurrentBoundingRectangle(&close_rect))) {
357-
result.on_close_button = PtInRect(&close_rect, pt) != FALSE;
358-
}
359-
}
360-
}
361-
362-
return result;
397+
return build_result(tab_element, count);
363398
}
364399

365400
return std::nullopt;

0 commit comments

Comments
 (0)