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/custom_button.h" | 5 #include "ui/views/controls/button/custom_button.h" |
6 | 6 |
7 #include "ui/base/accessibility/accessible_view_state.h" | 7 #include "ui/base/accessibility/accessible_view_state.h" |
8 #include "ui/base/animation/throb_animation.h" | 8 #include "ui/base/animation/throb_animation.h" |
| 9 #include "ui/base/event.h" |
9 #include "ui/base/keycodes/keyboard_codes.h" | 10 #include "ui/base/keycodes/keyboard_codes.h" |
10 #include "ui/gfx/screen.h" | 11 #include "ui/gfx/screen.h" |
11 #include "ui/views/widget/widget.h" | 12 #include "ui/views/widget/widget.h" |
12 | 13 |
13 namespace views { | 14 namespace views { |
14 | 15 |
15 // How long the hover animation takes if uninterrupted. | 16 // How long the hover animation takes if uninterrupted. |
16 static const int kHoverFadeDurationMs = 150; | 17 static const int kHoverFadeDurationMs = 150; |
17 | 18 |
18 // static | 19 // static |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 // Starting a drag results in a MouseExited, we need to ignore it. | 160 // Starting a drag results in a MouseExited, we need to ignore it. |
160 if (state_ != BS_DISABLED && !InDrag()) | 161 if (state_ != BS_DISABLED && !InDrag()) |
161 SetState(BS_NORMAL); | 162 SetState(BS_NORMAL); |
162 } | 163 } |
163 | 164 |
164 void CustomButton::OnMouseMoved(const MouseEvent& event) { | 165 void CustomButton::OnMouseMoved(const MouseEvent& event) { |
165 if (state_ != BS_DISABLED) | 166 if (state_ != BS_DISABLED) |
166 SetState(HitTest(event.location()) ? BS_HOT : BS_NORMAL); | 167 SetState(HitTest(event.location()) ? BS_HOT : BS_NORMAL); |
167 } | 168 } |
168 | 169 |
169 bool CustomButton::OnKeyPressed(const KeyEvent& event) { | 170 bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) { |
170 if (state_ == BS_DISABLED) | 171 if (state_ == BS_DISABLED) |
171 return false; | 172 return false; |
172 | 173 |
173 // Space sets button state to pushed. Enter clicks the button. This matches | 174 // Space sets button state to pushed. Enter clicks the button. This matches |
174 // the Windows native behavior of buttons, where Space clicks the button on | 175 // the Windows native behavior of buttons, where Space clicks the button on |
175 // KeyRelease and Enter clicks the button on KeyPressed. | 176 // KeyRelease and Enter clicks the button on KeyPressed. |
176 if (event.key_code() == ui::VKEY_SPACE) { | 177 if (event.key_code() == ui::VKEY_SPACE) { |
177 SetState(BS_PUSHED); | 178 SetState(BS_PUSHED); |
178 } else if (event.key_code() == ui::VKEY_RETURN) { | 179 } else if (event.key_code() == ui::VKEY_RETURN) { |
179 SetState(BS_NORMAL); | 180 SetState(BS_NORMAL); |
180 NotifyClick(event); | 181 // TODO(beng): remove once NotifyClick takes ui::Event. |
| 182 views::MouseEvent synthetic_event( |
| 183 ui::ET_MOUSE_RELEASED, 0, 0, ui::EF_LEFT_MOUSE_BUTTON); |
| 184 NotifyClick(synthetic_event); |
181 } else { | 185 } else { |
182 return false; | 186 return false; |
183 } | 187 } |
184 return true; | 188 return true; |
185 } | 189 } |
186 | 190 |
187 bool CustomButton::OnKeyReleased(const KeyEvent& event) { | 191 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) { |
188 if ((state_ == BS_DISABLED) || (event.key_code() != ui::VKEY_SPACE)) | 192 if ((state_ == BS_DISABLED) || (event.key_code() != ui::VKEY_SPACE)) |
189 return false; | 193 return false; |
190 | 194 |
191 SetState(BS_NORMAL); | 195 SetState(BS_NORMAL); |
192 NotifyClick(event); | 196 // TODO(beng): remove once NotifyClick takes ui::Event. |
| 197 views::MouseEvent synthetic_event( |
| 198 ui::ET_MOUSE_RELEASED, 0, 0, ui::EF_LEFT_MOUSE_BUTTON); |
| 199 NotifyClick(synthetic_event); |
193 return true; | 200 return true; |
194 } | 201 } |
195 | 202 |
196 ui::GestureStatus CustomButton::OnGestureEvent(const GestureEvent& event) { | 203 ui::GestureStatus CustomButton::OnGestureEvent(const GestureEvent& event) { |
197 if (state_ == BS_DISABLED) | 204 if (state_ == BS_DISABLED) |
198 return Button::OnGestureEvent(event); | 205 return Button::OnGestureEvent(event); |
199 | 206 |
200 if (event.type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(event)) { | 207 if (event.type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(event)) { |
201 // Set the button state to hot and start the animation fully faded in. The | 208 // Set the button state to hot and start the animation fully faded in. The |
202 // TAP_UP event issued immediately after will set the state to BS_NORMAL | 209 // TAP_UP event issued immediately after will set the state to BS_NORMAL |
203 // beginning the fade out animation. See http://crbug.com/131184. | 210 // beginning the fade out animation. See http://crbug.com/131184. |
204 SetState(BS_HOT); | 211 SetState(BS_HOT); |
205 hover_animation_->Reset(1.0); | 212 hover_animation_->Reset(1.0); |
206 NotifyClick(event); | 213 NotifyClick(event); |
207 return ui::GESTURE_STATUS_CONSUMED; | 214 return ui::GESTURE_STATUS_CONSUMED; |
208 } else if (event.type() == ui::ET_GESTURE_TAP_DOWN && | 215 } else if (event.type() == ui::ET_GESTURE_TAP_DOWN && |
209 ShouldEnterPushedState(event)) { | 216 ShouldEnterPushedState(event)) { |
210 SetState(BS_PUSHED); | 217 SetState(BS_PUSHED); |
211 if (request_focus_on_press_) | 218 if (request_focus_on_press_) |
212 RequestFocus(); | 219 RequestFocus(); |
213 return ui::GESTURE_STATUS_CONSUMED; | 220 return ui::GESTURE_STATUS_CONSUMED; |
214 } else { | 221 } else { |
215 SetState(BS_NORMAL); | 222 SetState(BS_NORMAL); |
216 } | 223 } |
217 return Button::OnGestureEvent(event); | 224 return Button::OnGestureEvent(event); |
218 } | 225 } |
219 | 226 |
220 bool CustomButton::AcceleratorPressed(const ui::Accelerator& accelerator) { | 227 bool CustomButton::AcceleratorPressed(const ui::Accelerator& accelerator) { |
221 SetState(BS_NORMAL); | 228 SetState(BS_NORMAL); |
222 KeyEvent key_event(ui::ET_KEY_RELEASED, accelerator.key_code(), | 229 /* |
223 accelerator.modifiers()); | 230 ui::KeyEvent key_event(ui::ET_KEY_RELEASED, accelerator.key_code(), |
224 NotifyClick(key_event); | 231 accelerator.modifiers()); |
| 232 */ |
| 233 // TODO(beng): remove once NotifyClick takes ui::Event. |
| 234 views::MouseEvent synthetic_event( |
| 235 ui::ET_MOUSE_RELEASED, 0, 0, ui::EF_LEFT_MOUSE_BUTTON); |
| 236 NotifyClick(synthetic_event); |
225 return true; | 237 return true; |
226 } | 238 } |
227 | 239 |
228 void CustomButton::ShowContextMenu(const gfx::Point& p, bool is_mouse_gesture) { | 240 void CustomButton::ShowContextMenu(const gfx::Point& p, bool is_mouse_gesture) { |
229 if (!context_menu_controller()) | 241 if (!context_menu_controller()) |
230 return; | 242 return; |
231 | 243 |
232 // We're about to show the context menu. Showing the context menu likely means | 244 // We're about to show the context menu. Showing the context menu likely means |
233 // we won't get a mouse exited and reset state. Reset it now to be sure. | 245 // we won't get a mouse exited and reset state. Reset it now to be sure. |
234 if (state_ != BS_DISABLED) | 246 if (state_ != BS_DISABLED) |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 if (!is_add && state_ != BS_DISABLED) | 314 if (!is_add && state_ != BS_DISABLED) |
303 SetState(BS_NORMAL); | 315 SetState(BS_NORMAL); |
304 } | 316 } |
305 | 317 |
306 void CustomButton::OnBlur() { | 318 void CustomButton::OnBlur() { |
307 if (IsHotTracked()) | 319 if (IsHotTracked()) |
308 SetState(BS_NORMAL); | 320 SetState(BS_NORMAL); |
309 } | 321 } |
310 | 322 |
311 } // namespace views | 323 } // namespace views |
OLD | NEW |