OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/views/sidebar/sidebar_base_tab_strip.h" |
| 6 |
| 7 #include "chrome/browser/ui/view_ids.h" |
| 8 #include "chrome/browser/ui/views/sidebar/sidebar_base_tab.h" |
| 9 #include "chrome/browser/ui/views/sidebar/sidebar_tab_strip_controller.h" |
| 10 #include "views/window/window.h" |
| 11 |
| 12 SidebarBaseTabStrip::SidebarBaseTabStrip(SidebarTabStripController* controller) |
| 13 : controller_(controller), |
| 14 ALLOW_THIS_IN_INITIALIZER_LIST(bounds_animator_(this)) { |
| 15 DCHECK(controller_.get()); |
| 16 bounds_animator_.set_observer(this); |
| 17 SetID(VIEW_ID_SIDEBAR_TAB_STRIP); |
| 18 } |
| 19 |
| 20 SidebarBaseTabStrip::~SidebarBaseTabStrip() { |
| 21 } |
| 22 |
| 23 void SidebarBaseTabStrip::OnBoundsChanged() { |
| 24 View::OnBoundsChanged(); |
| 25 controller_->TabStripBoundsChanged(IsAnimating()); |
| 26 } |
| 27 |
| 28 void SidebarBaseTabStrip::SelectTab(SidebarBaseTab* tab) { |
| 29 int model_index = GetModelIndexOfBaseTab(tab); |
| 30 controller_->SelectTab(model_index); |
| 31 } |
| 32 |
| 33 bool SidebarBaseTabStrip::IsTabSelected(const SidebarBaseTab* tab) { |
| 34 if (tab->closing()) |
| 35 return false; |
| 36 int model_index = GetModelIndexOfBaseTab(tab); |
| 37 return controller_->IsTabSelected(model_index); |
| 38 } |
| 39 |
| 40 bool SidebarBaseTabStrip::IsSidebarExpanded() { |
| 41 return controller_->IsSidebarExpanded(); |
| 42 } |
| 43 |
| 44 bool SidebarBaseTabStrip::IsAnimating() const { |
| 45 return bounds_animator_.IsAnimating(); |
| 46 } |
| 47 |
| 48 void SidebarBaseTabStrip::AddTabAt(int model_index, |
| 49 bool animate, |
| 50 const SidebarTabRendererData& data) { |
| 51 SidebarBaseTab* tab = CreateTab(); |
| 52 tab->SetData(data); |
| 53 |
| 54 TabData d = { tab, gfx::Rect() }; |
| 55 tab_data_.insert(tab_data_.begin() + ModelIndexToTabIndex(model_index), d); |
| 56 |
| 57 AddChildView(tab); |
| 58 |
| 59 tab->Layout(); |
| 60 |
| 61 if (animate) { |
| 62 // Don't animate the first tab, it looks weird, and don't animate anything |
| 63 // if the containing window isn't visible yet. |
| 64 if (tab_count() > 1 && GetWindow() && GetWindow()->IsVisible()) |
| 65 StartInsertTabAnimation(model_index); |
| 66 else |
| 67 SetToIdealBounds(); |
| 68 } |
| 69 } |
| 70 |
| 71 void SidebarBaseTabStrip::RemoveTabAt(int model_index, bool animate) { |
| 72 if (animate) { |
| 73 if (tab_count() > 1) { |
| 74 // Have to do it before calling set_closing(), otherwise model_index |
| 75 // to tab_data_index mapping will miss this tab. |
| 76 StartRemoveTabAnimation(model_index); |
| 77 } else { |
| 78 // Do not animate the last tab removal, do it right away. |
| 79 GetBaseTabAtModelIndex(model_index)->set_closing(true); |
| 80 SetToIdealBounds(); |
| 81 } |
| 82 } else { |
| 83 GetBaseTabAtModelIndex(model_index)->set_closing(true); |
| 84 } |
| 85 } |
| 86 |
| 87 void SidebarBaseTabStrip::SelectTabAt(int new_model_index) { |
| 88 SchedulePaint(); |
| 89 } |
| 90 |
| 91 void SidebarBaseTabStrip::SetTabData(int model_index, |
| 92 const SidebarTabRendererData& data) { |
| 93 SidebarBaseTab* tab = GetBaseTabAtModelIndex(model_index); |
| 94 tab->SetData(data); |
| 95 tab->SchedulePaint(); |
| 96 } |
| 97 |
| 98 SidebarBaseTab* SidebarBaseTabStrip::GetBaseTabAtModelIndex( |
| 99 int model_index) const { |
| 100 return base_tab_at_tab_index(ModelIndexToTabIndex(model_index)); |
| 101 } |
| 102 |
| 103 int SidebarBaseTabStrip::GetModelIndexOfBaseTab( |
| 104 const SidebarBaseTab* tab) const { |
| 105 for (int i = 0, model_index = 0; i < tab_count(); ++i) { |
| 106 SidebarBaseTab* current_tab = base_tab_at_tab_index(i); |
| 107 if (!current_tab->closing()) { |
| 108 if (current_tab == tab) |
| 109 return model_index; |
| 110 ++model_index; |
| 111 } |
| 112 } |
| 113 return -1; |
| 114 } |
| 115 |
| 116 int SidebarBaseTabStrip::ModelIndexToTabIndex(int model_index) const { |
| 117 int current_model_index = 0; |
| 118 for (int i = 0; i < tab_count(); ++i) { |
| 119 if (!base_tab_at_tab_index(i)->closing()) { |
| 120 if (current_model_index == model_index) |
| 121 return i; |
| 122 ++current_model_index; |
| 123 } |
| 124 } |
| 125 return static_cast<int>(tab_data_.size()); |
| 126 } |
| 127 |
| 128 void SidebarBaseTabStrip::SetToIdealBounds() { |
| 129 StopAnimating(); |
| 130 |
| 131 RemoveClosingTabs(); |
| 132 |
| 133 GenerateIdealBounds(); |
| 134 |
| 135 // Move all tabs to their planned positions. |
| 136 for (int i = 0; i < tab_count(); ++i) |
| 137 base_tab_at_tab_index(i)->SetBoundsRect(ideal_bounds(i)); |
| 138 |
| 139 UpdateViewBounds(); |
| 140 |
| 141 SchedulePaint(); |
| 142 } |
| 143 |
| 144 void SidebarBaseTabStrip::AnimateToIdealBounds() { |
| 145 for (int i = 0; i < tab_count(); ++i) { |
| 146 SidebarBaseTab* tab = base_tab_at_tab_index(i); |
| 147 if (tab->bounds() != ideal_bounds(i)) |
| 148 bounds_animator_.AnimateViewTo(tab, ideal_bounds(i)); |
| 149 } |
| 150 } |
| 151 |
| 152 void SidebarBaseTabStrip::StopAnimating() { |
| 153 if (!IsAnimating()) |
| 154 return; |
| 155 |
| 156 bounds_animator_.Cancel(); |
| 157 |
| 158 DCHECK(!IsAnimating()); |
| 159 } |
| 160 |
| 161 void SidebarBaseTabStrip::UpdateViewBounds() { |
| 162 int width = 0; |
| 163 int height = 0; |
| 164 for (int i = 0; i < tab_count(); ++i) { |
| 165 const gfx::Rect& bounds = base_tab_at_tab_index(i)->bounds(); |
| 166 width = std::max(width, bounds.x() + bounds.width()); |
| 167 height = std::max(height, bounds.y() + bounds.height()); |
| 168 } |
| 169 SetBounds(x(), y(), width, height); |
| 170 } |
| 171 |
| 172 void SidebarBaseTabStrip::OnBoundsAnimatorProgressed( |
| 173 views::BoundsAnimator* animator) { |
| 174 UpdateViewBounds(); |
| 175 } |
| 176 |
| 177 void SidebarBaseTabStrip::OnBoundsAnimatorDone( |
| 178 views::BoundsAnimator* animator) { |
| 179 RemoveClosingTabs(); |
| 180 } |
| 181 |
| 182 void SidebarBaseTabStrip::RemoveAndDeleteTab(int tab_data_index) { |
| 183 SidebarBaseTab* tab = base_tab_at_tab_index(tab_data_index); |
| 184 |
| 185 // Remove the tab from the SidebarTabStrip's list. |
| 186 tab_data_.erase(tab_data_.begin() + tab_data_index); |
| 187 |
| 188 delete tab; |
| 189 } |
| 190 |
| 191 void SidebarBaseTabStrip::RemoveClosingTabs() { |
| 192 for (int i = tab_count() - 1; i >= 0; --i) { |
| 193 SidebarBaseTab* tab = base_tab_at_tab_index(i); |
| 194 if (tab->closing()) |
| 195 RemoveAndDeleteTab(i); |
| 196 } |
| 197 } |
| 198 |
OLD | NEW |