native_window_views_win.cc 19 KB

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