1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/button/custom_button.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/animation/throb_animation.h"
#include "ui/gfx/screen.h"
#include "ui/views/controls/button/blue_button.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/button/radio_button.h"
#include "ui/views/widget/widget.h"
namespace views {
// How long the hover animation takes if uninterrupted.
static const int kHoverFadeDurationMs = 150;
// static
const char CustomButton::kViewClassName[] = "CustomButton";
////////////////////////////////////////////////////////////////////////////////
// CustomButton, public:
// static
const CustomButton* CustomButton::AsCustomButton(const views::View* view) {
return AsCustomButton(const_cast<views::View*>(view));
}
CustomButton* CustomButton::AsCustomButton(views::View* view) {
if (view) {
const char* classname = view->GetClassName();
if (!strcmp(classname, Checkbox::kViewClassName) ||
!strcmp(classname, CustomButton::kViewClassName) ||
!strcmp(classname, ImageButton::kViewClassName) ||
!strcmp(classname, LabelButton::kViewClassName) ||
!strcmp(classname, RadioButton::kViewClassName) ||
!strcmp(classname, MenuButton::kViewClassName)) {
return static_cast<CustomButton*>(view);
}
}
return NULL;
}
CustomButton::~CustomButton() {
}
void CustomButton::SetState(ButtonState state) {
if (state == state_)
return;
if (animate_on_state_change_ &&
(!is_throbbing_ || !hover_animation_->is_animating())) {
is_throbbing_ = false;
if (state_ == STATE_NORMAL && state == STATE_HOVERED) {
// Button is hovered from a normal state, start hover animation.
hover_animation_->Show();
} else if ((state_ == STATE_HOVERED || state_ == STATE_PRESSED)
&& state == STATE_NORMAL) {
// Button is returning to a normal state from hover, start hover
// fade animation.
hover_animation_->Hide();
} else {
hover_animation_->Stop();
}
}
state_ = state;
StateChanged();
if (state_changed_delegate_.get())
state_changed_delegate_->StateChanged(state_);
SchedulePaint();
}
void CustomButton::StartThrobbing(int cycles_til_stop) {
is_throbbing_ = true;
hover_animation_->StartThrobbing(cycles_til_stop);
}
void CustomButton::StopThrobbing() {
if (hover_animation_->is_animating()) {
hover_animation_->Stop();
SchedulePaint();
}
}
void CustomButton::SetAnimationDuration(int duration) {
hover_animation_->SetSlideDuration(duration);
}
void CustomButton::SetHotTracked(bool is_hot_tracked) {
if (state_ != STATE_DISABLED)
SetState(is_hot_tracked ? STATE_HOVERED : STATE_NORMAL);
if (is_hot_tracked)
NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
}
bool CustomButton::IsHotTracked() const {
return state_ == STATE_HOVERED;
}
////////////////////////////////////////////////////////////////////////////////
// CustomButton, View overrides:
void CustomButton::OnEnabledChanged() {
if (enabled() ? (state_ != STATE_DISABLED) : (state_ == STATE_DISABLED))
return;
if (enabled())
SetState(IsMouseHovered() ? STATE_HOVERED : STATE_NORMAL);
else
SetState(STATE_DISABLED);
}
const char* CustomButton::GetClassName() const {
return kViewClassName;
}
bool CustomButton::OnMousePressed(const ui::MouseEvent& event) {
if (state_ != STATE_DISABLED) {
if (ShouldEnterPushedState(event) && HitTestPoint(event.location()))
SetState(STATE_PRESSED);
if (request_focus_on_press_)
RequestFocus();
}
return true;
}
bool CustomButton::OnMouseDragged(const ui::MouseEvent& event) {
if (state_ != STATE_DISABLED) {
if (HitTestPoint(event.location()))
SetState(ShouldEnterPushedState(event) ? STATE_PRESSED : STATE_HOVERED);
else
SetState(STATE_NORMAL);
}
return true;
}
void CustomButton::OnMouseReleased(const ui::MouseEvent& event) {
if (state_ == STATE_DISABLED)
return;
if (!HitTestPoint(event.location())) {
SetState(STATE_NORMAL);
return;
}
SetState(STATE_HOVERED);
if (IsTriggerableEvent(event)) {
NotifyClick(event);
// NOTE: We may be deleted at this point (by the listener's notification
// handler).
}
}
void CustomButton::OnMouseCaptureLost() {
// Starting a drag results in a MouseCaptureLost, we need to ignore it.
if (state_ != STATE_DISABLED && !InDrag())
SetState(STATE_NORMAL);
}
void CustomButton::OnMouseEntered(const ui::MouseEvent& event) {
if (state_ != STATE_DISABLED)
SetState(STATE_HOVERED);
}
void CustomButton::OnMouseExited(const ui::MouseEvent& event) {
// Starting a drag results in a MouseExited, we need to ignore it.
if (state_ != STATE_DISABLED && !InDrag())
SetState(STATE_NORMAL);
}
void CustomButton::OnMouseMoved(const ui::MouseEvent& event) {
if (state_ != STATE_DISABLED)
SetState(HitTestPoint(event.location()) ? STATE_HOVERED : STATE_NORMAL);
}
bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) {
if (state_ == STATE_DISABLED)
return false;
// Space sets button state to pushed. Enter clicks the button. This matches
// the Windows native behavior of buttons, where Space clicks the button on
// KeyRelease and Enter clicks the button on KeyPressed.
if (event.key_code() == ui::VKEY_SPACE) {
SetState(STATE_PRESSED);
} else if (event.key_code() == ui::VKEY_RETURN) {
SetState(STATE_NORMAL);
// TODO(beng): remove once NotifyClick takes ui::Event.
ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED,
gfx::Point(),
gfx::Point(),
ui::EF_LEFT_MOUSE_BUTTON,
ui::EF_LEFT_MOUSE_BUTTON);
NotifyClick(synthetic_event);
} else {
return false;
}
return true;
}
bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) {
if ((state_ == STATE_DISABLED) || (event.key_code() != ui::VKEY_SPACE))
return false;
SetState(STATE_NORMAL);
// TODO(beng): remove once NotifyClick takes ui::Event.
ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED,
gfx::Point(),
gfx::Point(),
ui::EF_LEFT_MOUSE_BUTTON,
ui::EF_LEFT_MOUSE_BUTTON);
NotifyClick(synthetic_event);
return true;
}
void CustomButton::OnGestureEvent(ui::GestureEvent* event) {
if (state_ == STATE_DISABLED) {
Button::OnGestureEvent(event);
return;
}
if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) {
// Set the button state to hot and start the animation fully faded in. The
// GESTURE_END event issued immediately after will set the state to
// STATE_NORMAL beginning the fade out animation. See
// http://crbug.com/131184.
SetState(STATE_HOVERED);
hover_animation_->Reset(1.0);
NotifyClick(*event);
event->StopPropagation();
} else if (event->type() == ui::ET_GESTURE_TAP_DOWN &&
ShouldEnterPushedState(*event)) {
SetState(STATE_PRESSED);
if (request_focus_on_press_)
RequestFocus();
event->StopPropagation();
} else if (event->type() == ui::ET_GESTURE_TAP_CANCEL ||
event->type() == ui::ET_GESTURE_END) {
SetState(STATE_NORMAL);
}
if (!event->handled())
Button::OnGestureEvent(event);
}
bool CustomButton::AcceleratorPressed(const ui::Accelerator& accelerator) {
SetState(STATE_NORMAL);
// TODO(beng): remove once NotifyClick takes ui::Event.
ui::MouseEvent synthetic_event(ui::ET_MOUSE_RELEASED,
gfx::Point(),
gfx::Point(),
ui::EF_LEFT_MOUSE_BUTTON,
ui::EF_LEFT_MOUSE_BUTTON);
NotifyClick(synthetic_event);
return true;
}
void CustomButton::ShowContextMenu(const gfx::Point& p,
ui::MenuSourceType source_type) {
if (!context_menu_controller())
return;
// We're about to show the context menu. Showing the context menu likely means
// we won't get a mouse exited and reset state. Reset it now to be sure.
if (state_ != STATE_DISABLED)
SetState(STATE_NORMAL);
View::ShowContextMenu(p, source_type);
}
void CustomButton::OnDragDone() {
SetState(STATE_NORMAL);
}
void CustomButton::GetAccessibleState(ui::AXViewState* state) {
Button::GetAccessibleState(state);
switch (state_) {
case STATE_HOVERED:
state->AddStateFlag(ui::AX_STATE_HOVERED);
break;
case STATE_PRESSED:
state->AddStateFlag(ui::AX_STATE_PRESSED);
break;
case STATE_DISABLED:
state->AddStateFlag(ui::AX_STATE_DISABLED);
break;
case STATE_NORMAL:
case STATE_COUNT:
// No additional accessibility state set for this button state.
break;
}
}
void CustomButton::VisibilityChanged(View* starting_from, bool visible) {
if (state_ == STATE_DISABLED)
return;
SetState(visible && IsMouseHovered() ? STATE_HOVERED : STATE_NORMAL);
}
////////////////////////////////////////////////////////////////////////////////
// CustomButton, gfx::AnimationDelegate implementation:
void CustomButton::AnimationProgressed(const gfx::Animation* animation) {
SchedulePaint();
}
////////////////////////////////////////////////////////////////////////////////
// CustomButton, protected:
CustomButton::CustomButton(ButtonListener* listener)
: Button(listener),
state_(STATE_NORMAL),
animate_on_state_change_(true),
is_throbbing_(false),
triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON),
request_focus_on_press_(true) {
hover_animation_.reset(new gfx::ThrobAnimation(this));
hover_animation_->SetSlideDuration(kHoverFadeDurationMs);
}
void CustomButton::StateChanged() {
}
bool CustomButton::IsTriggerableEvent(const ui::Event& event) {
return event.type() == ui::ET_GESTURE_TAP_DOWN ||
event.type() == ui::ET_GESTURE_TAP ||
(event.IsMouseEvent() &&
(triggerable_event_flags_ & event.flags()) != 0);
}
bool CustomButton::ShouldEnterPushedState(const ui::Event& event) {
return IsTriggerableEvent(event);
}
////////////////////////////////////////////////////////////////////////////////
// CustomButton, View overrides (protected):
void CustomButton::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
if (!details.is_add && state_ != STATE_DISABLED)
SetState(STATE_NORMAL);
}
void CustomButton::OnBlur() {
if (IsHotTracked())
SetState(STATE_NORMAL);
}
} // namespace views
|