native_window_views_win.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "atom/browser/browser.h"
  5. #include "atom/browser/native_window_views.h"
  6. #include "content/public/browser/browser_accessibility_state.h"
  7. #include "ui/base/win/accessibility_misc_utils.h"
  8. // Must be included after other Windows headers.
  9. #include <UIAutomationCoreApi.h>
  10. namespace atom {
  11. namespace {
  12. // Convert Win32 WM_APPCOMMANDS to strings.
  13. const char* AppCommandToString(int command_id) {
  14. switch (command_id) {
  15. case APPCOMMAND_BROWSER_BACKWARD:
  16. return "browser-backward";
  17. case APPCOMMAND_BROWSER_FORWARD:
  18. return "browser-forward";
  19. case APPCOMMAND_BROWSER_REFRESH:
  20. return "browser-refresh";
  21. case APPCOMMAND_BROWSER_STOP:
  22. return "browser-stop";
  23. case APPCOMMAND_BROWSER_SEARCH:
  24. return "browser-search";
  25. case APPCOMMAND_BROWSER_FAVORITES:
  26. return "browser-favorites";
  27. case APPCOMMAND_BROWSER_HOME:
  28. return "browser-home";
  29. case APPCOMMAND_VOLUME_MUTE:
  30. return "volume-mute";
  31. case APPCOMMAND_VOLUME_DOWN:
  32. return "volume-down";
  33. case APPCOMMAND_VOLUME_UP:
  34. return "volume-up";
  35. case APPCOMMAND_MEDIA_NEXTTRACK:
  36. return "media-nexttrack";
  37. case APPCOMMAND_MEDIA_PREVIOUSTRACK:
  38. return "media-previoustrack";
  39. case APPCOMMAND_MEDIA_STOP:
  40. return "media-stop";
  41. case APPCOMMAND_MEDIA_PLAY_PAUSE:
  42. return "media-play-pause";
  43. case APPCOMMAND_LAUNCH_MAIL:
  44. return "launch-mail";
  45. case APPCOMMAND_LAUNCH_MEDIA_SELECT:
  46. return "launch-media-select";
  47. case APPCOMMAND_LAUNCH_APP1:
  48. return "launch-app1";
  49. case APPCOMMAND_LAUNCH_APP2:
  50. return "launch-app2";
  51. case APPCOMMAND_BASS_DOWN:
  52. return "bass-down";
  53. case APPCOMMAND_BASS_BOOST:
  54. return "bass-boost";
  55. case APPCOMMAND_BASS_UP:
  56. return "bass-up";
  57. case APPCOMMAND_TREBLE_DOWN:
  58. return "treble-down";
  59. case APPCOMMAND_TREBLE_UP:
  60. return "treble-up";
  61. case APPCOMMAND_MICROPHONE_VOLUME_MUTE:
  62. return "microphone-volume-mute";
  63. case APPCOMMAND_MICROPHONE_VOLUME_DOWN:
  64. return "microphone-volume-down";
  65. case APPCOMMAND_MICROPHONE_VOLUME_UP:
  66. return "microphone-volume-up";
  67. case APPCOMMAND_HELP:
  68. return "help";
  69. case APPCOMMAND_FIND:
  70. return "find";
  71. case APPCOMMAND_NEW:
  72. return "new";
  73. case APPCOMMAND_OPEN:
  74. return "open";
  75. case APPCOMMAND_CLOSE:
  76. return "close";
  77. case APPCOMMAND_SAVE:
  78. return "save";
  79. case APPCOMMAND_PRINT:
  80. return "print";
  81. case APPCOMMAND_UNDO:
  82. return "undo";
  83. case APPCOMMAND_REDO:
  84. return "redo";
  85. case APPCOMMAND_COPY:
  86. return "copy";
  87. case APPCOMMAND_CUT:
  88. return "cut";
  89. case APPCOMMAND_PASTE:
  90. return "paste";
  91. case APPCOMMAND_REPLY_TO_MAIL:
  92. return "reply-to-mail";
  93. case APPCOMMAND_FORWARD_MAIL:
  94. return "forward-mail";
  95. case APPCOMMAND_SEND_MAIL:
  96. return "send-mail";
  97. case APPCOMMAND_SPELL_CHECK:
  98. return "spell-check";
  99. case APPCOMMAND_MIC_ON_OFF_TOGGLE:
  100. return "mic-on-off-toggle";
  101. case APPCOMMAND_CORRECTION_LIST:
  102. return "correction-list";
  103. case APPCOMMAND_MEDIA_PLAY:
  104. return "media-play";
  105. case APPCOMMAND_MEDIA_PAUSE:
  106. return "media-pause";
  107. case APPCOMMAND_MEDIA_RECORD:
  108. return "media-record";
  109. case APPCOMMAND_MEDIA_FAST_FORWARD:
  110. return "media-fast-forward";
  111. case APPCOMMAND_MEDIA_REWIND:
  112. return "media-rewind";
  113. case APPCOMMAND_MEDIA_CHANNEL_UP:
  114. return "media-channel-up";
  115. case APPCOMMAND_MEDIA_CHANNEL_DOWN:
  116. return "media-channel-down";
  117. case APPCOMMAND_DELETE:
  118. return "delete";
  119. case APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE:
  120. return "dictate-or-command-control-toggle";
  121. default:
  122. return "unknown";
  123. }
  124. }
  125. bool IsScreenReaderActive() {
  126. UINT screenReader = 0;
  127. SystemParametersInfo(SPI_GETSCREENREADER, 0, &screenReader, 0);
  128. return screenReader && UiaClientsAreListening();
  129. }
  130. } // namespace
  131. std::set<NativeWindowViews*> NativeWindowViews::forwarding_windows_;
  132. HHOOK NativeWindowViews::mouse_hook_ = NULL;
  133. bool NativeWindowViews::ExecuteWindowsCommand(int command_id) {
  134. std::string command = AppCommandToString(command_id);
  135. NotifyWindowExecuteWindowsCommand(command);
  136. return false;
  137. }
  138. bool NativeWindowViews::PreHandleMSG(UINT message,
  139. WPARAM w_param,
  140. LPARAM l_param,
  141. LRESULT* result) {
  142. NotifyWindowMessage(message, w_param, l_param);
  143. switch (message) {
  144. // Screen readers send WM_GETOBJECT in order to get the accessibility
  145. // object, so take this opportunity to push Chromium into accessible
  146. // mode if it isn't already, always say we didn't handle the message
  147. // because we still want Chromium to handle returning the actual
  148. // accessibility object.
  149. case WM_GETOBJECT: {
  150. if (checked_for_a11y_support_)
  151. return false;
  152. const DWORD obj_id = static_cast<DWORD>(l_param);
  153. if (obj_id != static_cast<DWORD>(OBJID_CLIENT)) {
  154. return false;
  155. }
  156. if (!IsScreenReaderActive()) {
  157. return false;
  158. }
  159. checked_for_a11y_support_ = true;
  160. auto* const axState = content::BrowserAccessibilityState::GetInstance();
  161. if (axState && !axState->IsAccessibleBrowser()) {
  162. axState->OnScreenReaderDetected();
  163. Browser::Get()->OnAccessibilitySupportChanged();
  164. }
  165. return false;
  166. }
  167. case WM_COMMAND:
  168. // Handle thumbar button click message.
  169. if (HIWORD(w_param) == THBN_CLICKED)
  170. return taskbar_host_.HandleThumbarButtonEvent(LOWORD(w_param));
  171. return false;
  172. case WM_SIZING: {
  173. bool prevent_default = false;
  174. NotifyWindowWillResize(gfx::Rect(*reinterpret_cast<RECT*>(l_param)),
  175. &prevent_default);
  176. if (prevent_default) {
  177. ::GetWindowRect(GetAcceleratedWidget(),
  178. reinterpret_cast<RECT*>(l_param));
  179. }
  180. return false;
  181. }
  182. case WM_SIZE: {
  183. // Handle window state change.
  184. HandleSizeEvent(w_param, l_param);
  185. consecutive_moves_ = false;
  186. last_normal_bounds_before_move_ = last_normal_bounds_;
  187. return false;
  188. }
  189. case WM_MOVING: {
  190. bool prevent_default = false;
  191. NotifyWindowWillMove(gfx::Rect(*reinterpret_cast<RECT*>(l_param)),
  192. &prevent_default);
  193. if (!movable_ || prevent_default) {
  194. ::GetWindowRect(GetAcceleratedWidget(),
  195. reinterpret_cast<RECT*>(l_param));
  196. return true; // Tells Windows that the Move is handled. If not true,
  197. // frameless windows can be moved using
  198. // -webkit-app-region: drag elements.
  199. }
  200. return false;
  201. }
  202. case WM_MOVE: {
  203. if (last_window_state_ == ui::SHOW_STATE_NORMAL) {
  204. if (consecutive_moves_)
  205. last_normal_bounds_ = last_normal_bounds_candidate_;
  206. last_normal_bounds_candidate_ = GetBounds();
  207. consecutive_moves_ = true;
  208. }
  209. return false;
  210. }
  211. case WM_ENDSESSION: {
  212. if (w_param) {
  213. NotifyWindowEndSession();
  214. }
  215. return false;
  216. }
  217. case WM_PARENTNOTIFY: {
  218. if (LOWORD(w_param) == WM_CREATE) {
  219. // Because of reasons regarding legacy drivers and stuff, a window that
  220. // matches the client area is created and used internally by Chromium.
  221. // This is used when forwarding mouse messages. We only cache the first
  222. // occurrence (the webview window) because dev tools also cause this
  223. // message to be sent.
  224. if (!legacy_window_) {
  225. legacy_window_ = reinterpret_cast<HWND>(l_param);
  226. }
  227. }
  228. return false;
  229. }
  230. default:
  231. return false;
  232. }
  233. }
  234. void NativeWindowViews::HandleSizeEvent(WPARAM w_param, LPARAM l_param) {
  235. // Here we handle the WM_SIZE event in order to figure out what is the current
  236. // window state and notify the user accordingly.
  237. switch (w_param) {
  238. case SIZE_MAXIMIZED:
  239. last_window_state_ = ui::SHOW_STATE_MAXIMIZED;
  240. if (consecutive_moves_) {
  241. last_normal_bounds_ = last_normal_bounds_before_move_;
  242. }
  243. NotifyWindowMaximize();
  244. break;
  245. case SIZE_MINIMIZED:
  246. last_window_state_ = ui::SHOW_STATE_MINIMIZED;
  247. NotifyWindowMinimize();
  248. break;
  249. case SIZE_RESTORED:
  250. if (last_window_state_ == ui::SHOW_STATE_NORMAL) {
  251. // Window was resized so we save it's new size.
  252. last_normal_bounds_ = GetBounds();
  253. last_normal_bounds_before_move_ = last_normal_bounds_;
  254. } else {
  255. switch (last_window_state_) {
  256. case ui::SHOW_STATE_MAXIMIZED:
  257. last_window_state_ = ui::SHOW_STATE_NORMAL;
  258. // Don't force out last known bounds onto the window as Windows
  259. // actually gets these correct
  260. NotifyWindowUnmaximize();
  261. break;
  262. case ui::SHOW_STATE_MINIMIZED:
  263. if (IsFullscreen()) {
  264. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  265. NotifyWindowEnterFullScreen();
  266. } else {
  267. last_window_state_ = ui::SHOW_STATE_NORMAL;
  268. // When the window is restored we resize it to the previous known
  269. // normal size.
  270. SetBounds(last_normal_bounds_, false);
  271. NotifyWindowRestore();
  272. }
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. break;
  279. }
  280. }
  281. void NativeWindowViews::SetForwardMouseMessages(bool forward) {
  282. if (forward && !forwarding_mouse_messages_) {
  283. forwarding_mouse_messages_ = true;
  284. forwarding_windows_.insert(this);
  285. // Subclassing is used to fix some issues when forwarding mouse messages;
  286. // see comments in |SubclassProc|.
  287. SetWindowSubclass(legacy_window_, SubclassProc, 1,
  288. reinterpret_cast<DWORD_PTR>(this));
  289. if (!mouse_hook_) {
  290. mouse_hook_ = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
  291. }
  292. } else if (!forward && forwarding_mouse_messages_) {
  293. forwarding_mouse_messages_ = false;
  294. forwarding_windows_.erase(this);
  295. RemoveWindowSubclass(legacy_window_, SubclassProc, 1);
  296. if (forwarding_windows_.size() == 0) {
  297. UnhookWindowsHookEx(mouse_hook_);
  298. mouse_hook_ = NULL;
  299. }
  300. }
  301. }
  302. LRESULT CALLBACK NativeWindowViews::SubclassProc(HWND hwnd,
  303. UINT msg,
  304. WPARAM w_param,
  305. LPARAM l_param,
  306. UINT_PTR subclass_id,
  307. DWORD_PTR ref_data) {
  308. NativeWindowViews* window = reinterpret_cast<NativeWindowViews*>(ref_data);
  309. switch (msg) {
  310. case WM_MOUSELEAVE: {
  311. // When input is forwarded to underlying windows, this message is posted.
  312. // If not handled, it interferes with Chromium logic, causing for example
  313. // mouseleave events to fire. If those events are used to exit forward
  314. // mode, excessive flickering on for example hover items in underlying
  315. // windows can occur due to rapidly entering and leaving forwarding mode.
  316. // By consuming and ignoring the message, we're essentially telling
  317. // Chromium that we have not left the window despite somebody else getting
  318. // the messages. As to why this is catched for the legacy window and not
  319. // the actual browser window is simply that the legacy window somehow
  320. // makes use of these events; posting to the main window didn't work.
  321. if (window->forwarding_mouse_messages_) {
  322. return 0;
  323. }
  324. break;
  325. }
  326. }
  327. return DefSubclassProc(hwnd, msg, w_param, l_param);
  328. }
  329. LRESULT CALLBACK NativeWindowViews::MouseHookProc(int n_code,
  330. WPARAM w_param,
  331. LPARAM l_param) {
  332. if (n_code < 0) {
  333. return CallNextHookEx(NULL, n_code, w_param, l_param);
  334. }
  335. // Post a WM_MOUSEMOVE message for those windows whose client area contains
  336. // the cursor since they are in a state where they would otherwise ignore all
  337. // mouse input.
  338. if (w_param == WM_MOUSEMOVE) {
  339. for (auto* window : forwarding_windows_) {
  340. // At first I considered enumerating windows to check whether the cursor
  341. // was directly above the window, but since nothing bad seems to happen
  342. // if we post the message even if some other window occludes it I have
  343. // just left it as is.
  344. RECT client_rect;
  345. GetClientRect(window->legacy_window_, &client_rect);
  346. POINT p = reinterpret_cast<MSLLHOOKSTRUCT*>(l_param)->pt;
  347. ScreenToClient(window->legacy_window_, &p);
  348. if (PtInRect(&client_rect, p)) {
  349. WPARAM w = 0; // No virtual keys pressed for our purposes
  350. LPARAM l = MAKELPARAM(p.x, p.y);
  351. PostMessage(window->legacy_window_, WM_MOUSEMOVE, w, l);
  352. }
  353. }
  354. }
  355. return CallNextHookEx(NULL, n_code, w_param, l_param);
  356. }
  357. } // namespace atom