Skip to content

Commit

Permalink
chore: optimize the logic for IsNeedKeep (#72)
Browse files Browse the repository at this point in the history
* Improve tick judgment conditions (#69)

* Change the parameter passed into the function from `hwnd` to `top_container_view` to avoid repeated traversal
  • Loading branch information
YorkWaugh authored May 7, 2024
1 parent 80561dc commit 982095b
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/tabbookmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,20 @@ bool IsPressed(int key) {
// Compared with `IsOnlyOneTab`, this function additionally implements tick
// fault tolerance to prevent users from directly closing the window when
// they click too fast.
bool IsNeedKeep(HWND hwnd, int32_t* ptr = nullptr) {
bool IsNeedKeep(NodePtr top_container_view) {
if (!IsKeepLastTab()) {
return false;
}

bool keep_tab = false;

NodePtr top_container_view = GetTopContainerView(hwnd);
auto tab_count = GetTabCount(top_container_view);
bool is_only_one_tab = (tab_count > 0 && tab_count <= 1);
bool keep_tab = (tab_count == 1);

static auto last_closing_tab_tick = GetTickCount64();
auto tick = GetTickCount64() - last_closing_tab_tick;
last_closing_tab_tick = GetTickCount64();

if (tick > 0 && tick <= 250 && tab_count <= 2) {
is_only_one_tab = true;
}
if (tab_count == 0) { // Processing full screen and other states.
is_only_one_tab = false;
}
keep_tab = is_only_one_tab;

if (ptr) {
*ptr = tick;
if (tick > 50 && tick <= 250 && tab_count == 2) {
keep_tab = true;
}

return keep_tab;
Expand Down Expand Up @@ -156,7 +145,7 @@ int HandleRightClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
}

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

if (is_on_one_tab) {
if (keep_tab) {
Expand All @@ -183,7 +172,7 @@ int HandleMiddleClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
}

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

if (is_on_one_tab && keep_tab) {
ExecuteCommand(IDC_NEW_TAB, hwnd);
Expand Down Expand Up @@ -340,7 +329,8 @@ int HandleKeepTab(WPARAM wParam) {
hwnd = GetAncestor(tmp_hwnd, GA_ROOTOWNER);
ExecuteCommand(IDC_CLOSE_FIND_OR_STOP, tmp_hwnd);

if (!IsNeedKeep(hwnd)) {
NodePtr top_container_view = GetTopContainerView(hwnd);
if (!IsNeedKeep(top_container_view)) {
return 0;
}

Expand Down

0 comments on commit 982095b

Please sign in to comment.