native_window_views_win.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 != OBJID_CLIENT) {
  154. return false;
  155. }
  156. if (!IsScreenReaderActive()) {
  157. return false;
  158. }
  159. checked_for_a11y_support_ = true;
  160. const auto 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_SIZE: {
  173. // Handle window state change.
  174. HandleSizeEvent(w_param, l_param);
  175. consecutive_moves_ = false;
  176. last_normal_bounds_before_move_ = last_normal_bounds_;
  177. return false;
  178. }
  179. case WM_MOVING: {
  180. if (!movable_)
  181. ::GetWindowRect(GetAcceleratedWidget(), (LPRECT)l_param);
  182. return false;
  183. }
  184. case WM_MOVE: {
  185. if (last_window_state_ == ui::SHOW_STATE_NORMAL) {
  186. if (consecutive_moves_)
  187. last_normal_bounds_ = last_normal_bounds_candidate_;
  188. last_normal_bounds_candidate_ = GetBounds();
  189. consecutive_moves_ = true;
  190. }
  191. return false;
  192. }
  193. case WM_ENDSESSION: {
  194. if (w_param) {
  195. NotifyWindowEndSession();
  196. }
  197. return false;
  198. }
  199. case WM_PARENTNOTIFY: {
  200. if (LOWORD(w_param) == WM_CREATE) {
  201. // Because of reasons regarding legacy drivers and stuff, a window that
  202. // matches the client area is created and used internally by Chromium.
  203. // This is used when forwarding mouse messages. We only cache the first
  204. // occurrence (the webview window) because dev tools also cause this
  205. // message to be sent.
  206. if (!legacy_window_) {
  207. legacy_window_ = reinterpret_cast<HWND>(l_param);
  208. }
  209. }
  210. return false;
  211. }
  212. default:
  213. return false;
  214. }
  215. }
  216. void NativeWindowViews::HandleSizeEvent(WPARAM w_param, LPARAM l_param) {
  217. // Here we handle the WM_SIZE event in order to figure out what is the current
  218. // window state and notify the user accordingly.
  219. switch (w_param) {
  220. case SIZE_MAXIMIZED:
  221. last_window_state_ = ui::SHOW_STATE_MAXIMIZED;
  222. if (consecutive_moves_) {
  223. last_normal_bounds_ = last_normal_bounds_before_move_;
  224. }
  225. NotifyWindowMaximize();
  226. break;
  227. case SIZE_MINIMIZED:
  228. last_window_state_ = ui::SHOW_STATE_MINIMIZED;
  229. NotifyWindowMinimize();
  230. break;
  231. case SIZE_RESTORED:
  232. if (last_window_state_ == ui::SHOW_STATE_NORMAL) {
  233. // Window was resized so we save it's new size.
  234. last_normal_bounds_ = GetBounds();
  235. last_normal_bounds_before_move_ = last_normal_bounds_;
  236. } else {
  237. switch (last_window_state_) {
  238. case ui::SHOW_STATE_MAXIMIZED:
  239. last_window_state_ = ui::SHOW_STATE_NORMAL;
  240. // Don't force out last known bounds onto the window as Windows
  241. // actually gets these correct
  242. NotifyWindowUnmaximize();
  243. break;
  244. case ui::SHOW_STATE_MINIMIZED:
  245. if (IsFullscreen()) {
  246. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  247. NotifyWindowEnterFullScreen();
  248. } else {
  249. last_window_state_ = ui::SHOW_STATE_NORMAL;
  250. // When the window is restored we resize it to the previous known
  251. // normal size.
  252. SetBounds(last_normal_bounds_, false);
  253. NotifyWindowRestore();
  254. }
  255. break;
  256. }
  257. }
  258. break;
  259. }
  260. }
  261. void NativeWindowViews::SetForwardMouseMessages(bool forward) {
  262. if (forward && !forwarding_mouse_messages_) {
  263. forwarding_mouse_messages_ = true;
  264. forwarding_windows_.insert(this);
  265. // Subclassing is used to fix some issues when forwarding mouse messages;
  266. // see comments in |SubclassProc|.
  267. SetWindowSubclass(legacy_window_, SubclassProc, 1,
  268. reinterpret_cast<DWORD_PTR>(this));
  269. if (!mouse_hook_) {
  270. mouse_hook_ = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
  271. }
  272. } else if (!forward && forwarding_mouse_messages_) {
  273. forwarding_mouse_messages_ = false;
  274. forwarding_windows_.erase(this);
  275. RemoveWindowSubclass(legacy_window_, SubclassProc, 1);
  276. if (forwarding_windows_.size() == 0) {
  277. UnhookWindowsHookEx(mouse_hook_);
  278. mouse_hook_ = NULL;
  279. }
  280. }
  281. }
  282. LRESULT CALLBACK NativeWindowViews::SubclassProc(HWND hwnd,
  283. UINT msg,
  284. WPARAM w_param,
  285. LPARAM l_param,
  286. UINT_PTR subclass_id,
  287. DWORD_PTR ref_data) {
  288. NativeWindowViews* window = reinterpret_cast<NativeWindowViews*>(ref_data);
  289. switch (msg) {
  290. case WM_MOUSELEAVE: {
  291. // When input is forwarded to underlying windows, this message is posted.
  292. // If not handled, it interferes with Chromium logic, causing for example
  293. // mouseleave events to fire. If those events are used to exit forward
  294. // mode, excessive flickering on for example hover items in underlying
  295. // windows can occur due to rapidly entering and leaving forwarding mode.
  296. // By consuming and ignoring the message, we're essentially telling
  297. // Chromium that we have not left the window despite somebody else getting
  298. // the messages. As to why this is catched for the legacy window and not
  299. // the actual browser window is simply that the legacy window somehow
  300. // makes use of these events; posting to the main window didn't work.
  301. if (window->forwarding_mouse_messages_) {
  302. return 0;
  303. }
  304. break;
  305. }
  306. }
  307. return DefSubclassProc(hwnd, msg, w_param, l_param);
  308. }
  309. LRESULT CALLBACK NativeWindowViews::MouseHookProc(int n_code,
  310. WPARAM w_param,
  311. LPARAM l_param) {
  312. if (n_code < 0) {
  313. return CallNextHookEx(NULL, n_code, w_param, l_param);
  314. }
  315. // Post a WM_MOUSEMOVE message for those windows whose client area contains
  316. // the cursor since they are in a state where they would otherwise ignore all
  317. // mouse input.
  318. if (w_param == WM_MOUSEMOVE) {
  319. for (auto window : forwarding_windows_) {
  320. // At first I considered enumerating windows to check whether the cursor
  321. // was directly above the window, but since nothing bad seems to happen
  322. // if we post the message even if some other window occludes it I have
  323. // just left it as is.
  324. RECT client_rect;
  325. GetClientRect(window->legacy_window_, &client_rect);
  326. POINT p = reinterpret_cast<MSLLHOOKSTRUCT*>(l_param)->pt;
  327. ScreenToClient(window->legacy_window_, &p);
  328. if (PtInRect(&client_rect, p)) {
  329. WPARAM w = 0; // No virtual keys pressed for our purposes
  330. LPARAM l = MAKELPARAM(p.x, p.y);
  331. PostMessage(window->legacy_window_, WM_MOUSEMOVE, w, l);
  332. }
  333. }
  334. }
  335. return CallNextHookEx(NULL, n_code, w_param, l_param);
  336. }
  337. } // namespace atom