electron_views_delegate_win.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (c) 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE-CHROMIUM file.
  4. #include "shell/browser/ui/views/electron_views_delegate.h"
  5. #include <dwmapi.h>
  6. #include <shellapi.h>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/task/thread_pool.h"
  10. namespace {
  11. bool MonitorHasAutohideTaskbarForEdge(UINT edge, HMONITOR monitor) {
  12. APPBARDATA taskbar_data = {sizeof(APPBARDATA), NULL, 0, edge};
  13. taskbar_data.hWnd = ::GetForegroundWindow();
  14. // MSDN documents an ABM_GETAUTOHIDEBAREX, which supposedly takes a monitor
  15. // rect and returns autohide bars on that monitor. This sounds like a good
  16. // idea for multi-monitor systems. Unfortunately, it appears to not work at
  17. // least some of the time (erroneously returning NULL) and there's almost no
  18. // online documentation or other sample code using it that suggests ways to
  19. // address this problem. We do the following:-
  20. // 1. Use the ABM_GETAUTOHIDEBAR message. If it works, i.e. returns a valid
  21. // window we are done.
  22. // 2. If the ABM_GETAUTOHIDEBAR message does not work we query the auto hide
  23. // state of the taskbar and then retrieve its position. That call returns
  24. // the edge on which the taskbar is present. If it matches the edge we
  25. // are looking for, we are done.
  26. // NOTE: This call spins a nested run loop.
  27. HWND taskbar = reinterpret_cast<HWND>(
  28. SHAppBarMessage(ABM_GETAUTOHIDEBAR, &taskbar_data));
  29. if (!::IsWindow(taskbar)) {
  30. APPBARDATA taskbar_data = {sizeof(APPBARDATA), 0, 0, 0};
  31. unsigned int taskbar_state = SHAppBarMessage(ABM_GETSTATE, &taskbar_data);
  32. if (!(taskbar_state & ABS_AUTOHIDE))
  33. return false;
  34. taskbar_data.hWnd = ::FindWindow(L"Shell_TrayWnd", NULL);
  35. if (!::IsWindow(taskbar_data.hWnd))
  36. return false;
  37. SHAppBarMessage(ABM_GETTASKBARPOS, &taskbar_data);
  38. if (taskbar_data.uEdge == edge)
  39. taskbar = taskbar_data.hWnd;
  40. }
  41. // There is a potential race condition here:
  42. // 1. A maximized chrome window is fullscreened.
  43. // 2. It is switched back to maximized.
  44. // 3. In the process the window gets a WM_NCCALCSIZE message which calls us to
  45. // get the autohide state.
  46. // 4. The worker thread is invoked. It calls the API to get the autohide
  47. // state. On Windows versions earlier than Windows 7, taskbars could
  48. // easily be always on top or not.
  49. // This meant that we only want to look for taskbars which have the topmost
  50. // bit set. However this causes problems in cases where the window on the
  51. // main thread is still in the process of switching away from fullscreen.
  52. // In this case the taskbar might not yet have the topmost bit set.
  53. // 5. The main thread resumes and does not leave space for the taskbar and
  54. // hence it does not pop when hovered.
  55. //
  56. // To address point 4 above, it is best to not check for the WS_EX_TOPMOST
  57. // window style on the taskbar, as starting from Windows 7, the topmost
  58. // style is always set. We don't support XP and Vista anymore.
  59. if (::IsWindow(taskbar)) {
  60. if (MonitorFromWindow(taskbar, MONITOR_DEFAULTTONEAREST) == monitor)
  61. return true;
  62. // In some cases like when the autohide taskbar is on the left of the
  63. // secondary monitor, the MonitorFromWindow call above fails to return the
  64. // correct monitor the taskbar is on. We fallback to MonitorFromPoint for
  65. // the cursor position in that case, which seems to work well.
  66. POINT cursor_pos = {0};
  67. GetCursorPos(&cursor_pos);
  68. if (MonitorFromPoint(cursor_pos, MONITOR_DEFAULTTONEAREST) == monitor)
  69. return true;
  70. }
  71. return false;
  72. }
  73. int GetAppbarAutohideEdgesOnWorkerThread(HMONITOR monitor) {
  74. DCHECK(monitor);
  75. int edges = 0;
  76. if (MonitorHasAutohideTaskbarForEdge(ABE_LEFT, monitor))
  77. edges |= views::ViewsDelegate::EDGE_LEFT;
  78. if (MonitorHasAutohideTaskbarForEdge(ABE_TOP, monitor))
  79. edges |= views::ViewsDelegate::EDGE_TOP;
  80. if (MonitorHasAutohideTaskbarForEdge(ABE_RIGHT, monitor))
  81. edges |= views::ViewsDelegate::EDGE_RIGHT;
  82. if (MonitorHasAutohideTaskbarForEdge(ABE_BOTTOM, monitor))
  83. edges |= views::ViewsDelegate::EDGE_BOTTOM;
  84. return edges;
  85. }
  86. } // namespace
  87. namespace electron {
  88. HICON ViewsDelegate::GetDefaultWindowIcon() const {
  89. // Use current exe's icon as default window icon.
  90. return LoadIcon(GetModuleHandle(NULL),
  91. MAKEINTRESOURCE(1 /* IDR_MAINFRAME */));
  92. }
  93. HICON ViewsDelegate::GetSmallWindowIcon() const {
  94. return GetDefaultWindowIcon();
  95. }
  96. bool ViewsDelegate::IsWindowInMetro(gfx::NativeWindow window) const {
  97. return false;
  98. }
  99. int ViewsDelegate::GetAppbarAutohideEdges(HMONITOR monitor,
  100. base::OnceClosure callback) {
  101. // Initialize the map with EDGE_BOTTOM. This is important, as if we return an
  102. // initial value of 0 (no auto-hide edges) then we'll go fullscreen and
  103. // windows will automatically remove WS_EX_TOPMOST from the appbar resulting
  104. // in us thinking there is no auto-hide edges. By returning at least one edge
  105. // we don't initially go fullscreen until we figure out the real auto-hide
  106. // edges.
  107. if (!appbar_autohide_edge_map_.count(monitor))
  108. appbar_autohide_edge_map_[monitor] = EDGE_BOTTOM;
  109. // We use the SHAppBarMessage API to get the taskbar autohide state. This API
  110. // spins a modal loop which could cause callers to be reentered. To avoid
  111. // that we retrieve the taskbar state in a worker thread.
  112. if (monitor && !in_autohide_edges_callback_) {
  113. // TODO(robliao): Annotate this task with .WithCOM() once supported.
  114. // https://crbug.com/662122
  115. base::ThreadPool::PostTaskAndReplyWithResult(
  116. FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
  117. base::BindOnce(&GetAppbarAutohideEdgesOnWorkerThread, monitor),
  118. base::BindOnce(&ViewsDelegate::OnGotAppbarAutohideEdges,
  119. weak_factory_.GetWeakPtr(), std::move(callback), monitor,
  120. appbar_autohide_edge_map_[monitor]));
  121. }
  122. return appbar_autohide_edge_map_[monitor];
  123. }
  124. void ViewsDelegate::OnGotAppbarAutohideEdges(base::OnceClosure callback,
  125. HMONITOR monitor,
  126. int returned_edges,
  127. int edges) {
  128. appbar_autohide_edge_map_[monitor] = edges;
  129. if (returned_edges == edges)
  130. return;
  131. base::AutoReset<bool> in_callback_setter(&in_autohide_edges_callback_, true);
  132. std::move(callback).Run();
  133. }
  134. } // namespace electron