Skip to content

Commit

Permalink
chore: implement tick fault tolerance to prevent rapid clicking from …
Browse files Browse the repository at this point in the history
…closing browser

Co-authored-by: Bush2021 <79072750+bush2021@users.noreply.github.com>
  • Loading branch information
Ritchie1108 and Bush2021 committed Apr 8, 2024
1 parent 04ad2d5 commit bb79e8a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 59 deletions.
70 changes: 37 additions & 33 deletions src/IAccessibleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,41 @@ NodePtr GetMenuBarPane(HWND hwnd) {
return MenuBarPane;
}

// 获取当前标签页数量
__int64 GetTabCount(NodePtr top) {
NodePtr PageTabList = FindElementWithRole(top, ROLE_SYSTEM_PAGETABLIST);
if (!PageTabList) {
return 0;
}

NodePtr PageTab = FindElementWithRole(PageTabList, ROLE_SYSTEM_PAGETAB);
if (!PageTab) {
return 0;
}

NodePtr PageTabPane = GetParentElement(PageTab);
if (!PageTabPane) {
return 0;
}

std::vector<NodePtr> children;
TraversalAccessible(PageTabPane, [&children](NodePtr child) {
children.push_back(child);
return false;
});

auto nTabCount =
std::count_if(children.begin(), children.end(), [](NodePtr child) {
auto role = GetAccessibleRole(child);
auto state = GetAccessibleState(child);
return role == ROLE_SYSTEM_PAGETAB ||
(role == ROLE_SYSTEM_PAGETABLIST &&
(state & STATE_SYSTEM_COLLAPSED));
});

return nTabCount;
}

NodePtr FindChildElement(NodePtr parent, long role, int skipcount = 0) {
NodePtr element = nullptr;
if (parent) {
Expand Down Expand Up @@ -292,43 +327,12 @@ bool IsOnOneTab(NodePtr top, POINT pt) {
return flag;
}

// 是否只有一个标签
bool IsOnlyOneTab(NodePtr top) {
if (!IsKeepLastTabFun()) {
return false;
}

NodePtr PageTabList = FindElementWithRole(top, ROLE_SYSTEM_PAGETABLIST);
if (!PageTabList) {
return false;
}

NodePtr PageTab = FindElementWithRole(PageTabList, ROLE_SYSTEM_PAGETAB);
if (!PageTab) {
return false;
}

NodePtr PageTabPane = GetParentElement(PageTab);
if (!PageTabPane) {
return false;
}

std::vector<NodePtr> children;
TraversalAccessible(PageTabPane, [&children](NodePtr child) {
children.push_back(child);
return false;
});

auto tab_count =
std::count_if(children.begin(), children.end(), [](NodePtr child) {
auto role = GetAccessibleRole(child);
auto state = GetAccessibleState(child);
return role == ROLE_SYSTEM_PAGETAB ||
(role == ROLE_SYSTEM_PAGETABLIST &&
(state & STATE_SYSTEM_COLLAPSED));
});

return tab_count <= 1;
auto nTabCount = GetTabCount(top);
return nTabCount <= 1;
}

// 鼠标是否在标签栏上
Expand Down
67 changes: 41 additions & 26 deletions src/TabBookmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,34 @@

#include "IAccessibleUtils.h"

HHOOK mouse_hook = NULL;
HHOOK mouse_hook = nullptr;

#define KEY_PRESSED 0x8000
bool IsPressed(int key) {
return key && (::GetKeyState(key) & KEY_PRESSED) != 0;
}

bool IsNeedKeep(HWND hwnd, int32_t* ptr = nullptr) {
bool bKeepTab = false;

NodePtr pTopContainerView = GetTopContainerView(hwnd);
auto nTabCount = GetTabCount(pTopContainerView);
bool bIsOnlyOneTab = nTabCount <= 1;

static auto LAST_CLOSING_TAB_TICK = GetTickCount64();
auto tick = GetTickCount64() - LAST_CLOSING_TAB_TICK;
LAST_CLOSING_TAB_TICK = GetTickCount64();
if (tick > 0 && tick <= 200 && nTabCount <= 2){
bIsOnlyOneTab = true;
}
bKeepTab = bIsOnlyOneTab;
if (ptr){
*ptr = tick;
}

return bKeepTab;
}

class IniConfig {
public:
IniConfig()
Expand Down Expand Up @@ -127,16 +148,21 @@ bool handleMiddleClick(WPARAM wParam, LPARAM lParam, PMOUSEHOOKSTRUCT pmouse) {
}

HWND hwnd = WindowFromPoint(pmouse->pt);
NodePtr TopContainerView = GetTopContainerView(hwnd);
NodePtr pTopContainerView = GetTopContainerView(hwnd);

bool isOnOneTab = IsOnOneTab(TopContainerView, pmouse->pt);
bool isOnlyOneTab = IsOnlyOneTab(TopContainerView);
bool bIsOnOneTab = IsOnOneTab(pTopContainerView, pmouse->pt);
bool bKeepTab = IsNeedKeep(hwnd);

if (isOnOneTab && isOnlyOneTab) {
ExecuteCommand(IDC_NEW_TAB);
if (bIsOnOneTab) {
if (bKeepTab) {
ExecuteCommand(IDC_NEW_TAB);
ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
ExecuteCommand(IDC_CLOSE_TAB);
} else {
ExecuteCommand(IDC_CLOSE_TAB);
}
return true;
}

return false;
}

Expand Down Expand Up @@ -241,7 +267,9 @@ LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
return 1;
}

if (handleMiddleClick(wParam, lParam, pmouse)) {}
if (handleMiddleClick(wParam, lParam, pmouse)) {
return 1;
}

if (handleBookmark(wParam, lParam, pmouse)) {
return 1;
Expand All @@ -256,19 +284,6 @@ LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
return CallNextHookEx(mouse_hook, nCode, wParam, lParam);
}

bool IsNeedKeep() {
bool keep_tab = false;

NodePtr TopContainerView = GetTopContainerView(GetForegroundWindow());
if (IsOnlyOneTab(TopContainerView)) {
keep_tab = true;
}

if (TopContainerView) {}

return keep_tab;
}

bool IsNeedOpenUrlInNewTab() {
bool open_url_ing = false;

Expand All @@ -284,20 +299,20 @@ bool IsNeedOpenUrlInNewTab() {
return open_url_ing;
}

HHOOK keyboard_hook = NULL;
HHOOK keyboard_hook = nullptr;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && !(lParam & 0x80000000)) // pressed
{
bool keep_tab = false;
bool bKeepTab = false;

if (wParam == 'W' && IsPressed(VK_CONTROL) && !IsPressed(VK_SHIFT)) {
keep_tab = IsNeedKeep();
bKeepTab = IsNeedKeep(GetForegroundWindow());
}
if (wParam == VK_F4 && IsPressed(VK_CONTROL)) {
keep_tab = IsNeedKeep();
bKeepTab = IsNeedKeep(GetForegroundWindow());
}

if (keep_tab) {
if (bKeepTab) {
ExecuteCommand(IDC_NEW_TAB);
ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
ExecuteCommand(IDC_CLOSE_TAB);
Expand Down

0 comments on commit bb79e8a

Please sign in to comment.