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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// 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/toolbar_action_view.h"
#include <string>
#include "base/auto_reset.h"
#include "base/bind.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
#include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/notification_source.h"
#include "grit/theme_resources.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/theme_provider.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/events/event.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_source.h"
#include "ui/resources/grit/ui_resources.h"
#include "ui/views/animation/button_ink_drop_delegate.h"
#include "ui/views/controls/button/label_button_border.h"
#include "ui/views/controls/menu/menu_controller.h"
#include "ui/views/controls/menu/menu_model_adapter.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/mouse_constants.h"
#include "ui/views/resources/grit/views_resources.h"
using views::LabelButtonBorder;
namespace {
// Toolbar action buttons have no insets because the badges are drawn right at
// the edge of the view's area. Other badding (such as centering the icon) is
// handled directly by the Image.
const int kBorderInset = 0;
// The callback to call directly before showing the context menu.
ToolbarActionView::ContextMenuCallback* context_menu_callback_for_test =
nullptr;
} // namespace
////////////////////////////////////////////////////////////////////////////////
// ToolbarActionView
ToolbarActionView::ToolbarActionView(
ToolbarActionViewController* view_controller,
ToolbarActionView::Delegate* delegate)
: MenuButton(base::string16(), this, false),
view_controller_(view_controller),
delegate_(delegate),
called_register_command_(false),
wants_to_run_(false),
menu_(nullptr),
ink_drop_delegate_(new views::ButtonInkDropDelegate(this, this)),
weak_factory_(this) {
set_ink_drop_delegate(ink_drop_delegate_.get());
set_has_ink_drop_action_on_click(true);
set_id(VIEW_ID_BROWSER_ACTION);
view_controller_->SetDelegate(this);
SetHorizontalAlignment(gfx::ALIGN_CENTER);
set_drag_controller(delegate_);
set_context_menu_controller(this);
// If the button is within a menu, we need to make it focusable in order to
// have it accessible via keyboard navigation, but it shouldn't request focus
// (because that would close the menu).
if (delegate_->ShownInsideMenu()) {
set_request_focus_on_press(false);
SetFocusable(true);
}
UpdateState();
}
ToolbarActionView::~ToolbarActionView() {
// Avoid access to a destroyed InkDropDelegate when the |pressed_lock_| is
// destroyed.
set_ink_drop_delegate(nullptr);
view_controller_->SetDelegate(nullptr);
}
void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) {
views::MenuButton::GetAccessibleState(state);
state->role = ui::AX_ROLE_BUTTON;
}
scoped_ptr<LabelButtonBorder> ToolbarActionView::CreateDefaultBorder() const {
scoped_ptr<LabelButtonBorder> border = LabelButton::CreateDefaultBorder();
border->set_insets(gfx::Insets(kBorderInset, kBorderInset,
kBorderInset, kBorderInset));
return border;
}
void ToolbarActionView::OnMouseEntered(const ui::MouseEvent& event) {
delegate_->OnMouseEnteredToolbarActionView();
views::MenuButton::OnMouseEntered(event);
}
bool ToolbarActionView::IsTriggerableEvent(const ui::Event& event) {
return views::MenuButton::IsTriggerableEvent(event) &&
(base::TimeTicks::Now() - popup_closed_time_).InMilliseconds() >
views::kMinimumMsBetweenButtonClicks;
}
SkColor ToolbarActionView::GetInkDropBaseColor() const {
if (delegate_->ShownInsideMenu()) {
return GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_HoverMenuItemBackgroundColor);
}
return GetThemeProvider()->GetColor(
ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON);
}
bool ToolbarActionView::ShouldShowInkDropHover() const {
return !delegate_->ShownInsideMenu() &&
views::MenuButton::ShouldShowInkDropHover();
}
content::WebContents* ToolbarActionView::GetCurrentWebContents() const {
return delegate_->GetCurrentWebContents();
}
void ToolbarActionView::UpdateState() {
content::WebContents* web_contents = GetCurrentWebContents();
if (SessionTabHelper::IdForTab(web_contents) < 0)
return;
if (!view_controller_->IsEnabled(web_contents) &&
!view_controller_->DisabledClickOpensMenu()) {
SetState(views::CustomButton::STATE_DISABLED);
} else if (state() == views::CustomButton::STATE_DISABLED) {
SetState(views::CustomButton::STATE_NORMAL);
}
wants_to_run_ = view_controller_->WantsToRun(web_contents);
gfx::ImageSkia icon(
view_controller_->GetIcon(web_contents,
GetPreferredSize()).AsImageSkia());
if (!icon.isNull())
SetImage(views::Button::STATE_NORMAL, icon);
SetTooltipText(view_controller_->GetTooltip(web_contents));
SetAccessibleName(view_controller_->GetAccessibleName(web_contents));
Layout(); // We need to layout since we may have added an icon as a result.
SchedulePaint();
}
void ToolbarActionView::OnMenuButtonClicked(views::MenuButton* sender,
const gfx::Point& point,
const ui::Event* event) {
if (!view_controller_->IsEnabled(GetCurrentWebContents())) {
// We should only get a button pressed event with a non-enabled action if
// the left-click behavior should open the menu.
DCHECK(view_controller_->DisabledClickOpensMenu());
context_menu_controller()->ShowContextMenuForView(this, point,
ui::MENU_SOURCE_NONE);
} else {
view_controller_->ExecuteAction(true);
}
}
void ToolbarActionView::OnMenuClosed() {
menu_runner_.reset();
menu_ = nullptr;
view_controller_->OnContextMenuClosed();
menu_adapter_.reset();
}
gfx::ImageSkia ToolbarActionView::GetIconForTest() {
return GetImage(views::Button::STATE_NORMAL);
}
void ToolbarActionView::set_context_menu_callback_for_testing(
base::Callback<void(ToolbarActionView*)>* callback) {
context_menu_callback_for_test = callback;
}
gfx::Size ToolbarActionView::GetPreferredSize() const {
return gfx::Size(ToolbarActionsBar::IconWidth(false),
ToolbarActionsBar::IconHeight());
}
bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) {
// views::MenuButton actions are only triggered by left mouse clicks.
if (event.IsOnlyLeftMouseButton() && !pressed_lock_) {
// TODO(bruthig): The ACTION_PENDING triggering logic should be in
// MenuButton::OnPressed() however there is a bug with the pressed state
// logic in MenuButton. See http://crbug.com/567252.
ink_drop_delegate()->OnAction(views::InkDropState::ACTION_PENDING);
}
return MenuButton::OnMousePressed(event);
}
void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) {
// While the dropdown menu is showing, the button should not handle gestures.
if (menu_)
event->StopPropagation();
else
MenuButton::OnGestureEvent(event);
}
void ToolbarActionView::OnDragDone() {
views::MenuButton::OnDragDone();
delegate_->OnToolbarActionViewDragDone();
}
void ToolbarActionView::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
if (details.is_add && !called_register_command_ && GetFocusManager()) {
view_controller_->RegisterCommand();
called_register_command_ = true;
}
MenuButton::ViewHierarchyChanged(details);
}
views::View* ToolbarActionView::GetAsView() {
return this;
}
views::FocusManager* ToolbarActionView::GetFocusManagerForAccelerator() {
return GetFocusManager();
}
views::View* ToolbarActionView::GetReferenceViewForPopup() {
// Browser actions in the overflow menu can still show popups, so we may need
// a reference view other than this button's parent. If so, use the overflow
// view.
return visible() ? this : delegate_->GetOverflowReferenceView();
}
bool ToolbarActionView::IsMenuRunning() const {
return menu_ != nullptr;
}
void ToolbarActionView::OnPopupShown(bool by_user) {
// If this was through direct user action, we press the menu button.
if (by_user) {
// We set the state of the menu button we're using as a reference view,
// which is either this or the overflow reference view.
// This cast is safe because GetReferenceViewForPopup returns either |this|
// or delegate_->GetOverflowReferenceView(), which returns a MenuButton.
views::MenuButton* reference_view =
static_cast<views::MenuButton*>(GetReferenceViewForPopup());
pressed_lock_.reset(new views::MenuButton::PressedLock(reference_view));
}
}
void ToolbarActionView::OnPopupClosed() {
popup_closed_time_ = base::TimeTicks::Now();
pressed_lock_.reset(); // Unpress the menu button if it was pressed.
}
void ToolbarActionView::ShowContextMenuForView(
views::View* source,
const gfx::Point& point,
ui::MenuSourceType source_type) {
if (CloseActiveMenuIfNeeded())
return;
// Otherwise, no other menu is showing, and we can proceed normally.
DoShowContextMenu(source_type);
}
void ToolbarActionView::DoShowContextMenu(
ui::MenuSourceType source_type) {
ui::MenuModel* context_menu_model = view_controller_->GetContextMenu();
// It's possible the action doesn't have a context menu.
if (!context_menu_model)
return;
DCHECK(visible()); // We should never show a context menu for a hidden item.
gfx::Point screen_loc;
ConvertPointToScreen(this, &screen_loc);
int run_types = views::MenuRunner::HAS_MNEMONICS |
views::MenuRunner::CONTEXT_MENU | views::MenuRunner::ASYNC;
if (delegate_->ShownInsideMenu())
run_types |= views::MenuRunner::IS_NESTED;
// RunMenuAt expects a nested menu to be parented by the same widget as the
// already visible menu, in this case the Chrome menu.
views::Widget* parent = delegate_->ShownInsideMenu() ?
delegate_->GetOverflowReferenceView()->GetWidget() :
GetWidget();
// Unretained() is safe here as ToolbarActionView will always outlive the
// menu. Any action that would lead to the deletion of |this| first triggers
// the closing of the menu through lost capture.
menu_adapter_.reset(new views::MenuModelAdapter(
context_menu_model,
base::Bind(&ToolbarActionView::OnMenuClosed, base::Unretained(this))));
menu_ = menu_adapter_->CreateMenu();
menu_runner_.reset(new views::MenuRunner(menu_, run_types));
if (context_menu_callback_for_test)
context_menu_callback_for_test->Run(this);
ignore_result(
menu_runner_->RunMenuAt(parent, this, gfx::Rect(screen_loc, size()),
views::MENU_ANCHOR_TOPLEFT, source_type));
}
bool ToolbarActionView::CloseActiveMenuIfNeeded() {
// If this view is shown inside another menu, there's a possibility that there
// is another context menu showing that we have to close before we can
// activate a different menu.
if (delegate_->ShownInsideMenu()) {
views::MenuController* menu_controller =
views::MenuController::GetActiveInstance();
// If this is shown inside a menu, then there should always be an active
// menu controller.
DCHECK(menu_controller);
if (menu_controller->in_nested_run()) {
// There is another menu showing. Close the outermost menu (since we are
// shown in the same menu, we don't want to close the whole thing).
menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST);
return true;
}
}
return false;
}
|