Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / controls / button / custom_button.cc
blobbf294c6e20bd111b8012adeba056df4680d90922
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"
21 namespace views {
23 // How long the hover animation takes if uninterrupted.
24 static const int kHoverFadeDurationMs = 150;
26 // static
27 const char CustomButton::kViewClassName[] = "CustomButton";
29 ////////////////////////////////////////////////////////////////////////////////
30 // CustomButton, public:
32 // static
33 const CustomButton* CustomButton::AsCustomButton(const views::View* view) {
34 return AsCustomButton(const_cast<views::View*>(view));
37 CustomButton* CustomButton::AsCustomButton(views::View* view) {
38 if (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);
49 return NULL;
52 CustomButton::~CustomButton() {
55 void CustomButton::SetState(ButtonState state) {
56 if (state == state_)
57 return;
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
68 // fade animation.
69 hover_animation_->Hide();
70 } else {
71 hover_animation_->Stop();
75 state_ = state;
76 StateChanged();
77 SchedulePaint();
80 void CustomButton::StartThrobbing(int cycles_til_stop) {
81 is_throbbing_ = true;
82 hover_animation_->StartThrobbing(cycles_til_stop);
85 void CustomButton::StopThrobbing() {
86 if (hover_animation_->is_animating()) {
87 hover_animation_->Stop();
88 SchedulePaint();
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);
100 if (is_hot_tracked)
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))
113 return;
115 if (enabled())
116 SetState(IsMouseHovered() ? STATE_HOVERED : STATE_NORMAL);
117 else
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_)
130 RequestFocus();
131 if (IsTriggerableEvent(event) && notify_action_ == NOTIFY_ON_PRESS) {
132 NotifyClick(event);
133 // NOTE: We may be deleted at this point (by the listener's notification
134 // handler).
137 return true;
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);
144 else
145 SetState(STATE_NORMAL);
147 return true;
150 void CustomButton::OnMouseReleased(const ui::MouseEvent& event) {
151 if (state_ == STATE_DISABLED)
152 return;
154 if (!HitTestPoint(event.location())) {
155 SetState(STATE_NORMAL);
156 return;
159 SetState(STATE_HOVERED);
160 if (IsTriggerableEvent(event) && notify_action_ == NOTIFY_ON_RELEASE) {
161 NotifyClick(event);
162 // NOTE: We may be deleted at this point (by the listener's notification
163 // handler).
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)
191 return false;
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);
200 NotifyClick(event);
201 } else {
202 return false;
204 return true;
207 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) {
208 if ((state_ == STATE_DISABLED) || (event.key_code() != ui::VKEY_SPACE))
209 return false;
211 SetState(STATE_NORMAL);
212 NotifyClick(event);
213 return true;
216 void CustomButton::OnGestureEvent(ui::GestureEvent* event) {
217 if (state_ == STATE_DISABLED) {
218 Button::OnGestureEvent(event);
219 return;
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);
229 NotifyClick(*event);
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_)
235 RequestFocus();
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);
252 return true;
255 void CustomButton::ShowContextMenu(const gfx::Point& p,
256 ui::MenuSourceType source_type) {
257 if (!context_menu_controller())
258 return;
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);
276 switch (state_) {
277 case STATE_HOVERED:
278 state->AddStateFlag(ui::AX_STATE_HOVERED);
279 break;
280 case STATE_PRESSED:
281 state->AddStateFlag(ui::AX_STATE_PRESSED);
282 break;
283 case STATE_DISABLED:
284 state->AddStateFlag(ui::AX_STATE_DISABLED);
285 break;
286 case STATE_NORMAL:
287 case STATE_COUNT:
288 // No additional accessibility state set for this button state.
289 break;
293 void CustomButton::VisibilityChanged(View* starting_from, bool visible) {
294 if (state_ == STATE_DISABLED)
295 return;
296 SetState(visible && IsMouseHovered() ? STATE_HOVERED : STATE_NORMAL);
299 ////////////////////////////////////////////////////////////////////////////////
300 // CustomButton, gfx::AnimationDelegate implementation:
302 void CustomButton::AnimationProgressed(const gfx::Animation* animation) {
303 SchedulePaint();
306 ////////////////////////////////////////////////////////////////////////////////
307 // CustomButton, protected:
309 CustomButton::CustomButton(ButtonListener* listener)
310 : Button(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() {
345 if (IsHotTracked())
346 SetState(STATE_NORMAL);
349 } // namespace views