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

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

Powered by Google App Engine
This is Rietveld 408576698