Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / toolbar_button.cc
blob835ccf2aa79112641295f60337ad902f7a8b45f1
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/toolbar_button.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/themes/theme_properties.h"
12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
13 #include "ui/accessibility/ax_view_state.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/models/menu_model.h"
16 #include "ui/base/resource/material_design/material_design_controller.h"
17 #include "ui/base/theme_provider.h"
18 #include "ui/gfx/display.h"
19 #include "ui/gfx/screen.h"
20 #include "ui/strings/grit/ui_strings.h"
21 #include "ui/views/animation/ink_drop_animation_controller.h"
22 #include "ui/views/animation/ink_drop_animation_controller_factory.h"
23 #include "ui/views/controls/button/label_button_border.h"
24 #include "ui/views/controls/menu/menu_item_view.h"
25 #include "ui/views/controls/menu/menu_model_adapter.h"
26 #include "ui/views/controls/menu/menu_runner.h"
27 #include "ui/views/widget/widget.h"
29 ToolbarButton::ToolbarButton(views::ButtonListener* listener,
30 ui::MenuModel* model)
31 : views::LabelButton(listener, base::string16()),
32 model_(model),
33 menu_showing_(false),
34 y_position_on_lbuttondown_(0),
35 show_menu_factory_(this) {
36 ink_drop_animation_controller_ = views::InkDropAnimationControllerFactory::
37 CreateInkDropAnimationController(this);
38 set_context_menu_controller(this);
41 ToolbarButton::~ToolbarButton() {
44 void ToolbarButton::Init() {
45 SetFocusable(false);
46 SetAccessibilityFocusable(true);
47 image()->EnableCanvasFlippingForRTLUI(true);
50 void ToolbarButton::ClearPendingMenu() {
51 show_menu_factory_.InvalidateWeakPtrs();
54 bool ToolbarButton::IsMenuShowing() const {
55 return menu_showing_;
58 gfx::Size ToolbarButton::GetPreferredSize() const {
59 gfx::Size size(image()->GetPreferredSize());
60 gfx::Size label_size = label()->GetPreferredSize();
61 if (label_size.width() > 0) {
62 const int horizontal_item_padding = GetThemeProvider()->GetDisplayProperty(
63 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING);
64 size.Enlarge(label_size.width() + horizontal_item_padding, 0);
66 // For non-material assets the entire size of the button is captured in the
67 // image resource. For Material Design the excess whitespace is being removed
68 // from the image assets. Enlarge the button by the theme provided insets.
69 if (ui::MaterialDesignController::IsModeMaterial()) {
70 ui::ThemeProvider* provider = GetThemeProvider();
71 if (provider && provider->UsingSystemTheme()) {
72 int inset = provider->GetDisplayProperty(
73 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET);
74 size.Enlarge(2 * inset, 2 * inset);
77 return size;
80 void ToolbarButton::Layout() {
81 LabelButton::Layout();
82 LayoutInkDrop();
85 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) {
86 if (enabled() && ShouldShowMenu() &&
87 IsTriggerableEvent(event) && HitTestPoint(event.location())) {
88 // Store the y pos of the mouse coordinates so we can use them later to
89 // determine if the user dragged the mouse down (which should pop up the
90 // drag down menu immediately, instead of waiting for the timer)
91 y_position_on_lbuttondown_ = event.y();
93 // Schedule a task that will show the menu.
94 const int kMenuTimerDelay = 500;
95 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
96 FROM_HERE, base::Bind(&ToolbarButton::ShowDropDownMenu,
97 show_menu_factory_.GetWeakPtr(),
98 ui::GetMenuSourceTypeForEvent(event)),
99 base::TimeDelta::FromMilliseconds(kMenuTimerDelay));
102 ink_drop_animation_controller_->AnimateToState(
103 views::InkDropState::ACTION_PENDING);
105 return LabelButton::OnMousePressed(event);
108 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) {
109 bool result = LabelButton::OnMouseDragged(event);
111 if (show_menu_factory_.HasWeakPtrs()) {
112 // If the mouse is dragged to a y position lower than where it was when
113 // clicked then we should not wait for the menu to appear but show
114 // it immediately.
115 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) {
116 show_menu_factory_.InvalidateWeakPtrs();
117 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
121 return result;
124 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) {
125 if (IsTriggerableEvent(event) ||
126 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) {
127 LabelButton::OnMouseReleased(event);
130 if (IsTriggerableEvent(event))
131 show_menu_factory_.InvalidateWeakPtrs();
133 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN);
136 void ToolbarButton::OnMouseCaptureLost() {
139 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) {
140 // Starting a drag results in a MouseExited, we need to ignore it.
141 // A right click release triggers an exit event. We want to
142 // remain in a PUSHED state until the drop down menu closes.
143 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED)
144 SetState(STATE_NORMAL);
147 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) {
148 if (menu_showing_) {
149 // While dropdown menu is showing the button should not handle gestures.
150 event->StopPropagation();
151 return;
154 LabelButton::OnGestureEvent(event);
156 views::InkDropState ink_drop_state = views::InkDropState::HIDDEN;
157 switch (event->type()) {
158 case ui::ET_GESTURE_TAP_DOWN:
159 ink_drop_state = views::InkDropState::ACTION_PENDING;
160 // The ui::ET_GESTURE_TAP_DOWN event needs to be marked as handled so that
161 // subsequent events for the gesture are sent to |this|.
162 event->SetHandled();
163 break;
164 case ui::ET_GESTURE_LONG_PRESS:
165 ink_drop_state = views::InkDropState::SLOW_ACTION;
166 break;
167 case ui::ET_GESTURE_TAP:
168 ink_drop_state = views::InkDropState::QUICK_ACTION;
169 break;
170 case ui::ET_GESTURE_END:
171 case ui::ET_GESTURE_TAP_CANCEL:
172 ink_drop_state = views::InkDropState::HIDDEN;
173 break;
174 default:
175 return;
177 ink_drop_animation_controller_->AnimateToState(ink_drop_state);
180 void ToolbarButton::GetAccessibleState(ui::AXViewState* state) {
181 CustomButton::GetAccessibleState(state);
182 state->role = ui::AX_ROLE_BUTTON_DROP_DOWN;
183 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
184 state->AddStateFlag(ui::AX_STATE_HASPOPUP);
187 scoped_ptr<views::LabelButtonBorder>
188 ToolbarButton::CreateDefaultBorder() const {
189 scoped_ptr<views::LabelButtonBorder> border =
190 views::LabelButton::CreateDefaultBorder();
192 ui::ThemeProvider* provider = GetThemeProvider();
193 if (provider && provider->UsingSystemTheme()) {
194 // Theme provided insets.
195 int inset = provider->GetDisplayProperty(
196 ThemeProperties::PROPERTY_TOOLBAR_BUTTON_BORDER_INSET);
197 border->set_insets(gfx::Insets(inset, inset, inset, inset));
200 return border.Pass();
203 void ToolbarButton::ShowContextMenuForView(View* source,
204 const gfx::Point& point,
205 ui::MenuSourceType source_type) {
206 if (!enabled())
207 return;
209 show_menu_factory_.InvalidateWeakPtrs();
210 ShowDropDownMenu(source_type);
213 void ToolbarButton::AddInkDropLayer(ui::Layer* ink_drop_layer) {
214 SetPaintToLayer(true);
215 image()->SetPaintToLayer(true);
216 image()->SetFillsBoundsOpaquely(false);
218 layer()->Add(ink_drop_layer);
219 layer()->StackAtBottom(ink_drop_layer);
222 void ToolbarButton::RemoveInkDropLayer(ui::Layer* ink_drop_layer) {
223 layer()->Remove(ink_drop_layer);
225 image()->SetFillsBoundsOpaquely(true);
226 image()->SetPaintToLayer(false);
227 SetPaintToLayer(false);
230 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) {
231 // Enter PUSHED state on press with Left or Right mouse button or on taps.
232 // Remain in this state while the context menu is open.
233 return event.type() == ui::ET_GESTURE_TAP ||
234 event.type() == ui::ET_GESTURE_TAP_DOWN ||
235 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON |
236 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0);
239 bool ToolbarButton::ShouldShowMenu() {
240 return model_ != NULL;
243 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
244 if (!ShouldShowMenu())
245 return;
247 gfx::Rect lb = GetLocalBounds();
249 // Both the menu position and the menu anchor type change if the UI layout
250 // is right-to-left.
251 gfx::Point menu_position(lb.origin());
252 menu_position.Offset(0, lb.height() - 1);
253 if (base::i18n::IsRTL())
254 menu_position.Offset(lb.width() - 1, 0);
256 View::ConvertPointToScreen(this, &menu_position);
258 #if defined(OS_WIN)
259 int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN);
260 #elif defined(OS_CHROMEOS)
261 // A window won't overlap between displays on ChromeOS.
262 // Use the left bound of the display on which
263 // the menu button exists.
264 gfx::NativeView view = GetWidget()->GetNativeView();
265 gfx::Display display = gfx::Screen::GetScreenFor(
266 view)->GetDisplayNearestWindow(view);
267 int left_bound = display.bounds().x();
268 #else
269 // The window might be positioned over the edge between two screens. We'll
270 // want to position the dropdown on the screen the mouse cursor is on.
271 gfx::NativeView view = GetWidget()->GetNativeView();
272 gfx::Screen* screen = gfx::Screen::GetScreenFor(view);
273 gfx::Display display = screen->GetDisplayNearestPoint(
274 screen->GetCursorScreenPoint());
275 int left_bound = display.bounds().x();
276 #endif
277 if (menu_position.x() < left_bound)
278 menu_position.set_x(left_bound);
280 // Make the button look depressed while the menu is open.
281 SetState(STATE_PRESSED);
283 menu_showing_ = true;
285 // Create and run menu. Display an empty menu if model is NULL.
286 if (model_.get()) {
287 views::MenuModelAdapter menu_delegate(model_.get());
288 menu_delegate.set_triggerable_event_flags(triggerable_event_flags());
289 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu(),
290 views::MenuRunner::HAS_MNEMONICS));
291 views::MenuRunner::RunResult result =
292 menu_runner_->RunMenuAt(GetWidget(),
293 NULL,
294 gfx::Rect(menu_position, gfx::Size(0, 0)),
295 views::MENU_ANCHOR_TOPLEFT,
296 source_type);
297 if (result == views::MenuRunner::MENU_DELETED)
298 return;
299 } else {
300 views::MenuDelegate menu_delegate;
301 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate);
302 menu_runner_.reset(
303 new views::MenuRunner(menu, views::MenuRunner::HAS_MNEMONICS));
304 views::MenuRunner::RunResult result =
305 menu_runner_->RunMenuAt(GetWidget(),
306 NULL,
307 gfx::Rect(menu_position, gfx::Size(0, 0)),
308 views::MENU_ANCHOR_TOPLEFT,
309 source_type);
310 if (result == views::MenuRunner::MENU_DELETED)
311 return;
314 ink_drop_animation_controller_->AnimateToState(views::InkDropState::HIDDEN);
316 menu_showing_ = false;
318 // Need to explicitly clear mouse handler so that events get sent
319 // properly after the menu finishes running. If we don't do this, then
320 // the first click to other parts of the UI is eaten.
321 SetMouseHandler(NULL);
323 // Set the state back to normal after the drop down menu is closed.
324 if (state_ != STATE_DISABLED)
325 SetState(STATE_NORMAL);
328 void ToolbarButton::LayoutInkDrop() {
329 ink_drop_animation_controller_->SetInkDropSize(size());
332 const char* ToolbarButton::GetClassName() const {
333 return "ToolbarButton";