Skip to content

Commit

Permalink
chore: extract common logic code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bush2021 committed Aug 2, 2024
1 parent 085aca9 commit 917361a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 54 deletions.
71 changes: 29 additions & 42 deletions src/iaccessible.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,30 @@ NodePtr GetParentElement(NodePtr child) {
return element;
}

NodePtr GetTopContainerView(HWND hwnd) {
NodePtr top_container_view = nullptr;
NodePtr GetChromeWidgetWin(HWND hwnd) {
NodePtr pacc_main_window = nullptr;
wchar_t name[MAX_PATH];
if (GetClassName(hwnd, name, MAX_PATH) &&
wcsstr(name, L"Chrome_WidgetWin_") == name) {
NodePtr pacc_main_window = nullptr;
if (S_OK == AccessibleObjectFromWindow(hwnd, OBJID_WINDOW,
IID_PPV_ARGS(&pacc_main_window))) {
NodePtr page_tab_list = FindPageTabList(pacc_main_window);
if (page_tab_list) {
top_container_view = GetParentElement(page_tab_list);
}
if (!top_container_view) {
DebugLog(L"GetTopContainerView failed");
}
return pacc_main_window;
}
}
DebugLog(L"GetChromeWidgetWin failed");
return nullptr;
}

NodePtr GetTopContainerView(HWND hwnd) {
NodePtr top_container_view = nullptr;
NodePtr page_tab_list = FindPageTabList(GetChromeWidgetWin(hwnd));
if (page_tab_list) {
top_container_view = GetParentElement(page_tab_list);
}
if (!top_container_view) {
DebugLog(L"GetTopContainerView failed");
}
return top_container_view;
}

Expand Down Expand Up @@ -416,17 +423,6 @@ bool IsOnNewTab(NodePtr top) {

// Whether the mouse is on a bookmark.
bool IsOnBookmark(HWND hwnd, POINT pt) {
wchar_t name[MAX_PATH];
if (!GetClassName(hwnd, name, MAX_PATH) ||
wcsstr(name, L"Chrome_WidgetWin_") != name) {
return false;
}
NodePtr pacc_main_window = nullptr;
if (S_OK != AccessibleObjectFromWindow(hwnd, OBJID_WINDOW,
IID_PPV_ARGS(&pacc_main_window))) {
return false;
}

bool flag = false;
std::function<bool(NodePtr)> LambdaEnumChild =
[&pt, &flag, &LambdaEnumChild](NodePtr child) -> bool {
Expand Down Expand Up @@ -454,7 +450,7 @@ bool IsOnBookmark(HWND hwnd, POINT pt) {
return flag;
};
// Start traversing.
TraversalAccessible(pacc_main_window, LambdaEnumChild);
TraversalAccessible(GetChromeWidgetWin(hwnd), LambdaEnumChild);
return flag;
}

Expand Down Expand Up @@ -489,28 +485,19 @@ bool IsOmniboxFocus(NodePtr top) {
// Whether the mouse is on the dialog box.
bool IsOnDialog(HWND hwnd, POINT pt) {
bool flag = false;
NodePtr pacc_main_window = nullptr;
wchar_t name[MAX_PATH];
if (GetClassName(hwnd, name, MAX_PATH) &&
wcsstr(name, L"Chrome_WidgetWin_") == name) {
if (S_OK == AccessibleObjectFromWindow(hwnd, OBJID_CLIENT,
IID_PPV_ARGS(&pacc_main_window))) {
TraversalAccessible(
pacc_main_window,
[&pt, &flag](NodePtr child) {
if (GetAccessibleRole(child) == ROLE_SYSTEM_DIALOG) {
GetAccessibleSize(child, [&pt, &flag](RECT rect) {
if (PtInRect(&rect, pt)) {
flag = true;
}
});
TraversalAccessible(
GetChromeWidgetWin(hwnd),
[&pt, &flag](NodePtr child) {
if (GetAccessibleRole(child) == ROLE_SYSTEM_DIALOG) {
GetAccessibleSize(child, [&pt, &flag](RECT rect) {
if (PtInRect(&rect, pt)) {
flag = true;
}
return flag;
},
true); // raw_traversal
return flag;
}
}
});
}
return flag;
},
true); // raw_traversal
return flag;
}

Expand Down
28 changes: 16 additions & 12 deletions src/tabbookmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ int HandleDoubleClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
return 0;
}

HWND hwnd = WindowFromPoint(pmouse->pt);
NodePtr top_container_view = HandleFindBar(hwnd, pmouse->pt);
POINT pt = pmouse->pt;
HWND hwnd = WindowFromPoint(pt);
NodePtr top_container_view = HandleFindBar(hwnd, pt);
if (!top_container_view) {
return 0;
}

bool is_on_one_tab = IsOnOneTab(top_container_view, pmouse->pt);
bool is_on_close_button = IsOnCloseButton(top_container_view, pmouse->pt);
bool is_on_one_tab = IsOnOneTab(top_container_view, pt);
bool is_on_close_button = IsOnCloseButton(top_container_view, pt);
bool is_only_one_tab = IsOnlyOneTab(top_container_view);
if (!is_on_one_tab || is_on_close_button) {
return 0;
Expand All @@ -144,13 +145,14 @@ int HandleRightClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
return 0;
}

HWND hwnd = WindowFromPoint(pmouse->pt);
NodePtr top_container_view = HandleFindBar(hwnd, pmouse->pt);
POINT pt = pmouse->pt;
HWND hwnd = WindowFromPoint(pt);
NodePtr top_container_view = HandleFindBar(hwnd, pt);
if (!top_container_view) {
return 0;
}

bool is_on_one_tab = IsOnOneTab(top_container_view, pmouse->pt);
bool is_on_one_tab = IsOnOneTab(top_container_view, pt);
bool keep_tab = IsNeedKeep(top_container_view);

if (is_on_one_tab) {
Expand All @@ -173,13 +175,14 @@ int HandleMiddleClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
return 0;
}

HWND hwnd = WindowFromPoint(pmouse->pt);
NodePtr top_container_view = HandleFindBar(hwnd, pmouse->pt);
POINT pt = pmouse->pt;
HWND hwnd = WindowFromPoint(pt);
NodePtr top_container_view = HandleFindBar(hwnd, pt);
if (!top_container_view) {
return 0;
}

bool is_on_one_tab = IsOnOneTab(top_container_view, pmouse->pt);
bool is_on_one_tab = IsOnOneTab(top_container_view, pt);
bool keep_tab = IsNeedKeep(top_container_view);

if (is_on_one_tab && keep_tab) {
Expand All @@ -198,10 +201,11 @@ bool HandleBookmark(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
return false;
}

HWND hwnd = WindowFromPoint(pmouse->pt);
POINT pt = pmouse->pt;
HWND hwnd = WindowFromPoint(pt);
NodePtr top_container_view = GetTopContainerView(hwnd);

bool is_on_bookmark = IsOnBookmark(hwnd, pmouse->pt);
bool is_on_bookmark = IsOnBookmark(hwnd, pt);
bool is_on_new_tab = IsOnNewTab(top_container_view);

if (is_on_bookmark && !is_on_new_tab) {
Expand Down

0 comments on commit 917361a

Please sign in to comment.