Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / browser_actions_container.cc
blob40ba0fd3e601849eeff60d7b80535864a352da4e
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/toolbar/wrench_menu_badge_controller.h"
19 #include "chrome/browser/ui/view_ids.h"
20 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
21 #include "chrome/browser/ui/views/extensions/extension_message_bubble_view.h"
22 #include "chrome/browser/ui/views/extensions/extension_toolbar_icon_surfacing_bubble_views.h"
23 #include "chrome/browser/ui/views/frame/browser_view.h"
24 #include "chrome/browser/ui/views/toolbar/browser_actions_container_observer.h"
25 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
26 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h"
27 #include "chrome/common/extensions/command.h"
28 #include "chrome/grit/generated_resources.h"
29 #include "extensions/common/feature_switch.h"
30 #include "grit/theme_resources.h"
31 #include "third_party/skia/include/core/SkColor.h"
32 #include "ui/accessibility/ax_view_state.h"
33 #include "ui/base/dragdrop/drag_utils.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/nine_image_painter_factory.h"
36 #include "ui/base/resource/resource_bundle.h"
37 #include "ui/base/theme_provider.h"
38 #include "ui/gfx/canvas.h"
39 #include "ui/gfx/geometry/rect.h"
40 #include "ui/resources/grit/ui_resources.h"
41 #include "ui/views/bubble/bubble_delegate.h"
42 #include "ui/views/controls/resize_area.h"
43 #include "ui/views/painter.h"
44 #include "ui/views/widget/widget.h"
46 namespace {
48 // Horizontal spacing before the chevron (if visible).
49 const int kChevronSpacing = ToolbarView::kStandardSpacing - 2;
51 } // namespace
53 ////////////////////////////////////////////////////////////////////////////////
54 // BrowserActionsContainer::DropPosition
56 struct BrowserActionsContainer::DropPosition {
57 DropPosition(size_t row, size_t icon_in_row);
59 // The (0-indexed) row into which the action will be dropped.
60 size_t row;
62 // The (0-indexed) icon in the row before the action will be dropped.
63 size_t icon_in_row;
66 BrowserActionsContainer::DropPosition::DropPosition(
67 size_t row, size_t icon_in_row)
68 : row(row), icon_in_row(icon_in_row) {
71 ////////////////////////////////////////////////////////////////////////////////
72 // BrowserActionsContainer
74 BrowserActionsContainer::BrowserActionsContainer(
75 Browser* browser,
76 BrowserActionsContainer* main_container)
77 : toolbar_actions_bar_(new ToolbarActionsBar(
78 this,
79 browser,
80 main_container ?
81 main_container->toolbar_actions_bar_.get() : nullptr)),
82 browser_(browser),
83 main_container_(main_container),
84 container_width_(0),
85 resize_area_(NULL),
86 chevron_(NULL),
87 suppress_chevron_(false),
88 added_to_view_(false),
89 shown_bubble_(false),
90 resize_amount_(0),
91 animation_target_size_(0),
92 active_bubble_(nullptr) {
93 set_id(VIEW_ID_BROWSER_ACTION_TOOLBAR);
95 bool overflow_experiment =
96 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled();
97 DCHECK(!in_overflow_mode() || overflow_experiment);
99 if (!in_overflow_mode()) {
100 resize_animation_.reset(new gfx::SlideAnimation(this));
101 resize_area_ = new views::ResizeArea(this);
102 AddChildView(resize_area_);
104 // 'Main' mode doesn't need a chevron overflow when overflow is shown inside
105 // the Chrome menu.
106 if (!overflow_experiment) {
107 // Since the ChevronMenuButton holds a raw pointer to us, we need to
108 // ensure it doesn't outlive us. Having it owned by the view hierarchy as
109 // a child will suffice.
110 chevron_ = new ChevronMenuButton(this);
111 chevron_->EnableCanvasFlippingForRTLUI(true);
112 chevron_->SetAccessibleName(
113 l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS_CHEVRON));
114 chevron_->SetVisible(false);
115 AddChildView(chevron_);
120 BrowserActionsContainer::~BrowserActionsContainer() {
121 if (active_bubble_)
122 active_bubble_->GetWidget()->Close();
123 // We should synchronously receive the OnWidgetClosing() event, so we should
124 // always have cleared the active bubble by now.
125 DCHECK(!active_bubble_);
127 FOR_EACH_OBSERVER(BrowserActionsContainerObserver,
128 observers_,
129 OnBrowserActionsContainerDestroyed());
131 toolbar_actions_bar_->DeleteActions();
132 // All views should be removed as part of ToolbarActionsBar::DeleteActions().
133 DCHECK(toolbar_action_views_.empty());
136 void BrowserActionsContainer::Init() {
137 LoadImages();
139 // We wait to set the container width until now so that the chevron images
140 // will be loaded. The width calculation needs to know the chevron size.
141 container_width_ = toolbar_actions_bar_->GetPreferredSize().width();
144 std::string BrowserActionsContainer::GetIdAt(size_t index) const {
145 return toolbar_action_views_[index]->view_controller()->GetId();
148 ToolbarActionView* BrowserActionsContainer::GetViewForId(
149 const std::string& id) {
150 for (ToolbarActionView* view : toolbar_action_views_) {
151 if (view->view_controller()->GetId() == id)
152 return view;
154 return nullptr;
157 void BrowserActionsContainer::RefreshToolbarActionViews() {
158 toolbar_actions_bar_->Update();
161 size_t BrowserActionsContainer::VisibleBrowserActions() const {
162 size_t visible_actions = 0;
163 for (const ToolbarActionView* view : toolbar_action_views_) {
164 if (view->visible())
165 ++visible_actions;
167 return visible_actions;
170 size_t BrowserActionsContainer::VisibleBrowserActionsAfterAnimation() const {
171 if (!animating())
172 return VisibleBrowserActions();
174 return toolbar_actions_bar_->WidthToIconCount(animation_target_size_);
177 void BrowserActionsContainer::ExecuteExtensionCommand(
178 const extensions::Extension* extension,
179 const extensions::Command& command) {
180 // Global commands are handled by the ExtensionCommandsGlobalRegistry
181 // instance.
182 DCHECK(!command.global());
183 extension_keybinding_registry_->ExecuteCommand(extension->id(),
184 command.accelerator());
187 bool BrowserActionsContainer::ShownInsideMenu() const {
188 return in_overflow_mode();
191 void BrowserActionsContainer::OnToolbarActionViewDragDone() {
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() const {
309 return container_width_;
312 bool BrowserActionsContainer::IsAnimating() const {
313 return animating();
316 void BrowserActionsContainer::StopAnimating() {
317 animation_target_size_ = container_width_;
318 resize_animation_->Reset();
321 int BrowserActionsContainer::GetChevronWidth() const {
322 return chevron_ ? chevron_->GetPreferredSize().width() + kChevronSpacing : 0;
325 void BrowserActionsContainer::OnOverflowedActionWantsToRunChanged(
326 bool overflowed_action_wants_to_run) {
327 DCHECK(!in_overflow_mode());
328 BrowserView::GetBrowserViewForBrowser(browser_)->toolbar()->
329 wrench_menu_badge_controller()->SetOverflowedToolbarActionWantsToRun(
330 overflowed_action_wants_to_run);
333 void BrowserActionsContainer::ShowExtensionMessageBubble(
334 scoped_ptr<extensions::ExtensionMessageBubbleController> controller,
335 ToolbarActionViewController* anchor_action) {
336 // The container shouldn't be asked to show a bubble if it's animating.
337 DCHECK(!animating());
339 views::View* reference_view = anchor_action ?
340 static_cast<views::View*>(GetViewForId(anchor_action->GetId())) :
341 BrowserView::GetBrowserViewForBrowser(browser_)->toolbar()->app_menu();
343 extensions::ExtensionMessageBubbleController* weak_controller =
344 controller.get();
345 extensions::ExtensionMessageBubbleView* bubble =
346 new extensions::ExtensionMessageBubbleView(
347 reference_view,
348 views::BubbleBorder::TOP_RIGHT,
349 controller.Pass());
350 views::BubbleDelegateView::CreateBubble(bubble);
351 active_bubble_ = bubble;
352 active_bubble_->GetWidget()->AddObserver(this);
353 weak_controller->Show(bubble);
356 void BrowserActionsContainer::OnWidgetClosing(views::Widget* widget) {
357 ClearActiveBubble(widget);
360 void BrowserActionsContainer::OnWidgetDestroying(views::Widget* widget) {
361 ClearActiveBubble(widget);
364 void BrowserActionsContainer::AddObserver(
365 BrowserActionsContainerObserver* observer) {
366 observers_.AddObserver(observer);
369 void BrowserActionsContainer::RemoveObserver(
370 BrowserActionsContainerObserver* observer) {
371 observers_.RemoveObserver(observer);
374 gfx::Size BrowserActionsContainer::GetPreferredSize() const {
375 if (in_overflow_mode())
376 return toolbar_actions_bar_->GetPreferredSize();
378 // If there are no actions to show, then don't show the container at all.
379 if (toolbar_action_views_.empty())
380 return gfx::Size();
382 // We calculate the size of the view by taking the current width and
383 // subtracting resize_amount_ (the latter represents how far the user is
384 // resizing the view or, if animating the snapping, how far to animate it).
385 // But we also clamp it to a minimum size and the maximum size, so that the
386 // container can never shrink too far or take up more space than it needs.
387 // In other words: minimum_width < width - resize < max_width.
388 int preferred_width = std::min(
389 std::max(toolbar_actions_bar_->GetMinimumWidth(),
390 container_width_ - resize_amount_),
391 toolbar_actions_bar_->GetMaximumWidth());
392 return gfx::Size(preferred_width, ToolbarActionsBar::IconHeight());
395 int BrowserActionsContainer::GetHeightForWidth(int width) const {
396 if (in_overflow_mode())
397 toolbar_actions_bar_->SetOverflowRowWidth(width);
398 return GetPreferredSize().height();
401 gfx::Size BrowserActionsContainer::GetMinimumSize() const {
402 return gfx::Size(toolbar_actions_bar_->GetMinimumWidth(),
403 ToolbarActionsBar::IconHeight());
406 void BrowserActionsContainer::Layout() {
407 if (toolbar_actions_bar_->suppress_layout())
408 return;
410 if (toolbar_action_views_.empty()) {
411 SetVisible(false);
412 return;
415 SetVisible(true);
416 if (resize_area_)
417 resize_area_->SetBounds(0, 0, platform_settings().item_spacing, height());
419 // If the icons don't all fit, show the chevron (unless suppressed).
420 int max_x = GetPreferredSize().width();
421 if (toolbar_actions_bar_->IconCountToWidth(-1) > max_x &&
422 !suppress_chevron_ && chevron_) {
423 chevron_->SetVisible(true);
424 gfx::Size chevron_size(chevron_->GetPreferredSize());
425 max_x -= chevron_size.width() + kChevronSpacing;
426 chevron_->SetBounds(
427 width() - ToolbarView::kStandardSpacing - chevron_size.width(),
429 chevron_size.width(),
430 chevron_size.height());
431 } else if (chevron_) {
432 chevron_->SetVisible(false);
435 // The range of visible icons, from start_index (inclusive) to end_index
436 // (exclusive).
437 size_t start_index = in_overflow_mode() ?
438 toolbar_action_views_.size() - toolbar_actions_bar_->GetIconCount() : 0u;
439 // For the main container's last visible icon, we calculate how many icons we
440 // can display with the given width. We add an extra item_spacing because the
441 // last icon doesn't need padding, but we want it to divide easily.
442 size_t end_index = in_overflow_mode() ?
443 toolbar_action_views_.size() :
444 (max_x - platform_settings().left_padding -
445 platform_settings().right_padding +
446 platform_settings().item_spacing) /
447 ToolbarActionsBar::IconWidth(true);
449 // Now draw the icons for the actions in the available space. Once all the
450 // variables are in place, the layout works equally well for the main and
451 // overflow container.
452 for (size_t i = 0u; i < toolbar_action_views_.size(); ++i) {
453 ToolbarActionView* view = toolbar_action_views_[i];
454 if (i < start_index || i >= end_index) {
455 view->SetVisible(false);
456 } else {
457 view->SetBoundsRect(toolbar_actions_bar_->GetFrameForIndex(i));
458 view->SetVisible(true);
463 void BrowserActionsContainer::OnMouseEntered(const ui::MouseEvent& event) {
464 OnMouseEnteredToolbarActionView();
467 bool BrowserActionsContainer::GetDropFormats(
468 int* formats,
469 std::set<OSExchangeData::CustomFormat>* custom_formats) {
470 return BrowserActionDragData::GetDropFormats(custom_formats);
473 bool BrowserActionsContainer::AreDropTypesRequired() {
474 return BrowserActionDragData::AreDropTypesRequired();
477 bool BrowserActionsContainer::CanDrop(const OSExchangeData& data) {
478 return BrowserActionDragData::CanDrop(data, browser_->profile());
481 int BrowserActionsContainer::OnDragUpdated(
482 const ui::DropTargetEvent& event) {
483 size_t row_index = 0;
484 size_t before_icon_in_row = 0;
485 // If there are no visible actions (such as when dragging an icon to an empty
486 // overflow/main container), then 0, 0 for row, column is correct.
487 if (VisibleBrowserActions() != 0) {
488 // Figure out where to display the indicator. This is a complex calculation:
490 // First, we subtract out the padding to the left of the icon area, which is
491 // ToolbarView::kStandardSpacing. If we're right-to-left, we also mirror the
492 // event.x() so that our calculations are consistent with left-to-right.
493 int offset_into_icon_area =
494 GetMirroredXInView(event.x()) - ToolbarView::kStandardSpacing;
496 // Next, figure out what row we're on. This only matters for overflow mode,
497 // but the calculation is the same for both.
498 row_index = event.y() / ToolbarActionsBar::IconHeight();
500 // Sanity check - we should never be on a different row in the main
501 // container.
502 DCHECK(in_overflow_mode() || row_index == 0);
504 // Next, we determine which icon to place the indicator in front of. We want
505 // to place the indicator in front of icon n when the cursor is between the
506 // midpoints of icons (n - 1) and n. To do this we take the offset into the
507 // icon area and transform it as follows:
509 // Real icon area:
510 // 0 a * b c
511 // | | | |
512 // |[IC|ON] [IC|ON] [IC|ON]
513 // We want to be before icon 0 for 0 < x <= a, icon 1 for a < x <= b, etc.
514 // Here the "*" represents the offset into the icon area, and since it's
515 // between a and b, we want to return "1".
517 // Transformed "icon area":
518 // 0 a * b c
519 // | | | |
520 // |[ICON] |[ICON] |[ICON] |
521 // If we shift both our offset and our divider points later by half an icon
522 // plus one spacing unit, then it becomes very easy to calculate how many
523 // divider points we've passed, because they're the multiples of "one icon
524 // plus padding".
525 int before_icon_unclamped =
526 (offset_into_icon_area + (ToolbarActionsBar::IconWidth(false) / 2) +
527 platform_settings().item_spacing) / ToolbarActionsBar::IconWidth(true);
529 // We need to figure out how many icons are visible on the relevant row.
530 // In the main container, this will just be the visible actions.
531 int visible_icons_on_row = VisibleBrowserActionsAfterAnimation();
532 if (in_overflow_mode()) {
533 int icons_per_row = platform_settings().icons_per_overflow_menu_row;
534 // If this is the final row of the overflow, then this is the remainder of
535 // visible icons. Otherwise, it's a full row (kIconsPerRow).
536 visible_icons_on_row =
537 row_index ==
538 static_cast<size_t>(visible_icons_on_row / icons_per_row) ?
539 visible_icons_on_row % icons_per_row : icons_per_row;
542 // Because the user can drag outside the container bounds, we need to clamp
543 // to the valid range. Note that the maximum allowable value is (num icons),
544 // not (num icons - 1), because we represent the indicator being past the
545 // last icon as being "before the (last + 1) icon".
546 before_icon_in_row =
547 std::min(std::max(before_icon_unclamped, 0), visible_icons_on_row);
550 if (!drop_position_.get() ||
551 !(drop_position_->row == row_index &&
552 drop_position_->icon_in_row == before_icon_in_row)) {
553 drop_position_.reset(new DropPosition(row_index, before_icon_in_row));
554 SchedulePaint();
557 return ui::DragDropTypes::DRAG_MOVE;
560 void BrowserActionsContainer::OnDragExited() {
561 drop_position_.reset();
562 SchedulePaint();
565 int BrowserActionsContainer::OnPerformDrop(
566 const ui::DropTargetEvent& event) {
567 BrowserActionDragData data;
568 if (!data.Read(event.data()))
569 return ui::DragDropTypes::DRAG_NONE;
571 // Make sure we have the same view as we started with.
572 DCHECK_EQ(GetIdAt(data.index()), data.id());
574 size_t i = drop_position_->row *
575 platform_settings().icons_per_overflow_menu_row +
576 drop_position_->icon_in_row;
577 if (in_overflow_mode())
578 i += main_container_->VisibleBrowserActionsAfterAnimation();
579 // |i| now points to the item to the right of the drop indicator*, which is
580 // correct when dragging an icon to the left. When dragging to the right,
581 // however, we want the icon being dragged to get the index of the item to
582 // the left of the drop indicator, so we subtract one.
583 // * Well, it can also point to the end, but not when dragging to the left. :)
584 if (i > data.index())
585 --i;
587 ToolbarActionsBar::DragType drag_type = ToolbarActionsBar::DRAG_TO_SAME;
588 if (!toolbar_action_views_[data.index()]->visible())
589 drag_type = in_overflow_mode() ? ToolbarActionsBar::DRAG_TO_OVERFLOW :
590 ToolbarActionsBar::DRAG_TO_MAIN;
592 toolbar_actions_bar_->OnDragDrop(data.index(), i, drag_type);
594 OnDragExited(); // Perform clean up after dragging.
595 return ui::DragDropTypes::DRAG_MOVE;
598 void BrowserActionsContainer::GetAccessibleState(
599 ui::AXViewState* state) {
600 state->role = ui::AX_ROLE_GROUP;
601 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS);
604 void BrowserActionsContainer::WriteDragDataForView(View* sender,
605 const gfx::Point& press_pt,
606 OSExchangeData* data) {
607 DCHECK(data);
609 ToolbarActionViews::iterator iter = std::find(toolbar_action_views_.begin(),
610 toolbar_action_views_.end(),
611 sender);
612 DCHECK(iter != toolbar_action_views_.end());
613 ToolbarActionViewController* view_controller = (*iter)->view_controller();
614 gfx::Size size(ToolbarActionsBar::IconWidth(false),
615 ToolbarActionsBar::IconHeight());
616 drag_utils::SetDragImageOnDataObject(
617 view_controller->GetIcon(GetCurrentWebContents(), size).AsImageSkia(),
618 press_pt.OffsetFromOrigin(),
619 data);
620 // Fill in the remaining info.
621 BrowserActionDragData drag_data(view_controller->GetId(),
622 iter - toolbar_action_views_.begin());
623 drag_data.Write(browser_->profile(), data);
626 int BrowserActionsContainer::GetDragOperationsForView(View* sender,
627 const gfx::Point& p) {
628 return ui::DragDropTypes::DRAG_MOVE;
631 bool BrowserActionsContainer::CanStartDragForView(View* sender,
632 const gfx::Point& press_pt,
633 const gfx::Point& p) {
634 // We don't allow dragging while we're highlighting.
635 return !toolbar_actions_bar_->is_highlighting();
638 void BrowserActionsContainer::OnResize(int resize_amount, bool done_resizing) {
639 // We don't allow resize while the toolbar is highlighting a subset of
640 // actions, since this is a temporary and entirely browser-driven sequence in
641 // order to warn the user about potentially dangerous items.
642 if (toolbar_actions_bar_->is_highlighting())
643 return;
645 if (!done_resizing) {
646 resize_amount_ = resize_amount;
647 Redraw(false);
648 return;
651 // Up until now we've only been modifying the resize_amount, but now it is
652 // time to set the container size to the size we have resized to, and then
653 // animate to the nearest icon count size if necessary (which may be 0).
654 container_width_ =
655 std::min(std::max(toolbar_actions_bar_->GetMinimumWidth(),
656 container_width_ - resize_amount),
657 toolbar_actions_bar_->GetMaximumWidth());
658 toolbar_actions_bar_->OnResizeComplete(container_width_);
661 void BrowserActionsContainer::AnimationProgressed(
662 const gfx::Animation* animation) {
663 DCHECK_EQ(resize_animation_.get(), animation);
664 resize_amount_ = static_cast<int>(resize_animation_->GetCurrentValue() *
665 (container_width_ - animation_target_size_));
666 Redraw(false);
669 void BrowserActionsContainer::AnimationCanceled(
670 const gfx::Animation* animation) {
671 AnimationEnded(animation);
674 void BrowserActionsContainer::AnimationEnded(const gfx::Animation* animation) {
675 container_width_ = animation_target_size_;
676 animation_target_size_ = 0;
677 resize_amount_ = 0;
678 suppress_chevron_ = false;
679 Redraw(false);
680 FOR_EACH_OBSERVER(BrowserActionsContainerObserver,
681 observers_,
682 OnBrowserActionsContainerAnimationEnded());
684 toolbar_actions_bar_->OnAnimationEnded();
687 content::WebContents* BrowserActionsContainer::GetCurrentWebContents() {
688 return browser_->tab_strip_model()->GetActiveWebContents();
691 extensions::ActiveTabPermissionGranter*
692 BrowserActionsContainer::GetActiveTabPermissionGranter() {
693 content::WebContents* web_contents = GetCurrentWebContents();
694 if (!web_contents)
695 return NULL;
696 return extensions::TabHelper::FromWebContents(web_contents)->
697 active_tab_permission_granter();
700 void BrowserActionsContainer::OnPaint(gfx::Canvas* canvas) {
701 // If the views haven't been initialized yet, wait for the next call to
702 // paint (one will be triggered by entering highlight mode).
703 if (toolbar_actions_bar_->is_highlighting() &&
704 !toolbar_action_views_.empty() && !in_overflow_mode()) {
705 ToolbarActionsModel::HighlightType highlight_type =
706 toolbar_actions_bar_->highlight_type();
707 views::Painter* painter =
708 highlight_type == ToolbarActionsModel::HIGHLIGHT_INFO
709 ? info_highlight_painter_.get()
710 : warning_highlight_painter_.get();
711 views::Painter::PaintPainterAt(canvas, painter, GetLocalBounds());
714 // TODO(sky/glen): Instead of using a drop indicator, animate the icons while
715 // dragging (like we do for tab dragging).
716 if (drop_position_.get()) {
717 // The two-pixel width drop indicator.
718 static const int kDropIndicatorWidth = 2;
720 // Convert back to a pixel offset into the container. First find the X
721 // coordinate of the drop icon.
722 int drop_icon_x = ToolbarView::kStandardSpacing +
723 (drop_position_->icon_in_row * ToolbarActionsBar::IconWidth(true));
724 // Next, find the space before the drop icon. This will either be
725 // left padding or item spacing, depending on whether this is the first
726 // icon.
727 // NOTE: Right now, these are the same. But let's do this right for if they
728 // ever aren't.
729 int space_before_drop_icon = drop_position_->icon_in_row == 0 ?
730 platform_settings().left_padding : platform_settings().item_spacing;
731 // Now place the drop indicator halfway between this and the end of the
732 // previous icon. If there is an odd amount of available space between the
733 // two icons (or the icon and the address bar) after subtracting the drop
734 // indicator width, this calculation puts the extra pixel on the left side
735 // of the indicator, since when the indicator is between the address bar and
736 // the first icon, it looks better closer to the icon.
737 int drop_indicator_x = drop_icon_x -
738 ((space_before_drop_icon + kDropIndicatorWidth) / 2);
739 int row_height = ToolbarActionsBar::IconHeight();
740 int drop_indicator_y = row_height * drop_position_->row;
741 gfx::Rect indicator_bounds(drop_indicator_x,
742 drop_indicator_y,
743 kDropIndicatorWidth,
744 row_height);
745 indicator_bounds.set_x(GetMirroredXForRect(indicator_bounds));
747 // Color of the drop indicator.
748 static const SkColor kDropIndicatorColor = SK_ColorBLACK;
749 canvas->FillRect(indicator_bounds, kDropIndicatorColor);
753 void BrowserActionsContainer::OnThemeChanged() {
754 LoadImages();
757 void BrowserActionsContainer::ViewHierarchyChanged(
758 const ViewHierarchyChangedDetails& details) {
759 if (!toolbar_actions_bar_->enabled())
760 return;
762 if (details.is_add && details.child == this) {
763 if (!in_overflow_mode() && // We only need one keybinding registry.
764 parent()->GetFocusManager()) { // focus manager can be null in tests.
765 extension_keybinding_registry_.reset(new ExtensionKeybindingRegistryViews(
766 browser_->profile(),
767 parent()->GetFocusManager(),
768 extensions::ExtensionKeybindingRegistry::ALL_EXTENSIONS,
769 this));
772 // Initial toolbar button creation and placement in the widget hierarchy.
773 // We do this here instead of in the constructor because adding views
774 // calls Layout on the Toolbar, which needs this object to be constructed
775 // before its Layout function is called.
776 toolbar_actions_bar_->CreateActions();
778 added_to_view_ = true;
782 void BrowserActionsContainer::LoadImages() {
783 if (in_overflow_mode())
784 return; // Overflow mode has neither a chevron nor highlighting.
786 ui::ThemeProvider* tp = GetThemeProvider();
787 if (tp && chevron_) {
788 chevron_->SetImage(views::Button::STATE_NORMAL,
789 *tp->GetImageSkiaNamed(IDR_BROWSER_ACTIONS_OVERFLOW));
792 const int kInfoImages[] = IMAGE_GRID(IDR_TOOLBAR_ACTION_HIGHLIGHT);
793 info_highlight_painter_.reset(
794 views::Painter::CreateImageGridPainter(kInfoImages));
795 const int kWarningImages[] = IMAGE_GRID(IDR_DEVELOPER_MODE_HIGHLIGHT);
796 warning_highlight_painter_.reset(
797 views::Painter::CreateImageGridPainter(kWarningImages));
800 void BrowserActionsContainer::ClearActiveBubble(views::Widget* widget) {
801 DCHECK(active_bubble_);
802 DCHECK_EQ(active_bubble_->GetWidget(), widget);
803 widget->RemoveObserver(this);
804 active_bubble_ = nullptr;