| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "views/controls/button/custom_button.h" | 5 #include "views/controls/button/custom_button.h" |
| 6 | 6 |
| 7 #include "app/throb_animation.h" | 7 #include "app/throb_animation.h" |
| 8 #include "base/keyboard_codes.h" | 8 #include "base/keyboard_codes.h" |
| 9 | 9 |
| 10 namespace views { | 10 namespace views { |
| 11 | 11 |
| 12 // How long the hover animation takes if uninterrupted. | 12 // How long the hover animation takes if uninterrupted. |
| 13 static const int kHoverFadeDurationMs = 150; | 13 static const int kHoverFadeDurationMs = 150; |
| 14 | 14 |
| 15 //////////////////////////////////////////////////////////////////////////////// | 15 //////////////////////////////////////////////////////////////////////////////// |
| 16 // CustomButton, public: | 16 // CustomButton, public: |
| 17 | 17 |
| 18 CustomButton::~CustomButton() { | 18 CustomButton::~CustomButton() { |
| 19 } | 19 } |
| 20 | 20 |
| 21 void CustomButton::SetState(ButtonState state) { | 21 void CustomButton::SetState(ButtonState state) { |
| 22 if (state != state_) { | 22 if (show_highlighted_ && state != state_) { |
| 23 if (animate_on_state_change_ || !hover_animation_->IsAnimating()) { | 23 if (animate_on_state_change_ || !hover_animation_->IsAnimating()) { |
| 24 animate_on_state_change_ = true; | 24 animate_on_state_change_ = true; |
| 25 if (state_ == BS_NORMAL && state == BS_HOT) { | 25 if (state_ == BS_NORMAL && state == BS_HOT) { |
| 26 // Button is hovered from a normal state, start hover animation. | 26 // Button is hovered from a normal state, start hover animation. |
| 27 hover_animation_->Show(); | 27 hover_animation_->Show(); |
| 28 } else if (state_ == BS_HOT && state == BS_NORMAL) { | 28 } else if (state_ == BS_HOT && state == BS_NORMAL) { |
| 29 // Button is returning to a normal state from hover, start hover | 29 // Button is returning to a normal state from hover, start hover |
| 30 // fade animation. | 30 // fade animation. |
| 31 hover_animation_->Hide(); | 31 hover_animation_->Hide(); |
| 32 } else { | 32 } else { |
| 33 hover_animation_->Stop(); | 33 hover_animation_->Stop(); |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 state_ = state; | 37 state_ = state; |
| 38 SchedulePaint(); | 38 SchedulePaint(); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 | 41 |
| 42 void CustomButton::StartThrobbing(int cycles_til_stop) { | 42 void CustomButton::StartThrobbing(int cycles_til_stop) { |
| 43 animate_on_state_change_ = false; | 43 animate_on_state_change_ = false; |
| 44 hover_animation_->StartThrobbing(cycles_til_stop); | 44 hover_animation_->StartThrobbing(cycles_til_stop); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void CustomButton::SetAnimationDuration(int duration) { | 47 void CustomButton::SetAnimationDuration(int duration) { |
| 48 hover_animation_->SetSlideDuration(duration); | 48 hover_animation_->SetSlideDuration(duration); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void CustomButton::SetShowHighlighted(bool show_highlighted) { |
| 52 show_highlighted_ = show_highlighted; |
| 53 } |
| 54 |
| 51 //////////////////////////////////////////////////////////////////////////////// | 55 //////////////////////////////////////////////////////////////////////////////// |
| 52 // CustomButton, View overrides: | 56 // CustomButton, View overrides: |
| 53 | 57 |
| 54 void CustomButton::SetEnabled(bool enabled) { | 58 void CustomButton::SetEnabled(bool enabled) { |
| 55 if (enabled && state_ == BS_DISABLED) { | 59 if (enabled && state_ == BS_DISABLED) { |
| 56 SetState(BS_NORMAL); | 60 SetState(BS_NORMAL); |
| 57 } else if (!enabled && state_ != BS_DISABLED) { | 61 } else if (!enabled && state_ != BS_DISABLED) { |
| 58 SetState(BS_DISABLED); | 62 SetState(BS_DISABLED); |
| 59 } | 63 } |
| 60 } | 64 } |
| 61 | 65 |
| 62 bool CustomButton::IsEnabled() const { | 66 bool CustomButton::IsEnabled() const { |
| 63 return state_ != BS_DISABLED; | 67 return state_ != BS_DISABLED; |
| 64 } | 68 } |
| 65 | 69 |
| 66 bool CustomButton::IsFocusable() const { | 70 bool CustomButton::IsFocusable() const { |
| 67 return (state_ != BS_DISABLED) && View::IsFocusable(); | 71 return (state_ != BS_DISABLED) && View::IsFocusable(); |
| 68 } | 72 } |
| 69 | 73 |
| 70 //////////////////////////////////////////////////////////////////////////////// | 74 //////////////////////////////////////////////////////////////////////////////// |
| 71 // CustomButton, protected: | 75 // CustomButton, protected: |
| 72 | 76 |
| 73 CustomButton::CustomButton(ButtonListener* listener) | 77 CustomButton::CustomButton(ButtonListener* listener) |
| 74 : Button(listener), | 78 : Button(listener), |
| 75 state_(BS_NORMAL), | 79 state_(BS_NORMAL), |
| 76 animate_on_state_change_(true), | 80 animate_on_state_change_(true), |
| 81 show_highlighted_(true), |
| 77 triggerable_event_flags_(MouseEvent::EF_LEFT_BUTTON_DOWN) { | 82 triggerable_event_flags_(MouseEvent::EF_LEFT_BUTTON_DOWN) { |
| 78 hover_animation_.reset(new ThrobAnimation(this)); | 83 hover_animation_.reset(new ThrobAnimation(this)); |
| 79 hover_animation_->SetSlideDuration(kHoverFadeDurationMs); | 84 hover_animation_->SetSlideDuration(kHoverFadeDurationMs); |
| 80 } | 85 } |
| 81 | 86 |
| 82 bool CustomButton::IsTriggerableEvent(const MouseEvent& e) { | 87 bool CustomButton::IsTriggerableEvent(const MouseEvent& e) { |
| 83 return (triggerable_event_flags_ & e.GetFlags()) != 0; | 88 return (triggerable_event_flags_ & e.GetFlags()) != 0; |
| 84 } | 89 } |
| 85 | 90 |
| 86 //////////////////////////////////////////////////////////////////////////////// | 91 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 | 262 |
| 258 bool CustomButton::IsHighlighted() const { | 263 bool CustomButton::IsHighlighted() const { |
| 259 return state_ == BS_HOT; | 264 return state_ == BS_HOT; |
| 260 } | 265 } |
| 261 | 266 |
| 262 bool CustomButton::IsPushed() const { | 267 bool CustomButton::IsPushed() const { |
| 263 return state_ == BS_PUSHED; | 268 return state_ == BS_PUSHED; |
| 264 } | 269 } |
| 265 | 270 |
| 266 } // namespace views | 271 } // namespace views |
| OLD | NEW |