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/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
21 | 21 |
22 // static | |
23 const char ButtonDropDown::kViewClassName[] = | |
24 "ui/views/controls/button/ButtonDropDown"; | |
25 | |
26 // How long to wait before showing the menu. | 22 // How long to wait before showing the menu. |
23 // TODO(gbillock): replace with virtual method. | |
Peter Kasting
2013/11/19 02:28:50
Why do we need a virtual method?
Greg Billock
2013/11/20 00:59:03
This pollutes the global namespace -- should be a
Peter Kasting
2013/11/20 01:10:20
OK, I'm not looking at the WrenchMenu code. At le
Greg Billock
2013/11/20 23:13:23
Done.
| |
27 const int kMenuTimerDelay = 500; | 24 const int kMenuTimerDelay = 500; |
28 | 25 |
29 //////////////////////////////////////////////////////////////////////////////// | 26 ToolbarButton::ToolbarButton(views::ButtonListener* listener, |
30 // | 27 ui::MenuModel* model) |
31 // ButtonDropDown - constructors, destructors, initialization, cleanup | 28 : views::LabelButton(listener, string16()), |
32 // | |
33 //////////////////////////////////////////////////////////////////////////////// | |
34 | |
35 ButtonDropDown::ButtonDropDown(views::ButtonListener* listener, | |
36 ui::MenuModel* model) | |
37 : views::ImageButton(listener), | |
38 model_(model), | 29 model_(model), |
39 menu_showing_(false), | 30 menu_showing_(false), |
40 y_position_on_lbuttondown_(0), | 31 y_position_on_lbuttondown_(0), |
41 show_menu_factory_(this) { | 32 show_menu_factory_(this) {} |
Peter Kasting
2013/11/19 02:28:50
Nit: I slightly prefer the original "linebreak bet
Greg Billock
2013/11/20 00:59:03
Done.
| |
42 set_context_menu_controller(this); | 33 |
34 ToolbarButton::~ToolbarButton() {} | |
35 | |
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); | |
Peter Kasting
2013/11/19 02:28:50
Don't we also need to set painters for the other f
Greg Billock
2013/11/20 00:59:03
I think the default painter is fine since the tool
Peter Kasting
2013/11/20 01:10:20
I know, but the default painters will be drawing s
Greg Billock
2013/11/20 23:13:23
The insets get established by the border. I think
| |
52 set_border(border); | |
43 } | 53 } |
44 | 54 |
45 ButtonDropDown::~ButtonDropDown() { | 55 void ToolbarButton::ButtonPressed(views::Button* sender, |
46 } | 56 const ui::Event& event) {} |
47 | 57 |
48 void ButtonDropDown::ClearPendingMenu() { | 58 |
59 void ToolbarButton::ClearPendingMenu() { | |
49 show_menu_factory_.InvalidateWeakPtrs(); | 60 show_menu_factory_.InvalidateWeakPtrs(); |
50 } | 61 } |
51 | 62 |
52 bool ButtonDropDown::IsMenuShowing() const { | 63 bool ToolbarButton::IsMenuShowing() const { |
53 return menu_showing_; | 64 return menu_showing_; |
54 } | 65 } |
55 | 66 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { |
56 //////////////////////////////////////////////////////////////////////////////// | |
57 // | |
58 // ButtonDropDown - Events | |
59 // | |
60 //////////////////////////////////////////////////////////////////////////////// | |
61 | |
62 bool ButtonDropDown::OnMousePressed(const ui::MouseEvent& event) { | |
63 if (enabled() && ShouldShowMenu() && | 67 if (enabled() && ShouldShowMenu() && |
64 IsTriggerableEvent(event) && HitTestPoint(event.location())) { | 68 IsTriggerableEvent(event) && HitTestPoint(event.location())) { |
65 // Store the y pos of the mouse coordinates so we can use them later to | 69 // 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 | 70 // determine if the user dragged the mouse down (which should pop up the |
67 // drag down menu immediately, instead of waiting for the timer) | 71 // drag down menu immediately, instead of waiting for the timer) |
68 y_position_on_lbuttondown_ = event.y(); | 72 y_position_on_lbuttondown_ = event.y(); |
69 | 73 |
70 // Schedule a task that will show the menu. | 74 // Schedule a task that will show the menu. |
71 base::MessageLoop::current()->PostDelayedTask( | 75 base::MessageLoop::current()->PostDelayedTask( |
72 FROM_HERE, | 76 FROM_HERE, |
73 base::Bind(&ButtonDropDown::ShowDropDownMenu, | 77 base::Bind(&ToolbarButton::ShowDropDownMenu, |
74 show_menu_factory_.GetWeakPtr(), | 78 show_menu_factory_.GetWeakPtr(), |
75 ui::GetMenuSourceTypeForEvent(event)), | 79 ui::GetMenuSourceTypeForEvent(event)), |
76 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 80 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
77 } | 81 } |
78 return ImageButton::OnMousePressed(event); | 82 return LabelButton::OnMousePressed(event); |
79 } | 83 } |
80 | 84 |
81 bool ButtonDropDown::OnMouseDragged(const ui::MouseEvent& event) { | 85 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
82 bool result = views::ImageButton::OnMouseDragged(event); | 86 bool result = LabelButton::OnMouseDragged(event); |
83 | 87 |
84 if (show_menu_factory_.HasWeakPtrs()) { | 88 if (show_menu_factory_.HasWeakPtrs()) { |
85 // If the mouse is dragged to a y position lower than where it was when | 89 // 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 | 90 // clicked then we should not wait for the menu to appear but show |
87 // it immediately. | 91 // it immediately. |
88 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { | 92 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { |
89 show_menu_factory_.InvalidateWeakPtrs(); | 93 show_menu_factory_.InvalidateWeakPtrs(); |
90 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); | 94 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); |
91 } | 95 } |
92 } | 96 } |
93 | 97 |
94 return result; | 98 return result; |
95 } | 99 } |
96 | 100 |
97 void ButtonDropDown::OnMouseReleased(const ui::MouseEvent& event) { | 101 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { |
98 if (IsTriggerableEvent(event) || | 102 if (IsTriggerableEvent(event) || |
99 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { | 103 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { |
100 views::ImageButton::OnMouseReleased(event); | 104 LabelButton::OnMouseReleased(event); |
101 } | 105 } |
102 | 106 |
103 if (IsTriggerableEvent(event)) | 107 if (IsTriggerableEvent(event)) |
104 show_menu_factory_.InvalidateWeakPtrs(); | 108 show_menu_factory_.InvalidateWeakPtrs(); |
105 } | 109 } |
106 | 110 |
107 const char* ButtonDropDown::GetClassName() const { | 111 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { |
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. | 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 | 113 // A right click release triggers an exit event. We want to |
114 // remain in a PUSHED state until the drop down menu closes. | 114 // remain in a PUSHED state until the drop down menu closes. |
115 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) | 115 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) |
116 SetState(STATE_NORMAL); | 116 SetState(STATE_NORMAL); |
117 } | 117 } |
118 | 118 |
119 void ButtonDropDown::OnGestureEvent(ui::GestureEvent* event) { | 119 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { |
120 if (menu_showing_) { | 120 if (menu_showing_) { |
121 // While dropdown menu is showing the button should not handle gestures. | 121 // While dropdown menu is showing the button should not handle gestures. |
122 event->StopPropagation(); | 122 event->StopPropagation(); |
123 return; | 123 return; |
124 } | 124 } |
125 | 125 |
126 ImageButton::OnGestureEvent(event); | 126 LabelButton::OnGestureEvent(event); |
127 } | 127 } |
128 | 128 |
129 void ButtonDropDown::GetAccessibleState(ui::AccessibleViewState* state) { | 129 void ToolbarButton::GetAccessibleState(ui::AccessibleViewState* state) { |
130 views::CustomButton::GetAccessibleState(state); | 130 CustomButton::GetAccessibleState(state); |
131 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN; | 131 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN; |
132 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 132 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
133 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; | 133 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; |
134 } | 134 } |
135 | 135 |
136 void ButtonDropDown::ShowContextMenuForView(View* source, | 136 gfx::Size ToolbarButton::GetPreferredSize() { |
137 const gfx::Point& point, | 137 gfx::Size label_size = label()->GetPreferredSize(); |
138 ui::MenuSourceType source_type) { | 138 gfx::Size icon_size = image()->GetPreferredSize(); |
139 if (label_size.width() > 0) { | |
140 return gfx::Size(label_size.width() + icon_size.width() + | |
141 LocationBarView::GetItemPadding(), | |
Peter Kasting
2013/11/19 02:28:50
Nit: Prefer to put this entire first arg on the sa
Greg Billock
2013/11/20 00:59:03
Done.
| |
142 icon_size.height()); | |
143 } else { | |
Peter Kasting
2013/11/19 02:28:50
Nit: No else after return.
If you reverse the con
Greg Billock
2013/11/20 00:59:03
Done.
| |
144 return icon_size; | |
145 } | |
146 } | |
147 | |
148 void ToolbarButton::ShowContextMenuForView(View* source, | |
149 const gfx::Point& point, | |
150 ui::MenuSourceType source_type) { | |
139 if (!enabled()) | 151 if (!enabled()) |
140 return; | 152 return; |
141 | 153 |
142 show_menu_factory_.InvalidateWeakPtrs(); | 154 show_menu_factory_.InvalidateWeakPtrs(); |
143 ShowDropDownMenu(source_type); | 155 ShowDropDownMenu(source_type); |
144 } | 156 } |
145 | 157 |
146 bool ButtonDropDown::ShouldEnterPushedState(const ui::Event& event) { | 158 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
147 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 159 // 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. | 160 // Remain in this state while the context menu is open. |
149 return event.type() == ui::ET_GESTURE_TAP || | 161 return event.type() == ui::ET_GESTURE_TAP || |
150 event.type() == ui::ET_GESTURE_TAP_DOWN || | 162 event.type() == ui::ET_GESTURE_TAP_DOWN || |
151 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 163 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
152 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 164 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
153 } | 165 } |
154 | 166 |
155 bool ButtonDropDown::ShouldShowMenu() { | 167 bool ToolbarButton::ShouldShowMenu() { |
156 return true; | 168 return model_.get() != NULL; |
157 } | 169 } |
158 | 170 |
159 void ButtonDropDown::ShowDropDownMenu(ui::MenuSourceType source_type) { | 171 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
160 if (!ShouldShowMenu()) | 172 if (!ShouldShowMenu()) |
161 return; | 173 return; |
162 | 174 |
163 gfx::Rect lb = GetLocalBounds(); | 175 gfx::Rect lb = GetLocalBounds(); |
164 | 176 |
165 // Both the menu position and the menu anchor type change if the UI layout | 177 // Both the menu position and the menu anchor type change if the UI layout |
166 // is right-to-left. | 178 // is right-to-left. |
167 gfx::Point menu_position(lb.origin()); | 179 gfx::Point menu_position(lb.origin()); |
168 menu_position.Offset(0, lb.height() - 1); | 180 menu_position.Offset(0, lb.height() - 1); |
169 if (base::i18n::IsRTL()) | 181 if (base::i18n::IsRTL()) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 | 236 |
225 // Need to explicitly clear mouse handler so that events get sent | 237 // 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 | 238 // 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. | 239 // the first click to other parts of the UI is eaten. |
228 SetMouseHandler(NULL); | 240 SetMouseHandler(NULL); |
229 | 241 |
230 // Set the state back to normal after the drop down menu is closed. | 242 // Set the state back to normal after the drop down menu is closed. |
231 if (state_ != STATE_DISABLED) | 243 if (state_ != STATE_DISABLED) |
232 SetState(STATE_NORMAL); | 244 SetState(STATE_NORMAL); |
233 } | 245 } |
234 | |
OLD | NEW |