native_window_views_win.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include <dwmapi.h>
  5. #include <shellapi.h>
  6. #include "content/public/browser/browser_accessibility_state.h"
  7. #include "shell/browser/browser.h"
  8. #include "shell/browser/native_window_views.h"
  9. #include "shell/browser/ui/views/root_view.h"
  10. #include "shell/common/electron_constants.h"
  11. #include "ui/base/win/accessibility_misc_utils.h"
  12. #include "ui/display/display.h"
  13. #include "ui/display/win/screen_win.h"
  14. #include "ui/gfx/geometry/insets.h"
  15. #include "ui/views/widget/native_widget_private.h"
  16. // Must be included after other Windows headers.
  17. #include <UIAutomationCoreApi.h>
  18. namespace electron {
  19. namespace {
  20. // Convert Win32 WM_APPCOMMANDS to strings.
  21. const char* AppCommandToString(int command_id) {
  22. switch (command_id) {
  23. case APPCOMMAND_BROWSER_BACKWARD:
  24. return kBrowserBackward;
  25. case APPCOMMAND_BROWSER_FORWARD:
  26. return kBrowserForward;
  27. case APPCOMMAND_BROWSER_REFRESH:
  28. return "browser-refresh";
  29. case APPCOMMAND_BROWSER_STOP:
  30. return "browser-stop";
  31. case APPCOMMAND_BROWSER_SEARCH:
  32. return "browser-search";
  33. case APPCOMMAND_BROWSER_FAVORITES:
  34. return "browser-favorites";
  35. case APPCOMMAND_BROWSER_HOME:
  36. return "browser-home";
  37. case APPCOMMAND_VOLUME_MUTE:
  38. return "volume-mute";
  39. case APPCOMMAND_VOLUME_DOWN:
  40. return "volume-down";
  41. case APPCOMMAND_VOLUME_UP:
  42. return "volume-up";
  43. case APPCOMMAND_MEDIA_NEXTTRACK:
  44. return "media-nexttrack";
  45. case APPCOMMAND_MEDIA_PREVIOUSTRACK:
  46. return "media-previoustrack";
  47. case APPCOMMAND_MEDIA_STOP:
  48. return "media-stop";
  49. case APPCOMMAND_MEDIA_PLAY_PAUSE:
  50. return "media-play-pause";
  51. case APPCOMMAND_LAUNCH_MAIL:
  52. return "launch-mail";
  53. case APPCOMMAND_LAUNCH_MEDIA_SELECT:
  54. return "launch-media-select";
  55. case APPCOMMAND_LAUNCH_APP1:
  56. return "launch-app1";
  57. case APPCOMMAND_LAUNCH_APP2:
  58. return "launch-app2";
  59. case APPCOMMAND_BASS_DOWN:
  60. return "bass-down";
  61. case APPCOMMAND_BASS_BOOST:
  62. return "bass-boost";
  63. case APPCOMMAND_BASS_UP:
  64. return "bass-up";
  65. case APPCOMMAND_TREBLE_DOWN:
  66. return "treble-down";
  67. case APPCOMMAND_TREBLE_UP:
  68. return "treble-up";
  69. case APPCOMMAND_MICROPHONE_VOLUME_MUTE:
  70. return "microphone-volume-mute";
  71. case APPCOMMAND_MICROPHONE_VOLUME_DOWN:
  72. return "microphone-volume-down";
  73. case APPCOMMAND_MICROPHONE_VOLUME_UP:
  74. return "microphone-volume-up";
  75. case APPCOMMAND_HELP:
  76. return "help";
  77. case APPCOMMAND_FIND:
  78. return "find";
  79. case APPCOMMAND_NEW:
  80. return "new";
  81. case APPCOMMAND_OPEN:
  82. return "open";
  83. case APPCOMMAND_CLOSE:
  84. return "close";
  85. case APPCOMMAND_SAVE:
  86. return "save";
  87. case APPCOMMAND_PRINT:
  88. return "print";
  89. case APPCOMMAND_UNDO:
  90. return "undo";
  91. case APPCOMMAND_REDO:
  92. return "redo";
  93. case APPCOMMAND_COPY:
  94. return "copy";
  95. case APPCOMMAND_CUT:
  96. return "cut";
  97. case APPCOMMAND_PASTE:
  98. return "paste";
  99. case APPCOMMAND_REPLY_TO_MAIL:
  100. return "reply-to-mail";
  101. case APPCOMMAND_FORWARD_MAIL:
  102. return "forward-mail";
  103. case APPCOMMAND_SEND_MAIL:
  104. return "send-mail";
  105. case APPCOMMAND_SPELL_CHECK:
  106. return "spell-check";
  107. case APPCOMMAND_MIC_ON_OFF_TOGGLE:
  108. return "mic-on-off-toggle";
  109. case APPCOMMAND_CORRECTION_LIST:
  110. return "correction-list";
  111. case APPCOMMAND_MEDIA_PLAY:
  112. return "media-play";
  113. case APPCOMMAND_MEDIA_PAUSE:
  114. return "media-pause";
  115. case APPCOMMAND_MEDIA_RECORD:
  116. return "media-record";
  117. case APPCOMMAND_MEDIA_FAST_FORWARD:
  118. return "media-fast-forward";
  119. case APPCOMMAND_MEDIA_REWIND:
  120. return "media-rewind";
  121. case APPCOMMAND_MEDIA_CHANNEL_UP:
  122. return "media-channel-up";
  123. case APPCOMMAND_MEDIA_CHANNEL_DOWN:
  124. return "media-channel-down";
  125. case APPCOMMAND_DELETE:
  126. return "delete";
  127. case APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE:
  128. return "dictate-or-command-control-toggle";
  129. default:
  130. return "unknown";
  131. }
  132. }
  133. bool IsScreenReaderActive() {
  134. UINT screenReader = 0;
  135. SystemParametersInfo(SPI_GETSCREENREADER, 0, &screenReader, 0);
  136. return screenReader && UiaClientsAreListening();
  137. }
  138. } // namespace
  139. std::set<NativeWindowViews*> NativeWindowViews::forwarding_windows_;
  140. HHOOK NativeWindowViews::mouse_hook_ = NULL;
  141. void NativeWindowViews::Maximize() {
  142. // Only use Maximize() when window is NOT transparent style
  143. if (!transparent()) {
  144. if (IsVisible())
  145. widget()->Maximize();
  146. else
  147. widget()->native_widget_private()->Show(ui::SHOW_STATE_MAXIMIZED,
  148. gfx::Rect());
  149. return;
  150. } else {
  151. restore_bounds_ = GetBounds();
  152. auto display = display::Screen::GetScreen()->GetDisplayNearestWindow(
  153. GetNativeWindow());
  154. SetBounds(display.work_area(), false);
  155. }
  156. }
  157. bool NativeWindowViews::ExecuteWindowsCommand(int command_id) {
  158. std::string command = AppCommandToString(command_id);
  159. NotifyWindowExecuteAppCommand(command);
  160. return false;
  161. }
  162. bool NativeWindowViews::PreHandleMSG(UINT message,
  163. WPARAM w_param,
  164. LPARAM l_param,
  165. LRESULT* result) {
  166. NotifyWindowMessage(message, w_param, l_param);
  167. // Avoid side effects when calling SetWindowPlacement.
  168. if (is_setting_window_placement_) {
  169. // Let Chromium handle the WM_NCCALCSIZE message otherwise the window size
  170. // would be wrong.
  171. // See https://github.com/electron/electron/issues/22393 for more.
  172. if (message == WM_NCCALCSIZE)
  173. return false;
  174. // Otherwise handle the message with default proc,
  175. *result = DefWindowProc(GetAcceleratedWidget(), message, w_param, l_param);
  176. // and tell Chromium to ignore this message.
  177. return true;
  178. }
  179. switch (message) {
  180. // Screen readers send WM_GETOBJECT in order to get the accessibility
  181. // object, so take this opportunity to push Chromium into accessible
  182. // mode if it isn't already, always say we didn't handle the message
  183. // because we still want Chromium to handle returning the actual
  184. // accessibility object.
  185. case WM_GETOBJECT: {
  186. if (checked_for_a11y_support_)
  187. return false;
  188. const DWORD obj_id = static_cast<DWORD>(l_param);
  189. if (obj_id != static_cast<DWORD>(OBJID_CLIENT)) {
  190. return false;
  191. }
  192. if (!IsScreenReaderActive()) {
  193. return false;
  194. }
  195. checked_for_a11y_support_ = true;
  196. auto* const axState = content::BrowserAccessibilityState::GetInstance();
  197. if (axState && !axState->IsAccessibleBrowser()) {
  198. axState->OnScreenReaderDetected();
  199. Browser::Get()->OnAccessibilitySupportChanged();
  200. }
  201. return false;
  202. }
  203. case WM_GETMINMAXINFO: {
  204. WINDOWPLACEMENT wp;
  205. wp.length = sizeof(WINDOWPLACEMENT);
  206. // We do this to work around a Windows bug, where the minimized Window
  207. // would report that the closest display to it is not the one that it was
  208. // previously on (but the leftmost one instead). We restore the position
  209. // of the window during the restore operation, this way chromium can
  210. // use the proper display to calculate the scale factor to use.
  211. if (!last_normal_placement_bounds_.IsEmpty() &&
  212. (IsVisible() || IsMinimized()) &&
  213. GetWindowPlacement(GetAcceleratedWidget(), &wp)) {
  214. wp.rcNormalPosition = last_normal_placement_bounds_.ToRECT();
  215. // When calling SetWindowPlacement, Chromium would do window messages
  216. // handling. But since we are already in PreHandleMSG this would cause
  217. // crash in Chromium under some cases.
  218. //
  219. // We work around the crash by prevent Chromium from handling window
  220. // messages until the SetWindowPlacement call is done.
  221. //
  222. // See https://github.com/electron/electron/issues/21614 for more.
  223. is_setting_window_placement_ = true;
  224. SetWindowPlacement(GetAcceleratedWidget(), &wp);
  225. is_setting_window_placement_ = false;
  226. last_normal_placement_bounds_ = gfx::Rect();
  227. }
  228. return false;
  229. }
  230. case WM_COMMAND:
  231. // Handle thumbar button click message.
  232. if (HIWORD(w_param) == THBN_CLICKED)
  233. return taskbar_host_.HandleThumbarButtonEvent(LOWORD(w_param));
  234. return false;
  235. case WM_SIZING: {
  236. is_resizing_ = true;
  237. bool prevent_default = false;
  238. gfx::Rect bounds = gfx::Rect(*reinterpret_cast<RECT*>(l_param));
  239. HWND hwnd = GetAcceleratedWidget();
  240. gfx::Rect dpi_bounds = ScreenToDIPRect(hwnd, bounds);
  241. NotifyWindowWillResize(dpi_bounds, &prevent_default);
  242. if (prevent_default) {
  243. ::GetWindowRect(hwnd, reinterpret_cast<RECT*>(l_param));
  244. return true; // Tells Windows that the Sizing is handled.
  245. }
  246. return false;
  247. }
  248. case WM_SIZE: {
  249. // Handle window state change.
  250. HandleSizeEvent(w_param, l_param);
  251. return false;
  252. }
  253. case WM_EXITSIZEMOVE: {
  254. if (is_resizing_) {
  255. NotifyWindowResized();
  256. is_resizing_ = false;
  257. }
  258. if (is_moving_) {
  259. NotifyWindowMoved();
  260. is_moving_ = false;
  261. }
  262. return false;
  263. }
  264. case WM_MOVING: {
  265. is_moving_ = true;
  266. bool prevent_default = false;
  267. gfx::Rect bounds = gfx::Rect(*reinterpret_cast<RECT*>(l_param));
  268. HWND hwnd = GetAcceleratedWidget();
  269. gfx::Rect dpi_bounds = ScreenToDIPRect(hwnd, bounds);
  270. NotifyWindowWillMove(dpi_bounds, &prevent_default);
  271. if (!movable_ || prevent_default) {
  272. ::GetWindowRect(hwnd, reinterpret_cast<RECT*>(l_param));
  273. return true; // Tells Windows that the Move is handled. If not true,
  274. // frameless windows can be moved using
  275. // -webkit-app-region: drag elements.
  276. }
  277. return false;
  278. }
  279. case WM_ENDSESSION: {
  280. if (w_param) {
  281. NotifyWindowEndSession();
  282. }
  283. return false;
  284. }
  285. case WM_PARENTNOTIFY: {
  286. if (LOWORD(w_param) == WM_CREATE) {
  287. // Because of reasons regarding legacy drivers and stuff, a window that
  288. // matches the client area is created and used internally by Chromium.
  289. // This is used when forwarding mouse messages. We only cache the first
  290. // occurrence (the webview window) because dev tools also cause this
  291. // message to be sent.
  292. if (!legacy_window_) {
  293. legacy_window_ = reinterpret_cast<HWND>(l_param);
  294. }
  295. }
  296. return false;
  297. }
  298. case WM_CONTEXTMENU: {
  299. bool prevent_default = false;
  300. NotifyWindowSystemContextMenu(GET_X_LPARAM(l_param),
  301. GET_Y_LPARAM(l_param), &prevent_default);
  302. return prevent_default;
  303. }
  304. case WM_SYSCOMMAND: {
  305. // Mask is needed to account for double clicking title bar to maximize
  306. WPARAM max_mask = 0xFFF0;
  307. if (transparent() && ((w_param & max_mask) == SC_MAXIMIZE)) {
  308. return true;
  309. }
  310. return false;
  311. }
  312. case WM_INITMENU: {
  313. // This is handling the scenario where the menu might get triggered by the
  314. // user doing "alt + space" resulting in system maximization and restore
  315. // being used on transparent windows when that does not work.
  316. if (transparent()) {
  317. HMENU menu = GetSystemMenu(GetAcceleratedWidget(), false);
  318. EnableMenuItem(menu, SC_MAXIMIZE,
  319. MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  320. EnableMenuItem(menu, SC_RESTORE,
  321. MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  322. return true;
  323. }
  324. return false;
  325. }
  326. default: {
  327. return false;
  328. }
  329. }
  330. }
  331. void NativeWindowViews::HandleSizeEvent(WPARAM w_param, LPARAM l_param) {
  332. // Here we handle the WM_SIZE event in order to figure out what is the current
  333. // window state and notify the user accordingly.
  334. switch (w_param) {
  335. case SIZE_MAXIMIZED:
  336. case SIZE_MINIMIZED: {
  337. WINDOWPLACEMENT wp;
  338. wp.length = sizeof(WINDOWPLACEMENT);
  339. if (GetWindowPlacement(GetAcceleratedWidget(), &wp)) {
  340. last_normal_placement_bounds_ = gfx::Rect(wp.rcNormalPosition);
  341. }
  342. // Note that SIZE_MAXIMIZED and SIZE_MINIMIZED might be emitted for
  343. // multiple times for one resize because of the SetWindowPlacement call.
  344. if (w_param == SIZE_MAXIMIZED &&
  345. last_window_state_ != ui::SHOW_STATE_MAXIMIZED) {
  346. last_window_state_ = ui::SHOW_STATE_MAXIMIZED;
  347. NotifyWindowMaximize();
  348. } else if (w_param == SIZE_MINIMIZED &&
  349. last_window_state_ != ui::SHOW_STATE_MINIMIZED) {
  350. last_window_state_ = ui::SHOW_STATE_MINIMIZED;
  351. NotifyWindowMinimize();
  352. }
  353. break;
  354. }
  355. case SIZE_RESTORED:
  356. switch (last_window_state_) {
  357. case ui::SHOW_STATE_MAXIMIZED:
  358. last_window_state_ = ui::SHOW_STATE_NORMAL;
  359. NotifyWindowUnmaximize();
  360. break;
  361. case ui::SHOW_STATE_MINIMIZED:
  362. if (IsFullscreen()) {
  363. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  364. NotifyWindowEnterFullScreen();
  365. } else {
  366. last_window_state_ = ui::SHOW_STATE_NORMAL;
  367. NotifyWindowRestore();
  368. }
  369. break;
  370. default:
  371. break;
  372. }
  373. break;
  374. }
  375. }
  376. void NativeWindowViews::SetForwardMouseMessages(bool forward) {
  377. if (forward && !forwarding_mouse_messages_) {
  378. forwarding_mouse_messages_ = true;
  379. forwarding_windows_.insert(this);
  380. // Subclassing is used to fix some issues when forwarding mouse messages;
  381. // see comments in |SubclassProc|.
  382. SetWindowSubclass(legacy_window_, SubclassProc, 1,
  383. reinterpret_cast<DWORD_PTR>(this));
  384. if (!mouse_hook_) {
  385. mouse_hook_ = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
  386. }
  387. } else if (!forward && forwarding_mouse_messages_) {
  388. forwarding_mouse_messages_ = false;
  389. forwarding_windows_.erase(this);
  390. RemoveWindowSubclass(legacy_window_, SubclassProc, 1);
  391. if (forwarding_windows_.empty()) {
  392. UnhookWindowsHookEx(mouse_hook_);
  393. mouse_hook_ = NULL;
  394. }
  395. }
  396. }
  397. LRESULT CALLBACK NativeWindowViews::SubclassProc(HWND hwnd,
  398. UINT msg,
  399. WPARAM w_param,
  400. LPARAM l_param,
  401. UINT_PTR subclass_id,
  402. DWORD_PTR ref_data) {
  403. auto* window = reinterpret_cast<NativeWindowViews*>(ref_data);
  404. switch (msg) {
  405. case WM_MOUSELEAVE: {
  406. // When input is forwarded to underlying windows, this message is posted.
  407. // If not handled, it interferes with Chromium logic, causing for example
  408. // mouseleave events to fire. If those events are used to exit forward
  409. // mode, excessive flickering on for example hover items in underlying
  410. // windows can occur due to rapidly entering and leaving forwarding mode.
  411. // By consuming and ignoring the message, we're essentially telling
  412. // Chromium that we have not left the window despite somebody else getting
  413. // the messages. As to why this is catched for the legacy window and not
  414. // the actual browser window is simply that the legacy window somehow
  415. // makes use of these events; posting to the main window didn't work.
  416. if (window->forwarding_mouse_messages_) {
  417. return 0;
  418. }
  419. break;
  420. }
  421. }
  422. return DefSubclassProc(hwnd, msg, w_param, l_param);
  423. }
  424. LRESULT CALLBACK NativeWindowViews::MouseHookProc(int n_code,
  425. WPARAM w_param,
  426. LPARAM l_param) {
  427. if (n_code < 0) {
  428. return CallNextHookEx(NULL, n_code, w_param, l_param);
  429. }
  430. // Post a WM_MOUSEMOVE message for those windows whose client area contains
  431. // the cursor since they are in a state where they would otherwise ignore all
  432. // mouse input.
  433. if (w_param == WM_MOUSEMOVE) {
  434. for (auto* window : forwarding_windows_) {
  435. // At first I considered enumerating windows to check whether the cursor
  436. // was directly above the window, but since nothing bad seems to happen
  437. // if we post the message even if some other window occludes it I have
  438. // just left it as is.
  439. RECT client_rect;
  440. GetClientRect(window->legacy_window_, &client_rect);
  441. POINT p = reinterpret_cast<MSLLHOOKSTRUCT*>(l_param)->pt;
  442. ScreenToClient(window->legacy_window_, &p);
  443. if (PtInRect(&client_rect, p)) {
  444. WPARAM w = 0; // No virtual keys pressed for our purposes
  445. LPARAM l = MAKELPARAM(p.x, p.y);
  446. PostMessage(window->legacy_window_, WM_MOUSEMOVE, w, l);
  447. }
  448. }
  449. }
  450. return CallNextHookEx(NULL, n_code, w_param, l_param);
  451. }
  452. } // namespace electron