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/keycodes/keyboard_codes.h"
10 #include "ui/gfx/animation/throb_animation.h"
11 #include "ui/gfx/screen.h"
12 #include "ui/views/controls/button/blue_button.h"
13 #include "ui/views/controls/button/checkbox.h"
14 #include "ui/views/controls/button/image_button.h"
15 #include "ui/views/controls/button/label_button.h"
16 #include "ui/views/controls/button/menu_button.h"
17 #include "ui/views/controls/button/radio_button.h"
18 #include "ui/views/controls/button/text_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 !strcmp(classname, TextButton::kViewClassName)) {
47 return static_cast<CustomButton*>(view);
53 CustomButton::~CustomButton() {
56 void CustomButton::SetState(ButtonState state) {
60 if (animate_on_state_change_ &&
61 (!is_throbbing_ || !hover_animation_->is_animating())) {
62 is_throbbing_ = false;
63 if (state_ == STATE_NORMAL && state == STATE_HOVERED) {
64 // Button is hovered from a normal state, start hover animation.
65 hover_animation_->Show();
66 } else if ((state_ == STATE_HOVERED || state_ == STATE_PRESSED)
67 && state == STATE_NORMAL) {
68 // Button is returning to a normal state from hover, start hover
70 hover_animation_->Hide();
72 hover_animation_->Stop();
78 if (state_changed_delegate_.get())
79 state_changed_delegate_->StateChanged(state_);
83 void CustomButton::StartThrobbing(int cycles_til_stop) {
85 hover_animation_->StartThrobbing(cycles_til_stop);
88 void CustomButton::StopThrobbing() {
89 if (hover_animation_->is_animating()) {
90 hover_animation_->Stop();
95 void CustomButton::SetAnimationDuration(int duration) {
96 hover_animation_->SetSlideDuration(duration);
99 void CustomButton::SetHotTracked(bool is_hot_tracked) {
100 if (state_ != STATE_DISABLED)
101 SetState(is_hot_tracked ? STATE_HOVERED : STATE_NORMAL);
104 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
107 bool CustomButton::IsHotTracked() const {
108 return state_ == STATE_HOVERED;
111 ////////////////////////////////////////////////////////////////////////////////
112 // CustomButton, View overrides:
114 void CustomButton::OnEnabledChanged() {
115 if (enabled() ? (state_ != STATE_DISABLED) : (state_ == STATE_DISABLED))
119 SetState(IsMouseHovered() ? STATE_HOVERED : STATE_NORMAL);
121 SetState(STATE_DISABLED);
124 const char* CustomButton::GetClassName() const {
125 return kViewClassName;
128 bool CustomButton::OnMousePressed(const ui::MouseEvent& event) {
129 if (state_ != STATE_DISABLED) {
130 if (ShouldEnterPushedState(event) && HitTestPoint(event.location()))
131 SetState(STATE_PRESSED);
132 if (request_focus_on_press_)
138 bool CustomButton::OnMouseDragged(const ui::MouseEvent& event) {
139 if (state_ != STATE_DISABLED) {
140 if (HitTestPoint(event.location()))
141 SetState(ShouldEnterPushedState(event) ? STATE_PRESSED : STATE_HOVERED);
143 SetState(STATE_NORMAL);
148 void CustomButton::OnMouseReleased(const ui::MouseEvent& event) {
149 if (state_ == STATE_DISABLED)
152 if (!HitTestPoint(event.location())) {
153 SetState(STATE_NORMAL);
157 SetState(STATE_HOVERED);
158 if (IsTriggerableEvent(event)) {
160 // NOTE: We may be deleted at this point (by the listener's notification
165 void CustomButton::OnMouseCaptureLost() {
166 // Starting a drag results in a MouseCaptureLost, we need to ignore it.
167 if (state_ != STATE_DISABLED && !InDrag())
168 SetState(STATE_NORMAL);
171 void CustomButton::OnMouseEntered(const ui::MouseEvent& event) {
172 if (state_ != STATE_DISABLED)
173 SetState(STATE_HOVERED);
176 void CustomButton::OnMouseExited(const ui::MouseEvent& event) {
177 // Starting a drag results in a MouseExited, we need to ignore it.
178 if (state_ != STATE_DISABLED && !InDrag())
179 SetState(STATE_NORMAL);
182 void CustomButton::OnMouseMoved(const ui::MouseEvent& event) {
183 if (state_ != STATE_DISABLED)
184 SetState(HitTestPoint(event.location()) ? STATE_HOVERED : STATE_NORMAL);
187 bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) {
188 if (state_ == STATE_DISABLED)
191 // Space sets button state to pushed. Enter clicks the button. This matches
192 // the Windows native behavior of buttons, where Space clicks the button on
193 // KeyRelease and Enter clicks the button on KeyPressed.
194 if (event.key_code() == ui::VKEY_SPACE) {
195 SetState(STATE_PRESSED);
196 } else if (event.key_code() == ui::VKEY_RETURN) {
197 SetState(STATE_NORMAL);
198 // TODO(beng): remove once NotifyClick takes ui::Event.
199 ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED,
202 ui::EF_LEFT_MOUSE_BUTTON,
203 ui::EF_LEFT_MOUSE_BUTTON);
204 NotifyClick(synthetic_event);
211 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) {
212 if ((state_ == STATE_DISABLED) || (event.key_code() != ui::VKEY_SPACE))
215 SetState(STATE_NORMAL);
216 // TODO(beng): remove once NotifyClick takes ui::Event.
217 ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED,
220 ui::EF_LEFT_MOUSE_BUTTON,
221 ui::EF_LEFT_MOUSE_BUTTON);
222 NotifyClick(synthetic_event);
226 void CustomButton::OnGestureEvent(ui::GestureEvent* event) {
227 if (state_ == STATE_DISABLED) {
228 Button::OnGestureEvent(event);
232 if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) {
233 // Set the button state to hot and start the animation fully faded in. The
234 // GESTURE_END event issued immediately after will set the state to
235 // STATE_NORMAL beginning the fade out animation. See
236 // http://crbug.com/131184.
237 SetState(STATE_HOVERED);
238 hover_animation_->Reset(1.0);
240 event->StopPropagation();
241 } else if (event->type() == ui::ET_GESTURE_TAP_DOWN &&
242 ShouldEnterPushedState(*event)) {
243 SetState(STATE_PRESSED);
244 if (request_focus_on_press_)
246 event->StopPropagation();
247 } else if (event->type() == ui::ET_GESTURE_TAP_CANCEL ||
248 event->type() == ui::ET_GESTURE_END) {
249 SetState(STATE_NORMAL);
251 if (!event->handled())
252 Button::OnGestureEvent(event);
255 bool CustomButton::AcceleratorPressed(const ui::Accelerator& accelerator) {
256 SetState(STATE_NORMAL);
258 ui::KeyEvent key_event(ui::ET_KEY_RELEASED, accelerator.key_code(),
259 accelerator.modifiers());
261 // TODO(beng): remove once NotifyClick takes ui::Event.
262 ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED,
265 ui::EF_LEFT_MOUSE_BUTTON,
266 ui::EF_LEFT_MOUSE_BUTTON);
267 NotifyClick(synthetic_event);
271 void CustomButton::ShowContextMenu(const gfx::Point& p,
272 ui::MenuSourceType source_type) {
273 if (!context_menu_controller())
276 // We're about to show the context menu. Showing the context menu likely means
277 // we won't get a mouse exited and reset state. Reset it now to be sure.
278 if (state_ != STATE_DISABLED)
279 SetState(STATE_NORMAL);
280 View::ShowContextMenu(p, source_type);
283 void CustomButton::OnDragDone() {
284 SetState(STATE_NORMAL);
287 void CustomButton::GetAccessibleState(ui::AXViewState* state) {
288 Button::GetAccessibleState(state);
291 state->AddStateFlag(ui::AX_STATE_HOVERED);
294 state->AddStateFlag(ui::AX_STATE_PRESSED);
297 state->AddStateFlag(ui::AX_STATE_DISABLED);
301 // No additional accessibility state set for this button state.
306 void CustomButton::VisibilityChanged(View* starting_from, bool visible) {
307 if (state_ == STATE_DISABLED)
309 SetState(visible && IsMouseHovered() ? STATE_HOVERED : STATE_NORMAL);
312 ////////////////////////////////////////////////////////////////////////////////
313 // CustomButton, gfx::AnimationDelegate implementation:
315 void CustomButton::AnimationProgressed(const gfx::Animation* animation) {
319 ////////////////////////////////////////////////////////////////////////////////
320 // CustomButton, protected:
322 CustomButton::CustomButton(ButtonListener* listener)
324 state_(STATE_NORMAL),
325 animate_on_state_change_(true),
326 is_throbbing_(false),
327 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON),
328 request_focus_on_press_(true) {
329 hover_animation_.reset(new gfx::ThrobAnimation(this));
330 hover_animation_->SetSlideDuration(kHoverFadeDurationMs);
333 void CustomButton::StateChanged() {
336 bool CustomButton::IsTriggerableEvent(const ui::Event& event) {
337 return event.type() == ui::ET_GESTURE_TAP_DOWN ||
338 event.type() == ui::ET_GESTURE_TAP ||
339 (event.IsMouseEvent() &&
340 (triggerable_event_flags_ & event.flags()) != 0);
343 bool CustomButton::ShouldEnterPushedState(const ui::Event& event) {
344 return IsTriggerableEvent(event);
347 ////////////////////////////////////////////////////////////////////////////////
348 // CustomButton, View overrides (protected):
350 void CustomButton::ViewHierarchyChanged(
351 const ViewHierarchyChangedDetails& details) {
352 if (!details.is_add && state_ != STATE_DISABLED)
353 SetState(STATE_NORMAL);
356 void CustomButton::OnBlur() {
358 SetState(STATE_NORMAL);