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/native_theme/native_theme_aura.h"
9 #include "base/logging.h"
10 #include "ui/base/layout.h"
11 #include "ui/base/nine_image_painter_factory.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/geometry/size.h"
15 #include "ui/gfx/image/image_skia.h"
16 #include "ui/gfx/nine_image_painter.h"
17 #include "ui/gfx/path.h"
18 #include "ui/gfx/skbitmap_operations.h"
19 #include "ui/gfx/skia_util.h"
20 #include "ui/native_theme/common_theme.h"
21 #include "ui/native_theme/native_theme_switches.h"
22 #include "ui/resources/grit/ui_resources.h"
24 using gfx::NineImagePainter
;
26 #define EMPTY_IMAGE_GRID { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
32 const int kScrollbarThumbImages
[NativeTheme::kNumStates
][9] = {
34 IMAGE_GRID(IDR_SCROLLBAR_THUMB_BASE_HOVER
),
35 IMAGE_GRID(IDR_SCROLLBAR_THUMB_BASE_NORMAL
),
36 IMAGE_GRID(IDR_SCROLLBAR_THUMB_BASE_PRESSED
)
39 const int kScrollbarArrowButtonImages
[NativeTheme::kNumStates
][9] = {
41 IMAGE_GRID(IDR_SCROLLBAR_ARROW_BUTTON_BASE_HOVER
),
42 IMAGE_GRID(IDR_SCROLLBAR_ARROW_BUTTON_BASE_NORMAL
),
43 IMAGE_GRID(IDR_SCROLLBAR_ARROW_BUTTON_BASE_PRESSED
)
46 const uint8 kScrollbarOverlayThumbFillAlphas
[NativeTheme::kNumStates
] = {
47 0, // Does not matter, will not paint for disabled state.
48 178, // Hover state, opacity 70%, alpha would be 0.7 * 255.
49 140, // Normal state, opacity 55%, alpha would be 0.55 * 255.
50 178 // Pressed state, opacity 70%, alpha would be 0.7 * 255.
53 const uint8 kScrollbarOverlayThumbStrokeAlphas
[NativeTheme::kNumStates
] = {
54 0, // Does not matter, will not paint for disabled state.
55 51, // Hover state, opacity 20%, alpha would be 0.2 * 255.
56 38, // Normal state, opacity 15%, alpha would be 0.15 * 255.
57 51 // Pressed state, opacity 20%, alpha would be 0.2 * 255.
60 const int kScrollbarOverlayThumbStrokeImages
[9] =
61 IMAGE_GRID_NO_CENTER(IDR_SCROLLBAR_OVERLAY_THUMB_STROKE
);
63 const int kScrollbarOverlayThumbFillImages
[9] =
64 IMAGE_GRID(IDR_SCROLLBAR_OVERLAY_THUMB_FILL
);
66 const int kScrollbarTrackImages
[9] = IMAGE_GRID(IDR_SCROLLBAR_BASE
);
72 NativeTheme
* NativeTheme::instance() {
73 return NativeThemeAura::instance();
77 NativeThemeAura
* NativeThemeAura::instance() {
78 CR_DEFINE_STATIC_LOCAL(NativeThemeAura
, s_native_theme
, ());
79 return &s_native_theme
;
83 NativeThemeAura::NativeThemeAura() {
84 // We don't draw scrollbar buttons.
85 #if defined(OS_CHROMEOS)
86 set_scrollbar_button_length(0);
89 // Images and alphas declarations assume the following order.
90 static_assert(kDisabled
== 0, "states unexpectedly changed");
91 static_assert(kHovered
== 1, "states unexpectedly changed");
92 static_assert(kNormal
== 2, "states unexpectedly changed");
93 static_assert(kPressed
== 3, "states unexpectedly changed");
96 NativeThemeAura::~NativeThemeAura() {
99 void NativeThemeAura::PaintMenuPopupBackground(
101 const gfx::Size
& size
,
102 const MenuBackgroundExtraParams
& menu_background
) const {
103 SkColor color
= GetSystemColor(NativeTheme::kColorId_MenuBackgroundColor
);
104 if (menu_background
.corner_radius
> 0) {
106 paint
.setStyle(SkPaint::kFill_Style
);
107 paint
.setFlags(SkPaint::kAntiAlias_Flag
);
108 paint
.setColor(color
);
111 SkRect rect
= SkRect::MakeWH(SkIntToScalar(size
.width()),
112 SkIntToScalar(size
.height()));
113 SkScalar radius
= SkIntToScalar(menu_background
.corner_radius
);
114 SkScalar radii
[8] = {radius
, radius
, radius
, radius
,
115 radius
, radius
, radius
, radius
};
116 path
.addRoundRect(rect
, radii
);
118 canvas
->drawPath(path
, paint
);
120 canvas
->drawColor(color
, SkXfermode::kSrc_Mode
);
124 void NativeThemeAura::PaintMenuItemBackground(
127 const gfx::Rect
& rect
,
128 const MenuListExtraParams
& menu_list
) const {
129 CommonThemePaintMenuItemBackground(canvas
, state
, rect
);
132 void NativeThemeAura::PaintArrowButton(
134 const gfx::Rect
& rect
,
137 if (direction
== kInnerSpinButton
) {
138 FallbackTheme::PaintArrowButton(gc
, rect
, direction
, state
);
141 PaintPainter(GetOrCreatePainter(
142 kScrollbarArrowButtonImages
, state
,
143 scrollbar_arrow_button_painters_
),
146 // Aura-win uses slightly different arrow colors.
147 SkColor arrow_color
= GetArrowColor(state
);
151 arrow_color
= SkColorSetRGB(0x50, 0x50, 0x50);
154 arrow_color
= SK_ColorWHITE
;
158 PaintArrow(gc
, rect
, direction
, arrow_color
);
161 void NativeThemeAura::PaintScrollbarTrack(
165 const ScrollbarTrackExtraParams
& extra_params
,
166 const gfx::Rect
& rect
) const {
167 // Overlay Scrollbar should never paint a scrollbar track.
168 DCHECK(!IsOverlayScrollbarEnabled());
169 if (!scrollbar_track_painter_
)
170 scrollbar_track_painter_
= CreateNineImagePainter(kScrollbarTrackImages
);
171 PaintPainter(scrollbar_track_painter_
.get(), sk_canvas
, rect
);
174 void NativeThemeAura::PaintScrollbarThumb(SkCanvas
* sk_canvas
,
177 const gfx::Rect
& rect
) const {
178 gfx::Rect
thumb_rect(rect
);
179 if (IsOverlayScrollbarEnabled()) {
180 // Overlay scrollbar has no track, just paint thumb directly.
181 // Do not paint if state is disabled.
182 if (state
== kDisabled
)
185 if (!scrollbar_overlay_thumb_painter_
) {
186 scrollbar_overlay_thumb_painter_
=
187 CreateDualPainter(kScrollbarOverlayThumbFillImages
,
188 kScrollbarOverlayThumbFillAlphas
,
189 kScrollbarOverlayThumbStrokeImages
,
190 kScrollbarOverlayThumbStrokeAlphas
);
194 scrollbar_overlay_thumb_painter_
.get(), sk_canvas
, thumb_rect
, state
);
197 // If there are no scrollbuttons then provide some padding so that thumb
198 // doesn't touch the top of the track.
199 const int extra_padding
= (scrollbar_button_length() == 0) ? 2 : 0;
200 if (part
== NativeTheme::kScrollbarVerticalThumb
)
201 thumb_rect
.Inset(2, extra_padding
, 2, extra_padding
);
203 thumb_rect
.Inset(extra_padding
, 2, extra_padding
, 2);
204 PaintPainter(GetOrCreatePainter(
205 kScrollbarThumbImages
, state
, scrollbar_thumb_painters_
),
210 void NativeThemeAura::PaintScrollbarThumbStateTransition(
215 const gfx::Rect
& rect
) const {
216 // Only Overlay scrollbars should have state transition animation.
217 DCHECK(IsOverlayScrollbarEnabled());
218 if (!scrollbar_overlay_thumb_painter_
) {
219 scrollbar_overlay_thumb_painter_
=
220 CreateDualPainter(kScrollbarOverlayThumbFillImages
,
221 kScrollbarOverlayThumbFillAlphas
,
222 kScrollbarOverlayThumbStrokeImages
,
223 kScrollbarOverlayThumbStrokeAlphas
);
226 PaintDualPainterTransition(scrollbar_overlay_thumb_painter_
.get(),
234 void NativeThemeAura::PaintScrollbarCorner(SkCanvas
* canvas
,
236 const gfx::Rect
& rect
) const {
237 // Overlay Scrollbar should never paint a scrollbar corner.
238 DCHECK(!IsOverlayScrollbarEnabled());
240 paint
.setColor(SkColorSetRGB(0xF1, 0xF1, 0xF1));
241 paint
.setStyle(SkPaint::kFill_Style
);
242 paint
.setXfermodeMode(SkXfermode::kSrc_Mode
);
243 canvas
->drawIRect(RectToSkIRect(rect
), paint
);
246 NineImagePainter
* NativeThemeAura::GetOrCreatePainter(
247 const int images
[kNumStates
][9],
249 scoped_ptr
<NineImagePainter
> painters
[kNumStates
]) const {
251 return painters
[state
].get();
252 if (images
[state
][0] == 0) {
253 // Must always provide normal state images.
254 DCHECK_NE(kNormal
, state
);
255 return GetOrCreatePainter(images
, kNormal
, painters
);
257 painters
[state
] = CreateNineImagePainter(images
[state
]);
258 return painters
[state
].get();
261 void NativeThemeAura::PaintPainter(NineImagePainter
* painter
,
263 const gfx::Rect
& rect
) const {
265 scoped_ptr
<gfx::Canvas
> canvas(CommonThemeCreateCanvas(sk_canvas
));
266 painter
->Paint(canvas
.get(), rect
);
269 scoped_ptr
<NativeThemeAura::DualPainter
> NativeThemeAura::CreateDualPainter(
270 const int fill_image_ids
[9],
271 const uint8 fill_alphas
[kNumStates
],
272 const int stroke_image_ids
[9],
273 const uint8 stroke_alphas
[kNumStates
]) const {
274 scoped_ptr
<NativeThemeAura::DualPainter
> dual_painter(
275 new NativeThemeAura::DualPainter(CreateNineImagePainter(fill_image_ids
),
277 CreateNineImagePainter(stroke_image_ids
),
279 return dual_painter
.Pass();
282 void NativeThemeAura::PaintDualPainter(
283 NativeThemeAura::DualPainter
* dual_painter
,
285 const gfx::Rect
& rect
,
287 DCHECK(dual_painter
);
288 scoped_ptr
<gfx::Canvas
> canvas(CommonThemeCreateCanvas(sk_canvas
));
289 dual_painter
->fill_painter
->Paint(
290 canvas
.get(), rect
, dual_painter
->fill_alphas
[state
]);
291 dual_painter
->stroke_painter
->Paint(
292 canvas
.get(), rect
, dual_painter
->stroke_alphas
[state
]);
295 void NativeThemeAura::PaintDualPainterTransition(
296 NativeThemeAura::DualPainter
* dual_painter
,
298 const gfx::Rect
& rect
,
301 double progress
) const {
302 DCHECK(dual_painter
);
303 scoped_ptr
<gfx::Canvas
> canvas(CommonThemeCreateCanvas(sk_canvas
));
304 uint8 fill_alpha
= dual_painter
->fill_alphas
[startState
] +
305 (dual_painter
->fill_alphas
[endState
] -
306 dual_painter
->fill_alphas
[startState
]) *
308 uint8 stroke_alpha
= dual_painter
->stroke_alphas
[startState
] +
309 (dual_painter
->stroke_alphas
[endState
] -
310 dual_painter
->stroke_alphas
[startState
]) *
313 dual_painter
->fill_painter
->Paint(canvas
.get(), rect
, fill_alpha
);
314 dual_painter
->stroke_painter
->Paint(canvas
.get(), rect
, stroke_alpha
);
317 NativeThemeAura::DualPainter::DualPainter(
318 scoped_ptr
<NineImagePainter
> fill_painter
,
319 const uint8 fill_alphas
[kNumStates
],
320 scoped_ptr
<NineImagePainter
> stroke_painter
,
321 const uint8 stroke_alphas
[kNumStates
])
322 : fill_painter(fill_painter
.Pass()),
323 fill_alphas(fill_alphas
),
324 stroke_painter(stroke_painter
.Pass()),
325 stroke_alphas(stroke_alphas
) {}
327 NativeThemeAura::DualPainter::~DualPainter() {}