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