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/panel_overflow_strip.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/ui/panels/panel_manager.h" |
| 9 #include "chrome/browser/ui/panels/panel_mouse_watcher.h" |
| 10 #include "chrome/browser/ui/panels/panel_strip.h" |
| 11 #include "ui/base/animation/slide_animation.h" |
| 12 |
| 13 namespace { |
| 14 // The width of the overflow area that is expanded to show more info, i.e. |
| 15 // titles, when the mouse hovers over the area. |
| 16 const int kOverflowAreaHoverWidth = 200; |
| 17 |
| 18 // Maximium number of overflow panels allowed to be shown. |
| 19 const size_t kMaxVisibleOverflowPanelsAllowed = 6; |
| 20 |
| 21 // This value is experimental and subjective. |
| 22 const int kOverflowHoverAnimationMs = 180; |
| 23 } |
| 24 |
| 25 PanelOverflowStrip::PanelOverflowStrip(PanelManager* panel_manager) |
| 26 : panel_manager_(panel_manager), |
| 27 are_overflow_titles_shown_(false), |
| 28 overflow_hover_animator_start_width_(0), |
| 29 overflow_hover_animator_end_width_(0) { |
| 30 } |
| 31 |
| 32 PanelOverflowStrip::~PanelOverflowStrip() { |
| 33 DCHECK(panels_.empty()); |
| 34 } |
| 35 |
| 36 void PanelOverflowStrip::SetDisplayArea(const gfx::Rect& display_area) { |
| 37 if (display_area_ == display_area) |
| 38 return; |
| 39 |
| 40 display_area_ = display_area; |
| 41 Refresh(); |
| 42 } |
| 43 |
| 44 void PanelOverflowStrip::AddPanel(Panel* panel, bool is_new) { |
| 45 // TODO(jianli): consider using other container to improve the perf for |
| 46 // inserting to the front. http://crbug.com/106222 |
| 47 if (is_new) |
| 48 panels_.push_back(panel); |
| 49 else |
| 50 panels_.insert(panels_.begin(), panel); |
| 51 |
| 52 if (panels_.size() == 1) |
| 53 panel_manager_->mouse_watcher()->AddObserver(this); |
| 54 |
| 55 panel->SetExpansionState(Panel::IN_OVERFLOW); |
| 56 |
| 57 if (is_new) { |
| 58 // When the overflow panel is added to the back, only need to refresh |
| 59 // itself. |
| 60 DoRefresh(panels_.size() - 1, panels_.size() - 1); |
| 61 } else { |
| 62 // When the overflow panel is added to the front, refresh all. |
| 63 Refresh(); |
| 64 } |
| 65 } |
| 66 |
| 67 bool PanelOverflowStrip::Remove(Panel* panel) { |
| 68 size_t index = 0; |
| 69 for (Panels::iterator iter = panels_.begin(); iter != panels_.end(); |
| 70 ++iter, ++index) { |
| 71 if (*iter == panel) { |
| 72 panels_.erase(iter); |
| 73 DoRefresh(index, panels_.size() - 1); |
| 74 panel_manager_->OnPanelRemoved(panel); |
| 75 if (panels_.empty()) |
| 76 panel_manager_->mouse_watcher()->RemoveObserver(this); |
| 77 return true; |
| 78 } |
| 79 } |
| 80 return false; |
| 81 } |
| 82 |
| 83 void PanelOverflowStrip::RemoveAll() { |
| 84 // Make a copy of the iterator as closing panels can modify the vector. |
| 85 Panels panels_copy = panels_; |
| 86 |
| 87 // Start from the bottom to avoid reshuffling. |
| 88 for (Panels::reverse_iterator iter = panels_copy.rbegin(); |
| 89 iter != panels_copy.rend(); ++iter) |
| 90 (*iter)->Close(); |
| 91 } |
| 92 |
| 93 void PanelOverflowStrip::OnPanelExpansionStateChanged( |
| 94 Panel* panel, Panel::ExpansionState old_state) { |
| 95 DCHECK(panel->expansion_state() == Panel::IN_OVERFLOW); |
| 96 } |
| 97 |
| 98 void PanelOverflowStrip::Refresh() { |
| 99 if (panels_.empty()) |
| 100 return; |
| 101 DoRefresh(0, panels_.size() - 1); |
| 102 } |
| 103 |
| 104 void PanelOverflowStrip::DoRefresh(size_t start_index, size_t end_index) { |
| 105 if (panels_.empty()) |
| 106 return; |
| 107 |
| 108 DCHECK(start_index < panels_.size()); |
| 109 DCHECK(end_index < panels_.size()); |
| 110 |
| 111 for (size_t index = start_index; index <= end_index; ++index) { |
| 112 Panel* panel = panels_[index]; |
| 113 gfx::Rect new_bounds = ComputeLayout(index, |
| 114 panel->IconOnlySize()); |
| 115 DCHECK(!new_bounds.IsEmpty()); |
| 116 panel->SetPanelBounds(new_bounds); |
| 117 } |
| 118 } |
| 119 |
| 120 gfx::Rect PanelOverflowStrip::ComputeLayout( |
| 121 size_t index, const gfx::Size& iconified_size) const { |
| 122 DCHECK(index != kInvalidPanelIndex); |
| 123 |
| 124 gfx::Rect bounds; |
| 125 int bottom = (index == 0) ? display_area_.bottom() : |
| 126 panels_[index - 1]->GetBounds().y(); |
| 127 bounds.set_x(display_area_.x()); |
| 128 bounds.set_y(bottom - iconified_size.height()); |
| 129 |
| 130 if (are_overflow_titles_shown_) { |
| 131 // Both icon and title are visible when the mouse hovers over the overflow |
| 132 // area. |
| 133 bounds.set_width(kOverflowAreaHoverWidth); |
| 134 bounds.set_height(iconified_size.height()); |
| 135 } else if (index < kMaxVisibleOverflowPanelsAllowed) { |
| 136 // Only the icon is visible. |
| 137 bounds.set_width(iconified_size.width()); |
| 138 bounds.set_height(iconified_size.height()); |
| 139 } else { |
| 140 // Invisible for overflow-on-overflow. |
| 141 bounds.set_width(0); |
| 142 bounds.set_height(0); |
| 143 } |
| 144 |
| 145 return bounds; |
| 146 } |
| 147 |
| 148 void PanelOverflowStrip::OnMouseMove(const gfx::Point& mouse_position) { |
| 149 bool show_overflow_titles = ShouldShowOverflowTitles(mouse_position); |
| 150 ShowOverflowTitles(show_overflow_titles); |
| 151 } |
| 152 |
| 153 bool PanelOverflowStrip::ShouldShowOverflowTitles( |
| 154 const gfx::Point& mouse_position) const { |
| 155 if (panels_.empty()) |
| 156 return false; |
| 157 |
| 158 int width = are_overflow_titles_shown_ ? kOverflowAreaHoverWidth |
| 159 : display_area_.width(); |
| 160 return display_area_.x() <= mouse_position.x() && |
| 161 mouse_position.x() <= display_area_.x() + width && |
| 162 panels_.back()->GetBounds().y() <= mouse_position.y() && |
| 163 mouse_position.y() <= display_area_.bottom(); |
| 164 } |
| 165 |
| 166 void PanelOverflowStrip::ShowOverflowTitles(bool show_overflow_titles) { |
| 167 if (show_overflow_titles == are_overflow_titles_shown_) |
| 168 return; |
| 169 are_overflow_titles_shown_ = show_overflow_titles; |
| 170 |
| 171 if (show_overflow_titles) { |
| 172 overflow_hover_animator_start_width_ = display_area_.width(); |
| 173 overflow_hover_animator_end_width_ = kOverflowAreaHoverWidth; |
| 174 |
| 175 // We need to bring all overflow panels to the top of z-order since the |
| 176 // active panel might obscure the expanded overflow panels. |
| 177 for (Panels::iterator iter = panels_.begin(); |
| 178 iter != panels_.end(); ++iter) { |
| 179 (*iter)->EnsureFullyVisible(); |
| 180 } |
| 181 } else { |
| 182 overflow_hover_animator_start_width_ = kOverflowAreaHoverWidth; |
| 183 overflow_hover_animator_end_width_ = display_area_.width(); |
| 184 } |
| 185 |
| 186 if (!overflow_hover_animator_.get()) |
| 187 overflow_hover_animator_.reset(new ui::SlideAnimation(this)); |
| 188 if (overflow_hover_animator_->IsShowing()) |
| 189 overflow_hover_animator_->Reset(); |
| 190 overflow_hover_animator_->SetSlideDuration(kOverflowHoverAnimationMs); |
| 191 |
| 192 overflow_hover_animator_->Show(); |
| 193 } |
| 194 |
| 195 void PanelOverflowStrip::AnimationProgressed(const ui::Animation* animation) { |
| 196 int current_width = overflow_hover_animator_->CurrentValueBetween( |
| 197 overflow_hover_animator_start_width_, overflow_hover_animator_end_width_); |
| 198 bool end_of_shrinking = current_width == display_area_.width(); |
| 199 |
| 200 // Update each overflow panel. |
| 201 for (size_t i = 0; i < panels_.size(); ++i) { |
| 202 Panel* overflow_panel = panels_[i]; |
| 203 gfx::Rect bounds = overflow_panel->GetBounds(); |
| 204 |
| 205 if (i >= kMaxVisibleOverflowPanelsAllowed && end_of_shrinking) { |
| 206 bounds.set_width(0); |
| 207 bounds.set_height(0); |
| 208 } else { |
| 209 bounds.set_width(current_width); |
| 210 bounds.set_height(overflow_panel->IconOnlySize().height()); |
| 211 } |
| 212 |
| 213 overflow_panel->SetPanelBoundsInstantly(bounds); |
| 214 } |
| 215 } |
| 216 |
| 217 void PanelOverflowStrip::OnFullScreenModeChanged(bool is_full_screen) { |
| 218 for (size_t i = 0; i < panels_.size(); ++i) |
| 219 panels_[i]->FullScreenModeChanged(is_full_screen); |
| 220 } |
OLD | NEW |