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

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: tweaks 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/ui/views/toolbar/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 "chrome/browser/ui/views/location_bar/location_bar_view.h"
9 #include "base/message_loop/message_loop.h" 9 #include "grit/theme_resources.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "grit/ui_strings.h" 10 #include "grit/ui_strings.h"
12 #include "ui/base/accessibility/accessible_view_state.h" 11 #include "ui/base/accessibility/accessible_view_state.h"
13 #include "ui/base/l10n/l10n_util.h" 12 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/models/menu_model.h" 13 #include "ui/base/models/menu_model.h"
15 #include "ui/gfx/display.h" 14 #include "ui/gfx/display.h"
16 #include "ui/gfx/screen.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" 17 #include "ui/views/controls/menu/menu_item_view.h"
18 #include "ui/views/controls/menu/menu_model_adapter.h" 18 #include "ui/views/controls/menu/menu_model_adapter.h"
19 #include "ui/views/controls/menu/menu_runner.h" 19 #include "ui/views/controls/menu/menu_runner.h"
20 #include "ui/views/focus_border.h"
20 #include "ui/views/widget/widget.h" 21 #include "ui/views/widget/widget.h"
21 22
22 // static 23 ToolbarButton::ToolbarButton(views::ButtonListener* listener,
23 const char ButtonDropDown::kViewClassName[] = 24 ui::MenuModel* model)
24 "ui/views/controls/button/ButtonDropDown"; 25 : views::LabelButton(listener, string16()),
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), 26 model_(model),
39 menu_showing_(false), 27 menu_showing_(false),
40 y_position_on_lbuttondown_(0), 28 y_position_on_lbuttondown_(0),
29 margin_left_(0),
41 show_menu_factory_(this) { 30 show_menu_factory_(this) {
42 set_context_menu_controller(this);
43 } 31 }
44 32
45 ButtonDropDown::~ButtonDropDown() { 33 ToolbarButton::~ToolbarButton() {
46 } 34 }
47 35
48 void ButtonDropDown::ClearPendingMenu() { 36 void ToolbarButton::Init() {
37 set_focusable(true);
38
39 // Provides the hover/pressed style used by buttons in the toolbar.
40 views::LabelButtonBorder* border =
41 new views::LabelButtonBorder(views::Button::STYLE_BUTTON);
42 const int kHoverImages[] = IMAGE_GRID(IDR_TOOLBAR_BUTTON_HOVER);
43 border->SetPainter(false, views::Button::STATE_HOVERED,
44 views::Painter::CreateImageGridPainter(
45 kHoverImages));
46 const int kPressedImages[] = IMAGE_GRID(IDR_TOOLBAR_BUTTON_PRESSED);
47 border->SetPainter(false, views::Button::STATE_PRESSED,
48 views::Painter::CreateImageGridPainter(
49 kPressedImages));
50 border->SetPainter(false, views::Button::STATE_NORMAL, NULL);
51 border->SetPainter(true, views::Button::STATE_NORMAL, NULL);
52 set_border(border);
53 }
54
55 void ToolbarButton::ClearPendingMenu() {
49 show_menu_factory_.InvalidateWeakPtrs(); 56 show_menu_factory_.InvalidateWeakPtrs();
50 } 57 }
51 58
52 bool ButtonDropDown::IsMenuShowing() const { 59 bool ToolbarButton::IsMenuShowing() const {
53 return menu_showing_; 60 return menu_showing_;
54 } 61 }
55 62
56 //////////////////////////////////////////////////////////////////////////////// 63 gfx::Size ToolbarButton::GetPreferredSize() {
57 // 64 gfx::Size size(image()->GetPreferredSize());
58 // ButtonDropDown - Events 65 gfx::Size label_size = label()->GetPreferredSize();
59 // 66 if (label_size.width() > 0)
60 //////////////////////////////////////////////////////////////////////////////// 67 size.Enlarge(label_size.width() + LocationBarView::GetItemPadding(), 0);
68 return size;
69 }
61 70
62 bool ButtonDropDown::OnMousePressed(const ui::MouseEvent& event) { 71 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) {
63 if (enabled() && ShouldShowMenu() && 72 if (enabled() && ShouldShowMenu() &&
64 IsTriggerableEvent(event) && HitTestPoint(event.location())) { 73 IsTriggerableEvent(event) && HitTestPoint(event.location())) {
65 // Store the y pos of the mouse coordinates so we can use them later to 74 // 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 75 // determine if the user dragged the mouse down (which should pop up the
67 // drag down menu immediately, instead of waiting for the timer) 76 // drag down menu immediately, instead of waiting for the timer)
68 y_position_on_lbuttondown_ = event.y(); 77 y_position_on_lbuttondown_ = event.y();
69 78
70 // Schedule a task that will show the menu. 79 // Schedule a task that will show the menu.
80 const int kMenuTimerDelay = 500;
71 base::MessageLoop::current()->PostDelayedTask( 81 base::MessageLoop::current()->PostDelayedTask(
72 FROM_HERE, 82 FROM_HERE,
73 base::Bind(&ButtonDropDown::ShowDropDownMenu, 83 base::Bind(&ToolbarButton::ShowDropDownMenu,
74 show_menu_factory_.GetWeakPtr(), 84 show_menu_factory_.GetWeakPtr(),
75 ui::GetMenuSourceTypeForEvent(event)), 85 ui::GetMenuSourceTypeForEvent(event)),
76 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); 86 base::TimeDelta::FromMilliseconds(kMenuTimerDelay));
77 } 87 }
78 return ImageButton::OnMousePressed(event); 88 return LabelButton::OnMousePressed(event);
79 } 89 }
80 90
81 bool ButtonDropDown::OnMouseDragged(const ui::MouseEvent& event) { 91 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) {
82 bool result = views::ImageButton::OnMouseDragged(event); 92 bool result = LabelButton::OnMouseDragged(event);
83 93
84 if (show_menu_factory_.HasWeakPtrs()) { 94 if (show_menu_factory_.HasWeakPtrs()) {
85 // If the mouse is dragged to a y position lower than where it was when 95 // 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 96 // clicked then we should not wait for the menu to appear but show
87 // it immediately. 97 // it immediately.
88 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { 98 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) {
89 show_menu_factory_.InvalidateWeakPtrs(); 99 show_menu_factory_.InvalidateWeakPtrs();
90 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); 100 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
91 } 101 }
92 } 102 }
93 103
94 return result; 104 return result;
95 } 105 }
96 106
97 void ButtonDropDown::OnMouseReleased(const ui::MouseEvent& event) { 107 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) {
98 if (IsTriggerableEvent(event) || 108 if (IsTriggerableEvent(event) ||
99 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { 109 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) {
100 views::ImageButton::OnMouseReleased(event); 110 LabelButton::OnMouseReleased(event);
101 } 111 }
102 112
103 if (IsTriggerableEvent(event)) 113 if (IsTriggerableEvent(event))
104 show_menu_factory_.InvalidateWeakPtrs(); 114 show_menu_factory_.InvalidateWeakPtrs();
105 } 115 }
106 116
107 const char* ButtonDropDown::GetClassName() const { 117 void ToolbarButton::OnMouseCaptureLost() {
108 return kViewClassName;
109 } 118 }
110 119
111 void ButtonDropDown::OnMouseExited(const ui::MouseEvent& event) { 120 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) {
112 // Starting a drag results in a MouseExited, we need to ignore it. 121 // Starting a drag results in a MouseExited, we need to ignore it.
113 // A right click release triggers an exit event. We want to 122 // A right click release triggers an exit event. We want to
114 // remain in a PUSHED state until the drop down menu closes. 123 // remain in a PUSHED state until the drop down menu closes.
115 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) 124 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED)
116 SetState(STATE_NORMAL); 125 SetState(STATE_NORMAL);
117 } 126 }
118 127
119 void ButtonDropDown::OnGestureEvent(ui::GestureEvent* event) { 128 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) {
120 if (menu_showing_) { 129 if (menu_showing_) {
121 // While dropdown menu is showing the button should not handle gestures. 130 // While dropdown menu is showing the button should not handle gestures.
122 event->StopPropagation(); 131 event->StopPropagation();
123 return; 132 return;
124 } 133 }
125 134
126 ImageButton::OnGestureEvent(event); 135 LabelButton::OnGestureEvent(event);
127 } 136 }
128 137
129 void ButtonDropDown::GetAccessibleState(ui::AccessibleViewState* state) { 138 void ToolbarButton::GetAccessibleState(ui::AccessibleViewState* state) {
130 views::CustomButton::GetAccessibleState(state); 139 CustomButton::GetAccessibleState(state);
131 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN; 140 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN;
132 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); 141 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
133 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; 142 state->state = ui::AccessibilityTypes::STATE_HASPOPUP;
134 } 143 }
135 144
136 void ButtonDropDown::ShowContextMenuForView(View* source, 145 gfx::Rect ToolbarButton::GetThemePaintRect() const {
137 const gfx::Point& point, 146 gfx::Rect rect = LabelButton::GetThemePaintRect();
138 ui::MenuSourceType source_type) { 147 if (margin_left_ > 0) {
148 rect = gfx::Rect(rect.x() + margin_left_, rect.y(),
149 rect.width() - margin_left_, rect.height());
150 }
151 return rect;
152 }
153
154 void ToolbarButton::ShowContextMenuForView(View* source,
155 const gfx::Point& point,
156 ui::MenuSourceType source_type) {
139 if (!enabled()) 157 if (!enabled())
140 return; 158 return;
141 159
142 show_menu_factory_.InvalidateWeakPtrs(); 160 show_menu_factory_.InvalidateWeakPtrs();
143 ShowDropDownMenu(source_type); 161 ShowDropDownMenu(source_type);
144 } 162 }
145 163
146 bool ButtonDropDown::ShouldEnterPushedState(const ui::Event& event) { 164 void ToolbarButton::SetLeftMargin(int margin) {
165 // Adjust border insets to follow the margin change,
166 // which will be reflected in where the border is painted
167 // through |GetThemePaintRect|.
168 gfx::Insets insets(border()->GetInsets());
169 static_cast<views::LabelButtonBorder*>(border())->set_insets(
170 gfx::Insets(insets.top(), insets.left() + margin - margin_left_,
171 insets.bottom(), insets.right()));
172
173 // Similarly fiddle the focus border. Value consistent with LabelButton
174 // and TextButton.
175 // TODO(gbillock): Refactor this magic number somewhere global to views,
176 // probably a FocusBorder constant.
177 const int kFocusRectInset = 3;
178 set_focus_border(views::FocusBorder::CreateDashedFocusBorder(
179 kFocusRectInset + margin, kFocusRectInset,
180 kFocusRectInset, kFocusRectInset));
181
182 margin_left_ = margin;
183 InvalidateLayout();
184 }
185
186 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) {
147 // Enter PUSHED state on press with Left or Right mouse button or on taps. 187 // 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. 188 // Remain in this state while the context menu is open.
149 return event.type() == ui::ET_GESTURE_TAP || 189 return event.type() == ui::ET_GESTURE_TAP ||
150 event.type() == ui::ET_GESTURE_TAP_DOWN || 190 event.type() == ui::ET_GESTURE_TAP_DOWN ||
151 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | 191 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON |
152 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); 192 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0);
153 } 193 }
154 194
155 bool ButtonDropDown::ShouldShowMenu() { 195 bool ToolbarButton::ShouldShowMenu() {
156 return true; 196 return model_;
Peter Kasting 2013/11/26 02:25:46 Nit: I'd intended "model_ != NULL"; "!!model_" als
Greg Billock 2013/11/26 16:58:32 Done.
157 } 197 }
158 198
159 void ButtonDropDown::ShowDropDownMenu(ui::MenuSourceType source_type) { 199 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) {
160 if (!ShouldShowMenu()) 200 if (!ShouldShowMenu())
161 return; 201 return;
162 202
163 gfx::Rect lb = GetLocalBounds(); 203 gfx::Rect lb = GetLocalBounds();
164 204
165 // Both the menu position and the menu anchor type change if the UI layout 205 // Both the menu position and the menu anchor type change if the UI layout
166 // is right-to-left. 206 // is right-to-left.
167 gfx::Point menu_position(lb.origin()); 207 gfx::Point menu_position(lb.origin());
168 menu_position.Offset(0, lb.height() - 1); 208 menu_position.Offset(0, lb.height() - 1);
169 if (base::i18n::IsRTL()) 209 if (base::i18n::IsRTL())
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 264
225 // Need to explicitly clear mouse handler so that events get sent 265 // 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 266 // 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. 267 // the first click to other parts of the UI is eaten.
228 SetMouseHandler(NULL); 268 SetMouseHandler(NULL);
229 269
230 // Set the state back to normal after the drop down menu is closed. 270 // Set the state back to normal after the drop down menu is closed.
231 if (state_ != STATE_DISABLED) 271 if (state_ != STATE_DISABLED)
232 SetState(STATE_NORMAL); 272 SetState(STATE_NORMAL);
233 } 273 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698