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.
4
5 #include "chrome/browser/ui/views/toolbar/toolbar_button.h"
6
7 #include "base/bind.h"
8 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
9 #include "grit/theme_resources.h"
10 #include "grit/ui_strings.h"
11 #include "ui/base/accessibility/accessible_view_state.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/base/models/menu_model.h"
14 #include "ui/gfx/display.h"
15 #include "ui/gfx/screen.h"
16 #include "ui/views/controls/button/label_button_border.h"
17 #include "ui/views/controls/menu/menu_item_view.h"
18 #include "ui/views/controls/menu/menu_model_adapter.h"
19 #include "ui/views/controls/menu/menu_runner.h"
20 #include "ui/views/widget/widget.h"
21
ToolbarButton(views::ButtonListener * listener,ui::MenuModel * model)22 ToolbarButton::ToolbarButton(views::ButtonListener* listener,
23 ui::MenuModel* model)
24 : views::LabelButton(listener, base::string16()),
25 model_(model),
26 menu_showing_(false),
27 y_position_on_lbuttondown_(0),
28 show_menu_factory_(this) {
29 set_context_menu_controller(this);
30 }
31
~ToolbarButton()32 ToolbarButton::~ToolbarButton() {
33 }
34
Init()35 void ToolbarButton::Init() {
36 SetFocusable(false);
37 SetAccessibilityFocusable(true);
38 image()->EnableCanvasFlippingForRTLUI(true);
39
40 // Provides the hover/pressed style used by buttons in the toolbar.
41 views::LabelButtonBorder* border =
42 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON);
43 const int kHoverImages[] = IMAGE_GRID(IDR_TOOLBAR_BUTTON_HOVER);
44 border->SetPainter(false, views::Button::STATE_HOVERED,
45 views::Painter::CreateImageGridPainter(
46 kHoverImages));
47 const int kPressedImages[] = IMAGE_GRID(IDR_TOOLBAR_BUTTON_PRESSED);
48 border->SetPainter(false, views::Button::STATE_PRESSED,
49 views::Painter::CreateImageGridPainter(
50 kPressedImages));
51 set_border(border);
52 }
53
ClearPendingMenu()54 void ToolbarButton::ClearPendingMenu() {
55 show_menu_factory_.InvalidateWeakPtrs();
56 }
57
IsMenuShowing() const58 bool ToolbarButton::IsMenuShowing() const {
59 return menu_showing_;
60 }
61
GetPreferredSize()62 gfx::Size ToolbarButton::GetPreferredSize() {
63 gfx::Size size(image()->GetPreferredSize());
64 gfx::Size label_size = label()->GetPreferredSize();
65 if (label_size.width() > 0)
66 size.Enlarge(label_size.width() + LocationBarView::GetItemPadding(), 0);
67 return size;
68 }
69
OnMousePressed(const ui::MouseEvent & event)70 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) {
71 if (enabled() && ShouldShowMenu() &&
72 IsTriggerableEvent(event) && HitTestPoint(event.location())) {
73 // Store the y pos of the mouse coordinates so we can use them later to
74 // determine if the user dragged the mouse down (which should pop up the
75 // drag down menu immediately, instead of waiting for the timer)
76 y_position_on_lbuttondown_ = event.y();
77
78 // Schedule a task that will show the menu.
79 const int kMenuTimerDelay = 500;
80 base::MessageLoop::current()->PostDelayedTask(
81 FROM_HERE,
82 base::Bind(&ToolbarButton::ShowDropDownMenu,
83 show_menu_factory_.GetWeakPtr(),
84 ui::GetMenuSourceTypeForEvent(event)),
85 base::TimeDelta::FromMilliseconds(kMenuTimerDelay));
86 }
87 return LabelButton::OnMousePressed(event);
88 }
89
OnMouseDragged(const ui::MouseEvent & event)90 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) {
91 bool result = LabelButton::OnMouseDragged(event);
92
93 if (show_menu_factory_.HasWeakPtrs()) {
94 // If the mouse is dragged to a y position lower than where it was when
95 // clicked then we should not wait for the menu to appear but show
96 // it immediately.
97 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) {
98 show_menu_factory_.InvalidateWeakPtrs();
99 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
100 }
101 }
102
103 return result;
104 }
105
OnMouseReleased(const ui::MouseEvent & event)106 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) {
107 if (IsTriggerableEvent(event) ||
108 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) {
109 LabelButton::OnMouseReleased(event);
110 }
111
112 if (IsTriggerableEvent(event))
113 show_menu_factory_.InvalidateWeakPtrs();
114 }
115
OnMouseCaptureLost()116 void ToolbarButton::OnMouseCaptureLost() {
117 }
118
OnMouseExited(const ui::MouseEvent & event)119 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) {
120 // Starting a drag results in a MouseExited, we need to ignore it.
121 // A right click release triggers an exit event. We want to
122 // remain in a PUSHED state until the drop down menu closes.
123 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED)
124 SetState(STATE_NORMAL);
125 }
126
OnGestureEvent(ui::GestureEvent * event)127 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) {
128 if (menu_showing_) {
129 // While dropdown menu is showing the button should not handle gestures.
130 event->StopPropagation();
131 return;
132 }
133
134 LabelButton::OnGestureEvent(event);
135 }
136
GetAccessibleState(ui::AccessibleViewState * state)137 void ToolbarButton::GetAccessibleState(ui::AccessibleViewState* state) {
138 CustomButton::GetAccessibleState(state);
139 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN;
140 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
141 state->state = ui::AccessibilityTypes::STATE_HASPOPUP;
142 }
143
ShowContextMenuForView(View * source,const gfx::Point & point,ui::MenuSourceType source_type)144 void ToolbarButton::ShowContextMenuForView(View* source,
145 const gfx::Point& point,
146 ui::MenuSourceType source_type) {
147 if (!enabled())
148 return;
149
150 show_menu_factory_.InvalidateWeakPtrs();
151 ShowDropDownMenu(source_type);
152 }
153
ShouldEnterPushedState(const ui::Event & event)154 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) {
155 // Enter PUSHED state on press with Left or Right mouse button or on taps.
156 // Remain in this state while the context menu is open.
157 return event.type() == ui::ET_GESTURE_TAP ||
158 event.type() == ui::ET_GESTURE_TAP_DOWN ||
159 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON |
160 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0);
161 }
162
ShouldShowMenu()163 bool ToolbarButton::ShouldShowMenu() {
164 return model_ != NULL;
165 }
166
ShowDropDownMenu(ui::MenuSourceType source_type)167 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
168 if (!ShouldShowMenu())
169 return;
170
171 gfx::Rect lb = GetLocalBounds();
172
173 // Both the menu position and the menu anchor type change if the UI layout
174 // is right-to-left.
175 gfx::Point menu_position(lb.origin());
176 menu_position.Offset(0, lb.height() - 1);
177 if (base::i18n::IsRTL())
178 menu_position.Offset(lb.width() - 1, 0);
179
180 View::ConvertPointToScreen(this, &menu_position);
181
182 #if defined(OS_WIN)
183 int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN);
184 #elif defined(OS_CHROMEOS)
185 // A window won't overlap between displays on ChromeOS.
186 // Use the left bound of the display on which
187 // the menu button exists.
188 gfx::NativeView view = GetWidget()->GetNativeView();
189 gfx::Display display = gfx::Screen::GetScreenFor(
190 view)->GetDisplayNearestWindow(view);
191 int left_bound = display.bounds().x();
192 #else
193 int left_bound = 0;
194 NOTIMPLEMENTED();
195 #endif
196 if (menu_position.x() < left_bound)
197 menu_position.set_x(left_bound);
198
199 // Make the button look depressed while the menu is open.
200 SetState(STATE_PRESSED);
201
202 menu_showing_ = true;
203
204 // Create and run menu. Display an empty menu if model is NULL.
205 if (model_.get()) {
206 views::MenuModelAdapter menu_delegate(model_.get());
207 menu_delegate.set_triggerable_event_flags(triggerable_event_flags());
208 menu_runner_.reset(new views::MenuRunner(menu_delegate.CreateMenu()));
209 views::MenuRunner::RunResult result =
210 menu_runner_->RunMenuAt(GetWidget(), NULL,
211 gfx::Rect(menu_position, gfx::Size(0, 0)),
212 views::MenuItemView::TOPLEFT,
213 source_type,
214 views::MenuRunner::HAS_MNEMONICS);
215 if (result == views::MenuRunner::MENU_DELETED)
216 return;
217 } else {
218 views::MenuDelegate menu_delegate;
219 views::MenuItemView* menu = new views::MenuItemView(&menu_delegate);
220 menu_runner_.reset(new views::MenuRunner(menu));
221 views::MenuRunner::RunResult result =
222 menu_runner_->RunMenuAt(GetWidget(), NULL,
223 gfx::Rect(menu_position, gfx::Size(0, 0)),
224 views::MenuItemView::TOPLEFT,
225 source_type,
226 views::MenuRunner::HAS_MNEMONICS);
227 if (result == views::MenuRunner::MENU_DELETED)
228 return;
229 }
230
231 menu_showing_ = false;
232
233 // Need to explicitly clear mouse handler so that events get sent
234 // properly after the menu finishes running. If we don't do this, then
235 // the first click to other parts of the UI is eaten.
236 SetMouseHandler(NULL);
237
238 // Set the state back to normal after the drop down menu is closed.
239 if (state_ != STATE_DISABLED)
240 SetState(STATE_NORMAL);
241 }
242