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/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/themes/theme_properties.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
14 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
15 #include "extensions/common/feature_switch.h"
16 #include "grit/theme_resources.h"
17 #include "ui/base/resource/material_design/material_design_controller.h"
18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/base/theme_provider.h"
20 #include "ui/views/controls/button/label_button_border.h"
21 #include "ui/views/metrics.h"
22 #include "ui/views/painter.h"
25 bool WrenchToolbarButton::g_open_wrench_immediately_for_testing
= false;
27 WrenchToolbarButton::WrenchToolbarButton(ToolbarView
* toolbar_view
)
28 : views::MenuButton(NULL
, base::string16(), toolbar_view
, false),
29 wrench_icon_painter_(nullptr),
30 toolbar_view_(toolbar_view
),
31 allow_extension_dragging_(
32 extensions::FeatureSwitch::extension_action_redesign()
35 if (!ui::MaterialDesignController::IsModeMaterial())
36 wrench_icon_painter_
.reset(new WrenchIconPainter(this));
39 WrenchToolbarButton::~WrenchToolbarButton() {
42 void WrenchToolbarButton::SetSeverity(WrenchIconPainter::Severity severity
,
44 if (ui::MaterialDesignController::IsModeMaterial())
47 wrench_icon_painter_
->SetSeverity(severity
, animate
);
51 gfx::Size
WrenchToolbarButton::GetPreferredSize() const {
52 if (ui::MaterialDesignController::IsModeMaterial()) {
53 gfx::Size
size(image()->GetPreferredSize());
54 ui::ThemeProvider
* provider
= GetThemeProvider();
55 if (provider
&& provider
->UsingSystemTheme()) {
56 int inset
= provider
->GetDisplayProperty(
57 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET
);
58 size
.Enlarge(2 * inset
, 2 * inset
);
63 return ResourceBundle::GetSharedInstance().
64 GetImageSkiaNamed(IDR_TOOLBAR_BEZEL_HOVER
)->size();
67 void WrenchToolbarButton::ScheduleWrenchIconPaint() {
71 const char* WrenchToolbarButton::GetClassName() const {
72 return "WrenchToolbarButton";
75 bool WrenchToolbarButton::GetDropFormats(
76 int* formats
, std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) {
77 return allow_extension_dragging_
?
78 BrowserActionDragData::GetDropFormats(custom_formats
) :
79 views::View::GetDropFormats(formats
, custom_formats
);
81 bool WrenchToolbarButton::AreDropTypesRequired() {
82 return allow_extension_dragging_
?
83 BrowserActionDragData::AreDropTypesRequired() :
84 views::View::AreDropTypesRequired();
86 bool WrenchToolbarButton::CanDrop(const ui::OSExchangeData
& data
) {
87 return allow_extension_dragging_
?
88 BrowserActionDragData::CanDrop(data
,
89 toolbar_view_
->browser()->profile()) :
90 views::View::CanDrop(data
);
93 void WrenchToolbarButton::OnDragEntered(const ui::DropTargetEvent
& event
) {
94 DCHECK(allow_extension_dragging_
);
95 DCHECK(!weak_factory_
.HasWeakPtrs());
96 if (!g_open_wrench_immediately_for_testing
) {
97 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
98 FROM_HERE
, base::Bind(&WrenchToolbarButton::ShowOverflowMenu
,
99 weak_factory_
.GetWeakPtr()),
100 base::TimeDelta::FromMilliseconds(views::GetMenuShowDelay()));
106 int WrenchToolbarButton::OnDragUpdated(const ui::DropTargetEvent
& event
) {
107 DCHECK(allow_extension_dragging_
);
108 return ui::DragDropTypes::DRAG_MOVE
;
111 void WrenchToolbarButton::OnDragExited() {
112 DCHECK(allow_extension_dragging_
);
113 weak_factory_
.InvalidateWeakPtrs();
116 int WrenchToolbarButton::OnPerformDrop(const ui::DropTargetEvent
& event
) {
117 DCHECK(allow_extension_dragging_
);
118 return ui::DragDropTypes::DRAG_MOVE
;
121 void WrenchToolbarButton::OnPaint(gfx::Canvas
* canvas
) {
122 views::MenuButton::OnPaint(canvas
);
123 if (ui::MaterialDesignController::IsModeMaterial())
125 wrench_icon_painter_
->Paint(canvas
,
128 WrenchIconPainter::BEZEL_NONE
);
131 void WrenchToolbarButton::ShowOverflowMenu() {
132 toolbar_view_
->ShowAppMenu(true); // For drop.