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_
)
135 bool CustomButton::OnMouseDragged(const ui::MouseEvent
& event
) {
136 if (state_
!= STATE_DISABLED
) {
137 if (HitTestPoint(event
.location()))
138 SetState(ShouldEnterPushedState(event
) ? STATE_PRESSED
: STATE_HOVERED
);
140 SetState(STATE_NORMAL
);
145 void CustomButton::OnMouseReleased(const ui::MouseEvent
& event
) {
146 if (state_
== STATE_DISABLED
)
149 if (!HitTestPoint(event
.location())) {
150 SetState(STATE_NORMAL
);
154 SetState(STATE_HOVERED
);
155 if (IsTriggerableEvent(event
)) {
157 // NOTE: We may be deleted at this point (by the listener's notification
162 void CustomButton::OnMouseCaptureLost() {
163 // Starting a drag results in a MouseCaptureLost, we need to ignore it.
164 if (state_
!= STATE_DISABLED
&& !InDrag())
165 SetState(STATE_NORMAL
);
168 void CustomButton::OnMouseEntered(const ui::MouseEvent
& event
) {
169 if (state_
!= STATE_DISABLED
)
170 SetState(STATE_HOVERED
);
173 void CustomButton::OnMouseExited(const ui::MouseEvent
& event
) {
174 // Starting a drag results in a MouseExited, we need to ignore it.
175 if (state_
!= STATE_DISABLED
&& !InDrag())
176 SetState(STATE_NORMAL
);
179 void CustomButton::OnMouseMoved(const ui::MouseEvent
& event
) {
180 if (state_
!= STATE_DISABLED
)
181 SetState(HitTestPoint(event
.location()) ? STATE_HOVERED
: STATE_NORMAL
);
184 bool CustomButton::OnKeyPressed(const ui::KeyEvent
& event
) {
185 if (state_
== STATE_DISABLED
)
188 // Space sets button state to pushed. Enter clicks the button. This matches
189 // the Windows native behavior of buttons, where Space clicks the button on
190 // KeyRelease and Enter clicks the button on KeyPressed.
191 if (event
.key_code() == ui::VKEY_SPACE
) {
192 SetState(STATE_PRESSED
);
193 } else if (event
.key_code() == ui::VKEY_RETURN
) {
194 SetState(STATE_NORMAL
);
195 // TODO(beng): remove once NotifyClick takes ui::Event.
196 ui::MouseEvent
synthetic_event(ui::ET_MOUSE_RELEASED
, gfx::Point(),
197 gfx::Point(), ui::EventTimeForNow(),
198 ui::EF_LEFT_MOUSE_BUTTON
,
199 ui::EF_LEFT_MOUSE_BUTTON
);
200 NotifyClick(synthetic_event
);
207 bool CustomButton::OnKeyReleased(const ui::KeyEvent
& event
) {
208 if ((state_
== STATE_DISABLED
) || (event
.key_code() != ui::VKEY_SPACE
))
211 SetState(STATE_NORMAL
);
212 // TODO(beng): remove once NotifyClick takes ui::Event.
213 ui::MouseEvent
synthetic_event(
214 ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
215 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
216 NotifyClick(synthetic_event
);
220 void CustomButton::OnGestureEvent(ui::GestureEvent
* event
) {
221 if (state_
== STATE_DISABLED
) {
222 Button::OnGestureEvent(event
);
226 if (event
->type() == ui::ET_GESTURE_TAP
&& IsTriggerableEvent(*event
)) {
227 // Set the button state to hot and start the animation fully faded in. The
228 // GESTURE_END event issued immediately after will set the state to
229 // STATE_NORMAL beginning the fade out animation. See
230 // http://crbug.com/131184.
231 SetState(STATE_HOVERED
);
232 hover_animation_
->Reset(1.0);
234 event
->StopPropagation();
235 } else if (event
->type() == ui::ET_GESTURE_TAP_DOWN
&&
236 ShouldEnterPushedState(*event
)) {
237 SetState(STATE_PRESSED
);
238 if (request_focus_on_press_
)
240 event
->StopPropagation();
241 } else if (event
->type() == ui::ET_GESTURE_TAP_CANCEL
||
242 event
->type() == ui::ET_GESTURE_END
) {
243 SetState(STATE_NORMAL
);
245 if (!event
->handled())
246 Button::OnGestureEvent(event
);
249 bool CustomButton::AcceleratorPressed(const ui::Accelerator
& accelerator
) {
250 SetState(STATE_NORMAL
);
251 // TODO(beng): remove once NotifyClick takes ui::Event.
252 ui::MouseEvent
synthetic_event(
253 ui::ET_MOUSE_RELEASED
, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
254 ui::EF_LEFT_MOUSE_BUTTON
, ui::EF_LEFT_MOUSE_BUTTON
);
255 NotifyClick(synthetic_event
);
259 void CustomButton::ShowContextMenu(const gfx::Point
& p
,
260 ui::MenuSourceType source_type
) {
261 if (!context_menu_controller())
264 // We're about to show the context menu. Showing the context menu likely means
265 // we won't get a mouse exited and reset state. Reset it now to be sure.
266 if (state_
!= STATE_DISABLED
)
267 SetState(STATE_NORMAL
);
268 View::ShowContextMenu(p
, source_type
);
271 void CustomButton::OnDragDone() {
272 // Only reset the state to normal if the button isn't currently disabled
273 // (since disabled buttons may still be able to be dragged).
274 if (state_
!= STATE_DISABLED
)
275 SetState(STATE_NORMAL
);
278 void CustomButton::GetAccessibleState(ui::AXViewState
* state
) {
279 Button::GetAccessibleState(state
);
282 state
->AddStateFlag(ui::AX_STATE_HOVERED
);
285 state
->AddStateFlag(ui::AX_STATE_PRESSED
);
288 state
->AddStateFlag(ui::AX_STATE_DISABLED
);
292 // No additional accessibility state set for this button state.
297 void CustomButton::VisibilityChanged(View
* starting_from
, bool visible
) {
298 if (state_
== STATE_DISABLED
)
300 SetState(visible
&& IsMouseHovered() ? STATE_HOVERED
: STATE_NORMAL
);
303 ////////////////////////////////////////////////////////////////////////////////
304 // CustomButton, gfx::AnimationDelegate implementation:
306 void CustomButton::AnimationProgressed(const gfx::Animation
* animation
) {
310 ////////////////////////////////////////////////////////////////////////////////
311 // CustomButton, protected:
313 CustomButton::CustomButton(ButtonListener
* listener
)
315 state_(STATE_NORMAL
),
316 animate_on_state_change_(true),
317 is_throbbing_(false),
318 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON
),
319 request_focus_on_press_(true) {
320 hover_animation_
.reset(new gfx::ThrobAnimation(this));
321 hover_animation_
->SetSlideDuration(kHoverFadeDurationMs
);
324 void CustomButton::StateChanged() {
327 bool CustomButton::IsTriggerableEvent(const ui::Event
& event
) {
328 return event
.type() == ui::ET_GESTURE_TAP_DOWN
||
329 event
.type() == ui::ET_GESTURE_TAP
||
330 (event
.IsMouseEvent() &&
331 (triggerable_event_flags_
& event
.flags()) != 0);
334 bool CustomButton::ShouldEnterPushedState(const ui::Event
& event
) {
335 return IsTriggerableEvent(event
);
338 ////////////////////////////////////////////////////////////////////////////////
339 // CustomButton, View overrides (protected):
341 void CustomButton::ViewHierarchyChanged(
342 const ViewHierarchyChangedDetails
& details
) {
343 if (!details
.is_add
&& state_
!= STATE_DISABLED
)
344 SetState(STATE_NORMAL
);
347 void CustomButton::OnBlur() {
349 SetState(STATE_NORMAL
);