OLD | NEW |
---|---|
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 "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 label_size = label()->GetPreferredSize(); |
58 // ButtonDropDown - Events | 65 gfx::Size icon_size = image()->GetPreferredSize(); |
59 // | 66 if (label_size.width() == 0) |
60 //////////////////////////////////////////////////////////////////////////////// | 67 return icon_size; |
61 | 68 |
62 bool ButtonDropDown::OnMousePressed(const ui::MouseEvent& event) { | 69 return gfx::Size(label_size.width() + icon_size.width() + |
70 LocationBarView::GetItemPadding(), | |
71 icon_size.height()); | |
Peter Kasting
2013/11/23 00:23:55
Nit: Simpler:
gfx::Size size(image()->GetPrefer
Greg Billock
2013/11/25 17:16:30
I like it. Hadn't noticed Enlarge().
| |
72 } | |
73 | |
74 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { | |
63 if (enabled() && ShouldShowMenu() && | 75 if (enabled() && ShouldShowMenu() && |
64 IsTriggerableEvent(event) && HitTestPoint(event.location())) { | 76 IsTriggerableEvent(event) && HitTestPoint(event.location())) { |
65 // Store the y pos of the mouse coordinates so we can use them later to | 77 // 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 | 78 // determine if the user dragged the mouse down (which should pop up the |
67 // drag down menu immediately, instead of waiting for the timer) | 79 // drag down menu immediately, instead of waiting for the timer) |
68 y_position_on_lbuttondown_ = event.y(); | 80 y_position_on_lbuttondown_ = event.y(); |
69 | 81 |
70 // Schedule a task that will show the menu. | 82 // Schedule a task that will show the menu. |
83 const int kMenuTimerDelay = 500; | |
71 base::MessageLoop::current()->PostDelayedTask( | 84 base::MessageLoop::current()->PostDelayedTask( |
72 FROM_HERE, | 85 FROM_HERE, |
73 base::Bind(&ButtonDropDown::ShowDropDownMenu, | 86 base::Bind(&ToolbarButton::ShowDropDownMenu, |
74 show_menu_factory_.GetWeakPtr(), | 87 show_menu_factory_.GetWeakPtr(), |
75 ui::GetMenuSourceTypeForEvent(event)), | 88 ui::GetMenuSourceTypeForEvent(event)), |
76 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 89 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
77 } | 90 } |
78 return ImageButton::OnMousePressed(event); | 91 return LabelButton::OnMousePressed(event); |
79 } | 92 } |
80 | 93 |
81 bool ButtonDropDown::OnMouseDragged(const ui::MouseEvent& event) { | 94 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
82 bool result = views::ImageButton::OnMouseDragged(event); | 95 bool result = LabelButton::OnMouseDragged(event); |
83 | 96 |
84 if (show_menu_factory_.HasWeakPtrs()) { | 97 if (show_menu_factory_.HasWeakPtrs()) { |
85 // If the mouse is dragged to a y position lower than where it was when | 98 // 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 | 99 // clicked then we should not wait for the menu to appear but show |
87 // it immediately. | 100 // it immediately. |
88 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { | 101 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { |
89 show_menu_factory_.InvalidateWeakPtrs(); | 102 show_menu_factory_.InvalidateWeakPtrs(); |
90 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); | 103 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); |
91 } | 104 } |
92 } | 105 } |
93 | 106 |
94 return result; | 107 return result; |
95 } | 108 } |
96 | 109 |
97 void ButtonDropDown::OnMouseReleased(const ui::MouseEvent& event) { | 110 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { |
98 if (IsTriggerableEvent(event) || | 111 if (IsTriggerableEvent(event) || |
99 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { | 112 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { |
100 views::ImageButton::OnMouseReleased(event); | 113 LabelButton::OnMouseReleased(event); |
101 } | 114 } |
102 | 115 |
103 if (IsTriggerableEvent(event)) | 116 if (IsTriggerableEvent(event)) |
104 show_menu_factory_.InvalidateWeakPtrs(); | 117 show_menu_factory_.InvalidateWeakPtrs(); |
105 } | 118 } |
106 | 119 |
107 const char* ButtonDropDown::GetClassName() const { | 120 void ToolbarButton::OnMouseCaptureLost() { |
108 return kViewClassName; | |
109 } | 121 } |
110 | 122 |
111 void ButtonDropDown::OnMouseExited(const ui::MouseEvent& event) { | 123 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { |
112 // Starting a drag results in a MouseExited, we need to ignore it. | 124 // Starting a drag results in a MouseExited, we need to ignore it. |
113 // A right click release triggers an exit event. We want to | 125 // A right click release triggers an exit event. We want to |
114 // remain in a PUSHED state until the drop down menu closes. | 126 // remain in a PUSHED state until the drop down menu closes. |
115 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) | 127 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) |
116 SetState(STATE_NORMAL); | 128 SetState(STATE_NORMAL); |
117 } | 129 } |
118 | 130 |
119 void ButtonDropDown::OnGestureEvent(ui::GestureEvent* event) { | 131 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { |
120 if (menu_showing_) { | 132 if (menu_showing_) { |
121 // While dropdown menu is showing the button should not handle gestures. | 133 // While dropdown menu is showing the button should not handle gestures. |
122 event->StopPropagation(); | 134 event->StopPropagation(); |
123 return; | 135 return; |
124 } | 136 } |
125 | 137 |
126 ImageButton::OnGestureEvent(event); | 138 LabelButton::OnGestureEvent(event); |
127 } | 139 } |
128 | 140 |
129 void ButtonDropDown::GetAccessibleState(ui::AccessibleViewState* state) { | 141 void ToolbarButton::GetAccessibleState(ui::AccessibleViewState* state) { |
130 views::CustomButton::GetAccessibleState(state); | 142 CustomButton::GetAccessibleState(state); |
131 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN; | 143 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN; |
132 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 144 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
133 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; | 145 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; |
134 } | 146 } |
135 | 147 |
136 void ButtonDropDown::ShowContextMenuForView(View* source, | 148 gfx::Rect ToolbarButton::GetThemePaintRect() const { |
137 const gfx::Point& point, | 149 gfx::Rect rect = LabelButton::GetThemePaintRect(); |
138 ui::MenuSourceType source_type) { | 150 if (margin_left_ > 0) { |
151 rect = gfx::Rect(rect.x() + margin_left_, rect.y(), | |
152 rect.width() - margin_left_, rect.height()); | |
153 } | |
154 return rect; | |
155 } | |
156 | |
157 void ToolbarButton::ShowContextMenuForView(View* source, | |
158 const gfx::Point& point, | |
159 ui::MenuSourceType source_type) { | |
139 if (!enabled()) | 160 if (!enabled()) |
140 return; | 161 return; |
141 | 162 |
142 show_menu_factory_.InvalidateWeakPtrs(); | 163 show_menu_factory_.InvalidateWeakPtrs(); |
143 ShowDropDownMenu(source_type); | 164 ShowDropDownMenu(source_type); |
144 } | 165 } |
145 | 166 |
146 bool ButtonDropDown::ShouldEnterPushedState(const ui::Event& event) { | 167 void ToolbarButton::SetLeftMargin(int margin) { |
168 // Adjust border insets to follow the margin change, | |
169 // which will be reflected in where the border is painted | |
170 // through |GetThemePaintRect|. | |
171 gfx::Insets insets(border()->GetInsets()); | |
172 int margin_delta = margin - margin_left_; | |
Peter Kasting
2013/11/23 00:23:55
Nit: Inline into next statement
Greg Billock
2013/11/25 17:16:30
Done.
| |
173 static_cast<views::LabelButtonBorder*>(border())->set_insets( | |
174 gfx::Insets(insets.top(), insets.left() + margin_delta, | |
Peter Kasting
2013/11/23 00:23:55
Nit: Indent 4, not 2
Greg Billock
2013/11/25 17:16:30
Done.
| |
175 insets.bottom(), insets.right())); | |
176 | |
177 // Similarly fiddle the focus border. | |
178 set_focus_border(views::FocusBorder::CreateDashedFocusBorder( | |
179 3 + margin, 3, 3, 3)); | |
Peter Kasting
2013/11/23 00:23:55
These 3s are magic.
Either the focus border shoul
Greg Billock
2013/11/25 17:16:30
See label_button.cc:150. Also TextButton, where th
| |
180 | |
181 margin_left_ = margin; | |
182 InvalidateLayout(); | |
183 } | |
184 | |
185 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { | |
147 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 186 // 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. | 187 // Remain in this state while the context menu is open. |
149 return event.type() == ui::ET_GESTURE_TAP || | 188 return event.type() == ui::ET_GESTURE_TAP || |
150 event.type() == ui::ET_GESTURE_TAP_DOWN || | 189 event.type() == ui::ET_GESTURE_TAP_DOWN || |
151 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 190 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
152 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 191 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
153 } | 192 } |
154 | 193 |
155 bool ButtonDropDown::ShouldShowMenu() { | 194 bool ToolbarButton::ShouldShowMenu() { |
156 return true; | 195 return model_.get() != NULL; |
Peter Kasting
2013/11/23 00:23:55
Nit: .get() not necessary
Greg Billock
2013/11/25 17:16:30
Done.
| |
157 } | 196 } |
158 | 197 |
159 void ButtonDropDown::ShowDropDownMenu(ui::MenuSourceType source_type) { | 198 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
160 if (!ShouldShowMenu()) | 199 if (!ShouldShowMenu()) |
161 return; | 200 return; |
162 | 201 |
163 gfx::Rect lb = GetLocalBounds(); | 202 gfx::Rect lb = GetLocalBounds(); |
164 | 203 |
165 // Both the menu position and the menu anchor type change if the UI layout | 204 // Both the menu position and the menu anchor type change if the UI layout |
166 // is right-to-left. | 205 // is right-to-left. |
167 gfx::Point menu_position(lb.origin()); | 206 gfx::Point menu_position(lb.origin()); |
168 menu_position.Offset(0, lb.height() - 1); | 207 menu_position.Offset(0, lb.height() - 1); |
169 if (base::i18n::IsRTL()) | 208 if (base::i18n::IsRTL()) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 | 263 |
225 // Need to explicitly clear mouse handler so that events get sent | 264 // 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 | 265 // 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. | 266 // the first click to other parts of the UI is eaten. |
228 SetMouseHandler(NULL); | 267 SetMouseHandler(NULL); |
229 | 268 |
230 // Set the state back to normal after the drop down menu is closed. | 269 // Set the state back to normal after the drop down menu is closed. |
231 if (state_ != STATE_DISABLED) | 270 if (state_ != STATE_DISABLED) |
232 SetState(STATE_NORMAL); | 271 SetState(STATE_NORMAL); |
233 } | 272 } |
OLD | NEW |