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
3 // found in the LICENSE file.
5 #include "ui/views/controls/button/custom_button.h"
7 #include "ui/base/accessibility/accessible_view_state.h"
8 #include "ui/events/event.h"
9 #include "ui/events/keycodes/keyboard_codes.h"
10 #include "ui/gfx/animation/throb_animation.h"
11 #include "ui/gfx/screen.h"
12 #include "ui/views/widget/widget.h"
16 // How long the hover animation takes if uninterrupted.
17 static const int kHoverFadeDurationMs = 150;
20 const char CustomButton::kViewClassName[] = "CustomButton";
22 ////////////////////////////////////////////////////////////////////////////////
23 // CustomButton, public:
25 CustomButton::~CustomButton() {
28 void CustomButton::SetState(ButtonState state) {
32 if (animate_on_state_change_ &&
33 (!is_throbbing_ || !hover_animation_->is_animating())) {
34 is_throbbing_ = false;
35 if (state_ == STATE_NORMAL && state == STATE_HOVERED) {
36 // Button is hovered from a normal state, start hover animation.
37 hover_animation_->Show();
38 } else if ((state_ == STATE_HOVERED || state_ == STATE_PRESSED)
39 && state == STATE_NORMAL) {
40 // Button is returning to a normal state from hover, start hover
42 hover_animation_->Hide();
44 hover_animation_->Stop();
50 if (state_changed_delegate_.get())
51 state_changed_delegate_->StateChanged(state_);
55 void CustomButton::StartThrobbing(int cycles_til_stop) {
57 hover_animation_->StartThrobbing(cycles_til_stop);
60 void CustomButton::StopThrobbing() {
61 if (hover_animation_->is_animating()) {
62 hover_animation_->Stop();
67 void CustomButton::SetAnimationDuration(int duration) {
68 hover_animation_->SetSlideDuration(duration);
71 void CustomButton::SetHotTracked(bool is_hot_tracked) {
72 if (state_ != STATE_DISABLED)
73 SetState(is_hot_tracked ? STATE_HOVERED : STATE_NORMAL);
76 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_FOCUS, true);
79 bool CustomButton::IsHotTracked() const {
80 return state_ == STATE_HOVERED;
83 ////////////////////////////////////////////////////////////////////////////////
84 // CustomButton, View overrides:
86 void CustomButton::OnEnabledChanged() {
87 if (enabled() ? (state_ != STATE_DISABLED) : (state_ == STATE_DISABLED))
91 SetState(IsMouseHovered() ? STATE_HOVERED : STATE_NORMAL);
93 SetState(STATE_DISABLED);
96 const char* CustomButton::GetClassName() const {
97 return kViewClassName;
100 bool CustomButton::OnMousePressed(const ui::MouseEvent& event) {
101 if (state_ != STATE_DISABLED) {
102 if (ShouldEnterPushedState(event) && HitTestPoint(event.location()))
103 SetState(STATE_PRESSED);
104 if (request_focus_on_press_)
110 bool CustomButton::OnMouseDragged(const ui::MouseEvent& event) {
111 if (state_ != STATE_DISABLED) {
112 if (HitTestPoint(event.location()))
113 SetState(ShouldEnterPushedState(event) ? STATE_PRESSED : STATE_HOVERED);
115 SetState(STATE_NORMAL);
120 void CustomButton::OnMouseReleased(const ui::MouseEvent& event) {
121 if (state_ == STATE_DISABLED)
124 if (!HitTestPoint(event.location())) {
125 SetState(STATE_NORMAL);
129 SetState(STATE_HOVERED);
130 if (IsTriggerableEvent(event)) {
132 // NOTE: We may be deleted at this point (by the listener's notification
137 void CustomButton::OnMouseCaptureLost() {
138 // Starting a drag results in a MouseCaptureLost, we need to ignore it.
139 if (state_ != STATE_DISABLED && !InDrag())
140 SetState(STATE_NORMAL);
143 void CustomButton::OnMouseEntered(const ui::MouseEvent& event) {
144 if (state_ != STATE_DISABLED)
145 SetState(STATE_HOVERED);
148 void CustomButton::OnMouseExited(const ui::MouseEvent& event) {
149 // Starting a drag results in a MouseExited, we need to ignore it.
150 if (state_ != STATE_DISABLED && !InDrag())
151 SetState(STATE_NORMAL);
154 void CustomButton::OnMouseMoved(const ui::MouseEvent& event) {
155 if (state_ != STATE_DISABLED)
156 SetState(HitTestPoint(event.location()) ? STATE_HOVERED : STATE_NORMAL);
159 bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) {
160 if (state_ == STATE_DISABLED)
163 // Space sets button state to pushed. Enter clicks the button. This matches
164 // the Windows native behavior of buttons, where Space clicks the button on
165 // KeyRelease and Enter clicks the button on KeyPressed.
166 if (event.key_code() == ui::VKEY_SPACE) {
167 SetState(STATE_PRESSED);
168 } else if (event.key_code() == ui::VKEY_RETURN) {
169 SetState(STATE_NORMAL);
170 // TODO(beng): remove once NotifyClick takes ui::Event.
171 ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED,
174 ui::EF_LEFT_MOUSE_BUTTON);
175 NotifyClick(synthetic_event);
182 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) {
183 if ((state_ == STATE_DISABLED) || (event.key_code() != ui::VKEY_SPACE))
186 SetState(STATE_NORMAL);
187 // TODO(beng): remove once NotifyClick takes ui::Event.
188 ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED,
191 ui::EF_LEFT_MOUSE_BUTTON);
192 NotifyClick(synthetic_event);
196 void CustomButton::OnGestureEvent(ui::GestureEvent* event) {
197 if (state_ == STATE_DISABLED) {
198 Button::OnGestureEvent(event);
202 if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) {
203 // Set the button state to hot and start the animation fully faded in. The
204 // TAP_UP event issued immediately after will set the state to STATE_NORMAL
205 // beginning the fade out animation. See http://crbug.com/131184.
206 SetState(STATE_HOVERED);
207 hover_animation_->Reset(1.0);
209 event->StopPropagation();
210 } else if (event->type() == ui::ET_GESTURE_TAP_DOWN &&
211 ShouldEnterPushedState(*event)) {
212 SetState(STATE_PRESSED);
213 if (request_focus_on_press_)
215 event->StopPropagation();
217 SetState(STATE_NORMAL);
219 if (!event->handled())
220 Button::OnGestureEvent(event);
223 bool CustomButton::AcceleratorPressed(const ui::Accelerator& accelerator) {
224 SetState(STATE_NORMAL);
226 ui::KeyEvent key_event(ui::ET_KEY_RELEASED, accelerator.key_code(),
227 accelerator.modifiers());
229 // TODO(beng): remove once NotifyClick takes ui::Event.
230 ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED,
233 ui::EF_LEFT_MOUSE_BUTTON);
234 NotifyClick(synthetic_event);
238 void CustomButton::ShowContextMenu(const gfx::Point& p,
239 ui::MenuSourceType source_type) {
240 if (!context_menu_controller())
243 // We're about to show the context menu. Showing the context menu likely means
244 // we won't get a mouse exited and reset state. Reset it now to be sure.
245 if (state_ != STATE_DISABLED)
246 SetState(STATE_NORMAL);
247 View::ShowContextMenu(p, source_type);
250 void CustomButton::OnDragDone() {
251 SetState(STATE_NORMAL);
254 void CustomButton::GetAccessibleState(ui::AccessibleViewState* state) {
255 Button::GetAccessibleState(state);
258 state->state = ui::AccessibilityTypes::STATE_HOTTRACKED;
261 state->state = ui::AccessibilityTypes::STATE_PRESSED;
264 state->state = ui::AccessibilityTypes::STATE_UNAVAILABLE;
268 // No additional accessibility state set for this button state.
273 void CustomButton::VisibilityChanged(View* starting_from, bool visible) {
274 if (state_ == STATE_DISABLED)
276 SetState(visible && IsMouseHovered() ? STATE_HOVERED : STATE_NORMAL);
279 ////////////////////////////////////////////////////////////////////////////////
280 // CustomButton, gfx::AnimationDelegate implementation:
282 void CustomButton::AnimationProgressed(const gfx::Animation* animation) {
286 ////////////////////////////////////////////////////////////////////////////////
287 // CustomButton, protected:
289 CustomButton::CustomButton(ButtonListener* listener)
291 state_(STATE_NORMAL),
292 animate_on_state_change_(true),
293 is_throbbing_(false),
294 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON),
295 request_focus_on_press_(true) {
296 hover_animation_.reset(new gfx::ThrobAnimation(this));
297 hover_animation_->SetSlideDuration(kHoverFadeDurationMs);
300 void CustomButton::StateChanged() {
303 bool CustomButton::IsTriggerableEvent(const ui::Event& event) {
304 return event.type() == ui::ET_GESTURE_TAP_DOWN ||
305 event.type() == ui::ET_GESTURE_TAP ||
306 (event.IsMouseEvent() &&
307 (triggerable_event_flags_ & event.flags()) != 0);
310 bool CustomButton::ShouldEnterPushedState(const ui::Event& event) {
311 return IsTriggerableEvent(event);
314 ////////////////////////////////////////////////////////////////////////////////
315 // CustomButton, View overrides (protected):
317 void CustomButton::ViewHierarchyChanged(
318 const ViewHierarchyChangedDetails& details) {
319 if (!details.is_add && state_ != STATE_DISABLED)
320 SetState(STATE_NORMAL);
323 void CustomButton::OnBlur() {
325 SetState(STATE_NORMAL);