| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/views/controls/button/button_dropdown.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 IsTriggerableEvent(event) && HitTestPoint(event.location())) { | 63 IsTriggerableEvent(event) && HitTestPoint(event.location())) { |
| 64 // Store the y pos of the mouse coordinates so we can use them later to | 64 // Store the y pos of the mouse coordinates so we can use them later to |
| 65 // determine if the user dragged the mouse down (which should pop up the | 65 // determine if the user dragged the mouse down (which should pop up the |
| 66 // drag down menu immediately, instead of waiting for the timer) | 66 // drag down menu immediately, instead of waiting for the timer) |
| 67 y_position_on_lbuttondown_ = event.y(); | 67 y_position_on_lbuttondown_ = event.y(); |
| 68 | 68 |
| 69 // Schedule a task that will show the menu. | 69 // Schedule a task that will show the menu. |
| 70 base::MessageLoop::current()->PostDelayedTask( | 70 base::MessageLoop::current()->PostDelayedTask( |
| 71 FROM_HERE, | 71 FROM_HERE, |
| 72 base::Bind(&ButtonDropDown::ShowDropDownMenu, | 72 base::Bind(&ButtonDropDown::ShowDropDownMenu, |
| 73 show_menu_factory_.GetWeakPtr()), | 73 show_menu_factory_.GetWeakPtr(), |
| 74 ui::GetMenuSourceTypeForEvent(event)), |
| 74 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); | 75 base::TimeDelta::FromMilliseconds(kMenuTimerDelay)); |
| 75 } | 76 } |
| 76 return ImageButton::OnMousePressed(event); | 77 return ImageButton::OnMousePressed(event); |
| 77 } | 78 } |
| 78 | 79 |
| 79 bool ButtonDropDown::OnMouseDragged(const ui::MouseEvent& event) { | 80 bool ButtonDropDown::OnMouseDragged(const ui::MouseEvent& event) { |
| 80 bool result = ImageButton::OnMouseDragged(event); | 81 bool result = ImageButton::OnMouseDragged(event); |
| 81 | 82 |
| 82 if (show_menu_factory_.HasWeakPtrs()) { | 83 if (show_menu_factory_.HasWeakPtrs()) { |
| 83 // If the mouse is dragged to a y position lower than where it was when | 84 // If the mouse is dragged to a y position lower than where it was when |
| 84 // clicked then we should not wait for the menu to appear but show | 85 // clicked then we should not wait for the menu to appear but show |
| 85 // it immediately. | 86 // it immediately. |
| 86 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { | 87 if (event.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { |
| 87 show_menu_factory_.InvalidateWeakPtrs(); | 88 show_menu_factory_.InvalidateWeakPtrs(); |
| 88 ShowDropDownMenu(); | 89 ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event)); |
| 89 } | 90 } |
| 90 } | 91 } |
| 91 | 92 |
| 92 return result; | 93 return result; |
| 93 } | 94 } |
| 94 | 95 |
| 95 void ButtonDropDown::OnMouseReleased(const ui::MouseEvent& event) { | 96 void ButtonDropDown::OnMouseReleased(const ui::MouseEvent& event) { |
| 96 if (IsTriggerableEvent(event) || | 97 if (IsTriggerableEvent(event) || |
| 97 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { | 98 (event.IsRightMouseButton() && !HitTestPoint(event.location()))) { |
| 98 ImageButton::OnMouseReleased(event); | 99 ImageButton::OnMouseReleased(event); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 125 } | 126 } |
| 126 | 127 |
| 127 void ButtonDropDown::GetAccessibleState(ui::AccessibleViewState* state) { | 128 void ButtonDropDown::GetAccessibleState(ui::AccessibleViewState* state) { |
| 128 CustomButton::GetAccessibleState(state); | 129 CustomButton::GetAccessibleState(state); |
| 129 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN; | 130 state->role = ui::AccessibilityTypes::ROLE_BUTTONDROPDOWN; |
| 130 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); | 131 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); |
| 131 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; | 132 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; |
| 132 } | 133 } |
| 133 | 134 |
| 134 void ButtonDropDown::ShowContextMenuForView(View* source, | 135 void ButtonDropDown::ShowContextMenuForView(View* source, |
| 135 const gfx::Point& point) { | 136 const gfx::Point& point, |
| 137 ui::MenuSourceType source_type) { |
| 136 if (!enabled()) | 138 if (!enabled()) |
| 137 return; | 139 return; |
| 138 | 140 |
| 139 show_menu_factory_.InvalidateWeakPtrs(); | 141 show_menu_factory_.InvalidateWeakPtrs(); |
| 140 ShowDropDownMenu(); | 142 ShowDropDownMenu(source_type); |
| 141 } | 143 } |
| 142 | 144 |
| 143 bool ButtonDropDown::ShouldEnterPushedState(const ui::Event& event) { | 145 bool ButtonDropDown::ShouldEnterPushedState(const ui::Event& event) { |
| 144 // Enter PUSHED state on press with Left or Right mouse button or on taps. | 146 // Enter PUSHED state on press with Left or Right mouse button or on taps. |
| 145 // Remain in this state while the context menu is open. | 147 // Remain in this state while the context menu is open. |
| 146 return event.type() == ui::ET_GESTURE_TAP || | 148 return event.type() == ui::ET_GESTURE_TAP || |
| 147 event.type() == ui::ET_GESTURE_TAP_DOWN || | 149 event.type() == ui::ET_GESTURE_TAP_DOWN || |
| 148 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | | 150 (event.IsMouseEvent() && ((ui::EF_LEFT_MOUSE_BUTTON | |
| 149 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); | 151 ui::EF_RIGHT_MOUSE_BUTTON) & event.flags()) != 0); |
| 150 } | 152 } |
| 151 | 153 |
| 152 bool ButtonDropDown::ShouldShowMenu() { | 154 bool ButtonDropDown::ShouldShowMenu() { |
| 153 return true; | 155 return true; |
| 154 } | 156 } |
| 155 | 157 |
| 156 void ButtonDropDown::ShowDropDownMenu() { | 158 void ButtonDropDown::ShowDropDownMenu(ui::MenuSourceType source_type) { |
| 157 if (!ShouldShowMenu()) | 159 if (!ShouldShowMenu()) |
| 158 return; | 160 return; |
| 159 | 161 |
| 160 gfx::Rect lb = GetLocalBounds(); | 162 gfx::Rect lb = GetLocalBounds(); |
| 161 | 163 |
| 162 // Both the menu position and the menu anchor type change if the UI layout | 164 // Both the menu position and the menu anchor type change if the UI layout |
| 163 // is right-to-left. | 165 // is right-to-left. |
| 164 gfx::Point menu_position(lb.origin()); | 166 gfx::Point menu_position(lb.origin()); |
| 165 menu_position.Offset(0, lb.height() - 1); | 167 menu_position.Offset(0, lb.height() - 1); |
| 166 if (base::i18n::IsRTL()) | 168 if (base::i18n::IsRTL()) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 184 | 186 |
| 185 // Create and run menu. Display an empty menu if model is NULL. | 187 // Create and run menu. Display an empty menu if model is NULL. |
| 186 if (model_.get()) { | 188 if (model_.get()) { |
| 187 MenuModelAdapter menu_delegate(model_.get()); | 189 MenuModelAdapter menu_delegate(model_.get()); |
| 188 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); | 190 menu_delegate.set_triggerable_event_flags(triggerable_event_flags()); |
| 189 menu_runner_.reset(new MenuRunner(menu_delegate.CreateMenu())); | 191 menu_runner_.reset(new MenuRunner(menu_delegate.CreateMenu())); |
| 190 MenuRunner::RunResult result = | 192 MenuRunner::RunResult result = |
| 191 menu_runner_->RunMenuAt(GetWidget(), NULL, | 193 menu_runner_->RunMenuAt(GetWidget(), NULL, |
| 192 gfx::Rect(menu_position, gfx::Size(0, 0)), | 194 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 193 MenuItemView::TOPLEFT, | 195 MenuItemView::TOPLEFT, |
| 196 source_type, |
| 194 MenuRunner::HAS_MNEMONICS); | 197 MenuRunner::HAS_MNEMONICS); |
| 195 if (result == MenuRunner::MENU_DELETED) | 198 if (result == MenuRunner::MENU_DELETED) |
| 196 return; | 199 return; |
| 197 } else { | 200 } else { |
| 198 MenuDelegate menu_delegate; | 201 MenuDelegate menu_delegate; |
| 199 MenuItemView* menu = new MenuItemView(&menu_delegate); | 202 MenuItemView* menu = new MenuItemView(&menu_delegate); |
| 200 menu_runner_.reset(new MenuRunner(menu)); | 203 menu_runner_.reset(new MenuRunner(menu)); |
| 201 MenuRunner::RunResult result = | 204 MenuRunner::RunResult result = |
| 202 menu_runner_->RunMenuAt(GetWidget(), NULL, | 205 menu_runner_->RunMenuAt(GetWidget(), NULL, |
| 203 gfx::Rect(menu_position, gfx::Size(0, 0)), | 206 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 204 MenuItemView::TOPLEFT, | 207 MenuItemView::TOPLEFT, |
| 208 source_type, |
| 205 MenuRunner::HAS_MNEMONICS); | 209 MenuRunner::HAS_MNEMONICS); |
| 206 if (result == MenuRunner::MENU_DELETED) | 210 if (result == MenuRunner::MENU_DELETED) |
| 207 return; | 211 return; |
| 208 } | 212 } |
| 209 | 213 |
| 210 menu_showing_ = false; | 214 menu_showing_ = false; |
| 211 | 215 |
| 212 // Need to explicitly clear mouse handler so that events get sent | 216 // Need to explicitly clear mouse handler so that events get sent |
| 213 // properly after the menu finishes running. If we don't do this, then | 217 // properly after the menu finishes running. If we don't do this, then |
| 214 // the first click to other parts of the UI is eaten. | 218 // the first click to other parts of the UI is eaten. |
| 215 SetMouseHandler(NULL); | 219 SetMouseHandler(NULL); |
| 216 | 220 |
| 217 // Set the state back to normal after the drop down menu is closed. | 221 // Set the state back to normal after the drop down menu is closed. |
| 218 if (state_ != STATE_DISABLED) | 222 if (state_ != STATE_DISABLED) |
| 219 SetState(STATE_NORMAL); | 223 SetState(STATE_NORMAL); |
| 220 } | 224 } |
| 221 | 225 |
| 222 //////////////////////////////////////////////////////////////////////////////// | 226 //////////////////////////////////////////////////////////////////////////////// |
| 223 // | 227 // |
| 224 // ButtonDropDown - Accessibility | 228 // ButtonDropDown - Accessibility |
| 225 // | 229 // |
| 226 //////////////////////////////////////////////////////////////////////////////// | 230 //////////////////////////////////////////////////////////////////////////////// |
| 227 | 231 |
| 228 } // namespace views | 232 } // namespace views |
| OLD | NEW |