| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" | 5 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "chrome/browser/extensions/extension_message_bubble_controller.h" | 11 #include "chrome/browser/extensions/extension_message_bubble_controller.h" |
| 12 #include "chrome/browser/extensions/tab_helper.h" | 12 #include "chrome/browser/extensions/tab_helper.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/layout_constants.h" | 15 #include "chrome/browser/ui/layout_constants.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" | 17 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" |
| 18 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | 18 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
| 19 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" | 19 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" |
| 20 #include "chrome/browser/ui/view_ids.h" | 20 #include "chrome/browser/ui/view_ids.h" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 // All views should be removed as part of ToolbarActionsBar::DeleteActions(). | 114 // All views should be removed as part of ToolbarActionsBar::DeleteActions(). |
| 115 DCHECK(toolbar_action_views_.empty()); | 115 DCHECK(toolbar_action_views_.empty()); |
| 116 } | 116 } |
| 117 | 117 |
| 118 std::string BrowserActionsContainer::GetIdAt(size_t index) const { | 118 std::string BrowserActionsContainer::GetIdAt(size_t index) const { |
| 119 return toolbar_action_views_[index]->view_controller()->GetId(); | 119 return toolbar_action_views_[index]->view_controller()->GetId(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 ToolbarActionView* BrowserActionsContainer::GetViewForId( | 122 ToolbarActionView* BrowserActionsContainer::GetViewForId( |
| 123 const std::string& id) { | 123 const std::string& id) { |
| 124 for (ToolbarActionView* view : toolbar_action_views_) { | 124 for (const auto& view : toolbar_action_views_) { |
| 125 if (view->view_controller()->GetId() == id) | 125 if (view->view_controller()->GetId() == id) |
| 126 return view; | 126 return view.get(); |
| 127 } | 127 } |
| 128 return nullptr; | 128 return nullptr; |
| 129 } | 129 } |
| 130 | 130 |
| 131 void BrowserActionsContainer::RefreshToolbarActionViews() { | 131 void BrowserActionsContainer::RefreshToolbarActionViews() { |
| 132 toolbar_actions_bar_->Update(); | 132 toolbar_actions_bar_->Update(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 size_t BrowserActionsContainer::VisibleBrowserActions() const { | 135 size_t BrowserActionsContainer::VisibleBrowserActions() const { |
| 136 size_t visible_actions = 0; | 136 size_t visible_actions = 0; |
| 137 for (const ToolbarActionView* view : toolbar_action_views_) { | 137 for (const auto& view : toolbar_action_views_) { |
| 138 if (view->visible()) | 138 if (view->visible()) |
| 139 ++visible_actions; | 139 ++visible_actions; |
| 140 } | 140 } |
| 141 return visible_actions; | 141 return visible_actions; |
| 142 } | 142 } |
| 143 | 143 |
| 144 size_t BrowserActionsContainer::VisibleBrowserActionsAfterAnimation() const { | 144 size_t BrowserActionsContainer::VisibleBrowserActionsAfterAnimation() const { |
| 145 if (!animating()) | 145 if (!animating()) |
| 146 return VisibleBrowserActions(); | 146 return VisibleBrowserActions(); |
| 147 | 147 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 158 | 158 |
| 159 views::MenuButton* BrowserActionsContainer::GetOverflowReferenceView() { | 159 views::MenuButton* BrowserActionsContainer::GetOverflowReferenceView() { |
| 160 return static_cast<views::MenuButton*>( | 160 return static_cast<views::MenuButton*>( |
| 161 GetToolbarView(browser_)->app_menu_button()); | 161 GetToolbarView(browser_)->app_menu_button()); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void BrowserActionsContainer::AddViewForAction( | 164 void BrowserActionsContainer::AddViewForAction( |
| 165 ToolbarActionViewController* view_controller, | 165 ToolbarActionViewController* view_controller, |
| 166 size_t index) { | 166 size_t index) { |
| 167 ToolbarActionView* view = new ToolbarActionView(view_controller, this); | 167 ToolbarActionView* view = new ToolbarActionView(view_controller, this); |
| 168 toolbar_action_views_.insert(toolbar_action_views_.begin() + index, view); | 168 toolbar_action_views_.insert(toolbar_action_views_.begin() + index, |
| 169 base::WrapUnique(view)); |
| 169 AddChildViewAt(view, index); | 170 AddChildViewAt(view, index); |
| 170 } | 171 } |
| 171 | 172 |
| 172 void BrowserActionsContainer::RemoveViewForAction( | 173 void BrowserActionsContainer::RemoveViewForAction( |
| 173 ToolbarActionViewController* action) { | 174 ToolbarActionViewController* action) { |
| 174 for (ToolbarActionViews::iterator iter = toolbar_action_views_.begin(); | 175 for (ToolbarActionViews::iterator iter = toolbar_action_views_.begin(); |
| 175 iter != toolbar_action_views_.end(); ++iter) { | 176 iter != toolbar_action_views_.end(); ++iter) { |
| 176 if ((*iter)->view_controller() == action) { | 177 if ((*iter)->view_controller() == action) { |
| 177 delete *iter; | |
| 178 toolbar_action_views_.erase(iter); | 178 toolbar_action_views_.erase(iter); |
| 179 break; | 179 break; |
| 180 } | 180 } |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 | 183 |
| 184 void BrowserActionsContainer::RemoveAllViews() { | 184 void BrowserActionsContainer::RemoveAllViews() { |
| 185 base::STLDeleteElements(&toolbar_action_views_); | 185 toolbar_action_views_.clear(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void BrowserActionsContainer::Redraw(bool order_changed) { | 188 void BrowserActionsContainer::Redraw(bool order_changed) { |
| 189 if (!added_to_view_) { | 189 if (!added_to_view_) { |
| 190 // We don't want to redraw before the view has been fully added to the | 190 // We don't want to redraw before the view has been fully added to the |
| 191 // hierarchy. | 191 // hierarchy. |
| 192 return; | 192 return; |
| 193 } | 193 } |
| 194 | 194 |
| 195 // Don't allow resizing if the bar is highlighting. | 195 // Don't allow resizing if the bar is highlighting. |
| 196 if (resize_area_) | 196 if (resize_area_) |
| 197 resize_area_->SetEnabled(!toolbar_actions_bar()->is_highlighting()); | 197 resize_area_->SetEnabled(!toolbar_actions_bar()->is_highlighting()); |
| 198 | 198 |
| 199 std::vector<ToolbarActionViewController*> actions = | 199 std::vector<ToolbarActionViewController*> actions = |
| 200 toolbar_actions_bar_->GetActions(); | 200 toolbar_actions_bar_->GetActions(); |
| 201 if (order_changed) { | 201 if (order_changed) { |
| 202 // Run through the views and compare them to the desired order. If something | 202 // Run through the views and compare them to the desired order. If something |
| 203 // is out of place, find the correct spot for it. | 203 // is out of place, find the correct spot for it. |
| 204 for (int i = 0; i < static_cast<int>(actions.size()) - 1; ++i) { | 204 for (int i = 0; i < static_cast<int>(actions.size()) - 1; ++i) { |
| 205 if (actions[i] != toolbar_action_views_[i]->view_controller()) { | 205 if (actions[i] != toolbar_action_views_[i]->view_controller()) { |
| 206 // Find where the correct view is (it's guaranteed to be after our | 206 // Find where the correct view is (it's guaranteed to be after our |
| 207 // current index, since everything up to this point is correct). | 207 // current index, since everything up to this point is correct). |
| 208 int j = i + 1; | 208 int j = i + 1; |
| 209 while (actions[i] != toolbar_action_views_[j]->view_controller()) | 209 while (actions[i] != toolbar_action_views_[j]->view_controller()) |
| 210 ++j; | 210 ++j; |
| 211 std::swap(toolbar_action_views_[i], toolbar_action_views_[j]); | 211 std::swap(toolbar_action_views_[i], toolbar_action_views_[j]); |
| 212 // Also move the view in the child views vector. | 212 // Also move the view in the child views vector. |
| 213 ReorderChildView(toolbar_action_views_[i], i); | 213 ReorderChildView(toolbar_action_views_[i].get(), i); |
| 214 } | 214 } |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 | 217 |
| 218 Layout(); | 218 Layout(); |
| 219 } | 219 } |
| 220 | 220 |
| 221 void BrowserActionsContainer::ResizeAndAnimate(gfx::Tween::Type tween_type, | 221 void BrowserActionsContainer::ResizeAndAnimate(gfx::Tween::Type tween_type, |
| 222 int target_width) { | 222 int target_width) { |
| 223 if (resize_animation_ && !toolbar_actions_bar_->suppress_animation()) { | 223 if (resize_animation_ && !toolbar_actions_bar_->suppress_animation()) { |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 | 353 |
| 354 // The range of visible icons, from start_index (inclusive) to end_index | 354 // The range of visible icons, from start_index (inclusive) to end_index |
| 355 // (exclusive). | 355 // (exclusive). |
| 356 size_t start_index = toolbar_actions_bar_->GetStartIndexInBounds(); | 356 size_t start_index = toolbar_actions_bar_->GetStartIndexInBounds(); |
| 357 size_t end_index = toolbar_actions_bar_->GetEndIndexInBounds(); | 357 size_t end_index = toolbar_actions_bar_->GetEndIndexInBounds(); |
| 358 | 358 |
| 359 // Now draw the icons for the actions in the available space. Once all the | 359 // Now draw the icons for the actions in the available space. Once all the |
| 360 // variables are in place, the layout works equally well for the main and | 360 // variables are in place, the layout works equally well for the main and |
| 361 // overflow container. | 361 // overflow container. |
| 362 for (size_t i = 0u; i < toolbar_action_views_.size(); ++i) { | 362 for (size_t i = 0u; i < toolbar_action_views_.size(); ++i) { |
| 363 ToolbarActionView* view = toolbar_action_views_[i]; | 363 ToolbarActionView* view = toolbar_action_views_[i].get(); |
| 364 if (i < start_index || i >= end_index) { | 364 if (i < start_index || i >= end_index) { |
| 365 view->SetVisible(false); | 365 view->SetVisible(false); |
| 366 } else { | 366 } else { |
| 367 view->SetBoundsRect(toolbar_actions_bar_->GetFrameForIndex(i)); | 367 view->SetBoundsRect(toolbar_actions_bar_->GetFrameForIndex(i)); |
| 368 view->SetVisible(true); | 368 view->SetVisible(true); |
| 369 } | 369 } |
| 370 } | 370 } |
| 371 } | 371 } |
| 372 | 372 |
| 373 bool BrowserActionsContainer::GetDropFormats( | 373 bool BrowserActionsContainer::GetDropFormats( |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 state->role = ui::AX_ROLE_GROUP; | 507 state->role = ui::AX_ROLE_GROUP; |
| 508 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS); | 508 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS); |
| 509 } | 509 } |
| 510 | 510 |
| 511 void BrowserActionsContainer::WriteDragDataForView(View* sender, | 511 void BrowserActionsContainer::WriteDragDataForView(View* sender, |
| 512 const gfx::Point& press_pt, | 512 const gfx::Point& press_pt, |
| 513 OSExchangeData* data) { | 513 OSExchangeData* data) { |
| 514 toolbar_actions_bar_->OnDragStarted(); | 514 toolbar_actions_bar_->OnDragStarted(); |
| 515 DCHECK(data); | 515 DCHECK(data); |
| 516 | 516 |
| 517 auto it = std::find(toolbar_action_views_.cbegin(), | 517 auto it = |
| 518 toolbar_action_views_.cend(), sender); | 518 std::find_if(toolbar_action_views_.cbegin(), toolbar_action_views_.cend(), |
| 519 [sender](const std::unique_ptr<ToolbarActionView>& ptr) { |
| 520 return ptr.get() == sender; |
| 521 }); |
| 519 DCHECK(it != toolbar_action_views_.cend()); | 522 DCHECK(it != toolbar_action_views_.cend()); |
| 520 ToolbarActionViewController* view_controller = (*it)->view_controller(); | 523 ToolbarActionViewController* view_controller = (*it)->view_controller(); |
| 521 gfx::Size size(ToolbarActionsBar::IconWidth(false), | 524 gfx::Size size(ToolbarActionsBar::IconWidth(false), |
| 522 ToolbarActionsBar::IconHeight()); | 525 ToolbarActionsBar::IconHeight()); |
| 523 drag_utils::SetDragImageOnDataObject( | 526 drag_utils::SetDragImageOnDataObject( |
| 524 view_controller->GetIcon(GetCurrentWebContents(), size).AsImageSkia(), | 527 view_controller->GetIcon(GetCurrentWebContents(), size).AsImageSkia(), |
| 525 press_pt.OffsetFromOrigin(), | 528 press_pt.OffsetFromOrigin(), |
| 526 data); | 529 data); |
| 527 // Fill in the remaining info. | 530 // Fill in the remaining info. |
| 528 BrowserActionDragData drag_data(view_controller->GetId(), | 531 BrowserActionDragData drag_data(view_controller->GetId(), |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 } | 666 } |
| 664 } | 667 } |
| 665 | 668 |
| 666 void BrowserActionsContainer::ClearActiveBubble(views::Widget* widget) { | 669 void BrowserActionsContainer::ClearActiveBubble(views::Widget* widget) { |
| 667 DCHECK(active_bubble_); | 670 DCHECK(active_bubble_); |
| 668 DCHECK_EQ(active_bubble_->GetWidget(), widget); | 671 DCHECK_EQ(active_bubble_->GetWidget(), widget); |
| 669 widget->RemoveObserver(this); | 672 widget->RemoveObserver(this); |
| 670 active_bubble_ = nullptr; | 673 active_bubble_ = nullptr; |
| 671 toolbar_actions_bar_->OnBubbleClosed(); | 674 toolbar_actions_bar_->OnBubbleClosed(); |
| 672 } | 675 } |
| OLD | NEW |