Skip to content

Commit

Permalink
chore: optimize tab closure and reservation for fullscreen and find-i…
Browse files Browse the repository at this point in the history
…n-page mode
  • Loading branch information
Ritchie1108 authored and Bush2021 committed Apr 11, 2024
1 parent 617c028 commit c519efe
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 57 deletions.
108 changes: 70 additions & 38 deletions src/tabbookmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,75 +99,85 @@ bool handleMouseWheel(WPARAM wParam, LPARAM lParam, PMOUSEHOOKSTRUCT pmouse) {
}

// 双击关闭标签页
bool handleDblClk(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
int handleDblClk(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
if (wParam != WM_LBUTTONDBLCLK || !config.is_double_click_close) {
return false;
return 0;
}

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

bool is_on_one_tab = IsOnOneTab(top_container_view, pmouse->pt);
bool is_only_one_tab = IsOnlyOneTab(top_container_view);

if (is_on_one_tab) {
if (is_only_one_tab) {
ExecuteCommand(IDC_NEW_TAB);
ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
ExecuteCommand(IDC_CLOSE_TAB);
ExecuteCommand(IDC_NEW_TAB, hwnd);
ExecuteCommand(IDC_WINDOW_CLOSE_OTHER_TABS, hwnd);
} else {
ExecuteCommand(IDC_CLOSE_TAB);
ExecuteCommand(IDC_CLOSE_TAB, hwnd);
}
return true;
return 1;
}

return false;
return 0;
}

// 右键关闭标签页
bool handleRightClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
int handleRightClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
if (wParam != WM_RBUTTONUP || IsPressed(VK_SHIFT) ||
!config.is_right_click_close) {
return false;
return 0;
}

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

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

if (is_on_one_tab) {
if (keep_tab) {
ExecuteCommand(IDC_NEW_TAB);
ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
ExecuteCommand(IDC_CLOSE_TAB);
ExecuteCommand(IDC_NEW_TAB, hwnd);
ExecuteCommand(IDC_WINDOW_CLOSE_OTHER_TABS, hwnd);
} else {
SendKeys(VK_MBUTTON);
}
return true;
return 1;
}
return false;
return 0;
}

// 保留最后标签页 - 中键
bool handleMiddleClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
int handleMiddleClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
if (wParam != WM_MBUTTONUP) {
return false;
return 0;
}

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

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

if (is_on_one_tab && keep_tab) {
ExecuteCommand(IDC_NEW_TAB);
return true;
ExecuteCommand(IDC_NEW_TAB, hwnd);
ExecuteCommand(IDC_WINDOW_CLOSE_OTHER_TABS, hwnd);
return 1;
}

return false;
return 0;
}

// 新标签页打开书签
Expand Down Expand Up @@ -266,14 +276,16 @@ LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
return 1;
}

if (handleDblClk(wParam, pmouse)) {}
if (handleDblClk(wParam, pmouse) != 0) {
return 1;
}

if (handleRightClick(wParam, pmouse)) {
if (handleRightClick(wParam, pmouse) != 0) {
return 1;
}

if (handleMiddleClick(wParam, pmouse)) {
// return 1;
if (handleMiddleClick(wParam, pmouse) != 0) {
return 1;
}

if (handleBookmark(wParam, pmouse)) {
Expand All @@ -289,6 +301,38 @@ LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
return CallNextHookEx(mouse_hook, nCode, wParam, lParam);
}

int handleKeepTab(WPARAM wParam) {

if (!(wParam == 'W' && IsPressed(VK_CONTROL) && !IsPressed(VK_SHIFT)) &&
!(wParam == VK_F4 && IsPressed(VK_CONTROL))) {
return 0;
}

HWND hwnd = GetFocus();
wchar_t name[256] = {0};
GetClassName(hwnd, name, 255);
if (wcsstr(name, L"Chrome_WidgetWin_") == nullptr) {
return 0;
}

if (IsFullScreen(hwnd)) {
// 必须退出全屏才能找到标签
ExecuteCommand(IDC_FULLSCREEN, hwnd);
}

HWND tmp_hwnd = hwnd;
hwnd = GetAncestor(tmp_hwnd, GA_ROOTOWNER);
ExecuteCommand(IDC_CLOSE_FIND_OR_STOP, tmp_hwnd);

if (!IsNeedKeep(hwnd)) {
return 0;
}

ExecuteCommand(IDC_NEW_TAB, hwnd);
ExecuteCommand(IDC_WINDOW_CLOSE_OTHER_TABS, hwnd);
return 1;
}

bool IsNeedOpenUrlInNewTab() {
bool open_url_ing = false;

Expand All @@ -308,19 +352,7 @@ HHOOK keyboard_hook = nullptr;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && !(lParam & 0x80000000)) // pressed
{
bool keep_tab = false;

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

if (keep_tab) {
ExecuteCommand(IDC_NEW_TAB);
ExecuteCommand(IDC_SELECT_PREVIOUS_TAB);
ExecuteCommand(IDC_CLOSE_TAB);
if (handleKeepTab(wParam) != 0) {
return 1;
}

Expand Down
48 changes: 29 additions & 19 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
#define IDC_SELECT_LAST_TAB 34026
#define IDC_SHOW_TRANSLATE 35009
#define IDC_UPGRADE_DIALOG 40024

#define IDC_FULLSCREEN 34030
#define IDC_CLOSE_FIND_OR_STOP 37003
#define IDC_WINDOW_CLOSE_OTHER_TABS 35023

// 字符串操作函数
std::wstring Format(const wchar_t* format, va_list args) {
Expand Down Expand Up @@ -270,24 +272,24 @@ std::wstring ExpandEnvironmentPath(const std::wstring& path) {

// 日志函数
void DebugLog(const wchar_t* format, ...) {
// va_list args;
//
// va_start(args, format);
// auto str = Format(format, args);
// va_end(args);
//
// str = Format(L"[chrome++] %s\n", str.c_str());
//
// std::string nstr = wstring_to_string(str);
// const char* cstr = nstr.c_str();
//
// FILE* fp = nullptr;
// std::wstring logPath = GetAppDir() + L"\\Chrome++_Debug.log";
// _wfopen_s(&fp, logPath.c_str(), L"a+");
// if (fp) {
// fwrite(cstr, strlen(cstr), 1, fp);
// fclose(fp);
// }
// va_list args;
//
// va_start(args, format);
// auto str = Format(format, args);
// va_end(args);
//
// str = Format(L"[chrome++] %s\n", str.c_str());
//
// std::string nstr = wstring_to_string(str);
// const char* cstr = nstr.c_str();
//
// FILE* fp = nullptr;
// std::wstring logPath = GetAppDir() + L"\\Chrome++_Debug.log";
// _wfopen_s(&fp, logPath.c_str(), L"a+");
// if (fp) {
// fwrite(cstr, strlen(cstr), 1, fp);
// fclose(fp);
// }
}


Expand All @@ -308,6 +310,14 @@ void ExecuteCommand(int id, HWND hwnd = 0) {
::SendMessageTimeoutW(hwnd, WM_SYSCOMMAND, id, 0, 0, 1000, 0);
}

bool IsFullScreen(HWND hwnd) {
RECT windowRect;
return (GetClientRect(hwnd, &windowRect)
&& (windowRect.left == 0 && windowRect.top == 0
&& windowRect.right == GetSystemMetrics(SM_CXSCREEN)
&& windowRect.bottom == GetSystemMetrics(SM_CYSCREEN)));
}


// 键盘和鼠标输入函数
// 发送组合按键操作
Expand Down

0 comments on commit c519efe

Please sign in to comment.