1 // Copyright 2015 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/style/mac/dialog_button_border_mac.h"
7 #include "base/logging.h"
8 #include "skia/ext/refptr.h"
9 #include "third_party/skia/include/core/SkCanvas.h"
10 #include "third_party/skia/include/core/SkDrawLooper.h"
11 #include "third_party/skia/include/core/SkPaint.h"
12 #include "third_party/skia/include/core/SkPath.h"
13 #include "third_party/skia/include/effects/SkGradientShader.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/views/border.h"
16 #include "ui/views/controls/button/custom_button.h"
21 // Type to map button states to a corresponding SkColor.
22 typedef const SkColor ColorByState
[Button::STATE_COUNT
];
24 // If a state is added, ColorByState will silently fill with zeros, so assert.
25 static_assert(Button::STATE_COUNT
== 4,
26 "DialogButtonBorderMac assumes 4 button states.");
28 // Corner radius of rounded rectangles.
29 const SkScalar kCornerRadius
= 2;
31 // Vertical offset of the drop shadow and the inner highlight shadow.
32 const SkScalar kShadowOffsetY
= 1;
34 // Shadow blur radius of the inner highlight shadow.
35 const double kInnerShadowBlurRadius
= 2.0;
37 // Default border insets, to provide text padding.
38 const int kPaddingX
= 14;
39 const int kPaddingY
= 4;
41 skia::RefPtr
<SkShader
> GetButtonGradient(int height
,
42 Button::ButtonState state
) {
43 ColorByState start
= {0xFFF0F0F0, 0xFFF4F4F4, 0xFFEBEBEB, 0xFFEDEDED};
44 ColorByState end
= {0xFFE0E0E0, 0xFFE4E4E4, 0xFFDBDBDB, 0xFFDEDEDE};
46 SkPoint gradient_points
[2];
47 gradient_points
[0].iset(0, 0);
48 gradient_points
[1].iset(0, height
);
50 SkColor gradient_colors
[] = {start
[state
], start
[state
], end
[state
]};
51 SkScalar gradient_positions
[] = {0.0, 0.38, 1.0};
53 skia::RefPtr
<SkShader
> gradient_shader
=
54 skia::AdoptRef(SkGradientShader::CreateLinear(
55 gradient_points
, gradient_colors
, gradient_positions
, 3,
56 SkShader::kClamp_TileMode
));
57 return gradient_shader
;
60 void DrawConstrainedButtonBackground(const SkRect
& button_rect
,
62 Button::ButtonState button_state
) {
63 // Extend the size of the SkRect so the border stroke is drawn over it on all
65 SkRect
rect(button_rect
);
72 ColorByState shadow
= {0x14000000, 0x1F000000, 0x00000000, 0x00000000};
73 const double blur
= 0.0;
74 std::vector
<gfx::ShadowValue
> shadows(
75 1, gfx::ShadowValue(gfx::Vector2d(0, kShadowOffsetY
), blur
,
76 shadow
[button_state
]));
77 skia::RefPtr
<SkDrawLooper
> looper
= gfx::CreateShadowDrawLooper(shadows
);
78 paint
.setLooper(looper
.get());
81 skia::RefPtr
<SkShader
> gradient_shader
=
82 GetButtonGradient(rect
.height(), button_state
);
83 paint
.setShader(gradient_shader
.get());
84 paint
.setStyle(SkPaint::kFill_Style
);
85 paint
.setFlags(SkPaint::kAntiAlias_Flag
);
86 canvas
->drawRoundRect(rect
, kCornerRadius
, kCornerRadius
, paint
);
89 // Draws an inner box shadow inside a rounded rectangle of size |rect|. The
90 // technique: draw a black "ring" around the outside of the button cell. Then
91 // clip out everything except the shadow it casts. Works similar to Blink's
92 // GraphicsContext::drawInnerShadow().
93 void DrawRoundRectInnerShadow(const SkRect
& rect
,
95 SkColor shadow_color
) {
96 const gfx::Vector2d
shadow_offset(0, kShadowOffsetY
);
98 outer
.outset(abs(shadow_offset
.x()) + kInnerShadowBlurRadius
,
99 abs(shadow_offset
.y()) + kInnerShadowBlurRadius
);
103 path
.setFillType(SkPath::kEvenOdd_FillType
);
104 path
.addRoundRect(rect
, kCornerRadius
, kCornerRadius
); // Poke a hole.
106 // Inset the clip to cater for the border stroke.
108 clip
.addRoundRect(rect
.makeInset(0.5, 0.5), kCornerRadius
, kCornerRadius
);
111 std::vector
<gfx::ShadowValue
> shadows(
112 1, gfx::ShadowValue(shadow_offset
, kInnerShadowBlurRadius
, shadow_color
));
113 skia::RefPtr
<SkDrawLooper
> looper
= gfx::CreateShadowDrawLooper(shadows
);
114 paint
.setLooper(looper
.get());
115 paint
.setStyle(SkPaint::kFill_Style
);
116 paint
.setColor(SK_ColorBLACK
); // Note: Entirely clipped.
119 canvas
->clipPath(clip
, SkRegion::kIntersect_Op
, true /* antialias */);
120 canvas
->drawPath(path
, paint
);
126 DialogButtonBorderMac::DialogButtonBorderMac() {
127 set_insets(gfx::Insets(kPaddingY
, kPaddingX
, kPaddingY
, kPaddingX
));
130 DialogButtonBorderMac::~DialogButtonBorderMac() {}
132 void DialogButtonBorderMac::Paint(const View
& view
, gfx::Canvas
* canvas
) {
133 DCHECK(CustomButton::AsCustomButton(&view
));
134 const CustomButton
& button
= static_cast<const CustomButton
&>(view
);
135 SkCanvas
* canvas_skia
= canvas
->sk_canvas();
137 // Inset all sides for the rounded rectangle stroke. Inset again to make room
138 // for the shadows (while keeping the text centered).
139 SkRect sk_rect
= gfx::RectToSkRect(view
.GetLocalBounds());
140 sk_rect
.inset(2.0, 2.0);
142 DrawConstrainedButtonBackground(sk_rect
, canvas_skia
, button
.state());
144 // Offset so that strokes are contained within the pixel boundary.
145 sk_rect
.offset(0.5, 0.5);
146 ColorByState highlight
= {0xBFFFFFFF, 0xF2FFFFFF, 0x24000000, 0x00000000};
147 DrawRoundRectInnerShadow(sk_rect
, canvas_skia
, highlight
[button
.state()]);
149 // Border or focus ring.
151 ColorByState border
= {0x40000000, 0x4D000000, 0x4D000000, 0x1F000000};
152 const SkColor focus_border
= {0xFF5DA5FF};
153 paint
.setStrokeWidth(1);
154 paint
.setStyle(SkPaint::kStroke_Style
);
155 paint
.setFlags(SkPaint::kAntiAlias_Flag
);
156 if (button
.HasFocus() && button
.state() != Button::STATE_PRESSED
)
157 paint
.setColor(focus_border
);
159 paint
.setColor(border
[button
.state()]);
160 canvas_skia
->drawRoundRect(sk_rect
, kCornerRadius
, kCornerRadius
, paint
);
163 gfx::Size
DialogButtonBorderMac::GetMinimumSize() const {
164 return gfx::Size(100, 30);