Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / views / controls / button / custom_button.cc
blob5040c4ea9cf13cf814f091766d2af59e6f9bceb7
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 // TODO(beng): remove once NotifyClick takes ui::Event.
201 ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED, gfx::Point(),
202 gfx::Point(), ui::EventTimeForNow(),
203 ui::EF_LEFT_MOUSE_BUTTON,
204 ui::EF_LEFT_MOUSE_BUTTON);
205 NotifyClick(synthetic_event);
206 } else {
207 return false;
209 return true;
212 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) {
213 if ((state_ == STATE_DISABLED) || (event.key_code() != ui::VKEY_SPACE))
214 return false;
216 SetState(STATE_NORMAL);
217 // TODO(beng): remove once NotifyClick takes ui::Event.
218 ui::MouseEvent synthetic_event(
219 ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
220 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
221 NotifyClick(synthetic_event);
222 return true;
225 void CustomButton::OnGestureEvent(ui::GestureEvent* event) {
226 if (state_ == STATE_DISABLED) {
227 Button::OnGestureEvent(event);
228 return;
231 if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) {
232 // Set the button state to hot and start the animation fully faded in. The
233 // GESTURE_END event issued immediately after will set the state to
234 // STATE_NORMAL beginning the fade out animation. See
235 // http://crbug.com/131184.
236 SetState(STATE_HOVERED);
237 hover_animation_->Reset(1.0);
238 NotifyClick(*event);
239 event->StopPropagation();
240 } else if (event->type() == ui::ET_GESTURE_TAP_DOWN &&
241 ShouldEnterPushedState(*event)) {
242 SetState(STATE_PRESSED);
243 if (request_focus_on_press_)
244 RequestFocus();
245 event->StopPropagation();
246 } else if (event->type() == ui::ET_GESTURE_TAP_CANCEL ||
247 event->type() == ui::ET_GESTURE_END) {
248 SetState(STATE_NORMAL);
250 if (!event->handled())
251 Button::OnGestureEvent(event);
254 bool CustomButton::AcceleratorPressed(const ui::Accelerator& accelerator) {
255 SetState(STATE_NORMAL);
256 // TODO(beng): remove once NotifyClick takes ui::Event.
257 ui::MouseEvent synthetic_event(
258 ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
259 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
260 NotifyClick(synthetic_event);
261 return true;
264 void CustomButton::ShowContextMenu(const gfx::Point& p,
265 ui::MenuSourceType source_type) {
266 if (!context_menu_controller())
267 return;
269 // We're about to show the context menu. Showing the context menu likely means
270 // we won't get a mouse exited and reset state. Reset it now to be sure.
271 if (state_ != STATE_DISABLED)
272 SetState(STATE_NORMAL);
273 View::ShowContextMenu(p, source_type);
276 void CustomButton::OnDragDone() {
277 // Only reset the state to normal if the button isn't currently disabled
278 // (since disabled buttons may still be able to be dragged).
279 if (state_ != STATE_DISABLED)
280 SetState(STATE_NORMAL);
283 void CustomButton::GetAccessibleState(ui::AXViewState* state) {
284 Button::GetAccessibleState(state);
285 switch (state_) {
286 case STATE_HOVERED:
287 state->AddStateFlag(ui::AX_STATE_HOVERED);
288 break;
289 case STATE_PRESSED:
290 state->AddStateFlag(ui::AX_STATE_PRESSED);
291 break;
292 case STATE_DISABLED:
293 state->AddStateFlag(ui::AX_STATE_DISABLED);
294 break;
295 case STATE_NORMAL:
296 case STATE_COUNT:
297 // No additional accessibility state set for this button state.
298 break;
302 void CustomButton::VisibilityChanged(View* starting_from, bool visible) {
303 if (state_ == STATE_DISABLED)
304 return;
305 SetState(visible && IsMouseHovered() ? STATE_HOVERED : STATE_NORMAL);
308 ////////////////////////////////////////////////////////////////////////////////
309 // CustomButton, gfx::AnimationDelegate implementation:
311 void CustomButton::AnimationProgressed(const gfx::Animation* animation) {
312 SchedulePaint();
315 ////////////////////////////////////////////////////////////////////////////////
316 // CustomButton, protected:
318 CustomButton::CustomButton(ButtonListener* listener)
319 : Button(listener),
320 state_(STATE_NORMAL),
321 animate_on_state_change_(true),
322 is_throbbing_(false),
323 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON),
324 request_focus_on_press_(true),
325 notify_action_(NOTIFY_ON_RELEASE) {
326 hover_animation_.reset(new gfx::ThrobAnimation(this));
327 hover_animation_->SetSlideDuration(kHoverFadeDurationMs);
330 void CustomButton::StateChanged() {
333 bool CustomButton::IsTriggerableEvent(const ui::Event& event) {
334 return event.type() == ui::ET_GESTURE_TAP_DOWN ||
335 event.type() == ui::ET_GESTURE_TAP ||
336 (event.IsMouseEvent() &&
337 (triggerable_event_flags_ & event.flags()) != 0);
340 bool CustomButton::ShouldEnterPushedState(const ui::Event& event) {
341 return IsTriggerableEvent(event);
344 ////////////////////////////////////////////////////////////////////////////////
345 // CustomButton, View overrides (protected):
347 void CustomButton::ViewHierarchyChanged(
348 const ViewHierarchyChangedDetails& details) {
349 if (!details.is_add && state_ != STATE_DISABLED)
350 SetState(STATE_NORMAL);
353 void CustomButton::OnBlur() {
354 if (IsHotTracked())
355 SetState(STATE_NORMAL);
358 } // namespace views