electron_views_delegate_win.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/functional/bind.h"
  9. #include "base/task/thread_pool.h"
  10. namespace {
  11. bool MonitorHasAutohideTaskbarForEdge(UINT edge, HMONITOR monitor) {
  12. APPBARDATA taskbar_data = {sizeof(APPBARDATA), nullptr, 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 nullptr) and there's almost
  18. // no 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 new_taskbar_data = {sizeof(APPBARDATA), 0, 0, 0};
  31. unsigned int taskbar_state =
  32. SHAppBarMessage(ABM_GETSTATE, &new_taskbar_data);
  33. if (!(taskbar_state & ABS_AUTOHIDE))
  34. return false;
  35. new_taskbar_data.hWnd = ::FindWindow(L"Shell_TrayWnd", nullptr);
  36. if (!::IsWindow(new_taskbar_data.hWnd))
  37. return false;
  38. SHAppBarMessage(ABM_GETTASKBARPOS, &new_taskbar_data);
  39. if (new_taskbar_data.uEdge == edge)
  40. taskbar = new_taskbar_data.hWnd;
  41. }
  42. // There is a potential race condition here:
  43. // 1. A maximized chrome window is fullscreened.
  44. // 2. It is switched back to maximized.
  45. // 3. In the process the window gets a WM_NCCALCSIZE message which calls us to
  46. // get the autohide state.
  47. // 4. The worker thread is invoked. It calls the API to get the autohide
  48. // state. On Windows versions earlier than Windows 7, taskbars could
  49. // easily be always on top or not.
  50. // This meant that we only want to look for taskbars which have the topmost
  51. // bit set. However this causes problems in cases where the window on the
  52. // main thread is still in the process of switching away from fullscreen.
  53. // In this case the taskbar might not yet have the topmost bit set.
  54. // 5. The main thread resumes and does not leave space for the taskbar and
  55. // hence it does not pop when hovered.
  56. //
  57. // To address point 4 above, it is best to not check for the WS_EX_TOPMOST
  58. // window style on the taskbar, as starting from Windows 7, the topmost
  59. // style is always set. We don't support XP and Vista anymore.
  60. if (::IsWindow(taskbar)) {
  61. if (MonitorFromWindow(taskbar, MONITOR_DEFAULTTONEAREST) == monitor)
  62. return true;
  63. // In some cases like when the autohide taskbar is on the left of the
  64. // secondary monitor, the MonitorFromWindow call above fails to return the
  65. // correct monitor the taskbar is on. We fallback to MonitorFromPoint for
  66. // the cursor position in that case, which seems to work well.
  67. POINT cursor_pos = {0};
  68. GetCursorPos(&cursor_pos);
  69. if (MonitorFromPoint(cursor_pos, MONITOR_DEFAULTTONEAREST) == monitor)
  70. return true;
  71. }
  72. return false;
  73. }
  74. int GetAppbarAutohideEdgesOnWorkerThread(HMONITOR monitor) {
  75. DCHECK(monitor);
  76. int edges = 0;
  77. if (MonitorHasAutohideTaskbarForEdge(ABE_LEFT, monitor))
  78. edges |= views::ViewsDelegate::EDGE_LEFT;
  79. if (MonitorHasAutohideTaskbarForEdge(ABE_TOP, monitor))
  80. edges |= views::ViewsDelegate::EDGE_TOP;
  81. if (MonitorHasAutohideTaskbarForEdge(ABE_RIGHT, monitor))
  82. edges |= views::ViewsDelegate::EDGE_RIGHT;
  83. if (MonitorHasAutohideTaskbarForEdge(ABE_BOTTOM, monitor))
  84. edges |= views::ViewsDelegate::EDGE_BOTTOM;
  85. return edges;
  86. }
  87. } // namespace
  88. namespace electron {
  89. HICON ViewsDelegate::GetDefaultWindowIcon() const {
  90. // Use current exe's icon as default window icon.
  91. return LoadIcon(GetModuleHandle(nullptr),
  92. MAKEINTRESOURCE(1 /* IDR_MAINFRAME */));
  93. }
  94. HICON ViewsDelegate::GetSmallWindowIcon() const {
  95. return GetDefaultWindowIcon();
  96. }
  97. int ViewsDelegate::GetAppbarAutohideEdges(HMONITOR monitor,
  98. base::OnceClosure callback) {
  99. // Initialize the map with EDGE_BOTTOM. This is important, as if we return an
  100. // initial value of 0 (no auto-hide edges) then we'll go fullscreen and
  101. // windows will automatically remove WS_EX_TOPMOST from the appbar resulting
  102. // in us thinking there is no auto-hide edges. By returning at least one edge
  103. // we don't initially go fullscreen until we figure out the real auto-hide
  104. // edges.
  105. if (!appbar_autohide_edge_map_.contains(monitor))
  106. appbar_autohide_edge_map_[monitor] = EDGE_BOTTOM;
  107. // We use the SHAppBarMessage API to get the taskbar autohide state. This API
  108. // spins a modal loop which could cause callers to be reentered. To avoid
  109. // that we retrieve the taskbar state in a worker thread.
  110. if (monitor && !in_autohide_edges_callback_) {
  111. // TODO(robliao): Annotate this task with .WithCOM() once supported.
  112. // https://crbug.com/662122
  113. base::ThreadPool::PostTaskAndReplyWithResult(
  114. FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
  115. base::BindOnce(&GetAppbarAutohideEdgesOnWorkerThread, monitor),
  116. base::BindOnce(&ViewsDelegate::OnGotAppbarAutohideEdges,
  117. weak_factory_.GetWeakPtr(), std::move(callback), monitor,
  118. appbar_autohide_edge_map_[monitor]));
  119. }
  120. return appbar_autohide_edge_map_[monitor];
  121. }
  122. void ViewsDelegate::OnGotAppbarAutohideEdges(base::OnceClosure callback,
  123. HMONITOR monitor,
  124. int returned_edges,
  125. int edges) {
  126. appbar_autohide_edge_map_[monitor] = edges;
  127. if (returned_edges == edges)
  128. return;
  129. base::AutoReset<bool> in_callback_setter(&in_autohide_edges_callback_, true);
  130. std::move(callback).Run();
  131. }
  132. } // namespace electron