Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(490)

Side by Side Diff: chrome/browser/ui/views/toolbar/button_dropdown.cc

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

Powered by Google App Engine
This is Rietveld 408576698