OLD | NEW |
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/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
21 | 21 |
22 // static | 22 ToolbarButton::ToolbarButton(views::ButtonListener* listener, |
23 const char ButtonDropDown::kViewClassName[] = | 23 ui::MenuModel* model) |
24 "ui/views/controls/button/ButtonDropDown"; | 24 : 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), | 25 model_(model), |
39 menu_showing_(false), | 26 menu_showing_(false), |
40 y_position_on_lbuttondown_(0), | 27 y_position_on_lbuttondown_(0), |
41 show_menu_factory_(this) { | 28 show_menu_factory_(this) { |
42 set_context_menu_controller(this); | |
43 } | 29 } |
44 | 30 |
45 ButtonDropDown::~ButtonDropDown() { | 31 ToolbarButton::~ToolbarButton() { |
46 } | 32 } |
47 | 33 |
48 void ButtonDropDown::ClearPendingMenu() { | 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 kHoverImages[] = IMAGE_GRID(IDR_TOOLBAR_BUTTON_HOVER); |
| 41 border->SetPainter(false, views::Button::STATE_HOVERED, |
| 42 views::Painter::CreateImageGridPainter( |
| 43 kHoverImages)); |
| 44 const int kPressedImages[] = IMAGE_GRID(IDR_TOOLBAR_BUTTON_PRESSED); |
| 45 border->SetPainter(false, views::Button::STATE_PRESSED, |
| 46 views::Painter::CreateImageGridPainter( |
| 47 kPressedImages)); |
| 48 border->SetPainter(false, views::Button::STATE_NORMAL, NULL); |
| 49 border->SetPainter(true, views::Button::STATE_NORMAL, NULL); |
| 50 set_border(border); |
| 51 } |
| 52 |
| 53 void ToolbarButton::ClearPendingMenu() { |
49 show_menu_factory_.InvalidateWeakPtrs(); | 54 show_menu_factory_.InvalidateWeakPtrs(); |
50 } | 55 } |
51 | 56 |
52 bool ButtonDropDown::IsMenuShowing() const { | 57 bool ToolbarButton::IsMenuShowing() const { |
53 return menu_showing_; | 58 return menu_showing_; |
54 } | 59 } |
55 | 60 |
56 //////////////////////////////////////////////////////////////////////////////// | 61 gfx::Size ToolbarButton::GetPreferredSize() { |
57 // | 62 gfx::Size size(image()->GetPreferredSize()); |
58 // ButtonDropDown - Events | 63 gfx::Size label_size = label()->GetPreferredSize(); |
59 // | 64 if (label_size.width() > 0) |
60 //////////////////////////////////////////////////////////////////////////////// | 65 size.Enlarge(label_size.width() + LocationBarView::GetItemPadding(), 0); |
| 66 return size; |
| 67 } |
61 | 68 |
62 bool ButtonDropDown::OnMousePressed(const ui::MouseEvent& event) { | 69 bool ToolbarButton::OnMousePressed(const ui::MouseEvent& event) { |
63 if (enabled() && ShouldShowMenu() && | 70 if (enabled() && ShouldShowMenu() && |
64 IsTriggerableEvent(event) && HitTestPoint(event.location())) { | 71 IsTriggerableEvent(event) && HitTestPoint(event.location())) { |
65 // Store the y pos of the mouse coordinates so we can use them later to | 72 // 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 | 73 // determine if the user dragged the mouse down (which should pop up the |
67 // drag down menu immediately, instead of waiting for the timer) | 74 // drag down menu immediately, instead of waiting for the timer) |
68 y_position_on_lbuttondown_ = event.y(); | 75 y_position_on_lbuttondown_ = event.y(); |
69 | 76 |
70 // Schedule a task that will show the menu. | 77 // Schedule a task that will show the menu. |
| 78 const int kMenuTimerDelay = 500; |
71 base::MessageLoop::current()->PostDelayedTask( | 79 base::MessageLoop::current()->PostDelayedTask( |
72 FROM_HERE, | 80 FROM_HERE, |
73 base::Bind(&ButtonDropDown::ShowDropDownMenu, | 81 base::Bind(&ToolbarButton::ShowDropDownMenu, |
74 show_menu_factory_.GetWeakPtr(), | 82 show_menu_factory_.GetWeakPtr(), |
75 ui::GetMenuSourceTypeForEvent(event)), | 83 ui::GetMenuSourceTypeForEvent(event)), |
76 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 84 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
77 } | 85 } |
78 return ImageButton::OnMousePressed(event); | 86 return LabelButton::OnMousePressed(event); |
79 } | 87 } |
80 | 88 |
81 bool ButtonDropDown::OnMouseDragged(const ui::MouseEvent& event) { | 89 bool ToolbarButton::OnMouseDragged(const ui::MouseEvent& event) { |
82 bool result = views::ImageButton::OnMouseDragged(event); | 90 bool result = LabelButton::OnMouseDragged(event); |
83 | 91 |
84 if (show_menu_factory_.HasWeakPtrs()) { | 92 if (show_menu_factory_.HasWeakPtrs()) { |
85 // If the mouse is dragged to a y position lower than where it was when | 93 // 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 | 94 // clicked then we should not wait for the menu to appear but show |
87 // it immediately. | 95 // it immediately. |
88 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { | 96 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { |
89 show_menu_factory_.InvalidateWeakPtrs(); | 97 show_menu_factory_.InvalidateWeakPtrs(); |
90 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); | 98 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); |
91 } | 99 } |
92 } | 100 } |
93 | 101 |
94 return result; | 102 return result; |
95 } | 103 } |
96 | 104 |
97 void ButtonDropDown::OnMouseReleased(const ui::MouseEvent& event) { | 105 void ToolbarButton::OnMouseReleased(const ui::MouseEvent& event) { |
98 if (IsTriggerableEvent(event) || | 106 if (IsTriggerableEvent(event) || |
99 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { | 107 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { |
100 views::ImageButton::OnMouseReleased(event); | 108 LabelButton::OnMouseReleased(event); |
101 } | 109 } |
102 | 110 |
103 if (IsTriggerableEvent(event)) | 111 if (IsTriggerableEvent(event)) |
104 show_menu_factory_.InvalidateWeakPtrs(); | 112 show_menu_factory_.InvalidateWeakPtrs(); |
105 } | 113 } |
106 | 114 |
107 const char* ButtonDropDown::GetClassName() const { | 115 void ToolbarButton::OnMouseCaptureLost() { |
108 return kViewClassName; | |
109 } | 116 } |
110 | 117 |
111 void ButtonDropDown::OnMouseExited(const ui::MouseEvent& event) { | 118 void ToolbarButton::OnMouseExited(const ui::MouseEvent& event) { |
112 // Starting a drag results in a MouseExited, we need to ignore it. | 119 // Starting a drag results in a MouseExited, we need to ignore it. |
113 // A right click release triggers an exit event. We want to | 120 // A right click release triggers an exit event. We want to |
114 // remain in a PUSHED state until the drop down menu closes. | 121 // remain in a PUSHED state until the drop down menu closes. |
115 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) | 122 if (state_ != STATE_DISABLED && !InDrag() && state_ != STATE_PRESSED) |
116 SetState(STATE_NORMAL); | 123 SetState(STATE_NORMAL); |
117 } | 124 } |
118 | 125 |
119 void ButtonDropDown::OnGestureEvent(ui::GestureEvent* event) { | 126 void ToolbarButton::OnGestureEvent(ui::GestureEvent* event) { |
120 if (menu_showing_) { | 127 if (menu_showing_) { |
121 // While dropdown menu is showing the button should not handle gestures. | 128 // While dropdown menu is showing the button should not handle gestures. |
122 event->StopPropagation(); | 129 event->StopPropagation(); |
123 return; | 130 return; |
124 } | 131 } |
125 | 132 |
126 ImageButton::OnGestureEvent(event); | 133 LabelButton::OnGestureEvent(event); |
127 } | 134 } |
128 | 135 |
129 void ButtonDropDown::GetAccessibleState(ui::AccessibleViewState* state) { | 136 void ToolbarButton::GetAccessibleState(ui::AccessibleViewState* state) { |
130 views::CustomButton::GetAccessibleState(state); | 137 CustomButton::GetAccessibleState(state); |
131 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN; | 138 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN; |
132 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 139 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
133 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; | 140 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; |
134 } | 141 } |
135 | 142 |
136 void ButtonDropDown::ShowContextMenuForView(View* source, | 143 void ToolbarButton::ShowContextMenuForView(View* source, |
137 const gfx::Point& point, | 144 const gfx::Point& point, |
138 ui::MenuSourceType source_type) { | 145 ui::MenuSourceType source_type) { |
139 if (!enabled()) | 146 if (!enabled()) |
140 return; | 147 return; |
141 | 148 |
142 show_menu_factory_.InvalidateWeakPtrs(); | 149 show_menu_factory_.InvalidateWeakPtrs(); |
143 ShowDropDownMenu(source_type); | 150 ShowDropDownMenu(source_type); |
144 } | 151 } |
145 | 152 |
146 bool ButtonDropDown::ShouldEnterPushedState(const ui::Event& event) { | 153 bool ToolbarButton::ShouldEnterPushedState(const ui::Event& event) { |
147 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 154 // 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. | 155 // Remain in this state while the context menu is open. |
149 return event.type() == ui::ET_GESTURE_TAP || | 156 return event.type() == ui::ET_GESTURE_TAP || |
150 event.type() == ui::ET_GESTURE_TAP_DOWN || | 157 event.type() == ui::ET_GESTURE_TAP_DOWN || |
151 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 158 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
152 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 159 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
153 } | 160 } |
154 | 161 |
155 bool ButtonDropDown::ShouldShowMenu() { | 162 bool ToolbarButton::ShouldShowMenu() { |
156 return true; | 163 return model_ != NULL; |
157 } | 164 } |
158 | 165 |
159 void ButtonDropDown::ShowDropDownMenu(ui::MenuSourceType source_type) { | 166 void ToolbarButton::ShowDropDownMenu(ui::MenuSourceType source_type) { |
160 if (!ShouldShowMenu()) | 167 if (!ShouldShowMenu()) |
161 return; | 168 return; |
162 | 169 |
163 gfx::Rect lb = GetLocalBounds(); | 170 gfx::Rect lb = GetLocalBounds(); |
164 | 171 |
165 // Both the menu position and the menu anchor type change if the UI layout | 172 // Both the menu position and the menu anchor type change if the UI layout |
166 // is right-to-left. | 173 // is right-to-left. |
167 gfx::Point menu_position(lb.origin()); | 174 gfx::Point menu_position(lb.origin()); |
168 menu_position.Offset(0, lb.height() - 1); | 175 menu_position.Offset(0, lb.height() - 1); |
169 if (base::i18n::IsRTL()) | 176 if (base::i18n::IsRTL()) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 | 231 |
225 // Need to explicitly clear mouse handler so that events get sent | 232 // 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 | 233 // 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. | 234 // the first click to other parts of the UI is eaten. |
228 SetMouseHandler(NULL); | 235 SetMouseHandler(NULL); |
229 | 236 |
230 // Set the state back to normal after the drop down menu is closed. | 237 // Set the state back to normal after the drop down menu is closed. |
231 if (state_ != STATE_DISABLED) | 238 if (state_ != STATE_DISABLED) |
232 SetState(STATE_NORMAL); | 239 SetState(STATE_NORMAL); |
233 } | 240 } |
OLD | NEW |