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/wrench_toolbar_button.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/time/time.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
11 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
12 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
13 #include "extensions/common/feature_switch.h"
14 #include "grit/theme_resources.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/base/theme_provider.h"
17 #include "ui/views/controls/button/label_button_border.h"
18 #include "ui/views/metrics.h"
19 #include "ui/views/painter.h"
22 bool WrenchToolbarButton::g_open_wrench_immediately_for_testing
= false;
24 WrenchToolbarButton::WrenchToolbarButton(ToolbarView
* toolbar_view
)
25 : views::MenuButton(NULL
, base::string16(), toolbar_view
, false),
26 wrench_icon_painter_(new WrenchIconPainter(this)),
27 toolbar_view_(toolbar_view
),
28 allow_extension_dragging_(
29 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()),
30 overflowed_toolbar_action_wants_to_run_for_testing_(false),
34 WrenchToolbarButton::~WrenchToolbarButton() {
37 void WrenchToolbarButton::SetSeverity(WrenchIconPainter::Severity severity
,
39 wrench_icon_painter_
->SetSeverity(severity
, animate
);
43 void WrenchToolbarButton::SetOverflowedToolbarActionWantsToRun(
45 overflowed_toolbar_action_wants_to_run_for_testing_
= wants_to_run
;
46 scoped_ptr
<views::LabelButtonBorder
> border
= CreateDefaultBorder();
48 // We use the same style of border as the ToolbarActionViews do to indicate
49 // an action wants to run.
50 ToolbarActionView::DecorateWantsToRunBorder(border
.get());
52 SetBorder(border
.Pass());
56 gfx::Size
WrenchToolbarButton::GetPreferredSize() const {
57 return ResourceBundle::GetSharedInstance().
58 GetImageSkiaNamed(IDR_TOOLBAR_BEZEL_HOVER
)->size();
61 void WrenchToolbarButton::ScheduleWrenchIconPaint() {
65 const char* WrenchToolbarButton::GetClassName() const {
66 return "WrenchToolbarButton";
69 bool WrenchToolbarButton::GetDropFormats(
70 int* formats
, std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) {
71 return allow_extension_dragging_
?
72 BrowserActionDragData::GetDropFormats(custom_formats
) :
73 views::View::GetDropFormats(formats
, custom_formats
);
75 bool WrenchToolbarButton::AreDropTypesRequired() {
76 return allow_extension_dragging_
?
77 BrowserActionDragData::AreDropTypesRequired() :
78 views::View::AreDropTypesRequired();
80 bool WrenchToolbarButton::CanDrop(const ui::OSExchangeData
& data
) {
81 return allow_extension_dragging_
?
82 BrowserActionDragData::CanDrop(data
,
83 toolbar_view_
->browser()->profile()) :
84 views::View::CanDrop(data
);
87 void WrenchToolbarButton::OnDragEntered(const ui::DropTargetEvent
& event
) {
88 DCHECK(allow_extension_dragging_
);
89 DCHECK(!weak_factory_
.HasWeakPtrs());
90 if (!g_open_wrench_immediately_for_testing
) {
91 base::MessageLoop::current()->PostDelayedTask(
93 base::Bind(&WrenchToolbarButton::ShowOverflowMenu
,
94 weak_factory_
.GetWeakPtr()),
95 base::TimeDelta::FromMilliseconds(views::GetMenuShowDelay()));
101 int WrenchToolbarButton::OnDragUpdated(const ui::DropTargetEvent
& event
) {
102 DCHECK(allow_extension_dragging_
);
103 return ui::DragDropTypes::DRAG_MOVE
;
106 void WrenchToolbarButton::OnDragExited() {
107 DCHECK(allow_extension_dragging_
);
108 weak_factory_
.InvalidateWeakPtrs();
111 int WrenchToolbarButton::OnPerformDrop(const ui::DropTargetEvent
& event
) {
112 DCHECK(allow_extension_dragging_
);
113 return ui::DragDropTypes::DRAG_MOVE
;
116 void WrenchToolbarButton::OnPaint(gfx::Canvas
* canvas
) {
117 views::MenuButton::OnPaint(canvas
);
118 wrench_icon_painter_
->Paint(canvas
,
121 WrenchIconPainter::BEZEL_NONE
);
124 void WrenchToolbarButton::ShowOverflowMenu() {
125 toolbar_view_
->ShowAppMenu(true); // For drop.