Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / browser_actions_container.cc
blobc8e31a6a38922415f666f4d87f76dcce6762dcfa
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 FOR_EACH_OBSERVER(BrowserActionsContainerObserver,
192 observers_,
193 OnBrowserActionDragDone());
196 views::MenuButton* BrowserActionsContainer::GetOverflowReferenceView() {
197 // With traditional overflow, the reference is the chevron. With the
198 // redesign, we use the wrench menu instead.
199 return chevron_ ?
200 static_cast<views::MenuButton*>(chevron_) :
201 static_cast<views::MenuButton*>(BrowserView::GetBrowserViewForBrowser(
202 browser_)->toolbar()->app_menu());
205 void BrowserActionsContainer::OnMouseEnteredToolbarActionView() {
206 if (!shown_bubble_ && !toolbar_action_views_.empty() &&
207 ExtensionToolbarIconSurfacingBubbleDelegate::ShouldShowForProfile(
208 browser_->profile())) {
209 ExtensionToolbarIconSurfacingBubble* bubble =
210 new ExtensionToolbarIconSurfacingBubble(
211 this,
212 make_scoped_ptr(new ExtensionToolbarIconSurfacingBubbleDelegate(
213 browser_->profile())));
214 views::BubbleDelegateView::CreateBubble(bubble);
215 bubble->Show();
217 shown_bubble_ = true;
220 void BrowserActionsContainer::AddViewForAction(
221 ToolbarActionViewController* view_controller,
222 size_t index) {
223 if (chevron_)
224 chevron_->CloseMenu();
226 ToolbarActionView* view =
227 new ToolbarActionView(view_controller, browser_->profile(), this);
228 toolbar_action_views_.insert(toolbar_action_views_.begin() + index, view);
229 AddChildViewAt(view, index);
232 void BrowserActionsContainer::RemoveViewForAction(
233 ToolbarActionViewController* action) {
234 if (chevron_)
235 chevron_->CloseMenu();
237 for (ToolbarActionViews::iterator iter = toolbar_action_views_.begin();
238 iter != toolbar_action_views_.end(); ++iter) {
239 if ((*iter)->view_controller() == action) {
240 delete *iter;
241 toolbar_action_views_.erase(iter);
242 break;
247 void BrowserActionsContainer::RemoveAllViews() {
248 STLDeleteElements(&toolbar_action_views_);
251 void BrowserActionsContainer::Redraw(bool order_changed) {
252 if (!added_to_view_) {
253 // We don't want to redraw before the view has been fully added to the
254 // hierarchy.
255 return;
258 std::vector<ToolbarActionViewController*> actions =
259 toolbar_actions_bar_->GetActions();
260 if (order_changed) {
261 // Run through the views and compare them to the desired order. If something
262 // is out of place, find the correct spot for it.
263 for (int i = 0; i < static_cast<int>(actions.size()) - 1; ++i) {
264 if (actions[i] != toolbar_action_views_[i]->view_controller()) {
265 // Find where the correct view is (it's guaranteed to be after our
266 // current index, since everything up to this point is correct).
267 int j = i + 1;
268 while (actions[i] != toolbar_action_views_[j]->view_controller())
269 ++j;
270 std::swap(toolbar_action_views_[i], toolbar_action_views_[j]);
275 if (width() != GetPreferredSize().width() && parent()) {
276 parent()->Layout();
277 parent()->SchedulePaint();
278 } else {
279 Layout();
280 SchedulePaint();
284 void BrowserActionsContainer::ResizeAndAnimate(
285 gfx::Tween::Type tween_type,
286 int target_width,
287 bool suppress_chevron) {
288 if (resize_animation_ && !toolbar_actions_bar_->suppress_animation()) {
289 // Animate! We have to set the animation_target_size_ after calling Reset(),
290 // because that could end up calling AnimationEnded which clears the value.
291 resize_animation_->Reset();
292 suppress_chevron_ = suppress_chevron;
293 resize_animation_->SetTweenType(tween_type);
294 animation_target_size_ = target_width;
295 resize_animation_->Show();
296 } else {
297 animation_target_size_ = target_width;
298 AnimationEnded(resize_animation_.get());
302 void BrowserActionsContainer::SetChevronVisibility(bool visible) {
303 if (chevron_)
304 chevron_->SetVisible(visible);
307 int BrowserActionsContainer::GetWidth() const {
308 return container_width_;
311 bool BrowserActionsContainer::IsAnimating() const {
312 return animating();
315 void BrowserActionsContainer::StopAnimating() {
316 animation_target_size_ = container_width_;
317 resize_animation_->Reset();
320 int BrowserActionsContainer::GetChevronWidth() const {
321 return chevron_ ? chevron_->GetPreferredSize().width() + kChevronSpacing : 0;
324 void BrowserActionsContainer::ShowExtensionMessageBubble(
325 scoped_ptr<extensions::ExtensionMessageBubbleController> controller,
326 ToolbarActionViewController* anchor_action) {
327 // The container shouldn't be asked to show a bubble if it's animating.
328 DCHECK(!animating());
330 views::View* reference_view = anchor_action ?
331 static_cast<views::View*>(GetViewForId(anchor_action->GetId())) :
332 BrowserView::GetBrowserViewForBrowser(browser_)->toolbar()->app_menu();
334 extensions::ExtensionMessageBubbleController* weak_controller =
335 controller.get();
336 extensions::ExtensionMessageBubbleView* bubble =
337 new extensions::ExtensionMessageBubbleView(
338 reference_view,
339 views::BubbleBorder::TOP_RIGHT,
340 controller.Pass());
341 views::BubbleDelegateView::CreateBubble(bubble);
342 active_bubble_ = bubble;
343 active_bubble_->GetWidget()->AddObserver(this);
344 weak_controller->Show(bubble);
347 void BrowserActionsContainer::OnWidgetClosing(views::Widget* widget) {
348 ClearActiveBubble(widget);
351 void BrowserActionsContainer::OnWidgetDestroying(views::Widget* widget) {
352 ClearActiveBubble(widget);
355 void BrowserActionsContainer::AddObserver(
356 BrowserActionsContainerObserver* observer) {
357 observers_.AddObserver(observer);
360 void BrowserActionsContainer::RemoveObserver(
361 BrowserActionsContainerObserver* observer) {
362 observers_.RemoveObserver(observer);
365 gfx::Size BrowserActionsContainer::GetPreferredSize() const {
366 if (in_overflow_mode())
367 return toolbar_actions_bar_->GetPreferredSize();
369 // If there are no actions to show, then don't show the container at all.
370 if (toolbar_action_views_.empty())
371 return gfx::Size();
373 // We calculate the size of the view by taking the current width and
374 // subtracting resize_amount_ (the latter represents how far the user is
375 // resizing the view or, if animating the snapping, how far to animate it).
376 // But we also clamp it to a minimum size and the maximum size, so that the
377 // container can never shrink too far or take up more space than it needs.
378 // In other words: minimum_width < width - resize < max_width.
379 int preferred_width = std::min(
380 std::max(toolbar_actions_bar_->GetMinimumWidth(),
381 container_width_ - resize_amount_),
382 toolbar_actions_bar_->GetMaximumWidth());
383 return gfx::Size(preferred_width, ToolbarActionsBar::IconHeight());
386 int BrowserActionsContainer::GetHeightForWidth(int width) const {
387 if (in_overflow_mode())
388 toolbar_actions_bar_->SetOverflowRowWidth(width);
389 return GetPreferredSize().height();
392 gfx::Size BrowserActionsContainer::GetMinimumSize() const {
393 return gfx::Size(toolbar_actions_bar_->GetMinimumWidth(),
394 ToolbarActionsBar::IconHeight());
397 void BrowserActionsContainer::Layout() {
398 if (toolbar_actions_bar_->suppress_layout())
399 return;
401 if (toolbar_action_views_.empty()) {
402 SetVisible(false);
403 return;
406 SetVisible(true);
407 if (resize_area_)
408 resize_area_->SetBounds(0, 0, platform_settings().item_spacing, height());
410 // If the icons don't all fit, show the chevron (unless suppressed).
411 int max_x = GetPreferredSize().width();
412 if (toolbar_actions_bar_->IconCountToWidth(-1) > max_x &&
413 !suppress_chevron_ && chevron_) {
414 chevron_->SetVisible(true);
415 gfx::Size chevron_size(chevron_->GetPreferredSize());
416 max_x -= chevron_size.width() + kChevronSpacing;
417 chevron_->SetBounds(
418 width() - ToolbarView::kStandardSpacing - chevron_size.width(),
420 chevron_size.width(),
421 chevron_size.height());
422 } else if (chevron_) {
423 chevron_->SetVisible(false);
426 // The range of visible icons, from start_index (inclusive) to end_index
427 // (exclusive).
428 size_t start_index = in_overflow_mode() ?
429 toolbar_action_views_.size() - toolbar_actions_bar_->GetIconCount() : 0u;
430 // For the main container's last visible icon, we calculate how many icons we
431 // can display with the given width. We add an extra item_spacing because the
432 // last icon doesn't need padding, but we want it to divide easily.
433 size_t end_index = in_overflow_mode() ?
434 toolbar_action_views_.size() :
435 (max_x - platform_settings().left_padding -
436 platform_settings().right_padding +
437 platform_settings().item_spacing) /
438 ToolbarActionsBar::IconWidth(true);
440 // Now draw the icons for the actions in the available space. Once all the
441 // variables are in place, the layout works equally well for the main and
442 // overflow container.
443 for (size_t i = 0u; i < toolbar_action_views_.size(); ++i) {
444 ToolbarActionView* view = toolbar_action_views_[i];
445 if (i < start_index || i >= end_index) {
446 view->SetVisible(false);
447 } else {
448 view->SetBoundsRect(toolbar_actions_bar_->GetFrameForIndex(i));
449 view->SetVisible(true);
454 void BrowserActionsContainer::OnMouseEntered(const ui::MouseEvent& event) {
455 OnMouseEnteredToolbarActionView();
458 bool BrowserActionsContainer::GetDropFormats(
459 int* formats,
460 std::set<OSExchangeData::CustomFormat>* custom_formats) {
461 return BrowserActionDragData::GetDropFormats(custom_formats);
464 bool BrowserActionsContainer::AreDropTypesRequired() {
465 return BrowserActionDragData::AreDropTypesRequired();
468 bool BrowserActionsContainer::CanDrop(const OSExchangeData& data) {
469 return BrowserActionDragData::CanDrop(data, browser_->profile());
472 int BrowserActionsContainer::OnDragUpdated(
473 const ui::DropTargetEvent& event) {
474 size_t row_index = 0;
475 size_t before_icon_in_row = 0;
476 // If there are no visible actions (such as when dragging an icon to an empty
477 // overflow/main container), then 0, 0 for row, column is correct.
478 if (VisibleBrowserActions() != 0) {
479 // Figure out where to display the indicator. This is a complex calculation:
481 // First, we subtract out the padding to the left of the icon area, which is
482 // ToolbarView::kStandardSpacing. If we're right-to-left, we also mirror the
483 // event.x() so that our calculations are consistent with left-to-right.
484 int offset_into_icon_area =
485 GetMirroredXInView(event.x()) - ToolbarView::kStandardSpacing;
487 // Next, figure out what row we're on. This only matters for overflow mode,
488 // but the calculation is the same for both.
489 row_index = event.y() / ToolbarActionsBar::IconHeight();
491 // Sanity check - we should never be on a different row in the main
492 // container.
493 DCHECK(in_overflow_mode() || row_index == 0);
495 // Next, we determine which icon to place the indicator in front of. We want
496 // to place the indicator in front of icon n when the cursor is between the
497 // midpoints of icons (n - 1) and n. To do this we take the offset into the
498 // icon area and transform it as follows:
500 // Real icon area:
501 // 0 a * b c
502 // | | | |
503 // |[IC|ON] [IC|ON] [IC|ON]
504 // We want to be before icon 0 for 0 < x <= a, icon 1 for a < x <= b, etc.
505 // Here the "*" represents the offset into the icon area, and since it's
506 // between a and b, we want to return "1".
508 // Transformed "icon area":
509 // 0 a * b c
510 // | | | |
511 // |[ICON] |[ICON] |[ICON] |
512 // If we shift both our offset and our divider points later by half an icon
513 // plus one spacing unit, then it becomes very easy to calculate how many
514 // divider points we've passed, because they're the multiples of "one icon
515 // plus padding".
516 int before_icon_unclamped =
517 (offset_into_icon_area + (ToolbarActionsBar::IconWidth(false) / 2) +
518 platform_settings().item_spacing) / ToolbarActionsBar::IconWidth(true);
520 // We need to figure out how many icons are visible on the relevant row.
521 // In the main container, this will just be the visible actions.
522 int visible_icons_on_row = VisibleBrowserActionsAfterAnimation();
523 if (in_overflow_mode()) {
524 int icons_per_row = platform_settings().icons_per_overflow_menu_row;
525 // If this is the final row of the overflow, then this is the remainder of
526 // visible icons. Otherwise, it's a full row (kIconsPerRow).
527 visible_icons_on_row =
528 row_index ==
529 static_cast<size_t>(visible_icons_on_row / icons_per_row) ?
530 visible_icons_on_row % icons_per_row : icons_per_row;
533 // Because the user can drag outside the container bounds, we need to clamp
534 // to the valid range. Note that the maximum allowable value is (num icons),
535 // not (num icons - 1), because we represent the indicator being past the
536 // last icon as being "before the (last + 1) icon".
537 before_icon_in_row =
538 std::min(std::max(before_icon_unclamped, 0), visible_icons_on_row);
541 if (!drop_position_.get() ||
542 !(drop_position_->row == row_index &&
543 drop_position_->icon_in_row == before_icon_in_row)) {
544 drop_position_.reset(new DropPosition(row_index, before_icon_in_row));
545 SchedulePaint();
548 return ui::DragDropTypes::DRAG_MOVE;
551 void BrowserActionsContainer::OnDragExited() {
552 drop_position_.reset();
553 SchedulePaint();
556 int BrowserActionsContainer::OnPerformDrop(
557 const ui::DropTargetEvent& event) {
558 BrowserActionDragData data;
559 if (!data.Read(event.data()))
560 return ui::DragDropTypes::DRAG_NONE;
562 // Make sure we have the same view as we started with.
563 DCHECK_EQ(GetIdAt(data.index()), data.id());
565 size_t i = drop_position_->row *
566 platform_settings().icons_per_overflow_menu_row +
567 drop_position_->icon_in_row;
568 if (in_overflow_mode())
569 i += main_container_->VisibleBrowserActionsAfterAnimation();
570 // |i| now points to the item to the right of the drop indicator*, which is
571 // correct when dragging an icon to the left. When dragging to the right,
572 // however, we want the icon being dragged to get the index of the item to
573 // the left of the drop indicator, so we subtract one.
574 // * Well, it can also point to the end, but not when dragging to the left. :)
575 if (i > data.index())
576 --i;
578 ToolbarActionsBar::DragType drag_type = ToolbarActionsBar::DRAG_TO_SAME;
579 if (!toolbar_action_views_[data.index()]->visible())
580 drag_type = in_overflow_mode() ? ToolbarActionsBar::DRAG_TO_OVERFLOW :
581 ToolbarActionsBar::DRAG_TO_MAIN;
583 toolbar_actions_bar_->OnDragDrop(data.index(), i, drag_type);
585 OnDragExited(); // Perform clean up after dragging.
586 return ui::DragDropTypes::DRAG_MOVE;
589 void BrowserActionsContainer::GetAccessibleState(
590 ui::AXViewState* state) {
591 state->role = ui::AX_ROLE_GROUP;
592 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS);
595 void BrowserActionsContainer::WriteDragDataForView(View* sender,
596 const gfx::Point& press_pt,
597 OSExchangeData* data) {
598 DCHECK(data);
600 ToolbarActionViews::iterator iter = std::find(toolbar_action_views_.begin(),
601 toolbar_action_views_.end(),
602 sender);
603 DCHECK(iter != toolbar_action_views_.end());
604 ToolbarActionViewController* view_controller = (*iter)->view_controller();
605 gfx::Size size(ToolbarActionsBar::IconWidth(false),
606 ToolbarActionsBar::IconHeight());
607 drag_utils::SetDragImageOnDataObject(
608 view_controller->GetIcon(GetCurrentWebContents(), size).AsImageSkia(),
609 press_pt.OffsetFromOrigin(),
610 data);
611 // Fill in the remaining info.
612 BrowserActionDragData drag_data(view_controller->GetId(),
613 iter - toolbar_action_views_.begin());
614 drag_data.Write(browser_->profile(), data);
617 int BrowserActionsContainer::GetDragOperationsForView(View* sender,
618 const gfx::Point& p) {
619 return ui::DragDropTypes::DRAG_MOVE;
622 bool BrowserActionsContainer::CanStartDragForView(View* sender,
623 const gfx::Point& press_pt,
624 const gfx::Point& p) {
625 // We don't allow dragging while we're highlighting.
626 return !toolbar_actions_bar_->is_highlighting();
629 void BrowserActionsContainer::OnResize(int resize_amount, bool done_resizing) {
630 // We don't allow resize while the toolbar is highlighting a subset of
631 // actions, since this is a temporary and entirely browser-driven sequence in
632 // order to warn the user about potentially dangerous items.
633 if (toolbar_actions_bar_->is_highlighting())
634 return;
636 if (!done_resizing) {
637 resize_amount_ = resize_amount;
638 Redraw(false);
639 return;
642 // Up until now we've only been modifying the resize_amount, but now it is
643 // time to set the container size to the size we have resized to, and then
644 // animate to the nearest icon count size if necessary (which may be 0).
645 container_width_ =
646 std::min(std::max(toolbar_actions_bar_->GetMinimumWidth(),
647 container_width_ - resize_amount),
648 toolbar_actions_bar_->GetMaximumWidth());
649 toolbar_actions_bar_->OnResizeComplete(container_width_);
652 void BrowserActionsContainer::AnimationProgressed(
653 const gfx::Animation* animation) {
654 DCHECK_EQ(resize_animation_.get(), animation);
655 resize_amount_ = static_cast<int>(resize_animation_->GetCurrentValue() *
656 (container_width_ - animation_target_size_));
657 Redraw(false);
660 void BrowserActionsContainer::AnimationCanceled(
661 const gfx::Animation* animation) {
662 AnimationEnded(animation);
665 void BrowserActionsContainer::AnimationEnded(const gfx::Animation* animation) {
666 container_width_ = animation_target_size_;
667 animation_target_size_ = 0;
668 resize_amount_ = 0;
669 suppress_chevron_ = false;
670 Redraw(false);
671 FOR_EACH_OBSERVER(BrowserActionsContainerObserver,
672 observers_,
673 OnBrowserActionsContainerAnimationEnded());
675 toolbar_actions_bar_->OnAnimationEnded();
678 content::WebContents* BrowserActionsContainer::GetCurrentWebContents() {
679 return browser_->tab_strip_model()->GetActiveWebContents();
682 extensions::ActiveTabPermissionGranter*
683 BrowserActionsContainer::GetActiveTabPermissionGranter() {
684 content::WebContents* web_contents = GetCurrentWebContents();
685 if (!web_contents)
686 return NULL;
687 return extensions::TabHelper::FromWebContents(web_contents)->
688 active_tab_permission_granter();
691 void BrowserActionsContainer::OnPaint(gfx::Canvas* canvas) {
692 // If the views haven't been initialized yet, wait for the next call to
693 // paint (one will be triggered by entering highlight mode).
694 if (toolbar_actions_bar_->is_highlighting() &&
695 !toolbar_action_views_.empty() && !in_overflow_mode()) {
696 ToolbarActionsModel::HighlightType highlight_type =
697 toolbar_actions_bar_->highlight_type();
698 views::Painter* painter =
699 highlight_type == ToolbarActionsModel::HIGHLIGHT_INFO
700 ? info_highlight_painter_.get()
701 : warning_highlight_painter_.get();
702 views::Painter::PaintPainterAt(canvas, painter, GetLocalBounds());
705 // TODO(sky/glen): Instead of using a drop indicator, animate the icons while
706 // dragging (like we do for tab dragging).
707 if (drop_position_.get()) {
708 // The two-pixel width drop indicator.
709 static const int kDropIndicatorWidth = 2;
711 // Convert back to a pixel offset into the container. First find the X
712 // coordinate of the drop icon.
713 int drop_icon_x = ToolbarView::kStandardSpacing +
714 (drop_position_->icon_in_row * ToolbarActionsBar::IconWidth(true));
715 // Next, find the space before the drop icon. This will either be
716 // left padding or item spacing, depending on whether this is the first
717 // icon.
718 // NOTE: Right now, these are the same. But let's do this right for if they
719 // ever aren't.
720 int space_before_drop_icon = drop_position_->icon_in_row == 0 ?
721 platform_settings().left_padding : platform_settings().item_spacing;
722 // Now place the drop indicator halfway between this and the end of the
723 // previous icon. If there is an odd amount of available space between the
724 // two icons (or the icon and the address bar) after subtracting the drop
725 // indicator width, this calculation puts the extra pixel on the left side
726 // of the indicator, since when the indicator is between the address bar and
727 // the first icon, it looks better closer to the icon.
728 int drop_indicator_x = drop_icon_x -
729 ((space_before_drop_icon + kDropIndicatorWidth) / 2);
730 int row_height = ToolbarActionsBar::IconHeight();
731 int drop_indicator_y = row_height * drop_position_->row;
732 gfx::Rect indicator_bounds(drop_indicator_x,
733 drop_indicator_y,
734 kDropIndicatorWidth,
735 row_height);
736 indicator_bounds.set_x(GetMirroredXForRect(indicator_bounds));
738 // Color of the drop indicator.
739 static const SkColor kDropIndicatorColor = SK_ColorBLACK;
740 canvas->FillRect(indicator_bounds, kDropIndicatorColor);
744 void BrowserActionsContainer::OnThemeChanged() {
745 LoadImages();
748 void BrowserActionsContainer::ViewHierarchyChanged(
749 const ViewHierarchyChangedDetails& details) {
750 if (!toolbar_actions_bar_->enabled())
751 return;
753 if (details.is_add && details.child == this) {
754 if (!in_overflow_mode() && // We only need one keybinding registry.
755 parent()->GetFocusManager()) { // focus manager can be null in tests.
756 extension_keybinding_registry_.reset(new ExtensionKeybindingRegistryViews(
757 browser_->profile(),
758 parent()->GetFocusManager(),
759 extensions::ExtensionKeybindingRegistry::ALL_EXTENSIONS,
760 this));
763 // Initial toolbar button creation and placement in the widget hierarchy.
764 // We do this here instead of in the constructor because adding views
765 // calls Layout on the Toolbar, which needs this object to be constructed
766 // before its Layout function is called.
767 toolbar_actions_bar_->CreateActions();
769 added_to_view_ = true;
773 void BrowserActionsContainer::LoadImages() {
774 if (in_overflow_mode())
775 return; // Overflow mode has neither a chevron nor highlighting.
777 ui::ThemeProvider* tp = GetThemeProvider();
778 if (tp && chevron_) {
779 chevron_->SetImage(views::Button::STATE_NORMAL,
780 *tp->GetImageSkiaNamed(IDR_BROWSER_ACTIONS_OVERFLOW));
783 const int kInfoImages[] = IMAGE_GRID(IDR_TOOLBAR_ACTION_HIGHLIGHT);
784 info_highlight_painter_.reset(
785 views::Painter::CreateImageGridPainter(kInfoImages));
786 const int kWarningImages[] = IMAGE_GRID(IDR_DEVELOPER_MODE_HIGHLIGHT);
787 warning_highlight_painter_.reset(
788 views::Painter::CreateImageGridPainter(kWarningImages));
791 void BrowserActionsContainer::ClearActiveBubble(views::Widget* widget) {
792 DCHECK(active_bubble_);
793 DCHECK_EQ(active_bubble_->GetWidget(), widget);
794 widget->RemoveObserver(this);
795 active_bubble_ = nullptr;