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/accessibility/ax_view_state.h"
8 #include "ui/events/event.h"
9 #include "ui/events/event_utils.h"
10 #include "ui/events/keycodes/keyboard_codes.h"
11 #include "ui/gfx/animation/throb_animation.h"
12 #include "ui/gfx/screen.h"
13 #include "ui/views/controls/button/blue_button.h"
14 #include "ui/views/controls/button/checkbox.h"
15 #include "ui/views/controls/button/image_button.h"
16 #include "ui/views/controls/button/label_button.h"
17 #include "ui/views/controls/button/menu_button.h"
18 #include "ui/views/controls/button/radio_button.h"
19 #include "ui/views/widget/widget.h"
23 // How long the hover animation takes if uninterrupted.
24 static const int kHoverFadeDurationMs
= 150;
27 const char CustomButton::kViewClassName
[] = "CustomButton";
29 ////////////////////////////////////////////////////////////////////////////////
30 // CustomButton, public:
33 const CustomButton
* CustomButton::AsCustomButton(const views::View
* view
) {
34 return AsCustomButton(const_cast<views::View
*>(view
));
37 CustomButton
* CustomButton::AsCustomButton(views::View
* view
) {
39 const char* classname
= view
->GetClassName();
40 if (!strcmp(classname
, Checkbox::kViewClassName
) ||
41 !strcmp(classname
, CustomButton::kViewClassName
) ||
42 !strcmp(classname
, ImageButton::kViewClassName
) ||
43 !strcmp(classname
, LabelButton::kViewClassName
) ||
44 !strcmp(classname
, RadioButton::kViewClassName
) ||
45 !strcmp(classname
, MenuButton::kViewClassName
)) {
46 return static_cast<CustomButton
*>(view
);
52 CustomButton::~CustomButton() {
55 void CustomButton::SetState(ButtonState state
) {
59 if (animate_on_state_change_
&&
60 (!is_throbbing_
|| !hover_animation_
->is_animating())) {
61 is_throbbing_
= false;
62 if (state_
== STATE_NORMAL
&& state
== STATE_HOVERED
) {
63 // Button is hovered from a normal state, start hover animation.
64 hover_animation_
->Show();
65 } else if ((state_
== STATE_HOVERED
|| state_
== STATE_PRESSED
)
66 && state
== STATE_NORMAL
) {
67 // Button is returning to a normal state from hover, start hover
69 hover_animation_
->Hide();
71 hover_animation_
->Stop();
80 void CustomButton::StartThrobbing(int cycles_til_stop
) {
82 hover_animation_
->StartThrobbing(cycles_til_stop
);
85 void CustomButton::StopThrobbing() {
86 if (hover_animation_
->is_animating()) {
87 hover_animation_
->Stop();
92 void CustomButton::SetAnimationDuration(int duration
) {
93 hover_animation_
->SetSlideDuration(duration
);
96 void CustomButton::SetHotTracked(bool is_hot_tracked
) {
97 if (state_
!= STATE_DISABLED
)
98 SetState(is_hot_tracked
? STATE_HOVERED
: STATE_NORMAL
);
101 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS
, true);
104 bool CustomButton::IsHotTracked() const {
105 return state_
== STATE_HOVERED
;
108 ////////////////////////////////////////////////////////////////////////////////
109 // CustomButton, View overrides:
111 void CustomButton::OnEnabledChanged() {
112 if (enabled() ? (state_
!= STATE_DISABLED
) : (state_
== STATE_DISABLED
))
116 SetState(IsMouseHovered() ? STATE_HOVERED
: STATE_NORMAL
);
118 SetState(STATE_DISABLED
);
121 const char* CustomButton::GetClassName() const {
122 return kViewClassName
;
125 bool CustomButton::OnMousePressed(const ui::MouseEvent
& event
) {
126 if (state_
!= STATE_DISABLED
) {
127 if (ShouldEnterPushedState(event
) && HitTestPoint(event
.location()))
128 SetState(STATE_PRESSED
);
129 if (request_focus_on_press_
)
131 if (IsTriggerableEvent(event
) && notify_action_
== NOTIFY_ON_PRESS
) {
133 // NOTE: We may be deleted at this point (by the listener's notification
140 bool CustomButton::OnMouseDragged(const ui::MouseEvent
& event
) {
141 if (state_
!= STATE_DISABLED
) {
142 if (HitTestPoint(event
.location()))
143 SetState(ShouldEnterPushedState(event
) ? STATE_PRESSED
: STATE_HOVERED
);
145 SetState(STATE_NORMAL
);
150 void CustomButton::OnMouseReleased(const ui::MouseEvent
& event
) {
151 if (state_
== STATE_DISABLED
)
154 if (!HitTestPoint(event
.location())) {
155 SetState(STATE_NORMAL
);
159 SetState(STATE_HOVERED
);
160 if (IsTriggerableEvent(event
) && notify_action_
== NOTIFY_ON_RELEASE
) {
162 // NOTE: We may be deleted at this point (by the listener's notification
167 void CustomButton::OnMouseCaptureLost() {
168 // Starting a drag results in a MouseCaptureLost, we need to ignore it.
169 if (state_
!= STATE_DISABLED
&& !InDrag())
170 SetState(STATE_NORMAL
);
173 void CustomButton::OnMouseEntered(const ui::MouseEvent
& event
) {
174 if (state_
!= STATE_DISABLED
)
175 SetState(STATE_HOVERED
);
178 void CustomButton::OnMouseExited(const ui::MouseEvent
& event
) {
179 // Starting a drag results in a MouseExited, we need to ignore it.
180 if (state_
!= STATE_DISABLED
&& !InDrag())
181 SetState(STATE_NORMAL
);
184 void CustomButton::OnMouseMoved(const ui::MouseEvent
& event
) {
185 if (state_
!= STATE_DISABLED
)
186 SetState(HitTestPoint(event
.location()) ? STATE_HOVERED
: STATE_NORMAL
);
189 bool CustomButton::OnKeyPressed(const ui::KeyEvent
& event
) {
190 if (state_
== STATE_DISABLED
)
193 // Space sets button state to pushed. Enter clicks the button. This matches
194 // the Windows native behavior of buttons, where Space clicks the button on
195 // KeyRelease and Enter clicks the button on KeyPressed.
196 if (event
.key_code() == ui::VKEY_SPACE
) {
197 SetState(STATE_PRESSED
);
198 } else if (event
.key_code() == ui::VKEY_RETURN
) {
199 SetState(STATE_NORMAL
);
207 bool CustomButton::OnKeyReleased(const ui::KeyEvent
& event
) {
208 if ((state_
== STATE_DISABLED
) || (event
.key_code() != ui::VKEY_SPACE
))
211 SetState(STATE_NORMAL
);
216 void CustomButton::OnGestureEvent(ui::GestureEvent
* event
) {
217 if (state_
== STATE_DISABLED
) {
218 Button::OnGestureEvent(event
);
222 if (event
->type() == ui::ET_GESTURE_TAP
&& IsTriggerableEvent(*event
)) {
223 // Set the button state to hot and start the animation fully faded in. The
224 // GESTURE_END event issued immediately after will set the state to
225 // STATE_NORMAL beginning the fade out animation. See
226 // http://crbug.com/131184.
227 SetState(STATE_HOVERED
);
228 hover_animation_
->Reset(1.0);
230 event
->StopPropagation();
231 } else if (event
->type() == ui::ET_GESTURE_TAP_DOWN
&&
232 ShouldEnterPushedState(*event
)) {
233 SetState(STATE_PRESSED
);
234 if (request_focus_on_press_
)
236 event
->StopPropagation();
237 } else if (event
->type() == ui::ET_GESTURE_TAP_CANCEL
||
238 event
->type() == ui::ET_GESTURE_END
) {
239 SetState(STATE_NORMAL
);
241 if (!event
->handled())
242 Button::OnGestureEvent(event
);
245 bool CustomButton::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
246 SetState(STATE_NORMAL
);
247 // TODO(beng): remove once NotifyClick takes ui::Event.
248 ui::MouseEvent
synthetic_event(
249 ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
250 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
251 NotifyClick(synthetic_event
);
255 void CustomButton::ShowContextMenu(const gfx::Point
& p
,
256 ui::MenuSourceType source_type
) {
257 if (!context_menu_controller())
260 // We're about to show the context menu. Showing the context menu likely means
261 // we won't get a mouse exited and reset state. Reset it now to be sure.
262 if (state_
!= STATE_DISABLED
)
263 SetState(STATE_NORMAL
);
264 View::ShowContextMenu(p
, source_type
);
267 void CustomButton::OnDragDone() {
268 // Only reset the state to normal if the button isn't currently disabled
269 // (since disabled buttons may still be able to be dragged).
270 if (state_
!= STATE_DISABLED
)
271 SetState(STATE_NORMAL
);
274 void CustomButton::GetAccessibleState(ui::AXViewState
* state
) {
275 Button::GetAccessibleState(state
);
278 state
->AddStateFlag(ui::AX_STATE_HOVERED
);
281 state
->AddStateFlag(ui::AX_STATE_PRESSED
);
284 state
->AddStateFlag(ui::AX_STATE_DISABLED
);
288 // No additional accessibility state set for this button state.
293 void CustomButton::VisibilityChanged(View
* starting_from
, bool visible
) {
294 if (state_
== STATE_DISABLED
)
296 SetState(visible
&& IsMouseHovered() ? STATE_HOVERED
: STATE_NORMAL
);
299 ////////////////////////////////////////////////////////////////////////////////
300 // CustomButton, gfx::AnimationDelegate implementation:
302 void CustomButton::AnimationProgressed(const gfx::Animation
* animation
) {
306 ////////////////////////////////////////////////////////////////////////////////
307 // CustomButton, protected:
309 CustomButton::CustomButton(ButtonListener
* listener
)
311 state_(STATE_NORMAL
),
312 animate_on_state_change_(true),
313 is_throbbing_(false),
314 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON
),
315 request_focus_on_press_(true),
316 notify_action_(NOTIFY_ON_RELEASE
) {
317 hover_animation_
.reset(new gfx::ThrobAnimation(this));
318 hover_animation_
->SetSlideDuration(kHoverFadeDurationMs
);
321 void CustomButton::StateChanged() {
324 bool CustomButton::IsTriggerableEvent(const ui::Event
& event
) {
325 return event
.type() == ui::ET_GESTURE_TAP_DOWN
||
326 event
.type() == ui::ET_GESTURE_TAP
||
327 (event
.IsMouseEvent() &&
328 (triggerable_event_flags_
& event
.flags()) != 0);
331 bool CustomButton::ShouldEnterPushedState(const ui::Event
& event
) {
332 return IsTriggerableEvent(event
);
335 ////////////////////////////////////////////////////////////////////////////////
336 // CustomButton, View overrides (protected):
338 void CustomButton::ViewHierarchyChanged(
339 const ViewHierarchyChangedDetails
& details
) {
340 if (!details
.is_add
&& state_
!= STATE_DISABLED
)
341 SetState(STATE_NORMAL
);
344 void CustomButton::OnBlur() {
346 SetState(STATE_NORMAL
);