Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / browser_actions_container.cc
blobd28fbf87d14aa0e4231e51f898d1271666c6a405
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
7 #include "base/compiler_specific.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/extensions/extension_message_bubble_controller.h"
10 #include "chrome/browser/extensions/tab_helper.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/extensions/extension_toolbar_icon_surfacing_bubble_delegate.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
16 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
17 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
18 #include "chrome/browser/ui/view_ids.h"
19 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
20 #include "chrome/browser/ui/views/extensions/extension_message_bubble_view.h"
21 #include "chrome/browser/ui/views/extensions/extension_toolbar_icon_surfacing_bubble_views.h"
22 #include "chrome/browser/ui/views/frame/browser_view.h"
23 #include "chrome/browser/ui/views/toolbar/browser_actions_container_observer.h"
24 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
25 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h"
26 #include "chrome/common/extensions/command.h"
27 #include "chrome/grit/generated_resources.h"
28 #include "extensions/common/feature_switch.h"
29 #include "grit/theme_resources.h"
30 #include "third_party/skia/include/core/SkColor.h"
31 #include "ui/accessibility/ax_view_state.h"
32 #include "ui/base/dragdrop/drag_utils.h"
33 #include "ui/base/l10n/l10n_util.h"
34 #include "ui/base/nine_image_painter_factory.h"
35 #include "ui/base/resource/resource_bundle.h"
36 #include "ui/base/theme_provider.h"
37 #include "ui/gfx/canvas.h"
38 #include "ui/gfx/geometry/rect.h"
39 #include "ui/resources/grit/ui_resources.h"
40 #include "ui/views/bubble/bubble_delegate.h"
41 #include "ui/views/controls/resize_area.h"
42 #include "ui/views/painter.h"
43 #include "ui/views/widget/widget.h"
45 namespace {
47 // Horizontal spacing before the chevron (if visible).
48 const int kChevronSpacing = ToolbarView::kStandardSpacing - 2;
50 } // namespace
52 ////////////////////////////////////////////////////////////////////////////////
53 // BrowserActionsContainer::DropPosition
55 struct BrowserActionsContainer::DropPosition {
56 DropPosition(size_t row, size_t icon_in_row);
58 // The (0-indexed) row into which the action will be dropped.
59 size_t row;
61 // The (0-indexed) icon in the row before the action will be dropped.
62 size_t icon_in_row;
65 BrowserActionsContainer::DropPosition::DropPosition(
66 size_t row, size_t icon_in_row)
67 : row(row), icon_in_row(icon_in_row) {
70 ////////////////////////////////////////////////////////////////////////////////
71 // BrowserActionsContainer
73 BrowserActionsContainer::BrowserActionsContainer(
74 Browser* browser,
75 BrowserActionsContainer* main_container)
76 : toolbar_actions_bar_(new ToolbarActionsBar(
77 this,
78 browser,
79 main_container ?
80 main_container->toolbar_actions_bar_.get() : nullptr)),
81 browser_(browser),
82 main_container_(main_container),
83 container_width_(0),
84 resize_area_(NULL),
85 chevron_(NULL),
86 suppress_chevron_(false),
87 added_to_view_(false),
88 shown_bubble_(false),
89 resize_amount_(0),
90 animation_target_size_(0),
91 active_bubble_(nullptr) {
92 set_id(VIEW_ID_BROWSER_ACTION_TOOLBAR);
94 bool overflow_experiment =
95 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled();
96 DCHECK(!in_overflow_mode() || overflow_experiment);
98 if (!in_overflow_mode()) {
99 resize_animation_.reset(new gfx::SlideAnimation(this));
100 resize_area_ = new views::ResizeArea(this);
101 AddChildView(resize_area_);
103 // 'Main' mode doesn't need a chevron overflow when overflow is shown inside
104 // the Chrome menu.
105 if (!overflow_experiment) {
106 // Since the ChevronMenuButton holds a raw pointer to us, we need to
107 // ensure it doesn't outlive us. Having it owned by the view hierarchy as
108 // a child will suffice.
109 chevron_ = new ChevronMenuButton(this);
110 chevron_->EnableCanvasFlippingForRTLUI(true);
111 chevron_->SetAccessibleName(
112 l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS_CHEVRON));
113 chevron_->SetVisible(false);
114 AddChildView(chevron_);
119 BrowserActionsContainer::~BrowserActionsContainer() {
120 if (active_bubble_)
121 active_bubble_->GetWidget()->Close();
122 // We should synchronously receive the OnWidgetClosing() event, so we should
123 // always have cleared the active bubble by now.
124 DCHECK(!active_bubble_);
126 FOR_EACH_OBSERVER(BrowserActionsContainerObserver,
127 observers_,
128 OnBrowserActionsContainerDestroyed());
130 toolbar_actions_bar_->DeleteActions();
131 // All views should be removed as part of ToolbarActionsBar::DeleteActions().
132 DCHECK(toolbar_action_views_.empty());
135 void BrowserActionsContainer::Init() {
136 LoadImages();
138 // We wait to set the container width until now so that the chevron images
139 // will be loaded. The width calculation needs to know the chevron size.
140 container_width_ = toolbar_actions_bar_->GetPreferredSize().width();
143 std::string BrowserActionsContainer::GetIdAt(size_t index) const {
144 return toolbar_action_views_[index]->view_controller()->GetId();
147 ToolbarActionView* BrowserActionsContainer::GetViewForId(
148 const std::string& id) {
149 for (ToolbarActionView* view : toolbar_action_views_) {
150 if (view->view_controller()->GetId() == id)
151 return view;
153 return nullptr;
156 void BrowserActionsContainer::RefreshToolbarActionViews() {
157 toolbar_actions_bar_->Update();
160 size_t BrowserActionsContainer::VisibleBrowserActions() const {
161 size_t visible_actions = 0;
162 for (const ToolbarActionView* view : toolbar_action_views_) {
163 if (view->visible())
164 ++visible_actions;
166 return visible_actions;
169 size_t BrowserActionsContainer::VisibleBrowserActionsAfterAnimation() const {
170 if (!animating())
171 return VisibleBrowserActions();
173 return toolbar_actions_bar_->WidthToIconCount(animation_target_size_);
176 void BrowserActionsContainer::ExecuteExtensionCommand(
177 const extensions::Extension* extension,
178 const extensions::Command& command) {
179 // Global commands are handled by the ExtensionCommandsGlobalRegistry
180 // instance.
181 DCHECK(!command.global());
182 extension_keybinding_registry_->ExecuteCommand(extension->id(),
183 command.accelerator());
186 bool BrowserActionsContainer::ShownInsideMenu() const {
187 return in_overflow_mode();
190 void BrowserActionsContainer::OnToolbarActionViewDragDone() {
191 toolbar_actions_bar_->OnDragEnded();
192 FOR_EACH_OBSERVER(BrowserActionsContainerObserver,
193 observers_,
194 OnBrowserActionDragDone());
197 views::MenuButton* BrowserActionsContainer::GetOverflowReferenceView() {
198 // With traditional overflow, the reference is the chevron. With the
199 // redesign, we use the wrench menu instead.
200 return chevron_ ?
201 static_cast<views::MenuButton*>(chevron_) :
202 static_cast<views::MenuButton*>(BrowserView::GetBrowserViewForBrowser(
203 browser_)->toolbar()->app_menu());
206 void BrowserActionsContainer::OnMouseEnteredToolbarActionView() {
207 if (!shown_bubble_ && !toolbar_action_views_.empty() &&
208 ExtensionToolbarIconSurfacingBubbleDelegate::ShouldShowForProfile(
209 browser_->profile())) {
210 ExtensionToolbarIconSurfacingBubble* bubble =
211 new ExtensionToolbarIconSurfacingBubble(
212 this,
213 make_scoped_ptr(new ExtensionToolbarIconSurfacingBubbleDelegate(
214 browser_->profile())));
215 views::BubbleDelegateView::CreateBubble(bubble);
216 bubble->Show();
218 shown_bubble_ = true;
221 void BrowserActionsContainer::AddViewForAction(
222 ToolbarActionViewController* view_controller,
223 size_t index) {
224 if (chevron_)
225 chevron_->CloseMenu();
227 ToolbarActionView* view =
228 new ToolbarActionView(view_controller, browser_->profile(), this);
229 toolbar_action_views_.insert(toolbar_action_views_.begin() + index, view);
230 AddChildViewAt(view, index);
233 void BrowserActionsContainer::RemoveViewForAction(
234 ToolbarActionViewController* action) {
235 if (chevron_)
236 chevron_->CloseMenu();
238 for (ToolbarActionViews::iterator iter = toolbar_action_views_.begin();
239 iter != toolbar_action_views_.end(); ++iter) {
240 if ((*iter)->view_controller() == action) {
241 delete *iter;
242 toolbar_action_views_.erase(iter);
243 break;
248 void BrowserActionsContainer::RemoveAllViews() {
249 STLDeleteElements(&toolbar_action_views_);
252 void BrowserActionsContainer::Redraw(bool order_changed) {
253 if (!added_to_view_) {
254 // We don't want to redraw before the view has been fully added to the
255 // hierarchy.
256 return;
259 std::vector<ToolbarActionViewController*> actions =
260 toolbar_actions_bar_->GetActions();
261 if (order_changed) {
262 // Run through the views and compare them to the desired order. If something
263 // is out of place, find the correct spot for it.
264 for (int i = 0; i < static_cast<int>(actions.size()) - 1; ++i) {
265 if (actions[i] != toolbar_action_views_[i]->view_controller()) {
266 // Find where the correct view is (it's guaranteed to be after our
267 // current index, since everything up to this point is correct).
268 int j = i + 1;
269 while (actions[i] != toolbar_action_views_[j]->view_controller())
270 ++j;
271 std::swap(toolbar_action_views_[i], toolbar_action_views_[j]);
276 if (width() != GetPreferredSize().width() && parent()) {
277 parent()->Layout();
278 parent()->SchedulePaint();
279 } else {
280 Layout();
281 SchedulePaint();
285 void BrowserActionsContainer::ResizeAndAnimate(
286 gfx::Tween::Type tween_type,
287 int target_width,
288 bool suppress_chevron) {
289 if (resize_animation_ && !toolbar_actions_bar_->suppress_animation()) {
290 // Animate! We have to set the animation_target_size_ after calling Reset(),
291 // because that could end up calling AnimationEnded which clears the value.
292 resize_animation_->Reset();
293 suppress_chevron_ = suppress_chevron;
294 resize_animation_->SetTweenType(tween_type);
295 animation_target_size_ = target_width;
296 resize_animation_->Show();
297 } else {
298 animation_target_size_ = target_width;
299 AnimationEnded(resize_animation_.get());
303 void BrowserActionsContainer::SetChevronVisibility(bool visible) {
304 if (chevron_)
305 chevron_->SetVisible(visible);
308 int BrowserActionsContainer::GetWidth(GetWidthTime get_width_time) const {
309 return get_width_time == GET_WIDTH_AFTER_ANIMATION &&
310 animation_target_size_ > 0
311 ? animation_target_size_
312 : width();
315 bool BrowserActionsContainer::IsAnimating() const {
316 return animating();
319 void BrowserActionsContainer::StopAnimating() {
320 animation_target_size_ = container_width_;
321 resize_animation_->Reset();
324 int BrowserActionsContainer::GetChevronWidth() const {
325 return chevron_ ? chevron_->GetPreferredSize().width() + kChevronSpacing : 0;
328 void BrowserActionsContainer::ShowExtensionMessageBubble(
329 scoped_ptr<extensions::ExtensionMessageBubbleController> controller,
330 ToolbarActionViewController* anchor_action) {
331 // The container shouldn't be asked to show a bubble if it's animating.
332 DCHECK(!animating());
334 views::View* reference_view = anchor_action ?
335 static_cast<views::View*>(GetViewForId(anchor_action->GetId())) :
336 BrowserView::GetBrowserViewForBrowser(browser_)->toolbar()->app_menu();
338 extensions::ExtensionMessageBubbleController* weak_controller =
339 controller.get();
340 extensions::ExtensionMessageBubbleView* bubble =
341 new extensions::ExtensionMessageBubbleView(
342 reference_view,
343 views::BubbleBorder::TOP_RIGHT,
344 controller.Pass());
345 views::BubbleDelegateView::CreateBubble(bubble);
346 active_bubble_ = bubble;
347 active_bubble_->GetWidget()->AddObserver(this);
348 weak_controller->Show(bubble);
351 void BrowserActionsContainer::OnWidgetClosing(views::Widget* widget) {
352 ClearActiveBubble(widget);
355 void BrowserActionsContainer::OnWidgetDestroying(views::Widget* widget) {
356 ClearActiveBubble(widget);
359 void BrowserActionsContainer::AddObserver(
360 BrowserActionsContainerObserver* observer) {
361 observers_.AddObserver(observer);
364 void BrowserActionsContainer::RemoveObserver(
365 BrowserActionsContainerObserver* observer) {
366 observers_.RemoveObserver(observer);
369 gfx::Size BrowserActionsContainer::GetPreferredSize() const {
370 if (in_overflow_mode())
371 return toolbar_actions_bar_->GetPreferredSize();
373 // If there are no actions to show, then don't show the container at all.
374 if (toolbar_action_views_.empty())
375 return gfx::Size();
377 // We calculate the size of the view by taking the current width and
378 // subtracting resize_amount_ (the latter represents how far the user is
379 // resizing the view or, if animating the snapping, how far to animate it).
380 // But we also clamp it to a minimum size and the maximum size, so that the
381 // container can never shrink too far or take up more space than it needs.
382 // In other words: minimum_width < width - resize < max_width.
383 int preferred_width = std::min(
384 std::max(toolbar_actions_bar_->GetMinimumWidth(),
385 container_width_ - resize_amount_),
386 toolbar_actions_bar_->GetMaximumWidth());
387 return gfx::Size(preferred_width, ToolbarActionsBar::IconHeight());
390 int BrowserActionsContainer::GetHeightForWidth(int width) const {
391 if (in_overflow_mode())
392 toolbar_actions_bar_->SetOverflowRowWidth(width);
393 return GetPreferredSize().height();
396 gfx::Size BrowserActionsContainer::GetMinimumSize() const {
397 return gfx::Size(toolbar_actions_bar_->GetMinimumWidth(),
398 ToolbarActionsBar::IconHeight());
401 void BrowserActionsContainer::Layout() {
402 if (toolbar_actions_bar_->suppress_layout())
403 return;
405 if (toolbar_action_views_.empty()) {
406 SetVisible(false);
407 return;
410 SetVisible(true);
411 if (resize_area_)
412 resize_area_->SetBounds(0, 0, platform_settings().item_spacing, height());
414 // The range of visible icons, from start_index (inclusive) to end_index
415 // (exclusive).
416 size_t start_index = toolbar_actions_bar_->GetStartIndexInBounds();
417 size_t end_index = toolbar_actions_bar_->GetEndIndexInBounds();
419 // If the icons don't all fit, show the chevron (unless suppressed).
420 if (chevron_ && !suppress_chevron_ && toolbar_actions_bar_->NeedsOverflow()) {
421 chevron_->SetVisible(true);
422 gfx::Size chevron_size(chevron_->GetPreferredSize());
423 chevron_->SetBounds(
424 width() - ToolbarView::kStandardSpacing - chevron_size.width(),
426 chevron_size.width(),
427 chevron_size.height());
428 } else if (chevron_) {
429 chevron_->SetVisible(false);
432 // Now draw the icons for the actions in the available space. Once all the
433 // variables are in place, the layout works equally well for the main and
434 // overflow container.
435 for (size_t i = 0u; i < toolbar_action_views_.size(); ++i) {
436 ToolbarActionView* view = toolbar_action_views_[i];
437 if (i < start_index || i >= end_index) {
438 view->SetVisible(false);
439 } else {
440 view->SetBoundsRect(toolbar_actions_bar_->GetFrameForIndex(i));
441 view->SetVisible(true);
446 void BrowserActionsContainer::OnMouseEntered(const ui::MouseEvent& event) {
447 OnMouseEnteredToolbarActionView();
450 bool BrowserActionsContainer::GetDropFormats(
451 int* formats,
452 std::set<OSExchangeData::CustomFormat>* custom_formats) {
453 return BrowserActionDragData::GetDropFormats(custom_formats);
456 bool BrowserActionsContainer::AreDropTypesRequired() {
457 return BrowserActionDragData::AreDropTypesRequired();
460 bool BrowserActionsContainer::CanDrop(const OSExchangeData& data) {
461 return BrowserActionDragData::CanDrop(data, browser_->profile());
464 int BrowserActionsContainer::OnDragUpdated(
465 const ui::DropTargetEvent& event) {
466 size_t row_index = 0;
467 size_t before_icon_in_row = 0;
468 // If there are no visible actions (such as when dragging an icon to an empty
469 // overflow/main container), then 0, 0 for row, column is correct.
470 if (VisibleBrowserActions() != 0) {
471 // Figure out where to display the indicator. This is a complex calculation:
473 // First, we subtract out the padding to the left of the icon area, which is
474 // ToolbarView::kStandardSpacing. If we're right-to-left, we also mirror the
475 // event.x() so that our calculations are consistent with left-to-right.
476 int offset_into_icon_area =
477 GetMirroredXInView(event.x()) - ToolbarView::kStandardSpacing;
479 // Next, figure out what row we're on. This only matters for overflow mode,
480 // but the calculation is the same for both.
481 row_index = event.y() / ToolbarActionsBar::IconHeight();
483 // Sanity check - we should never be on a different row in the main
484 // container.
485 DCHECK(in_overflow_mode() || row_index == 0);
487 // Next, we determine which icon to place the indicator in front of. We want
488 // to place the indicator in front of icon n when the cursor is between the
489 // midpoints of icons (n - 1) and n. To do this we take the offset into the
490 // icon area and transform it as follows:
492 // Real icon area:
493 // 0 a * b c
494 // | | | |
495 // |[IC|ON] [IC|ON] [IC|ON]
496 // We want to be before icon 0 for 0 < x <= a, icon 1 for a < x <= b, etc.
497 // Here the "*" represents the offset into the icon area, and since it's
498 // between a and b, we want to return "1".
500 // Transformed "icon area":
501 // 0 a * b c
502 // | | | |
503 // |[ICON] |[ICON] |[ICON] |
504 // If we shift both our offset and our divider points later by half an icon
505 // plus one spacing unit, then it becomes very easy to calculate how many
506 // divider points we've passed, because they're the multiples of "one icon
507 // plus padding".
508 int before_icon_unclamped =
509 (offset_into_icon_area + (ToolbarActionsBar::IconWidth(false) / 2) +
510 platform_settings().item_spacing) / ToolbarActionsBar::IconWidth(true);
512 // We need to figure out how many icons are visible on the relevant row.
513 // In the main container, this will just be the visible actions.
514 int visible_icons_on_row = VisibleBrowserActionsAfterAnimation();
515 if (in_overflow_mode()) {
516 int icons_per_row = platform_settings().icons_per_overflow_menu_row;
517 // If this is the final row of the overflow, then this is the remainder of
518 // visible icons. Otherwise, it's a full row (kIconsPerRow).
519 visible_icons_on_row =
520 row_index ==
521 static_cast<size_t>(visible_icons_on_row / icons_per_row) ?
522 visible_icons_on_row % icons_per_row : icons_per_row;
525 // Because the user can drag outside the container bounds, we need to clamp
526 // to the valid range. Note that the maximum allowable value is (num icons),
527 // not (num icons - 1), because we represent the indicator being past the
528 // last icon as being "before the (last + 1) icon".
529 before_icon_in_row =
530 std::min(std::max(before_icon_unclamped, 0), visible_icons_on_row);
533 if (!drop_position_.get() ||
534 !(drop_position_->row == row_index &&
535 drop_position_->icon_in_row == before_icon_in_row)) {
536 drop_position_.reset(new DropPosition(row_index, before_icon_in_row));
537 SchedulePaint();
540 return ui::DragDropTypes::DRAG_MOVE;
543 void BrowserActionsContainer::OnDragExited() {
544 drop_position_.reset();
545 SchedulePaint();
548 int BrowserActionsContainer::OnPerformDrop(
549 const ui::DropTargetEvent& event) {
550 BrowserActionDragData data;
551 if (!data.Read(event.data()))
552 return ui::DragDropTypes::DRAG_NONE;
554 // Make sure we have the same view as we started with.
555 DCHECK_EQ(GetIdAt(data.index()), data.id());
557 size_t i = drop_position_->row *
558 platform_settings().icons_per_overflow_menu_row +
559 drop_position_->icon_in_row;
560 if (in_overflow_mode())
561 i += main_container_->VisibleBrowserActionsAfterAnimation();
562 // |i| now points to the item to the right of the drop indicator*, which is
563 // correct when dragging an icon to the left. When dragging to the right,
564 // however, we want the icon being dragged to get the index of the item to
565 // the left of the drop indicator, so we subtract one.
566 // * Well, it can also point to the end, but not when dragging to the left. :)
567 if (i > data.index())
568 --i;
570 ToolbarActionsBar::DragType drag_type = ToolbarActionsBar::DRAG_TO_SAME;
571 if (!toolbar_action_views_[data.index()]->visible())
572 drag_type = in_overflow_mode() ? ToolbarActionsBar::DRAG_TO_OVERFLOW :
573 ToolbarActionsBar::DRAG_TO_MAIN;
575 toolbar_actions_bar_->OnDragDrop(data.index(), i, drag_type);
577 OnDragExited(); // Perform clean up after dragging.
578 return ui::DragDropTypes::DRAG_MOVE;
581 void BrowserActionsContainer::GetAccessibleState(
582 ui::AXViewState* state) {
583 state->role = ui::AX_ROLE_GROUP;
584 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS);
587 void BrowserActionsContainer::WriteDragDataForView(View* sender,
588 const gfx::Point& press_pt,
589 OSExchangeData* data) {
590 toolbar_actions_bar_->OnDragStarted();
591 DCHECK(data);
593 ToolbarActionViews::iterator iter = std::find(toolbar_action_views_.begin(),
594 toolbar_action_views_.end(),
595 sender);
596 DCHECK(iter != toolbar_action_views_.end());
597 ToolbarActionViewController* view_controller = (*iter)->view_controller();
598 gfx::Size size(ToolbarActionsBar::IconWidth(false),
599 ToolbarActionsBar::IconHeight());
600 drag_utils::SetDragImageOnDataObject(
601 view_controller->GetIcon(GetCurrentWebContents(), size).AsImageSkia(),
602 press_pt.OffsetFromOrigin(),
603 data);
604 // Fill in the remaining info.
605 BrowserActionDragData drag_data(view_controller->GetId(),
606 iter - toolbar_action_views_.begin());
607 drag_data.Write(browser_->profile(), data);
610 int BrowserActionsContainer::GetDragOperationsForView(View* sender,
611 const gfx::Point& p) {
612 return ui::DragDropTypes::DRAG_MOVE;
615 bool BrowserActionsContainer::CanStartDragForView(View* sender,
616 const gfx::Point& press_pt,
617 const gfx::Point& p) {
618 // We don't allow dragging while we're highlighting.
619 return !toolbar_actions_bar_->is_highlighting();
622 void BrowserActionsContainer::OnResize(int resize_amount, bool done_resizing) {
623 // We don't allow resize while the toolbar is highlighting a subset of
624 // actions, since this is a temporary and entirely browser-driven sequence in
625 // order to warn the user about potentially dangerous items.
626 // We also don't allow resize when the bar is already animating, since we
627 // don't want two competing size changes.
628 if (toolbar_actions_bar_->is_highlighting() || animating())
629 return;
631 if (!done_resizing) {
632 // If this is the start of the resize gesture, then set |container_width_|
633 // to be the current width. It might not have been if the container was
634 // shrunk to give room to the omnibox, but to adjust the size, it needs to
635 // be accurate.
636 if (!resize_amount_)
637 container_width_ = width();
639 resize_amount_ = resize_amount;
640 Redraw(false);
641 return;
644 // Up until now we've only been modifying the resize_amount, but now it is
645 // time to set the container size to the size we have resized to, and then
646 // animate to the nearest icon count size if necessary (which may be 0).
647 container_width_ =
648 std::min(std::max(toolbar_actions_bar_->GetMinimumWidth(),
649 container_width_ - resize_amount),
650 toolbar_actions_bar_->GetMaximumWidth());
651 toolbar_actions_bar_->OnResizeComplete(container_width_);
654 void BrowserActionsContainer::AnimationProgressed(
655 const gfx::Animation* animation) {
656 DCHECK_EQ(resize_animation_.get(), animation);
657 resize_amount_ = static_cast<int>(resize_animation_->GetCurrentValue() *
658 (container_width_ - animation_target_size_));
659 Redraw(false);
662 void BrowserActionsContainer::AnimationCanceled(
663 const gfx::Animation* animation) {
664 AnimationEnded(animation);
667 void BrowserActionsContainer::AnimationEnded(const gfx::Animation* animation) {
668 container_width_ = animation_target_size_;
669 animation_target_size_ = 0;
670 resize_amount_ = 0;
671 suppress_chevron_ = false;
672 Redraw(false);
673 FOR_EACH_OBSERVER(BrowserActionsContainerObserver,
674 observers_,
675 OnBrowserActionsContainerAnimationEnded());
677 toolbar_actions_bar_->OnAnimationEnded();
680 content::WebContents* BrowserActionsContainer::GetCurrentWebContents() {
681 return browser_->tab_strip_model()->GetActiveWebContents();
684 extensions::ActiveTabPermissionGranter*
685 BrowserActionsContainer::GetActiveTabPermissionGranter() {
686 content::WebContents* web_contents = GetCurrentWebContents();
687 if (!web_contents)
688 return NULL;
689 return extensions::TabHelper::FromWebContents(web_contents)->
690 active_tab_permission_granter();
693 void BrowserActionsContainer::OnPaint(gfx::Canvas* canvas) {
694 // If the views haven't been initialized yet, wait for the next call to
695 // paint (one will be triggered by entering highlight mode).
696 if (toolbar_actions_bar_->is_highlighting() &&
697 !toolbar_action_views_.empty() && !in_overflow_mode()) {
698 ToolbarActionsModel::HighlightType highlight_type =
699 toolbar_actions_bar_->highlight_type();
700 views::Painter* painter =
701 highlight_type == ToolbarActionsModel::HIGHLIGHT_INFO
702 ? info_highlight_painter_.get()
703 : warning_highlight_painter_.get();
704 views::Painter::PaintPainterAt(canvas, painter, GetLocalBounds());
707 // TODO(sky/glen): Instead of using a drop indicator, animate the icons while
708 // dragging (like we do for tab dragging).
709 if (drop_position_.get()) {
710 // The two-pixel width drop indicator.
711 static const int kDropIndicatorWidth = 2;
713 // Convert back to a pixel offset into the container. First find the X
714 // coordinate of the drop icon.
715 int drop_icon_x = ToolbarView::kStandardSpacing +
716 (drop_position_->icon_in_row * ToolbarActionsBar::IconWidth(true));
717 // Next, find the space before the drop icon. This will either be
718 // left padding or item spacing, depending on whether this is the first
719 // icon.
720 // NOTE: Right now, these are the same. But let's do this right for if they
721 // ever aren't.
722 int space_before_drop_icon = drop_position_->icon_in_row == 0 ?
723 platform_settings().left_padding : platform_settings().item_spacing;
724 // Now place the drop indicator halfway between this and the end of the
725 // previous icon. If there is an odd amount of available space between the
726 // two icons (or the icon and the address bar) after subtracting the drop
727 // indicator width, this calculation puts the extra pixel on the left side
728 // of the indicator, since when the indicator is between the address bar and
729 // the first icon, it looks better closer to the icon.
730 int drop_indicator_x = drop_icon_x -
731 ((space_before_drop_icon + kDropIndicatorWidth) / 2);
732 int row_height = ToolbarActionsBar::IconHeight();
733 int drop_indicator_y = row_height * drop_position_->row;
734 gfx::Rect indicator_bounds(drop_indicator_x,
735 drop_indicator_y,
736 kDropIndicatorWidth,
737 row_height);
738 indicator_bounds.set_x(GetMirroredXForRect(indicator_bounds));
740 // Color of the drop indicator.
741 static const SkColor kDropIndicatorColor = SK_ColorBLACK;
742 canvas->FillRect(indicator_bounds, kDropIndicatorColor);
746 void BrowserActionsContainer::OnThemeChanged() {
747 LoadImages();
750 void BrowserActionsContainer::ViewHierarchyChanged(
751 const ViewHierarchyChangedDetails& details) {
752 if (!toolbar_actions_bar_->enabled())
753 return;
755 if (details.is_add && details.child == this) {
756 if (!in_overflow_mode() && // We only need one keybinding registry.
757 parent()->GetFocusManager()) { // focus manager can be null in tests.
758 extension_keybinding_registry_.reset(new ExtensionKeybindingRegistryViews(
759 browser_->profile(),
760 parent()->GetFocusManager(),
761 extensions::ExtensionKeybindingRegistry::ALL_EXTENSIONS,
762 this));
765 // Initial toolbar button creation and placement in the widget hierarchy.
766 // We do this here instead of in the constructor because adding views
767 // calls Layout on the Toolbar, which needs this object to be constructed
768 // before its Layout function is called.
769 toolbar_actions_bar_->CreateActions();
771 added_to_view_ = true;
775 void BrowserActionsContainer::LoadImages() {
776 if (in_overflow_mode())
777 return; // Overflow mode has neither a chevron nor highlighting.
779 ui::ThemeProvider* tp = GetThemeProvider();
780 if (tp && chevron_) {
781 chevron_->SetImage(views::Button::STATE_NORMAL,
782 *tp->GetImageSkiaNamed(IDR_BROWSER_ACTIONS_OVERFLOW));
785 const int kInfoImages[] = IMAGE_GRID(IDR_TOOLBAR_ACTION_HIGHLIGHT);
786 info_highlight_painter_.reset(
787 views::Painter::CreateImageGridPainter(kInfoImages));
788 const int kWarningImages[] = IMAGE_GRID(IDR_DEVELOPER_MODE_HIGHLIGHT);
789 warning_highlight_painter_.reset(
790 views::Painter::CreateImageGridPainter(kWarningImages));
793 void BrowserActionsContainer::ClearActiveBubble(views::Widget* widget) {
794 DCHECK(active_bubble_);
795 DCHECK_EQ(active_bubble_->GetWidget(), widget);
796 widget->RemoveObserver(this);
797 active_bubble_ = nullptr;