forked from Bush2021/chrome_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabbookmark.h
342 lines (287 loc) · 9.49 KB
/
tabbookmark.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#ifndef TABBOOKMARK_H_
#define TABBOOKMARK_H_
#include "iaccessible.h"
HHOOK mouse_hook = nullptr;
#define KEY_PRESSED 0x8000
bool IsPressed(int key) {
return key && (::GetKeyState(key) & KEY_PRESSED) != 0;
}
// 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(NodePtr top_container_view) {
if (!IsKeepLastTab()) {
return false;
}
auto tab_count = GetTabCount(top_container_view);
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 > 50 && tick <= 250 && tab_count == 2) {
keep_tab = true;
}
return keep_tab;
}
// If the top_container_view is not found at the first time, try to close the
// find-in-page bar and find the top_container_view again.
NodePtr HandleFindBar(HWND hwnd, POINT pt) {
// If the mouse is clicked directly on the find-in-page bar, follow Chrome's
// original logic. Otherwise, clicking the button on the find-in-page bar may
// directly close the find-in-page bar.
if (IsOnDialog(hwnd, pt)) {
return nullptr;
}
NodePtr top_container_view = GetTopContainerView(hwnd);
if (!top_container_view) {
ExecuteCommand(IDC_CLOSE_FIND_OR_STOP, hwnd);
top_container_view = GetTopContainerView(hwnd);
if (!top_container_view) {
return nullptr;
}
}
return top_container_view;
}
class IniConfig {
public:
IniConfig()
: is_double_click_close(IsDoubleClickClose()),
is_right_click_close(IsRightClickClose()),
is_wheel_tab(IsWheelTab()),
is_wheel_tab_when_press_right_button(IsWheelTabWhenPressRightButton()),
is_bookmark_new_tab(IsBookmarkNewTab()),
is_open_url_new_tab(IsOpenUrlNewTabFun()) {}
bool is_double_click_close;
bool is_right_click_close;
bool is_wheel_tab;
bool is_wheel_tab_when_press_right_button;
std::string is_bookmark_new_tab;
std::string is_open_url_new_tab;
};
IniConfig config;
// Use the mouse wheel to switch tabs
bool HandleMouseWheel(WPARAM wParam, LPARAM lParam, PMOUSEHOOKSTRUCT pmouse) {
if (wParam != WM_MOUSEWHEEL ||
(!config.is_wheel_tab && !config.is_wheel_tab_when_press_right_button)) {
return false;
}
HWND hwnd = GetFocus();
NodePtr top_container_view = GetTopContainerView(hwnd);
PMOUSEHOOKSTRUCTEX pwheel = (PMOUSEHOOKSTRUCTEX)lParam;
int zDelta = GET_WHEEL_DELTA_WPARAM(pwheel->mouseData);
// If the mouse wheel is used to switch tabs when the mouse is on the tab bar.
if (config.is_wheel_tab && IsOnTheTabBar(top_container_view, pmouse->pt)) {
hwnd = GetTopWnd(hwnd);
if (zDelta > 0) {
ExecuteCommand(IDC_SELECT_PREVIOUS_TAB, hwnd);
} else {
ExecuteCommand(IDC_SELECT_NEXT_TAB, hwnd);
}
return true;
}
// If it is used to switch tabs when the right button is held.
if (config.is_wheel_tab_when_press_right_button && IsPressed(VK_RBUTTON)) {
hwnd = GetTopWnd(hwnd);
if (zDelta > 0) {
ExecuteCommand(IDC_SELECT_PREVIOUS_TAB, hwnd);
} else {
ExecuteCommand(IDC_SELECT_NEXT_TAB, hwnd);
}
return true;
}
return false;
}
// Double-click to close tab.
int HandleDoubleClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
if (wParam != WM_LBUTTONDBLCLK || !config.is_double_click_close) {
return 0;
}
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, 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;
}
if (is_only_one_tab) {
ExecuteCommand(IDC_NEW_TAB, hwnd);
ExecuteCommand(IDC_WINDOW_CLOSE_OTHER_TABS, hwnd);
} else {
ExecuteCommand(IDC_CLOSE_TAB, hwnd);
}
return 1;
}
// Right-click to close tab (Hold Shift to show the original menu).
int HandleRightClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
if (wParam != WM_RBUTTONUP || IsPressed(VK_SHIFT) ||
!config.is_right_click_close) {
return 0;
}
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, pt);
bool keep_tab = IsNeedKeep(top_container_view);
if (is_on_one_tab) {
if (keep_tab) {
ExecuteCommand(IDC_NEW_TAB, hwnd);
ExecuteCommand(IDC_WINDOW_CLOSE_OTHER_TABS, hwnd);
} else {
// Attempt new SendKey function which includes a `dwExtraInfo`
// value (MAGIC_CODE).
SendKey(VK_MBUTTON);
}
return 1;
}
return 0;
}
// Preserve the last tab when the middle button is clicked on the tab.
int HandleMiddleClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
if (wParam != WM_MBUTTONUP) {
return 0;
}
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, pt);
bool keep_tab = IsNeedKeep(top_container_view);
if (is_on_one_tab && keep_tab) {
ExecuteCommand(IDC_NEW_TAB, hwnd);
ExecuteCommand(IDC_WINDOW_CLOSE_OTHER_TABS, hwnd);
return 1;
}
return 0;
}
// Open bookmarks in a new tab.
bool HandleBookmark(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) {
if (wParam != WM_LBUTTONUP || IsPressed(VK_CONTROL) || IsPressed(VK_SHIFT) ||
config.is_bookmark_new_tab == "disabled") {
return false;
}
POINT pt = pmouse->pt;
HWND hwnd = WindowFromPoint(pt);
NodePtr top_container_view = GetTopContainerView(
GetFocus()); // Must use `GetFocus()`, otherwise when opening bookmarks
// in a bookmark folder (and similar expanded menus),
// `top_container_view` cannot be obtained, making it
// impossible to correctly determine `is_on_new_tab`. See
// #98.
bool is_on_bookmark = IsOnBookmark(hwnd, pt);
bool is_on_new_tab = IsOnNewTab(top_container_view);
if (is_on_bookmark && !is_on_new_tab) {
if (config.is_bookmark_new_tab == "foreground") {
SendKey(VK_MBUTTON, VK_SHIFT);
} else if (config.is_bookmark_new_tab == "background") {
SendKey(VK_MBUTTON);
}
return true;
}
return false;
}
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode != HC_ACTION) {
return CallNextHookEx(mouse_hook, nCode, wParam, lParam);
}
do {
if (wParam == WM_MOUSEMOVE || wParam == WM_NCMOUSEMOVE) {
break;
}
PMOUSEHOOKSTRUCT pmouse = (PMOUSEHOOKSTRUCT)lParam;
// Defining a `dwExtraInfo` value to prevent hook the message sent by
// Chrome++ itself.
if (pmouse->dwExtraInfo == MAGIC_CODE) {
break;
}
if (HandleMouseWheel(wParam, lParam, pmouse)) {
return 1;
}
if (HandleDoubleClick(wParam, pmouse) != 0) {
// Do not return 1. Returning 1 could cause the keep_tab to fail
// or trigger double-click operations consecutively when the user
// double-clicks on the tab page rapidly and repeatedly.
}
if (HandleRightClick(wParam, pmouse) != 0) {
return 1;
}
if (HandleMiddleClick(wParam, pmouse) != 0) {
return 1;
}
if (HandleBookmark(wParam, pmouse)) {
return 1;
}
} while (0);
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)) {
// Have to exit full screen to find the tab.
ExecuteCommand(IDC_FULLSCREEN, hwnd);
}
HWND tmp_hwnd = hwnd;
hwnd = GetAncestor(tmp_hwnd, GA_ROOTOWNER);
ExecuteCommand(IDC_CLOSE_FIND_OR_STOP, tmp_hwnd);
NodePtr top_container_view = GetTopContainerView(hwnd);
if (!IsNeedKeep(top_container_view)) {
return 0;
}
ExecuteCommand(IDC_NEW_TAB, hwnd);
ExecuteCommand(IDC_WINDOW_CLOSE_OTHER_TABS, hwnd);
return 1;
}
int HandleOpenUrlNewTab(WPARAM wParam) {
if (!(config.is_open_url_new_tab != "disabled" && wParam == VK_RETURN &&
!IsPressed(VK_MENU))) {
return 0;
}
NodePtr top_container_view = GetTopContainerView(GetForegroundWindow());
if (IsOmniboxFocus(top_container_view) && !IsOnNewTab(top_container_view)) {
if (config.is_open_url_new_tab == "foreground") {
SendKey(VK_MENU, VK_RETURN);
} else if (config.is_open_url_new_tab == "background") {
SendKey(VK_SHIFT, VK_MENU, VK_RETURN);
}
return 1;
}
return 0;
}
HHOOK keyboard_hook = nullptr;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && !(lParam & 0x80000000)) // pressed
{
if (HandleKeepTab(wParam) != 0) {
return 1;
}
if (HandleOpenUrlNewTab(wParam) != 0) {
return 1;
}
}
return CallNextHookEx(keyboard_hook, nCode, wParam, lParam);
}
void TabBookmark() {
mouse_hook =
SetWindowsHookEx(WH_MOUSE, MouseProc, hInstance, GetCurrentThreadId());
keyboard_hook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInstance,
GetCurrentThreadId());
}
#endif // TABBOOKMARK_H_