| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/button/custom_button.h" | 5 #include "ui/views/controls/button/custom_button.h" |
| 6 | 6 |
| 7 #include "ui/accessibility/ax_view_state.h" | 7 #include "ui/accessibility/ax_view_state.h" |
| 8 #include "ui/events/event.h" | 8 #include "ui/events/event.h" |
| 9 #include "ui/events/event_utils.h" | 9 #include "ui/events/event_utils.h" |
| 10 #include "ui/events/keycodes/keyboard_codes.h" | 10 #include "ui/events/keycodes/keyboard_codes.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 return NULL; | 55 return NULL; |
| 56 } | 56 } |
| 57 | 57 |
| 58 CustomButton::~CustomButton() {} | 58 CustomButton::~CustomButton() {} |
| 59 | 59 |
| 60 void CustomButton::SetState(ButtonState state) { | 60 void CustomButton::SetState(ButtonState state) { |
| 61 if (state == state_) | 61 if (state == state_) |
| 62 return; | 62 return; |
| 63 | 63 |
| 64 if (animate_on_state_change_ && | 64 if (animate_on_state_change_ && |
| 65 (!is_throbbing_ || !hover_animation_->is_animating())) { | 65 (!is_throbbing_ || !hover_animation_.is_animating())) { |
| 66 is_throbbing_ = false; | 66 is_throbbing_ = false; |
| 67 if ((state_ == STATE_HOVERED) && (state == STATE_NORMAL)) { | 67 if ((state_ == STATE_HOVERED) && (state == STATE_NORMAL)) { |
| 68 // For HOVERED -> NORMAL, animate from hovered (1) to not hovered (0). | 68 // For HOVERED -> NORMAL, animate from hovered (1) to not hovered (0). |
| 69 hover_animation_->Hide(); | 69 hover_animation_.Hide(); |
| 70 } else if (state != STATE_HOVERED) { | 70 } else if (state != STATE_HOVERED) { |
| 71 // For HOVERED -> PRESSED/DISABLED, or any transition not involving | 71 // For HOVERED -> PRESSED/DISABLED, or any transition not involving |
| 72 // HOVERED at all, simply set the state to not hovered (0). | 72 // HOVERED at all, simply set the state to not hovered (0). |
| 73 hover_animation_->Reset(); | 73 hover_animation_.Reset(); |
| 74 } else if (state_ == STATE_NORMAL) { | 74 } else if (state_ == STATE_NORMAL) { |
| 75 // For NORMAL -> HOVERED, animate from not hovered (0) to hovered (1). | 75 // For NORMAL -> HOVERED, animate from not hovered (0) to hovered (1). |
| 76 hover_animation_->Show(); | 76 hover_animation_.Show(); |
| 77 } else { | 77 } else { |
| 78 // For PRESSED/DISABLED -> HOVERED, simply set the state to hovered (1). | 78 // For PRESSED/DISABLED -> HOVERED, simply set the state to hovered (1). |
| 79 hover_animation_->Reset(1); | 79 hover_animation_.Reset(1); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 state_ = state; | 83 state_ = state; |
| 84 StateChanged(); | 84 StateChanged(); |
| 85 SchedulePaint(); | 85 SchedulePaint(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void CustomButton::StartThrobbing(int cycles_til_stop) { | 88 void CustomButton::StartThrobbing(int cycles_til_stop) { |
| 89 is_throbbing_ = true; | 89 is_throbbing_ = true; |
| 90 hover_animation_->StartThrobbing(cycles_til_stop); | 90 hover_animation_.StartThrobbing(cycles_til_stop); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void CustomButton::StopThrobbing() { | 93 void CustomButton::StopThrobbing() { |
| 94 if (hover_animation_->is_animating()) { | 94 if (hover_animation_.is_animating()) { |
| 95 hover_animation_->Stop(); | 95 hover_animation_.Stop(); |
| 96 SchedulePaint(); | 96 SchedulePaint(); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 void CustomButton::SetAnimationDuration(int duration) { | 100 void CustomButton::SetAnimationDuration(int duration) { |
| 101 hover_animation_->SetSlideDuration(duration); | 101 hover_animation_.SetSlideDuration(duration); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void CustomButton::SetHotTracked(bool is_hot_tracked) { | 104 void CustomButton::SetHotTracked(bool is_hot_tracked) { |
| 105 if (state_ != STATE_DISABLED) | 105 if (state_ != STATE_DISABLED) |
| 106 SetState(is_hot_tracked ? STATE_HOVERED : STATE_NORMAL); | 106 SetState(is_hot_tracked ? STATE_HOVERED : STATE_NORMAL); |
| 107 | 107 |
| 108 if (is_hot_tracked) | 108 if (is_hot_tracked) |
| 109 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true); | 109 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true); |
| 110 } | 110 } |
| 111 | 111 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 Button::OnGestureEvent(event); | 236 Button::OnGestureEvent(event); |
| 237 return; | 237 return; |
| 238 } | 238 } |
| 239 | 239 |
| 240 if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) { | 240 if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) { |
| 241 // Set the button state to hot and start the animation fully faded in. The | 241 // Set the button state to hot and start the animation fully faded in. The |
| 242 // GESTURE_END event issued immediately after will set the state to | 242 // GESTURE_END event issued immediately after will set the state to |
| 243 // STATE_NORMAL beginning the fade out animation. See | 243 // STATE_NORMAL beginning the fade out animation. See |
| 244 // http://crbug.com/131184. | 244 // http://crbug.com/131184. |
| 245 SetState(STATE_HOVERED); | 245 SetState(STATE_HOVERED); |
| 246 hover_animation_->Reset(1.0); | 246 hover_animation_.Reset(1.0); |
| 247 NotifyClick(*event); | 247 NotifyClick(*event); |
| 248 event->StopPropagation(); | 248 event->StopPropagation(); |
| 249 } else if (event->type() == ui::ET_GESTURE_TAP_DOWN && | 249 } else if (event->type() == ui::ET_GESTURE_TAP_DOWN && |
| 250 ShouldEnterPushedState(*event)) { | 250 ShouldEnterPushedState(*event)) { |
| 251 SetState(STATE_PRESSED); | 251 SetState(STATE_PRESSED); |
| 252 if (request_focus_on_press_) | 252 if (request_focus_on_press_) |
| 253 RequestFocus(); | 253 RequestFocus(); |
| 254 event->StopPropagation(); | 254 event->StopPropagation(); |
| 255 } else if (event->type() == ui::ET_GESTURE_TAP_CANCEL || | 255 } else if (event->type() == ui::ET_GESTURE_TAP_CANCEL || |
| 256 event->type() == ui::ET_GESTURE_END) { | 256 event->type() == ui::ET_GESTURE_END) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 void CustomButton::AnimationProgressed(const gfx::Animation* animation) { | 329 void CustomButton::AnimationProgressed(const gfx::Animation* animation) { |
| 330 SchedulePaint(); | 330 SchedulePaint(); |
| 331 } | 331 } |
| 332 | 332 |
| 333 //////////////////////////////////////////////////////////////////////////////// | 333 //////////////////////////////////////////////////////////////////////////////// |
| 334 // CustomButton, protected: | 334 // CustomButton, protected: |
| 335 | 335 |
| 336 CustomButton::CustomButton(ButtonListener* listener) | 336 CustomButton::CustomButton(ButtonListener* listener) |
| 337 : Button(listener), | 337 : Button(listener), |
| 338 state_(STATE_NORMAL), | 338 state_(STATE_NORMAL), |
| 339 hover_animation_(this), |
| 339 animate_on_state_change_(true), | 340 animate_on_state_change_(true), |
| 340 is_throbbing_(false), | 341 is_throbbing_(false), |
| 341 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON), | 342 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON), |
| 342 request_focus_on_press_(true), | 343 request_focus_on_press_(true), |
| 343 ink_drop_delegate_(nullptr), | 344 ink_drop_delegate_(nullptr), |
| 344 notify_action_(NOTIFY_ON_RELEASE), | 345 notify_action_(NOTIFY_ON_RELEASE), |
| 345 has_ink_drop_action_on_click_(false), | 346 has_ink_drop_action_on_click_(false), |
| 346 ink_drop_action_on_click_(InkDropState::QUICK_ACTION) { | 347 ink_drop_action_on_click_(InkDropState::QUICK_ACTION) { |
| 347 hover_animation_.reset(new gfx::ThrobAnimation(this)); | 348 hover_animation_.SetSlideDuration(kHoverFadeDurationMs); |
| 348 hover_animation_->SetSlideDuration(kHoverFadeDurationMs); | |
| 349 } | 349 } |
| 350 | 350 |
| 351 void CustomButton::StateChanged() { | 351 void CustomButton::StateChanged() { |
| 352 } | 352 } |
| 353 | 353 |
| 354 bool CustomButton::IsTriggerableEvent(const ui::Event& event) { | 354 bool CustomButton::IsTriggerableEvent(const ui::Event& event) { |
| 355 return event.type() == ui::ET_GESTURE_TAP_DOWN || | 355 return event.type() == ui::ET_GESTURE_TAP_DOWN || |
| 356 event.type() == ui::ET_GESTURE_TAP || | 356 event.type() == ui::ET_GESTURE_TAP || |
| 357 (event.IsMouseEvent() && | 357 (event.IsMouseEvent() && |
| 358 (triggerable_event_flags_ & event.flags()) != 0); | 358 (triggerable_event_flags_ & event.flags()) != 0); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 return GetWidget() && GetWidget()->GetTopLevelWidget() != GetWidget(); | 422 return GetWidget() && GetWidget()->GetTopLevelWidget() != GetWidget(); |
| 423 } | 423 } |
| 424 | 424 |
| 425 bool CustomButton::FocusInChildWidget() const { | 425 bool CustomButton::FocusInChildWidget() const { |
| 426 return GetWidget() && | 426 return GetWidget() && |
| 427 GetWidget()->GetRootView()->Contains( | 427 GetWidget()->GetRootView()->Contains( |
| 428 GetFocusManager()->GetFocusedView()); | 428 GetFocusManager()->GetFocusedView()); |
| 429 } | 429 } |
| 430 | 430 |
| 431 } // namespace views | 431 } // namespace views |
| OLD | NEW |