OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 file. |
| 4 |
| 5 #include "chrome/browser/ui/panels/auto_hiding_desktop_bar_win.h" |
| 6 |
| 7 #include <shellapi.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "views/widget/monitor_win.h" |
| 11 |
| 12 namespace { |
| 13 // The thickness of the area of an auto-hiding taskbar that is still visible |
| 14 // when the taskbar becomes hidden. |
| 15 const int kHiddenAutoHideTaskbarThickness = 2; |
| 16 |
| 17 // The polling interval to check auto-hiding taskbars. |
| 18 const int kCheckTaskbarPollingIntervalMs = 500; |
| 19 } // namespace |
| 20 |
| 21 AutoHidingDesktopBarWin::AutoHidingDesktopBarWin(Observer* observer) |
| 22 : observer_(observer) { |
| 23 DCHECK(observer); |
| 24 memset(taskbars_, 0, sizeof(taskbars_)); |
| 25 } |
| 26 |
| 27 AutoHidingDesktopBarWin::~AutoHidingDesktopBarWin() { |
| 28 } |
| 29 |
| 30 void AutoHidingDesktopBarWin::UpdateWorkArea(const gfx::Rect& work_area) { |
| 31 if (work_area_ == work_area) |
| 32 return; |
| 33 work_area_ = work_area; |
| 34 |
| 35 RECT rect = work_area_.ToRECT(); |
| 36 monitor_ = ::MonitorFromRect(&rect, MONITOR_DEFAULTTOPRIMARY); |
| 37 DCHECK(monitor_); |
| 38 |
| 39 bool taskbar_exists = CheckTaskbars(false); |
| 40 |
| 41 // If no auto-hiding taskbar exists, we do not need to start the polling |
| 42 // timer. If a taskbar is then set to auto-hiding, UpdateWorkArea will be |
| 43 // called due to the work area change. |
| 44 if (taskbar_exists) { |
| 45 if (!polling_timer_.IsRunning()) { |
| 46 polling_timer_.Start( |
| 47 base::TimeDelta::FromMilliseconds(kCheckTaskbarPollingIntervalMs), |
| 48 this, |
| 49 &AutoHidingDesktopBarWin::OnPollingTimer); |
| 50 } |
| 51 } else { |
| 52 if (polling_timer_.IsRunning()) |
| 53 polling_timer_.Stop(); |
| 54 } |
| 55 } |
| 56 |
| 57 bool AutoHidingDesktopBarWin::IsEnabled( |
| 58 AutoHidingDesktopBar::Alignment alignment) { |
| 59 CheckTaskbars(false); |
| 60 return taskbars_[static_cast<int>(alignment)].window != NULL; |
| 61 } |
| 62 |
| 63 int AutoHidingDesktopBarWin::GetThickness( |
| 64 AutoHidingDesktopBar::Alignment alignment) const { |
| 65 return GetThicknessFromBounds(alignment, GetBounds(alignment)); |
| 66 } |
| 67 |
| 68 AutoHidingDesktopBar::Visibility AutoHidingDesktopBarWin::GetVisibility( |
| 69 AutoHidingDesktopBar::Alignment alignment) const { |
| 70 return GetVisibilityFromBounds(alignment, GetBounds(alignment)); |
| 71 } |
| 72 |
| 73 gfx::Rect AutoHidingDesktopBarWin::GetBounds( |
| 74 AutoHidingDesktopBar::Alignment alignment) const { |
| 75 HWND taskbar_window = taskbars_[static_cast<int>(alignment)].window; |
| 76 if (!taskbar_window) |
| 77 return gfx::Rect(); |
| 78 |
| 79 RECT rect; |
| 80 if (!::GetWindowRect(taskbar_window, &rect)) |
| 81 return gfx::Rect(); |
| 82 return gfx::Rect(rect); |
| 83 } |
| 84 |
| 85 int AutoHidingDesktopBarWin::GetThicknessFromBounds( |
| 86 AutoHidingDesktopBar::Alignment alignment, |
| 87 const gfx::Rect& taskbar_bounds) const { |
| 88 switch (alignment) { |
| 89 case AutoHidingDesktopBar::ALIGN_BOTTOM: |
| 90 return taskbar_bounds.height(); |
| 91 case AutoHidingDesktopBar::ALIGN_LEFT: |
| 92 case AutoHidingDesktopBar::ALIGN_RIGHT: |
| 93 return taskbar_bounds.width(); |
| 94 default: |
| 95 NOTREACHED(); |
| 96 return 0; |
| 97 } |
| 98 } |
| 99 |
| 100 AutoHidingDesktopBar::Visibility |
| 101 AutoHidingDesktopBarWin::GetVisibilityFromBounds( |
| 102 AutoHidingDesktopBar::Alignment alignment, |
| 103 const gfx::Rect& taskbar_bounds) const { |
| 104 switch (alignment) { |
| 105 case AutoHidingDesktopBar::ALIGN_BOTTOM: |
| 106 if (taskbar_bounds.bottom() <= work_area_.bottom()) |
| 107 return VISIBLE; |
| 108 else if (taskbar_bounds.y() >= |
| 109 work_area_.bottom() - kHiddenAutoHideTaskbarThickness) |
| 110 return HIDDEN; |
| 111 else |
| 112 return ANIMATING; |
| 113 |
| 114 case AutoHidingDesktopBar::ALIGN_LEFT: |
| 115 if (taskbar_bounds.x() >= work_area_.x()) |
| 116 return VISIBLE; |
| 117 else if (taskbar_bounds.right() <= |
| 118 work_area_.x() + kHiddenAutoHideTaskbarThickness) |
| 119 return HIDDEN; |
| 120 else |
| 121 return ANIMATING; |
| 122 |
| 123 case AutoHidingDesktopBar::ALIGN_RIGHT: |
| 124 if (taskbar_bounds.right() <= work_area_.right()) |
| 125 return VISIBLE; |
| 126 else if (taskbar_bounds.x() >= |
| 127 work_area_.right() - kHiddenAutoHideTaskbarThickness) |
| 128 return HIDDEN; |
| 129 else |
| 130 return ANIMATING; |
| 131 |
| 132 default: |
| 133 NOTREACHED(); |
| 134 return VISIBLE; |
| 135 } |
| 136 } |
| 137 |
| 138 void AutoHidingDesktopBarWin::OnPollingTimer() { |
| 139 CheckTaskbars(true); |
| 140 } |
| 141 |
| 142 bool AutoHidingDesktopBarWin::CheckTaskbars(bool notify_observer) { |
| 143 bool taskbar_exists = false; |
| 144 UINT edges[] = { ABE_BOTTOM, ABE_LEFT, ABE_RIGHT }; |
| 145 for (size_t i = 0; i < kMaxTaskbars; ++i) { |
| 146 taskbars_[i].window = |
| 147 views::GetTopmostAutoHideTaskbarForEdge(edges[i], monitor_); |
| 148 if (taskbars_[i].window) |
| 149 taskbar_exists = true; |
| 150 } |
| 151 if (!taskbar_exists) { |
| 152 for (size_t i = 0; i < kMaxTaskbars; ++i) { |
| 153 taskbars_[i].thickness = 0; |
| 154 taskbars_[i].visibility = AutoHidingDesktopBar::HIDDEN; |
| 155 } |
| 156 return false; |
| 157 } |
| 158 |
| 159 bool thickness_changed = false; |
| 160 for (size_t i = 0; i < kMaxTaskbars; ++i) { |
| 161 AutoHidingDesktopBar::Alignment alignment = static_cast<Alignment>(i); |
| 162 |
| 163 gfx::Rect bounds = GetBounds(alignment); |
| 164 |
| 165 // Check the thickness change. |
| 166 int thickness = GetThicknessFromBounds(alignment, bounds); |
| 167 if (thickness != taskbars_[i].thickness) { |
| 168 taskbars_[i].thickness = thickness; |
| 169 thickness_changed = true; |
| 170 } |
| 171 |
| 172 // Check and notify the visibility change. |
| 173 AutoHidingDesktopBar::Visibility visibility = |
| 174 GetVisibilityFromBounds(alignment, bounds); |
| 175 if (visibility != taskbars_[i].visibility) { |
| 176 taskbars_[i].visibility = visibility; |
| 177 if (notify_observer) { |
| 178 observer_->OnAutoHidingDesktopBarVisibilityChanged(alignment, |
| 179 visibility); |
| 180 } |
| 181 } |
| 182 } |
| 183 |
| 184 // Notify the thickness change if needed. |
| 185 if (thickness_changed && notify_observer) |
| 186 observer_->OnAutoHidingDesktopBarThicknessChanged(); |
| 187 |
| 188 return true; |
| 189 } |
| 190 |
| 191 // static |
| 192 AutoHidingDesktopBar* AutoHidingDesktopBar::Create(Observer* observer) { |
| 193 return new AutoHidingDesktopBarWin(observer); |
| 194 } |
OLD | NEW |