native_window_views_win.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. switch (message) {
  209. // Screen readers send WM_GETOBJECT in order to get the accessibility
  210. // object, so take this opportunity to push Chromium into accessible
  211. // mode if it isn't already, always say we didn't handle the message
  212. // because we still want Chromium to handle returning the actual
  213. // accessibility object.
  214. case WM_GETOBJECT: {
  215. if (checked_for_a11y_support_)
  216. return false;
  217. const DWORD obj_id = static_cast<DWORD>(l_param);
  218. if (obj_id != static_cast<DWORD>(OBJID_CLIENT)) {
  219. return false;
  220. }
  221. if (!IsScreenReaderActive()) {
  222. return false;
  223. }
  224. checked_for_a11y_support_ = true;
  225. auto* const axState = content::BrowserAccessibilityState::GetInstance();
  226. if (axState && !axState->IsAccessibleBrowser()) {
  227. axState->OnScreenReaderDetected();
  228. Browser::Get()->OnAccessibilitySupportChanged();
  229. }
  230. return false;
  231. }
  232. case WM_GETMINMAXINFO: {
  233. WINDOWPLACEMENT wp;
  234. wp.length = sizeof(WINDOWPLACEMENT);
  235. // We do this to work around a Windows bug, where the minimized Window
  236. // would report that the closest display to it is not the one that it was
  237. // previously on (but the leftmost one instead). We restore the position
  238. // of the window during the restore operation, this way chromium can
  239. // use the proper display to calculate the scale factor to use.
  240. if (!last_normal_placement_bounds_.IsEmpty() &&
  241. (IsVisible() || IsMinimized()) &&
  242. GetWindowPlacement(GetAcceleratedWidget(), &wp)) {
  243. wp.rcNormalPosition = last_normal_placement_bounds_.ToRECT();
  244. // When calling SetWindowPlacement, Chromium would do window messages
  245. // handling. But since we are already in PreHandleMSG this would cause
  246. // crash in Chromium under some cases.
  247. //
  248. // We work around the crash by prevent Chromium from handling window
  249. // messages until the SetWindowPlacement call is done.
  250. //
  251. // See https://github.com/electron/electron/issues/21614 for more.
  252. is_setting_window_placement_ = true;
  253. SetWindowPlacement(GetAcceleratedWidget(), &wp);
  254. is_setting_window_placement_ = false;
  255. last_normal_placement_bounds_ = gfx::Rect();
  256. }
  257. return false;
  258. }
  259. case WM_COMMAND:
  260. // Handle thumbar button click message.
  261. if (HIWORD(w_param) == THBN_CLICKED)
  262. return taskbar_host_.HandleThumbarButtonEvent(LOWORD(w_param));
  263. return false;
  264. case WM_SIZING: {
  265. is_resizing_ = 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. NotifyWindowWillResize(dpi_bounds, GetWindowResizeEdge(w_param),
  271. &prevent_default);
  272. if (prevent_default) {
  273. ::GetWindowRect(hwnd, reinterpret_cast<RECT*>(l_param));
  274. pending_bounds_change_.reset();
  275. return true; // Tells Windows that the Sizing is handled.
  276. }
  277. return false;
  278. }
  279. case WM_SIZE: {
  280. // Handle window state change.
  281. HandleSizeEvent(w_param, l_param);
  282. return false;
  283. }
  284. case WM_EXITSIZEMOVE: {
  285. if (is_resizing_) {
  286. NotifyWindowResized();
  287. is_resizing_ = false;
  288. }
  289. if (is_moving_) {
  290. NotifyWindowMoved();
  291. is_moving_ = false;
  292. }
  293. // If the user dragged or moved the window during one or more
  294. // calls to window.setBounds(), we want to apply the most recent
  295. // one once they are done with the move or resize operation.
  296. if (pending_bounds_change_.has_value()) {
  297. SetBounds(pending_bounds_change_.value(), false /* animate */);
  298. pending_bounds_change_.reset();
  299. }
  300. return false;
  301. }
  302. case WM_MOVING: {
  303. is_moving_ = true;
  304. bool prevent_default = false;
  305. gfx::Rect bounds = gfx::Rect(*reinterpret_cast<RECT*>(l_param));
  306. HWND hwnd = GetAcceleratedWidget();
  307. gfx::Rect dpi_bounds = ScreenToDIPRect(hwnd, bounds);
  308. NotifyWindowWillMove(dpi_bounds, &prevent_default);
  309. if (!movable_ || prevent_default) {
  310. ::GetWindowRect(hwnd, reinterpret_cast<RECT*>(l_param));
  311. pending_bounds_change_.reset();
  312. return true; // Tells Windows that the Move is handled. If not true,
  313. // frameless windows can be moved using
  314. // -webkit-app-region: drag elements.
  315. }
  316. return false;
  317. }
  318. case WM_ENDSESSION: {
  319. if (w_param) {
  320. NotifyWindowEndSession();
  321. }
  322. return false;
  323. }
  324. case WM_PARENTNOTIFY: {
  325. if (LOWORD(w_param) == WM_CREATE) {
  326. // Because of reasons regarding legacy drivers and stuff, a window that
  327. // matches the client area is created and used internally by Chromium.
  328. // This is used when forwarding mouse messages. We only cache the first
  329. // occurrence (the webview window) because dev tools also cause this
  330. // message to be sent.
  331. if (!legacy_window_) {
  332. legacy_window_ = reinterpret_cast<HWND>(l_param);
  333. }
  334. }
  335. return false;
  336. }
  337. case WM_CONTEXTMENU: {
  338. bool prevent_default = false;
  339. NotifyWindowSystemContextMenu(GET_X_LPARAM(l_param),
  340. GET_Y_LPARAM(l_param), &prevent_default);
  341. return prevent_default;
  342. }
  343. case WM_SYSCOMMAND: {
  344. // Mask is needed to account for double clicking title bar to maximize
  345. WPARAM max_mask = 0xFFF0;
  346. if (transparent() && ((w_param & max_mask) == SC_MAXIMIZE)) {
  347. return true;
  348. }
  349. return false;
  350. }
  351. case WM_INITMENU: {
  352. // This is handling the scenario where the menu might get triggered by the
  353. // user doing "alt + space" resulting in system maximization and restore
  354. // being used on transparent windows when that does not work.
  355. if (transparent()) {
  356. HMENU menu = GetSystemMenu(GetAcceleratedWidget(), false);
  357. EnableMenuItem(menu, SC_MAXIMIZE,
  358. MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  359. EnableMenuItem(menu, SC_RESTORE,
  360. MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  361. return true;
  362. }
  363. return false;
  364. }
  365. default: {
  366. return false;
  367. }
  368. }
  369. }
  370. void NativeWindowViews::HandleSizeEvent(WPARAM w_param, LPARAM l_param) {
  371. // Here we handle the WM_SIZE event in order to figure out what is the current
  372. // window state and notify the user accordingly.
  373. switch (w_param) {
  374. case SIZE_MAXIMIZED:
  375. case SIZE_MINIMIZED: {
  376. WINDOWPLACEMENT wp;
  377. wp.length = sizeof(WINDOWPLACEMENT);
  378. if (GetWindowPlacement(GetAcceleratedWidget(), &wp)) {
  379. last_normal_placement_bounds_ = gfx::Rect(wp.rcNormalPosition);
  380. }
  381. // Note that SIZE_MAXIMIZED and SIZE_MINIMIZED might be emitted for
  382. // multiple times for one resize because of the SetWindowPlacement call.
  383. if (w_param == SIZE_MAXIMIZED &&
  384. last_window_state_ != ui::SHOW_STATE_MAXIMIZED) {
  385. if (last_window_state_ == ui::SHOW_STATE_MINIMIZED)
  386. NotifyWindowRestore();
  387. last_window_state_ = ui::SHOW_STATE_MAXIMIZED;
  388. NotifyWindowMaximize();
  389. ResetWindowControls();
  390. } else if (w_param == SIZE_MINIMIZED &&
  391. last_window_state_ != ui::SHOW_STATE_MINIMIZED) {
  392. last_window_state_ = ui::SHOW_STATE_MINIMIZED;
  393. NotifyWindowMinimize();
  394. }
  395. break;
  396. }
  397. case SIZE_RESTORED: {
  398. switch (last_window_state_) {
  399. case ui::SHOW_STATE_MAXIMIZED:
  400. last_window_state_ = ui::SHOW_STATE_NORMAL;
  401. NotifyWindowUnmaximize();
  402. break;
  403. case ui::SHOW_STATE_MINIMIZED:
  404. if (IsFullscreen()) {
  405. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  406. NotifyWindowEnterFullScreen();
  407. } else {
  408. last_window_state_ = ui::SHOW_STATE_NORMAL;
  409. NotifyWindowRestore();
  410. }
  411. break;
  412. default:
  413. break;
  414. }
  415. ResetWindowControls();
  416. break;
  417. }
  418. }
  419. }
  420. void NativeWindowViews::ResetWindowControls() {
  421. // If a given window was minimized and has since been
  422. // unminimized (restored/maximized), ensure the WCO buttons
  423. // are reset to their default unpressed state.
  424. auto* ncv = widget()->non_client_view();
  425. if (IsWindowControlsOverlayEnabled() && ncv) {
  426. auto* frame_view = static_cast<WinFrameView*>(ncv->frame_view());
  427. frame_view->caption_button_container()->ResetWindowControls();
  428. }
  429. }
  430. void NativeWindowViews::SetForwardMouseMessages(bool forward) {
  431. if (forward && !forwarding_mouse_messages_) {
  432. forwarding_mouse_messages_ = true;
  433. forwarding_windows_.insert(this);
  434. // Subclassing is used to fix some issues when forwarding mouse messages;
  435. // see comments in |SubclassProc|.
  436. SetWindowSubclass(legacy_window_, SubclassProc, 1,
  437. reinterpret_cast<DWORD_PTR>(this));
  438. if (!mouse_hook_) {
  439. mouse_hook_ = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
  440. }
  441. } else if (!forward && forwarding_mouse_messages_) {
  442. forwarding_mouse_messages_ = false;
  443. forwarding_windows_.erase(this);
  444. RemoveWindowSubclass(legacy_window_, SubclassProc, 1);
  445. if (forwarding_windows_.empty()) {
  446. UnhookWindowsHookEx(mouse_hook_);
  447. mouse_hook_ = NULL;
  448. }
  449. }
  450. }
  451. LRESULT CALLBACK NativeWindowViews::SubclassProc(HWND hwnd,
  452. UINT msg,
  453. WPARAM w_param,
  454. LPARAM l_param,
  455. UINT_PTR subclass_id,
  456. DWORD_PTR ref_data) {
  457. auto* window = reinterpret_cast<NativeWindowViews*>(ref_data);
  458. switch (msg) {
  459. case WM_MOUSELEAVE: {
  460. // When input is forwarded to underlying windows, this message is posted.
  461. // If not handled, it interferes with Chromium logic, causing for example
  462. // mouseleave events to fire. If those events are used to exit forward
  463. // mode, excessive flickering on for example hover items in underlying
  464. // windows can occur due to rapidly entering and leaving forwarding mode.
  465. // By consuming and ignoring the message, we're essentially telling
  466. // Chromium that we have not left the window despite somebody else getting
  467. // the messages. As to why this is caught for the legacy window and not
  468. // the actual browser window is simply that the legacy window somehow
  469. // makes use of these events; posting to the main window didn't work.
  470. if (window->forwarding_mouse_messages_) {
  471. return 0;
  472. }
  473. break;
  474. }
  475. }
  476. return DefSubclassProc(hwnd, msg, w_param, l_param);
  477. }
  478. LRESULT CALLBACK NativeWindowViews::MouseHookProc(int n_code,
  479. WPARAM w_param,
  480. LPARAM l_param) {
  481. if (n_code < 0) {
  482. return CallNextHookEx(NULL, n_code, w_param, l_param);
  483. }
  484. // Post a WM_MOUSEMOVE message for those windows whose client area contains
  485. // the cursor since they are in a state where they would otherwise ignore all
  486. // mouse input.
  487. if (w_param == WM_MOUSEMOVE) {
  488. for (auto* window : forwarding_windows_) {
  489. // At first I considered enumerating windows to check whether the cursor
  490. // was directly above the window, but since nothing bad seems to happen
  491. // if we post the message even if some other window occludes it I have
  492. // just left it as is.
  493. RECT client_rect;
  494. GetClientRect(window->legacy_window_, &client_rect);
  495. POINT p = reinterpret_cast<MSLLHOOKSTRUCT*>(l_param)->pt;
  496. ScreenToClient(window->legacy_window_, &p);
  497. if (PtInRect(&client_rect, p)) {
  498. WPARAM w = 0; // No virtual keys pressed for our purposes
  499. LPARAM l = MAKELPARAM(p.x, p.y);
  500. PostMessage(window->legacy_window_, WM_MOUSEMOVE, w, l);
  501. }
  502. }
  503. }
  504. return CallNextHookEx(NULL, n_code, w_param, l_param);
  505. }
  506. } // namespace electron