Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / wrench_toolbar_button.cc
blobc8dbfd3a885a8b36c25c809041405af68d711566
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/ui/browser.h"
12 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
13 #include "chrome/browser/ui/views/layout_constants.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"
24 // static
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()
33 ->IsEnabled()),
34 weak_factory_(this) {
35 if (!ui::MaterialDesignController::IsModeMaterial())
36 wrench_icon_painter_.reset(new WrenchIconPainter(this));
39 WrenchToolbarButton::~WrenchToolbarButton() {
42 void WrenchToolbarButton::SetSeverity(WrenchIconPainter::Severity severity,
43 bool animate) {
44 if (ui::MaterialDesignController::IsModeMaterial())
45 return;
47 wrench_icon_painter_->SetSeverity(severity, animate);
48 SchedulePaint();
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 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON));
57 size.Enlarge(insets.width(), insets.height());
59 return size;
62 return ResourceBundle::GetSharedInstance().
63 GetImageSkiaNamed(IDR_TOOLBAR_BEZEL_HOVER)->size();
66 void WrenchToolbarButton::ScheduleWrenchIconPaint() {
67 SchedulePaint();
70 const char* WrenchToolbarButton::GetClassName() const {
71 return "WrenchToolbarButton";
74 bool WrenchToolbarButton::GetDropFormats(
75 int* formats, std::set<ui::OSExchangeData::CustomFormat>* custom_formats) {
76 return allow_extension_dragging_ ?
77 BrowserActionDragData::GetDropFormats(custom_formats) :
78 views::View::GetDropFormats(formats, custom_formats);
80 bool WrenchToolbarButton::AreDropTypesRequired() {
81 return allow_extension_dragging_ ?
82 BrowserActionDragData::AreDropTypesRequired() :
83 views::View::AreDropTypesRequired();
85 bool WrenchToolbarButton::CanDrop(const ui::OSExchangeData& data) {
86 return allow_extension_dragging_ ?
87 BrowserActionDragData::CanDrop(data,
88 toolbar_view_->browser()->profile()) :
89 views::View::CanDrop(data);
92 void WrenchToolbarButton::OnDragEntered(const ui::DropTargetEvent& event) {
93 DCHECK(allow_extension_dragging_);
94 DCHECK(!weak_factory_.HasWeakPtrs());
95 if (!g_open_wrench_immediately_for_testing) {
96 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
97 FROM_HERE, base::Bind(&WrenchToolbarButton::ShowOverflowMenu,
98 weak_factory_.GetWeakPtr()),
99 base::TimeDelta::FromMilliseconds(views::GetMenuShowDelay()));
100 } else {
101 ShowOverflowMenu();
105 int WrenchToolbarButton::OnDragUpdated(const ui::DropTargetEvent& event) {
106 DCHECK(allow_extension_dragging_);
107 return ui::DragDropTypes::DRAG_MOVE;
110 void WrenchToolbarButton::OnDragExited() {
111 DCHECK(allow_extension_dragging_);
112 weak_factory_.InvalidateWeakPtrs();
115 int WrenchToolbarButton::OnPerformDrop(const ui::DropTargetEvent& event) {
116 DCHECK(allow_extension_dragging_);
117 return ui::DragDropTypes::DRAG_MOVE;
120 void WrenchToolbarButton::OnPaint(gfx::Canvas* canvas) {
121 views::MenuButton::OnPaint(canvas);
122 if (ui::MaterialDesignController::IsModeMaterial())
123 return;
124 wrench_icon_painter_->Paint(canvas,
125 GetThemeProvider(),
126 gfx::Rect(size()),
127 WrenchIconPainter::BEZEL_NONE);
130 void WrenchToolbarButton::ShowOverflowMenu() {
131 toolbar_view_->ShowAppMenu(true); // For drop.