Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: ui/views/controls/button/custom_button.cc

Issue 1817253003: [MD] Use same focus ring on BarControlButton as MdTextButton. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update docs Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/views/controls/button/custom_button.h ('k') | ui/views/controls/button/image_button.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/button/custom_button.cc
diff --git a/ui/views/controls/button/custom_button.cc b/ui/views/controls/button/custom_button.cc
index d655ac34c148bbeff8f95d633a5fb571cc081720..fc07e0b7b0fa4bf42ef5db8e41cb481045232006 100644
--- a/ui/views/controls/button/custom_button.cc
+++ b/ui/views/controls/button/custom_button.cc
@@ -10,8 +10,10 @@
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/animation/throb_animation.h"
+#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/screen.h"
+#include "ui/native_theme/native_theme.h"
#include "ui/views/animation/ink_drop_delegate.h"
#include "ui/views/animation/ink_drop_hover.h"
#include "ui/views/controls/button/blue_button.h"
@@ -29,16 +31,51 @@
namespace views {
+namespace {
+
// How long the hover animation takes if uninterrupted.
-static const int kHoverFadeDurationMs = 150;
+const int kHoverFadeDurationMs = 150;
-// static
-const char CustomButton::kViewClassName[] = "CustomButton";
+// The amount to enlarge the focus border in all directions relative to the
+// button.
+const int kFocusBorderOutset = -2;
+
+// The corner radius of the focus border roundrect.
+const int kFocusBorderCornerRadius = 3;
+
+class MdFocusRing : public views::View {
+ public:
+ MdFocusRing() {
+ SetPaintToLayer(true);
+ layer()->SetFillsBoundsOpaquely(false);
+ }
+ ~MdFocusRing() override {}
+
+ void OnPaint(gfx::Canvas* canvas) override {
+ SkPaint paint;
+ paint.setAntiAlias(true);
+ paint.setColor(GetNativeTheme()->GetSystemColor(
+ ui::NativeTheme::kColorId_CallToActionColor));
+ paint.setStyle(SkPaint::kStroke_Style);
+ paint.setStrokeWidth(1);
+ gfx::RectF rect(GetLocalBounds());
+ rect.Inset(gfx::InsetsF(0.5));
+ canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint);
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MdFocusRing);
+};
+
+} // namespace
////////////////////////////////////////////////////////////////////////////////
// CustomButton, public:
// static
+const char CustomButton::kViewClassName[] = "CustomButton";
+
+// static
const CustomButton* CustomButton::AsCustomButton(const views::View* view) {
return AsCustomButton(const_cast<views::View*>(view));
}
@@ -352,6 +389,37 @@ void CustomButton::AnimationProgressed(const gfx::Animation* animation) {
}
////////////////////////////////////////////////////////////////////////////////
+// CustomButton, View overrides (public):
+
+void CustomButton::Layout() {
+ Button::Layout();
+ gfx::Rect focus_bounds = GetLocalBounds();
+ focus_bounds.Inset(gfx::Insets(kFocusBorderOutset));
+ if (md_focus_ring_)
+ md_focus_ring_->SetBoundsRect(focus_bounds);
+}
+
+void CustomButton::ViewHierarchyChanged(
+ const ViewHierarchyChangedDetails& details) {
+ if (!details.is_add && state_ != STATE_DISABLED)
+ SetState(STATE_NORMAL);
+}
+
+void CustomButton::OnFocus() {
+ Button::OnFocus();
+ if (md_focus_ring_)
+ md_focus_ring_->SetVisible(true);
+}
+
+void CustomButton::OnBlur() {
+ Button::OnBlur();
+ if (IsHotTracked())
+ SetState(STATE_NORMAL);
+ if (md_focus_ring_)
+ md_focus_ring_->SetVisible(false);
+}
+
+////////////////////////////////////////////////////////////////////////////////
// CustomButton, protected:
CustomButton::CustomButton(ButtonListener* listener)
@@ -366,7 +434,8 @@ CustomButton::CustomButton(ButtonListener* listener)
notify_action_(NOTIFY_ON_RELEASE),
has_ink_drop_action_on_click_(false),
ink_drop_action_on_click_(InkDropState::QUICK_ACTION),
- ink_drop_base_color_(gfx::kPlaceholderColor) {
+ ink_drop_base_color_(gfx::kPlaceholderColor),
+ md_focus_ring_(nullptr) {
hover_animation_.SetSlideDuration(kHoverFadeDurationMs);
}
@@ -412,19 +481,16 @@ bool CustomButton::ShouldEnterHoveredState() {
return check_mouse_position && IsMouseHovered();
}
-////////////////////////////////////////////////////////////////////////////////
-// CustomButton, View overrides (protected):
-
-void CustomButton::ViewHierarchyChanged(
- const ViewHierarchyChangedDetails& details) {
- if (!details.is_add && state_ != STATE_DISABLED)
- SetState(STATE_NORMAL);
+void CustomButton::UseMdFocusRing() {
+ DCHECK(!md_focus_ring_);
+ md_focus_ring_ = new MdFocusRing();
+ AddChildView(md_focus_ring_);
+ md_focus_ring_->SetVisible(false);
+ set_request_focus_on_press(false);
}
-void CustomButton::OnBlur() {
- if (IsHotTracked())
- SetState(STATE_NORMAL);
-}
+////////////////////////////////////////////////////////////////////////////////
+// CustomButton, Button overrides (protected):
void CustomButton::NotifyClick(const ui::Event& event) {
if (ink_drop_delegate() && has_ink_drop_action_on_click_)
« no previous file with comments | « ui/views/controls/button/custom_button.h ('k') | ui/views/controls/button/image_button.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698