forked from electron/electron
/
native_window_views_win.cc
393 lines (357 loc) · 12.9 KB
/
native_window_views_win.cc
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/browser.h"
#include "atom/browser/native_window_views.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "ui/base/win/accessibility_misc_utils.h"
// Must be included after other Windows headers.
#include <UIAutomationCoreApi.h>
namespace atom {
namespace {
// Convert Win32 WM_APPCOMMANDS to strings.
const char* AppCommandToString(int command_id) {
switch (command_id) {
case APPCOMMAND_BROWSER_BACKWARD:
return "browser-backward";
case APPCOMMAND_BROWSER_FORWARD:
return "browser-forward";
case APPCOMMAND_BROWSER_REFRESH:
return "browser-refresh";
case APPCOMMAND_BROWSER_STOP:
return "browser-stop";
case APPCOMMAND_BROWSER_SEARCH:
return "browser-search";
case APPCOMMAND_BROWSER_FAVORITES:
return "browser-favorites";
case APPCOMMAND_BROWSER_HOME:
return "browser-home";
case APPCOMMAND_VOLUME_MUTE:
return "volume-mute";
case APPCOMMAND_VOLUME_DOWN:
return "volume-down";
case APPCOMMAND_VOLUME_UP:
return "volume-up";
case APPCOMMAND_MEDIA_NEXTTRACK:
return "media-nexttrack";
case APPCOMMAND_MEDIA_PREVIOUSTRACK:
return "media-previoustrack";
case APPCOMMAND_MEDIA_STOP:
return "media-stop";
case APPCOMMAND_MEDIA_PLAY_PAUSE:
return "media-play-pause";
case APPCOMMAND_LAUNCH_MAIL:
return "launch-mail";
case APPCOMMAND_LAUNCH_MEDIA_SELECT:
return "launch-media-select";
case APPCOMMAND_LAUNCH_APP1:
return "launch-app1";
case APPCOMMAND_LAUNCH_APP2:
return "launch-app2";
case APPCOMMAND_BASS_DOWN:
return "bass-down";
case APPCOMMAND_BASS_BOOST:
return "bass-boost";
case APPCOMMAND_BASS_UP:
return "bass-up";
case APPCOMMAND_TREBLE_DOWN:
return "treble-down";
case APPCOMMAND_TREBLE_UP:
return "treble-up";
case APPCOMMAND_MICROPHONE_VOLUME_MUTE:
return "microphone-volume-mute";
case APPCOMMAND_MICROPHONE_VOLUME_DOWN:
return "microphone-volume-down";
case APPCOMMAND_MICROPHONE_VOLUME_UP:
return "microphone-volume-up";
case APPCOMMAND_HELP:
return "help";
case APPCOMMAND_FIND:
return "find";
case APPCOMMAND_NEW:
return "new";
case APPCOMMAND_OPEN:
return "open";
case APPCOMMAND_CLOSE:
return "close";
case APPCOMMAND_SAVE:
return "save";
case APPCOMMAND_PRINT:
return "print";
case APPCOMMAND_UNDO:
return "undo";
case APPCOMMAND_REDO:
return "redo";
case APPCOMMAND_COPY:
return "copy";
case APPCOMMAND_CUT:
return "cut";
case APPCOMMAND_PASTE:
return "paste";
case APPCOMMAND_REPLY_TO_MAIL:
return "reply-to-mail";
case APPCOMMAND_FORWARD_MAIL:
return "forward-mail";
case APPCOMMAND_SEND_MAIL:
return "send-mail";
case APPCOMMAND_SPELL_CHECK:
return "spell-check";
case APPCOMMAND_MIC_ON_OFF_TOGGLE:
return "mic-on-off-toggle";
case APPCOMMAND_CORRECTION_LIST:
return "correction-list";
case APPCOMMAND_MEDIA_PLAY:
return "media-play";
case APPCOMMAND_MEDIA_PAUSE:
return "media-pause";
case APPCOMMAND_MEDIA_RECORD:
return "media-record";
case APPCOMMAND_MEDIA_FAST_FORWARD:
return "media-fast-forward";
case APPCOMMAND_MEDIA_REWIND:
return "media-rewind";
case APPCOMMAND_MEDIA_CHANNEL_UP:
return "media-channel-up";
case APPCOMMAND_MEDIA_CHANNEL_DOWN:
return "media-channel-down";
case APPCOMMAND_DELETE:
return "delete";
case APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE:
return "dictate-or-command-control-toggle";
default:
return "unknown";
}
}
bool IsScreenReaderActive() {
UINT screenReader = 0;
SystemParametersInfo(SPI_GETSCREENREADER, 0, &screenReader, 0);
return screenReader && UiaClientsAreListening();
}
} // namespace
std::set<NativeWindowViews*> NativeWindowViews::forwarding_windows_;
HHOOK NativeWindowViews::mouse_hook_ = NULL;
bool NativeWindowViews::ExecuteWindowsCommand(int command_id) {
std::string command = AppCommandToString(command_id);
NotifyWindowExecuteWindowsCommand(command);
return false;
}
bool NativeWindowViews::PreHandleMSG(UINT message,
WPARAM w_param,
LPARAM l_param,
LRESULT* result) {
NotifyWindowMessage(message, w_param, l_param);
switch (message) {
// Screen readers send WM_GETOBJECT in order to get the accessibility
// object, so take this opportunity to push Chromium into accessible
// mode if it isn't already, always say we didn't handle the message
// because we still want Chromium to handle returning the actual
// accessibility object.
case WM_GETOBJECT: {
if (checked_for_a11y_support_)
return false;
const DWORD obj_id = static_cast<DWORD>(l_param);
if (obj_id != static_cast<DWORD>(OBJID_CLIENT)) {
return false;
}
if (!IsScreenReaderActive()) {
return false;
}
checked_for_a11y_support_ = true;
auto* const axState = content::BrowserAccessibilityState::GetInstance();
if (axState && !axState->IsAccessibleBrowser()) {
axState->OnScreenReaderDetected();
Browser::Get()->OnAccessibilitySupportChanged();
}
return false;
}
case WM_COMMAND:
// Handle thumbar button click message.
if (HIWORD(w_param) == THBN_CLICKED)
return taskbar_host_.HandleThumbarButtonEvent(LOWORD(w_param));
return false;
case WM_SIZING: {
bool prevent_default = false;
NotifyWindowWillResize(gfx::Rect(*reinterpret_cast<RECT*>(l_param)),
&prevent_default);
if (prevent_default) {
::GetWindowRect(GetAcceleratedWidget(),
reinterpret_cast<RECT*>(l_param));
}
return false;
}
case WM_SIZE: {
// Handle window state change.
HandleSizeEvent(w_param, l_param);
consecutive_moves_ = false;
last_normal_bounds_before_move_ = last_normal_bounds_;
return false;
}
case WM_MOVING: {
bool prevent_default = false;
NotifyWindowWillMove(gfx::Rect(*reinterpret_cast<RECT*>(l_param)),
&prevent_default);
if (!movable_ || prevent_default) {
::GetWindowRect(GetAcceleratedWidget(),
reinterpret_cast<RECT*>(l_param));
return true; // Tells Windows that the Move is handled. If not true,
// frameless windows can be moved using
// -webkit-app-region: drag elements.
}
return false;
}
case WM_MOVE: {
if (last_window_state_ == ui::SHOW_STATE_NORMAL) {
if (consecutive_moves_)
last_normal_bounds_ = last_normal_bounds_candidate_;
last_normal_bounds_candidate_ = GetBounds();
consecutive_moves_ = true;
}
return false;
}
case WM_ENDSESSION: {
if (w_param) {
NotifyWindowEndSession();
}
return false;
}
case WM_PARENTNOTIFY: {
if (LOWORD(w_param) == WM_CREATE) {
// Because of reasons regarding legacy drivers and stuff, a window that
// matches the client area is created and used internally by Chromium.
// This is used when forwarding mouse messages. We only cache the first
// occurrence (the webview window) because dev tools also cause this
// message to be sent.
if (!legacy_window_) {
legacy_window_ = reinterpret_cast<HWND>(l_param);
}
}
return false;
}
default:
return false;
}
}
void NativeWindowViews::HandleSizeEvent(WPARAM w_param, LPARAM l_param) {
// Here we handle the WM_SIZE event in order to figure out what is the current
// window state and notify the user accordingly.
switch (w_param) {
case SIZE_MAXIMIZED:
last_window_state_ = ui::SHOW_STATE_MAXIMIZED;
if (consecutive_moves_) {
last_normal_bounds_ = last_normal_bounds_before_move_;
}
NotifyWindowMaximize();
break;
case SIZE_MINIMIZED:
last_window_state_ = ui::SHOW_STATE_MINIMIZED;
NotifyWindowMinimize();
break;
case SIZE_RESTORED:
if (last_window_state_ == ui::SHOW_STATE_NORMAL) {
// Window was resized so we save it's new size.
last_normal_bounds_ = GetBounds();
last_normal_bounds_before_move_ = last_normal_bounds_;
} else {
switch (last_window_state_) {
case ui::SHOW_STATE_MAXIMIZED:
last_window_state_ = ui::SHOW_STATE_NORMAL;
// Don't force out last known bounds onto the window as Windows
// actually gets these correct
NotifyWindowUnmaximize();
break;
case ui::SHOW_STATE_MINIMIZED:
if (IsFullscreen()) {
last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
NotifyWindowEnterFullScreen();
} else {
last_window_state_ = ui::SHOW_STATE_NORMAL;
// When the window is restored we resize it to the previous known
// normal size.
SetBounds(last_normal_bounds_, false);
NotifyWindowRestore();
}
break;
default:
break;
}
}
break;
}
}
void NativeWindowViews::SetForwardMouseMessages(bool forward) {
if (forward && !forwarding_mouse_messages_) {
forwarding_mouse_messages_ = true;
forwarding_windows_.insert(this);
// Subclassing is used to fix some issues when forwarding mouse messages;
// see comments in |SubclassProc|.
SetWindowSubclass(legacy_window_, SubclassProc, 1,
reinterpret_cast<DWORD_PTR>(this));
if (!mouse_hook_) {
mouse_hook_ = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
}
} else if (!forward && forwarding_mouse_messages_) {
forwarding_mouse_messages_ = false;
forwarding_windows_.erase(this);
RemoveWindowSubclass(legacy_window_, SubclassProc, 1);
if (forwarding_windows_.size() == 0) {
UnhookWindowsHookEx(mouse_hook_);
mouse_hook_ = NULL;
}
}
}
LRESULT CALLBACK NativeWindowViews::SubclassProc(HWND hwnd,
UINT msg,
WPARAM w_param,
LPARAM l_param,
UINT_PTR subclass_id,
DWORD_PTR ref_data) {
NativeWindowViews* window = reinterpret_cast<NativeWindowViews*>(ref_data);
switch (msg) {
case WM_MOUSELEAVE: {
// When input is forwarded to underlying windows, this message is posted.
// If not handled, it interferes with Chromium logic, causing for example
// mouseleave events to fire. If those events are used to exit forward
// mode, excessive flickering on for example hover items in underlying
// windows can occur due to rapidly entering and leaving forwarding mode.
// By consuming and ignoring the message, we're essentially telling
// Chromium that we have not left the window despite somebody else getting
// the messages. As to why this is catched for the legacy window and not
// the actual browser window is simply that the legacy window somehow
// makes use of these events; posting to the main window didn't work.
if (window->forwarding_mouse_messages_) {
return 0;
}
break;
}
}
return DefSubclassProc(hwnd, msg, w_param, l_param);
}
LRESULT CALLBACK NativeWindowViews::MouseHookProc(int n_code,
WPARAM w_param,
LPARAM l_param) {
if (n_code < 0) {
return CallNextHookEx(NULL, n_code, w_param, l_param);
}
// Post a WM_MOUSEMOVE message for those windows whose client area contains
// the cursor since they are in a state where they would otherwise ignore all
// mouse input.
if (w_param == WM_MOUSEMOVE) {
for (auto* window : forwarding_windows_) {
// At first I considered enumerating windows to check whether the cursor
// was directly above the window, but since nothing bad seems to happen
// if we post the message even if some other window occludes it I have
// just left it as is.
RECT client_rect;
GetClientRect(window->legacy_window_, &client_rect);
POINT p = reinterpret_cast<MSLLHOOKSTRUCT*>(l_param)->pt;
ScreenToClient(window->legacy_window_, &p);
if (PtInRect(&client_rect, p)) {
WPARAM w = 0; // No virtual keys pressed for our purposes
LPARAM l = MAKELPARAM(p.x, p.y);
PostMessage(window->legacy_window_, WM_MOUSEMOVE, w, l);
}
}
}
return CallNextHookEx(NULL, n_code, w_param, l_param);
}
} // namespace atom