Chromium Code Reviews| 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/base/material_design/material_design_controller.h" | 8 #include "ui/base/material_design/material_design_controller.h" |
| 9 #include "ui/events/event.h" | 9 #include "ui/events/event.h" |
| 10 #include "ui/events/event_utils.h" | 10 #include "ui/events/event_utils.h" |
| 11 #include "ui/events/keycodes/keyboard_codes.h" | 11 #include "ui/events/keycodes/keyboard_codes.h" |
| 12 #include "ui/gfx/animation/throb_animation.h" | 12 #include "ui/gfx/animation/throb_animation.h" |
| 13 #include "ui/gfx/canvas.h" | |
| 13 #include "ui/gfx/color_palette.h" | 14 #include "ui/gfx/color_palette.h" |
| 14 #include "ui/gfx/screen.h" | 15 #include "ui/gfx/screen.h" |
| 16 #include "ui/native_theme/native_theme.h" | |
| 15 #include "ui/views/animation/ink_drop_delegate.h" | 17 #include "ui/views/animation/ink_drop_delegate.h" |
| 16 #include "ui/views/animation/ink_drop_hover.h" | 18 #include "ui/views/animation/ink_drop_hover.h" |
| 17 #include "ui/views/controls/button/blue_button.h" | 19 #include "ui/views/controls/button/blue_button.h" |
| 18 #include "ui/views/controls/button/checkbox.h" | 20 #include "ui/views/controls/button/checkbox.h" |
| 19 #include "ui/views/controls/button/image_button.h" | 21 #include "ui/views/controls/button/image_button.h" |
| 20 #include "ui/views/controls/button/label_button.h" | 22 #include "ui/views/controls/button/label_button.h" |
| 21 #include "ui/views/controls/button/menu_button.h" | 23 #include "ui/views/controls/button/menu_button.h" |
| 22 #include "ui/views/controls/button/radio_button.h" | 24 #include "ui/views/controls/button/radio_button.h" |
| 23 #include "ui/views/widget/widget.h" | 25 #include "ui/views/widget/widget.h" |
| 24 | 26 |
| 25 #if defined(USE_AURA) | 27 #if defined(USE_AURA) |
| 26 #include "ui/aura/client/capture_client.h" | 28 #include "ui/aura/client/capture_client.h" |
| 27 #include "ui/aura/window.h" | 29 #include "ui/aura/window.h" |
| 28 #endif | 30 #endif |
| 29 | 31 |
| 30 namespace views { | 32 namespace views { |
| 31 | 33 |
| 34 namespace { | |
| 35 | |
| 32 // How long the hover animation takes if uninterrupted. | 36 // How long the hover animation takes if uninterrupted. |
| 33 static const int kHoverFadeDurationMs = 150; | 37 const int kHoverFadeDurationMs = 150; |
| 34 | 38 |
| 35 // static | 39 // The amount to enlarge the focus border in all directions relative to the |
| 36 const char CustomButton::kViewClassName[] = "CustomButton"; | 40 // button. |
| 41 const int kFocusBorderOutset = -2; | |
| 42 | |
| 43 // The corner radius of the focus border roundrect. | |
| 44 const int kFocusBorderCornerRadius = 3; | |
| 45 | |
| 46 class MdFocusRing : public views::View { | |
| 47 public: | |
| 48 MdFocusRing() { | |
| 49 SetPaintToLayer(true); | |
| 50 layer()->SetFillsBoundsOpaquely(false); | |
| 51 } | |
| 52 ~MdFocusRing() override {} | |
| 53 | |
| 54 void OnPaint(gfx::Canvas* canvas) override { | |
| 55 SkPaint paint; | |
| 56 paint.setAntiAlias(true); | |
| 57 paint.setColor(GetNativeTheme()->GetSystemColor( | |
| 58 ui::NativeTheme::kColorId_CallToActionColor)); | |
| 59 paint.setStyle(SkPaint::kStroke_Style); | |
| 60 paint.setStrokeWidth(1); | |
| 61 gfx::RectF rect(GetLocalBounds()); | |
| 62 rect.Inset(gfx::InsetsF(0.5)); | |
| 63 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 DISALLOW_COPY_AND_ASSIGN(MdFocusRing); | |
| 68 }; | |
| 69 | |
| 70 } // namespace | |
| 37 | 71 |
| 38 //////////////////////////////////////////////////////////////////////////////// | 72 //////////////////////////////////////////////////////////////////////////////// |
| 39 // CustomButton, public: | 73 // CustomButton, public: |
| 40 | 74 |
| 41 // static | 75 // static |
| 76 const char CustomButton::kViewClassName[] = "CustomButton"; | |
| 77 | |
| 78 // static | |
| 42 const CustomButton* CustomButton::AsCustomButton(const views::View* view) { | 79 const CustomButton* CustomButton::AsCustomButton(const views::View* view) { |
| 43 return AsCustomButton(const_cast<views::View*>(view)); | 80 return AsCustomButton(const_cast<views::View*>(view)); |
| 44 } | 81 } |
| 45 | 82 |
| 46 CustomButton* CustomButton::AsCustomButton(views::View* view) { | 83 CustomButton* CustomButton::AsCustomButton(views::View* view) { |
| 47 if (view) { | 84 if (view) { |
| 48 const char* classname = view->GetClassName(); | 85 const char* classname = view->GetClassName(); |
| 49 if (!strcmp(classname, Checkbox::kViewClassName) || | 86 if (!strcmp(classname, Checkbox::kViewClassName) || |
| 50 !strcmp(classname, CustomButton::kViewClassName) || | 87 !strcmp(classname, CustomButton::kViewClassName) || |
| 51 !strcmp(classname, ImageButton::kViewClassName) || | 88 !strcmp(classname, ImageButton::kViewClassName) || |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 } | 382 } |
| 346 | 383 |
| 347 //////////////////////////////////////////////////////////////////////////////// | 384 //////////////////////////////////////////////////////////////////////////////// |
| 348 // CustomButton, gfx::AnimationDelegate implementation: | 385 // CustomButton, gfx::AnimationDelegate implementation: |
| 349 | 386 |
| 350 void CustomButton::AnimationProgressed(const gfx::Animation* animation) { | 387 void CustomButton::AnimationProgressed(const gfx::Animation* animation) { |
| 351 SchedulePaint(); | 388 SchedulePaint(); |
| 352 } | 389 } |
| 353 | 390 |
| 354 //////////////////////////////////////////////////////////////////////////////// | 391 //////////////////////////////////////////////////////////////////////////////// |
| 392 // CustomButton, View overrides (public): | |
| 393 | |
| 394 void CustomButton::Layout() { | |
| 395 Button::Layout(); | |
| 396 gfx::Rect focus_bounds = GetLocalBounds(); | |
| 397 focus_bounds.Inset(gfx::Insets(kFocusBorderOutset)); | |
| 398 if (md_focus_ring_) | |
| 399 md_focus_ring_->SetBoundsRect(focus_bounds); | |
| 400 } | |
| 401 | |
| 402 void CustomButton::ViewHierarchyChanged( | |
| 403 const ViewHierarchyChangedDetails& details) { | |
| 404 if (!details.is_add && state_ != STATE_DISABLED) | |
| 405 SetState(STATE_NORMAL); | |
| 406 } | |
| 407 | |
| 408 void CustomButton::OnFocus() { | |
| 409 Button::OnFocus(); | |
| 410 if (md_focus_ring_) | |
| 411 md_focus_ring_->SetVisible(true); | |
| 412 } | |
| 413 | |
| 414 void CustomButton::OnBlur() { | |
| 415 Button::OnBlur(); | |
| 416 if (IsHotTracked()) | |
| 417 SetState(STATE_NORMAL); | |
| 418 if (md_focus_ring_) | |
| 419 md_focus_ring_->SetVisible(false); | |
| 420 } | |
| 421 | |
| 422 //////////////////////////////////////////////////////////////////////////////// | |
| 355 // CustomButton, protected: | 423 // CustomButton, protected: |
| 356 | 424 |
| 357 CustomButton::CustomButton(ButtonListener* listener) | 425 CustomButton::CustomButton(ButtonListener* listener) |
| 358 : Button(listener), | 426 : Button(listener), |
| 359 state_(STATE_NORMAL), | 427 state_(STATE_NORMAL), |
| 360 hover_animation_(this), | 428 hover_animation_(this), |
| 361 animate_on_state_change_(true), | 429 animate_on_state_change_(true), |
| 362 is_throbbing_(false), | 430 is_throbbing_(false), |
| 363 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON), | 431 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON), |
| 364 request_focus_on_press_(true), | 432 request_focus_on_press_(true), |
| 365 ink_drop_delegate_(nullptr), | 433 ink_drop_delegate_(nullptr), |
| 366 notify_action_(NOTIFY_ON_RELEASE), | 434 notify_action_(NOTIFY_ON_RELEASE), |
| 367 has_ink_drop_action_on_click_(false), | 435 has_ink_drop_action_on_click_(false), |
| 368 ink_drop_action_on_click_(InkDropState::QUICK_ACTION), | 436 ink_drop_action_on_click_(InkDropState::QUICK_ACTION), |
| 369 ink_drop_base_color_(gfx::kPlaceholderColor) { | 437 ink_drop_base_color_(gfx::kPlaceholderColor), |
| 438 md_focus_ring_(nullptr) { | |
| 370 hover_animation_.SetSlideDuration(kHoverFadeDurationMs); | 439 hover_animation_.SetSlideDuration(kHoverFadeDurationMs); |
| 371 } | 440 } |
| 372 | 441 |
| 373 void CustomButton::StateChanged() { | 442 void CustomButton::StateChanged() { |
| 374 } | 443 } |
| 375 | 444 |
| 376 bool CustomButton::IsTriggerableEvent(const ui::Event& event) { | 445 bool CustomButton::IsTriggerableEvent(const ui::Event& event) { |
| 377 return event.type() == ui::ET_GESTURE_TAP_DOWN || | 446 return event.type() == ui::ET_GESTURE_TAP_DOWN || |
| 378 event.type() == ui::ET_GESTURE_TAP || | 447 event.type() == ui::ET_GESTURE_TAP || |
| 379 (event.IsMouseEvent() && | 448 (event.IsMouseEvent() && |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 405 aura::client::GetCaptureClient(root_window); | 474 aura::client::GetCaptureClient(root_window); |
| 406 aura::Window* capture_window = | 475 aura::Window* capture_window = |
| 407 capture_client ? capture_client->GetGlobalCaptureWindow() : nullptr; | 476 capture_client ? capture_client->GetGlobalCaptureWindow() : nullptr; |
| 408 check_mouse_position = !capture_window || capture_window == root_window; | 477 check_mouse_position = !capture_window || capture_window == root_window; |
| 409 } | 478 } |
| 410 #endif | 479 #endif |
| 411 | 480 |
| 412 return check_mouse_position && IsMouseHovered(); | 481 return check_mouse_position && IsMouseHovered(); |
| 413 } | 482 } |
| 414 | 483 |
| 415 //////////////////////////////////////////////////////////////////////////////// | 484 void CustomButton::UseMdFocusRing() { |
| 416 // CustomButton, View overrides (protected): | 485 md_focus_ring_ = new MdFocusRing(); |
|
sky
2016/03/22 15:31:40
DCHECK(!md_focus_ring_), or perhaps this code shou
Evan Stade
2016/03/22 17:35:44
Done.
| |
| 417 | 486 AddChildView(md_focus_ring_); |
| 418 void CustomButton::ViewHierarchyChanged( | 487 md_focus_ring_->SetVisible(false); |
| 419 const ViewHierarchyChangedDetails& details) { | 488 set_request_focus_on_press(false); |
| 420 if (!details.is_add && state_ != STATE_DISABLED) | |
| 421 SetState(STATE_NORMAL); | |
| 422 } | 489 } |
| 423 | 490 |
| 424 void CustomButton::OnBlur() { | 491 //////////////////////////////////////////////////////////////////////////////// |
| 425 if (IsHotTracked()) | 492 // CustomButton, Button overrides (protected): |
| 426 SetState(STATE_NORMAL); | |
| 427 } | |
| 428 | 493 |
| 429 void CustomButton::NotifyClick(const ui::Event& event) { | 494 void CustomButton::NotifyClick(const ui::Event& event) { |
| 430 if (ink_drop_delegate() && has_ink_drop_action_on_click_) | 495 if (ink_drop_delegate() && has_ink_drop_action_on_click_) |
| 431 ink_drop_delegate()->OnAction(ink_drop_action_on_click_); | 496 ink_drop_delegate()->OnAction(ink_drop_action_on_click_); |
| 432 Button::NotifyClick(event); | 497 Button::NotifyClick(event); |
| 433 } | 498 } |
| 434 | 499 |
| 435 void CustomButton::OnClickCanceled(const ui::Event& event) { | 500 void CustomButton::OnClickCanceled(const ui::Event& event) { |
| 436 if (ink_drop_delegate()) | 501 if (ink_drop_delegate()) |
| 437 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); | 502 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); |
| 438 Button::OnClickCanceled(event); | 503 Button::OnClickCanceled(event); |
| 439 } | 504 } |
| 440 | 505 |
| 441 } // namespace views | 506 } // namespace views |
| OLD | NEW |