| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h" | 5 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h" |
| 6 | 6 |
| 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_view.h" | |
| 12 #include "extensions/common/feature_switch.h" | |
| 13 #include "grit/theme_resources.h" | 7 #include "grit/theme_resources.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 8 #include "ui/base/resource/resource_bundle.h" |
| 15 #include "ui/base/theme_provider.h" | 9 #include "ui/base/theme_provider.h" |
| 16 #include "ui/views/metrics.h" | |
| 17 #include "ui/views/painter.h" | 10 #include "ui/views/painter.h" |
| 18 | 11 |
| 19 // static | 12 WrenchToolbarButton::WrenchToolbarButton(views::MenuButtonListener* listener) |
| 20 bool WrenchToolbarButton::g_open_wrench_immediately_for_testing = false; | 13 : views::MenuButton(NULL, base::string16(), listener, false) { |
| 21 | 14 wrench_icon_painter_.reset(new WrenchIconPainter(this)); |
| 22 WrenchToolbarButton::WrenchToolbarButton(ToolbarView* toolbar_view) | |
| 23 : views::MenuButton(NULL, base::string16(), toolbar_view, false), | |
| 24 wrench_icon_painter_(new WrenchIconPainter(this)), | |
| 25 toolbar_view_(toolbar_view), | |
| 26 allow_extension_dragging_( | |
| 27 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()), | |
| 28 weak_factory_(this) { | |
| 29 } | 15 } |
| 30 | 16 |
| 31 WrenchToolbarButton::~WrenchToolbarButton() { | 17 WrenchToolbarButton::~WrenchToolbarButton() { |
| 32 } | 18 } |
| 33 | 19 |
| 34 void WrenchToolbarButton::SetSeverity(WrenchIconPainter::Severity severity, | 20 void WrenchToolbarButton::SetSeverity(WrenchIconPainter::Severity severity, |
| 35 bool animate) { | 21 bool animate) { |
| 36 wrench_icon_painter_->SetSeverity(severity, animate); | 22 wrench_icon_painter_->SetSeverity(severity, animate); |
| 37 SchedulePaint(); | 23 SchedulePaint(); |
| 38 } | 24 } |
| 39 | 25 |
| 40 gfx::Size WrenchToolbarButton::GetPreferredSize() const { | 26 gfx::Size WrenchToolbarButton::GetPreferredSize() const { |
| 41 return ResourceBundle::GetSharedInstance(). | 27 return ResourceBundle::GetSharedInstance(). |
| 42 GetImageSkiaNamed(IDR_TOOLBAR_BEZEL_HOVER)->size(); | 28 GetImageSkiaNamed(IDR_TOOLBAR_BEZEL_HOVER)->size(); |
| 43 } | 29 } |
| 44 | 30 |
| 45 void WrenchToolbarButton::OnPaint(gfx::Canvas* canvas) { | 31 void WrenchToolbarButton::OnPaint(gfx::Canvas* canvas) { |
| 46 views::MenuButton::OnPaint(canvas); | 32 views::MenuButton::OnPaint(canvas); |
| 47 wrench_icon_painter_->Paint(canvas, | 33 wrench_icon_painter_->Paint(canvas, |
| 48 GetThemeProvider(), | 34 GetThemeProvider(), |
| 49 gfx::Rect(size()), | 35 gfx::Rect(size()), |
| 50 WrenchIconPainter::BEZEL_NONE); | 36 WrenchIconPainter::BEZEL_NONE); |
| 51 } | 37 } |
| 52 | 38 |
| 53 void WrenchToolbarButton::ScheduleWrenchIconPaint() { | 39 void WrenchToolbarButton::ScheduleWrenchIconPaint() { |
| 54 SchedulePaint(); | 40 SchedulePaint(); |
| 55 } | 41 } |
| 56 | |
| 57 bool WrenchToolbarButton::GetDropFormats( | |
| 58 int* formats, std::set<ui::OSExchangeData::CustomFormat>* custom_formats) { | |
| 59 return allow_extension_dragging_ ? | |
| 60 BrowserActionDragData::GetDropFormats(custom_formats) : | |
| 61 views::View::GetDropFormats(formats, custom_formats); | |
| 62 } | |
| 63 bool WrenchToolbarButton::AreDropTypesRequired() { | |
| 64 return allow_extension_dragging_ ? | |
| 65 BrowserActionDragData::AreDropTypesRequired() : | |
| 66 views::View::AreDropTypesRequired(); | |
| 67 } | |
| 68 bool WrenchToolbarButton::CanDrop(const ui::OSExchangeData& data) { | |
| 69 return allow_extension_dragging_ ? | |
| 70 BrowserActionDragData::CanDrop(data, | |
| 71 toolbar_view_->browser()->profile()) : | |
| 72 views::View::CanDrop(data); | |
| 73 } | |
| 74 | |
| 75 void WrenchToolbarButton::OnDragEntered(const ui::DropTargetEvent& event) { | |
| 76 DCHECK(allow_extension_dragging_); | |
| 77 DCHECK(!weak_factory_.HasWeakPtrs()); | |
| 78 if (!g_open_wrench_immediately_for_testing) { | |
| 79 base::MessageLoop::current()->PostDelayedTask( | |
| 80 FROM_HERE, | |
| 81 base::Bind(&WrenchToolbarButton::ShowOverflowMenu, | |
| 82 weak_factory_.GetWeakPtr()), | |
| 83 base::TimeDelta::FromMilliseconds(views::GetMenuShowDelay())); | |
| 84 } else { | |
| 85 ShowOverflowMenu(); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 int WrenchToolbarButton::OnDragUpdated(const ui::DropTargetEvent& event) { | |
| 90 DCHECK(allow_extension_dragging_); | |
| 91 return ui::DragDropTypes::DRAG_MOVE; | |
| 92 } | |
| 93 | |
| 94 void WrenchToolbarButton::OnDragExited() { | |
| 95 DCHECK(allow_extension_dragging_); | |
| 96 weak_factory_.InvalidateWeakPtrs(); | |
| 97 } | |
| 98 | |
| 99 int WrenchToolbarButton::OnPerformDrop(const ui::DropTargetEvent& event) { | |
| 100 DCHECK(allow_extension_dragging_); | |
| 101 return ui::DragDropTypes::DRAG_MOVE; | |
| 102 } | |
| 103 | |
| 104 void WrenchToolbarButton::ShowOverflowMenu() { | |
| 105 toolbar_view_->ShowAppMenu(true); // For drop. | |
| 106 } | |
| OLD | NEW |